Skip to content

Commit

Permalink
Merge pull request #2367 from fanoush/f-SD6.1-advertising
Browse files Browse the repository at this point in the history
for SD>5 use static buffers for advertising and scan response data
  • Loading branch information
gfwilliams authored May 26, 2023
2 parents c0bed8d + d29146f commit 25b7987
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 53 deletions.
1 change: 1 addition & 0 deletions libs/bluetooth/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ void jsble_restart_softdevice(JsVar *jsFunction);

uint32_t jsble_advertising_start();
uint32_t jsble_advertising_update_advdata(char *dPtr, unsigned int dLen);
uint32_t jsble_advertising_update_scanresponse(char *dPtr, unsigned int dLen);
void jsble_advertising_stop();

/** Is BLE connected to any device at all? */
Expand Down
25 changes: 1 addition & 24 deletions libs/bluetooth/jswrap_bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -1207,30 +1207,7 @@ void jswrap_ble_setScanResponse(JsVar *data) {
// only set data if we managed to decode it ok
jsvObjectSetOrRemoveChild(execInfo.hiddenRoot, BLE_NAME_SCAN_RESPONSE_DATA, data);

#ifdef NRF5X
#if NRF_SD_BLE_API_VERSION<5
err_code = sd_ble_gap_adv_data_set(NULL, 0, (uint8_t *)respPtr, respLen);
#else
extern uint8_t m_adv_handle;
// Get existing advertising data as on SDK15 we have to be able to re-supply this
// when changing the scan response
JsVar *advData = jswrap_ble_getCurrentAdvertisingData();
JSV_GET_AS_CHAR_ARRAY(advPtr, advLen, advData);

ble_gap_adv_data_t d;
d.adv_data.p_data = (uint8_t *)advPtr;
d.adv_data.len = advLen;
d.scan_rsp_data.p_data = (uint8_t *)respPtr;
d.scan_rsp_data.len = respLen;
if (bleStatus & BLE_IS_ADVERTISING) sd_ble_gap_adv_stop(m_adv_handle);
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &d, NULL);
if (bleStatus & BLE_IS_ADVERTISING) sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
jsvUnLock(advData);
#endif
#else
err_code = 0xDEAD;
jsiConsolePrintf("FIXME\n");
#endif
err_code=jsble_advertising_update_scanresponse((uint8_t *)respPtr, respLen);
jsble_check_error(err_code);
} else {
jsExceptionHere(JSET_TYPEERROR, "Expecting array-like object or undefined, got %t", data);
Expand Down
5 changes: 5 additions & 0 deletions targets/esp32/BLE/esp32_gap_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ JsVar *bluetooth_gap_getAdvertisingData(JsVar *data, JsVar *options){
return jsvNewArrayBufferWithData(i,encoded_advdata);
}

uint32_t jsble_advertising_update_scanresponse(char *dPtr, unsigned int dLen) {
jsiConsolePrintf("FIXME\n");
return 0xDEAD;
}

esp_err_t bluetooth_gap_setAdvertising(JsVar *advArray) {
if(!ESP32_Get_NVS_Status(ESP_NETWORK_BLE))
return 0; // ESP32.enableBLE(false) - we return 0 here so we don't output an error message at boot
Expand Down
1 change: 1 addition & 0 deletions targets/esp32/BLE/esp32_gap_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "esp_gap_ble_api.h"
#include "jsvar.h"
#include "bluetooth.h"

#define BLE_DEVICE_NAME "BLE_DEV_N"

Expand Down
95 changes: 66 additions & 29 deletions targets/nrf5x/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,60 @@ void jsble_setup_advdata(ble_advdata_t *advdata) {
advdata->flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
}

#if NRF_SD_BLE_API_VERSION>5
static ble_gap_adv_data_t m_ble_gap_adv_data;
// SoftDevice >= 6.1.0 needs this as static buffers
static uint8_t m_adv_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
static uint8_t m_scan_rsp_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; // 31
#endif

uint32_t jsble_advertising_update(uint8_t *advPtr, unsigned int advLen, uint8_t *rspPtr, unsigned int rspLen, ble_gap_adv_params_t *adv_params) {
#if NRF_SD_BLE_API_VERSION>5
if (bleStatus & BLE_IS_ADVERTISING){
// first we need to switch away from our static live advertising data buffers
// otherwise we would get NRF_ERROR_INVALID_STATE from sd_ble_gap_adv_set_configure
ble_gap_adv_data_t tmp;
uint8_t tmp_adv_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
uint8_t tmp_scan_rsp_data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];

tmp.adv_data.len = m_ble_gap_adv_data.adv_data.len;
if (tmp.adv_data.len){
memcpy(tmp_adv_data, m_adv_data, tmp.adv_data.len);
tmp.adv_data.p_data = tmp_adv_data;
} else tmp.adv_data.p_data = NULL;

tmp.scan_rsp_data.len = m_ble_gap_adv_data.scan_rsp_data.len;
if (tmp.scan_rsp_data.len){
memcpy(tmp_scan_rsp_data, m_scan_rsp_data, tmp.scan_rsp_data.len);
tmp.scan_rsp_data.p_data = tmp_scan_rsp_data;
} else tmp.scan_rsp_data.p_data = NULL;

sd_ble_gap_adv_set_configure(&m_adv_handle, &tmp, NULL);
}

// now we can modify data and switch back to it
if (advPtr){
if (advLen){
advLen=MIN(advLen,BLE_GAP_ADV_SET_DATA_SIZE_MAX);
memcpy(m_adv_data, advPtr, advLen);
}
m_ble_gap_adv_data.adv_data.p_data = m_adv_data;
m_ble_gap_adv_data.adv_data.len = advLen;
}
if (rspPtr){
if (rspLen){
rspLen=MIN(rspLen,BLE_GAP_ADV_SET_DATA_SIZE_MAX);
memcpy(m_scan_rsp_data, rspPtr, rspLen);
}
m_ble_gap_adv_data.scan_rsp_data.p_data = m_scan_rsp_data;
m_ble_gap_adv_data.scan_rsp_data.len = rspLen;
}
return sd_ble_gap_adv_set_configure(&m_adv_handle, &m_ble_gap_adv_data, adv_params);
#else
return sd_ble_gap_adv_data_set((uint8_t *)advPtr, advLen, (uint8_t *)rspPtr, rspLen);
#endif
}

uint32_t jsble_advertising_start() {
// try not to call from IRQ as we might want to allocate JsVars
if (bleStatus & BLE_IS_ADVERTISING) return 0;
Expand Down Expand Up @@ -2570,36 +2624,27 @@ uint32_t jsble_advertising_start() {
return err_code;
}


//jsiConsolePrintf("adv_data_set %d %d\n", advPtr, advLen);
#if NRF_SD_BLE_API_VERSION>5
ble_gap_adv_data_t d;
memset(&d, 0, sizeof(d));
d.adv_data.p_data = (uint8_t*)advPtr;
d.adv_data.len = advLen;
if (!non_scannable) {
d.scan_rsp_data.p_data = m_enc_scan_response_data;
d.scan_rsp_data.len = m_enc_scan_response_data_len;
}

err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &d, &adv_params);
memset(&m_ble_gap_adv_data, 0, sizeof(m_ble_gap_adv_data));
#endif
err_code = jsble_advertising_update(
(uint8_t*)advPtr, advLen,
non_scannable ? NULL : m_enc_scan_response_data, non_scannable ? 0 : m_enc_scan_response_data_len,
&adv_params
);
jsble_check_error(err_code);
#if NRF_SD_BLE_API_VERSION>5
if (!err_code) {
jsble_check_error(sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, m_adv_handle, m_tx_power));
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
jsble_check_error(err_code);
}
#elif NRF_SD_BLE_API_VERSION<5
err_code = sd_ble_gap_adv_data_set(
(uint8_t *)advPtr, advLen,
m_enc_scan_response_data, m_enc_scan_response_data_len);
if (!err_code)
err_code = sd_ble_gap_adv_start(&adv_params);
#else
// do we care about SDK14?
err_code = sd_ble_gap_adv_data_set(
(uint8_t *)advPtr, advLen,
m_enc_scan_response_data, m_enc_scan_response_data_len);
if (!err_code)
err_code = sd_ble_gap_adv_start(&adv_params, APP_BLE_CONN_CFG_TAG);
#endif
Expand All @@ -2610,18 +2655,10 @@ uint32_t jsble_advertising_start() {
}

uint32_t jsble_advertising_update_advdata(char *dPtr, unsigned int dLen) {
#if NRF_SD_BLE_API_VERSION>5
ble_gap_adv_data_t d;
d.adv_data.p_data = (uint8_t *)dPtr;
d.adv_data.len = dLen;
d.scan_rsp_data.p_data = NULL;
d.scan_rsp_data.len = 0;

// TODO: scan_rsp_data? Does not setting this remove it?
return sd_ble_gap_adv_set_configure(&m_adv_handle, &d, NULL);
#else
return sd_ble_gap_adv_data_set((uint8_t *)dPtr, dLen, NULL, 0);
#endif
return jsble_advertising_update((uint8_t *)dPtr, dLen, NULL, 0, NULL);
}
uint32_t jsble_advertising_update_scanresponse(char *dPtr, unsigned int dLen) {
return jsble_advertising_update(NULL, 0, (uint8_t*)dPtr, dLen, NULL);
}

void jsble_advertising_stop() {
Expand Down

0 comments on commit 25b7987

Please sign in to comment.