From 93234db7e405ff2c4215d8a80f2807666bcd27fc Mon Sep 17 00:00:00 2001 From: schneider <schneider@blinkenlichts.net> Date: Mon, 5 Oct 2020 22:40:41 +0200 Subject: [PATCH] fix(bondings): Bring API more in line with upstream --- epicardium/ble/ble_main.c | 4 ++ epicardium/ble/bondings.c | 58 ++++++++++++++++--- .../stack/ble-profiles/include/app/app_db.h | 22 +++++++ 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/epicardium/ble/ble_main.c b/epicardium/ble/ble_main.c index a5522b4e7..74ae62a60 100644 --- a/epicardium/ble/ble_main.c +++ b/epicardium/ble/ble_main.c @@ -394,6 +394,7 @@ static void bleCccCback(attsCccEvt_t *pEvt) { /* store value in device database */ AppDbSetCccTblValue(dbHdl, pEvt->idx, pEvt->value); + AppDbNvmStoreCccTbl(dbHdl); } if ((pMsg = WsfMsgAlloc(sizeof(attsCccEvt_t))) != NULL) @@ -761,6 +762,9 @@ static void bleProcMsg(bleMsg_t *pMsg) case DM_SEC_PAIR_CMPL_IND: LOG_INFO("ble", "Secure pairing successful, auth: 0x%02X", pMsg->dm.pairCmpl.auth); + + AppDbNvmStoreBond(AppDbGetHdl((dmConnId_t) pMsg->hdr.param)); + pair_connId = DM_CONN_ID_NONE; trigger_event(BLE_EVENT_PAIRING_COMPLETE); /* After a successful pairing, bonding is disabled again. diff --git a/epicardium/ble/bondings.c b/epicardium/ble/bondings.c index 3f43ebde4..42a06f133 100644 --- a/epicardium/ble/bondings.c +++ b/epicardium/ble/bondings.c @@ -182,6 +182,10 @@ static int read_tlv(int fd, uint32_t t, uint32_t l, void *v) static int write_bond_to_file(appDbRec_t *r, char *filename) { + if (!r->inUse) { + return -EINVAL; + } + int fd = epic_file_open(filename, "w"); int ret; if (fd < 0) { @@ -436,11 +440,6 @@ void AppDbValidateRecord(appDbHdl_t hdl, uint8_t keyMask) { ((appDbRec_t *) hdl)->valid = TRUE; ((appDbRec_t *) hdl)->keyValidMask = keyMask; - char filename[32]; - record_to_filename((appDbRec_t *) hdl, filename, sizeof(filename)); - /* Directory might exist already. Call will fail silently in that case. */ - epic_file_mkdir("pairings"); - write_bond_to_file((appDbRec_t *) hdl, filename); } /*************************************************************************************************/ @@ -708,10 +707,6 @@ void AppDbSetCccTblValue(appDbHdl_t hdl, uint16_t idx, uint16_t value) WSF_ASSERT(idx < APP_DB_NUM_CCCD); ((appDbRec_t *) hdl)->cccTbl[idx] = value; - - char filename[32]; - record_to_filename((appDbRec_t *) hdl, filename, sizeof(filename)); - write_bond_to_file((appDbRec_t *) hdl, filename); } /*************************************************************************************************/ @@ -889,4 +884,49 @@ void AppDbSetPeerRpao(appDbHdl_t hdl, bool_t peerRpao) { ((appDbRec_t *)hdl)->peerRpao = peerRpao; } + +/*************************************************************************************************/ +/*! + * \brief Store the client characteristic configuration table for a device record in NVM. + * + * \param hdl Database record handle. + * + * \return None. + */ +/*************************************************************************************************/ +void AppDbNvmStoreCccTbl(appDbHdl_t hdl) +{ + /* We take a short cut and simply write the whole file again. */ + AppDbNvmStoreBond(hdl); +} + +/*************************************************************************************************/ +/*! + * \brief Store bonding information for device record in NVM. + * + * \param hdl Database record handle. + * + * \return None. + */ +/*************************************************************************************************/ +void AppDbNvmStoreBond(appDbHdl_t hdl) +{ + appDbRec_t *pRec = (appDbRec_t *) hdl; + + if (pRec->inUse && pRec->valid) { + char filename[32]; + record_to_filename(pRec, filename, sizeof(filename)); + /* Directory might exist already. Call will fail silently in that case. */ + epic_file_mkdir("pairings"); + int ret = write_bond_to_file(pRec, filename); + if(ret < 0) { + LOG_WARN( + "bondings", + "Writing pairing '%s' failed: %d", + filename, + ret + ); + } + } +} /* clang-format on */ 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 e72fb4cf8..a34b6d3bb 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 @@ -395,6 +395,28 @@ bool_t AppDbGetPeerRpao(appDbHdl_t hdl); /*************************************************************************************************/ void AppDbSetPeerRpao(appDbHdl_t hdl, bool_t peerRpao); +/*************************************************************************************************/ +/*! + * \brief Store the client characteristic configuration table for a device record in NVM. + * + * \param hdl Database record handle. + * + * \return None. + */ +/*************************************************************************************************/ +void AppDbNvmStoreCccTbl(appDbHdl_t hdl); + +/*************************************************************************************************/ +/*! + * \brief Store bonding information for device record in NVM. + * + * \param hdl Database record handle. + * + * \return None. + */ +/*************************************************************************************************/ +void AppDbNvmStoreBond(appDbHdl_t hdl); + /**@}*/ /*! \} */ /*! APP_FRAMEWORK_DB_API */ -- GitLab