Skip to content

Fixes Maximum BLE Device Length #7901

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

Closed
wants to merge 5 commits into from

Conversation

SuGlider
Copy link
Collaborator

@SuGlider SuGlider commented Feb 28, 2023

Description of Change

Fixes IDF esp_ble_gap_set_device_name() that allows name length bigger than Advertsing payload space.

The device name must have the maximum length of 29 characters.
If the device name has between 30 and 32 characters long, an error of Stack smashing protect failure! + ABORT & RESET will occur

If the device name has a length of 33 or more, it won't reset the board, but the last valid device name will be used.
Also, an error message will be displayed in the Serial Console.
[BLEDevice.cpp:416] init(): esp_ble_gap_set_device_name: rc=258 Unknown ESP_ERR error

There is a "gap" that allows name with 30 to 32 bytes to move forward and it leads to abort and reset the board.
Changing the device name to something that call Arduino User attention may also help.
Another way to heko the Arduino User, would be to change void BLEDevice::init(std::string deviceName) to return a bool in case of failure in order to allow the Sketch to identify it and take some action.

Tests scenarios

Testing case from #7894

New bool BLEDevice::init() shall allow testing if it was successful.

    if (!BLEDevice::init("MZ_MZL-FC-E-XX-999999-C8F09EE7C0CC")) {
       log_e("BLE Initialization Failure!");
       while(1) delay(1000); // halts execution.
   }

This code demonstrates the initial issue:

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

uint8_t txValue = 0;
BLEServer *pServer = NULL;                   //BLEServer指针 pServer
BLECharacteristic *pTxCharacteristic = NULL; //BLECharacteristic指针 pTxCharacteristic
bool deviceBleConnected = false;             //本次连接状态
bool oldDeviceConnected = false;             //上次连接状态

// See the following for generating UUIDs: https://www.uuidgenerator.net/
#define SERVICE_UUID "83677baa-3eb8-4866-b6b6-96e5ed5cc48d" // UART service UUID
#define CHARACTERISTIC_UUID_RX "f5d2b3fe-e6b5-49b5-aa5f-a00bb4156d1d"
#define CHARACTERISTIC_UUID_TX "6f588463-f8f1-44f8-bdae-a1272a1b0f6e"


class MyServerCallbacks : public BLEServerCallbacks
{
    void onConnect(BLEServer *pServer) {};
    void onDisconnect(BLEServer *pServer) {}
};

//ble相关
class MyBLECallbacks : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *pCharacteristic) {}
};

void setup() {
  Serial.begin(115200);
  Serial.println("START >>>>>>>>>>>>>>>>>>>> ");

  BLEDevice::init(String("MZ_MZL-FC-E-XX-999999-C8F09EE7C0CC").c_str());

  // 创建一个 BLE 服务
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks()); //设置回调
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // 创建一个 BLE 特征
  pTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
  pTxCharacteristic->addDescriptor(new BLE2902());
  BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, 
  BLECharacteristic::PROPERTY_WRITE);
  pRxCharacteristic->setCallbacks(new MyBLECallbacks()); //设置回调

  BLEAdvertisementData d = BLEAdvertisementData();
  d.setManufacturerData("123");

  pService->start();                  // 开始服务
  pServer->getAdvertising()->setAdvertisementData(d);

  pServer->getAdvertising()->start(); // 开始广播
  Serial.println("ble inited, waiting for connection.....");
}

void loop() {
}

Related links

Fixes #7894

fixes IDF esp_ble_gap_set_device_name() that allows name length bigger than Advertsing payload space.
@SuGlider SuGlider added the Area: BLE Issues related to BLE label Feb 28, 2023
@SuGlider SuGlider added this to the 3.0.0 milestone Feb 28, 2023
@SuGlider SuGlider self-assigned this Feb 28, 2023
@SuGlider SuGlider added the Sprint Issue is in current sprint label Feb 28, 2023
@VojtechBartoska VojtechBartoska modified the milestones: 3.0.0, 2.0.8 Feb 28, 2023
@SuGlider
Copy link
Collaborator Author

SuGlider commented Mar 1, 2023

Closing it. Not necessary any more.
Details in #7894
#7894 (comment)
#7894 (comment)

@SuGlider SuGlider closed this Mar 1, 2023
@SuGlider SuGlider deleted the Suglider-BLE-device-Name-Length branch April 3, 2023 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BLE Issues related to BLE Sprint Issue is in current sprint
Projects
Development

Successfully merging this pull request may close these issues.

BLE Name Length Too Long Cause Crash
2 participants