/*----------------------------------------------------------------------------*
 *  NAME
 *      BatteryDataInit
 *
 *  DESCRIPTION
 *      This function is used to initialise the Battery Service data structure.
 *
 *  PARAMETERS
 *      None
 *
 *  RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
extern void BatteryDataInit(void)
{
    if(!IsDeviceBonded())
    {
        /* Initialise battery level client configuration characterisitic
         * descriptor value only if device is not bonded
         */
        g_batt_data.level_client_config = gatt_client_config_none;
    }

}
Exemple #2
0
/*----------------------------------------------------------------------------*
 *  NAME
 *      GattTriggerFastAdverts
 *
 *  DESCRIPTION
 *      This function is used to trigger fast advertisements.
 *
 *  PARAMETERS
 *      p_addr [in]             Bonded host address
 *
 *  RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
extern void GattTriggerFastAdverts(TYPED_BD_ADDR_T *p_addr)
{
    if(IsDeviceBonded())
    {
        g_gatt_data.advert_timer_value = BONDED_DEVICE_ADVERT_TIMEOUT_VALUE;
    }
    else
    {
        g_gatt_data.advert_timer_value = FAST_CONNECTION_ADVERT_TIMEOUT_VALUE;
    }

    /* Trigger fast connections */
    GattStartAdverts(p_addr, TRUE);
}
/*----------------------------------------------------------------------------*
 *  NAME
 *      BatteryBondingNotify
 *
 *  DESCRIPTION
 *      This function is used by application to notify bonding status to the
 *      Battery Service.
 *
 *  PARAMETERS
 *      None
 *
 *  RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
extern void BatteryBondingNotify(void)
{

    /* Write data to NVM if bond is established */
    if(IsDeviceBonded())
    {
        /* Write to NVM the client configuration value of battery level 
         * that was configured prior to bonding 
         */
        Nvm_Write((uint16*)&g_batt_data.level_client_config, 
                  sizeof(g_batt_data.level_client_config), 
                  g_batt_data.nvm_offset + 
                  BATTERY_NVM_LEVEL_CLIENT_CONFIG_OFFSET);
    }

}
/*----------------------------------------------------------------------------*
 *  NAME
 *      BatteryReadDataFromNVM
 *
 *  DESCRIPTION
 *      This function is used to read Battery Service specific data stored in 
 *      NVM.
 *
 *  PARAMETERS
 *      p_offset [in]           Offset to Battery Service data in NVM
 *               [out]          Offset to next entry in NVM
 *
 *  RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
extern void BatteryReadDataFromNVM(uint16 *p_offset)
{

    g_batt_data.nvm_offset = *p_offset;

    /* Read NVM only if devices are bonded */
    if(IsDeviceBonded())
    {
        /* Read battery level client configuration descriptor */
        Nvm_Read((uint16*)&g_batt_data.level_client_config,
                sizeof(g_batt_data.level_client_config),
                *p_offset + 
                BATTERY_NVM_LEVEL_CLIENT_CONFIG_OFFSET);
    }

    /* Increment the offset by the number of words of NVM memory required 
     * by the Battery Service 
     */
    *p_offset += BATTERY_SERVICE_NVM_MEMORY_WORDS;

}
/*----------------------------------------------------------------------------*
 *  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;

                /* Write battery level client configuration to NVM if the 
                 * device is bonded.
                 */
                if(IsDeviceBonded())
                {
                     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_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);
    }

}