extern void BatteryHandleAccessWrite(GATT_ACCESS_IND_T *p_ind) { uint8 *p_value = p_ind->value; uint16 client_config; sys_status rc = sys_status_success; switch(p_ind->handle) { case HANDLE_BATT_LEVEL_C_CFG: { client_config = BufReadUint16(&p_value); /* Client Configuration is bit field value so ideally bitwise * comparison should be used but since the application supports only * notifications, direct comparison is being used. */ if((client_config == gatt_client_config_notification) || (client_config == gatt_client_config_none)) { g_batt_data.level_client_config = client_config; /* Write battery level client configuration to NVM if the * device is bonded. */ Nvm_Write(&client_config, sizeof(client_config), g_batt_data.nvm_offset + BATTERY_NVM_LEVEL_CLIENT_CONFIG_OFFSET); } else { /* INDICATION or RESERVED */ /* Return error as only notifications are supported */ rc = gatt_status_desc_improper_config; } } break; default: rc = gatt_status_write_not_permitted; break; } /* Send ACCESS RESPONSE */ GattAccessRsp(p_ind->cid, p_ind->handle, rc, 0, NULL); /* Send an update as soon as notifications are configured */ if(g_batt_data.level_client_config & gatt_client_config_notification) { /* Reset current battery level to an invalid value so that it * triggers notifications on reading the current battery level */ g_batt_data.level = 0xFF; /* 0 to 100: Valid value range */ BatteryUpdateLevel(p_ind->cid); } }
extern void BatteryHandleAccessWrite(GATT_ACCESS_IND_T *p_ind) { uint8 *p_value = p_ind->value; uint16 client_config; sys_status rc = sys_status_success; switch(p_ind->handle) { case HANDLE_BATT_LEVEL_C_CFG: { client_config = BufReadUint16(&p_value); if((client_config == gatt_client_config_notification) || (client_config == gatt_client_config_none)) { g_batt_data.level_client_config = client_config; /* Write batery level client configuration to NVM if the * device is bonded. */ if(AppIsDeviceBonded()) { Nvm_Write((uint16 *)&client_config, sizeof(client_config), g_batt_data.nvm_offset + BATTERY_NVM_LEVEL_CLIENT_CONFIG_OFFSET); } } else { /* INDICATION or RESERVED */ /* Return error as only notifications are supported */ rc = gatt_status_app_mask; } } break; default: { rc = gatt_status_write_not_permitted; } break; } /* Send ACCESS RESPONSE */ GattAccessRsp(p_ind->cid, p_ind->handle, rc, 0, NULL); /* Send an update as soon as notifications are configured */ if(g_batt_data.level_client_config == gatt_client_config_notification) { /* Reset current battery level to an invalid value so that it * triggers notifications on reading the current battery level */ g_batt_data.level = 0xFF; /* 0 to 100: Valid value range */ BatteryUpdateLevel(p_ind->cid); } }
/*----------------------------------------------------------------------------* * NAME * HandleAccessWrite * * DESCRIPTION * This function handles write operations on Battery Service attributes * maintained by the application and responds with the GATT_ACCESS_RSP * message. * * PARAMETERS * p_ind [in] Data received in GATT_ACCESS_IND message. * * RETURNS * Nothing *----------------------------------------------------------------------------*/ extern void BatteryHandleAccessWrite(GATT_ACCESS_IND_T *p_ind) { uint8 *p_value = p_ind->value; /* New attribute value */ uint16 client_config; /* Client configuration descriptor */ sys_status rc = sys_status_success; /* Function status */ switch(p_ind->handle) { case HANDLE_BATT_LEVEL_C_CFG: { /* Write the client configuration descriptor for the battery level * characteristic. */ client_config = BufReadUint16(&p_value); /* Only notifications are allowed for this client configuration * descriptor. */ if((client_config == gatt_client_config_notification) || (client_config == gatt_client_config_none)) { g_batt_data.level_client_config = client_config; } else { /* INDICATION or RESERVED */ /* Return error as only notifications are supported */ rc = gatt_status_app_mask; } } break; default: rc = gatt_status_write_not_permitted; break; } /* Send ACCESS RESPONSE */ GattAccessRsp(p_ind->cid, p_ind->handle, rc, 0, NULL); /* Send an update as soon as notifications are configured */ if(g_batt_data.level_client_config == gatt_client_config_notification) { /* Reset current battery level to an invalid value so that it * triggers notifications on reading the current battery level */ g_batt_data.level = 0xFF; /* 0 to 100: Valid value range */ /* Update the battery level and send notification. */ BatteryUpdateLevel(p_ind->cid); } }