Skip to content

Commit 4e05ca8

Browse files
committed
some more stuff
1 parent fa1de50 commit 4e05ca8

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

main.cpp

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include <iostream>
2121
#include <string>
22+
#include <thread>
23+
#include <atomic>
2224

2325
/*
2426
* 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') {
414416
str = "";
415417

416418
do {
417-
charsRead = is.readsome(&inChar, 1);
419+
charsRead = is.readsome(&inChar, 255);
418420
if (charsRead == 1) {
419421
// if the delimiter is read then return the string so far
420422
if (inChar == delim) {
421423
str = lineSoFar;
422424
lineSoFar = "";
423425
lineRead = true;
424426
} else { // otherwise add it to the string so far
425-
lineSoFar.append(1, inChar);
427+
lineSoFar.append(charsRead, inChar);
426428
}
427429
}
428430
} while (charsRead != 0 && !lineRead);
429431

430432
return lineRead;
431433
}
432434

435+
void print_list() {
436+
list_dfu_interfaces();
437+
}
438+
439+
void print_event() {
440+
list_dfu_interfaces();
441+
}
442+
433443
int main(int argc, char **argv)
434444
{
435445
libusb_context *ctx;
@@ -449,44 +459,51 @@ int main(int argc, char **argv)
449459
}
450460

451461
// 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);
454464

455465
printf("{\n\
456466
\"eventType\": \"hello\",\n\
457467
\"protocolVersion\": 1,\n\
458468
\"message\": \"OK\"\n\
459469
}\n");
460470

461-
bool events = false;
462-
463471
while (1) {
464472

465473
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;
468477

469478
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();
471489
} else if (line.find("START") != std::string::npos) {
472490
ret = libusb_init(&ctx);
473491
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);
475501
}
476-
} else if (line.find("START") != std::string::npos) {
477-
// start listener
478502
} else if (line.find("QUIT") != std::string::npos) {
479503
exit(0);
480504
} else if (line.find("LIST") != std::string::npos) {
481505
probe_devices(ctx);
482506
print_list();
483507
}
484-
if (events) {
485-
probe_devices(ctx);
486-
if (changed) {
487-
print_event();
488-
milli_sleep(1000);
489-
}
490-
}
491508
}
492509
}

0 commit comments

Comments
 (0)