/******************************************************************************* NAME handleWriteBatteryLevelClientConfig DESCRIPTION Handle when a GATT_BATTERY_SERVER_WRITE_CLIENT_CONFIG_IND message is recieved. PARAMETERS ind Pointer to a GATT_BATTERY_SERVER_WRITE_CLIENT_CONFIG_IND message. RETURNS void */ static void handleWriteBatteryLevelClientConfig(GATT_BATTERY_SERVER_WRITE_CLIENT_CONFIG_IND_T * ind) { gatt_client_connection_t *conn = gattClientFindByCid(ind->cid); /* * Check whether the remote device has enabled or disabled * notifications for the Battery Level characteristic. This value will need * to be stored as device attributes so they are persistent. */ GATT_BATTERY_SERVER_INFO(("GATT_BATTERY_SERVER_WRITE_CLIENT_CONFIG_IND bas=[0x%p] cid=[0x%x] value[0x%x]\n", (void *)ind->battery_server, ind->cid, ind->config_value)); if (conn) { unsigned short updateNeedsSending = 0; if (ind->battery_server == GATT_SERVER.bas_server_local) { conn->client_config.battery_local = ind->config_value; GATT_BATTERY_SERVER_INFO((" battery local client_config[0x%x]\n", conn->client_config.battery_local)); gattClientStoreConfigAttributes(ind->cid, gatt_attr_service_battery_local); if( LOCAL_UPDATE_REQD(conn) ) { updateNeedsSending++; } } else if (ind->battery_server == GATT_SERVER.bas_server_remote) { conn->client_config.battery_remote = ind->config_value; GATT_BATTERY_SERVER_INFO((" battery remote client_config[0x%x]\n", conn->client_config.battery_remote)); gattClientStoreConfigAttributes(ind->cid, gatt_attr_service_battery_remote); if( REMOTE_UPDATE_REQD(conn) ) { updateNeedsSending++; } } else if (ind->battery_server == GATT_SERVER.bas_server_peer) { conn->client_config.battery_peer = ind->config_value; GATT_BATTERY_SERVER_INFO((" battery peer client_config[0x%x]\n", conn->client_config.battery_peer)); gattClientStoreConfigAttributes(ind->cid, gatt_attr_service_battery_peer); if( PEER_UPDATE_REQD(conn) ) { updateNeedsSending++; } } if( updateNeedsSending > 0 ) { MessageCancelFirst( sinkGetBleTask(), BLE_INTERNAL_MESSAGE_BATTERY_READ_TIMER ); MessageSend( sinkGetBleTask(), BLE_INTERNAL_MESSAGE_BATTERY_READ_TIMER, 0 ); } } }
/******************************************************************************* NAME sinkGattBatteryServerInitTask DESCRIPTION Initialise a Battery server task. NOTE: This function will modify *ptr. PARAMETERS ptr - pointer to allocated memory to store server tasks rundata. remote_battery - TRUE if a remote battery, FALSE otherwise. RETURNS TRUE if the Battery server task was initialised, FALSE otherwise. */ static bool sinkGattBatteryServerInitTask(uint16 **ptr, gatt_server_battery_id battery_id) { gatt_battery_server_init_params_t params = {BATTERY_SERVER_ENABLE_NOTIFICATIONS}; gatt_battery_server_status_t bas_status; uint16 start_handle; uint16 end_handle; if (!ptr) { return FALSE; } if(!sinkGattGetBatteryServerHandles(&start_handle, &end_handle, battery_id)) { return FALSE; } bas_status = GattBatteryServerInit((GBASS*)*ptr, sinkGetBleTask(), ¶ms, start_handle, end_handle); if( bas_status != gatt_battery_server_status_success) { GATT_BATTERY_SERVER_INFO(("GATT Battery Server init failed [%x]\n", bas_status)); return FALSE; } if(!sinkGattGetBatteryServerUpdateRundata(ptr, battery_id)) { return FALSE; } GATT_BATTERY_SERVER_INFO(("GATT Battery Server [%u] initialised\n", battery_id)); return TRUE; }
/******************************************************************************* NAME initialiseGattWithServers DESCRIPTION Function to initialise GATT for the device when server roles have been requested. PARAMETERS None RETURNS TRUE if the initialisation request was successful, FALSE otherwise. */ static bool initialiseGattWithServers(void) { GATT_DEBUG(("Initialise GATT Manager - with servers\n")); if (GattManagerInit(sinkGetBleTask())) { if (GattManagerRegisterConstDB(&gattDatabase[0], GattGetDatabaseSize())) { if (initialiseGattServers()) { GattManagerRegisterWithGatt(); return TRUE; } } } return FALSE; }
/****************************************************************************** NAME sinkBleBatteryLevelReadSendAndRepeat DESCRIPTION Checks and dispatches the battery levels required for each gatt connection. If required, it starts a repeat timer. PARAMETERS NONE RETURNS NONE */ void sinkBleBatteryLevelReadSendAndRepeat(void) { uint16 idx; uint16 updateNeedsSending = 0; for( idx = 0;idx < GATT.client.number_connections;idx++ ) { gatt_client_connection_t *conn = &(GATT.client.connection[idx]); if( LOCAL_UPDATE_REQD( conn ) ) { updateNeedsSending++; GattBatteryServerSendLevelNotification( GATT_SERVER.bas_server_local, 1, &(conn->cid), powerManagerBatteryLevelAsPercentage() ); } #if defined(GATT_BATTERY_SERVER_PEER) && defined(ENABLE_PEER) if( PEER_UPDATE_REQD( conn ) ) { updateNeedsSending++; GattBatteryServerSendLevelNotification( GATT_SERVER.bas_server_peer, 1, &(conn->cid), peerGetBatteryLevel() ); } #endif #ifdef GATT_BATTERY_SERVER_REMOTE if( REMOTE_UPDATE_REQD( conn ) ) { updateNeedsSending++; GattBatteryServerSendLevelNotification( GATT_SERVER.bas_server_remote, 1, &(conn->cid), gattBatteryClientGetCachedLevel() ); } #endif } if( updateNeedsSending > 0 ) { MessageSendLater( sinkGetBleTask(), BLE_INTERNAL_MESSAGE_BATTERY_READ_TIMER, 0, GATT_SERVER_BATTERY_UPDATE_TIME ); } }