diff --git a/epicardium/ble/ble_adv.c b/epicardium/ble/ble_adv.c index bdc5d39efcbb50701839335528fb5364c03128ad..51ff30918fd5e6b2c1792d19e8bd04060a661b52 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 24ca3c162477c3dd965634935424a47b63a0c5fe..18f86baa4eeac740f9aeb0c88d7ee1fa0d7d8ec7 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 726d26a949ece4dbefbebc4d7e9f0b8b8c9e818e..623ff567b500c4f4c6087a2fa4e91a48a187db9b 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 3b93ea5bd1cdf3718867bc52986e34728f5e38c2..62d7d01e6cb1d4acaa0e6eb5261e81e4f948d7c3 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 a13d3f7934cc10080b041995807cf3d04a3264ac..3c3bbdbc833cac533b1df52c477fb7034a756b5f 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.