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

Conversation

LeeLeahy2
Copy link
Contributor

Description of Change

Summary:

  • Fixes Issue 10318
  • Includes pull request 10321 that fixes 10316
  • Adds code to find the event callbacks
  • Issues error when duplicate callbacks insertion attempts are made
  • Issues error when callbacks are not found during removal

The fix for 10316 is required to get beyond the crash and detect Issue 10318.

Moved code from removeEvent to scan for the matching event into findEvent routines. The findEvent routines return the "id" or zero if the handler was not found.

Reworked removeEvent routines to use findEvent. Added log_e in the event callback handler not found path in removeEvent.

Added code to onEvent and onSysEvent routines to check for duplicate entries using findEvent. Added log_e that displays when the duplicate event is found.

Tests scenarios

Used the updated test sketch below

Related links

Closes 10318

Test Sketch


#define wifiSSID        "Your_WiFi_SSID"
#define wifiPassword    "Your_WiFi_Password"

#include <WiFi.h>

bool RTK_CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC = false;
network_event_handle_t wifiId;
network_event_handle_t ethernetId;
network_event_handle_t pppId;

//----------------------------------------
// System initialization
void setup()
{
    Serial.begin(115200);
    Serial.println();
    Serial.println("bug-NetworkEvents-removeEvent.ino");

    // Listen for wifi events
Serial.println("----------");
    Serial.printf("Registering WiFi callback handler: %p\r\n", (void *)wifiEvent);
    wifiId = Network.onEvent(wifiEvent);
Serial.println("----------");
    Serial.printf("Registering duplicate WiFi callback handler: %p, expecting error\r\n", (void *)wifiEvent);
    Network.onEvent(wifiEvent);

Serial.println("----------");
    Serial.printf("Registering Ethernet callback handler: %p\r\n", (void *)ethernetEvent);
    ethernetId = Network.onEvent(ethernetEvent);

Serial.println("----------");
    Serial.printf("Registering PPP callback handler: %p\r\n", (void *)pppEvent);
    pppId = Network.onEvent(pppEvent);

    // Start displaying the events
    Serial.println("==================== Receiving 3 events / actual event ====================");

    // Start WiFi
    WiFi.begin(wifiSSID, wifiPassword);
}

//----------------------------------------
// Main loop
void loop()
{
    static bool allCbsRemoved;
    static bool eventRemoved;
    static uint32_t lastTimeMillis;
    static bool wifiState;

    uint32_t currentMillis = millis();
    if ((currentMillis - lastTimeMillis) >= (10 * 1000))
    {
        lastTimeMillis = currentMillis;

        // Toggle the Wifi state
        wifiState ^= 1;
        if (wifiState)
        {
            Serial.println("---------- WiFi.disconnect ----------");
            WiFi.disconnect(true);
        }
        else
        {
            Serial.println("---------- WiFi.begin ----------");
            WiFi.begin(wifiSSID, wifiPassword);
        }
    }
    if ((!eventRemoved) && (currentMillis >= (30 * 1000)))
    {
        // Removing the WiFi event handler
        // This should remove them all!!!!!!
        Serial.println("==================== Removing the WiFi Event Handler ======================");
        Network.removeEvent(wifiEvent);
        eventRemoved = true;
        Serial.println("==================== Receiving 2 events / actual event ====================");
    }
    if ((!allCbsRemoved) && (currentMillis >= (60 * 1000)))
    {
        // Removing all handlers
        // This should remove them all!!!!!!
        Serial.println("==================== Removing all Event Handlers ============================");
        Network.removeEvent(ethernetEvent);
        Serial.println("Attempting duplicate function removal, expecting error");
        Network.removeEvent(ethernetEvent);
        Network.removeEvent(pppId);
        Serial.println("Attempting duplicate id removal, expecting error");
        Network.removeEvent(pppId);
        allCbsRemoved = true;
        Serial.println("==================== No Event Output Should Be Displayed ====================");
    }
}

//----------------------------------------
void wifiEvent(arduino_event_id_t event, arduino_event_info_t info)
{
    char ssid[sizeof(info.wifi_sta_connected.ssid) + 1];
    IPAddress ipAddress;

    // Handle the event
    switch (event)
    {
    default:
        Serial.printf("ERROR: Unknown WiFi event: %d\r\n", event);
        break;

    case ARDUINO_EVENT_WIFI_OFF:
        Serial.println("WiFi Off");
        break;

    case ARDUINO_EVENT_WIFI_READY:
        Serial.println("WiFi Ready");
        break;

    case ARDUINO_EVENT_WIFI_SCAN_DONE:
        Serial.println("WiFi Scan Done");
        // wifi_event_sta_scan_done_t info.wifi_scan_done;
        break;

    case ARDUINO_EVENT_WIFI_STA_START:
        Serial.println("WiFi STA Started");
        break;

    case ARDUINO_EVENT_WIFI_STA_STOP:
        Serial.println("WiFi STA Stopped");
        break;

    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
        memcpy(ssid, info.wifi_sta_connected.ssid, info.wifi_sta_connected.ssid_len);
        ssid[info.wifi_sta_connected.ssid_len] = 0;
        Serial.printf("WiFi STA connected to %s\r\n", ssid);
        break;

    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
        memcpy(ssid, info.wifi_sta_disconnected.ssid, info.wifi_sta_disconnected.ssid_len);
        ssid[info.wifi_sta_disconnected.ssid_len] = 0;
        Serial.printf("WiFi STA disconnected from %s\r\n", ssid);
        // wifi_event_sta_disconnected_t info.wifi_sta_disconnected;
        break;

    case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE:
        Serial.println("WiFi STA Auth Mode Changed");
        // wifi_event_sta_authmode_change_t info.wifi_sta_authmode_change;
        break;

    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
        ipAddress = WiFi.localIP();
        Serial.print("WiFi STA Got IPv4: ");
        Serial.println(ipAddress);
        break;

    case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
        Serial.print("WiFi STA Got IPv6: ");
        Serial.println(ipAddress);
        break;

    case ARDUINO_EVENT_WIFI_STA_LOST_IP:
        Serial.println("WiFi STA Lost IP");
        break;
    }
}

//----------------------------------------
void ethernetEvent(arduino_event_id_t event, arduino_event_info_t info)
{
    wifiEvent(event, info);
}

//----------------------------------------
void pppEvent(arduino_event_id_t event, arduino_event_info_t info)
{
    wifiEvent(event, info);
}

…veEvent

Fixes Issue 10318
Includes pull request 10321 that fixes 10316

This change:
* Adds code to find the event callbacks
* Issues error when duplicate callbacks insertion attempts are made
* Issues error when callbacks are not found during removal
Copy link
Contributor

github-actions bot commented Sep 12, 2024

Warnings
⚠️

Some issues found for the commit messages in this PR:

  • the commit message "Fix(NetworkEvents): Don't skip event callbacks in NetworkEvents::removeEvent":
    • scope/component should be lowercase without whitespace, allowed special characters are _ / . , * - .
    • type/action should start with a lowercase letter
    • type/action should be one of [change, ci, docs, feat, fix, refactor, remove, revert, test]
  • the commit message "Fix(NetworkEvents): Don't skip event callbacks in NetworkEvents::removeEvent":
    • scope/component should be lowercase without whitespace, allowed special characters are _ / . , * - .
    • type/action should start with a lowercase letter
    • type/action should be one of [change, ci, docs, feat, fix, refactor, remove, revert, test]

Please fix these commit messages - here are some basic tips:

  • follow Conventional Commits style
  • correct format of commit message should be: <type/action>(<scope/component>): <summary>, for example fix(esp32): Fixed startup timeout issue
  • allowed types are: change,ci,docs,feat,fix,refactor,remove,revert,test
  • sufficiently descriptive message summary should be between 20 to 72 characters and start with upper case letter
  • avoid Jira references in commit messages (unavailable/irrelevant for our customers)

TIP: Install pre-commit hooks and run this check when committing (uses the Conventional Precommit Linter).

Messages
📖 You might consider squashing your 4 commits (simplifying branch history).

👋 Hello LeeLeahy2, we appreciate your contribution to this project!


Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- Resolve all warnings (⚠️ ) before requesting a review from human reviewers - they will appreciate it.
- Addressing info messages (📖) is strongly recommended; they're less critical but valuable.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against 33a77d8

@me-no-dev
Copy link
Member

small conflict cause by the update of getStdFunctionAddress

Copy link
Contributor

github-actions bot commented Sep 13, 2024

Test Results

 56 files   -  83   56 suites   - 83   4m 18s ⏱️ - 1h 37m 38s
 21 tests  -   9   21 ✅  -   9  0 💤 ±0  0 ❌ ±0 
135 runs   - 168  135 ✅  - 168  0 💤 ±0  0 ❌ ±0 

Results for commit 33a77d8. ± Comparison against base commit 9e60bbe.

This pull request removes 9 tests.
performance.coremark.test_coremark ‑ test_coremark
performance.fibonacci.test_fibonacci ‑ test_fibonacci
performance.psramspeed.test_psramspeed ‑ test_psramspeed
performance.ramspeed.test_ramspeed ‑ test_ramspeed
performance.superpi.test_superpi ‑ test_superpi
test_touch_errors
test_touch_interrtupt
test_touch_read
validation.periman.test_periman ‑ test_periman

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Sep 13, 2024

Memory usage test (comparing PR against master branch)

The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.

MemoryFLASH [bytes]FLASH [%]RAM [bytes]RAM [%]
TargetDECINCDECINCDECINCDECINC
ESP32S30⚠️ +6880.00⚠️ +0.08000.000.00
ESP32S20⚠️ +7280.00⚠️ +0.09000.000.00
ESP32C30⚠️ +6800.00⚠️ +0.08000.000.00
ESP32C60⚠️ +6900.00⚠️ +0.08000.000.00
ESP32H20⚠️ +3580.00⚠️ +0.07000.000.00
ESP320⚠️ +7000.00⚠️ +0.09000.000.00
Click to expand the detailed deltas report [usage change in BYTES]
TargetESP32S3ESP32S2ESP32C3ESP32C6ESP32H2ESP32
ExampleFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAM
Ethernet/examples/ETH_W5500_Arduino_SPI⚠️ +3800⚠️ +3840⚠️ +3500⚠️ +3580⚠️ +3580⚠️ +3880
Ethernet/examples/ETH_W5500_IDF_SPI⚠️ +3800⚠️ +3720⚠️ +3500⚠️ +3580⚠️ +3580⚠️ +3880
Ethernet/examples/ETH_WIFI_BRIDGE⚠️ +4680⚠️ +4480⚠️ +4360⚠️ +4360--⚠️ +4680
NetworkClientSecure/examples/WiFiClientInsecure⚠️ +1600⚠️ +1720⚠️ +760⚠️ +680--⚠️ +1760
NetworkClientSecure/examples/WiFiClientPSK⚠️ +1640⚠️ +1680⚠️ +760⚠️ +680--⚠️ +1760
NetworkClientSecure/examples/WiFiClientSecure⚠️ +1640⚠️ +1720⚠️ +760⚠️ +680--⚠️ +1760
NetworkClientSecure/examples/WiFiClientSecureEnterprise⚠️ +1640⚠️ +1720⚠️ +760⚠️ +760--⚠️ +1760
NetworkClientSecure/examples/WiFiClientSecureProtocolUpgrade⚠️ +1720⚠️ +1680⚠️ +740⚠️ +740--⚠️ +1720
NetworkClientSecure/examples/WiFiClientShowPeerCredentials⚠️ +1640⚠️ +1760⚠️ +740⚠️ +740--⚠️ +1720
NetworkClientSecure/examples/WiFiClientTrustOnFirstUse⚠️ +1640⚠️ +1680⚠️ +760⚠️ +760--⚠️ +1720
PPP/examples/PPP_Basic⚠️ +3840⚠️ +3760⚠️ +3460⚠️ +3460⚠️ +3400⚠️ +3880
PPP/examples/PPP_WIFI_BRIDGE⚠️ +4640⚠️ +4360⚠️ +4420⚠️ +4420--⚠️ +4640
WebServer/examples/AdvancedWebServer⚠️ +1640⚠️ +1720⚠️ +780⚠️ +760--⚠️ +1680
WebServer/examples/FSBrowser⚠️ +1640⚠️ +1720⚠️ +700⚠️ +780--⚠️ +1800
WebServer/examples/Filters⚠️ +1640⚠️ +1640⚠️ +780⚠️ +780--⚠️ +1720
WebServer/examples/HelloServer⚠️ +1640⚠️ +1680⚠️ +780⚠️ +780--⚠️ +1760
WebServer/examples/HttpAdvancedAuth⚠️ +1680⚠️ +1880⚠️ +660⚠️ +760--⚠️ +1760
WebServer/examples/HttpAuthCallback⚠️ +1600⚠️ +1760⚠️ +760⚠️ +760--⚠️ +1800
WebServer/examples/HttpAuthCallbackInline⚠️ +1680⚠️ +1920⚠️ +760⚠️ +760--⚠️ +1720
WebServer/examples/HttpBasicAuth⚠️ +1640⚠️ +1840⚠️ +740⚠️ +660--⚠️ +1760
WebServer/examples/HttpBasicAuthSHA1⚠️ +1680⚠️ +1680⚠️ +760⚠️ +680--⚠️ +1760
WebServer/examples/HttpBasicAuthSHA1orBearerToken⚠️ +1640⚠️ +1640⚠️ +800⚠️ +800--⚠️ +1760
WebServer/examples/MultiHomedServers⚠️ +1640⚠️ +1600⚠️ +760⚠️ +760--⚠️ +1800
WebServer/examples/PathArgServer⚠️ +1640⚠️ +1680⚠️ +780⚠️ +780--⚠️ +1800
WebServer/examples/SDWebServer⚠️ +1680⚠️ +1840⚠️ +780⚠️ +700--⚠️ +1800
WebServer/examples/SimpleAuthentification⚠️ +1600⚠️ +1680⚠️ +780⚠️ +780--⚠️ +1680
WebServer/examples/UploadHugeFile⚠️ +1640⚠️ +1880⚠️ +780⚠️ +780--⚠️ +1400
WebServer/examples/WebServer⚠️ +1720⚠️ +1680⚠️ +780⚠️ +780--⚠️ +1760
WebServer/examples/WebUpdate⚠️ +1720⚠️ +1760⚠️ +760⚠️ +680--⚠️ +1760
WiFi/examples/FTM/FTM_Initiator⚠️ +2080⚠️ +1920⚠️ +1020⚠️ +1120--⚠️ +2120
WiFi/examples/FTM/FTM_Responder⚠️ +1680⚠️ +1720⚠️ +740⚠️ +840--⚠️ +1720
WiFi/examples/SimpleWiFiServer⚠️ +1640⚠️ +1680⚠️ +660⚠️ +740--⚠️ +1680
WiFi/examples/WPS⚠️ +5240⚠️ +5320⚠️ +4860⚠️ +4880--⚠️ +5680
WiFi/examples/WiFiAccessPoint⚠️ +1560⚠️ +1680⚠️ +660⚠️ +740--⚠️ +1720
WiFi/examples/WiFiBlueToothSwitch⚠️ +3120--⚠️ +2700⚠️ +2620--⚠️ +3280
WiFi/examples/WiFiClient⚠️ +1640⚠️ +1720⚠️ +760⚠️ +680--⚠️ +1680
WiFi/examples/WiFiClientBasic⚠️ +1640⚠️ +1760⚠️ +760⚠️ +680--⚠️ +1720
WiFi/examples/WiFiClientConnect⚠️ +1680⚠️ +1800⚠️ +680⚠️ +760--⚠️ +1720
WiFi/examples/WiFiClientEnterprise⚠️ +1640⚠️ +1680⚠️ +680⚠️ +760--⚠️ +1720
WiFi/examples/WiFiClientEvents⚠️ +6880⚠️ +7280⚠️ +6800⚠️ +6900--⚠️ +7000
WiFi/examples/WiFiClientStaticIP⚠️ +1640⚠️ +1720⚠️ +800⚠️ +680--⚠️ +1760
WiFi/examples/WiFiExtender⚠️ +5520⚠️ +5680⚠️ +4960⚠️ +5000--⚠️ +5520
WiFi/examples/WiFiIPv6⚠️ +2960⚠️ +3280⚠️ +2660⚠️ +2580--⚠️ +3200
WiFi/examples/WiFiMulti⚠️ +1800⚠️ +1720⚠️ +780⚠️ +700--⚠️ +1720
WiFi/examples/WiFiMultiAdvanced⚠️ +1680⚠️ +1640⚠️ +780⚠️ +780--⚠️ +1760
WiFi/examples/WiFiScan⚠️ +1680⚠️ +1680⚠️ +760⚠️ +680--⚠️ +1720
WiFi/examples/WiFiScanAsync⚠️ +1720⚠️ +1720⚠️ +780⚠️ +660--⚠️ +1800
WiFi/examples/WiFiScanDualAntenna⚠️ +1680⚠️ +1640⚠️ +680⚠️ +800--⚠️ +1720
WiFi/examples/WiFiScanTime⚠️ +1680⚠️ +1720⚠️ +740⚠️ +680--⚠️ +1720
WiFi/examples/WiFiSmartConfig⚠️ +1680⚠️ +1720⚠️ +740⚠️ +760--⚠️ +1720
WiFi/examples/WiFiTelnetToSerial⚠️ +1560⚠️ +1680⚠️ +700⚠️ +780--⚠️ +1720
WiFi/examples/WiFiUDPClient⚠️ +3120⚠️ +3240⚠️ +2700⚠️ +2640--⚠️ +3400
Ethernet/examples/ETH_LAN8720----------⚠️ +4680
Ethernet/examples/ETH_TLK110----------⚠️ +4680

@JAndrassy
Copy link
Contributor

I would log duplicate addition and not found removal only as warning.

@me-no-dev
Copy link
Member

@LeeLeahy2 I agree with @JAndrassy . Will you please change the logs to warning?

…veEvent

Fixes Issue 10318
Includes pull request 10321 that fixes 10316

This change:
* Adds code to find the event callbacks
* Issues warning when duplicate callbacks insertion attempts are made
* Issues warning when callbacks are not found during removal
@LeeLeahy2
Copy link
Contributor Author

The pull request was updated to change log_e to log_w in onEvent, onSysEvent and removeEvent. The initial choice of log_e was due to the comment in 10319.


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

@me-no-dev me-no-dev added the Status: Pending Merge Pull Request is ready to be merged label Sep 16, 2024
@me-no-dev me-no-dev merged commit 2f89026 into espressif:master Sep 16, 2024
68 checks passed
@vortigont
Copy link
Contributor

This PR causes #10365 issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Pending Merge Pull Request is ready to be merged
Projects
None yet
5 participants