diff --git a/epicardium/ble/ble_main.c b/epicardium/ble/ble_main.c index 72fd0569603130ad9b72f5e1dbcb622e0e80d81f..1ea302fe5aa4e4cdd5876765a1eedea4b6e20d13 100644 --- a/epicardium/ble/ble_main.c +++ b/epicardium/ble/ble_main.c @@ -43,6 +43,7 @@ #include "api/interrupt-sender.h" #include "modules/log.h" +static bool active; /************************************************************************************************** Macros **************************************************************************************************/ @@ -360,6 +361,30 @@ static void bleSetup(bleMsg_t *pMsg) } else { AppAdvStart(APP_MODE_CONNECTABLE); } + active = true; +} + +void epic_ble_set_bondable(bool bondable) +{ + if(!active) { + return; + } + + if(bondable) { + LOG_INFO("ble", "Making bondable and discoverable"); + /* We need to stop advertising in between or the + * adv set will not be changed... */ + AppAdvStop(); + AppSetBondable(TRUE); + AppAdvStart(APP_MODE_DISCOVERABLE); + } else { + LOG_INFO("ble", "Making connectable"); + AppAdvStop(); + AppSetBondable(FALSE); + if(AppDbCheckBonded()) { + AppAdvStart(APP_MODE_CONNECTABLE); + } + } } uint32_t epic_ble_get_compare_value(void) diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 790d84eeaa70ae7b4c1a3653970e4b064872ea86..1690706b70f8892b2629d2e92434ddf0cae81427 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -150,6 +150,7 @@ typedef _Bool bool; #define API_BLE_GET_COMPARE_VALUE 0x140 #define API_BLE_CONFIRM_COMPARE_VALUE 0x141 +#define API_BLE_SET_BONDABLE 0x142 /* clang-format on */ @@ -2092,5 +2093,6 @@ API(API_CONFIG_SET_STRING, int epic_config_set_string(const char *key, const cha API(API_BLE_GET_COMPARE_VALUE, uint32_t epic_ble_get_compare_value(void)); API(API_BLE_CONFIRM_COMPARE_VALUE, void epic_ble_confirm_compare_value(void)); API_ISR(EPIC_INT_BLE_NUMERIC_COMPARISON, epic_isr_ble_numeric_comparison); +API(API_BLE_SET_BONDABLE, void epic_ble_set_bondable(bool bondable)); #endif /* _EPICARDIUM_H */ diff --git a/epicardium/modules/hardware.c b/epicardium/modules/hardware.c index 6d118fe73ef59007c4e3cb915afdba004e678d56..1168f5a756d81ee3e3d16a15e5e4c703314b2b7a 100644 --- a/epicardium/modules/hardware.c +++ b/epicardium/modules/hardware.c @@ -289,5 +289,10 @@ int hardware_reset(void) epic_max86150_disable_sensor(); + /* + * BLE + */ + epic_ble_set_bondable(false); + return 0; } diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index 589dcafd0e3da1c41a12cfbf84d2383afa6b5183..1ae1ac84b2a64de9bfae34252113d0c7c54ec1fe 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -196,4 +196,5 @@ Q(get_string) Q(ble) Q(get_compare_value) Q(confirm_compare_value) +Q(set_bondable) diff --git a/pycardium/modules/sys_ble.c b/pycardium/modules/sys_ble.c index 856db75672d4a99ef3d004e1109668aa1c9c8ad0..c6fbf47f92dab1240251b320b49b81ebf7f32f7c 100644 --- a/pycardium/modules/sys_ble.c +++ b/pycardium/modules/sys_ble.c @@ -19,10 +19,19 @@ static mp_obj_t mp_ble_get_compare_value(void) } static MP_DEFINE_CONST_FUN_OBJ_0(ble_get_compare_value_obj, mp_ble_get_compare_value); +static mp_obj_t mp_ble_set_bondable(mp_obj_t bondable_obj) +{ + bool bondable = mp_obj_is_true(bondable_obj); + epic_ble_set_bondable(bondable); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(ble_set_bondable_obj, mp_ble_set_bondable); + static const mp_rom_map_elem_t ble_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys_ble) }, { MP_ROM_QSTR(MP_QSTR_confirm_compare_value), MP_ROM_PTR(&ble_confirm_compare_value_obj) }, { MP_ROM_QSTR(MP_QSTR_get_compare_value), MP_ROM_PTR(&ble_get_compare_value_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_bondable), MP_ROM_PTR(&ble_set_bondable_obj) }, }; static MP_DEFINE_CONST_DICT(ble_module_globals, ble_module_globals_table);