@@ -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+
5874esp_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 );
0 commit comments