Skip to content

Commit ad3c025

Browse files
committed
Some more cleanup
1 parent 9bd868f commit ad3c025

File tree

4 files changed

+58
-46
lines changed

4 files changed

+58
-46
lines changed

main/badge_hid_drivers.c

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ static void decode_hat_to_dpad(uint8_t hat, gamepad_report_t* rpt) {
5555
rpt->buttons.left = (hat == 0x05 || hat == 0x06 || hat == 0x07);
5656
}
5757

58+
static void set_mouse_field(field_info_t* f, uint16_t offset, uint8_t size, mouse_field_layout_t* layout) {
59+
f->present = true;
60+
f->offset = offset;
61+
f->size = size;
62+
uint16_t end = offset + size;
63+
if (end > layout->report_len_bits) layout->report_len_bits = end;
64+
}
65+
66+
static void set_gamepad_field(field_info_t* f, uint16_t offset, uint8_t size, gamepad_field_layout_t* layout) {
67+
f->present = true;
68+
f->offset = offset;
69+
f->size = size;
70+
uint16_t end = offset + size;
71+
if (end > layout->report_len_bits) layout->report_len_bits = end;
72+
}
73+
5874
esp_err_t analyze_mouse_layout(const uint8_t* desc, int desc_len, mouse_field_layout_t* layout_out) {
5975
memset(layout_out, 0, sizeof(*layout_out));
6076

@@ -64,7 +80,7 @@ esp_err_t analyze_mouse_layout(const uint8_t* desc, int desc_len, mouse_field_la
6480
int usage_index = 0;
6581
int report_size = 0;
6682
int report_count = 0;
67-
uint16_t usage_min = 0;
83+
// uint16_t usage_min = 0;
6884
uint16_t usage_max = 0;
6985

7086
for (int i = 0; i < desc_len;) {
@@ -100,8 +116,8 @@ esp_err_t analyze_mouse_layout(const uint8_t* desc, int desc_len, mouse_field_la
100116
case HID_TYPE_LOCAL:
101117
if (tag == HID_TAG_USAGE && usage_index < 32)
102118
usages[usage_index++] = data;
103-
else if (tag == HID_TAG_USAGE_MIN)
104-
usage_min = data;
119+
// else if (tag == HID_TAG_USAGE_MIN)
120+
// usage_min = data;
105121
else if (tag == HID_TAG_USAGE_MAX)
106122
usage_max = data;
107123
break;
@@ -119,29 +135,18 @@ esp_err_t analyze_mouse_layout(const uint8_t* desc, int desc_len, mouse_field_la
119135
uint16_t usage = usages[u];
120136
if (usage_page == USAGE_PAGE_GENERIC_DESKTOP) {
121137
if (usage == USAGE_X) {
122-
layout_out->x.present = true;
123-
layout_out->x.offset = bit_offset;
124-
layout_out->x.size = bits;
138+
set_mouse_field(&layout_out->x, bit_offset, bits, layout_out);
125139
} else if (usage == USAGE_Y) {
126-
layout_out->y.present = true;
127-
layout_out->y.offset = bit_offset;
128-
layout_out->y.size = bits;
140+
set_mouse_field(&layout_out->y, bit_offset, bits, layout_out);
129141
} else if (usage == USAGE_WHEEL) {
130-
layout_out->scroll.present = true;
131-
layout_out->scroll.offset = bit_offset;
132-
layout_out->scroll.size = bits;
142+
set_mouse_field(&layout_out->scroll, bit_offset, bits, layout_out);
133143
} else if (usage == USAGE_TILT) {
134-
layout_out->tilt.present = true;
135-
layout_out->tilt.offset = bit_offset;
136-
layout_out->tilt.size = bits;
144+
set_mouse_field(&layout_out->tilt, bit_offset, bits, layout_out);
137145
}
138146
} else if (usage_page == USAGE_PAGE_CONSUMER && usage == USAGE_CONSUMER_TILT) {
139-
layout_out->tilt.present = true;
140-
layout_out->tilt.offset = bit_offset;
141-
layout_out->tilt.size = bits;
147+
set_mouse_field(&layout_out->tilt, bit_offset, bits, layout_out);
142148
} else if (usage_page == USAGE_PAGE_BUTTON) {
143149
if (!layout_out->buttons.present) layout_out->buttons.offset = bit_offset;
144-
145150
layout_out->buttons.present = true;
146151
}
147152
bit_offset += bits;
@@ -218,7 +223,7 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, const int desc_len, gamepa
218223
break;
219224
case HID_TAG_REPORT_ID:
220225
layout_out->report_id = data;
221-
bit_offset = 8;
226+
bit_offset += 8;
222227
break;
223228
}
224229
break;
@@ -231,7 +236,7 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, const int desc_len, gamepa
231236

232237
case HID_TYPE_MAIN:
233238
if (tag == HID_TAG_INPUT) {
234-
bool is_constant = (data & 0x01) != 0;
239+
// bool is_constant = (data & 0x01) != 0;
235240

236241
if (usage_page == USAGE_PAGE_BUTTON && usage_index >= 1 && usages[0] >= USAGE_BUTTON_MIN &&
237242
usages[0] <= USAGE_BUTTON_MAX) {
@@ -254,41 +259,27 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, const int desc_len, gamepa
254259
if (usage_page == USAGE_PAGE_GENERIC_DESKTOP) {
255260
switch (usage) {
256261
case USAGE_HATSWITCH:
257-
layout_out->dpad.present = true;
258-
layout_out->dpad.offset = bit_offset;
259-
layout_out->dpad.size = report_size;
262+
set_gamepad_field(&layout_out->dpad, bit_offset, report_size, layout_out);
260263
break;
261264
case USAGE_X:
262-
layout_out->lx.present = true;
263-
layout_out->lx.offset = bit_offset;
264-
layout_out->lx.size = report_size;
265+
set_gamepad_field(&layout_out->lx, bit_offset, report_size, layout_out);
265266
break;
266267
case USAGE_Y:
267-
layout_out->ly.present = true;
268-
layout_out->ly.offset = bit_offset;
269-
layout_out->ly.size = report_size;
268+
set_gamepad_field(&layout_out->ly, bit_offset, report_size, layout_out);
270269
break;
271270
case USAGE_Z:
272-
layout_out->rx.present = true;
273-
layout_out->rx.offset = bit_offset;
274-
layout_out->rx.size = report_size;
271+
set_gamepad_field(&layout_out->rx, bit_offset, report_size, layout_out);
275272
break;
276273
case USAGE_RZ:
277-
layout_out->ry.present = true;
278-
layout_out->ry.offset = bit_offset;
279-
layout_out->ry.size = report_size;
274+
set_gamepad_field(&layout_out->ry, bit_offset, report_size, layout_out);
280275
break;
281276
}
282277
} else if (usage_page == USAGE_PAGE_SIMULATION &&
283278
(usage == USAGE_ACCELERATOR || usage == USAGE_BRAKE)) {
284279
if (!layout_out->lt.present) {
285-
layout_out->lt.present = true;
286-
layout_out->lt.offset = bit_offset;
287-
layout_out->lt.size = report_size;
280+
set_gamepad_field(&layout_out->lt, bit_offset, report_size, layout_out);
288281
} else {
289-
layout_out->rt.present = true;
290-
layout_out->rt.offset = bit_offset;
291-
layout_out->rt.size = report_size;
282+
set_gamepad_field(&layout_out->rt, bit_offset, report_size, layout_out);
292283
}
293284
}
294285

@@ -322,6 +313,10 @@ mouse_report_t parse_mouse_report(const uint8_t* data, const int length, mouse_f
322313
ESP_LOGW(TAG, "No layout for mouse!");
323314
return report;
324315
}
316+
if (layout->report_len_bits > (length * 8)) {
317+
ESP_LOGW(TAG, "Data too short for mouse event!");
318+
return report;
319+
}
325320

326321
if (layout->buttons.present) {
327322
report.buttons.val = extract_unsigned_bits(data, layout->buttons.offset, layout->buttons.size);
@@ -359,6 +354,11 @@ gamepad_report_t parse_gamepad_report(const uint8_t* data, int length, gamepad_f
359354
ESP_LOGW(TAG, "No layout for gamepad!");
360355
return report;
361356
}
357+
if (layout->report_len_bits > (length * 8)) {
358+
ESP_LOGW(TAG, "Data too short for gamepad event!");
359+
return report;
360+
}
361+
362362
if (layout->dpad.present) {
363363
uint8_t hat = extract_unsigned_bits(data, layout->dpad.offset, layout->dpad.size);
364364
decode_hat_to_dpad(hat, &report);

main/badge_hid_drivers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ typedef struct {
113113
field_info_t lx, ly;
114114
field_info_t rx, ry;
115115
field_info_t lt, rt;
116+
uint16_t report_len_bits;
116117
} gamepad_field_layout_t;
117118

118119
typedef struct {
@@ -122,6 +123,7 @@ typedef struct {
122123
field_info_t scroll;
123124
field_info_t tilt;
124125
field_info_t buttons;
126+
uint16_t report_len_bits;
125127
} mouse_field_layout_t;
126128

127129
typedef struct {

test_native/test_descriptors.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ const uint8_t mouse2_desc[] = {
2626
const size_t mouse2_len = sizeof(mouse2_desc);
2727

2828
// Fujitsu M520
29-
const uint8_t mouse3_desc[] = {0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, 0xA1, 0x00, 0x05, 0x09,
30-
0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x08, 0x75, 0x01,
31-
0x81, 0x02, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81,
32-
0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 0xC0, 0xC0};
29+
const uint8_t mouse3_desc[] = {
30+
0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, 0xA1, 0x00, 0x05, 0x09,
31+
0x19, 0x01, 0x29, 0x03, 0x15, 0x00, 0x25, 0x01, 0x95, 0x08, 0x75, 0x01,
32+
0x81, 0x02, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81,
33+
0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 0xC0, 0xC0
34+
};
3335
const size_t mouse3_len = sizeof(mouse3_desc);
3436

3537
// Stadia Controller

test_native/test_hid.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ int test_layouts(void) {
1616

1717
assert(ESP_OK == analyze_mouse_layout(mouse1_desc, mouse1_len, &mouse_layout));
1818

19+
assert(64 == mouse_layout.report_len_bits);
20+
1921
assert(mouse_layout.buttons.present);
2022
assert(mouse_layout.x.present);
2123
assert(mouse_layout.y.present);
@@ -92,6 +94,8 @@ int test_layouts(void) {
9294

9395
assert(ESP_OK == analyze_mouse_layout(mouse2_desc, mouse2_len, &mouse_layout));
9496

97+
assert(40 == mouse_layout.report_len_bits);
98+
9599
assert(mouse_layout.buttons.present);
96100
assert(mouse_layout.x.present);
97101
assert(mouse_layout.y.present);
@@ -141,6 +145,8 @@ int test_layouts(void) {
141145

142146
assert(ESP_OK == analyze_mouse_layout(mouse3_desc, mouse3_len, &mouse_layout));
143147

148+
assert(32 == mouse_layout.report_len_bits);
149+
144150
assert(mouse_layout.buttons.present);
145151
assert(mouse_layout.x.present);
146152
assert(mouse_layout.y.present);
@@ -190,6 +196,8 @@ int test_layouts(void) {
190196

191197
assert(ESP_OK == analyze_gamepad_layout(gamepad1_desc, gamepad1_len, &pad_layout));
192198

199+
assert(80 == pad_layout.report_len_bits);
200+
193201
assert(pad_layout.dpad.present);
194202
assert(pad_layout.buttons.present);
195203
assert(pad_layout.lx.present);

0 commit comments

Comments
 (0)