From b7f11c05e480c55fadb0b3f5e62afa81e692afb1 Mon Sep 17 00:00:00 2001 From: schneider <schneider@blinkenlichts.net> Date: Sat, 30 Jan 2021 18:42:19 +0100 Subject: [PATCH] feat(mp-ble): Allow to configure advertisements from MP --- epicardium/ble/ble_adv.c | 55 +++++++++++-------------- epicardium/ble/ble_api.h | 1 + epicardium/ble/epic_ble_api.c | 43 +++++++++++++++++++ epicardium/epicardium.h | 3 ++ pycardium/modules/modbluetooth_card10.c | 11 ++++- 5 files changed, 79 insertions(+), 34 deletions(-) diff --git a/epicardium/ble/ble_adv.c b/epicardium/ble/ble_adv.c index bdc5d39ef..51ff30918 100644 --- a/epicardium/ble/ble_adv.c +++ b/epicardium/ble/ble_adv.c @@ -202,51 +202,42 @@ void ble_adv_stop(void) } } +void ble_adv_start(uint8_t mode) +{ + if (advertising_mode != APP_MODE_NONE) { + /* We need to stop advertising in between or the + * adv set will not be changed. + * Also need to wait for the stop operation to finish + * before we can start again + * Also need to set the variables first as we don't + * have a lock on the stack.*/ + advertising_mode_target = mode; + advertising_mode = APP_MODE_NONE; + AppAdvStop(); + } else { + advertising_mode = mode; + advertising_mode_target = mode; + AppAdvStart(advertising_mode); + } +} + void ble_adv_discoverable(bool discoverable) { if (discoverable) { if (advertising_mode != APP_MODE_DISCOVERABLE) { LOG_INFO("ble", "Making bondable and discoverable"); - if (advertising_mode != APP_MODE_NONE) { - /* We need to stop advertising in between or the - * adv set will not be changed. - * Also need to wait for the stop operation to finish - * before we can start again - * Also need to set the variables first as we don't - * have a lock on the stack.*/ - advertising_mode_target = APP_MODE_DISCOVERABLE; - advertising_mode = APP_MODE_NONE; - AppAdvStop(); - } else { - advertising_mode = APP_MODE_DISCOVERABLE; - advertising_mode_target = APP_MODE_DISCOVERABLE; - AppAdvStart(advertising_mode); - } + ble_adv_start(APP_MODE_DISCOVERABLE); } } else { /* TODO: This does way more than the function name indicates */ if (AppDbCheckBonded()) { if (advertising_mode != APP_MODE_CONNECTABLE) { LOG_INFO("ble", "Bonded. Making connectable"); - if (advertising_mode != APP_MODE_NONE) { - advertising_mode_target = - APP_MODE_CONNECTABLE; - advertising_mode = APP_MODE_NONE; - AppAdvStop(); - } else { - advertising_mode = APP_MODE_CONNECTABLE; - advertising_mode_target = - APP_MODE_CONNECTABLE; - AppAdvStart(advertising_mode); - } + ble_adv_start(APP_MODE_CONNECTABLE); } } else { - if (advertising_mode != APP_MODE_NONE) { - LOG_INFO("ble", "Not bonded. Stop advertising"); - advertising_mode = APP_MODE_NONE; - advertising_mode_target = APP_MODE_NONE; - AppAdvStop(); - } + LOG_INFO("ble", "Not bonded. Stop advertising"); + ble_adv_stop(); } } } diff --git a/epicardium/ble/ble_api.h b/epicardium/ble/ble_api.h index 24ca3c162..18f86baa4 100644 --- a/epicardium/ble/ble_api.h +++ b/epicardium/ble/ble_api.h @@ -52,5 +52,6 @@ void ble_epic_dm_api_event(dmEvt_t *dm_event); void ble_adv_init(void); void ble_adv_setup(void); void ble_adv_stop(void); +void ble_adv_start(uint8_t mode); void ble_adv_discoverable(bool discoverable); void ble_adv_proc_msg(bleMsg_t *pMsg); diff --git a/epicardium/ble/epic_ble_api.c b/epicardium/ble/epic_ble_api.c index 726d26a94..623ff567b 100644 --- a/epicardium/ble/epic_ble_api.c +++ b/epicardium/ble/epic_ble_api.c @@ -13,6 +13,7 @@ #include <stdint.h> #include <string.h> +#include <stdio.h> #define BLE_EVENT_QUEUE_SIZE 10 @@ -21,6 +22,9 @@ static uint8_t ble_event_queue_buffer [sizeof(struct epic_ble_event) * BLE_EVENT_QUEUE_SIZE]; static StaticQueue_t ble_event_queue_data; +static uint8_t adv_data_buf[HCI_ADV_DATA_LEN]; +static uint8_t sr_data_buf[HCI_ADV_DATA_LEN]; + int epic_ble_free_event(struct epic_ble_event *e) { if (e->data) { @@ -113,3 +117,42 @@ int epic_ble_get_device_name(uint8_t **buf, uint16_t *len) uint8_t ret = AttsGetAttr(GAP_DN_HDL, len, buf); return ret; } + +int epic_ble_advertise( + int interval_us, + const uint8_t *adv_data, + size_t adv_data_len, + const uint8_t *sr_data, + size_t sr_data_len, + bool connectable +) { + if (adv_data_len > sizeof(adv_data_buf)) { + adv_data_len = sizeof(adv_data_buf); + } + + if (sr_data_len > sizeof(sr_data_buf)) { + sr_data_len = sizeof(sr_data_buf); + } + + memcpy(adv_data_buf, adv_data, adv_data_len); + memcpy(sr_data_buf, sr_data, sr_data_len); + + if (connectable) { + AppAdvSetData( + APP_ADV_DATA_CONNECTABLE, adv_data_len, adv_data_buf + ); + AppAdvSetData( + APP_SCAN_DATA_CONNECTABLE, sr_data_len, sr_data_buf + ); + ble_adv_start(APP_MODE_CONNECTABLE); + } else { + AppAdvSetData( + APP_ADV_DATA_DISCOVERABLE, adv_data_len, adv_data_buf + ); + AppAdvSetData( + APP_SCAN_DATA_DISCOVERABLE, sr_data_len, sr_data_buf + ); + ble_adv_start(APP_MODE_DISCOVERABLE); + } + return 0; +} diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 3b93ea5bd..62d7d01e6 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -180,6 +180,7 @@ typedef _Bool bool; #define API_BLE_SET_DEVICE_NAME 0x182 #define API_BLE_GET_DEVICE_NAME 0x183 #define API_BLE_GET_ADDRESS 0x184 +#define API_BLE_ADVERTISE 0x185 /* clang-format on */ @@ -2661,5 +2662,7 @@ API(API_BLE_SET_DEVICE_NAME, int epic_ble_set_device_name(const uint8_t *buf, ui API(API_BLE_GET_DEVICE_NAME, int epic_ble_get_device_name(uint8_t **buf, uint16_t *len)); API(API_BLE_GET_ADDRESS, void epic_ble_get_address(uint8_t *addr)); +API(API_BLE_ADVERTISE, int epic_ble_advertise(int interval_us, const uint8_t *adv_data, size_t adv_data_len, const uint8_t *sr_data, size_t sr_data_len, bool connectable)); + #endif /* _EPICARDIUM_H */ diff --git a/pycardium/modules/modbluetooth_card10.c b/pycardium/modules/modbluetooth_card10.c index a13d3f793..3c3bbdbc8 100644 --- a/pycardium/modules/modbluetooth_card10.c +++ b/pycardium/modules/modbluetooth_card10.c @@ -220,12 +220,19 @@ int mp_bluetooth_gap_advertise_start( size_t sr_data_len ) { // Dropping any current connection starts advertising on the card10 - // TODO: modify the advertising data int connection = epic_ble_is_connection_open(); if (connection > 0) { epic_ble_close_connection(connection); } - return 0; + + return epic_ble_advertise( + interval_us, + adv_data, + adv_data_len, + sr_data, + sr_data_len, + connectable + ); } // Stop advertisement. No-op when already stopped. -- GitLab