10
10
#include " crc32.h"
11
11
12
12
#define LOG_TAG " HID_COMM"
13
+
13
14
#include " elog.h"
14
15
15
16
#ifdef CONFIG_USE_HID_CONFIG
@@ -79,14 +80,16 @@ const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = {
79
80
0xC0 /* END_COLLECTION */
80
81
};
81
82
82
- void usbd_hid_custom_in_callback (uint8_t busid, uint8_t ep, uint32_t nbytes) {
83
+ void usbd_hid_custom_in_callback (uint8_t busid, uint8_t ep, uint32_t nbytes)
84
+ {
83
85
(void ) busid;
84
86
(void ) ep;
85
87
USB_LOG_DBG (" actual in len:%d\r\n " , nbytes);
86
88
// custom_state = HID_STATE_IDLE;
87
89
}
88
90
89
- void usbd_hid_custom_out_callback (uint8_t busid, uint8_t ep, uint32_t nbytes) {
91
+ void usbd_hid_custom_out_callback (uint8_t busid, uint8_t ep, uint32_t nbytes)
92
+ {
90
93
(void ) busid;
91
94
HID_ReadState = HID_STATE_DONE;
92
95
// usbd_ep_start_read(0, HID_OUT_EP, HID_read_buffer, HID_PACKET_SIZE);// 重新开启读取
@@ -102,7 +105,8 @@ struct usbd_endpoint hid_custom_out_ep = {
102
105
.ep_cb = usbd_hid_custom_out_callback,
103
106
};
104
107
105
- static bool CheckField (const Value &object, std::pair<std::string_view, Type> field) {
108
+ static bool CheckField (const Value &object, std::pair<std::string_view, Type> field)
109
+ {
106
110
auto &[field_name, field_type] = field;
107
111
if (!object.HasMember (field_name.data ())) {
108
112
return false ;
@@ -113,7 +117,8 @@ static bool CheckField(const Value &object, std::pair<std::string_view, Type> fi
113
117
return true ;
114
118
}
115
119
116
- static bool CheckFields (const Value &object, std::vector<std::pair<std::string_view, Type>> fields) {
120
+ static bool CheckFields (const Value &object, std::vector<std::pair<std::string_view, Type>> fields)
121
+ {
117
122
for (auto &field: fields) {
118
123
if (!CheckField (object, field)) {
119
124
return false ;
@@ -122,7 +127,8 @@ static bool CheckFields(const Value &object, std::vector<std::pair<std::string_v
122
127
return true ;
123
128
}
124
129
125
- static void FillStatus (HID_Response_t status, char *res, const char *message) {
130
+ static void FillStatus (HID_Response_t status, char *res, const char *message)
131
+ {
126
132
StringBuffer buffer;
127
133
Writer writer (buffer);
128
134
writer.StartObject ();
@@ -134,15 +140,18 @@ static void FillStatus(HID_Response_t status, char *res, const char *message) {
134
140
std::strcpy (res, buffer.GetString ());
135
141
}
136
142
137
- static void FillStatus (HID_Response_t status, char *res, std::string_view message) {
143
+ static void FillStatus (HID_Response_t status, char *res, std::string_view message)
144
+ {
138
145
FillStatus (status, res, message.data ());
139
146
}
140
147
141
- static void FillStatus (HID_Response_t status, char *res) {
148
+ static void FillStatus (HID_Response_t status, char *res)
149
+ {
142
150
FillStatus (status, res, " " );
143
151
}
144
152
145
- static void Hello (Document &root, char *res) {
153
+ static void Hello (Document &root, char *res)
154
+ {
146
155
(void ) root;
147
156
StringBuffer buffer;
148
157
@@ -198,14 +207,24 @@ static void Hello(Document &root, char *res) {
198
207
std::strcpy (res, buffer.GetString ());
199
208
}
200
209
201
- static void settings (Document &root, char *res) {
210
+ static void settings (Document &root, char *res)
211
+ {
202
212
if (!root.HasMember (" data" ) || !root[" data" ].IsObject ()) {
203
213
const char *message = " data not found" ;
204
214
USB_LOG_WRN (" %s\n " , message);
205
215
FillStatus (HID_RESPONSE_FAILED, res, message);
206
216
return ;
207
217
}
208
218
219
+ auto get_value_bool = [&](const Value &val, const char *key, bool default_value) -> bool
220
+ {
221
+ if (val.HasMember (key)) {
222
+ printf (" key: %s, value: %d\n " , key, val[key].GetBool ());
223
+ return val[key].GetBool ();
224
+ }
225
+ return default_value;
226
+ };
227
+
209
228
const Value &data = root[" data" ].GetObject ();
210
229
211
230
if (!data.HasMember (" boost" )) {
@@ -215,14 +234,16 @@ static void settings(Document &root, char *res) {
215
234
return ;
216
235
}
217
236
HSLink_Setting.boost = data[" boost" ].GetBool ();
218
- auto mode = [](const char *mode) {
237
+ auto mode = [](const char *mode)
238
+ {
219
239
if (strcmp (mode, " spi" ) == 0 ) {
220
240
return PORT_MODE_SPI;
221
241
}
222
242
return PORT_MODE_GPIO;
223
243
};
224
244
HSLink_Setting.swd_port_mode = mode (data[" swd_port_mode" ].GetString ());
225
245
HSLink_Setting.jtag_port_mode = mode (data[" jtag_port_mode" ].GetString ());
246
+ HSLink_Setting.jtag_single_bit_mode = get_value_bool (data, " jtag_single_bit_mode" , false );
226
247
227
248
const Value &power = data[" power" ];
228
249
HSLink_Setting.power .vref = power[" vref" ].GetDouble ();
@@ -248,7 +269,8 @@ static void settings(Document &root, char *res) {
248
269
FillStatus (HID_RESPONSE_SUCCESS, res);
249
270
}
250
271
251
- static void set_nickname (Document &root, char *res) {
272
+ static void set_nickname (Document &root, char *res)
273
+ {
252
274
if (!root.HasMember (" nickname" ) || !root[" nickname" ].IsString ()) {
253
275
const char *message = " nickname not found" ;
254
276
USB_LOG_WRN (" %s\n " , message);
@@ -263,7 +285,8 @@ static void set_nickname(Document &root, char *res) {
263
285
FillStatus (HID_RESPONSE_SUCCESS, res);
264
286
}
265
287
266
- static void upgrade (Document &root, char *res) {
288
+ static void upgrade (Document &root, char *res)
289
+ {
267
290
(void ) root;
268
291
269
292
FillStatus (HID_RESPONSE_SUCCESS, res);
@@ -272,7 +295,8 @@ static void upgrade(Document &root, char *res) {
272
295
HSP_EnterHSLinkBootloader ();
273
296
}
274
297
275
- static void entry_sys_bl (Document &root, char *res) {
298
+ static void entry_sys_bl (Document &root, char *res)
299
+ {
276
300
(void ) root;
277
301
278
302
FillStatus (HID_RESPONSE_SUCCESS, res);
@@ -281,7 +305,8 @@ static void entry_sys_bl(Document &root, char *res) {
281
305
HSP_EntrySysBootloader ();
282
306
}
283
307
284
- static void entry_hslink_bl (Document &root, char *res) {
308
+ static void entry_hslink_bl (Document &root, char *res)
309
+ {
285
310
(void ) root;
286
311
287
312
FillStatus (HID_RESPONSE_SUCCESS, res);
@@ -290,7 +315,8 @@ static void entry_hslink_bl(Document &root, char *res) {
290
315
HSP_EnterHSLinkBootloader ();
291
316
}
292
317
293
- static void set_hw_ver (Document &root, char *res) {
318
+ static void set_hw_ver (Document &root, char *res)
319
+ {
294
320
if (!root.HasMember (" hw_ver" ) || !root[" hw_ver" ].IsString ()) {
295
321
const char *message = " hw_ver not found" ;
296
322
USB_LOG_WRN (" %s\n " , message);
@@ -311,7 +337,8 @@ static void set_hw_ver(Document &root, char *res) {
311
337
FillStatus (HID_RESPONSE_SUCCESS, res);
312
338
}
313
339
314
- static void get_setting (Document &root, char *res) {
340
+ static void get_setting (Document &root, char *res)
341
+ {
315
342
(void ) root;
316
343
StringBuffer buffer;
317
344
Writer writer (buffer);
@@ -326,6 +353,9 @@ static void get_setting(Document &root, char *res) {
326
353
writer.Key (" jtag_port_mode" );
327
354
writer.String (HSLink_Setting.jtag_port_mode == PORT_MODE_SPI ? " spi" : " gpio" );
328
355
356
+ writer.Key (" jtag_single_bit_mode" );
357
+ writer.Bool (HSLink_Setting.jtag_single_bit_mode );
358
+
329
359
writer.Key (" power" );
330
360
writer.StartObject ();
331
361
writer.Key (" vref" );
@@ -360,12 +390,14 @@ static void get_setting(Document &root, char *res) {
360
390
std::strcpy (res, buffer.GetString ());
361
391
}
362
392
363
- static void erase_bl_b (Document &root, char *res) {
364
- fal_partition_erase (bl_b_part,0 , bl_b_part->len );
393
+ static void erase_bl_b (Document &root, char *res)
394
+ {
395
+ fal_partition_erase (bl_b_part, 0 , bl_b_part->len );
365
396
FillStatus (HID_RESPONSE_SUCCESS, res);
366
397
}
367
398
368
- static void write_bl_b (Document &root, char *res) {
399
+ static void write_bl_b (Document &root, char *res)
400
+ {
369
401
#define PACK_SIZE 512
370
402
if (!CheckFields (root,
371
403
{{" addr" , Type::kNumberType },
@@ -380,7 +412,7 @@ static void write_bl_b(Document &root, char *res) {
380
412
auto data_b64_len = root[" data" ].GetStringLength ();
381
413
log_d (" addr: 0x%X, len: %d, data_len: %d" , addr, len, data_b64_len);
382
414
// elog_hexdump("b64", 16, data_b64, data_b64_len);
383
- if (addr + len > bl_b_part->len ) {
415
+ if (size_t ( addr + len) > bl_b_part->len ) {
384
416
const char *message = " addr out of range" ;
385
417
USB_LOG_WRN (" %s\n " , message);
386
418
FillStatus (HID_RESPONSE_FAILED, res, message);
@@ -402,7 +434,7 @@ static void write_bl_b(Document &root, char *res) {
402
434
uint8_t *data = b64_decode_ex (data_b64, data_b64_len, &data_len);
403
435
log_d (" solve b64 data_len: %d" , data_len);
404
436
// elog_hexdump(LOG_TAG, 16, data, data_len);
405
- if (data_len != len) {
437
+ if (data_len != ( size_t ) len) {
406
438
log_w (" data_len != len" );
407
439
FillStatus (HID_RESPONSE_FAILED, res, " data_len != len" );
408
440
return ;
@@ -420,7 +452,8 @@ static void write_bl_b(Document &root, char *res) {
420
452
free (data);
421
453
}
422
454
423
- static void upgrade_bl (Document &root, char *res) {
455
+ static void upgrade_bl (Document &root, char *res)
456
+ {
424
457
if (!CheckFields (root,
425
458
{{" len" , Type::kNumberType },
426
459
{" crc" , Type::kStringType }})) {
@@ -430,7 +463,7 @@ static void upgrade_bl(Document &root, char *res) {
430
463
auto len = root[" len" ].GetInt ();
431
464
auto crc_str = root[" crc" ].GetString ();
432
465
auto crc = strtoul (crc_str + 2 , nullptr , 16 );
433
- if (len > bl_b_part->len ) {
466
+ if (( size_t ) len > bl_b_part->len ) {
434
467
char msg[64 ];
435
468
snprintf (msg, sizeof (msg), " len %d > %d" , len, bl_b_part->len );
436
469
log_w (msg);
@@ -447,7 +480,7 @@ static void upgrade_bl(Document &root, char *res) {
447
480
uint32_t crc_calc = 0xFFFFFFFF ;
448
481
const uint32_t CRC_CALC_LEN = 8 * 1024 ;
449
482
auto buf = std::make_unique<uint8_t []>(CRC_CALC_LEN);
450
- for (uint32_t i = 0 ; i < len; i += CRC_CALC_LEN) {
483
+ for (uint32_t i = 0 ; i < ( size_t ) len; i += CRC_CALC_LEN) {
451
484
auto calc_len = std::min (CRC_CALC_LEN, len - i);
452
485
fal_partition_read (bl_b_part, i, buf.get (), calc_len);
453
486
crc_calc = CRC_CalcArray_Software (buf.get (), calc_len, crc_calc);
@@ -466,7 +499,7 @@ static void upgrade_bl(Document &root, char *res) {
466
499
auto buf = std::make_unique<uint8_t []>(COPY_LEN);
467
500
fal_partition_erase (bl_part, 0 , bl_part->len );
468
501
for (size_t i = 0 ; i < bl_part->len ; i += COPY_LEN) {
469
- auto copy_len = std::min (COPY_LEN, (uint32_t )(bl_part->len - i));
502
+ auto copy_len = std::min (COPY_LEN, (uint32_t ) (bl_part->len - i));
470
503
log_d (" copy from 0x%X to 0x%X, size 0x%X" ,
471
504
i + bl_b_part->offset ,
472
505
i + bl_part->offset ,
@@ -481,7 +514,7 @@ static void upgrade_bl(Document &root, char *res) {
481
514
auto buf_1 = std::make_unique<uint8_t []>(COMP_LEN);
482
515
auto buf_2 = std::make_unique<uint8_t []>(COMP_LEN);
483
516
for (size_t i = 0 ; i < bl_part->len ; i += COMP_LEN) {
484
- auto comp_len = std::min (COMP_LEN, (uint32_t )(bl_part->len - i));
517
+ auto comp_len = std::min (COMP_LEN, (uint32_t ) (bl_part->len - i));
485
518
fal_partition_read (bl_b_part, i, buf_1.get (), comp_len);
486
519
fal_partition_read (bl_part, i, buf_2.get (), comp_len);
487
520
if (memcmp (buf_1.get (), buf_2.get (), comp_len) != 0 ) {
@@ -505,17 +538,20 @@ static void upgrade_bl(Document &root, char *res) {
505
538
HSP_Reboot ();
506
539
}
507
540
508
- static void HID_Write (const std::string &res) {
541
+ static void HID_Write (const std::string &res)
542
+ {
509
543
std::strcpy (reinterpret_cast <char *>(HID_write_buffer + 1 ), res.c_str ());
510
544
}
511
545
512
- static void HID_Write (const char *res) {
546
+ static void HID_Write (const char *res)
547
+ {
513
548
std::strcpy (reinterpret_cast <char *>(HID_write_buffer + 1 ), res);
514
549
}
515
550
516
551
using HID_Command_t = std::function<void (Document &, char *res)>;
517
552
518
- void HID_Handle () {
553
+ void HID_Handle ()
554
+ {
519
555
if (HID_ReadState == HID_STATE_BUSY) {
520
556
return ; // 接收中,不处理
521
557
}
@@ -538,7 +574,8 @@ void HID_Handle() {
538
574
Document root;
539
575
const auto parse = reinterpret_cast <char *>(HID_read_buffer + 1 );
540
576
root.Parse (parse);
541
- [&]() {
577
+ [&]()
578
+ {
542
579
if (root.HasParseError ()
543
580
|| root.HasMember (" name" ) == false
544
581
) {
0 commit comments