Skip to content

Commit 7ecb762

Browse files
committed
implement protocol
start_sync broken ATM
1 parent 4e05ca8 commit 7ecb762

File tree

2 files changed

+93
-28
lines changed

2 files changed

+93
-28
lines changed

dfu_util.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ extern const char *match_serial_dfu;
3030
void probe_devices(libusb_context *);
3131
void disconnect_devices(void);
3232
void print_dfu_if(struct dfu_if *);
33-
void list_dfu_interfaces(void);
3433

3534
#endif /* DFU_UTIL_H */

main.cpp

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,40 @@ void print_dfu_if(struct dfu_if *dfu_if)
385385
dfu_if->serial_name);
386386
}
387387

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+
388398
/* Walk the device tree and print out DFU devices */
389-
void list_dfu_interfaces(void)
399+
json list_dfu_interfaces(void)
390400
{
391401
struct dfu_if *pdfu;
392402

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;
395422
}
396423

397424
struct dfu_if *dfu_root = NULL;
@@ -433,13 +460,28 @@ bool getline_async(std::istream& is, std::string& str, char delim = '\n') {
433460
}
434461

435462
void print_list() {
436-
list_dfu_interfaces();
463+
auto j = list_dfu_interfaces();
464+
std::cout << j.dump(2) << std::endl;
437465
}
438466

467+
json previous_list;
439468
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;
441475
}
442476

477+
enum states {
478+
IDLE,
479+
START,
480+
START_SYNC,
481+
STOP,
482+
QUIT
483+
};
484+
443485
int main(int argc, char **argv)
444486
{
445487
libusb_context *ctx;
@@ -461,45 +503,69 @@ int main(int argc, char **argv)
461503
// Set STDIN as nonblocking
462504
// int flags = fcntl(0, F_GETFL, 0);
463505
// fcntl(0, F_SETFL, flags | O_NONBLOCK);
506+
json j;
507+
508+
j["eventType"] = "hello";
509+
j["protocolVersion"] = 1;
510+
j["message"] = "OK";
464511

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;
470515

471516
while (1) {
472517

473518
std::string line;
474-
bool changed = false;
475519
std::getline(std::cin, line);
476-
std::atomic<bool> events = false;
477520

478521
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;
487530
}
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;
489545
} else if (line.find("START") != std::string::npos) {
546+
state = START;
547+
j["eventType"] = "start";
490548
ret = libusb_init(&ctx);
491-
if (ret) {
492-
// report error
549+
if (ret == 0) {
550+
j["message"] = "OK";
493551
} else {
494-
// report ok
552+
j["error"] = true;
553+
j["message"] = "Permission error";
495554
}
555+
std::cout << j.dump(2) << std::endl;
496556
} 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) {
500561
libusb_exit(ctx);
501562
}
563+
state = STOP;
502564
} 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;
503569
exit(0);
504570
} else if (line.find("LIST") != std::string::npos) {
505571
probe_devices(ctx);

0 commit comments

Comments
 (0)