Repository navigation
Expand file tree
/
Copy pathacdprobe.cpp
More file actions
2594 lines (2316 loc) · 98.6 KB
/
Copy pathacdprobe.cpp
File metadata and controls
2594 lines (2316 loc) · 98.6 KB
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
#include <linux/hiddev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <dirent.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "acd/apple_led_cinema_27.h"
#define ACDPROBE_VERSION "0.5"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
/* HID usage pages used by Apple USB monitor controls. */
static const unsigned int kUsagePageUsbMonitor = 0x0080U;
static const unsigned int kUsagePageVesaVirtualControls = 0x0082U;
static const unsigned int kUsagePageAppleVendorPrivate = 0xff92U;
/* HID usage IDs.
*
* Confirmed on Apple LED Cinema Display 27-inch (05ac:9226):
* report 16 / usage 0x00820010 = Brightness
* report 102 / usage 0x00820066 = Ambient Light Sensor / auto-brightness
*
* Tentative labels from controlled reverse-engineering:
* report 225 / usage 0xff9200e1 = vendor-private boolean
* report 236 / usage 0xff9200ec = vendor-private status/data
*/
static const unsigned int kUsageIdMonitorControl = 0x0001U;
static const unsigned int kUsageIdBrightness = 0x0010U;
static const unsigned int kUsageIdAmbientLightSensor = 0x0066U;
static const unsigned int kUsageIdVendorPrivateBool = 0x00e1U;
static const unsigned int kUsageIdVendorPrivateStatus = 0x00ecU;
/* Known report IDs for Apple LED Cinema Display 27-inch (05ac:9226). */
static const unsigned int kFeatureReportBrightness = 16U;
static const unsigned int kFeatureReportAmbientLightSensor = 102U;
static const unsigned int kFeatureReportVendorPrivateBool = 225U;
static const unsigned int kFeatureReportVendorPrivateStatus = 236U;
struct StringEntry {
int index;
std::string value;
};
struct UsageInfo {
unsigned int usage_index;
unsigned int usage_code;
int value;
bool have_value;
bool get_code_ok;
bool get_value_ok;
};
struct FieldInfoWrap {
hiddev_field_info info;
std::vector<UsageInfo> usages;
};
struct ReportInfoWrap {
hiddev_report_info info;
std::vector<FieldInfoWrap> fields;
};
struct CandidateHint {
std::string key;
std::string level;
std::string text;
};
struct FeatureSetRequest {
bool enabled;
unsigned int report_id;
unsigned int field_index;
unsigned int usage_index;
int value;
unsigned int readback_delay_ms;
FeatureSetRequest()
: enabled(false), report_id(0), field_index(0), usage_index(0), value(0), readback_delay_ms(0) {}
};
struct FeatureSetResult {
bool attempted;
bool success;
std::string error;
unsigned int report_id;
unsigned int field_index;
unsigned int usage_index;
unsigned int usage_code;
int logical_minimum;
int logical_maximum;
int before_value;
int requested_value;
int after_value;
int delayed_value;
bool have_before;
bool have_after;
bool have_delayed;
FeatureSetResult()
: attempted(false), success(false), report_id(0), field_index(0), usage_index(0), usage_code(0),
logical_minimum(0), logical_maximum(0), before_value(0), requested_value(0), after_value(0),
delayed_value(0), have_before(false), have_after(false), have_delayed(false) {}
};
struct UsbSysfsIdentity {
std::string manufacturer;
std::string product;
std::string id_vendor;
std::string id_product;
std::string bcd_device;
};
struct ProbeData {
int hid_version;
hiddev_devinfo devinfo;
std::string device_node;
std::string hid_name;
std::string hid_phys;
std::vector<StringEntry> strings;
std::vector<unsigned int> applications;
std::vector<ReportInfoWrap> input_reports;
std::vector<ReportInfoWrap> output_reports;
std::vector<ReportInfoWrap> feature_reports;
std::string sysfs_hiddev_realpath;
std::string sysfs_devchar_realpath;
std::string sysfs_device_realpath;
std::string sysfs_report_descriptor_path;
std::string report_descriptor_hex;
std::string report_descriptor_bytes;
UsbSysfsIdentity usb_sysfs_identity;
std::string vid_pid_key;
std::string base_filename;
};
struct SummaryControl {
unsigned int report_type;
unsigned int report_id;
unsigned int field_index;
unsigned int usage_code;
unsigned int usage_page;
std::string report_type_text;
unsigned int application_usage;
std::string application_decoded;
std::string usage_decoded;
std::string name;
std::string confidence;
unsigned int logical;
unsigned int physical;
int logical_minimum;
int logical_maximum;
int physical_minimum;
int physical_maximum;
unsigned int field_flags;
std::string field_flags_text;
int unit_exponent;
int unit;
unsigned int usage_count;
bool is_single_value;
unsigned int value_count;
std::vector<int> current_values;
SummaryControl()
: report_type(0), report_id(0), field_index(0), usage_code(0), usage_page(0),
application_usage(0), logical(0), physical(0), logical_minimum(0),
logical_maximum(0), physical_minimum(0), physical_maximum(0), field_flags(0),
unit_exponent(0), unit(0), usage_count(0), is_single_value(true),
value_count(0) {}
};
struct ReportDescriptorFingerprint {
bool available;
std::string fnv1a64_hex;
std::string preview_hex;
std::string::size_type byte_count;
};
struct TelemetryCandidate {
std::string report_type_text;
unsigned int report_id;
unsigned int field_index;
unsigned int usage_code;
unsigned int usage_page;
std::string usage_decoded;
std::string confidence;
int logical_minimum;
int logical_maximum;
unsigned int field_flags;
std::string field_flags_text;
bool readable;
std::string value_shape;
std::vector<int> current_values;
TelemetryCandidate()
: report_id(0), field_index(0), usage_code(0), usage_page(0),
logical_minimum(0), logical_maximum(0), field_flags(0), readable(false) {}
};
struct MacOSParameterHint {
std::string parameter;
std::string status;
unsigned int usage_code;
std::string usage_decoded;
std::string note;
MacOSParameterHint()
: usage_code(0) {}
};
static std::vector<TelemetryCandidate> collect_vendor_private_telemetry_candidates(const ProbeData& data);
static std::vector<MacOSParameterHint> collect_macos_parameter_hints(const ProbeData& data);
static std::string now_utc_iso8601() {
std::time_t now = std::time(NULL);
struct tm tm_now;
gmtime_r(&now, &tm_now);
char buf[64];
if (std::strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &tm_now) == 0) {
return "1970-01-01T00:00:00Z";
}
return buf;
}
static std::string escape_json(const std::string& s) {
std::ostringstream out;
for (std::string::size_type i = 0; i < s.size(); ++i) {
unsigned char c = static_cast<unsigned char>(s[i]);
switch (c) {
case '"': out << "\\\""; break;
case '\\': out << "\\\\"; break;
case '\b': out << "\\b"; break;
case '\f': out << "\\f"; break;
case '\n': out << "\\n"; break;
case '\r': out << "\\r"; break;
case '\t': out << "\\t"; break;
default:
if (c < 0x20) {
out << "\\u" << std::hex << std::setw(4) << std::setfill('0')
<< static_cast<unsigned int>(c)
<< std::dec << std::setfill(' ');
} else {
out << s[i];
}
}
}
return out.str();
}
static std::string csv_escape(const std::string& s) {
bool need_quotes = false;
for (std::string::size_type i = 0; i < s.size(); ++i) {
if (s[i] == ',' || s[i] == '"' || s[i] == '\n' || s[i] == '\r') {
need_quotes = true;
break;
}
}
if (!need_quotes) {
return s;
}
std::ostringstream out;
out << '"';
for (std::string::size_type i = 0; i < s.size(); ++i) {
if (s[i] == '"') {
out << '"';
}
out << s[i];
}
out << '"';
return out.str();
}
static std::string hex_u32(unsigned int value) {
std::ostringstream out;
out << "0x" << std::hex << std::setw(8) << std::setfill('0') << value;
return out.str();
}
static std::string hex_u16(unsigned int value) {
std::ostringstream out;
out << "0x" << std::hex << std::setw(4) << std::setfill('0') << (value & 0xffffU);
return out.str();
}
static std::string vid_pid_dirname(unsigned int vendor, unsigned int product) {
std::ostringstream out;
out << std::hex << std::nouppercase
<< std::setw(4) << std::setfill('0') << (vendor & 0xffffU)
<< "_"
<< std::setw(4) << std::setfill('0') << (product & 0xffffU);
return out.str();
}
static std::string report_type_name(unsigned int report_type) {
switch (report_type) {
case HID_REPORT_TYPE_INPUT: return "input";
case HID_REPORT_TYPE_OUTPUT: return "output";
case HID_REPORT_TYPE_FEATURE: return "feature";
default: return "unknown";
}
}
static std::string field_flags_to_string(unsigned int flags) {
std::ostringstream out;
bool first = true;
struct FlagName {
unsigned int bit;
const char* name;
} flag_names[] = {
{ HID_FIELD_CONSTANT, "constant" },
{ HID_FIELD_VARIABLE, "variable" },
{ HID_FIELD_RELATIVE, "relative" },
{ HID_FIELD_WRAP, "wrap" },
{ HID_FIELD_NONLINEAR, "nonlinear" },
{ HID_FIELD_NO_PREFERRED, "no_preferred" },
{ HID_FIELD_NULL_STATE, "null_state" },
{ HID_FIELD_VOLATILE, "volatile" },
{ HID_FIELD_BUFFERED_BYTE, "buffered_byte" }
};
for (unsigned int i = 0; i < sizeof(flag_names) / sizeof(flag_names[0]); ++i) {
if (flags & flag_names[i].bit) {
if (!first) {
out << '|';
}
out << flag_names[i].name;
first = false;
}
}
if (first) {
out << "none";
}
return out.str();
}
static std::string basename_string(const std::string& path) {
std::string::size_type pos = path.find_last_of('/');
if (pos == std::string::npos) {
return path;
}
return path.substr(pos + 1);
}
static std::string dirname_string(const std::string& path) {
std::string::size_type pos = path.find_last_of('/');
if (pos == std::string::npos) {
return ".";
}
if (pos == 0) {
return "/";
}
return path.substr(0, pos);
}
static bool file_exists(const std::string& path) {
struct stat st;
return stat(path.c_str(), &st) == 0;
}
static bool is_directory(const std::string& path) {
struct stat st;
if (stat(path.c_str(), &st) != 0) {
return false;
}
return S_ISDIR(st.st_mode);
}
static std::string realpath_string(const std::string& path) {
char buf[PATH_MAX + 1];
if (realpath(path.c_str(), buf) == NULL) {
return "";
}
return std::string(buf);
}
static bool read_binary_file(const std::string& path, std::string& out) {
std::ifstream in(path.c_str(), std::ios::in | std::ios::binary);
if (!in) {
return false;
}
std::ostringstream tmp;
tmp << in.rdbuf();
out = tmp.str();
return true;
}
static std::string trim_ascii_whitespace(const std::string& s) {
std::string::size_type begin = 0;
while (begin < s.size()) {
unsigned char c = static_cast<unsigned char>(s[begin]);
if (c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != '\f' && c != '\v') {
break;
}
++begin;
}
std::string::size_type end = s.size();
while (end > begin) {
unsigned char c = static_cast<unsigned char>(s[end - 1]);
if (c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != '\f' && c != '\v') {
break;
}
--end;
}
return s.substr(begin, end - begin);
}
static bool read_text_file_trimmed(const std::string& path, std::string& out) {
std::ifstream in(path.c_str(), std::ios::in | std::ios::binary);
if (!in) {
return false;
}
std::ostringstream tmp;
tmp << in.rdbuf();
out = trim_ascii_whitespace(tmp.str());
return true;
}
static std::string bytes_to_hex(const std::string& bytes) {
std::ostringstream out;
for (std::string::size_type i = 0; i < bytes.size(); ++i) {
unsigned int c = static_cast<unsigned char>(bytes[i]);
out << std::hex << std::setw(2) << std::setfill('0') << c;
if ((i + 1) != bytes.size()) {
out << ' ';
}
}
return out.str();
}
static const uint64_t kFNV1a64OffsetBasis = 0xcbf29ce484222325ULL;
static const uint64_t kFNV1a64Prime = 0x100000001b3ULL;
static uint64_t fnv1a64_bytes(const std::string& bytes) {
uint64_t hash = kFNV1a64OffsetBasis;
for (std::string::size_type i = 0; i < bytes.size(); ++i) {
hash ^= static_cast<unsigned char>(bytes[i]);
hash *= kFNV1a64Prime;
}
return hash;
}
static std::string bytes_to_preview_hex(const std::string& bytes, std::string::size_type max_bytes) {
if (bytes.empty() || max_bytes == 0) {
return "";
}
std::string::size_type preview_len = bytes.size();
if (preview_len > max_bytes) {
preview_len = max_bytes;
}
return bytes_to_hex(bytes.substr(0, preview_len));
}
static std::string hex_u64(uint64_t value) {
std::ostringstream out;
out << "0x" << std::hex << std::setw(16) << std::setfill('0') << value;
return out.str();
}
static bool write_text_file(const std::string& path, const std::string& content) {
std::ofstream out(path.c_str(), std::ios::out | std::ios::binary);
if (!out) {
return false;
}
out << content;
return out.good();
}
static bool ensure_directory_recursive(const std::string& path) {
if (path.empty() || path == ".") {
return true;
}
if (is_directory(path)) {
return true;
}
std::string parent = dirname_string(path);
if (parent != path && !ensure_directory_recursive(parent)) {
return false;
}
if (::mkdir(path.c_str(), 0755) == 0) {
return true;
}
if (errno == EEXIST && is_directory(path)) {
return true;
}
return false;
}
static std::string join_path(const std::string& a, const std::string& b) {
if (a.empty()) {
return b;
}
if (a[a.size() - 1] == '/') {
return a + b;
}
return a + "/" + b;
}
static bool has_usb_sysfs_identity_files(const std::string& path) {
return !path.empty()
&& is_directory(path)
&& file_exists(join_path(path, "idVendor"))
&& file_exists(join_path(path, "idProduct"));
}
static std::string find_usb_sysfs_device_path(const std::string& start) {
if (start.empty()) {
return "";
}
std::string path = start;
for (int i = 0; i < 16; ++i) {
if (has_usb_sysfs_identity_files(path)) {
return path;
}
std::string parent = dirname_string(path);
if (parent == path) {
break;
}
path = parent;
}
return "";
}
static void read_usb_sysfs_identity(const std::string& usb_device_path, UsbSysfsIdentity& identity) {
if (usb_device_path.empty()) {
return;
}
read_text_file_trimmed(join_path(usb_device_path, "manufacturer"), identity.manufacturer);
read_text_file_trimmed(join_path(usb_device_path, "product"), identity.product);
read_text_file_trimmed(join_path(usb_device_path, "idVendor"), identity.id_vendor);
read_text_file_trimmed(join_path(usb_device_path, "idProduct"), identity.id_product);
read_text_file_trimmed(join_path(usb_device_path, "bcdDevice"), identity.bcd_device);
}
static std::string collect_dirname(const std::string& save_dir, const ProbeData& data) {
if (save_dir == "probes/raw") {
return join_path("probes", data.vid_pid_key);
}
return join_path(save_dir, data.vid_pid_key);
}
static bool get_hid_name(int fd, std::string& out) {
char buf[256];
std::memset(buf, 0, sizeof(buf));
int rc = ioctl(fd, HIDIOCGNAME(sizeof(buf)), buf);
if (rc < 0) {
return false;
}
out = buf;
return true;
}
static bool get_hid_phys(int fd, std::string& out) {
char buf[256];
std::memset(buf, 0, sizeof(buf));
int rc = ioctl(fd, HIDIOCGPHYS(sizeof(buf)), buf);
if (rc < 0) {
return false;
}
out = buf;
return true;
}
static std::string usage_page_name(unsigned int page) {
switch (page) {
case kUsagePageUsbMonitor: return "USB Monitor";
case kUsagePageVesaVirtualControls: return "VESA Virtual Controls";
case kUsagePageAppleVendorPrivate: return "Vendor Private 0xff92";
default: return "Unknown";
}
}
static std::string decode_usage_code(unsigned int usage_code) {
unsigned int page = (usage_code >> 16) & 0xffffU;
unsigned int id = usage_code & 0xffffU;
if (page == kUsagePageUsbMonitor && id == kUsageIdMonitorControl) {
return "Monitor Control";
}
if (page == kUsagePageVesaVirtualControls && id == kUsageIdBrightness) {
return "Brightness";
}
if (page == kUsagePageVesaVirtualControls && id == kUsageIdAmbientLightSensor) {
return "Ambient Light Sensor";
}
if (page == kUsagePageAppleVendorPrivate && id == kUsageIdVendorPrivateBool) {
return "Vendor-private boolean (tentative)";
}
if (page == kUsagePageAppleVendorPrivate && id == kUsageIdVendorPrivateStatus) {
return "Vendor-private data/status (tentative)";
}
if ((page & 0xff00U) == 0xff00U) {
return "Vendor-private usage";
}
return "Unknown";
}
static std::string meaning_confidence_name(acd::hid::monitor::MeaningConfidence confidence) {
switch (confidence) {
case acd::hid::monitor::MEANING_CONFIRMED: return "confirmed";
case acd::hid::monitor::MEANING_INFERRED: return "inferred";
case acd::hid::monitor::MEANING_UNKNOWN:
default:
return "unknown";
}
}
static const acd::hid::monitor::KnownReportInfo* find_known_report_by_name(
const ProbeData& data,
const std::string& name
) {
if (!acd::apple::led_cinema_27::matches_device(
static_cast<std::uint16_t>(data.devinfo.vendor),
static_cast<std::uint16_t>(data.devinfo.product))) {
return 0;
}
for (std::size_t i = 0; i < acd::apple::led_cinema_27::KNOWN_REPORT_COUNT; ++i) {
const acd::hid::monitor::KnownReportInfo& info =
acd::apple::led_cinema_27::KNOWN_REPORTS[i];
if (name == info.short_name) {
return &info;
}
}
return 0;
}
static const ReportInfoWrap* find_feature_report_by_id(const ProbeData& data, unsigned int report_id) {
for (std::vector<ReportInfoWrap>::const_iterator it = data.feature_reports.begin();
it != data.feature_reports.end(); ++it) {
if (it->info.report_id == report_id) {
return &(*it);
}
}
return 0;
}
static std::string build_known_report_text(const ProbeData& data,
const std::string& known_name,
std::string& error) {
const acd::hid::monitor::KnownReportInfo* info =
find_known_report_by_name(data, known_name);
if (!info) {
std::ostringstream out;
if (!acd::apple::led_cinema_27::matches_device(
static_cast<std::uint16_t>(data.devinfo.vendor),
static_cast<std::uint16_t>(data.devinfo.product))) {
out << "--get-known is currently supported only for Apple LED Cinema Display 27-inch (05ac:9226); probed device is "
<< hex_u16(static_cast<unsigned int>(data.devinfo.vendor))
<< ":"
<< hex_u16(static_cast<unsigned int>(data.devinfo.product));
} else {
out << "unknown known-report name: " << known_name
<< ". Available names:";
for (std::size_t i = 0; i < acd::apple::led_cinema_27::KNOWN_REPORT_COUNT; ++i) {
out << (i == 0 ? " " : ", ")
<< acd::apple::led_cinema_27::KNOWN_REPORTS[i].short_name;
}
}
error = out.str();
return std::string();
}
const ReportInfoWrap* report = find_feature_report_by_id(data, info->report_id);
if (!report) {
std::ostringstream out;
out << "known report " << info->short_name
<< " (report_id=" << static_cast<unsigned int>(info->report_id)
<< ") was not found in the feature report set";
error = out.str();
return std::string();
}
std::vector<int> current_values;
unsigned int usage_code = 0U;
unsigned int field_index = 0U;
int logical_minimum = 0;
int logical_maximum = 0;
unsigned int field_flags = 0U;
unsigned int maxusage = 0U;
bool have_field_metadata = false;
for (std::vector<FieldInfoWrap>::const_iterator fit = report->fields.begin();
fit != report->fields.end(); ++fit) {
if (!have_field_metadata) {
field_index = fit->info.field_index;
logical_minimum = fit->info.logical_minimum;
logical_maximum = fit->info.logical_maximum;
field_flags = fit->info.flags;
maxusage = fit->info.maxusage;
have_field_metadata = true;
}
for (std::vector<UsageInfo>::const_iterator uit = fit->usages.begin();
uit != fit->usages.end(); ++uit) {
if (usage_code == 0U && uit->get_code_ok) {
usage_code = uit->usage_code;
}
if (uit->have_value) {
current_values.push_back(uit->value);
}
}
}
std::ostringstream out;
out << "acdprobe known report\n";
out << "device_node: " << data.device_node << "\n";
out << "hid_name: " << (data.hid_name.empty() ? std::string("<unavailable>") : data.hid_name) << "\n";
out << "known_name: " << info->short_name << "\n";
out << "report_type: feature\n";
out << "report_id: " << static_cast<unsigned int>(info->report_id)
<< " (" << hex_u16(static_cast<unsigned int>(info->report_id)) << ")\n";
out << "payload_size_bytes: " << info->payload_size_bytes << "\n";
out << "usage_page: " << hex_u16(info->usage_page)
<< " (" << usage_page_name(info->usage_page) << ")\n";
out << "usage: " << hex_u16(info->usage) << "\n";
out << "usage_code: " << (usage_code == 0U ? std::string("<unavailable>") : hex_u32(usage_code)) << "\n";
out << "decoded: "
<< (usage_code == 0U ? decode_usage_code((info->usage_page << 16) | info->usage)
: decode_usage_code(usage_code))
<< "\n";
out << "meaning: " << info->meaning << "\n";
out << "confidence: " << meaning_confidence_name(info->confidence) << "\n";
out << "field_index: " << field_index << "\n";
out << "maxusage: " << maxusage << "\n";
out << "logical_range: " << logical_minimum << ".." << logical_maximum << "\n";
out << "field_flags: " << field_flags << " (" << field_flags_to_string(field_flags) << ")\n";
out << "current_value: ";
if (current_values.empty()) {
out << "<unavailable>\n";
} else if (current_values.size() == 1U) {
out << current_values[0] << "\n";
} else {
out << "[";
for (std::size_t i = 0; i < current_values.size(); ++i) {
out << current_values[i];
if ((i + 1U) != current_values.size()) {
out << ", ";
}
}
out << "]\n";
}
if (info->report_id == acd::apple::led_cinema_27::REPORT_ID_BRIGHTNESS && current_values.size() == 1U) {
out << "decoded_value: brightness=" << current_values[0]
<< " percent="
<< std::fixed << std::setprecision(1)
<< acd::apple::led_cinema_27::brightness_value_to_percent(
static_cast<std::uint16_t>(current_values[0]))
<< "\n";
} else if (info->report_id == acd::apple::led_cinema_27::REPORT_ID_MODE_2STATE && current_values.size() == 1U) {
out << "decoded_value: mode_2state=" << current_values[0] << "\n";
} else if (info->report_id == acd::apple::led_cinema_27::REPORT_ID_VENDOR_FLAG && current_values.size() == 1U) {
out << "decoded_value: vendor_flag=" << current_values[0] << "\n";
} else if (info->report_id == acd::apple::led_cinema_27::REPORT_ID_VENDOR_STATE && current_values.size() == 2U) {
out << "decoded_value: value0=" << current_values[0]
<< " value1=" << current_values[1] << "\n";
}
return out.str();
}
static void enumerate_strings(int fd, ProbeData& data) {
for (int idx = 1; idx <= 16; ++idx) {
hiddev_string_descriptor desc;
std::memset(&desc, 0, sizeof(desc));
desc.index = idx;
if (ioctl(fd, HIDIOCGSTRING, &desc) >= 0) {
if (desc.value[0] != '\0') {
StringEntry entry;
entry.index = idx;
entry.value = desc.value;
data.strings.push_back(entry);
}
}
}
}
static void enumerate_applications(int fd, const hiddev_devinfo& devinfo, ProbeData& data) {
for (__u32 i = 0; i < devinfo.num_applications; ++i) {
int app = ioctl(fd, HIDIOCAPPLICATION, i);
if (app >= 0) {
data.applications.push_back(static_cast<unsigned int>(app));
}
}
}
static void enumerate_report_type(int fd, unsigned int report_type, std::vector<ReportInfoWrap>& out_reports) {
hiddev_report_info rep;
std::memset(&rep, 0, sizeof(rep));
rep.report_type = report_type;
rep.report_id = HID_REPORT_ID_FIRST;
while (ioctl(fd, HIDIOCGREPORTINFO, &rep) >= 0) {
ReportInfoWrap rep_wrap;
std::memset(&rep_wrap.info, 0, sizeof(rep_wrap.info));
rep_wrap.info = rep;
for (__u32 field_index = 0; field_index < rep.num_fields; ++field_index) {
hiddev_field_info finfo;
std::memset(&finfo, 0, sizeof(finfo));
finfo.report_type = rep.report_type;
finfo.report_id = rep.report_id;
finfo.field_index = field_index;
if (ioctl(fd, HIDIOCGFIELDINFO, &finfo) < 0) {
continue;
}
FieldInfoWrap field_wrap;
std::memset(&field_wrap.info, 0, sizeof(field_wrap.info));
field_wrap.info = finfo;
for (__u32 usage_index = 0; usage_index < finfo.maxusage; ++usage_index) {
hiddev_usage_ref uref;
std::memset(&uref, 0, sizeof(uref));
uref.report_type = rep.report_type;
uref.report_id = rep.report_id;
uref.field_index = field_index;
uref.usage_index = usage_index;
UsageInfo usage;
usage.usage_index = usage_index;
usage.usage_code = 0;
usage.value = 0;
usage.have_value = false;
usage.get_code_ok = false;
usage.get_value_ok = false;
if (ioctl(fd, HIDIOCGUCODE, &uref) >= 0) {
usage.usage_code = uref.usage_code;
usage.get_code_ok = true;
}
if (ioctl(fd, HIDIOCGUSAGE, &uref) >= 0) {
usage.value = uref.value;
usage.have_value = true;
usage.get_value_ok = true;
if (!usage.get_code_ok) {
usage.usage_code = uref.usage_code;
}
}
field_wrap.usages.push_back(usage);
}
rep_wrap.fields.push_back(field_wrap);
}
out_reports.push_back(rep_wrap);
rep.report_id |= HID_REPORT_ID_NEXT;
}
}
static bool find_named_file_recursive(const std::string& base, const char* wanted_name, int depth, std::string& found) {
if (base.empty() || depth < 0 || !is_directory(base)) {
return false;
}
DIR* dir = opendir(base.c_str());
if (!dir) {
return false;
}
bool ok = false;
struct dirent* ent;
while ((ent = readdir(dir)) != NULL) {
const char* name = ent->d_name;
if (std::strcmp(name, ".") == 0 || std::strcmp(name, "..") == 0) {
continue;
}
std::string child = base + "/" + name;
if (std::strcmp(name, wanted_name) == 0 && file_exists(child)) {
found = child;
ok = true;
break;
}
if (depth > 0 && is_directory(child)) {
if (find_named_file_recursive(child, wanted_name, depth - 1, found)) {
ok = true;
break;
}
}
}
closedir(dir);
return ok;
}
static std::string find_report_descriptor_path_from_base(const std::string& base) {
if (base.empty()) {
return "";
}
std::string direct = base + "/report_descriptor";
if (file_exists(direct)) {
return direct;
}
std::string found;
if (find_named_file_recursive(base, "report_descriptor", 5, found)) {
return found;
}
std::string path = base;
for (int i = 0; i < 8; ++i) {
std::string parent = dirname_string(path);
if (parent == path) {
break;
}
path = parent;
std::string candidate = path + "/report_descriptor";
if (file_exists(candidate)) {
return candidate;
}
found.clear();
if (find_named_file_recursive(path, "report_descriptor", 3, found)) {
return found;
}
if (path == "/") {
break;
}
}
return "";
}
static void detect_sysfs_paths(ProbeData& data) {
std::string node_base = basename_string(data.device_node);
std::string hiddev_sysfs = std::string("/sys/class/hiddev/") + node_base;
data.sysfs_hiddev_realpath = realpath_string(hiddev_sysfs);
struct stat st;
if (stat(data.device_node.c_str(), &st) == 0) {
std::ostringstream devchar;
devchar << "/sys/dev/char/" << major(st.st_rdev) << ":" << minor(st.st_rdev);
data.sysfs_devchar_realpath = realpath_string(devchar.str());
}
std::string descriptor_path;
std::string summary_path;
if (!data.sysfs_hiddev_realpath.empty()) {
descriptor_path = find_report_descriptor_path_from_base(data.sysfs_hiddev_realpath);
std::string maybe_device = realpath_string(data.sysfs_hiddev_realpath + "/device");
if (!maybe_device.empty()) {
data.sysfs_device_realpath = maybe_device;
}
}
if (descriptor_path.empty() && !data.sysfs_devchar_realpath.empty()) {
descriptor_path = find_report_descriptor_path_from_base(data.sysfs_devchar_realpath);
if (data.sysfs_device_realpath.empty()) {
std::string maybe_device = realpath_string(data.sysfs_devchar_realpath + "/device");
if (!maybe_device.empty()) {