BtStatus FmpReadServiceDB(BD_ADDR *bd_addr, FmpServiceDB **servicedb) { S32 fd; BD_ADDR addr; FmpServiceDB *db; S32 pos_file_end; S32 pos_end; U16 service_size; fd = btmtk_fs_open((const U8*)FMP_SERVICE_DATABASE_FILE, BTMTK_FS_READ_ONLY); if (fd < 0) { // file is not exist return BT_STATUS_FAILED; } service_size = sizeof(FmpServiceDB); pos_file_end = btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_END); pos_end = btmtk_fs_seek(fd, 0, BTMTK_FS_SEEK_BEGIN); /* Seach database file util the end of the file */ while (pos_end != pos_file_end) { btmtk_fs_read(fd, (U8 *)&addr, 6); Report(("[FMP] read 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 */ btmtk_fs_close(fd); return BT_STATUS_FAILED; } db = (FmpServiceDB *)fmp_malloc(sizeof(FmpServiceDB)); if (db == NULL) { return BT_STATUS_NO_RESOURCES; } btmtk_fs_seek(fd, pos_end, BTMTK_FS_SEEK_BEGIN); btmtk_fs_read(fd, (U8 *)db, service_size); Report(("[FMP] read service db, handle: 0x%x 0x%x 0x%x ", db->start_handle, db->end_handle, db->char_value_handle)); btmtk_fs_close(fd); *servicedb = db; return BT_STATUS_SUCCESS; }
BtStatus FMPL_SetAlertLevel(FmpChannel *channel, FmpAlertLevel level) { U8 *level_data; level_data = (U8 *)fmp_malloc(sizeof(FmpAlertLevel)); *level_data = level; if (channel->servicedb == NULL) { return BT_STATUS_FAILED; } return FmplWriteWithoutResponse(channel, channel->servicedb->char_value_handle, level_data, sizeof(FmpAlertLevel)); }
void FMPL_QueryGattService(FmpChannel *channel) { BtStatus status; BtDeviceRecord record; OS_LockStack(); /* check service database first */ if (SEC_FindDeviceRecord(&channel->link->bdAddr, &record) == BT_STATUS_SUCCESS) // bonded device { if (FmpReadServiceDB(&channel->link->bdAddr, &channel->servicedb) == BT_STATUS_SUCCESS) { FmpAppCallback(channel, FMPEVENT_GATT_QUERY_CNF, BT_STATUS_SUCCESS, 0, 0); return; } } else { FmpRemoveServiceDB(&channel->link->bdAddr); } channel->servicedb = (FmpServiceDB *)fmp_malloc(sizeof(FmpServiceDB)); if (channel->servicedb == NULL) { FmpAppCallback(channel, FMPEVENT_GATT_QUERY_CNF, BT_STATUS_NO_RESOURCES, 0, 0); return; } OS_MemSet((U8 *)channel->servicedb, 0, sizeof(FmpServiceDB)); channel->servicedb->bt_addr = channel->link->bdAddr; kal_trace(BT_TRACE_BLE_PROFILES, FMP_QUEREYGATTSERVICE); /* search IAS service */ status = FmplFindPrimaryServicesByUuid(channel, BT_UUID_GATT_SERVICE_ALERT, 0x0001, 0xffff); if (status != BT_STATUS_PENDING && status != BT_STATUS_SUCCESS) { fmp_free(channel->servicedb); FmpAppCallback(channel, FMPEVENT_GATT_QUERY_CNF, status, 0, 0); } OS_UnlockStack(); }
FmpChannel *FmpNewChannel(void) { FmpChannel *ch; U8 i; ch = (FmpChannel *)fmp_malloc(sizeof(FmpChannel)); if (ch != NULL) { OS_MemSet((U8 *)ch, 0, sizeof(FmpChannel)); InsertTailList(&FMP(dev_list), &(ch->node)); CMGR_RegisterHandler(&ch->cmgr_handler, FmpCmgrCallback); /* Initialize packet pool for each device */ InitializeListHead(&ch->packet_pool); for (i = 0; i < FMP_NUM_TX_PACKETS; i++) { InsertTailList(&ch->packet_pool, &ch->packets[i].node); } } return ch; }
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); }