Skip to content

BLE provisioning sample doesn't work on release/v2.x branch #8176

Closed
@sidwarkd

Description

@sidwarkd

Board

ESP32 Dev Module

Device Description

DevKitC plain

Hardware Configuration

No additional hardware connected.

Version

latest master (checkout manually)

IDE Name

VSCode

Operating System

Linux

Flash frequency

40MHz

PSRAM enabled

no

Upload speed

2000000

Description

The use of esp_bt_controller_mem_release does not seem correct in esp32-hal-misc.c. In the function initArduino() if you have set BT_ENABLED in sdkconfig, it will call btInUse to determine whether to release the BT memory. There are two problems.

First, in using the sample sketch below (a slightly modified version of the provisioning sample included with this library) it does not appear to link correctly. The sample will fail to run properly as the BT memory is released in initArduino so trying to enable BT later for provisioning fails. The weak linking is not working as expected.

Secondly, this pattern seems wrong as well. The code in initArduino() would imply that you could have CONFIG_BT_ENABLED but have btInUse() return false which, per the code, would never be possible. If CONFIG_BT_ENABLED is yes then esp32-hal-bt.c declares btInUse() to return true always. So there is no scenario where if the config is enabled btInUse() should return false (although it does per the first problem mentioned above).

This makes the following block of code in esp32-hal-misc.c puzzling:

#ifdef CONFIG_BT_ENABLED
    if(!btInUse()){
        esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
    }
#endif

To get the provisioning example code to work with BLE the call to esp_bt_controller_mem_release must NOT be called in initArduino.

Sketch

#include "WiFiProv.h"
#include "WiFi.h"
#include "nvs_flash.h"
#include "nvs.h"

bool is_provisioned = false;
void SysProvEvent(arduino_event_t *sys_event)
{
    switch (sys_event->event_id)
    {
    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
        Serial.print("\nConnected IP address : ");
        Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr));
        is_provisioned = true;
        break;
    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
        Serial.println("\nDisconnected. Connecting to the AP again... ");
        break;
    case ARDUINO_EVENT_PROV_START:
        Serial.println("\nProvisioning started\nGive Credentials of your access point using \" Android app \"");
        break;
    case ARDUINO_EVENT_PROV_CRED_RECV:
    {
        Serial.println("\nReceived Wi-Fi credentials");
        Serial.print("\tSSID : ");
        Serial.println((const char *)sys_event->event_info.prov_cred_recv.ssid);
        Serial.print("\tPassword : ");
        Serial.println((char const *)sys_event->event_info.prov_cred_recv.password);
        break;
    }
    case ARDUINO_EVENT_PROV_CRED_FAIL:
    {
        Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n");
        if (sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR)
            Serial.println("\nWi-Fi AP password incorrect");
        else
            Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()");
        break;
    }
    case ARDUINO_EVENT_PROV_CRED_SUCCESS:
        Serial.println("\nProvisioning Successful");
        break;
    case ARDUINO_EVENT_PROV_END:
        Serial.println("\nProvisioning Ends");
        break;
    default:
        break;
    }
}

void setup()
{
    Serial.begin(115200);
    WiFi.onEvent(SysProvEvent);
    WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, "abcd1234", "Prov_123");
}

void loop()
{
    if(is_provisioned)
    {
        Serial.println("Connected to Wi-Fi and ready to run main application");
        delay(5000);
    }
    else
    {
        Serial.println("Waiting for Wi-Fi credentials. Open app to get started.");
        delay(5000);
    }
}


### Debug Message

```plain
E (912) wifi_prov_scheme_ble: bt_mem_release of classic BT failed 259
I (920) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (1022) wifi:mode : sta (ac:0b:fb:6c:f4:8c)
I (1023) wifi:enable tsf
I (1024) wifi:Set ps type: 1

E (1027) simple_ble: simple_ble_start enable controller failed 259
E (1028) protocomm_ble: simple_ble_start failed w/ error code 0x103
E (1035) wifi_prov_scheme_ble: Failed to start protocomm BLE service
E (1041) wifi_prov_mgr: Failed to start service

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Labels

Area: BLEIssues related to BLEType: Bug 🐛All bugsbug 🐞Inconsistencies or issues which will cause a problem for users or implementers.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions