forked from nowrep/dualsensectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1400 lines (1239 loc) · 50.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* HID driver for Sony DualSense(TM) controller.
*
* Copyright (c) 2020-2023 Sony Interactive Entertainment
*/
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <poll.h>
#include <sys/wait.h>
#include <dbus/dbus.h>
#include <hidapi/hidapi.h>
#include <libudev.h>
#include "crc32.h"
#define DS_VENDOR_ID 0x054c
#define DS_PRODUCT_ID 0x0ce6
/* Seed values for DualShock4 / DualSense CRC32 for different report types. */
#define PS_INPUT_CRC32_SEED 0xA1
#define PS_OUTPUT_CRC32_SEED 0xA2
#define PS_FEATURE_CRC32_SEED 0xA3
#define DS_INPUT_REPORT_USB 0x01
#define DS_INPUT_REPORT_USB_SIZE 64
#define DS_INPUT_REPORT_BT 0x31
#define DS_INPUT_REPORT_BT_SIZE 78
#define DS_OUTPUT_REPORT_USB 0x02
#define DS_OUTPUT_REPORT_USB_SIZE 63
#define DS_OUTPUT_REPORT_BT 0x31
#define DS_OUTPUT_REPORT_BT_SIZE 78
#define DS_FEATURE_REPORT_CALIBRATION 0x05
#define DS_FEATURE_REPORT_CALIBRATION_SIZE 41
#define DS_FEATURE_REPORT_PAIRING_INFO 0x09
#define DS_FEATURE_REPORT_PAIRING_INFO_SIZE 20
#define DS_FEATURE_REPORT_FIRMWARE_INFO 0x20
#define DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE 64
/* Magic value required in tag field of Bluetooth output report. */
#define DS_OUTPUT_TAG 0x10
/* Flags for DualSense output report. */
#define BIT(n) (1 << n)
#define DS_OUTPUT_VALID_FLAG0_COMPATIBLE_VIBRATION BIT(0)
#define DS_OUTPUT_VALID_FLAG0_HAPTICS_SELECT BIT(1)
#define DS_OUTPUT_VALID_FLAG0_RIGHT_TRIGGER_MOTOR_ENABLE BIT(2)
#define DS_OUTPUT_VALID_FLAG0_LEFT_TRIGGER_MOTOR_ENABLE BIT(3)
#define DS_OUTPUT_VALID_FLAG0_HEADPHONE_VOLUME_ENABLE BIT(4)
#define DS_OUTPUT_VALID_FLAG0_SPEAKER_VOLUME_ENABLE BIT(5)
#define DS_OUTPUT_VALID_FLAG0_MICROPHONE_VOLUME_ENABLE BIT(6)
#define DS_OUTPUT_VALID_FLAG0_AUDIO_CONTROL_ENABLE BIT(7)
#define DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE BIT(0)
#define DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE BIT(1)
#define DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE BIT(2)
#define DS_OUTPUT_VALID_FLAG1_RELEASE_LEDS BIT(3)
#define DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE BIT(4)
#define DS_OUTPUT_VALID_FLAG1_VIBRATION_ATTENUATION_ENABLE BIT(6)
#define DS_OUTPUT_VALID_FLAG1_AUDIO_CONTROL2_ENABLE BIT(7)
#define DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE BIT(1)
#define DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE BIT(4)
#define DS_OUTPUT_POWER_SAVE_CONTROL_AUDIO_MUTE BIT(5)
#define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_ON BIT(0)
#define DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT BIT(1)
/* audio control flags */
#define DS_OUTPUT_AUDIO_FLAG_FORCE_INTERNAL_MIC BIT(0)
#define DS_OUTPUT_AUDIO_FLAG_FORCE_HEADSET_MIC BIT(1)
#define DS_OUTPUT_AUDIO_FLAG_ECHO_CANCEL BIT(2)
#define DS_OUTPUT_AUDIO_FLAG_NOISE_CANCEL BIT(3)
#define DS_OUTPUT_AUDIO_OUTPUT_PATH_SHIFT 4
#define DS_OUTPUT_AUDIO_FLAG_DISABLE_HEADPHONE BIT(4)
#define DS_OUTPUT_AUDIO_FLAG_ENABLE_INTERNAL_SPEAKER BIT(5)
/* audio control2 flags */
#define DS_OUTPUT_AUDIO2_FLAG_BEAM_FORMING BIT(4)
/* Status field of DualSense input report. */
#define DS_STATUS_BATTERY_CAPACITY 0xF
#define DS_STATUS_CHARGING 0xF0
#define DS_STATUS_CHARGING_SHIFT 4
#define DS_TRIGGER_EFFECT_OFF 0x05
#define DS_TRIGGER_EFFECT_FEEDBACK 0x21
#define DS_TRIGGER_EFFECT_BOW 0x22
#define DS_TRIGGER_EFFECT_GALLOPING 0x23
#define DS_TRIGGER_EFFECT_WEAPON 0x25
#define DS_TRIGGER_EFFECT_VIBRATION 0x26
#define DS_TRIGGER_EFFECT_MACHINE 0x27
struct dualsense_touch_point {
uint8_t contact;
uint8_t x_lo;
uint8_t x_hi:4, y_lo:4;
uint8_t y_hi;
} __attribute__((packed));
/* Main DualSense input report excluding any Bluetooth/USB specific headers. */
struct dualsense_input_report {
uint8_t x, y;
uint8_t rx, ry;
uint8_t z, rz;
uint8_t seq_number;
uint8_t buttons[4];
uint8_t reserved[4];
/* Motion sensors */
uint16_t gyro[3]; /* x, y, z */
uint16_t accel[3]; /* x, y, z */
uint32_t sensor_timestamp;
uint8_t reserved2;
/* Touchpad */
struct dualsense_touch_point points[2];
uint8_t reserved3[12];
uint8_t status;
uint8_t reserved4[10];
} __attribute__((packed));
/* Common data between DualSense Bluetooth/USB main output report. */
struct dualsense_output_report_common {
uint8_t valid_flag0;
uint8_t valid_flag1;
/* For DualShock 4 compatibility mode. */
uint8_t motor_right;
uint8_t motor_left;
/* Audio controls */
uint8_t headphone_audio_volume; /* 0-0x7f */
uint8_t speaker_audio_volume; /* 0-255 */
uint8_t internal_microphone_volume; /* 0-0x40 */
uint8_t audio_flags;
uint8_t mute_button_led;
uint8_t power_save_control;
/* right trigger motor */
uint8_t right_trigger_motor_mode;
uint8_t right_trigger_param[10];
/* right trigger motor */
uint8_t left_trigger_motor_mode;
uint8_t left_trigger_param[10];
uint8_t reserved2[4];
uint8_t reduce_motor_power;
uint8_t audio_flags2; /* 3 first bits: speaker pre-gain */
/* LEDs and lightbar */
uint8_t valid_flag2;
uint8_t reserved3[2];
uint8_t lightbar_setup;
uint8_t led_brightness;
uint8_t player_leds;
uint8_t lightbar_red;
uint8_t lightbar_green;
uint8_t lightbar_blue;
} __attribute__((packed));
_Static_assert(sizeof(struct dualsense_output_report_common) == 47, "Bad output report structure size");
struct dualsense_output_report_bt {
uint8_t report_id; /* 0x31 */
uint8_t seq_tag;
uint8_t tag;
struct dualsense_output_report_common common;
uint8_t reserved[24];
uint32_t crc32;
} __attribute__((packed));
struct dualsense_output_report_usb {
uint8_t report_id; /* 0x02 */
struct dualsense_output_report_common common;
uint8_t reserved[15];
} __attribute__((packed));
/*
* The DualSense has a main output report used to control most features. It is
* largely the same between Bluetooth and USB except for different headers and CRC.
* This structure hide the differences between the two to simplify sending output reports.
*/
struct dualsense_output_report {
uint8_t *data; /* Start of data */
uint8_t len; /* Size of output report */
/* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
struct dualsense_output_report_bt *bt;
/* Points to USB data payload in case for a USB report else NULL. */
struct dualsense_output_report_usb *usb;
/* Points to common section of report, so past any headers. */
struct dualsense_output_report_common *common;
};
struct dualsense_feature_report_firmware {
uint8_t report_id; // 0x20
char build_date[11]; // string
char build_time[8]; // string
uint16_t fw_type;
uint16_t sw_series;
uint32_t hardware_info; // 0x00FF0000 - Variation
// 0x0000FF00 - Generation
// 0x0000003F - Trial?
// ^ Values tied to enumerations
uint32_t firmware_version; // 0xAABBCCCC AA.BB.CCCC
char device_info[12];
////
uint16_t update_version;
char update_image_info;
char update_unk;
////
uint32_t fw_version_1; // AKA SblFwVersion
// 0xAABBCCCC AA.BB.CCCC
// Ignored for fw_type 0
// HardwareVersion used for fw_type 1
// Unknown behavior if HardwareVersion < 0.1.38 for fw_type 2 & 3
// If HardwareVersion >= 0.1.38 for fw_type 2 & 3
uint32_t fw_version_2; // AKA VenomFwVersion
uint32_t fw_version_3; // AKA SpiderDspFwVersion AKA BettyFwVer
// May be Memory Control Unit for Non Volatile Storage
uint32_t crc32;
};
_Static_assert(sizeof(struct dualsense_feature_report_firmware) == DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE, "Bad feature report firmware structure size");
struct dualsense {
bool bt;
hid_device *dev;
char mac_address[18];
uint8_t output_seq;
};
static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp, void *buf)
{
if (ds->bt) {
struct dualsense_output_report_bt *bt = buf;
memset(bt, 0, sizeof(*bt));
bt->report_id = DS_OUTPUT_REPORT_BT;
bt->tag = DS_OUTPUT_TAG; /* Tag must be set. Exact meaning is unclear. */
/*
* Highest 4-bit is a sequence number, which needs to be increased
* every report. Lowest 4-bit is tag and can be zero for now.
*/
bt->seq_tag = (ds->output_seq << 4) | 0x0;
if (++ds->output_seq == 16)
ds->output_seq = 0;
rp->data = buf;
rp->len = sizeof(*bt);
rp->bt = bt;
rp->usb = NULL;
rp->common = &bt->common;
} else { /* USB */
struct dualsense_output_report_usb *usb = buf;
memset(usb, 0, sizeof(*usb));
usb->report_id = DS_OUTPUT_REPORT_USB;
rp->data = buf;
rp->len = sizeof(*usb);
rp->bt = NULL;
rp->usb = usb;
rp->common = &usb->common;
}
}
static void dualsense_send_output_report(struct dualsense *ds, struct dualsense_output_report *report)
{
/* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
if (report->bt) {
uint32_t crc;
uint8_t seed = PS_OUTPUT_CRC32_SEED;
crc = crc32_le(0xFFFFFFFF, &seed, 1);
crc = ~crc32_le(crc, report->data, report->len - 4);
report->bt->crc32 = crc;
}
int res = hid_write(ds->dev, report->data, report->len);
if (res < 0) {
fprintf(stderr, "Error: %ls\n", hid_error(ds->dev));
}
}
static bool compare_serial(const char *s, const wchar_t *dev)
{
if (!s) {
return true;
}
const size_t len = wcslen(dev);
if (strlen(s) != len) {
return false;
}
for (size_t i = 0; i < len; ++i) {
if (s[i] != dev[i]) {
return false;
}
}
return true;
}
static bool dualsense_init(struct dualsense *ds, const char *serial)
{
bool ret = false;
memset(ds, 0, sizeof(*ds));
bool found = false;
struct hid_device_info *devs = hid_enumerate(DS_VENDOR_ID, DS_PRODUCT_ID);
struct hid_device_info *dev = devs;
while (dev) {
if (compare_serial(serial, dev->serial_number)) {
found = true;
break;
}
dev = dev->next;
}
if (!found) {
if (serial) {
fprintf(stderr, "Device '%s' not found\n", serial);
} else {
fprintf(stderr, "No device found\n");
}
ret = false;
goto out;
}
ds->dev = hid_open(DS_VENDOR_ID, DS_PRODUCT_ID, dev->serial_number);
if (!ds->dev) {
fprintf(stderr, "Failed to open device: %ls\n", hid_error(NULL));
ret = false;
goto out;
}
wchar_t *serial_number = dev->serial_number;
if (wcslen(serial_number) != 17) {
fprintf(stderr, "Invalid device serial number: %ls\n", serial_number);
// Let's just fake serial number as everything except disconnecting will still work
serial_number = L"00:00:00:00:00:00";
}
for (int i = 0; i < 18; ++i) {
char c = serial_number[i];
if (c && (i + 1) % 3) {
c = toupper(c);
}
ds->mac_address[i] = c;
}
ds->bt = dev->interface_number == -1;
ret = true;
out:
if (devs) {
hid_free_enumeration(devs);
}
return ret;
}
static void dualsense_destroy(struct dualsense *ds)
{
hid_close(ds->dev);
}
static bool dualsense_bt_disconnect(struct dualsense *ds)
{
DBusError err;
dbus_error_init(&err);
DBusConnection *conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Failed to connect to DBus daemon: %s %s\n", err.name, err.message);
return false;
}
DBusMessage *msg = dbus_message_new_method_call("org.bluez", "/", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
DBusMessage *reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
dbus_message_unref(msg);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Failed to enumerate Bluetooth devices: %s %s\n", err.name, err.message);
return false;
}
DBusMessageIter dict;
dbus_message_iter_init(reply, &dict);
int objects_count = dbus_message_iter_get_element_count(&dict);
DBusMessageIter dict_entry;
dbus_message_iter_recurse(&dict, &dict_entry);
DBusMessageIter dict_kv;
char *ds_path = NULL;
char *path, *iface, *prop;
while (objects_count-- && !ds_path) {
dbus_message_iter_recurse(&dict_entry, &dict_kv);
dbus_message_iter_get_basic(&dict_kv, &path);
dbus_message_iter_next(&dict_kv);
int ifaces_count = dbus_message_iter_get_element_count(&dict_kv);
DBusMessageIter ifacedict_entry, ifacedict_kv;
dbus_message_iter_recurse(&dict_kv, &ifacedict_entry);
while (ifaces_count-- && !ds_path) {
dbus_message_iter_recurse(&ifacedict_entry, &ifacedict_kv);
dbus_message_iter_get_basic(&ifacedict_kv, &iface);
if (!strcmp(iface, "org.bluez.Device1")) {
dbus_message_iter_next(&ifacedict_kv);
int props_count = dbus_message_iter_get_element_count(&ifacedict_kv);
DBusMessageIter propdict_entry, propdict_kv;
dbus_message_iter_recurse(&ifacedict_kv, &propdict_entry);
char *address = NULL;
int connected = 0;
while (props_count-- && !ds_path) {
dbus_message_iter_recurse(&propdict_entry, &propdict_kv);
dbus_message_iter_get_basic(&propdict_kv, &prop);
DBusMessageIter variant;
if (!strcmp(prop, "Address")) {
dbus_message_iter_next(&propdict_kv);
dbus_message_iter_recurse(&propdict_kv, &variant);
dbus_message_iter_get_basic(&variant, &address);
} else if (!strcmp(prop, "Connected")) {
dbus_message_iter_next(&propdict_kv);
dbus_message_iter_recurse(&propdict_kv, &variant);
dbus_message_iter_get_basic(&variant, &connected);
}
dbus_message_iter_next(&propdict_entry);
}
if (connected && address && !strcmp(address, ds->mac_address) && !ds_path) {
ds_path = path;
}
}
dbus_message_iter_next(&ifacedict_entry);
}
dbus_message_iter_next(&dict_entry);
}
dbus_message_unref(reply);
if (!ds_path) {
fprintf(stderr, "Failed to find Bluetooth device\n");
return false;
}
msg = dbus_message_new_method_call("org.bluez", ds_path, "org.bluez.Device1", "Disconnect");
reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
dbus_message_unref(msg);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Failed to disconnect Bluetooth device: %s %s\n", err.name, err.message);
return false;
}
dbus_message_unref(reply);
dbus_connection_unref(conn);
return true;
}
static int command_power_off(struct dualsense *ds)
{
if (!ds->bt) {
fprintf(stderr, "Controller is not connected via Bluetooth\n");
return 1;
}
if (!dualsense_bt_disconnect(ds)) {
return 2;
}
return 0;
}
static int command_battery(struct dualsense *ds)
{
uint8_t data[DS_INPUT_REPORT_BT_SIZE];
int res = hid_read_timeout(ds->dev, data, sizeof(data), 1000);
if (res <= 0) {
if (res == 0) {
fprintf(stderr, "Timeout waiting for report\n");
} else {
fprintf(stderr, "Failed to read report %ls\n", hid_error(ds->dev));
}
return 2;
}
struct dualsense_input_report *ds_report;
if (!ds->bt && data[0] == DS_INPUT_REPORT_USB && res == DS_INPUT_REPORT_USB_SIZE) {
ds_report = (struct dualsense_input_report *)&data[1];
} else if (ds->bt && data[0] == DS_INPUT_REPORT_BT && res == DS_INPUT_REPORT_BT_SIZE) {
/* Last 4 bytes of input report contain crc32 */
/* uint32_t report_crc = *(uint32_t*)&data[res - 4]; */
ds_report = (struct dualsense_input_report *)&data[2];
} else {
fprintf(stderr, "Unhandled report ID %d\n", (int)data[0]);
return 3;
}
const char *battery_status;
uint8_t battery_capacity;
uint8_t battery_data = ds_report->status & DS_STATUS_BATTERY_CAPACITY;
uint8_t charging_status = (ds_report->status & DS_STATUS_CHARGING) >> DS_STATUS_CHARGING_SHIFT;
#define min(a, b) ((a) < (b) ? (a) : (b))
switch (charging_status) {
case 0x0:
/*
* Each unit of battery data corresponds to 10%
* 0 = 0-9%, 1 = 10-19%, .. and 10 = 100%
*/
battery_capacity = min(battery_data * 10 + 5, 100);
battery_status = "DISCHARGING";
break;
case 0x1:
battery_capacity = min(battery_data * 10 + 5, 100);
battery_status = "CHARGING";
break;
case 0x2:
battery_capacity = 100;
battery_status = "FULL";
break;
case 0xa: /* voltage or temperature out of range */
case 0xb: /* temperature error */
battery_capacity = 0;
battery_status = "NOT-CHARGING";
break;
case 0xf: /* charging error */
default:
battery_capacity = 0;
battery_status = "UNKNOWN";
}
#undef min
printf("%d %s\n", (int)battery_capacity, battery_status);
return 0;
}
static int command_info(struct dualsense *ds)
{
uint8_t buf[DS_FEATURE_REPORT_FIRMWARE_INFO_SIZE];
memset(buf, 0, sizeof(buf));
buf[0] = DS_FEATURE_REPORT_FIRMWARE_INFO;
int res = hid_get_feature_report(ds->dev, buf, sizeof(buf));
if (res != sizeof(buf)) {
fprintf(stderr, "Invalid feature report\n");
return false;
}
struct dualsense_feature_report_firmware *ds_report;
ds_report = (struct dualsense_feature_report_firmware *)&buf;
printf("hardware: %x,\tfirmware: %x\n", ds_report->hardware_info, ds_report->firmware_version);
printf("build_date: %.11s,\tbuild_time: %.8s\n", ds_report->build_date, ds_report->build_time);
printf("fw_type: %i,\tsw_series %i\n", ds_report->fw_type, ds_report->sw_series);
/*printf("device_info: %.12s,\tupdate_version: %i\n", ds_report->device_info, ds_report->update_version);
printf("update_image_info: %c,\tupdate_unk: %c\n", ds_report->update_image_info, ds_report->update_unk);
printf("fw_version_1: %i,\tfw_version_2: %i,\tfw_version_3: %i\n", ds_report->fw_version_1, ds_report->fw_version_2, ds_report->fw_version_3);
*/
return 0;
}
static int command_lightbar1(struct dualsense *ds, char *state)
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
rp.common->valid_flag2 = DS_OUTPUT_VALID_FLAG2_LIGHTBAR_SETUP_CONTROL_ENABLE;
if (!strcmp(state, "ON")) {
rp.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_ON;
} else if (!strcmp(state, "OFF")) {
rp.common->lightbar_setup = DS_OUTPUT_LIGHTBAR_SETUP_LIGHT_OUT;
} else {
fprintf(stderr, "Invalid state\n");
return 1;
}
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_lightbar3(struct dualsense *ds, uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness)
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
uint8_t max_brightness = 255;
rp.common->valid_flag1 = DS_OUTPUT_VALID_FLAG1_LIGHTBAR_CONTROL_ENABLE;
rp.common->lightbar_red = brightness * red / max_brightness;
rp.common->lightbar_green = brightness * green / max_brightness;
rp.common->lightbar_blue = brightness * blue / max_brightness;
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_player_leds(struct dualsense *ds, uint8_t number)
{
if (number > 5) {
fprintf(stderr, "Invalid player number\n");
return 1;
}
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
static const int player_ids[6] = {
0,
BIT(2),
BIT(3) | BIT(1),
BIT(4) | BIT(2) | BIT(0),
BIT(4) | BIT(3) | BIT(1) | BIT(0),
BIT(4) | BIT(3) | BIT(2) | BIT(1) | BIT(0)
};
rp.common->valid_flag1 = DS_OUTPUT_VALID_FLAG1_PLAYER_INDICATOR_CONTROL_ENABLE;
rp.common->player_leds = player_ids[number];
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_microphone(struct dualsense *ds, char *state)
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
rp.common->valid_flag1 = DS_OUTPUT_VALID_FLAG1_POWER_SAVE_CONTROL_ENABLE;
if (!strcmp(state, "ON")) {
rp.common->power_save_control &= ~DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
} else if (!strcmp(state, "OFF")) {
rp.common->power_save_control |= DS_OUTPUT_POWER_SAVE_CONTROL_MIC_MUTE;
} else {
fprintf(stderr, "Invalid state\n");
return 1;
}
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_microphone_led(struct dualsense *ds, char *state)
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
rp.common->valid_flag1 = DS_OUTPUT_VALID_FLAG1_MIC_MUTE_LED_CONTROL_ENABLE;
if (!strcmp(state, "ON")) {
rp.common->mute_button_led = 1;
} else if (!strcmp(state, "OFF")) {
rp.common->mute_button_led = 0;
} else {
fprintf(stderr, "Invalid state\n");
return 1;
}
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_speaker(struct dualsense *ds, char *state)
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
rp.common->valid_flag0 = DS_OUTPUT_VALID_FLAG0_AUDIO_CONTROL_ENABLE;
/* value
* | /left headphone
* | | / right headphone
* | | | / internal speaker
* 0 L_R_X
* 1 L_L_X
* 2 L_L_R
* 3 X_X_R
*/
if (!strcmp(state, "INTERNAL")) { /* right channel to speaker */
rp.common->audio_flags = 3 << DS_OUTPUT_AUDIO_OUTPUT_PATH_SHIFT;
} else if (!strcmp(state, "HEADPHONE")) { /* stereo channel to headphone */
rp.common->audio_flags = 0;
} else if (!strcmp(state, "MONOHEADPHONE")) { /* left channel to headphone */
rp.common->audio_flags = 1 << DS_OUTPUT_AUDIO_OUTPUT_PATH_SHIFT;
} else if (!strcmp(state, "BOTH")) { /* left channel to headphone, right channel to speaker */
rp.common->audio_flags = 2 << DS_OUTPUT_AUDIO_OUTPUT_PATH_SHIFT;
} else {
fprintf(stderr, "Invalid state\n");
return 1;
}
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_volume(struct dualsense *ds, uint8_t volume)
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
uint8_t max_volume = 255;
/* TODO see if we can get old values of volumes to be able to set values independently */
rp.common->valid_flag0 = DS_OUTPUT_VALID_FLAG0_HEADPHONE_VOLUME_ENABLE;
rp.common->headphone_audio_volume = volume * 0x7f / max_volume;
rp.common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_SPEAKER_VOLUME_ENABLE;
/* the PS5 use 0x3d-0x64 trying over 0x64 doesnt change but below 0x3d can still lower the volume */
rp.common->speaker_audio_volume = volume * 0x64 / max_volume;
/* if we want to set speaker pre gain */
//rp.common->valid_flag1 = DS_OUTPUT_VALID_FLAG1_AUDIO_CONTROL2_ENABLE;
//rp.common->audio_flags2 = 4;
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_vibration_attenuation(struct dualsense *ds, uint8_t rumble_attenuation, uint8_t trigger_attenuation)
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
/* need to store or get current values if we want to change motor/haptic and trigger separately */
rp.common->valid_flag1 = DS_OUTPUT_VALID_FLAG1_VIBRATION_ATTENUATION_ENABLE;
rp.common->reduce_motor_power = (uint8_t)((rumble_attenuation & 0x07) | ((trigger_attenuation & 0x07) << 4 ));
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_trigger(struct dualsense *ds, char *trigger, uint8_t mode, uint8_t param1, uint8_t param2, uint8_t param3, uint8_t param4, uint8_t param5, uint8_t param6, uint8_t param7, uint8_t param8, uint8_t param9 )
{
struct dualsense_output_report rp;
uint8_t rbuf[DS_OUTPUT_REPORT_BT_SIZE];
dualsense_init_output_report(ds, &rp, rbuf);
if (!strcmp(trigger, "RIGHT") || !strcmp(trigger, "BOTH")) {
rp.common->valid_flag0 = DS_OUTPUT_VALID_FLAG0_RIGHT_TRIGGER_MOTOR_ENABLE;
}
if (!strcmp(trigger, "LEFT") || !strcmp(trigger, "BOTH")) {
rp.common->valid_flag0 |= DS_OUTPUT_VALID_FLAG0_LEFT_TRIGGER_MOTOR_ENABLE;
}
rp.common->right_trigger_motor_mode = mode;
rp.common->right_trigger_param[0] = param1;
rp.common->right_trigger_param[1] = param2;
rp.common->right_trigger_param[2] = param3;
rp.common->right_trigger_param[3] = param4;
rp.common->right_trigger_param[4] = param5;
rp.common->right_trigger_param[5] = param6;
rp.common->right_trigger_param[6] = param7;
rp.common->right_trigger_param[7] = param8;
rp.common->right_trigger_param[8] = param9;
rp.common->left_trigger_motor_mode = mode;
rp.common->left_trigger_param[0] = param1;
rp.common->left_trigger_param[1] = param2;
rp.common->left_trigger_param[2] = param3;
rp.common->left_trigger_param[3] = param4;
rp.common->left_trigger_param[4] = param5;
rp.common->left_trigger_param[5] = param6;
rp.common->left_trigger_param[6] = param7;
rp.common->left_trigger_param[7] = param8;
rp.common->left_trigger_param[8] = param9;
dualsense_send_output_report(ds, &rp);
return 0;
}
static int command_trigger_off(struct dualsense *ds, char *trigger)
{
return command_trigger(ds, trigger, DS_TRIGGER_EFFECT_OFF, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
static int trigger_bitpacking_array(struct dualsense *ds, char *trigger, uint8_t mode, uint8_t strength[10], uint8_t frequency)
{
uint32_t strength_zones = 0;
uint16_t active_zones = 0;
for (int i = 0; i < 10; i++) {
if (strength[i] > 8) {
fprintf(stderr, "Strengths must be between 0 and 8\n");
return 1;
}
if (strength[i] > 0) {
uint8_t strength_value = (uint8_t)((strength[i] -1) & 0x07);
strength_zones |= (uint32_t)(strength_value << (3 * i));
active_zones |= (uint16_t)(1 << i);
}
}
return command_trigger(ds, trigger, mode,
(uint8_t)((active_zones >> 0) & 0xff),
(uint8_t)((active_zones >> 8) & 0xff),
(uint8_t)((strength_zones >> 0) & 0xff),
(uint8_t)((strength_zones >> 8) & 0xff),
(uint8_t)((strength_zones >> 16) & 0xff),
(uint8_t)((strength_zones >> 24) & 0xff),
0, 0,
frequency);
}
static int command_trigger_feedback(struct dualsense *ds, char *trigger, uint8_t position, uint8_t strength)
{
if (position > 9) {
fprintf(stderr, "Position must be between 0 and 9\n");
return 1;
}
if (strength > 8 || !(strength > 0)) {
fprintf(stderr, "Strength must be between 1 and 8\n");
return 1;
}
uint8_t strength_array[10] = {0};
for (int i = position; i < 10; i++) {
strength_array[i] = strength;
}
return trigger_bitpacking_array(ds, trigger, DS_TRIGGER_EFFECT_FEEDBACK, strength_array, 0);
}
static int command_trigger_weapon(struct dualsense *ds, char *trigger, uint8_t start_position, uint8_t end_position, uint8_t strength)
{
if (start_position > 7 || start_position < 2) {
fprintf(stderr, "Start position must be between 2 and 7\n");
return 1;
}
if (end_position > 8 || end_position < start_position+1) {
fprintf(stderr, "End position must be between start position+1 and 8\n");
return 1;
}
if (strength > 8 || !(strength > 0)) {
fprintf(stderr, "Strength must be between 1 and 8\n");
return 1;
}
uint16_t start_stop_zones = (uint16_t)((1 << start_position) | (1 << end_position));
return command_trigger(ds, trigger, DS_TRIGGER_EFFECT_WEAPON,
(uint8_t)((start_stop_zones >> 0) & 0xff),
(uint8_t)((start_stop_zones >> 8) & 0xff),
strength-1,
0, 0, 0, 0, 0, 0);
}
static int command_trigger_bow(struct dualsense *ds, char *trigger, uint8_t start_position, uint8_t end_position, uint8_t strength, uint8_t snap_force)
{
if (start_position > 8 || !(start_position > 0)) {
fprintf(stderr, "Start position must be between 0 and 8\n");
return 1;
}
if (end_position > 8 || end_position < start_position+1) {
fprintf(stderr, "End position must be between start position+1 and 8\n");
return 1;
}
if (strength > 8 || !(strength > 0)) {
fprintf(stderr, "Strength must be between 1 and 8\n");
return 1;
}
if (snap_force > 8 || !(snap_force > 0)) {
fprintf(stderr, "SNAP_FORCE must be between 1 and 8\n");
return 1;
}
uint16_t start_stop_zones = (uint16_t)((1 << start_position) | (1 << end_position));
uint32_t force_pair = (uint16_t)(((strength -1) & 0x07) | (((snap_force -1 ) & 0x07) << 3 ));
return command_trigger(ds, trigger, DS_TRIGGER_EFFECT_BOW,
(uint8_t)((start_stop_zones >> 0) & 0xff),
(uint8_t)((start_stop_zones >> 8) & 0xff),
(uint8_t)((force_pair >> 0) & 0xff),
0, 0, 0, 0, 0, 0);
}
static int command_trigger_galloping(struct dualsense *ds, char *trigger, uint8_t start_position, uint8_t end_position, uint8_t first_foot, uint8_t second_foot, uint8_t frequency)
{
if (start_position > 8) {
fprintf(stderr, "Start position must be between 0 and 8\n");
return 1;
}
if (end_position > 9 || end_position < start_position+1) {
fprintf(stderr, "End position must be between start position+1 and 9\n");
return 1;
}
if (first_foot > 6) {
fprintf(stderr, "FIRST_FOOT must be between 0 and 8\n");
return 1;
}
if (second_foot > 7 || second_foot < first_foot+1) {
fprintf(stderr, "SECOND_FOOT must be between first_foot+1 and 8\n");
return 1;
}
if (!(frequency > 0)) {
fprintf(stderr, "Frequency must be greater than 0\n");
return 1;
}
if (frequency > 8) {
fprintf(stdout, "Frequency has a better effect when lower than 8\n");
}
uint16_t start_stop_zones = (uint16_t)((1 << start_position) | (1 << end_position));
uint32_t ratio = (uint16_t)((second_foot & 0x07) | ((first_foot & 0x07) << 3 ));
return command_trigger(ds, trigger, DS_TRIGGER_EFFECT_GALLOPING,
(uint8_t)((start_stop_zones >> 0) & 0xff),
(uint8_t)((start_stop_zones >> 8) & 0xff),
(uint8_t)((ratio >> 0) & 0xff),
frequency,
0, 0, 0, 0, 0);
}
static int command_trigger_machine(struct dualsense *ds, char *trigger, uint8_t start_position, uint8_t end_position, uint8_t strength_a, uint8_t strength_b, uint8_t frequency, uint8_t period)
{
// if start_position == 0 nothing happen
if (start_position > 8 || !(start_position > 0)) {
fprintf(stderr, "Start position must be between 1 and 8\n");
return 1;
}
if (end_position > 9 || end_position < start_position+1) {
fprintf(stderr, "End position must be between start position+1 and 9\n");
return 1;
}
if (strength_a > 7) {
fprintf(stderr, "STRENGHT_A position must be between 0 and 7\n");
return 1;
}
if (strength_b > 7) {
fprintf(stderr, "STRENGHT_B position must be between 0 and 7\n");
return 1;
}
if (!(frequency > 0)) {
fprintf(stderr, "Frequency must be greater than 0\n");
return 1;
}
uint16_t start_stop_zones = (uint16_t)((1 << start_position) | (1 << end_position));
uint32_t force_pair = (uint16_t)((strength_a & 0x07) | ((strength_b & 0x07) << 3 ));
return command_trigger(ds, trigger, DS_TRIGGER_EFFECT_MACHINE,
(uint8_t)((start_stop_zones >> 0) & 0xff),
(uint8_t)((start_stop_zones >> 8) & 0xff),
(uint8_t)((force_pair >> 0) & 0xff),
frequency,
period,
0, 0, 0, 0);
}
static int command_trigger_vibration(struct dualsense *ds, char *trigger, uint8_t position, uint8_t amplitude, uint8_t frequency)
{
if (position > 9) {
fprintf(stderr, "Position must be between 0 and 9\n");
return 1;
}
if (amplitude > 8 || !(amplitude > 0)) {
fprintf(stderr, "Amplitude must be between 1 and 8\n");
return 1;
}
if (!(frequency > 0)) {
fprintf(stderr, "Frequency must be greater than 0\n");
return 1;
}
uint8_t strength_array[10] = {0};
for (int i = position; i < 10; i++) {
strength_array[i] = amplitude;
}
return trigger_bitpacking_array(ds, trigger, DS_TRIGGER_EFFECT_VIBRATION, strength_array, frequency);
}
static int command_trigger_feedback_raw(struct dualsense *ds, char *trigger, uint8_t strength[10] )
{
return trigger_bitpacking_array(ds, trigger, DS_TRIGGER_EFFECT_FEEDBACK, strength, 0);
}
static int command_trigger_vibration_raw(struct dualsense *ds, char *trigger, uint8_t strength[10], uint8_t frequency)
{
return trigger_bitpacking_array(ds, trigger, DS_TRIGGER_EFFECT_VIBRATION, strength, frequency);
}
static bool sh_command_wait = false;
static const char *sh_command_add = NULL;
static const char *sh_command_remove = NULL;
static void run_sh_command(const char *command, const char *serial_number)
{
pid_t pid = fork();
if (pid == 0) {
if (!sh_command_wait) {
pid = fork();
}