-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltunify.c
1422 lines (1279 loc) · 39.7 KB
/
ltunify.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
/*
* Pair, unpair or list information about wireless devices like keyboards and
* mice that use the Logitech® Unifying receiver.
*
* Copyright (C) 2013 Peter Wu <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <stdlib.h> /* strtoul */
#include <stdint.h> /* uint16_t */
#include <arpa/inet.h> /* ntohs, ntohl */
#include <glob.h> /* for /dev/hidrawX discovery */
#include <getopt.h> /* for getopt_long */
#include <poll.h>
#include <libgen.h> /* for basename, used during discovery */
#include <time.h> /* needs -lrt, for clock_gettime as timeout helper */
#include <stdarg.h>
#ifndef PACKAGE_VERSION
# define PACKAGE_VERSION "0.3"
#endif
#define ARRAY_SIZE(a) (sizeof (a) / sizeof *(a))
// pass -D option to print very verbose details like protocol communication
static bool debug_enabled;
#define DPRINTF(...) if (debug_enabled) { fprintf(stderr, __VA_ARGS__); }
typedef unsigned char u8;
#define VID_LOGITECH 0x046d
#define PID_NANO_RECEIVER 0xc52f
#define PID_NANO_RECEIVER_2 0xc534
#define HEADER_SIZE 3
#define SHORT_MESSAGE 0x10
#define SHORT_MESSAGE_LEN 7
#define LONG_MESSAGE 0x11
#define LONG_MESSAGE_LEN 20
#define DEVICE_RECEIVER 0xFF
#define SUB_SET_REGISTER 0x80
#define SUB_GET_REGISTER 0x81
#define SUB_SET_LONG_REGISTER 0x82
#define SUB_GET_LONG_REGISTER 0x83
#define SUB_ERROR_MSG 0x8F
#define NOTIF_DEV_DISCONNECT 0x40 /* Device Disconnection */
#define NOTIF_DEV_CONNECT 0x41 /* Device Connection */
#define NOTIF_RECV_LOCK_CHANGE 0x4A /* Unifying Receiver Locking Change information */
#define REG_ENABLED_NOTIFS 0x00
#define REG_CONNECTION_STATE 0x02
/* Device Connection and Disconnection (Pairing) */
#define REG_DEVICE_PAIRING 0xB2
#define REG_DEVICE_ACTIVITY 0xB3
#define REG_PAIRING_INFO 0xB5
#define REG_VERSION_INFO 0xF1 /* undocumented */
// Used for: {GET,SET}_REGISTER_{REQ,RSP}, SET_LONG_REGISTER_RSP, GET_LONG_REGISTER_REQ
struct msg_short {
u8 address;
u8 value[3];
};
// Used for: SET_LONG_REGISTER_REQ, GET_LONG_REGISTER_RSP
struct msg_long {
u8 address;
u8 str[16];
};
// Used for: ERROR_MSG
struct msg_error {
u8 sub_id;
u8 address;
u8 error_code;
u8 padding; /* set to 0 */
};
// 0x00 Enable HID++ Notifications
struct msg_enable_notifs {
u8 reporting_flags_devices; // bit 4 Battery status
u8 reporting_flags_receiver; // bit 0 Wireless notifications, 3 Software Present
u8 reporting_flags_receiver2; // (reserved)
};
// long receiver resp - 0xB5 Pairing information, 0x03 - "Receiver information"? (undocumented)
struct msg_receiver_info {
u8 _dunno1; // always 0x03 for receiver?
u8 serial_number[4];
u8 _dunno2; // 06 - Max Device Capability? (not sure, but it is six)
u8 _dunno3;
u8 padding[8]; // 00 00 00 00 00 00 00 00 - ??
};
// 0xB5 Pairing information, 0x20..0x2F - Unifying Device pairing information
struct msg_dev_pair_info {
u8 requested_field; // 0x20..0x25
u8 dest_id;
u8 report_interval; // ms
u8 pid_msb;
u8 pid_lsb;
u8 _reserved1[2];
u8 device_type;
u8 _reserved2[6];
};
// 0xB5 Pairing information, 0x30..0x3F - Unifying Device extended pairing info
struct msg_dev_ext_pair_info {
u8 requested_field; // 0x30..0x35
u8 serial_number[4]; // index 0 is MSB
u8 report_types[4]; // index 0 is MSB
u8 usability_info; // bits 0..3 is location of power switch
};
// 0xB5 Pairing information, 0x40..0x4F - Unifying Device name
#define DEVICE_NAME_MAXLEN 14
struct msg_dev_name {
u8 requested_field; // 0x40..0x45
u8 length;
char str[DEVICE_NAME_MAXLEN]; // UTF-8 encoding
};
struct notif_devcon {
#define DEVCON_PROT_UNIFYING 0x04
#define DEVCON_PROT_NANO_LITE 0x0a
u8 prot_type; // bits 0..2 is protocol type (4 for unifying), 3..7 is reserved
#define DEVCON_DEV_TYPE_MASK 0x0f
// Link status: 0 is established (in range), 1 is not established (out of range)
#define DEVCON_LINK_STATUS_FLAG 0x40
u8 device_info;
// wireless product id:
u8 pid_lsb;
u8 pid_msb;
};
// Register 0x02 Connection State
struct val_reg_connection_state {
// 0x02 triggers a 0x41 notification for all known devices
#define CONSTATE_ACTION_LIST_DEVICES 0x02
u8 action; // always 0 for read
u8 connected_devices_count;
u8 _undocumented2;
};
// Register 0xB2 Device Connection and Disconnection (Pairing)
struct val_reg_devpair {
#define DEVPAIR_KEEP_LOCK 0
#define DEVPAIR_OPEN_LOCK 1
#define DEVPAIR_CLOSE_LOCK 2
#define DEVPAIR_DISCONNECT 3
u8 action;
u8 device_number; // same as device index from 0x41 notif
u8 open_lock_timeout; // timeout in seconds, 0 = default (30s)
};
// Register 0xF1 Version Info (undocumented)
struct val_reg_version {
// v1.v2.xxx
#define VERSION_FIRMWARE 1
// x.x.v1v2
#define VERSION_FW_BUILD 2
// value 3 is invalid for receiver, but returns 00 07 for keyboard
// BL.v1.v2
#define VERSION_BOOTLOADER 4
u8 select_field;
u8 v1;
u8 v2;
};
struct hidpp_message {
u8 report_id;
u8 device_index;
u8 sub_id;
union {
struct msg_short msg_short;
struct msg_long msg_long;
struct msg_error msg_error;
};
};
struct hidpp_version {
u8 major;
u8 minor;
};
struct version {
u8 fw_major;
u8 fw_minor;
uint16_t fw_build;
u8 bl_major;
u8 bl_minor;
};
#define DEVICES_MAX 6u
struct device {
bool device_present; // whether the device is paired
bool device_available; // whether the device is connected
u8 device_type;
uint16_t wireless_pid;
char name[DEVICE_NAME_MAXLEN + 1]; // include NUL byte
uint32_t serial_number;
u8 power_switch_location;
struct hidpp_version hidpp_version;
struct version version;
};
struct device devices[DEVICES_MAX];
struct receiver_info {
uint32_t serial_number;
struct version version;
};
struct receiver_info receiver;
// error messages for type=8F (ERROR_MSG)
static const char * error_messages[0x100] = {
[0x00] = "SUCCESS",
[0x01] = "INVALID_SUBID",
[0x02] = "INVALID_ADDRESS",
[0x03] = "INVALID_VALUE",
[0x04] = "CONNECT_FAIL",
[0x05] = "TOO_MANY_DEVICES",
[0x06] = "ALREADY_EXISTS",
[0x07] = "BUSY",
[0x08] = "UNKNOWN_DEVICE",
[0x09] = "RESOURCE_ERROR",
[0x0A] = "REQUEST_UNAVAILABLE",
[0x0B] = "INVALID_PARAM_VALUE",
[0x0C] = "WRONG_PIN_CODE",
};
static const char * device_type[0x10] = {
[0x00] = "Unknown",
[0x01] = "Keyboard",
[0x02] = "Mouse",
[0x03] = "Numpad",
[0x04] = "Presenter",
// 0x05..0x07 Reserved for future
[0x08] = "Trackball",
[0x09] = "Touchpad",
// 0x0A..0x0F Reserved
};
const char *device_type_str(u8 type) {
if (type > 0x0F) {
return "(invalid)";
}
if (device_type[type]) {
return device_type[type];
}
return "(reserved)";
}
// returns device type index or -1 if the string is invalid
int device_type_from_str(const char *str) {
unsigned i;
// skip "Unknown" type
for (i = 1; i < ARRAY_SIZE(device_type); i++) {
if (device_type[i] && !strcasecmp(device_type[i], str)) {
return i;
}
}
return -1;
}
static void print_device_types(void) {
unsigned i;
// skip "Unknown" type
for (i = 1; i < ARRAY_SIZE(device_type); i++) {
if (device_type[i]) {
fprintf(stderr, " %s", device_type[i]);
}
}
putchar('\n');
}
static void dump_msg(struct hidpp_message *msg, size_t payload_size, const char *tag) {
size_t i;
if (!debug_enabled) {
return;
}
// HACK: do not mess with stderr colors
fflush(NULL);
printf("\033[34m");
printf("%s: ", tag);
for (i=0; i<payload_size; i++) {
printf("%02x%c", ((char *) msg)[i] & 0xFF,
i + 1 == payload_size ? '\n' : ' ');
}
printf("\033[m");
fflush(NULL);
}
static long long unsigned get_timestamp_ms(void) {
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
}
#include <execinfo.h>
static ssize_t do_read(int fd, struct hidpp_message *msg, u8 expected_report_id, int timeout) {
ssize_t r;
size_t payload_size = LONG_MESSAGE_LEN;
long long unsigned begin_ms, end_ms;
if (expected_report_id == SHORT_MESSAGE) {
payload_size = SHORT_MESSAGE_LEN;
}
begin_ms = get_timestamp_ms();
while (timeout > 0) {
struct pollfd pollfd;
pollfd.fd = fd;
pollfd.events = POLLIN;
r = poll(&pollfd, 1, timeout);
if (r < 0) {
perror("poll");
return 0;
} else if (r == 0) {
DPRINTF("poll timeout reached!\n");
// timeout
return 0;
}
memset(msg, 0, payload_size);
r = read(fd, msg, payload_size);
if (r < 0) {
perror("read");
return 0;
} else if (r > 0) {
dump_msg(msg, r, "rd");
if (msg->report_id == expected_report_id) {
return r;
} else if (expected_report_id == 0 &&
(msg->report_id == SHORT_MESSAGE ||
msg->report_id == LONG_MESSAGE)) {
/* HACK: ping response for HID++ 2.0 is a LONG
* message, but for HID++ 1.0 it is a SHORT one. */
return r;
} else {
DPRINTF("Skipping unexpected report ID %#x (want %#x)\n",
msg->report_id, expected_report_id);
}
}
/* unexpected message, try again with updated timeout */
end_ms = get_timestamp_ms();
timeout -= end_ms - begin_ms;
begin_ms = end_ms;
}
/* timeout expired, no report found unfortunately */
return 0;
}
static ssize_t do_write(int fd, struct hidpp_message *msg) {
ssize_t r, payload_size = SHORT_MESSAGE_LEN;
if (msg->report_id == LONG_MESSAGE) {
payload_size = LONG_MESSAGE_LEN;
}
dump_msg(msg, payload_size, "wr");
r = write(fd, msg, payload_size);
if (r < 0) {
perror("write");
}
return payload_size == r ? payload_size : 0;
}
const char *get_report_id_str(u8 report_type) {
switch (report_type) {
case SHORT_MESSAGE:
return "short";
case LONG_MESSAGE:
return "long";
default:
return "unkn";
}
}
bool process_notif_dev_connect(struct hidpp_message *msg, u8 *device_index,
bool *is_new_device) {
u8 dev_idx = msg->device_index;
struct notif_devcon *dcon = (struct notif_devcon *) &msg->msg_short;
struct device *dev;
if (msg->sub_id != NOTIF_DEV_CONNECT) {
fprintf(stderr, "Invalid msg type %#0x, expected dev conn notif\n",
msg->sub_id);
return false;
}
if (msg->report_id != SHORT_MESSAGE) {
fprintf(stderr, "Dev conn notif is expected to be short, got "
"%#04x instead\n", msg->report_id);
return false;
}
if (dcon->prot_type != DEVCON_PROT_UNIFYING &&
dcon->prot_type != DEVCON_PROT_NANO_LITE) {
fprintf(stderr, "Unknown protocol %#04x in devcon notif\n",
dcon->prot_type);
return false;
}
if (dev_idx < 1 || dev_idx > DEVICES_MAX) {
fprintf(stderr, "Disallowed device index %#04x\n", dev_idx);
return false;
}
dev = &devices[dev_idx - 1];
if (device_index) *device_index = dev_idx;
if (is_new_device) *is_new_device = !dev->device_present;
memset(dev, 0, sizeof *dev);
dev->device_type = dcon->device_info & DEVCON_DEV_TYPE_MASK;
dev->wireless_pid = (dcon->pid_msb << 8) | dcon->pid_lsb;
dev->device_present = true;
dev->device_available = !(dcon->device_info & DEVCON_LINK_STATUS_FLAG);
return true;
}
// use for reading registers and skipping notifications from return
static bool do_read_skippy(int fd, struct hidpp_message *msg,
u8 exp_report_id, u8 exp_sub_id) {
for (;;) {
if (!do_read(fd, msg, exp_report_id, 2000)) {
return false;
}
if (msg->report_id == exp_report_id && msg->sub_id == exp_sub_id) {
return true;
}
/* ignore non-HID++ reports (e.g. DJ reports) */
if (msg->report_id != SHORT_MESSAGE && msg->report_id != LONG_MESSAGE) {
continue;
}
// guess: 0xFF is error message in HID++ 2.0?
if (msg->report_id == LONG_MESSAGE && msg->sub_id == 0xFF) {
if (debug_enabled) {
fprintf(stderr, "HID++ 2.0 error %#04x\n",
msg->msg_long.str[2]);
}
return false;
}
if (msg->report_id == SHORT_MESSAGE && msg->sub_id == SUB_ERROR_MSG
&& msg->msg_error.sub_id == exp_sub_id) {
struct msg_error *error = &msg->msg_error;
// TODO: consider address (register)?
u8 error_code = error->error_code;
const char *err_str = error_messages[error_code];
if (debug_enabled) {
fprintf(stderr, "Received error for subid=%#04x,"
" reg=%#04x: %#04x (%s)\n", error->sub_id,
error->address, error_code, err_str);
}
return false;
}
if (msg->sub_id == NOTIF_DEV_CONNECT) {
process_notif_dev_connect(msg, NULL, NULL);
continue;
} else if (msg->sub_id == NOTIF_DEV_DISCONNECT) {
u8 device_index = msg->device_index;
u8 disconnect_type = *(u8 *) &msg->msg_short;
if (device_index < 1 || device_index > DEVICES_MAX) {
fprintf(stderr, "Invalid device index %#04x\n", device_index);
} else if (disconnect_type & 0x02) {
memset(&devices[device_index - 1], 0, sizeof *devices);
} else {
fprintf(stderr, "Unexpected disconnection type %#04x\n", disconnect_type);
}
} else if (msg->sub_id == NOTIF_RECV_LOCK_CHANGE) {
if (msg->report_id != SHORT_MESSAGE || msg->device_index != DEVICE_RECEIVER) {
fprintf(stderr, "Received invalid Unifying Receiver Locking Change notification (0x4A)\n");
continue;
}
// TODO: this can be used to make an application aware
// that pairing is (not) possible (anymore)
if (debug_enabled) {
fprintf(stderr, "Receiver lock state is now %s\n",
(*(u8 *)&msg->msg_short) & 1 ? "open" : "closed");
}
}
if (debug_enabled &&
(msg->report_id != exp_report_id || msg->sub_id != exp_sub_id)) {
fprintf(stderr, "Expected %s msg %#02x, got %s msg %#02x\n",
get_report_id_str(exp_report_id), exp_sub_id,
get_report_id_str(msg->report_id), msg->sub_id);
}
// let's try reading another message...
}
return true;
}
// TODO: separate files
#include "hidpp20.c"
static bool set_register(int fd, u8 device_index, u8 address,
u8 *params, struct hidpp_message *res, bool is_long_req) {
u8 exp_sub_id;
struct hidpp_message msg;
msg.device_index = device_index;
if (is_long_req) {
msg.report_id = LONG_MESSAGE;
exp_sub_id = SUB_SET_LONG_REGISTER;
msg.msg_long.address = address;
memcpy(&msg.msg_long.str, params, sizeof msg.msg_long.str);
} else {
msg.report_id = SHORT_MESSAGE;
exp_sub_id = SUB_SET_REGISTER;
msg.msg_short.address = address;
memcpy(&msg.msg_short.value, params, sizeof msg.msg_short.value);
}
msg.sub_id = exp_sub_id;
if (!do_write(fd, &msg)) {
return false;
}
msg.report_id = SHORT_MESSAGE;
if (!do_read_skippy(fd, &msg, SHORT_MESSAGE, exp_sub_id)) {
return false;
}
memcpy(res, &msg, sizeof msg);
return true;
}
bool set_short_register(int fd, u8 device_index, u8 address, u8 *params, struct hidpp_message *res) {
return set_register(fd, device_index, address, params, res, false);
}
bool set_long_register(int fd, u8 device_index, u8 address, u8 *params, struct hidpp_message *res) {
return set_register(fd, device_index, address, params, res, true);
}
static bool get_register(int fd, u8 device_index, u8 address,
struct hidpp_message *out, u8 *params, bool is_long_resp) {
u8 exp_report_id, exp_sub_id;
struct hidpp_message msg;
exp_report_id = is_long_resp ? LONG_MESSAGE : SHORT_MESSAGE;
exp_sub_id = is_long_resp ? SUB_GET_LONG_REGISTER : SUB_GET_REGISTER;
msg.report_id = SHORT_MESSAGE;
msg.device_index = device_index;
msg.sub_id = exp_sub_id;
memset(msg.msg_short.value, 0, sizeof msg.msg_short.value);
msg.msg_short.address = address;
if (params) {
memcpy(&msg.msg_short.value, params, sizeof msg.msg_short.value);
}
if (!do_write(fd, &msg)) {
return false;
}
if (!do_read_skippy(fd, &msg, exp_report_id, exp_sub_id)) {
return false;
}
memcpy(out, &msg, sizeof msg);
return true;
}
bool get_short_register(int fd, u8 device_index, u8 address, u8 *params, struct hidpp_message *out) {
return get_register(fd, device_index, address, out, params, false);
}
bool get_long_register(int fd, u8 device_index, u8 address, u8 *params, struct hidpp_message *out) {
return get_register(fd, device_index, address, out, params, true);
}
bool get_info(int fd, struct hidpp_message *msg) {
if (!get_long_register(fd, DEVICE_RECEIVER, REG_DEVICE_ACTIVITY, NULL, msg)) {
return false;
}
return true;
}
// begin directly-usable functions
bool get_notifications(int fd, u8 device_index, struct msg_enable_notifs *params) {
struct hidpp_message msg;
if (!get_short_register(fd, device_index, REG_ENABLED_NOTIFS, NULL, &msg)) {
return false;
}
memcpy((u8 *) params, &msg.msg_short.value, sizeof *params);
return true;
}
bool set_notifications(int fd, u8 device_index, struct msg_enable_notifs *params) {
struct hidpp_message msg;
if (!set_short_register(fd, device_index, REG_ENABLED_NOTIFS, (u8 *) params, &msg)) {
return false;
}
return true;
}
bool get_and_print_notifications(int fd, u8 device_index, struct msg_enable_notifs *notifsp) {
putchar('\n');
if (get_notifications(fd, device_index, notifsp)) {
u8 flags = notifsp->reporting_flags_receiver;
printf("Reporting Flags (Receiver) = %02x\n", flags & 0xFF);
printf("Wireless notifications = %s\n", flags & 1 ? "yes" : "no");
printf("Software Present = %s\n", flags & 4 ? "yes" : "no");
return true;
} else {
fprintf(stderr, "Failed to get HID++ Notification status\n");
return false;
}
}
bool get_connected_devices(int fd, u8 *devices_count) {
struct hidpp_message msg;
struct val_reg_connection_state *cval;
if (!get_short_register(fd, DEVICE_RECEIVER, REG_CONNECTION_STATE, NULL, &msg)) {
return false;
}
cval = (struct val_reg_connection_state *) msg.msg_short.value;
*devices_count = cval->connected_devices_count;
return true;
}
bool pair_start(int fd, u8 timeout) {
struct hidpp_message msg;
struct val_reg_devpair cmd;
cmd.action = DEVPAIR_OPEN_LOCK;
// device_index is 1..6 for a specific device, 0x53 is seen for "any
// device". Not sure if this is a special value or randomly chosen
cmd.device_number = 0;
cmd.open_lock_timeout = timeout;
if (!set_short_register(fd, DEVICE_RECEIVER, REG_DEVICE_PAIRING, (u8 *) &cmd, &msg)) {
return false;
}
return true;
}
bool pair_cancel(int fd) {
struct hidpp_message msg;
struct val_reg_devpair cmd;
cmd.action = DEVPAIR_CLOSE_LOCK;
// see discussion at pair_start, why did logitech use 0x53? Confusion?
cmd.device_number = 0;
// timeout applies to open lock, not sure why I saw 0x94 (148 sec)
cmd.open_lock_timeout = 0;
if (!set_short_register(fd, DEVICE_RECEIVER, REG_DEVICE_PAIRING, (u8 *) &cmd, &msg)) {
return false;
}
return true;
}
bool device_unpair(int fd, u8 device_index) {
struct hidpp_message msg;
struct val_reg_devpair cmd;
cmd.action = DEVPAIR_DISCONNECT;
cmd.device_number = device_index;
cmd.open_lock_timeout = 0;
if (!set_short_register(fd, DEVICE_RECEIVER, REG_DEVICE_PAIRING, (u8 *) &cmd, &msg)) {
return false;
}
return true;
}
void perform_pair(int fd, u8 timeout) {
struct hidpp_message msg;
if (timeout == 0) {
timeout = 30;
}
if (!pair_start(fd, timeout)) {
fprintf(stderr, "Failed to send pair request\n");
return;
}
puts("Please turn your wireless device off and on to start pairing.");
// WARNING: mess ahead. I knew it would become messy before writing it.
for (;;) {
if (!do_read(fd, &msg, SHORT_MESSAGE, timeout * 1000 + 2000)) {
fprintf(stderr, "Failed to read short message\n");
break;
}
if (msg.sub_id == NOTIF_RECV_LOCK_CHANGE) {
u8 *bytes = (u8 *) &msg.msg_short;
if (msg.report_id != SHORT_MESSAGE || msg.device_index != DEVICE_RECEIVER) {
fprintf(stderr, "Received invalid Unifying Receiver Locking Change notification (0x4A)\n");
continue;
}
if (bytes[0] & 1) { // locking open
continue;
} else { // locking closed
const char *result;
switch (bytes[1]) {
case 0x00:
result = "Success";
break;
case 0x01:
result = "Timeout";
break;
default:
result = "Failure";
}
printf("Pairing result: %s (%i)\n", result, bytes[1]);
break;
}
} else if (msg.sub_id == NOTIF_DEV_CONNECT) {
u8 device_index;
bool is_new_dev;
if (!process_notif_dev_connect(&msg, &device_index, &is_new_dev)) {
// error message is already emitted
continue;
}
if (device_index < 1 || device_index > DEVICES_MAX) {
fprintf(stderr, "Invalid device index %#04x\n", device_index);
} else if (is_new_dev) {
u8 device_type = devices[device_index - 1].device_type;
printf("Found new device, id=%#04x %s\n",
device_index,
device_type_str(device_type));
break;
} else {
printf("Ignoring existent device id=%#04x\n", device_index);
}
}
}
// end of mess
if (!pair_cancel(fd)) {
fprintf(stderr, "Failed to cancel pair visibility\n");
}
}
void perform_unpair(int fd, u8 device_index) {
struct device *dev = &devices[device_index - 1];
u8 dev_device_type = dev->device_type; // will be overwritten, therefore store it
if (!dev->device_present) {
printf("Device %#04x does not appear to be paired\n", device_index);
return;
}
if (device_unpair(fd, device_index)) {
if (!dev->device_present) {
printf("Device %#04x %s successfully unpaired\n", device_index,
device_type_str(dev_device_type));
} else {
fprintf(stderr, "Unpairing of %#04x possibly failed\n", device_index);
}
} else {
fprintf(stderr, "Failed to send unpair %#04x request\n", device_index);
}
}
// triggers a notification that updates the list of paired devices
bool get_all_devices(int fd) {
struct hidpp_message msg;
struct val_reg_connection_state cval;
memset(&cval, 0, sizeof cval);
cval.action = CONSTATE_ACTION_LIST_DEVICES;
if (!set_short_register(fd, DEVICE_RECEIVER, REG_CONNECTION_STATE, (u8 *) &cval, &msg)) {
return false;
}
return true;
}
bool get_receiver_info(int fd, struct receiver_info *rinfo) {
struct hidpp_message msg;
u8 params[3] = {0};
params[0] = 0x03; // undocumented
if (get_long_register(fd, DEVICE_RECEIVER, REG_PAIRING_INFO, params, &msg)) {
struct msg_receiver_info *info = (struct msg_receiver_info *) &msg.msg_long.str;
uint32_t serial_number;
memcpy(&serial_number, &info->serial_number, sizeof(serial_number));
rinfo->serial_number = ntohl(serial_number);
return true;
}
return false;
}
bool get_device_pair_info(int fd, u8 device_index) {
struct device *dev = &devices[device_index - 1];
struct hidpp_message msg;
u8 params[3] = {0};
params[0] = 0x20 | (device_index - 1); // 0x20..0x2F Unifying Device pairing info
if (get_long_register(fd, DEVICE_RECEIVER, REG_PAIRING_INFO, params, &msg)) {
struct msg_dev_pair_info *info = (struct msg_dev_pair_info *) &msg.msg_long.str;
dev->wireless_pid = (info->pid_msb << 8) | info->pid_lsb;
dev->device_type = info->device_type;
return true;
}
return false;
}
bool get_device_ext_pair_info(int fd, u8 device_index) {
struct device *dev = &devices[device_index - 1];
struct hidpp_message msg;
u8 params[3] = {0};
params[0] = 0x30 | (device_index - 1); // 0x30..0x3F Unifying Device extended pairing info
if (get_long_register(fd, DEVICE_RECEIVER, REG_PAIRING_INFO, params, &msg)) {
struct msg_dev_ext_pair_info *info;
uint32_t serial_number;
info = (struct msg_dev_ext_pair_info *) &msg.msg_long.str;
memcpy(&serial_number, &info->serial_number, sizeof(serial_number));
dev->serial_number = ntohl(serial_number);
dev->power_switch_location = info->usability_info & 0x0F;
return true;
}
return false;
}
bool get_device_name(int fd, u8 device_index) {
struct device *dev = &devices[device_index - 1];
struct hidpp_message msg;
u8 params[3] = {0};
params[0] = 0x40 | (device_index - 1); // 0x40..0x4F Unifying Device Name
if (get_long_register(fd, DEVICE_RECEIVER, REG_PAIRING_INFO, params, &msg)) {
struct msg_dev_name *name = (struct msg_dev_name *) &msg.msg_long.str;
if (name->length > DEVICE_NAME_MAXLEN) {
fprintf(stderr, "Invalid name length %#04x for idx=%i\n", name->length, device_index);
return false;
}
memcpy(&dev->name, name->str, name->length);
dev->name[name->length] = 0;
return true;
}
return false;
}
bool get_hidpp_version(int fd, u8 device_index, struct hidpp_version *version) {
struct hidpp_message msg;
struct msg_short *payload = &msg.msg_short;
u8 softwareId = 0x04; // value 1..15 - random choice for 0x4
u8 ping_data = 0x00; // can be any value
msg.report_id = SHORT_MESSAGE;
msg.device_index = device_index;
msg.sub_id = 0x00; // Root feature index
memset(payload->value, 0, sizeof payload->value);
payload->address = 0x10 | softwareId;
payload->value[2] = ping_data;
if (!do_write(fd, &msg)) {
return false;
}
for (;;) {
if (!do_read(fd, &msg, 0, 3000)) {
if (debug_enabled) {
fprintf(stderr, "Failed to read HID++ version, device does not respond!\n");
}
return false;
}
if (msg.sub_id == 0x8F) {
struct msg_error *error = (struct msg_error *) &msg.msg_error;
if (error->sub_id == 0x00 && error->address == (0x10 | softwareId)) {
// if error is ERR_INVALID_SUBID (0x01), then HID++ 1.0
if (error->error_code == 0x01) {
version->major = 1;
version->minor = 0;
return true;
} else if (debug_enabled) {
const char *err_str = error_messages[error->error_code];
fprintf(stderr, "Failed to retrieve version: %#04x (%s)\n",
error->error_code, err_str);
}
// fatal error - is device connected?
return false;
}
// ignore other errors
}
if (msg.sub_id == 0x00 && (payload->address & 0xF) == softwareId &&
payload->value[2] == ping_data) {
break; // I think we got a version
} else if (debug_enabled) {
fprintf(stderr, "Ignoring sub_id=%02x\n", msg.sub_id);
}
// ignore other messages
}
version->major = payload->value[0];
version->minor = payload->value[1];
return true;
}
// device_index can also be 0xFF for receiver
bool get_device_version(int fd, u8 device_index, u8 version_type, struct val_reg_version *ver) {
struct hidpp_message msg;
// TODO: not 100% reliable for wireless devices, it may return MSG_ERR
// (err=SUCCESS, wtf). Perhaps we need to send another msg type=00
// (whatever the undocumented params are).
memset(ver, 0, sizeof *ver);
ver->select_field = version_type;
if (get_short_register(fd, device_index, REG_VERSION_INFO, (u8 *) ver, &msg)) {
memcpy(ver, msg.msg_short.value, sizeof *ver);
return true;
}
return false;
}
bool get_device_versions(int fd, u8 device_index, struct version *version) {
struct val_reg_version ver;
memset(version, 0, sizeof *version);
if (get_device_version(fd, device_index, VERSION_FIRMWARE, &ver)) {
version->fw_major = ver.v1;
version->fw_minor = ver.v2;
} else {
// assume that other versions will fail too
return false;
}
if (get_device_version(fd, device_index, VERSION_FW_BUILD, &ver)) {
version->fw_build = (ver.v1 << 8) | ver.v2;
}
//if (get_device_version(fd, device_index, 3, &ver)) puts("No idea what this is useful for");
if (get_device_version(fd, device_index, VERSION_BOOTLOADER, &ver)) {
version->bl_major = ver.v1;
version->bl_minor = ver.v2;
}
return true;
}
// device index is 1..6
void gather_device_info(int fd, u8 device_index) {
if (get_device_pair_info(fd, device_index)) {
struct device *dev = &devices[device_index - 1];
dev->device_present = true;
get_hidpp_version(fd, device_index, &dev->hidpp_version);
get_device_ext_pair_info(fd, device_index);
get_device_name(fd, device_index);
if (dev->hidpp_version.major == 1 && dev->hidpp_version.minor == 0) {
if (get_device_versions(fd, device_index, &dev->version)) {
dev->device_available = true;
}
} else {
// TODO: hid++20 support
}
} else {
// retrieve some information from notifier
get_all_devices(fd);
}
}
void print_versions(struct version *ver) {
// versions are shown as hex. Probably a mistake given that the length
// is 3 which can fit 255 instead of FF (and 65535 instead of FF)
printf("Firmware version: %03x.%03x.%05x\n",
ver->fw_major, ver->fw_minor, ver->fw_build);
printf("Bootloader version: BL.%03x.%03x\n",
ver->bl_major, ver->bl_minor);
}
void get_and_print_recv_info(int fd) {
if (get_receiver_info(fd, &receiver)) {
printf("Serial number: %08X\n", receiver.serial_number);
}
if (get_device_versions(fd, DEVICE_RECEIVER, &receiver.version)) {
print_versions(&receiver.version);
}
}
void print_detailed_device(u8 device_index) {
struct device *dev = &devices[device_index - 1];
if (!dev->device_present) {
printf("Device %i is not paired\n", device_index);
return;
}
if (dev->hidpp_version.major) {
printf("HID++ version: %i.%i\n", dev->hidpp_version.major, dev->hidpp_version.minor);
} else {
puts("HID++ version: unknown");
}
printf("Device index %i\n", device_index);
printf("%s\n", device_type_str(dev->device_type));
printf("Name: %s\n", dev->name);
printf("Wireless Product ID: %04X\n", dev->wireless_pid);
printf("Serial number: %08X\n", dev->serial_number);
if (dev->device_available) {
print_versions(&dev->version);
} else {
puts("Device was unavailable, version information not available.");
}
}