Commit f2d4062f authored by Rahix's avatar Rahix
Browse files

Merge 'Always overwrite the oldest pairing'

See merge request card10/firmware!405
parents 368343cd 3a8c85fc
......@@ -70,6 +70,8 @@
#define APP_DB_NVM_HDL_LIST_ID 19
#define APP_DB_NVM_DISC_STATUS_ID 20
#define APP_DB_NVM_SEQUENCE_NUMBER_ID 100
/**************************************************************************************************
Data Types
**************************************************************************************************/
......@@ -104,6 +106,8 @@ typedef struct
/*! for ATT client */
uint16_t hdlList[APP_DB_HDL_LIST_LEN]; /*! Cached handle list */
uint8_t discStatus; /*! Service discovery and configuration status */
uint32_t sequenceNumber;
} appDbRec_t;
......@@ -114,9 +118,6 @@ typedef struct
/*! Database */
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 */
/* 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)
......@@ -129,6 +130,28 @@ static int record_to_filename(appDbRec_t *record, char *buf, size_t buf_size)
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. */
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)
write_element(APP_DB_NVM_PEER_SIGN_CTR_ID, peerSignCounter);
write_element(APP_DB_NVM_HDL_LIST_ID, hdlList);
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);
#undef write_element
......@@ -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_HDL_LIST_ID, hdlList);
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);
#undef read_element
......@@ -337,15 +362,8 @@ appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr)
/* if all records were allocated */
if (i == 0)
{
/* overwrite a record */
pRec = pAppDbNewRec;
/* get next record to overwrite */
pAppDbNewRec++;
if (pAppDbNewRec == &records[APP_DB_NUM_RECS])
{
pAppDbNewRec = records;
}
/* overwrite the oldest record */
pRec = record_with_lowest_seq_number();
}
/* initialize record */
......@@ -355,6 +373,7 @@ appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr)
BdaCpy(pRec->peerAddr, pAddr);
pRec->peerAddedToRl = FALSE;
pRec->peerRpao = FALSE;
pRec->sequenceNumber = record_with_highest_seq_number()->sequenceNumber + 1;
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