Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(NetworkEvents): Don't skip event callbacks in NetworkEvents::remo… #10337

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 118 additions & 23 deletions libraries/Network/src/NetworkEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,73 @@ void NetworkEvents::checkForEvent() {
free(event);
}

uint32_t NetworkEvents::findEvent(NetworkEventCb cbEvent, arduino_event_id_t event) {
uint32_t i;

if (!cbEvent) {
return cbEventList.size();
}

for (i = 0; i < cbEventList.size(); i++) {
NetworkEventCbList_t entry = cbEventList[i];
if (entry.cb == cbEvent && entry.event == event) {
break;
}
}
return i;
}

template<typename T, typename... U> static size_t getStdFunctionAddress(std::function<T(U...)> f) {
typedef T(fnType)(U...);
fnType **fnPointer = f.template target<fnType *>();
if (fnPointer != nullptr) {
return (size_t)*fnPointer;
}
return (size_t)fnPointer;
}

uint32_t NetworkEvents::findEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t event) {
uint32_t i;

if (!cbEvent) {
return cbEventList.size();
}

for (i = 0; i < cbEventList.size(); i++) {
NetworkEventCbList_t entry = cbEventList[i];
if (getStdFunctionAddress(entry.fcb) == getStdFunctionAddress(cbEvent) && entry.event == event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not place the result of getStdFunctionAddress(cbEvent) outside the for loop?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't before, it's OK not to change that now also

break;
}
}
return i;
}

uint32_t NetworkEvents::findEvent(NetworkEventSysCb cbEvent, arduino_event_id_t event) {
uint32_t i;

if (!cbEvent) {
return cbEventList.size();
}

for (i = 0; i < cbEventList.size(); i++) {
NetworkEventCbList_t entry = cbEventList[i];
if (entry.scb == cbEvent && entry.event == event) {
break;
}
}
return i;
}

network_event_handle_t NetworkEvents::onEvent(NetworkEventCb cbEvent, arduino_event_id_t event) {
if (!cbEvent) {
return 0;
}

if (findEvent(cbEvent, event) < cbEventList.size()) {
log_w("Attempt to add duplicate event handler!");
return 0;
}

NetworkEventCbList_t newEventHandler;
newEventHandler.cb = cbEvent;
newEventHandler.fcb = NULL;
Expand All @@ -151,6 +214,12 @@ network_event_handle_t NetworkEvents::onEvent(NetworkEventFuncCb cbEvent, arduin
if (!cbEvent) {
return 0;
}

if (findEvent(cbEvent, event) < cbEventList.size()) {
log_w("Attempt to add duplicate event handler!");
return 0;
}

NetworkEventCbList_t newEventHandler;
newEventHandler.cb = NULL;
newEventHandler.fcb = cbEvent;
Expand All @@ -164,6 +233,12 @@ network_event_handle_t NetworkEvents::onEvent(NetworkEventSysCb cbEvent, arduino
if (!cbEvent) {
return 0;
}

if (findEvent(cbEvent, event) < cbEventList.size()) {
log_w("Attempt to add duplicate event handler!");
return 0;
}

NetworkEventCbList_t newEventHandler;
newEventHandler.cb = NULL;
newEventHandler.fcb = NULL;
Expand All @@ -177,6 +252,12 @@ network_event_handle_t NetworkEvents::onSysEvent(NetworkEventCb cbEvent, arduino
if (!cbEvent) {
return 0;
}

if (findEvent(cbEvent, event) < cbEventList.size()) {
log_w("Attempt to add duplicate event handler!");
return 0;
}

NetworkEventCbList_t newEventHandler;
newEventHandler.cb = cbEvent;
newEventHandler.fcb = NULL;
Expand All @@ -190,6 +271,12 @@ network_event_handle_t NetworkEvents::onSysEvent(NetworkEventFuncCb cbEvent, ard
if (!cbEvent) {
return 0;
}

if (findEvent(cbEvent, event) < cbEventList.size()) {
log_w("Attempt to add duplicate event handler!");
return 0;
}

NetworkEventCbList_t newEventHandler;
newEventHandler.cb = NULL;
newEventHandler.fcb = cbEvent;
Expand All @@ -203,6 +290,12 @@ network_event_handle_t NetworkEvents::onSysEvent(NetworkEventSysCb cbEvent, ardu
if (!cbEvent) {
return 0;
}

if (findEvent(cbEvent, event) < cbEventList.size()) {
log_w("Attempt to add duplicate event handler!");
return 0;
}

NetworkEventCbList_t newEventHandler;
newEventHandler.cb = NULL;
newEventHandler.fcb = NULL;
Expand All @@ -213,60 +306,62 @@ network_event_handle_t NetworkEvents::onSysEvent(NetworkEventSysCb cbEvent, ardu
}

void NetworkEvents::removeEvent(NetworkEventCb cbEvent, arduino_event_id_t event) {
uint32_t i;

if (!cbEvent) {
return;
}

for (uint32_t i = 0; i < cbEventList.size(); i++) {
NetworkEventCbList_t entry = cbEventList[i];
if (entry.cb == cbEvent && entry.event == event) {
cbEventList.erase(cbEventList.begin() + i);
}
i = findEvent(cbEvent, event);
if (i >= cbEventList.size()) {
log_w("Event handler not found!");
return;
}
}

template<typename T, typename... U> static size_t getStdFunctionAddress(std::function<T(U...)> f) {
typedef T(fnType)(U...);
fnType **fnPointer = f.template target<fnType *>();
if (fnPointer != nullptr) {
return (size_t)*fnPointer;
}
return (size_t)fnPointer;
cbEventList.erase(cbEventList.begin() + i);
}

void NetworkEvents::removeEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t event) {
uint32_t i;

if (!cbEvent) {
return;
}

for (uint32_t i = 0; i < cbEventList.size(); i++) {
NetworkEventCbList_t entry = cbEventList[i];
if (getStdFunctionAddress(entry.fcb) == getStdFunctionAddress(cbEvent) && entry.event == event) {
cbEventList.erase(cbEventList.begin() + i);
}
i = findEvent(cbEvent, event);
if (i >= cbEventList.size()) {
log_w("Event handler not found!");
return;
}

cbEventList.erase(cbEventList.begin() + i);
}

void NetworkEvents::removeEvent(NetworkEventSysCb cbEvent, arduino_event_id_t event) {
uint32_t i;

if (!cbEvent) {
return;
}

for (uint32_t i = 0; i < cbEventList.size(); i++) {
NetworkEventCbList_t entry = cbEventList[i];
if (entry.scb == cbEvent && entry.event == event) {
cbEventList.erase(cbEventList.begin() + i);
}
i = findEvent(cbEvent, event);
if (i >= cbEventList.size()) {
log_w("Event handler not found!");
return;
}

cbEventList.erase(cbEventList.begin() + i);
}

void NetworkEvents::removeEvent(network_event_handle_t id) {
for (uint32_t i = 0; i < cbEventList.size(); i++) {
NetworkEventCbList_t entry = cbEventList[i];
if (entry.id == id) {
cbEventList.erase(cbEventList.begin() + i);
return;
}
}
log_w("Event handler not found!");
}

int NetworkEvents::setStatusBits(int bits) {
Expand Down
3 changes: 3 additions & 0 deletions libraries/Network/src/NetworkEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class NetworkEvents {

protected:
bool initNetworkEvents();
uint32_t findEvent(NetworkEventCb cbEvent, arduino_event_id_t event);
uint32_t findEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t event);
uint32_t findEvent(NetworkEventSysCb cbEvent, arduino_event_id_t event);
network_event_handle_t onSysEvent(NetworkEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
network_event_handle_t onSysEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
network_event_handle_t onSysEvent(NetworkEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
Expand Down
Loading