forked from tonyrog/hidapi
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhidapi_parser.c
1545 lines (1389 loc) · 50.7 KB
/
hidapi_parser.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
/* hidapi_parser $
*
* Copyright (C) 2013, Marije Baalman <nescivi _at_ gmail.com>
* This work was funded by a crowd-funding initiative for SuperCollider's [1] HID implementation
* including a substantial donation from BEK, Bergen Center for Electronic Arts, Norway
*
* [1] http://supercollider.sourceforge.net
* [2] http://www.bek.no
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "hidapi_parser.h"
// SET IN CMAKE
// #define DEBUG_PARSER
//// ---------- HID descriptor parser
// main items
#define HID_INPUT 0x80
#define HID_OUTPUT 0x90
#define HID_COLLECTION 0xA0
#define HID_FEATURE 0xB0
#define HID_END_COLLECTION 0xC0
// HID Report Items from HID 1.11 Section 6.2.2
#define HID_USAGE_PAGE 0x04
#define HID_USAGE 0x08
#define HID_USAGE_MIN 0x18
#define HID_USAGE_MAX 0x28
#define HID_DESIGNATOR_INDEX 0x38
#define HID_DESIGNATOR_MIN 0x48
#define HID_DESIGNATOR_MAX 0x58
#define HID_STRING_INDEX 0x78
#define HID_STRING_MIN 0x88
#define HID_STRING_MAX 0x98
#define HID_DELIMITER 0xA8
#define HID_LOGICAL_MIN 0x14
#define HID_LOGICAL_MAX 0x24
#define HID_PHYSICAL_MIN 0x34
#define HID_PHYSICAL_MAX 0x44
#define HID_UNIT_EXPONENT 0x54
#define HID_UNIT 0x64
#define HID_REPORT_SIZE 0x74
#define HID_REPORT_ID 0x84
#define HID_REPORT_COUNT 0x94
#define HID_PUSH 0xA4
#define HID_POP 0xB4
#define HID_RESERVED 0xC4 // above this it is all reserved
// HID Report Usage Pages from HID Usage Tables 1.12 Section 3, Table 1
// #define HID_USAGE_PAGE_GENERICDESKTOP 0x01
// #define HID_USAGE_PAGE_KEY_CODES 0x07
// #define HID_USAGE_PAGE_LEDS 0x08
// #define HID_USAGE_PAGE_BUTTONS 0x09
// HID Report Usages from HID Usage Tables 1.12 Section 4, Table 6
// #define HID_USAGE_POINTER 0x01
// #define HID_USAGE_MOUSE 0x02
// #define HID_USAGE_JOYSTICK 0x04
// #define HID_USAGE_KEYBOARD 0x06
// #define HID_USAGE_X 0x30
// #define HID_USAGE_Y 0x31
// #define HID_USAGE_Z 0x32
// #define HID_USAGE_RX 0x33
// #define HID_USAGE_RY 0x34
// #define HID_USAGE_RZ 0x35
// #define HID_USAGE_SLIDER 0x36
// #define HID_USAGE_DIAL 0x37
// #define HID_USAGE_WHEEL 0x38
// HID Report Collection Types from HID 1.12 6.2.2.6
#define HID_COLLECTION_PHYSICAL 0x00
#define HID_COLLECTION_APPLICATION 0x01
#define HID_COLLECTION_LOGICAL 0x02
#define HID_COLLECTION_REPORT 0x03
#define HID_COLLECTION_NAMED_ARRAY 0x04
#define HID_COLLECTION_USAGE_SWITCH 0x05
#define HID_COLLECTION_USAGE_MODIFIER 0x06
#define HID_COLLECTION_RESERVED 0x07
#define HID_COLLECTION_VENDOR 0x80
// HID Input/Output/Feature Item Data (attributes) from HID 1.11 6.2.2.5
/// more like flags - for input, output, and feature
#define HID_ITEM_CONSTANT 0x1 // data(0), constant(1)
#define HID_ITEM_VARIABLE 0x2 // array(0), variable(1)
#define HID_ITEM_RELATIVE 0x4 // absolute(0), relative(1)
#define HID_ITEM_WRAP 0x8 // no wrap(0), wrap(1)
#define HID_ITEM_LINEAR 0x10 // linear(0), non linear(1)
#define HID_ITEM_PREFERRED 0x20 // no preferred(0), preferred(1)
#define HID_ITEM_NULL 0x40 // no null(0), null(1)
#define HID_ITEM_VOLATILE 0x60 // non volatile(0), volatile(1)
#define HID_ITEM_BITFIELD 0x80 // bit field(0), buffered bytes(1)
// Report Types from HID 1.11 Section 7.2.1
#define HID_REPORT_TYPE_INPUT 1
#define HID_REPORT_TYPE_OUTPUT 2
#define HID_REPORT_TYPE_FEATURE 3
#define BITMASK1(n) ((1ULL << (n)) - 1ULL)
#define INVERTBITMASK1(n) ( 255 -(n))
#define BITTESTMASK1(n) (1ULL << (n))
// struct hid_device_descriptor * hid_new_descriptor(){
// struct hid_device_descriptor * descriptor;
// descriptor = (struct hid_device_descriptor *) malloc( sizeof( struct hid_device_descriptor) );
// // hid_descriptor_init( descriptor );
//
// descriptor->first = NULL;
// hid_set_descriptor_callback(descriptor, NULL, NULL);
// hid_set_element_callback(descriptor, NULL, NULL);
// return descriptor;
// }
struct hid_device_element * hid_new_element(){
struct hid_device_element * element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
element->next = NULL;
element->repeat = 0;
element->usage_min = 0;
element->usage_max = 0;
element->logical_min = 0;
element->logical_max = 0;
element->phys_min = 0;
element->phys_max = 0;
element->report_id = 0;
element->unit = 0;
element->unit_exponent = 0;
element->rawvalue = 0;
return element;
}
void hid_free_element( struct hid_device_element * ele ){
free( ele );
}
struct hid_device_collection * hid_new_collection(){
struct hid_device_collection * collection = (struct hid_device_collection *) malloc( sizeof( struct hid_device_collection ) );
collection->first_collection = NULL;
collection->next_collection = NULL;
collection->parent_collection = NULL;
collection->first_element = NULL;
collection->num_collections = 0;
collection->num_elements = 0;
collection->index = -1;
collection->usage_page = 0;
collection->usage_index = 0;
return collection;
}
void hid_free_collection( struct hid_device_collection * coll ){
struct hid_device_element * cur_element = coll->first_element;
struct hid_device_element * next_element;
while (cur_element != NULL ) {
next_element = cur_element->next;
free( cur_element );
cur_element = next_element;
}
struct hid_device_collection * cur_collection = coll->first_collection;
struct hid_device_collection * next_collection;
while (cur_collection != NULL ) {
next_collection = cur_collection->next_collection;
free( cur_collection );
cur_collection = next_collection;
}
free( coll );
}
void hid_set_descriptor_callback( struct hid_dev_desc * devd, hid_descriptor_callback cb, void *user_data ){
devd->_descriptor_callback = cb;
devd->_descriptor_data = user_data;
}
void hid_set_readerror_callback( struct hid_dev_desc * devd, hid_descriptor_callback cb, void *user_data ){
devd->_readerror_callback = cb;
devd->_readerror_data = user_data;
}
void hid_set_element_callback( struct hid_dev_desc * devd, hid_element_callback cb, void *user_data ){
devd->_element_callback = cb;
devd->_element_data = user_data;
}
void hid_set_from_making_element( struct hid_device_element * making, struct hid_device_element * new_element ){
new_element->type = making->type;
new_element->isrelative = (making->type & HID_ITEM_RELATIVE ) > 0;
new_element->isarray = ( (making->type & HID_ITEM_VARIABLE ) == 0 );
new_element->isvariable = (making->type & HID_ITEM_CONSTANT ) == 0;
new_element->usage_page = making->usage_page;
new_element->logical_min = making->logical_min;
new_element->logical_max = making->logical_max;
new_element->usage_min = making->usage_min;
new_element->usage_max = making->usage_max;
if ( (making->phys_min == 0) && (making->phys_max == 0) ){
new_element->phys_min = making->logical_min;
new_element->phys_max = making->logical_max;
} else {
new_element->phys_min = making->phys_min;
new_element->phys_max = making->phys_max;
}
new_element->unit = making->unit;
new_element->unit_exponent = making->unit_exponent;
new_element->report_size = making->report_size;
new_element->report_id = making->report_id;
}
int hid_element_get_signed_value( int inputvalue, int bytesize ){
int outputvalue;
int bitSignIndex = bytesize*8 - 1;
int signBit = 0x1 << bitSignIndex;
if ( signBit & inputvalue ){
unsigned int bitMask = BITMASK1( bytesize*8 );
unsigned int uvalue = (unsigned int) inputvalue;
unsigned int negvalue = ~(uvalue);
negvalue = ~(uvalue) & bitMask;
negvalue = negvalue + 1;
outputvalue = -1 * negvalue;
} else {
outputvalue = inputvalue;
}
return outputvalue;
}
// int hid_parse_report_descriptor( char* descr_buf, int size, struct hid_device_descriptor * descriptor ){
int hid_parse_report_descriptor( unsigned char* descr_buf, int size, struct hid_dev_desc * device_desc ){
struct hid_device_collection * device_collection = hid_new_collection();
device_desc->device_collection = device_collection;
struct hid_device_collection * parent_collection = device_desc->device_collection;
struct hid_device_collection * prev_collection = 0;
struct hid_device_element * prev_element = 0;
struct hid_device_element * making_element = hid_new_element();
int current_usages[256];
int current_usage_index = 0;
int current_report_size;
int current_usage_min = -1;
int current_usage_max = -1;
int current_report_count = 0;
// unsigned char current_input;
// unsigned char current_output;
int collection_nesting = 0;
int next_byte_tag = -1;
int next_byte_size = 0;
int next_byte_type = 0;
int next_val = 0;
unsigned char toadd = 0;
int byte_count = 0;
int i,j;
int numreports = 1;
int report_lengths[256];
int report_ids[256];
report_ids[0] = 0;
report_lengths[0] = 0;
int k;
int index;
device_collection->num_collections = 0;
device_collection->num_elements = 0;
#ifdef DEBUG_PARSER
printf("----------- parsing report descriptor --------------\n " );
#endif
for ( i = 0; i < size; i++){
#ifdef DEBUG_PARSER
printf("\nbuffer value: %02hhx ", descr_buf[i]);
printf("\tbyte_type %i, %i, %i \t", next_byte_tag, next_byte_size, next_val);
#endif
if ( next_byte_tag != -1 ){
// unsigned char ubyte = (unsigned char) descr_buf[i];
// char sbyte = descr_buf[i]; // descr_buf is signed already
int shift = byte_count*8;
int bufval = (int) descr_buf[i];
next_val |= (bufval << shift);
#ifdef DEBUG_PARSER
printf("\t nextval shift: %i", next_val);
#endif
byte_count++;
if ( byte_count == next_byte_size ){
switch( next_byte_tag ){
case HID_USAGE_PAGE:
making_element->usage_page = next_val;
#ifdef DEBUG_PARSER
printf("\n\tusage page: 0x%02hhx", making_element->usage_page);
#endif
break;
case HID_USAGE:
making_element->usage = next_val;
current_usage_min = -1;
current_usage_max = -1;
current_usages[ current_usage_index ] = next_val;
#ifdef DEBUG_PARSER
printf("\n\tusage: 0x%02hhx, %i", current_usages[ current_usage_index ], current_usage_index );
#endif
current_usage_index++;
break;
case HID_COLLECTION:
{
//TODO: COULD ALSO READ WHICH KIND OF COLLECTION
struct hid_device_collection * new_collection = hid_new_collection();
if ( parent_collection->num_collections == 0 ){
parent_collection->first_collection = new_collection;
}
if ( device_collection->num_collections == 0 ){
device_collection->first_collection = new_collection;
} else {
prev_collection->next_collection = new_collection;
}
new_collection->parent_collection = parent_collection;
new_collection->type = next_val;
new_collection->usage_page = making_element->usage_page;
new_collection->usage_index = making_element->usage;
new_collection->usage_min = making_element->usage_min;
new_collection->usage_max = making_element->usage_max;
new_collection->index = device_collection->num_collections;
device_collection->num_collections++;
if ( device_collection != parent_collection ){
parent_collection->num_collections++;
}
parent_collection = new_collection;
prev_collection = new_collection;
collection_nesting++;
#ifdef DEBUG_PARSER
printf("\n\tcollection: %i, %i", collection_nesting, next_val );
#endif
break;
}
case HID_USAGE_MIN:
current_usage_min = next_val;
making_element->usage_min = next_val;
#ifdef DEBUG_PARSER
printf("\n\tusage min: %i", current_usage_min);
#endif
break;
case HID_USAGE_MAX:
current_usage_max = next_val;
making_element->usage_max = next_val;
#ifdef DEBUG_PARSER
printf("\n\tusage max: %i", current_usage_max);
#endif
break;
case HID_LOGICAL_MIN:
making_element->logical_min = hid_element_get_signed_value( next_val, byte_count );
#ifdef DEBUG_PARSER
printf("\n\tlogical min: %i", making_element->logical_min);
#endif
break;
case HID_LOGICAL_MAX:
if ( making_element->logical_min >= 0 ){
making_element->logical_max = next_val;
} else {
making_element->logical_max = hid_element_get_signed_value( next_val, byte_count );
}
#ifdef DEBUG_PARSER
printf("\n\tlogical max: %i", making_element->logical_max);
#endif
break;
case HID_PHYSICAL_MIN:
making_element->phys_min = hid_element_get_signed_value( next_val, byte_count );
#ifdef DEBUG_PARSER
printf("\n\tphysical min: %i", making_element->phys_min);
#endif
break;
case HID_PHYSICAL_MAX:
if ( making_element->phys_min >= 0 ){
making_element->phys_max = next_val;
} else {
making_element->phys_max = hid_element_get_signed_value( next_val, byte_count );
}
#ifdef DEBUG_PARSER
printf("\n\tphysical max: %i", making_element->phys_min);
#endif
break;
case HID_REPORT_COUNT:
current_report_count = next_val;
#ifdef DEBUG_PARSER
printf("\n\treport count: %i", current_report_count);
#endif
break;
case HID_REPORT_SIZE:
making_element->report_size = next_val;
#ifdef DEBUG_PARSER
printf("\n\treport size: %i", making_element->report_size);
#endif
break;
case HID_REPORT_ID:
making_element->report_id = next_val;
// check if report id already exists
int reportexists = 0;
for ( j = 0; j < numreports; j++ ){
reportexists = (report_ids[j] == making_element->report_id);
}
if ( !reportexists ){
report_ids[ numreports ] = making_element->report_id;
report_lengths[ numreports ] = 0;
numreports++;
}
#ifdef DEBUG_PARSER
printf("\n\treport id: %i", making_element->report_id);
#endif
break;
case HID_POP:
// TODO: something useful with pop
#ifdef DEBUG_PARSER
printf("\n\tpop: %i", next_val );
#endif
break;
case HID_PUSH:
// TODO: something useful with push
#ifdef DEBUG_PARSER
printf("\n\tpop: %i", next_val );
#endif
break;
case HID_UNIT:
making_element->unit = next_val;
#ifdef DEBUG_PARSER
printf("\n\tunit: %i", next_val );
#endif
break;
case HID_UNIT_EXPONENT:
making_element->unit_exponent = hid_element_get_signed_value( next_val, byte_count );
#ifdef DEBUG_PARSER
printf("\n\tunit exponent: %i", next_val );
#endif
break;
case HID_INPUT:
#ifdef DEBUG_PARSER
printf("\n\tinput: %i", next_val);
printf("\tmaking_element->usage: %i", making_element->usage);
#endif
making_element->type = next_val;
// add the elements for this report
for ( j=0; j<current_report_count; j++ ){
struct hid_device_element * new_element = hid_new_element();
// = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
new_element->io_type = 1;
new_element->index = device_collection->num_elements;
new_element->parent_collection = parent_collection;
hid_set_from_making_element( making_element, new_element );
if ( current_usage_min == -1 ){
new_element->usage = current_usages[j]; /// FIXME
} else {
new_element->usage = current_usage_min + j;
}
new_element->report_index = j;
new_element->value = 0;
new_element->array_value = 0;
if ( parent_collection->num_elements == 0 ){
parent_collection->first_element = new_element;
}
if ( device_collection->num_elements == 0 ){
device_collection->first_element = new_element;
}
device_collection->num_elements++;
if ( parent_collection != device_collection ) {
parent_collection->num_elements++;
}
if ( prev_element != NULL ){
prev_element->next = new_element;
}
prev_element = new_element;
}
for ( j=0; j < current_usage_index; j++ ){
current_usages[j] = 0;
}
current_usage_index = 0;
current_usage_min = -1;
current_usage_max = -1;
making_element->usage_min = -1;
making_element->usage_max = -1;
making_element->usage = 0;
break;
case HID_OUTPUT:
#ifdef DEBUG_PARSER
printf("\n\toutput: %i", next_val);
printf("\tmaking_element->usage: %i", making_element->usage);
#endif
making_element->type = next_val;
// add the elements for this report
for ( j=0; j<current_report_count; j++ ){
struct hid_device_element * new_element = hid_new_element();
// struct hid_device_element * new_element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
new_element->io_type = 2;
new_element->index = device_collection->num_elements;
new_element->parent_collection = parent_collection;
hid_set_from_making_element( making_element, new_element );
if ( current_usage_min == -1 ){
new_element->usage = current_usages[j]; /// FIXME
} else {
new_element->usage = current_usage_min + j;
}
new_element->report_index = j;
index = 0;
for ( k=0; k<numreports; k++ ){
if ( making_element->report_id == report_ids[k] ){
index = k;
break;
}
}
report_lengths[index] += making_element->report_size;
new_element->value = 0;
new_element->array_value = 0;
if ( parent_collection->num_elements == 0 ){
parent_collection->first_element = new_element;
}
if ( device_collection->num_elements == 0 ){
device_collection->first_element = new_element;
}
device_collection->num_elements++;
if ( parent_collection != device_collection ) {
parent_collection->num_elements++;
}
if ( prev_element != NULL ){
prev_element->next = new_element;
}
prev_element = new_element;
}
for ( j=0; j < current_usage_index; j++ ){
current_usages[j] = 0;
}
current_usage_index = 0;
current_usage_min = -1;
current_usage_max = -1;
making_element->usage_min = -1;
making_element->usage_max = -1;
making_element->usage = 0;
break;
case HID_FEATURE:
#ifdef DEBUG_PARSER
printf("\n\tfeature: %i", next_val);
printf("\tcurrent_usage: %i", making_element->usage);
#endif
making_element->type = next_val;
// add the elements for this report
for ( j=0; j<current_report_count; j++ ){
struct hid_device_element * new_element = hid_new_element();
new_element->io_type = 3;
new_element->index = device_collection->num_elements;
new_element->parent_collection = parent_collection;
hid_set_from_making_element( making_element, new_element );
if ( current_usage_min == -1 ){
new_element->usage = current_usages[j]; /// FIXME
} else {
new_element->usage = current_usage_min + j;
}
new_element->report_index = j;
new_element->value = 0;
new_element->array_value = 0;
if ( parent_collection->num_elements == 0 ){
parent_collection->first_element = new_element;
}
if ( device_collection->num_elements == 0 ){
device_collection->first_element = new_element;
}
device_collection->num_elements++;
if ( parent_collection != device_collection ) {
parent_collection->num_elements++;
}
if ( prev_element != NULL ){
prev_element->next = new_element;
}
prev_element = new_element;
}
for ( j=0; j < current_usage_index; j++ ){
current_usages[j] = 0;
}
current_usage_index = 0;
current_usage_min = -1;
current_usage_max = -1;
making_element->usage_min = -1;
making_element->usage_max = -1;
making_element->usage = 0;
break;
#ifdef DEBUG_PARSER
default:
if ( next_byte_tag >= HID_RESERVED ){
printf("\n\treserved bytes 0x%02hhx, %i", next_byte_tag, next_val );
} else {
printf("\n\tundefined byte type 0x%02hhx, %i", next_byte_tag, next_val );
}
#endif
}
next_byte_tag = -1;
}
} else {
#ifdef DEBUG_PARSER
printf("\tsetting next byte type: %i, 0x%02hhx ", descr_buf[i], descr_buf[i] );
#endif
if ( descr_buf[i] == HID_END_COLLECTION ){ // JUST one byte
// prev_collection = parent_collection;
making_element->usage_page = parent_collection->usage_page;
making_element->usage = parent_collection->usage_index;
parent_collection = parent_collection->parent_collection;
for ( j=0; j < current_usage_index; j++ ){
current_usages[j] = 0;
}
making_element->usage_min = -1;
making_element->usage_max = -1;
current_usage_index = 0;
current_usage_min = -1;
current_usage_max = -1;
collection_nesting--;
#ifdef DEBUG_PARSER
printf("\n\tend collection: %i, %i\n", collection_nesting, descr_buf[i] );
#endif
} else {
byte_count = 0;
next_val = 0;
// u_next_val = 0;
next_byte_tag = descr_buf[i] & 0xFC;
next_byte_type = descr_buf[i] & 0x0C;
next_byte_size = descr_buf[i] & 0x03;
if ( next_byte_size == 3 ){
next_byte_size = 4;
}
#ifdef DEBUG_PARSER
printf("\t next byte type: 0x%02hhx, %i, %i ", next_byte_tag, next_byte_type, next_byte_size );
#endif
}
}
}
#ifdef DEBUG_PARSER
printf("----------- end parsing report descriptor --------------\n " );
#endif
device_desc->number_of_reports = numreports;
device_desc->report_lengths = (int*) malloc( sizeof( int ) * numreports );
device_desc->report_ids = (int*) malloc( sizeof( int ) * numreports );
for ( j = 0; j<numreports; j++ ){
device_desc->report_lengths[j] = report_lengths[j];
device_desc->report_ids[j] = report_ids[j];
}
#ifdef DEBUG_PARSER
printf("----------- end setting report ids --------------\n " );
#endif
return 0;
}
void hid_element_set_value_from_input( struct hid_device_element * element, int value ){
element->rawvalue = value;
if ( element->logical_min < 0 ){
// value should be interpreted as signed value
// so: check report size, test the highest bit, if one, invert and add one, otherwise keep value
int bitSignIndex = element->report_size - 1;
int signBit = 0x1 << bitSignIndex;
if ( signBit & value ){
unsigned int bitMask = BITMASK1( element->report_size );
unsigned int uvalue = (unsigned int) value;
unsigned int negvalue = ~(uvalue);
negvalue = ~(uvalue) & bitMask;
negvalue = negvalue + 1;
element->value = -1 * negvalue;
} else {
element->value = value;
}
} else {
// value should be interpreted as unsigned value
// so: keep value as is
if ( element->isarray ){ // array elements should be parsed differently
if ( value == 0 ){ // previous key was pressed, so keep previous usage
element->value = 0;
element->array_value = 0;
} else { // new key, so value + usage min is the current usage
element->usage = element->usage_min + value;
element->value = 1;
element->array_value = value;
}
} else {
element->value = value;
}
}
}
float hid_element_map_logical( struct hid_device_element * element ){
float result;
if ( element->isarray ){
result = (float) element->value;
} else {
result = ( (float) element->value - (float) element->logical_min)/( (float) element->logical_max - (float) element->logical_min );
}
return result;
}
/** TODO: this needs a linking with the math library */
float hid_element_resolution( struct hid_device_element * element ){
float result = 0;
// result = ( element->logical_max - element->logical_min) / ( ( element->phys_max - element->phys_min) * pow(10, element->unit_exponent) );
return result;
}
float hid_element_map_physical( struct hid_device_element * element ){
float result;
float logicalvalue = hid_element_map_logical(element);
result = logicalvalue * ( element->phys_max - element->phys_min ) + element->phys_min;
return result;
}
/** is this used anywhere? */
void hid_element_set_rawvalue( struct hid_device_element * element, int value ){
element->value = value;
}
/** is this used anywhere? */
void hid_element_set_logicalvalue( struct hid_device_element * element, float value ){
int mapvalue;
mapvalue = (int) ( value * ( (float) element->logical_max - (float) element->logical_min ) ) - element->logical_min;
element->value = mapvalue;
}
struct hid_device_element * hid_get_next_input_element( struct hid_device_element * curel ){
struct hid_device_element * nextel = curel->next;
while ( nextel != NULL ){
if ( nextel->io_type == 1 ){
return nextel;
} else {
nextel = nextel->next;
}
}
return nextel; // is NULL
// return curel; // return the previous element
// is NULL
}
struct hid_device_element * hid_get_next_input_element_with_reportid( struct hid_device_element * curel, int reportid ){
struct hid_device_element * nextel = curel->next;
while ( nextel != NULL ){
if ( nextel->io_type == 1 && ( nextel->report_id == reportid ) ){
return nextel;
} else {
nextel = nextel->next;
}
}
return nextel;
// return curel; // return the previous element
// is NULL
}
struct hid_device_element * hid_get_next_output_element( struct hid_device_element * curel ){
struct hid_device_element * nextel = curel->next;
while ( nextel != NULL ){
if ( nextel->io_type == 2 ){
return nextel;
} else {
nextel = nextel->next;
}
}
return nextel;
// return curel; // return the previous element
// is NULL
}
struct hid_device_element * hid_get_next_output_element_with_reportid( struct hid_device_element * curel, int reportid ){
struct hid_device_element * nextel = curel->next;
while ( nextel != NULL ){
if ( nextel->io_type == 2 && ( nextel->report_id == reportid ) ){
return nextel;
} else {
nextel = nextel->next;
}
}
return NULL;
// return curel; // return the previous element
// is NULL
}
struct hid_device_element * hid_get_next_feature_element( struct hid_device_element * curel ){
struct hid_device_element * nextel = curel->next;
while ( nextel != NULL ){
if ( nextel->io_type == 3 ){
return nextel;
} else {
nextel = nextel->next;
}
}
return nextel;
//return curel; // return the previous element
// is NULL
}
struct hid_parsing_byte {
int nextVal;
int currentSize;
int bitIndex;
int remainingBits;
int shiftedByte;
};
int hid_parse_single_byte( unsigned char current_byte, struct hid_parsing_byte * pbyte ){
int nextVal;
unsigned char bitMask;
unsigned char invBitMask;
unsigned char maskedByte;
int currentBitsize = pbyte->currentSize - pbyte->bitIndex;
if ( currentBitsize >= pbyte->remainingBits ){
// using the full byte
nextVal = ( current_byte << pbyte->bitIndex );
pbyte->bitIndex += pbyte->remainingBits;
pbyte->remainingBits = 0;
} else {
// use a partial byte:
bitMask = BITMASK1( currentBitsize );
nextVal = bitMask & current_byte;
nextVal = nextVal << pbyte->bitIndex;
pbyte->remainingBits -= currentBitsize;
// shift the remaining value
invBitMask = INVERTBITMASK1( bitMask );
maskedByte = current_byte & invBitMask;
pbyte->shiftedByte = maskedByte >> currentBitsize;
pbyte->bitIndex = pbyte->currentSize; // is this always true?
};
pbyte->nextVal += nextVal;
if ( (pbyte->currentSize - pbyte->bitIndex) == 0 ){
pbyte->bitIndex = 0;
nextVal = pbyte->nextVal;
pbyte->nextVal = 0;
return nextVal;
}
return -1;
}
int hid_parse_input_report( unsigned char* buf, int size, struct hid_dev_desc * devdesc ){
#ifdef APPLE
return hid_parse_input_elements_values( buf, devdesc );
#endif
#ifdef WIN32
return hid_parse_input_elements_values( buf, devdesc );
#endif
#ifdef LINUX_FREEBSD
struct hid_parsing_byte pbyte;
pbyte.nextVal = 0;
pbyte.currentSize = 10;
pbyte.bitIndex = 0;
pbyte.remainingBits = 0;
pbyte.shiftedByte = 0;
struct hid_device_collection * device_collection = devdesc->device_collection;
struct hid_device_element * cur_element = device_collection->first_element;
int newvalue;
int i;
int starti = 0;
int reportid = 0;
if ( devdesc->number_of_reports > 1 ){
reportid = (int) buf[i];
starti = 1;
}
if ( cur_element->io_type != 1 || ( cur_element->report_id != reportid ) ){
cur_element = hid_get_next_input_element_with_reportid(cur_element, reportid );
}
for ( i = starti; i < size; i++){
unsigned char curbyte = buf[i];
pbyte.remainingBits = 8;
pbyte.shiftedByte = curbyte;
while( pbyte.remainingBits > 0 ) {
// get next element
pbyte.currentSize = cur_element->report_size;
newvalue = hid_parse_single_byte( pbyte.shiftedByte, &pbyte );
if ( newvalue != -1 ){
if ( devdesc->_element_callback != NULL ){
if ( newvalue != cur_element->rawvalue || cur_element->repeat ){
hid_element_set_value_from_input( cur_element, newvalue );
devdesc->_element_callback( cur_element, devdesc->_element_data );
}
}
cur_element = hid_get_next_input_element_with_reportid( cur_element, reportid );
}
}
}
return 0;
#endif
}
void hid_throw_readerror( struct hid_dev_desc * devd ){
devd->_readerror_callback( devd, devd->_readerror_data );
}
int hid_send_output_report( struct hid_dev_desc * devd, int reportid ){
char * buf;
// find the right report id
int index = 0;
int i;
for ( i=0; i<devd->number_of_reports; i++ ){
if ( reportid == devd->report_ids[i] ){
index = i;
break;
}
}
size_t buflength = devd->report_lengths[ index ] / 8;
#ifdef DEBUG_PARSER
printf("report id %i, buflength %i\t", reportid, buflength );
#endif
// if ( reportid != 0 ){
buflength++; // one more byte if report id is not 0
// }
buf = (char *) malloc( sizeof( char ) * buflength );
memset(buf, 0x0, sizeof(char) * buflength);
// iterate over elements, find which ones are output elements with the right report id,
// and set their output values to the buffer
struct hid_device_collection * device_collection = devd->device_collection;
struct hid_device_element * cur_element = device_collection->first_element;
if ( cur_element->io_type != 2 || ( cur_element->report_id != reportid ) ){
cur_element = hid_get_next_output_element_with_reportid(cur_element, reportid);
}
#ifdef DEBUG_PARSER
printf("-----------------------\n");
#endif
buf[0] = reportid;
int byte_index = 1;
int bit_offset = 0;
int next_val = 0;
while ( cur_element != NULL && (byte_index < buflength) ){
int current_output = 0;
unsigned char current_byte = 0;
int current_bit_size = cur_element->report_size;
int current_byte_size = (int) ceil( (float) current_bit_size / 8);
#ifdef DEBUG_PARSER
printf("report_size %i, bytesize %i, bitsize %i, bitoffset %i, byte_index %i \n", cur_element->report_size, current_byte_size, current_bit_size, bit_offset, byte_index );
printf("current_output %i \t", current_output );
#endif
current_output = cur_element->value << bit_offset;
#ifdef DEBUG_PARSER
printf("current_output shift %i \t", current_output );
#endif
int i;
for ( i=0; i<current_byte_size; i++ ){
current_byte = current_output % 256;
current_output = current_output >> 8;
buf[ byte_index + i ] += current_byte;
#ifdef DEBUG_PARSER
printf("current_output %i, current_byte %i, buf %i\t", current_output, current_byte, buf[ byte_index + i ] );
#endif