@@ -385,13 +385,40 @@ void print_dfu_if(struct dfu_if *dfu_if)
385
385
dfu_if->serial_name );
386
386
}
387
387
388
+ using json = nlohmann::json;
389
+ using namespace std ;
390
+
391
+ std::string print_hex (int number) {
392
+ char tmp[10 ];
393
+ sprintf (tmp, " 0x%04x" , number);
394
+ std::string s (tmp);
395
+ return s;
396
+ }
397
+
388
398
/* Walk the device tree and print out DFU devices */
389
- void list_dfu_interfaces (void )
399
+ json list_dfu_interfaces (void )
390
400
{
391
401
struct dfu_if *pdfu;
392
402
393
- for (pdfu = dfu_root; pdfu != NULL ; pdfu = pdfu->next )
394
- print_dfu_if (pdfu);
403
+ json j;
404
+ j[" event" ] = " list" ;
405
+
406
+ int index = 0 ;
407
+
408
+ for (pdfu = dfu_root; pdfu != NULL ; pdfu = pdfu->next ) {
409
+ j[" ports" ][index][" address" ] = get_path (pdfu->dev );
410
+ j[" ports" ][index][" label" ] = pdfu->alt_name ;
411
+ j[" ports" ][index][" protocol" ] = " dfu" ;
412
+ j[" ports" ][index][" protocolLabel" ] = pdfu->flags & DFU_IFF_DFU ? " DFU" : " Runtime" ;
413
+ j[" ports" ][index][" properties" ] = {
414
+ {" vid" , print_hex (pdfu->vendor )},
415
+ {" pid" , print_hex (pdfu->product )},
416
+ {" serialNumber" , pdfu->serial_name },
417
+ {" name" , pdfu->alt_name }
418
+ };
419
+ index++;
420
+ }
421
+ return j;
395
422
}
396
423
397
424
struct dfu_if *dfu_root = NULL ;
@@ -433,13 +460,28 @@ bool getline_async(std::istream& is, std::string& str, char delim = '\n') {
433
460
}
434
461
435
462
void print_list () {
436
- list_dfu_interfaces ();
463
+ auto j = list_dfu_interfaces ();
464
+ std::cout << j.dump (2 ) << std::endl;
437
465
}
438
466
467
+ json previous_list;
439
468
void print_event () {
440
- list_dfu_interfaces ();
469
+ auto j = list_dfu_interfaces ();
470
+ if (j.size ()!= previous_list.size ()) {
471
+ auto diff = json::diff (j, previous_list);
472
+ std::cout << diff.dump (2 ) << std::endl;
473
+ }
474
+ previous_list = j;
441
475
}
442
476
477
+ enum states {
478
+ IDLE,
479
+ START,
480
+ START_SYNC,
481
+ STOP,
482
+ QUIT
483
+ };
484
+
443
485
int main (int argc, char **argv)
444
486
{
445
487
libusb_context *ctx;
@@ -461,45 +503,69 @@ int main(int argc, char **argv)
461
503
// Set STDIN as nonblocking
462
504
// int flags = fcntl(0, F_GETFL, 0);
463
505
// fcntl(0, F_SETFL, flags | O_NONBLOCK);
506
+ json j;
507
+
508
+ j[" eventType" ] = " hello" ;
509
+ j[" protocolVersion" ] = 1 ;
510
+ j[" message" ] = " OK" ;
464
511
465
- printf (" {\n \
466
- \" eventType\" : \" hello\" ,\n \
467
- \" protocolVersion\" : 1,\n \
468
- \" message\" : \" OK\"\n \
469
- }\n " );
512
+ std::cout << j.dump (2 ) << std::endl;
513
+
514
+ std::atomic<int > state = IDLE;
470
515
471
516
while (1 ) {
472
517
473
518
std::string line;
474
- bool changed = false ;
475
519
std::getline (std::cin, line);
476
- std::atomic<bool > events = false ;
477
520
478
521
if (line.find (" START_SYNC" ) != std::string::npos) {
479
- std::thread ([&]
480
- {
481
- while ( 1 ) {
482
- if (events ) {
483
- probe_devices (ctx) ;
484
- print_event () ;
485
- std::this_thread::sleep_for ( std::chrono::seconds ( 1 )) ;
486
- }
522
+ if (state == IDLE) {
523
+ ret = libusb_init (&ctx);
524
+ j[ " eventType " ] = " start_sync " ;
525
+ if (ret != 0 ) {
526
+ j[ " message " ] = " Permission error " ;
527
+ j[ " error " ] = true ;
528
+ std::cout << j. dump ( 2 ) << std::endl ;
529
+ continue ;
487
530
}
488
- }).detach ();
531
+ std::thread ([&]
532
+ {
533
+ j[" message" ] = " OK" ;
534
+ std::cout << j.dump (2 ) << std::endl;
535
+ while (1 ) {
536
+ if (state == START_SYNC) {
537
+ probe_devices (ctx);
538
+ print_event ();
539
+ }
540
+ std::this_thread::sleep_for (std::chrono::seconds (5 ));
541
+ }
542
+ }).detach ();
543
+ }
544
+ state = START_SYNC;
489
545
} else if (line.find (" START" ) != std::string::npos) {
546
+ state = START;
547
+ j[" eventType" ] = " start" ;
490
548
ret = libusb_init (&ctx);
491
- if (ret) {
492
- // report error
549
+ if (ret == 0 ) {
550
+ j[ " message " ] = " OK " ;
493
551
} else {
494
- // report ok
552
+ j[" error" ] = true ;
553
+ j[" message" ] = " Permission error" ;
495
554
}
555
+ std::cout << j.dump (2 ) << std::endl;
496
556
} else if (line.find (" STOP" ) != std::string::npos) {
497
- if (events) {
498
- events = false ;
499
- } else {
557
+ j[" eventType" ] = " stop" ;
558
+ j[" message" ] = " OK" ;
559
+ std::cout << j.dump (2 ) << std::endl;
560
+ if (state != START_SYNC) {
500
561
libusb_exit (ctx);
501
562
}
563
+ state = STOP;
502
564
} else if (line.find (" QUIT" ) != std::string::npos) {
565
+ state = QUIT;
566
+ j[" eventType" ] = " quit" ;
567
+ j[" message" ] = " OK" ;
568
+ std::cout << j.dump (2 ) << std::endl;
503
569
exit (0 );
504
570
} else if (line.find (" LIST" ) != std::string::npos) {
505
571
probe_devices (ctx);
0 commit comments