void FmpFreeChannel(FmpChannel *channel) { CMGR_DeregisterHandler(&channel->cmgr_handler); RemoveEntryList(&(channel->node)); fmp_free(channel->servicedb); fmp_free(channel); }
void FmpAppCallback(FmpChannel *channel, FmpEvent event, U32 status, void *ptr, U16 len) { FmpCallbackParms parms; switch (event) { case FMPEVENT_OPEN: case FMPEVENT_CLOSED: parms.parm.rem_dev = ptr; break; case FMPEVENT_GATT_QUERY_CNF: if (status != BT_STATUS_SUCCESS) { fmp_free(channel->servicedb); } break; case FMPEVENT_SET_ALERT_LEVEL_REQ: case FMPEVENT_SET_ALERT_LEVEL_CNF: parms.parm.level = *(U8 *)ptr; break; default: break; } parms.event = event; parms.len = len; parms.status = status; FMP(fmp_callback)(channel, &parms); }
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(); }
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); }