Changes in uspace/lib/usb/src/hidparser.c [70a71e5:b53d3b7] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usb/src/hidparser.c
r70a71e5 rb53d3b7 31 31 */ 32 32 /** @file 33 * HID report descriptor and report dataparser implementation.33 * @brief HID parser implementation. 34 34 */ 35 35 #include <usb/classes/hidparser.h> … … 40 40 #include <usb/debug.h> 41 41 42 /** */43 42 #define USB_HID_NEW_REPORT_ITEM 1 44 45 /** */46 43 #define USB_HID_NO_ACTION 2 47 48 /** */49 44 #define USB_HID_UNKNOWN_TAG -99 50 45 51 /* 52 * Private descriptor parser functions 53 */ 46 #define BAD_HACK_USAGE_PAGE 0x07 47 54 48 int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size, 55 49 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path); … … 64 58 int usb_hid_report_reset_local_items(); 65 59 void usb_hid_free_report_list(link_t *head); 66 67 /*68 * Data translation private functions69 */70 60 int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size); 71 61 inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset); 72 62 int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j); 73 int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int32_t value);74 63 int usb_pow(int a, int b); 75 64 76 // TODO: tohle ma bejt asi jinde 65 77 66 int usb_pow(int a, int b) 78 67 { … … 91 80 92 81 /** 93 * Initialize the report descriptor parser structure 94 * 95 * @param parser Report descriptor parser structure 96 * @return Error code 82 * 97 83 */ 98 84 int usb_hid_parser_init(usb_hid_report_parser_t *parser) 99 85 { 100 101 102 103 104 86 if(parser == NULL) { 87 return EINVAL; 88 } 89 90 list_initialize(&(parser->input)); 105 91 list_initialize(&(parser->output)); 106 92 list_initialize(&(parser->feature)); … … 178 164 // store current usage path 179 165 report_item->usage_path = usage_path; 166 167 // new current usage path 168 tmp_usage_path = usb_hid_report_path(); 180 169 181 // c lonepath to the new one182 tmp_usage_path = usb_hid_report_path_clone(usage_path);170 // copy old path to the new one 171 usb_hid_report_path_clone(tmp_usage_path, usage_path); 183 172 184 173 // swap … … 315 304 316 305 /** 317 * Parse one tag of the report descriptor318 306 * 319 307 * @param Tag to parse … … 403 391 * @return Error code 404 392 */ 393 405 394 int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size, 406 395 usb_hid_report_item_t *report_item, usb_hid_report_path_t *usage_path) … … 531 520 * Prints content of given list of report items. 532 521 * 533 * @param List of report items (usb_hid_report_item_t)522 * @param List of report items 534 523 * @return void 535 524 */ … … 563 552 path = path->next; 564 553 } 565 554 555 556 // usb_log_debug("\tUSAGE: %X\n", report_item->usage); 557 // usb_log_debug("\tUSAGE PAGE: %X\n", report_item->usage_page); 566 558 usb_log_debug("\tLOGMIN: %X\n", report_item->logical_minimum); 567 559 usb_log_debug("\tLOGMAX: %X\n", report_item->logical_maximum); … … 578 570 } 579 571 /** 580 * Prints content of given reportdescriptor in human readable format.581 * 582 * @param parserParsed descriptor to print572 * Prints content of given descriptor in human readable format. 573 * 574 * @param Parsed descriptor to print 583 575 * @return void 584 576 */ … … 603 595 * Releases whole linked list of report items 604 596 * 605 * @param head Head of list of report descriptor items (usb_hid_report_item_t) 606 * @return void 597 * 607 598 */ 608 599 void usb_hid_free_report_list(link_t *head) … … 636 627 } 637 628 638 /** Free s the HID report descriptorparser structure629 /** Free the HID report parser structure 639 630 * 640 631 * @param parser Opaque HID report parser structure 641 * @return void632 * @return Error code 642 633 */ 643 634 void usb_hid_free_report_parser(usb_hid_report_parser_t *parser) … … 669 660 const usb_hid_report_in_callbacks_t *callbacks, void *arg) 670 661 { 662 /* 663 * 664 * only key codes (usage page 0x07) will be processed 665 * other usages will be ignored 666 */ 671 667 link_t *list_item; 672 668 usb_hid_report_item_t *item; … … 680 676 return EINVAL; 681 677 } 682 683 /* get the size of result array */ 678 679 680 // get the size of result keycodes array 684 681 key_count = usb_hid_report_input_length(parser, path, flags); 685 682 … … 688 685 } 689 686 690 / * read data */687 // read data 691 688 list_item = parser->input.next; 692 689 while(list_item != &(parser->input)) { … … 722 719 } 723 720 724 /** 725 * Translate data from the report as specified in report descriptor 726 * 727 * @param item Report descriptor item with definition of translation 728 * @param data Data to translate 729 * @param j Index of processed field in report descriptor item 730 * @return Translated data 731 */ 721 732 722 int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j) 733 723 { … … 806 796 } 807 797 808 /** 809 * 810 * 811 * @param parser 812 * @param path 813 * @param flags 814 * @return 815 */ 816 size_t usb_hid_report_input_length(const usb_hid_report_parser_t *parser, 798 int usb_hid_report_input_length(const usb_hid_report_parser_t *parser, 817 799 usb_hid_report_path_t *path, int flags) 818 800 { 819 size_t ret = 0;801 int ret = 0; 820 802 link_t *item; 821 803 usb_hid_report_item_t *report_item; 822 804 823 805 if(parser == NULL) { 824 return 0;825 } 826 827 item = parser->input.next;806 return EINVAL; 807 } 808 809 item = (&parser->input)->next; 828 810 while(&parser->input != item) { 829 811 report_item = list_get_instance(item, usb_hid_report_item_t, link); … … 842 824 /** 843 825 * 844 * @param usage_path845 * @param usage_page846 * @param usage847 * @return848 826 */ 849 827 int usb_hid_report_path_append_item(usb_hid_report_path_t *usage_path, … … 867 845 /** 868 846 * 869 * @param usage_path870 * @return871 847 */ 872 848 void usb_hid_report_remove_last_item(usb_hid_report_path_t *usage_path) … … 884 860 /** 885 861 * 886 * @param usage_path887 * @return888 862 */ 889 863 void usb_hid_report_null_last_item(usb_hid_report_path_t *usage_path) … … 899 873 /** 900 874 * 901 * @param usage_path902 * @param tag903 * @param data904 * @return905 875 */ 906 876 void usb_hid_report_set_last_item(usb_hid_report_path_t *usage_path, int32_t tag, int32_t data) … … 924 894 925 895 /** 926 * 927 * 928 * @param report_path 929 * @param path 930 * @param flags 931 * @return 896 * 932 897 */ 933 898 int usb_hid_report_compare_usage_path(usb_hid_report_path_t *report_path, … … 984 949 break; 985 950 986 /* compare with only the end of path*/951 /* given path must be the end of the report one*/ 987 952 case USB_HID_PATH_COMPARE_END: 988 953 report_link = report_path->link.prev; … … 1027 992 /** 1028 993 * 1029 * @return1030 994 */ 1031 995 usb_hid_report_path_t *usb_hid_report_path(void) … … 1045 1009 /** 1046 1010 * 1047 * @param path1048 * @return void1049 1011 */ 1050 1012 void usb_hid_report_path_free(usb_hid_report_path_t *path) … … 1057 1019 1058 1020 /** 1059 * Clone content of given usage path to the new one 1060 * 1061 * @param usage_path 1062 * @return 1063 */ 1064 usb_hid_report_path_t *usb_hid_report_path_clone(usb_hid_report_path_t *usage_path) 1021 * 1022 */ 1023 int usb_hid_report_path_clone(usb_hid_report_path_t *new_usage_path, usb_hid_report_path_t *usage_path) 1065 1024 { 1066 1025 usb_hid_report_usage_path_t *path_item; 1067 1026 link_t *path_link; 1068 usb_hid_report_path_t *new_usage_path = usb_hid_report_path (); 1069 1070 if(new_usage_path == NULL){ 1071 return NULL; 1072 } 1027 1073 1028 1074 1029 if(list_empty(&usage_path->link)){ 1075 return new_usage_path;1030 return EOK; 1076 1031 } 1077 1032 … … 1084 1039 } 1085 1040 1086 return new_usage_path;1087 }1088 1089 1090 /*** OUTPUT API **/1091 1092 /** Allocates output report buffer1093 *1094 * @param parser1095 * @param size1096 * @return1097 */1098 uint8_t *usb_hid_report_output(usb_hid_report_parser_t *parser, size_t *size)1099 {1100 if(parser == NULL) {1101 *size = 0;1102 return NULL;1103 }1104 1105 // read the last output report item1106 usb_hid_report_item_t *last;1107 link_t *link;1108 1109 link = parser->output.prev;1110 if(link != &parser->output) {1111 last = list_get_instance(link, usb_hid_report_item_t, link);1112 *size = (last->offset + (last->size * last->count)) / 8;1113 1114 uint8_t *buffer = malloc(sizeof(uint8_t) * (*size));1115 memset(buffer, 0, sizeof(uint8_t) * (*size));1116 usb_log_debug("output buffer: %s\n", usb_debug_str_buffer(buffer, *size, 0));1117 1118 return buffer;1119 }1120 else {1121 *size = 0;1122 return NULL;1123 }1124 }1125 1126 1127 /** Frees output report buffer1128 *1129 * @param output Output report buffer1130 * @return1131 */1132 void usb_hid_report_output_free(uint8_t *output)1133 1134 {1135 if(output != NULL) {1136 free (output);1137 }1138 }1139 1140 /** Returns size of output for given usage path1141 *1142 * @param parser1143 * @param path1144 * @param flags1145 * @return1146 */1147 size_t usb_hid_report_output_size(usb_hid_report_parser_t *parser,1148 usb_hid_report_path_t *path, int flags)1149 {1150 size_t ret = 0;1151 link_t *item;1152 usb_hid_report_item_t *report_item;1153 1154 if(parser == NULL) {1155 return 0;1156 }1157 1158 item = parser->output.next;1159 while(&parser->output != item) {1160 report_item = list_get_instance(item, usb_hid_report_item_t, link);1161 if(!USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags) &&1162 (usb_hid_report_compare_usage_path(report_item->usage_path, path, flags) == EOK)) {1163 ret += report_item->count;1164 }1165 1166 item = item->next;1167 }1168 1169 return ret;1170 1171 }1172 1173 /** Updates the output report buffer by translated given data1174 *1175 * @param parser1176 * @param path1177 * @param flags1178 * @param buffer1179 * @param size1180 * @param data1181 * @param data_size1182 * @return1183 */1184 int usb_hid_report_output_translate(usb_hid_report_parser_t *parser,1185 usb_hid_report_path_t *path, int flags,1186 uint8_t *buffer, size_t size,1187 int32_t *data, size_t data_size)1188 {1189 usb_hid_report_item_t *report_item;1190 link_t *item;1191 size_t idx=0;1192 int i=0;1193 int32_t value=0;1194 int offset;1195 int length;1196 int32_t tmp_value;1197 1198 if(parser == NULL) {1199 return EINVAL;1200 }1201 1202 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));1203 usb_log_debug("OUTPUT DATA[0]: %d, DATA[1]: %d, DATA[2]: %d\n", data[0], data[1], data[2]);1204 1205 item = parser->output.next;1206 while(item != &parser->output) {1207 report_item = list_get_instance(item, usb_hid_report_item_t, link);1208 1209 for(i=0; i<report_item->count; i++) {1210 1211 if(idx >= data_size) {1212 break;1213 }1214 1215 if((USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) ||1216 ((report_item->usage_minimum == 0) && (report_item->usage_maximum == 0))) {1217 1218 // // variable item1219 value = usb_hid_translate_data_reverse(report_item, data[idx++]);1220 offset = report_item->offset + (i * report_item->size);1221 length = report_item->size;1222 }1223 else {1224 //bitmap1225 value += usb_hid_translate_data_reverse(report_item, data[idx++]);1226 offset = report_item->offset;1227 length = report_item->size * report_item->count;1228 }1229 1230 if((offset/8) == ((offset+length-1)/8)) {1231 // je to v jednom bytu1232 if(((size_t)(offset/8) >= size) || ((size_t)(offset+length-1)/8) >= size) {1233 break; // TODO ErrorCode1234 }1235 1236 size_t shift = offset%8;1237 1238 value = value << shift;1239 value = value & (((1 << length)-1) << shift);1240 1241 uint8_t mask = 0;1242 mask = 0xff - (((1 << length) - 1) << shift);1243 buffer[offset/8] = (buffer[offset/8] & mask) | value;1244 }1245 else {1246 // je to ve dvou!! FIXME: melo by to umet delsi jak 21247 1248 // konec prvniho -- dolni x bitu1249 tmp_value = value;1250 tmp_value = tmp_value & ((1 << (8-(offset%8)))-1);1251 tmp_value = tmp_value << (offset%8);1252 1253 uint8_t mask = 0;1254 mask = ~(((1 << (8-(offset%8)))-1) << (offset%8));1255 buffer[offset/8] = (buffer[offset/8] & mask) | tmp_value;1256 1257 // a ted druhej -- hornich length-x bitu1258 value = value >> (8 - (offset % 8));1259 value = value & ((1 << (length - (8 - (offset % 8)))) - 1);1260 1261 mask = ((1 << (length - (8 - (offset % 8)))) - 1);1262 buffer[(offset+length-1)/8] = (buffer[(offset+length-1)/8] & mask) | value;1263 }1264 1265 }1266 1267 item = item->next;1268 }1269 1270 usb_log_debug("OUTPUT BUFFER: %s\n", usb_debug_str_buffer(buffer,size, 0));1271 1272 1041 return EOK; 1273 1042 } 1274 1043 1275 /**1276 *1277 * @param item1278 * @param value1279 * @return1280 */1281 int32_t usb_hid_translate_data_reverse(usb_hid_report_item_t *item, int value)1282 {1283 int ret=0;1284 int resolution;1285 1286 if(USB_HID_ITEM_FLAG_CONSTANT(item->item_flags)) {1287 ret = item->logical_minimum;1288 }1289 1290 if((USB_HID_ITEM_FLAG_VARIABLE(item->item_flags) == 0)) {1291 1292 // variable item1293 if((item->physical_minimum == 0) && (item->physical_maximum == 0)) {1294 item->physical_minimum = item->logical_minimum;1295 item->physical_maximum = item->logical_maximum;1296 }1297 1298 if(item->physical_maximum == item->physical_minimum){1299 resolution = 1;1300 }1301 else {1302 resolution = (item->logical_maximum - item->logical_minimum) /1303 ((item->physical_maximum - item->physical_minimum) *1304 (usb_pow(10,(item->unit_exponent))));1305 }1306 1307 ret = ((value - item->physical_minimum) * resolution) + item->logical_minimum;1308 }1309 else {1310 // bitmapa1311 if(value == 0) {1312 ret = 0;1313 }1314 else {1315 size_t bitmap_idx = (value - item->usage_minimum);1316 ret = 1 << bitmap_idx;1317 }1318 }1319 1320 1321 return ret;1322 }1323 1324 1044 1325 1045 /**
Note:
See TracChangeset
for help on using the changeset viewer.