21
21
22
22
class USBHID ::AsyncSend: public AsyncOp {
23
23
public:
24
- AsyncSend (const HID_REPORT *report): AsyncOp( NULL ), report(report), result(false )
24
+ AsyncSend (USBHID *hid, const HID_REPORT *report): hid(hid ), report(report), result(false )
25
25
{
26
26
27
27
}
28
+
29
+ ~AsyncSend ()
30
+ {
31
+
32
+ }
33
+
34
+ virtual bool process ()
35
+ {
36
+ if (!hid->configured ()) {
37
+ result = false ;
38
+ return true ;
39
+ }
40
+
41
+ if (hid->send_nb (report)) {
42
+ result = true ;
43
+ return true ;
44
+ }
45
+
46
+ return false ;
47
+ }
48
+
49
+ USBHID *hid;
28
50
const HID_REPORT *report;
29
51
bool result;
30
52
};
31
53
32
54
class USBHID ::AsyncRead: public AsyncOp {
33
55
public:
34
- AsyncRead (HID_REPORT *report): AsyncOp(NULL ), report(report), result(false )
56
+ AsyncRead (USBHID *hid, HID_REPORT *report): hid(hid), report(report), result(false )
57
+ {
58
+
59
+ }
60
+
61
+ ~AsyncRead ()
35
62
{
36
63
37
64
}
65
+
66
+ virtual bool process ()
67
+ {
68
+ if (!hid->configured ()) {
69
+ result = false ;
70
+ return true ;
71
+ }
72
+
73
+ if (hid->read_nb (report)) {
74
+ result = true ;
75
+ return true ;
76
+ }
77
+
78
+ return false ;
79
+ }
80
+
81
+ USBHID *hid;
38
82
HID_REPORT *report;
39
83
bool result;
40
84
};
41
85
86
+ class USBHID ::AsyncWait: public AsyncOp {
87
+ public:
88
+ AsyncWait (USBHID *hid): hid(hid)
89
+ {
90
+
91
+ }
92
+
93
+ ~AsyncWait ()
94
+ {
95
+
96
+ }
97
+
98
+ virtual bool process ()
99
+ {
100
+ if (hid->configured ()) {
101
+ return true ;
102
+ }
103
+
104
+ return false ;
105
+ }
106
+
107
+ USBHID *hid;
108
+ };
42
109
43
110
USBHID::USBHID (bool connect_blocking, uint8_t output_report_length, uint8_t input_report_length, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
44
111
: USBDevice(get_usb_phy(), vendor_id, product_id, product_release)
@@ -88,39 +155,25 @@ void USBHID::wait_ready()
88
155
{
89
156
lock ();
90
157
91
- AsyncOp wait_op (NULL );
92
- wait_op.start (&_connect_list);
93
- if (configured ()) {
94
- wait_op.complete ();
95
- }
158
+ AsyncWait wait_op (this );
159
+ _connect_list.add (&wait_op);
96
160
97
161
unlock ();
98
162
99
- wait_op.wait ();
163
+ wait_op.wait (NULL );
100
164
}
101
165
102
166
103
167
bool USBHID::send (const HID_REPORT *report)
104
168
{
105
169
lock ();
106
170
107
- if (!configured ()) {
108
- unlock ();
109
- return false ;
110
- }
111
-
112
- if (send_nb (report)) {
113
- unlock ();
114
- return true ;
115
- }
116
-
117
- AsyncSend send_op (report);
118
- send_op.start (&_send_list);
171
+ AsyncSend send_op (this , report);
172
+ _send_list.add (&send_op);
119
173
120
174
unlock ();
121
175
122
- send_op.wait ();
123
-
176
+ send_op.wait (NULL );
124
177
return send_op.result ;
125
178
}
126
179
@@ -149,23 +202,12 @@ bool USBHID::read(HID_REPORT *report)
149
202
{
150
203
lock ();
151
204
152
- if (!configured ()) {
153
- unlock ();
154
- return false ;
155
- }
156
-
157
- if (read_nb (report)) {
158
- unlock ();
159
- return true ;
160
- }
161
-
162
- AsyncRead read_op (report);
163
- read_op.start (&_read_list);
205
+ AsyncRead read_op (this , report);
206
+ _read_list.add (&read_op);
164
207
165
208
unlock ();
166
209
167
- read_op.wait ();
168
-
210
+ read_op.wait (NULL );
169
211
return read_op.result ;
170
212
}
171
213
@@ -198,13 +240,8 @@ void USBHID::_send_isr(usb_ep_t endpoint)
198
240
write_finish (_int_in);
199
241
_send_idle = true ;
200
242
201
- AsyncSend *send_op = _send_list.head ();
202
- if (send_op != NULL ) {
203
- if (send_nb (send_op->report )) {
204
- send_op->result = true ;
205
- send_op->complete ();
206
- }
207
- } else {
243
+ _send_list.process ();
244
+ if (_send_idle) {
208
245
report_tx ();
209
246
}
210
247
@@ -217,60 +254,12 @@ void USBHID::_read_isr(usb_ep_t endpoint)
217
254
_output_report.length = read_finish (_int_out);
218
255
_read_idle = true ;
219
256
220
- AsyncRead *read_op = _read_list.head ();
221
- if (read_op != NULL ) {
222
- if (read_nb (read_op->report )) {
223
- read_op->result = true ;
224
- read_op->complete ();
225
- }
226
- } else {
257
+ _read_list.process ();
258
+ if (_read_idle) {
227
259
report_rx ();
228
260
}
229
261
}
230
262
231
- void USBHID::_connect_wake_all ()
232
- {
233
- assert_locked ();
234
-
235
- AsyncOp *wait_op = _connect_list.head ();
236
- while (wait_op != NULL ) {
237
- wait_op->complete ();
238
- wait_op = _connect_list.head ();
239
- }
240
- }
241
-
242
- void USBHID::_send_abort_all ()
243
- {
244
- assert_locked ();
245
-
246
- if (!_send_idle) {
247
- endpoint_abort (_int_in);
248
- _send_idle = true ;
249
- }
250
- AsyncSend *tx_cur = _send_list.head ();
251
- while (tx_cur != NULL ) {
252
- tx_cur->result = false ;
253
- tx_cur->complete ();
254
- tx_cur = _send_list.head ();
255
- }
256
- }
257
-
258
- void USBHID::_read_abort_all ()
259
- {
260
- assert_locked ();
261
-
262
- if (!_read_idle) {
263
- endpoint_abort (_int_out);
264
- _read_idle = true ;
265
- }
266
- AsyncRead *rx_cur = _read_list.head ();
267
- while (rx_cur != NULL ) {
268
- rx_cur->result = false ;
269
- rx_cur->complete ();
270
- rx_cur = _read_list.head ();
271
- }
272
- }
273
-
274
263
uint16_t USBHID::report_desc_length ()
275
264
{
276
265
report_desc ();
@@ -280,12 +269,19 @@ uint16_t USBHID::report_desc_length()
280
269
281
270
void USBHID::callback_state_change (DeviceState new_state)
282
271
{
283
- if (new_state == Configured) {
284
- _connect_wake_all ();
285
- } else {
286
- _send_abort_all ();
287
- _read_abort_all ();
272
+ if (new_state != Configured) {
273
+ if (!_send_idle) {
274
+ endpoint_abort (_int_in);
275
+ _send_idle = true ;
276
+ }
277
+ if (!_read_idle) {
278
+ endpoint_abort (_int_out);
279
+ _read_idle = true ;
280
+ }
288
281
}
282
+ _send_list.process ();
283
+ _read_list.process ();
284
+ _connect_list.process ();
289
285
}
290
286
291
287
//
0 commit comments