Commit 3a8c85fc authored by schneider's avatar schneider
Browse files

feat(ble): Always overwrite the oldest pairing

parent 368343cd
...@@ -70,6 +70,8 @@ ...@@ -70,6 +70,8 @@
#define APP_DB_NVM_HDL_LIST_ID 19 #define APP_DB_NVM_HDL_LIST_ID 19
#define APP_DB_NVM_DISC_STATUS_ID 20 #define APP_DB_NVM_DISC_STATUS_ID 20
#define APP_DB_NVM_SEQUENCE_NUMBER_ID 100
/************************************************************************************************** /**************************************************************************************************
Data Types Data Types
**************************************************************************************************/ **************************************************************************************************/
...@@ -104,6 +106,8 @@ typedef struct ...@@ -104,6 +106,8 @@ typedef struct
/*! for ATT client */ /*! for ATT client */
uint16_t hdlList[APP_DB_HDL_LIST_LEN]; /*! Cached handle list */ uint16_t hdlList[APP_DB_HDL_LIST_LEN]; /*! Cached handle list */
uint8_t discStatus; /*! Service discovery and configuration status */ uint8_t discStatus; /*! Service discovery and configuration status */
uint32_t sequenceNumber;
} appDbRec_t; } appDbRec_t;
...@@ -114,9 +118,6 @@ typedef struct ...@@ -114,9 +118,6 @@ typedef struct
/*! Database */ /*! Database */
static appDbRec_t records[APP_DB_NUM_RECS]; static appDbRec_t records[APP_DB_NUM_RECS];
/*! When all records are allocated use this index to determine which to overwrite */
static appDbRec_t *pAppDbNewRec = records;
/* clang-format on */ /* clang-format on */
/* Translate a pointer to a record into the filename to be used for it. */ /* Translate a pointer to a record into the filename to be used for it. */
static int record_to_filename(appDbRec_t *record, char *buf, size_t buf_size) static int record_to_filename(appDbRec_t *record, char *buf, size_t buf_size)
...@@ -129,6 +130,28 @@ static int record_to_filename(appDbRec_t *record, char *buf, size_t buf_size) ...@@ -129,6 +130,28 @@ static int record_to_filename(appDbRec_t *record, char *buf, size_t buf_size)
return ret; return ret;
} }
static appDbRec_t *record_with_highest_seq_number()
{
appDbRec_t *r = &records[0];
for (int i = 0; i < APP_DB_NUM_RECS; i++) {
if (records[i].sequenceNumber > r->sequenceNumber) {
r = &records[i];
}
}
return r;
}
static appDbRec_t *record_with_lowest_seq_number()
{
appDbRec_t *r = &records[0];
for (int i = 0; i < APP_DB_NUM_RECS; i++) {
if (records[i].sequenceNumber < r->sequenceNumber) {
r = &records[i];
}
}
return r;
}
/* Write a TLV to a file. */ /* Write a TLV to a file. */
static int write_tlv(int fd, uint32_t t, uint32_t l, void *v) static int write_tlv(int fd, uint32_t t, uint32_t l, void *v)
{ {
...@@ -218,6 +241,7 @@ static int write_bond_to_file(appDbRec_t *r, char *filename) ...@@ -218,6 +241,7 @@ static int write_bond_to_file(appDbRec_t *r, char *filename)
write_element(APP_DB_NVM_PEER_SIGN_CTR_ID, peerSignCounter); write_element(APP_DB_NVM_PEER_SIGN_CTR_ID, peerSignCounter);
write_element(APP_DB_NVM_HDL_LIST_ID, hdlList); write_element(APP_DB_NVM_HDL_LIST_ID, hdlList);
write_element(APP_DB_NVM_DISC_STATUS_ID, discStatus); write_element(APP_DB_NVM_DISC_STATUS_ID, discStatus);
write_element(APP_DB_NVM_SEQUENCE_NUMBER_ID, sequenceNumber);
write_element(APP_DB_NVM_VALID_ID, valid); write_element(APP_DB_NVM_VALID_ID, valid);
#undef write_element #undef write_element
...@@ -265,6 +289,7 @@ static int read_bond_from_file(appDbRec_t *r, char *filename) ...@@ -265,6 +289,7 @@ static int read_bond_from_file(appDbRec_t *r, char *filename)
read_element(APP_DB_NVM_PEER_SIGN_CTR_ID, peerSignCounter); read_element(APP_DB_NVM_PEER_SIGN_CTR_ID, peerSignCounter);
read_element(APP_DB_NVM_HDL_LIST_ID, hdlList); read_element(APP_DB_NVM_HDL_LIST_ID, hdlList);
read_element(APP_DB_NVM_DISC_STATUS_ID, discStatus); read_element(APP_DB_NVM_DISC_STATUS_ID, discStatus);
read_element(APP_DB_NVM_SEQUENCE_NUMBER_ID, sequenceNumber);
read_element(APP_DB_NVM_VALID_ID, valid); read_element(APP_DB_NVM_VALID_ID, valid);
#undef read_element #undef read_element
...@@ -337,15 +362,8 @@ appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr) ...@@ -337,15 +362,8 @@ appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr)
/* if all records were allocated */ /* if all records were allocated */
if (i == 0) if (i == 0)
{ {
/* overwrite a record */ /* overwrite the oldest record */
pRec = pAppDbNewRec; pRec = record_with_lowest_seq_number();
/* get next record to overwrite */
pAppDbNewRec++;
if (pAppDbNewRec == &records[APP_DB_NUM_RECS])
{
pAppDbNewRec = records;
}
} }
/* initialize record */ /* initialize record */
...@@ -355,6 +373,7 @@ appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr) ...@@ -355,6 +373,7 @@ appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr)
BdaCpy(pRec->peerAddr, pAddr); BdaCpy(pRec->peerAddr, pAddr);
pRec->peerAddedToRl = FALSE; pRec->peerAddedToRl = FALSE;
pRec->peerRpao = FALSE; pRec->peerRpao = FALSE;
pRec->sequenceNumber = record_with_highest_seq_number()->sequenceNumber + 1;
return (appDbHdl_t) pRec; return (appDbHdl_t) pRec;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment