* The caller is responsible for free()ing the (newly-allocated) character * strings pointed to by serial_number_utf8 and product_name_utf8 after use. */
| 508 | * strings pointed to by serial_number_utf8 and product_name_utf8 after use. |
| 509 | */ |
| 510 | static int parse_uevent_info(const char *uevent, unsigned *bus_type, |
| 511 | unsigned short *vendor_id, unsigned short *product_id, |
| 512 | char **serial_number_utf8, char **product_name_utf8) |
| 513 | { |
| 514 | char tmp[1024]; |
| 515 | size_t uevent_len = strlen(uevent); |
| 516 | if (uevent_len > sizeof(tmp) - 1) |
| 517 | uevent_len = sizeof(tmp) - 1; |
| 518 | memcpy(tmp, uevent, uevent_len); |
| 519 | tmp[uevent_len] = '\0'; |
| 520 | |
| 521 | char *saveptr = NULL; |
| 522 | char *line; |
| 523 | char *key; |
| 524 | char *value; |
| 525 | |
| 526 | int found_id = 0; |
| 527 | int found_serial = 0; |
| 528 | int found_name = 0; |
| 529 | |
| 530 | line = strtok_r(tmp, "\n", &saveptr); |
| 531 | while (line != NULL) { |
| 532 | /* line: "KEY=value" */ |
| 533 | key = line; |
| 534 | value = strchr(line, '='); |
| 535 | if (!value) { |
| 536 | goto next_line; |
| 537 | } |
| 538 | *value = '\0'; |
| 539 | value++; |
| 540 | |
| 541 | if (strcmp(key, "HID_ID") == 0) { |
| 542 | /** |
| 543 | * type vendor product |
| 544 | * HID_ID=0003:000005AC:00008242 |
| 545 | **/ |
| 546 | int ret = sscanf(value, "%x:%hx:%hx", bus_type, vendor_id, product_id); |
| 547 | if (ret == 3) { |
| 548 | found_id = 1; |
| 549 | } |
| 550 | } else if (strcmp(key, "HID_NAME") == 0) { |
| 551 | /* The caller has to free the product name */ |
| 552 | *product_name_utf8 = strdup(value); |
| 553 | found_name = 1; |
| 554 | } else if (strcmp(key, "HID_UNIQ") == 0) { |
| 555 | /* The caller has to free the serial number */ |
| 556 | *serial_number_utf8 = strdup(value); |
| 557 | found_serial = 1; |
| 558 | } |
| 559 | |
| 560 | next_line: |
| 561 | line = strtok_r(NULL, "\n", &saveptr); |
| 562 | } |
| 563 | |
| 564 | return (found_id && found_name && found_serial); |
| 565 | } |
| 566 | |
| 567 |
no outgoing calls
no test coverage detected