Skip to content

Commit 1024fa4

Browse files
committed
Properly implement add/remove for start_sync
1 parent 7bc3926 commit 1024fa4

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

main.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,21 @@ json list_dfu_interfaces(void) {
402402
int index = 0;
403403

404404
for (pdfu = dfu_root; pdfu != NULL; pdfu = pdfu->next) {
405+
406+
if (!(pdfu->flags & DFU_IFF_DFU)) {
407+
// decide what to do with DFU runtime objects
408+
// for the time being, ignore them
409+
continue;
410+
}
411+
412+
for (int i = 0; i < index; i++) {
413+
// allow only one object
414+
if (j["ports"][i]["address"] == get_path(pdfu->dev)) {
415+
index = i;
416+
break;
417+
}
418+
}
419+
405420
j["ports"][index]["address"] = get_path(pdfu->dev);
406421
j["ports"][index]["label"] = pdfu->alt_name;
407422
j["ports"][index]["protocol"] = "dfu";
@@ -459,12 +474,48 @@ void print_list() {
459474
std::cout << j.dump(2) << std::endl;
460475
}
461476

477+
478+
479+
462480
json previous_list;
463481
void print_event() {
464482
auto j = list_dfu_interfaces();
465483
auto diff = json::diff(previous_list, j);
484+
485+
json diff_discovery;
486+
//diff_discovery["eventType"] = "remove";
487+
466488
if (diff.size() > 0) {
467-
std::cout << diff.dump(2) << std::endl;
489+
for (auto& new_port : j["ports"].items()) {
490+
bool found = false;
491+
for (auto& old_port : previous_list["ports"].items()) {
492+
if (new_port.value()["address"] == old_port.value()["address"]) {
493+
found = true;
494+
break;
495+
}
496+
}
497+
if (!found) {
498+
diff_discovery["eventType"] = "add";
499+
diff_discovery["port"] = new_port.value();
500+
std::cout << diff_discovery.dump(2) << std::endl;
501+
}
502+
}
503+
504+
for (auto& new_port : previous_list["ports"].items()) {
505+
bool found = false;
506+
for (auto& old_port : j["ports"].items()) {
507+
if (new_port.value()["address"] == old_port.value()["address"]) {
508+
found = true;
509+
break;
510+
}
511+
}
512+
if (!found) {
513+
diff_discovery["eventType"] = "remove";
514+
diff_discovery["port"] = new_port.value();
515+
std::cout << diff_discovery.dump(2) << std::endl;
516+
}
517+
}
518+
//std::cout << diff.dump(2) << std::endl;
468519
}
469520
previous_list = j;
470521
}

0 commit comments

Comments
 (0)