diff --git a/epicardium/ble/ble_main.c b/epicardium/ble/ble_main.c index dc3d54669c2619f1cf548a7310c1e379f2a8dd34..5c6f70da6dcc5ecbfc703e7b2c2b93b79b82f5f2 100644 --- a/epicardium/ble/ble_main.c +++ b/epicardium/ble/ble_main.c @@ -224,6 +224,7 @@ wsfHandlerId_t bleHandlerId; static dmConnId_t pair_connId = DM_CONN_ID_NONE; static uint32_t pair_confirm_value; +static appDbHdl_t last_pairing = NULL; static void BleHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg); @@ -578,6 +579,15 @@ uint32_t epic_ble_get_compare_value(void) return pair_confirm_value; } +int epic_ble_get_last_pairing_name(char *buf, size_t buf_size) +{ + if(last_pairing == NULL) { + return -ENOENT; + } + + return AppDbGetPairingName(last_pairing, buf, buf_size); +} + void epic_ble_compare_response(bool confirmed) { if(!active) { @@ -765,7 +775,8 @@ static void bleProcMsg(bleMsg_t *pMsg) pMsg->dm.pairCmpl.auth); DmSecGenerateEccKeyReq(); - AppDbNvmStoreBond(AppDbGetHdl((dmConnId_t) pMsg->hdr.param)); + last_pairing = AppDbGetHdl((dmConnId_t) pMsg->hdr.param); + AppDbNvmStoreBond(last_pairing); pair_connId = DM_CONN_ID_NONE; trigger_event(BLE_EVENT_PAIRING_COMPLETE); diff --git a/epicardium/ble/bondings.c b/epicardium/ble/bondings.c index 42a06f133841384477c273a7c58b17c2db98655e..425dc710acb0db120767e1a91f53b53ab0fa56e4 100644 --- a/epicardium/ble/bondings.c +++ b/epicardium/ble/bondings.c @@ -930,3 +930,9 @@ void AppDbNvmStoreBond(appDbHdl_t hdl) } } /* clang-format on */ + +int AppDbGetPairingName(appDbHdl_t hdl, char *buf, size_t buf_size) +{ + appDbRec_t *pRec = (appDbRec_t *)hdl; + return record_to_filename(pRec, buf, buf_size); +} diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index a90ad4b70b91caf486fdcee741ced98943c0958d..92e4ce82db8e826cdf09dd4d01be8d96868d2722 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -155,6 +155,7 @@ typedef _Bool bool; #define API_BLE_SET_MODE 0x142 #define API_BLE_GET_EVENT 0x143 #define API_BLE_GET_SCAN_REPORT 0x144 +#define API_BLE_GET_LAST_PAIRING_NAME 0x145 /* clang-format on */ @@ -2236,6 +2237,18 @@ API(API_BLE_GET_EVENT, enum ble_event_type epic_ble_get_event(void)); */ API(API_BLE_GET_COMPARE_VALUE, uint32_t epic_ble_get_compare_value(void)); +/** + * Retrieve the (file) name of the last pairing which was successful. + * + * :return: `0` on success or a negative value if an error occured. Possible + * errors: + * + * - ``-ENOENT``: There was no successful pairing yet. + * + * .. versionadded:: 1.16 + */ +API(API_BLE_GET_LAST_PAIRING_NAME, int epic_ble_get_last_pairing_name(char *buf, size_t buf_size)); + /** * Indicate wether the user confirmed the compare value. * diff --git a/lib/sdk/Libraries/BTLE/stack/ble-profiles/include/app/app_db.h b/lib/sdk/Libraries/BTLE/stack/ble-profiles/include/app/app_db.h index a34b6d3bbb1e20444d3892f34d4e20e915ddd4e4..3f9d4733ddbaf5975ecabdac958bbb01402357d7 100644 --- a/lib/sdk/Libraries/BTLE/stack/ble-profiles/include/app/app_db.h +++ b/lib/sdk/Libraries/BTLE/stack/ble-profiles/include/app/app_db.h @@ -21,6 +21,7 @@ #include "wsf_os.h" #include "dm_api.h" +#include <stddef.h> #ifdef __cplusplus extern "C" { @@ -417,6 +418,19 @@ void AppDbNvmStoreCccTbl(appDbHdl_t hdl); /*************************************************************************************************/ void AppDbNvmStoreBond(appDbHdl_t hdl); +/*************************************************************************************************/ +/*! + * \brief Get a human readable (file) name of a pairing. + * + * \param hdl Database record handle. + * \param buf Buffer to put the name into. + * \param buf_size Size of the buffer in bytes. + * + * \return Length of name. -1 if the buffer is too small. + */ +/*************************************************************************************************/ +int AppDbGetPairingName(appDbHdl_t hdl, char *buf, size_t buf_size); + /**@}*/ /*! \} */ /*! APP_FRAMEWORK_DB_API */ diff --git a/preload/apps/ble/__init__.py b/preload/apps/ble/__init__.py index e578b30c2f280735a723bde321725a5e9ae2a923..0287ea5e028bc20d54d6edca29ac5b5708acca18 100644 --- a/preload/apps/ble/__init__.py +++ b/preload/apps/ble/__init__.py @@ -137,9 +137,12 @@ while True: state = 6 elif ble_event == sys_ble.EVENT_PAIRING_COMPLETE: ble_event = None + pairing_name = sys_ble.get_last_pairing_name().split("/")[-1].split(".")[0] disp.clear() disp.print("BLE Pairing", posy=0, fg=[0, 0, 255]) - disp.print(" Success", posy=40, fg=[0, 255, 0]) + disp.print(" Success", posy=20, fg=[0, 255, 0]) + disp.print("Name:", posy=40, fg=[255, 255, 255]) + disp.print("%11s" % pairing_name, posy=60, fg=[255, 255, 255]) disp.update() time.sleep(5) os.exec("main.py") diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index d5039a5a90a28d144b1f05139de0001eae9758ce..da9ba34beaf3660ea7711e5e8eac95c7a5ef38ce 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -199,6 +199,7 @@ Q(get_string) Q(BLE) Q(ble) Q(get_compare_value) +Q(get_last_pairing_name) Q(get_scan_report) Q(confirm_compare_value) Q(set_bondable) diff --git a/pycardium/modules/sys_ble.c b/pycardium/modules/sys_ble.c index 3ea10d5f63cfb5298739b4d0952da095f7b3276f..760cae1863cc215c466e6df9321255a4b903f6e5 100644 --- a/pycardium/modules/sys_ble.c +++ b/pycardium/modules/sys_ble.c @@ -5,6 +5,7 @@ #include "py/runtime.h" #include <stdint.h> +#include <string.h> static mp_obj_t mp_ble_confirm_compare_value(mp_obj_t confirmed_obj) { @@ -24,6 +25,23 @@ static MP_DEFINE_CONST_FUN_OBJ_0( ble_get_compare_value_obj, mp_ble_get_compare_value ); +static mp_obj_t mp_ble_get_last_pairing_name(void) +{ + char pairing_str[32]; + int status = epic_ble_get_last_pairing_name( + pairing_str, sizeof(pairing_str) + ); + if (status < 0) { + mp_raise_OSError(-status); + } + + mp_obj_t ret = mp_obj_new_str(pairing_str, strlen(pairing_str)); + return ret; +} +static MP_DEFINE_CONST_FUN_OBJ_0( + ble_get_last_pairing_name_obj, mp_ble_get_last_pairing_name +); + static mp_obj_t mp_ble_get_scan_report(void) { struct epic_scan_report scan_report; @@ -74,6 +92,8 @@ static const mp_rom_map_elem_t ble_module_globals_table[] = { 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_get_last_pairing_name), + MP_ROM_PTR(&ble_get_last_pairing_name_obj) }, { MP_ROM_QSTR(MP_QSTR_get_scan_report), MP_ROM_PTR(&ble_get_scan_report_obj) }, { MP_ROM_QSTR(MP_QSTR_get_event), MP_ROM_PTR(&ble_get_event_obj) },