19
19
20
20
#include < iostream>
21
21
#include < string>
22
+ #include < thread>
23
+ #include < atomic>
22
24
23
25
/*
24
26
* Look for a descriptor in a concatenated descriptor list. Will
@@ -414,22 +416,30 @@ bool getline_async(std::istream& is, std::string& str, char delim = '\n') {
414
416
str = " " ;
415
417
416
418
do {
417
- charsRead = is.readsome (&inChar, 1 );
419
+ charsRead = is.readsome (&inChar, 255 );
418
420
if (charsRead == 1 ) {
419
421
// if the delimiter is read then return the string so far
420
422
if (inChar == delim) {
421
423
str = lineSoFar;
422
424
lineSoFar = " " ;
423
425
lineRead = true ;
424
426
} else { // otherwise add it to the string so far
425
- lineSoFar.append (1 , inChar);
427
+ lineSoFar.append (charsRead , inChar);
426
428
}
427
429
}
428
430
} while (charsRead != 0 && !lineRead);
429
431
430
432
return lineRead;
431
433
}
432
434
435
+ void print_list () {
436
+ list_dfu_interfaces ();
437
+ }
438
+
439
+ void print_event () {
440
+ list_dfu_interfaces ();
441
+ }
442
+
433
443
int main (int argc, char **argv)
434
444
{
435
445
libusb_context *ctx;
@@ -449,44 +459,51 @@ int main(int argc, char **argv)
449
459
}
450
460
451
461
// Set STDIN as nonblocking
452
- // int flags = fcntl(0, F_GETFL, 0);
453
- // fcntl(0, F_SETFL, flags | O_NONBLOCK);
462
+ // int flags = fcntl(0, F_GETFL, 0);
463
+ // fcntl(0, F_SETFL, flags | O_NONBLOCK);
454
464
455
465
printf (" {\n \
456
466
\" eventType\" : \" hello\" ,\n \
457
467
\" protocolVersion\" : 1,\n \
458
468
\" message\" : \" OK\"\n \
459
469
}\n " );
460
470
461
- bool events = false ;
462
-
463
471
while (1 ) {
464
472
465
473
std::string line;
466
- getline_async (std::cin, line);
467
-
474
+ bool changed = false ;
475
+ std::getline (std::cin, line);
476
+ std::atomic<bool > events = false ;
468
477
469
478
if (line.find (" START_SYNC" ) != std::string::npos) {
470
- events = true ;
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
+ }
487
+ }
488
+ }).detach ();
471
489
} else if (line.find (" START" ) != std::string::npos) {
472
490
ret = libusb_init (&ctx);
473
491
if (ret) {
474
-
492
+ // report error
493
+ } else {
494
+ // report ok
495
+ }
496
+ } else if (line.find (" STOP" ) != std::string::npos) {
497
+ if (events) {
498
+ events = false ;
499
+ } else {
500
+ libusb_exit (ctx);
475
501
}
476
- } else if (line.find (" START" ) != std::string::npos) {
477
- // start listener
478
502
} else if (line.find (" QUIT" ) != std::string::npos) {
479
503
exit (0 );
480
504
} else if (line.find (" LIST" ) != std::string::npos) {
481
505
probe_devices (ctx);
482
506
print_list ();
483
507
}
484
- if (events) {
485
- probe_devices (ctx);
486
- if (changed) {
487
- print_event ();
488
- milli_sleep (1000 );
489
- }
490
- }
491
508
}
492
509
}
0 commit comments