espressif / esp-nimble

A fork of NimBLE stack, for use with ESP32 and ESP-IDF
Apache License 2.0
76 stars 49 forks source link

L2CAP: Proper shutdown? #67

Closed mickeyl closed 3 months ago

mickeyl commented 3 months ago

In my application, L2CAP is not always active. I want the system to reclaim the memory. What's the recommended shutdown procedure, i.e. what's the inverse of ble_l2cap_create_server?

rahult-github commented 3 months ago

Hi @mickeyl ,

Upstream nimble architecture doesn't have a way to shutdown L2CAP . One of the reasons being that this is not something most system would do . Also shutting down l2cap will eventually imply shutting down upper protocol layers that work on top of L2CAP too. This , as i see, is not supported architecturally ( atleast till now ) in NimBLE .

You can shut down the complete stack and reclaim memory, but just shutting down L2CAP layer is currently not possible.

mickeyl commented 3 months ago

@rahult-github I see, thanks. Ok, how would I go about shutting down the complete stack then? Would

    int ret = nimble_port_stop();
    if (ret == 0) {
        nimble_port_deinit();

be enough?

Right now I see a memory leak of about 20K when I do that.

rahult-github commented 3 months ago

Hi @mickeyl ,

Yes. You can refer to this example code which basically inits / deinits the stack in a loop for more details.

rahult-github commented 3 months ago

Hi @mickeyl

Right now I see a memory leak of about 20K when I do that.

may i know if there is any other allocation done apart from stack resources in your code ?

Can you use the blecent example in IDF and put prints for same ?

I gave a quick try for the application and printed heap memory before / after init .

I (356) BLE_INIT: BT controller compile version [30b57c4] I (366) BLE_INIT: Bluetooth MAC: 7c:df:a1:a2:9b:f2 I (366) phy_init: phy_version 1150,7c3c08f,Jan 24 2024,17:32:21 I (406) NimBLE_BLE_CENT: BLE Host Task Started I (406) NimBLE: GAP procedure initiated: stop advertising.

I (1416) NimBLE_BLE_CENT: Deinit host Before stack stop 220488 I (1416) NimBLE: GAP procedure initiated: stop advertising.

I (2416) NimBLE_BLE_CENT: Init host Before stack start 279168 I (2416) BLE_INIT: BT controller compile version [30b57c4] I (2416) BLE_INIT: Bluetooth MAC: 7c:df:a1:a2:9b:f2 I (2426) NimBLE_BLE_CENT: BLE Host Task Started I (2426) NimBLE: GAP procedure initiated: stop advertising.

I (2436) NimBLE_BLE_CENT: Waiting for 1 second I (3436) NimBLE_BLE_CENT: Deinit host Before stack stop 220488 I (3436) NimBLE: GAP procedure initiated: stop advertising.

I (4436) NimBLE_BLE_CENT: Init host Before stack start 279168 I (4436) BLE_INIT: BT controller compile version [30b57c4] I (4436) BLE_INIT: Bluetooth MAC: 7c:df:a1:a2:9b:f2 I (4446) NimBLE_BLE_CENT: BLE Host Task Started I (4446) NimBLE: GAP procedure initiated: stop advertising.

I (4456) NimBLE_BLE_CENT: Waiting for 1 second I (5456) NimBLE_BLE_CENT: Deinit host Before stack stop 220488 I (5456) NimBLE: GAP procedure initiated: stop advertising.

I (6456) NimBLE_BLE_CENT: Init host Before stack start 279168 I (6456) BLE_INIT: BT controller compile version [30b57c4] I (6456) BLE_INIT: Bluetooth MAC: 7c:df:a1:a2:9b:f2 I (6466) NimBLE_BLE_CENT: BLE Host Task Started I (6466) NimBLE: GAP procedure initiated: stop advertising.

I (6476) NimBLE_BLE_CENT: Waiting for 1 second I (7476) NimBLE_BLE_CENT: Deinit host Before stack stop 220488 I (7476) NimBLE: GAP procedure initiated: stop advertising.

I (8476) NimBLE_BLE_CENT: Init host Before stack start 279168 I (8476) BLE_INIT: BT controller compile version [30b57c4] I (8476) BLE_INIT: Bluetooth MAC: 7c:df:a1:a2:9b:f2 I (8486) NimBLE_BLE_CENT: BLE Host Task Started I (8486) NimBLE: GAP procedure initiated: stop advertising.

for above, i used the heap_caps_get_free_size API. if you using some other method, then please let me know . Also the chip and idf versions information would be helpful.

mickeyl commented 3 months ago

@rahult-github Thanks a lot, that looks very good. You are right, I need to go on and check all my allocations, I'm sure the BLE subsystem itself is not the culprit then. Thanks again for your quick and competent answers!

mickeyl commented 3 months ago

FWIW, I still think there should be an equivalent to ble_l2cap_create_server, since we can't avoid memory leaks otherwise. I'm trying to address this upstream.