Skip to content

usb_hid.set_interface_name #8989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions shared-bindings/usb_hid/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,39 @@ STATIC mp_obj_t usb_hid_get_boot_device(void) {
}
MP_DEFINE_CONST_FUN_OBJ_0(usb_hid_get_boot_device_obj, usb_hid_get_boot_device);


//| def set_interface_name(interface_name: str) -> None:
//| """Override HID interface name in the USB Interface Descriptor.
//|
//| ``interface_name`` must be an ASCII string (or buffer) of at most 126.
//|
//| This method must be called in boot.py to have any effect.
//|
//| Not available on boards without native USB support.
//| """
//| ...
//|
STATIC mp_obj_t usb_hid_set_interface_name(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_interface_name, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = mp_const_none} }
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);

mp_buffer_info_t interface_name;
mp_get_buffer_raise(args[0].u_obj, &interface_name, MP_BUFFER_READ);
mp_arg_validate_length_range(interface_name.len, 1, 126, MP_QSTR_interface_name);

if (custom_usb_hid_interface_name == NULL) {
custom_usb_hid_interface_name = port_malloc(sizeof(char) * 128, false);
}
memcpy(custom_usb_hid_interface_name, interface_name.buf, interface_name.len);
custom_usb_hid_interface_name[interface_name.len] = 0;

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(usb_hid_set_interface_name_obj, 1, usb_hid_set_interface_name);

// usb_hid.devices is set once the usb devices are determined, after boot.py runs.
STATIC mp_map_elem_t usb_hid_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid) },
Expand All @@ -163,6 +196,7 @@ STATIC mp_map_elem_t usb_hid_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_disable), MP_OBJ_FROM_PTR(&usb_hid_disable_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable), MP_OBJ_FROM_PTR(&usb_hid_enable_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_boot_device), MP_OBJ_FROM_PTR(&usb_hid_get_boot_device_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_interface_name), MP_OBJ_FROM_PTR(&usb_hid_set_interface_name_obj) },
};

STATIC MP_DEFINE_MUTABLE_DICT(usb_hid_module_globals, usb_hid_module_globals_table);
Expand Down
2 changes: 2 additions & 0 deletions shared-module/usb_hid/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ const usb_hid_device_obj_t usb_hid_device_consumer_control_obj = {
.out_report_lengths = { 0, },
};

char *custom_usb_hid_interface_name;

STATIC size_t get_report_id_idx(usb_hid_device_obj_t *self, size_t report_id) {
for (size_t i = 0; i < self->num_report_ids; i++) {
if (report_id == self->report_ids[i]) {
Expand Down
2 changes: 2 additions & 0 deletions shared-module/usb_hid/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ extern const usb_hid_device_obj_t usb_hid_device_consumer_control_obj;

void usb_hid_device_create_report_buffers(usb_hid_device_obj_t *self);

extern char *custom_usb_hid_interface_name;

#endif /* SHARED_MODULE_USB_HID_DEVICE_H */
9 changes: 7 additions & 2 deletions shared-module/usb_hid/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ size_t usb_hid_descriptor_length(void) {
return sizeof(usb_hid_descriptor_template);
}

static const char usb_hid_interface_name[] = USB_INTERFACE_NAME " HID";

// This is the interface descriptor, not the report descriptor.
size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string, uint16_t report_descriptor_length, uint8_t boot_device) {
const char *usb_hid_interface_name;

memcpy(descriptor_buf, usb_hid_descriptor_template, sizeof(usb_hid_descriptor_template));

descriptor_buf[HID_DESCRIPTOR_INTERFACE_INDEX] = descriptor_counts->current_interface;
Expand All @@ -177,6 +177,11 @@ size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *desc
descriptor_buf[HID_DESCRIPTOR_INTERFACE_PROTOCOL_INDEX] = boot_device; // 1: keyboard, 2: mouse
}

if (custom_usb_hid_interface_name == NULL) {
usb_hid_interface_name = USB_INTERFACE_NAME " HID";
} else {
usb_hid_interface_name = custom_usb_hid_interface_name;
}
usb_add_interface_string(*current_interface_string, usb_hid_interface_name);
descriptor_buf[HID_DESCRIPTOR_INTERFACE_STRING_INDEX] = *current_interface_string;
(*current_interface_string)++;
Expand Down