@@ -120,49 +120,49 @@ int __USBGetMouseReportID() {
120
120
return __USBInstallKeyboard ? 2 : 1 ;
121
121
}
122
122
123
- static uint8_t *GetDescHIDReport (int *len) {
124
- static uint8_t *report = nullptr ;
125
- int report_len = 0 ;
123
+ static int __hid_report_len = 0 ;
124
+ static uint8_t *__hid_report = nullptr ;
126
125
127
- if (report ) {
128
- free (report);
129
- report = nullptr ;
126
+ static uint8_t * GetDescHIDReport ( int *len ) {
127
+ if (len) {
128
+ *len = __hid_report_len ;
130
129
}
130
+ return __hid_report;
131
+ }
131
132
133
+ static void __SetupDescHIDReport () {
132
134
if (__USBInstallKeyboard && __USBInstallMouse) {
133
135
uint8_t desc_hid_report[] = {
134
136
TUD_HID_REPORT_DESC_KEYBOARD (HID_REPORT_ID (1 )),
135
137
TUD_HID_REPORT_DESC_MOUSE (HID_REPORT_ID (2 ))
136
138
};
137
- report_len = sizeof (desc_hid_report);
138
- report = ( uint8_t *) malloc (report_len);
139
- if (report) {
140
- memcpy (report , desc_hid_report, report_len );
139
+ __hid_report = ( uint8_t *) malloc ( sizeof (desc_hid_report) );
140
+ if (__hid_report) {
141
+ __hid_report_len = sizeof (desc_hid_report);
142
+ memcpy (__hid_report , desc_hid_report, __hid_report_len );
141
143
}
142
144
} else if (__USBInstallKeyboard && ! __USBInstallMouse) {
143
145
uint8_t desc_hid_report[] = {
144
146
TUD_HID_REPORT_DESC_KEYBOARD (HID_REPORT_ID (1 ))
145
147
};
146
- report_len = sizeof (desc_hid_report);
147
- report = ( uint8_t *) malloc (report_len);
148
- if (report) {
149
- memcpy (report , desc_hid_report, report_len );
148
+ __hid_report = ( uint8_t *) malloc ( sizeof (desc_hid_report) );
149
+ if (__hid_report) {
150
+ __hid_report_len = sizeof (desc_hid_report);
151
+ memcpy (__hid_report , desc_hid_report, __hid_report_len );
150
152
}
151
- } else { // if (!__USBInstallKeyboard && __USBInstallMouse) {
153
+ } else if (! __USBInstallKeyboard && __USBInstallMouse) {
152
154
uint8_t desc_hid_report[] = {
153
155
TUD_HID_REPORT_DESC_MOUSE (HID_REPORT_ID (1 ))
154
156
};
155
- report_len = sizeof (desc_hid_report);
156
- report = ( uint8_t *) malloc (report_len);
157
- if (report) {
158
- memcpy (report , desc_hid_report, report_len );
157
+ __hid_report = ( uint8_t *) malloc ( sizeof (desc_hid_report) );
158
+ if (__hid_report) {
159
+ __hid_report_len = sizeof (desc_hid_report);
160
+ memcpy (__hid_report , desc_hid_report, __hid_report_len );
159
161
}
162
+ } else {
163
+ __hid_report = nullptr ;
164
+ __hid_report_len = 0 ;
160
165
}
161
-
162
- if (len) {
163
- *len = report_len;
164
- }
165
- return report;
166
166
}
167
167
168
168
// Invoked when received GET HID REPORT DESCRIPTOR
@@ -173,11 +173,13 @@ uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance) {
173
173
return GetDescHIDReport (nullptr );
174
174
}
175
175
176
-
176
+ static uint8_t *usbd_desc_cfg = nullptr ;
177
177
const uint8_t *tud_descriptor_configuration_cb (uint8_t index) {
178
178
(void )index;
179
- static uint8_t *usbd_desc_cfg = nullptr ;
179
+ return usbd_desc_cfg;
180
+ }
180
181
182
+ static void __SetupUSBDescriptor () {
181
183
if (!usbd_desc_cfg) {
182
184
bool hasHID = __USBInstallKeyboard || __USBInstallMouse;
183
185
@@ -229,7 +231,6 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
229
231
}
230
232
}
231
233
}
232
- return usbd_desc_cfg;
233
234
}
234
235
235
236
const uint16_t *tud_descriptor_string_cb (uint8_t index, uint16_t langid) {
@@ -295,6 +296,9 @@ void __USBStart() {
295
296
return ;
296
297
}
297
298
299
+ __SetupDescHIDReport ();
300
+ __SetupUSBDescriptor ();
301
+
298
302
mutex_init (&__usb_mutex);
299
303
300
304
tusb_init ();
0 commit comments