Skip to content

[ServerDev] Some feedback #1

Closed
Closed
@beegee-tokyo

Description

@beegee-tokyo

Just pulled the latest from ServerDev branch and try to compile simple BLE-Uart code.
Using PlatformIO.

Compilation error due to missing #defines

Compilation failes with

C:\users\beegee\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\bt/esp_bt.h:120:24: error:
'CONFIG_BTDM_CONTROLLER_BR_EDR_SCO_DATA_PATH_EFF' undeclared (first use in this function)
     .bt_sco_datapath = CONFIG_BTDM_CONTROLLER_BR_EDR_SCO_DATA_PATH_EFF,    \
                        ^
lib\NimBLE-Arduino\src\esp-hci\src\esp_nimble_hci.c:453:41: note: in expansion of macro 'BT_CONTROLLER_INIT_CONFIG_DEFAULT'
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
                                         ^
C:\users\beegee\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\bt/esp_bt.h:120:24: note: each undeclared identifier is reported only once for each function it appears in
     .bt_sco_datapath = CONFIG_BTDM_CONTROLLER_BR_EDR_SCO_DATA_PATH_EFF,    \
                        ^
lib\NimBLE-Arduino\src\esp-hci\src\esp_nimble_hci.c:453:41: note: in expansion of macro 'BT_CONTROLLER_INIT_CONFIG_DEFAULT'
     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();

sdkconfig.h in NimBLE library is missing some defines that are set in ESP32-Arduino's :
Solution:
in NimBLE-Arduino->src->sdkconfig.h add

#define CONFIG_BTDM_CONTROLLER_BR_EDR_SCO_DATA_PATH_EFF 1
#define CONFIG_ARDUINO_EVENT_RUNNING_CORE 1

SW crash when starting advertise service

After that code compiles but then crashes with

I NimBLEDevice: "BLE Host Task Started"
GAP procedure initiated: stop advertising.
I NimBLEDevice: "NimBle host synced."
E NimBLEAdvertising: "error setting advertisement data; rc=4, The provided buffer is too small."
abort() was called at PC 0x400d7b64 on core 1

Backtrace: 0x400916a8:0x3ffc7290 0x400918d9:0x3ffc72b0 0x400d7b64:0x3ffc72d0 0x400d24a2:0x3ffc7310 0x400d4a50:0x3ffc7360 0x400ec08f:0x3ffc73a0 0x4008dd2d:0x3ffc73c0

Rebooting...
ets Jun  8 2016 00:22:57

Backtrace decoded

Decoding stack results
0x400916a8: invoke_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 155
0x400918d9: abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 170
0x400d7b64: NimBLEAdvertising::start() at lib\NimBLE-Arduino\src\NimBLEAdvertising.cpp line 269
0x400d24a2: initBLE() at src\BLE\ble_esp32.cpp line 174
0x400d4a50: setup() at src\main.cpp line 107
0x400ec08f: loopTask(void*) at C:\users\beegee\.platformio\packages\framework-arduinoespressif32\cores\esp32\main.cpp line 14
0x4008dd2d: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

Adverting part of my code:

#include "main.h"
#include <NimBLEUtils.h>
#include <NimBLEServer.h>
#include <NimBLEDevice.h>
#include <NimBLEAdvertising.h>

/** Service UUID for Uart */
#define UART_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
/** Characteristic UUID for receiver */
#define RX_UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
/** Characteristic UUID for transmitter */
#define TX_UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

/** Characteristic for BLE-UART TX */
BLECharacteristic *pCharacteristicUartTX;
/** Characteristic for BLE-UART RX */
BLECharacteristic *pCharacteristicUartRX;
/** BLE Advertiser */
BLEAdvertising *pAdvertising;
/** BLE Service for Uart*/
BLEService *uartService;
/** BLE Server */
BLEServer *pServer;

/** Flag if a device is connected */
bool bleUARTisConnected = false;

/** Flag if client notification is enabled */
bool bleUARTnotifyEnabled = false;

/** RX and TX buffer size */
#define BUFF_SIZE 253

/** Buffer for outgoing data */
uint8_t txData[BUFF_SIZE] = {0};
/** Buffer for incoming data */
uint8_t rxData[BUFF_SIZE] = {0};
/** Number of bytes in the of incoming data buffer */
size_t rxLen = 0;

/** Mutex used to enter critical code part (RX data buffer access) */
SemaphoreHandle_t _mutex;

