static void BleServiceDbBondingCb(U8 status, const U8 *bd_addr) { ble_service_db *servicedb; Report(("[BleDB] bonding callback, status: 0x%x", status)); // kal_trace(BT_TRACE_BLE_PROFILES, BLEDB_BONDINGCB_STATUS, status); switch (status) { //case ATT_SEC_DEVICE_BOND: /* Device Bonded or Bonded again*/ //servicedb = BleServiceDbGetRecord((BD_ADDR *)bd_addr); //BleServiceDbUpdateRecord(servicedb); //break; case ATT_SEC_DEVICE_UNBOND: /* Device Un Bonded */ BleServiceDbRemoveRecord((BD_ADDR *)bd_addr); break; case ATT_SEC_DEVICE_ALL_UNBOND: /* All Device Un Bonded */ btmtk_fs_delete((const U8 *)BLE_SERVICE_DATABASE_FILE); break; } }
static void BleServiceDbRemoveRecord(const BD_ADDR *bd_addr) { S32 fd; S32 fd_temp; S32 pos_current; S32 pos_end; U8 *db_buff; U8 *buffer; if (!btmtk_fs_is_dir_exist((const U8*)BLE_SERVICE_DATABASE_FILE)) { return; } fd = btmtk_fs_open((const U8*)BLE_SERVICE_DATABASE_FILE, BTMTK_FS_READ_WRITE); if (fd < 0) { return; } pos_end = btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_END); pos_current = btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_BEGIN); db_buff = (U8 *)get_ctrl_buffer(sizeof(ble_service_db) - sizeof(ListEntry)); while (pos_current < pos_end) { btmtk_fs_read(fd, db_buff, sizeof(ble_service_db) - sizeof(ListEntry)); if (OS_MemCmp(db_buff, 6, bd_addr->addr, 6)) { break; } pos_current += (sizeof(ble_service_db) - sizeof(ListEntry)); } free_ctrl_buffer(db_buff); if (pos_current == pos_end) { Report(("[BleDB] Remove record, NOT FOUND!")); // kal_trace(BT_TRACE_BLE_PROFILES, BLEDB_REMOVERECORD_NOTFOUND);. return; } /* Case 1: If there is only one database, delete the file directly */ if (pos_end - pos_current == sizeof(ble_service_db) - sizeof(ListEntry)) { Report(("[BleDB] Remove record, delete file directly...")); // kal_trace(BT_TRACE_BLE_PROFILES, BLEDB_REMOVERECORD_DELETEFILE); btmtk_fs_delete((const U8 *)BLE_SERVICE_DATABASE_FILE); return; } /* Case 2: If there is more than one database, create a new temp file, * move the left database to the temp file, delete the original database * file, and rename the temp file as the new database file. */ fd_temp = btmtk_fs_open((const U8*)BLE_SERVICE_DATABASE_TEMP_FILE, BTMTK_FS_READ_WRITE | BTMTK_FS_CREATE); if (fd_temp < 0) { return; } if (pos_current > 0) { /* Move first half of the database to temp file */ buffer = (U8 *)get_ctrl_buffer(pos_current); btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_BEGIN); btmtk_fs_read(fd, buffer, pos_current); btmtk_fs_write(fd_temp, buffer, pos_current); free_ctrl_buffer(buffer); } pos_current = btmtk_fs_seek(fd, sizeof(ble_service_db) - sizeof(ListEntry), BTMTK_FS_SEEK_CURRENT); if (pos_current < pos_end) { /* Move first half of the database to temp file */ buffer = (U8 *)get_ctrl_buffer(pos_end - pos_current); btmtk_fs_read(fd, buffer, pos_end - pos_current); btmtk_fs_write(fd_temp, buffer, pos_end - pos_current); free_ctrl_buffer(buffer); } Report(("[BleDB] pos cur: %d, pos end: %d", pos_current, pos_end)); btmtk_fs_close(fd); btmtk_fs_close(fd_temp); btmtk_fs_delete((const U8 *)BLE_SERVICE_DATABASE_FILE); btmtk_fs_rename((const U8 *)BLE_SERVICE_DATABASE_TEMP_FILE, (const U8 *)BLE_SERVICE_DATABASE_FILE); }
void FmpRemoveServiceDB(BD_ADDR *bd_addr) { S32 fd; S32 fd_temp; BD_ADDR addr; U16 service_size; S32 pos_file_end; S32 pos_start; S32 pos_end; U8 *buffer; fd = btmtk_fs_open((const U8*)FMP_SERVICE_DATABASE_FILE, BTMTK_FS_READ_ONLY); if (fd < 0) { // file is not exist return; } service_size = sizeof(FmpServiceDB); pos_file_end = btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_END); pos_start = btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_BEGIN); pos_end = pos_start; /* Seach database file util the end of the file */ while (pos_end != pos_file_end) { btmtk_fs_read(fd, (U8 *)&addr, 6); Report(("[FMP] write service db, addr: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x", addr.addr[0], addr.addr[1], addr.addr[2], addr.addr[3], addr.addr[4], addr.addr[5])); if (!fmp_dev_addr_equal(bd_addr, &addr)) { pos_end = btmtk_fs_seek(fd, service_size - 6, BTMTK_FS_SEEK_CURRENT); continue; } break; } if (pos_end == pos_file_end) { /* No matched device database has been found */ kal_trace(BT_TRACE_BLE_PROFILES, FMP_REMOVEDATABASE_NO_MATCHED); btmtk_fs_close(fd); return; } /* Case 1: If there is only one database, delete the file directly */ if (pos_file_end - pos_start == service_size) { kal_trace(BT_TRACE_BLE_PROFILES, HTP_REMOVEDATABASE_DELETEFILE); btmtk_fs_delete((const U8 *)FMP_SERVICE_DATABASE_FILE); return; } /* Case 2: If there is more than one database, create a new temp file, * move the left database to the temp file, delete the original database * file, and rename the temp file as the new database file. */ fd_temp = btmtk_fs_open((const U8*)FMP_SERVICE_DATABASE_TEMP_FILE, BTMTK_FS_READ_WRITE | BTMTK_FS_CREATE); if (fd_temp < 0) { return; } if (pos_end != pos_start) { /* Move first half of the database to temp file */ buffer = (U8 *)fmp_malloc(pos_end - pos_start); btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_BEGIN); btmtk_fs_read(fd, buffer, pos_end - pos_start); btmtk_fs_write(fd_temp, buffer, pos_end - pos_start); fmp_free(buffer); pos_start = btmtk_fs_seek(fd, service_size, BTMTK_FS_SEEK_CURRENT); } else { pos_start = btmtk_fs_seek(fd, service_size - 6, BTMTK_FS_SEEK_CURRENT); } if (pos_start != pos_file_end) { /* Move first half of the database to temp file */ buffer = (U8 *)fmp_malloc(pos_file_end - pos_start); btmtk_fs_read(fd, buffer, pos_file_end - pos_start); btmtk_fs_write(fd_temp, buffer, pos_file_end - pos_start); fmp_free(buffer); } btmtk_fs_close(fd); btmtk_fs_close(fd_temp); btmtk_fs_delete((const U8 *)FMP_SERVICE_DATABASE_FILE); btmtk_fs_rename((const U8 *)FMP_SERVICE_DATABASE_TEMP_FILE, (const U8 *)FMP_SERVICE_DATABASE_FILE); }