pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

Instability when exiting btstack run loop

Open josh26turner opened this issue 1 year ago • 1 comments

I'm not sure if this issue should sit in this repo or BTStack repo.

I have an application for the Pico W that will periodically take a measurement and send the value over a Bluetooth notifcation to a connected device. As it's battery powered, after sending a notification, I run the function btstack_run_loop_trigger_exit(), and have a loop that looks (roughly) like this:

while (true) {
    printf("Starting btstack\n");
    btstack_run_loop_execute();
    printf("Exited btstack\n");

    printf("Sleeping for %ds\n", POST_MEASUREMENT_SLEEP_TIME_MS / 1000);
    sleep_ms(POST_MEASUREMENT_SLEEP_TIME_MS);

    // Less important, queuing up work for when btstack starts again
    if (le_notification_enabled)
    {
        btstack_run_loop_set_timer(&sensor_reading_timer, 0);
        btstack_run_loop_add_timer(&sensor_reading_timer);
    }
}

Which seems to work for the first few measurement, producing logs that look like this (the "Exiting btstack" log line is printed immediately before calling btstack_run_loop_trigger_exit()):

Starting btstack
BTstack up and running on 28:CD:C1:09:3E:D0.
Enabling notifications
Requesting reading
Sensor values read
Sending notification to connected device
Exiting btstack
Exited btstack
Sleeping for 10s
Starting btstack
Requesting reading
Sensor values read
Sending notification to connected device
Exiting btstack
Exited btstack
Sleeping for 10s
Starting btstack
Requesting reading
Sensor values read
Sending notification to connected device
Exiting btstack
Exited btstack

However, after a few successful attempts (not consistent in number), instead of continuing code following btstack_run_loop_execute(), the exit will jump back code when exiting the run loop. It starts with the line before and then immediately starts btstack again:

Starting btstack
Requesting reading
Sensor values read
Sending notification to connected device
Exiting btstack
Exited btstack
Sleeping for 10s
Requesting reading
Sensor values read
Sending notification to connected device
Exiting btstack
Starting btstack

You can see that immediately following the "Exiting btstack" log line is the line "Starting btstack". At which point the program hangs (as I haven't queued up any work for btstack to do).

If required, I can add some example code to run and replicate.

josh26turner avatar Aug 19 '23 22:08 josh26turner