/**
 * Callbacks for client connection and disconnection
 */
class MyServerCallbacks : public BLEServerCallbacks
{
	void onConnect(BLEServer *pServer)
	{
		myLog_d("BLE client connected");
		pServer->updatePeerMTU(pServer->getConnId(), 260);
		bleUARTisConnected = true;
	};

	void onDisconnect(BLEServer *pServer)
	{
		myLog_d("BLE client disconnected");
		bleUARTisConnected = false;
		bleUARTnotifyEnabled = false;
		pAdvertising->start();
	}
};

/**
 * Callbacks for BLE client read/write requests
 * on BLE UART characteristic
 */
class UartTxCbHandler : public BLECharacteristicCallbacks
{
	void onWrite(BLECharacteristic *pCharacteristic)
	{
		std::string rxValue = pCharacteristic->getValue();
		rxLen = rxValue.size();

		xSemaphoreTake(_mutex, portMAX_DELAY);
		// if ((rxLen > 0) && (dataRcvd == false))
		if (rxLen > 0)
		{
			strncpy((char *)rxData, rxValue.c_str(), 253);

			myLog_d("UART write callback received %s", (char *)rxData);
		}
		xSemaphoreGive(_mutex);
	};
};

/**
 * Initialize nRF52 BLE UART
 */
void initBLE(void)
{
	char apName[] = "EC-xxxxxxxxxxxx";
	// Using ESP32 MAC (48 bytes only, so upper 2 bytes will be 0)
	sprintf(apName, "EC-%08X", deviceID);
	myLog_d("Device name: %s", apName);

	// Create mutex for access to RX data buffer
	_mutex = xSemaphoreCreateMutex();
	xSemaphoreGive(_mutex);

	// Initialize BLE and set output power
	BLEDevice::init(apName);
	BLEDevice::setMTU(260);
	BLEDevice::setPower(ESP_PWR_LVL_P7);

	myLog_d("BLE address: %s", BLEDevice::getAddress().toString().c_str());

	// Create BLE Server
	pServer = BLEDevice::createServer();

	// Set server callbacks
	pServer->setCallbacks(new MyServerCallbacks());

	// Create the UART BLE Service
	uartService = pServer->createService(UART_UUID);

	// Create a BLE Characteristic
	pCharacteristicUartTX = uartService->createCharacteristic(
		TX_UUID,
		BLECharacteristic::PROPERTY_NOTIFY |
			BLECharacteristic::PROPERTY_READ);

	// Register callback for notification enabled
	pCharacteristicUartTX->setNotifyProperty(true);

	pCharacteristicUartRX = uartService->createCharacteristic(
		RX_UUID,
		BLECharacteristic::PROPERTY_WRITE);

	pCharacteristicUartRX->setCallbacks(new UartTxCbHandler());

	// Start the service
	uartService->start();

	// Start advertising
	pAdvertising = pServer->getAdvertising();
	pAdvertising->addServiceUUID(UART_UUID);
	pAdvertising->start();
}

Maybe I am missing something in the setup of the advertising service???

Missing feature

Maybe I did miss it, but I used to set a callback on the characteristic descriptor to get a callback if the client enables notifications.
What I used was

...
#include <BLE2902.h>
#include <esp_bt_device.h>
...
/** Descriptor for the BLE-UART TX characteristic */
BLEDescriptor *txDescriptor;
 ...
/**
 * Callbacks for BLE client descriptor changes
 * on BLE UART characteristic
 */
class DescriptorCallbacks : public BLEDescriptorCallbacks
{
	void onWrite(BLEDescriptor *pDescriptor)
	{
		uint8_t *descrValue;
		descrValue = pDescriptor->getValue();
		if (descrValue[0] & (1 << 0))
		{
			bleUARTnotifyEnabled = true;
		}
		else
		{
			bleUARTnotifyEnabled = false;
		}
	};
};
...
void initBLE(void)
{
...
	// Create a BLE Characteristic
	pCharacteristicUartTX = uartService->createCharacteristic(
		TX_UUID,
		BLECharacteristic::PROPERTY_NOTIFY |
			BLECharacteristic::PROPERTY_READ);

	pCharacteristicUartTX->addDescriptor(new BLE2902());
	txDescriptor = pCharacteristicUartTX->getDescriptorByUUID("2902");
	if (txDescriptor != NULL)
	{
		txDescriptor->setCallbacks(new DescriptorCallbacks());
	}
...
}

Not sure how to do this with NimBLE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions