Introduce hid_open and hid_close events

This allows to handle HID open/close at the same place as HID input
events (in the HID layer).

This will be especially useful to manage HID gamepads, to avoid
implementing one part in the HID layer and another part in the gamepad
processor implementation.
This commit is contained in:
Romain Vimont 2024-09-06 23:08:08 +02:00
parent 24f7ea5894
commit 8fb87b5e6b
11 changed files with 89 additions and 47 deletions

View File

@ -13,4 +13,14 @@ struct sc_hid_input {
uint8_t size; uint8_t size;
}; };
struct sc_hid_open {
uint16_t hid_id;
const uint8_t *report_desc; // pointer to static memory
size_t report_desc_size;
};
struct sc_hid_close {
uint16_t hid_id;
};
#endif #endif

View File

@ -47,7 +47,7 @@
* *
* (change vid:pid' to your device's vendor ID and product ID). * (change vid:pid' to your device's vendor ID and product ID).
*/ */
const uint8_t SC_HID_KEYBOARD_REPORT_DESC[] = { static const uint8_t SC_HID_KEYBOARD_REPORT_DESC[] = {
// Usage Page (Generic Desktop) // Usage Page (Generic Desktop)
0x05, 0x01, 0x05, 0x01,
// Usage (Keyboard) // Usage (Keyboard)
@ -121,9 +121,6 @@ const uint8_t SC_HID_KEYBOARD_REPORT_DESC[] = {
0xC0 0xC0
}; };
const size_t SC_HID_KEYBOARD_REPORT_DESC_LEN =
sizeof(SC_HID_KEYBOARD_REPORT_DESC);
/** /**
* A keyboard HID event is 8 bytes long: * A keyboard HID event is 8 bytes long:
* *
@ -332,3 +329,13 @@ sc_hid_keyboard_generate_input_from_mods(struct sc_hid_input *hid_input,
return true; return true;
} }
void sc_hid_keyboard_generate_open(struct sc_hid_open *hid_open) {
hid_open->hid_id = SC_HID_ID_KEYBOARD;
hid_open->report_desc = SC_HID_KEYBOARD_REPORT_DESC;
hid_open->report_desc_size = sizeof(SC_HID_KEYBOARD_REPORT_DESC);
}
void sc_hid_keyboard_generate_close(struct sc_hid_close *hid_close) {
hid_close->hid_id = SC_HID_ID_KEYBOARD;
}

View File

@ -16,9 +16,6 @@
#define SC_HID_ID_KEYBOARD 1 #define SC_HID_ID_KEYBOARD 1
extern const uint8_t SC_HID_KEYBOARD_REPORT_DESC[];
extern const size_t SC_HID_KEYBOARD_REPORT_DESC_LEN;
/** /**
* HID keyboard events are sequence-based, every time keyboard state changes * HID keyboard events are sequence-based, every time keyboard state changes
* it sends an array of currently pressed keys, the host is responsible for * it sends an array of currently pressed keys, the host is responsible for
@ -38,6 +35,12 @@ struct sc_hid_keyboard {
void void
sc_hid_keyboard_init(struct sc_hid_keyboard *hid); sc_hid_keyboard_init(struct sc_hid_keyboard *hid);
void
sc_hid_keyboard_generate_open(struct sc_hid_open *hid_open);
void
sc_hid_keyboard_generate_close(struct sc_hid_close *hid_close);
bool bool
sc_hid_keyboard_generate_input_from_key(struct sc_hid_keyboard *hid, sc_hid_keyboard_generate_input_from_key(struct sc_hid_keyboard *hid,
struct sc_hid_input *hid_input, struct sc_hid_input *hid_input,

View File

@ -80,9 +80,6 @@ const uint8_t SC_HID_MOUSE_REPORT_DESC[] = {
0xC0, 0xC0,
}; };
const size_t SC_HID_MOUSE_REPORT_DESC_LEN =
sizeof(SC_HID_MOUSE_REPORT_DESC);
/** /**
* A mouse HID event is 4 bytes long: * A mouse HID event is 4 bytes long:
* *
@ -190,3 +187,13 @@ sc_hid_mouse_generate_input_from_scroll(struct sc_hid_input *hid_input,
data[3] = CLAMP(event->vscroll, -127, 127); data[3] = CLAMP(event->vscroll, -127, 127);
// Horizontal scrolling ignored // Horizontal scrolling ignored
} }
void sc_hid_mouse_generate_open(struct sc_hid_open *hid_open) {
hid_open->hid_id = SC_HID_ID_MOUSE;
hid_open->report_desc = SC_HID_MOUSE_REPORT_DESC;
hid_open->report_desc_size = sizeof(SC_HID_MOUSE_REPORT_DESC);
}
void sc_hid_mouse_generate_close(struct sc_hid_close *hid_close) {
hid_close->hid_id = SC_HID_ID_MOUSE;
}

View File

@ -10,8 +10,11 @@
#define SC_HID_ID_MOUSE 2 #define SC_HID_ID_MOUSE 2
extern const uint8_t SC_HID_MOUSE_REPORT_DESC[]; void
extern const size_t SC_HID_MOUSE_REPORT_DESC_LEN; sc_hid_mouse_generate_open(struct sc_hid_open *hid_open);
void
sc_hid_mouse_generate_close(struct sc_hid_close *hid_close);
void void
sc_hid_mouse_generate_input_from_motion(struct sc_hid_input *hid_input, sc_hid_mouse_generate_input_from_motion(struct sc_hid_input *hid_input,

View File

@ -145,11 +145,15 @@ sc_keyboard_uhid_init(struct sc_keyboard_uhid *kb,
kb->uhid_receiver.ops = &uhid_receiver_ops; kb->uhid_receiver.ops = &uhid_receiver_ops;
sc_uhid_devices_add_receiver(uhid_devices, &kb->uhid_receiver); sc_uhid_devices_add_receiver(uhid_devices, &kb->uhid_receiver);
struct sc_hid_open hid_open;
sc_hid_keyboard_generate_open(&hid_open);
assert(hid_open.hid_id == SC_HID_ID_KEYBOARD);
struct sc_control_msg msg; struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_UHID_CREATE; msg.type = SC_CONTROL_MSG_TYPE_UHID_CREATE;
msg.uhid_create.id = SC_HID_ID_KEYBOARD; msg.uhid_create.id = SC_HID_ID_KEYBOARD;
msg.uhid_create.report_desc = SC_HID_KEYBOARD_REPORT_DESC; msg.uhid_create.report_desc = hid_open.report_desc;
msg.uhid_create.report_desc_size = SC_HID_KEYBOARD_REPORT_DESC_LEN; msg.uhid_create.report_desc_size = hid_open.report_desc_size;
if (!sc_controller_push_msg(controller, &msg)) { if (!sc_controller_push_msg(controller, &msg)) {
LOGE("Could not send UHID_CREATE message (keyboard)"); LOGE("Could not send UHID_CREATE message (keyboard)");
return false; return false;

View File

@ -74,11 +74,15 @@ sc_mouse_uhid_init(struct sc_mouse_uhid *mouse,
mouse->mouse_processor.relative_mode = true; mouse->mouse_processor.relative_mode = true;
struct sc_hid_open hid_open;
sc_hid_mouse_generate_open(&hid_open);
assert(hid_open.hid_id == SC_HID_ID_MOUSE);
struct sc_control_msg msg; struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_UHID_CREATE; msg.type = SC_CONTROL_MSG_TYPE_UHID_CREATE;
msg.uhid_create.id = SC_HID_ID_MOUSE; msg.uhid_create.id = SC_HID_ID_MOUSE;
msg.uhid_create.report_desc = SC_HID_MOUSE_REPORT_DESC; msg.uhid_create.report_desc = hid_open.report_desc;
msg.uhid_create.report_desc_size = SC_HID_MOUSE_REPORT_DESC_LEN; msg.uhid_create.report_desc_size = hid_open.report_desc_size;
if (!sc_controller_push_msg(controller, &msg)) { if (!sc_controller_push_msg(controller, &msg)) {
LOGE("Could not send UHID_CREATE message (mouse)"); LOGE("Could not send UHID_CREATE message (mouse)");
return false; return false;

View File

@ -226,8 +226,7 @@ sc_aoa_push_input_with_ack_to_wait(struct sc_aoa *aoa,
} }
bool bool
sc_aoa_push_open(struct sc_aoa *aoa, uint16_t accessory_id, sc_aoa_push_open(struct sc_aoa *aoa, const struct sc_hid_open *hid_open) {
const uint8_t *report_desc, uint16_t report_desc_size) {
// TODO log verbose // TODO log verbose
sc_mutex_lock(&aoa->mutex); sc_mutex_lock(&aoa->mutex);
@ -243,9 +242,7 @@ sc_aoa_push_open(struct sc_aoa *aoa, uint16_t accessory_id,
} }
aoa_event->type = SC_AOA_EVENT_TYPE_OPEN; aoa_event->type = SC_AOA_EVENT_TYPE_OPEN;
aoa_event->open.hid_id = accessory_id; aoa_event->open.hid = *hid_open;
aoa_event->open.report_desc = report_desc;
aoa_event->open.report_desc_size = report_desc_size;
if (was_empty) { if (was_empty) {
sc_cond_signal(&aoa->event_cond); sc_cond_signal(&aoa->event_cond);
@ -257,7 +254,7 @@ sc_aoa_push_open(struct sc_aoa *aoa, uint16_t accessory_id,
} }
bool bool
sc_aoa_push_close(struct sc_aoa *aoa, uint16_t accessory_id) { sc_aoa_push_close(struct sc_aoa *aoa, const struct sc_hid_close *hid_close) {
// TODO log verbose // TODO log verbose
sc_mutex_lock(&aoa->mutex); sc_mutex_lock(&aoa->mutex);
@ -273,7 +270,7 @@ sc_aoa_push_close(struct sc_aoa *aoa, uint16_t accessory_id) {
} }
aoa_event->type = SC_AOA_EVENT_TYPE_CLOSE; aoa_event->type = SC_AOA_EVENT_TYPE_CLOSE;
aoa_event->close.hid_id = accessory_id; aoa_event->close.hid = *hid_close;
if (was_empty) { if (was_empty) {
sc_cond_signal(&aoa->event_cond); sc_cond_signal(&aoa->event_cond);
@ -312,29 +309,31 @@ sc_aoa_process_event(struct sc_aoa *aoa, struct sc_aoa_event *event) {
} }
} }
bool ok = sc_aoa_send_hid_event(aoa, &event->input.hid); struct sc_hid_input *hid_input = &event->input.hid;
bool ok = sc_aoa_send_hid_event(aoa, hid_input);
if (!ok) { if (!ok) {
LOGW("Could not send HID event to USB device: %" PRIu16, LOGW("Could not send HID event to USB device: %" PRIu16,
event->input.hid.hid_id); hid_input->hid_id);
} }
break; break;
} }
case SC_AOA_EVENT_TYPE_OPEN: { case SC_AOA_EVENT_TYPE_OPEN: {
bool ok = sc_aoa_setup_hid(aoa, event->open.hid_id, struct sc_hid_open *hid_open = &event->open.hid;
event->open.report_desc, bool ok = sc_aoa_setup_hid(aoa, hid_open->hid_id,
event->open.report_desc_size); hid_open->report_desc,
hid_open->report_desc_size);
if (!ok) { if (!ok) {
LOGW("Could not open AOA device: %" PRIu16, event->open.hid_id); LOGW("Could not open AOA device: %" PRIu16, hid_open->hid_id);
} }
break; break;
} }
case SC_AOA_EVENT_TYPE_CLOSE: { case SC_AOA_EVENT_TYPE_CLOSE: {
bool ok = sc_aoa_unregister_hid(aoa, event->close.hid_id); struct sc_hid_close *hid_close = &event->close.hid;
bool ok = sc_aoa_unregister_hid(aoa, hid_close->hid_id);
if (!ok) { if (!ok) {
LOGW("Could not close AOA device: %" PRIu16, LOGW("Could not close AOA device: %" PRIu16, hid_close->hid_id);
event->close.hid_id);
} }
break; break;

View File

@ -23,12 +23,10 @@ struct sc_aoa_event {
enum sc_aoa_event_type type; enum sc_aoa_event_type type;
union { union {
struct { struct {
uint16_t hid_id; struct sc_hid_open hid;
const uint8_t *report_desc; // pointer to static memory
uint16_t report_desc_size;
} open; } open;
struct { struct {
uint16_t hid_id; struct sc_hid_close hid;
} close; } close;
struct { struct {
struct sc_hid_input hid; struct sc_hid_input hid;
@ -75,11 +73,10 @@ sc_aoa_join(struct sc_aoa *aoa);
// report_desc must be a pointer to static memory, accessed at any time from // report_desc must be a pointer to static memory, accessed at any time from
// another thread // another thread
bool bool
sc_aoa_push_open(struct sc_aoa *aoa, uint16_t accessory_id, sc_aoa_push_open(struct sc_aoa *aoa, const struct sc_hid_open *hid_open);
const uint8_t *report_desc, uint16_t report_desc_size);
bool bool
sc_aoa_push_close(struct sc_aoa *aoa, uint16_t accessory_id); sc_aoa_push_close(struct sc_aoa *aoa, const struct sc_hid_close *hid_close);
bool bool
sc_aoa_push_input_with_ack_to_wait(struct sc_aoa *aoa, sc_aoa_push_input_with_ack_to_wait(struct sc_aoa *aoa,

View File

@ -66,9 +66,10 @@ bool
sc_keyboard_aoa_init(struct sc_keyboard_aoa *kb, struct sc_aoa *aoa) { sc_keyboard_aoa_init(struct sc_keyboard_aoa *kb, struct sc_aoa *aoa) {
kb->aoa = aoa; kb->aoa = aoa;
bool ok = sc_aoa_push_open(aoa, SC_HID_ID_KEYBOARD, struct sc_hid_open hid_open;
SC_HID_KEYBOARD_REPORT_DESC, sc_hid_keyboard_generate_open(&hid_open);
SC_HID_KEYBOARD_REPORT_DESC_LEN);
bool ok = sc_aoa_push_open(aoa, &hid_open);
if (!ok) { if (!ok) {
LOGW("Could not push AOA keyboard open request"); LOGW("Could not push AOA keyboard open request");
return false; return false;
@ -97,7 +98,10 @@ sc_keyboard_aoa_init(struct sc_keyboard_aoa *kb, struct sc_aoa *aoa) {
void void
sc_keyboard_aoa_destroy(struct sc_keyboard_aoa *kb) { sc_keyboard_aoa_destroy(struct sc_keyboard_aoa *kb) {
bool ok = sc_aoa_push_close(kb->aoa, SC_HID_ID_KEYBOARD); struct sc_hid_close hid_close;
sc_hid_keyboard_generate_close(&hid_close);
bool ok = sc_aoa_push_close(kb->aoa, &hid_close);
if (!ok) { if (!ok) {
LOGW("Could not push AOA keyboard close request"); LOGW("Could not push AOA keyboard close request");
} }

View File

@ -52,9 +52,10 @@ bool
sc_mouse_aoa_init(struct sc_mouse_aoa *mouse, struct sc_aoa *aoa) { sc_mouse_aoa_init(struct sc_mouse_aoa *mouse, struct sc_aoa *aoa) {
mouse->aoa = aoa; mouse->aoa = aoa;
bool ok = sc_aoa_push_open(aoa, SC_HID_ID_MOUSE, struct sc_hid_open hid_open;
SC_HID_MOUSE_REPORT_DESC, sc_hid_mouse_generate_open(&hid_open);
SC_HID_MOUSE_REPORT_DESC_LEN);
bool ok = sc_aoa_push_open(aoa, &hid_open);
if (!ok) { if (!ok) {
LOGW("Could not push AOA mouse open request"); LOGW("Could not push AOA mouse open request");
return false; return false;
@ -77,7 +78,10 @@ sc_mouse_aoa_init(struct sc_mouse_aoa *mouse, struct sc_aoa *aoa) {
void void
sc_mouse_aoa_destroy(struct sc_mouse_aoa *mouse) { sc_mouse_aoa_destroy(struct sc_mouse_aoa *mouse) {
bool ok = sc_aoa_push_close(mouse->aoa, SC_HID_ID_MOUSE); struct sc_hid_close hid_close;
sc_hid_mouse_generate_close(&hid_close);
bool ok = sc_aoa_push_close(mouse->aoa, &hid_close);
if (!ok) { if (!ok) {
LOGW("Could not push AOA mouse close request"); LOGW("Could not push AOA mouse close request");
} }