extern void GattStartAdverts(bool fast_connection)
{
    /* Variable 'connect_flags' needs to be updated to have peer address type
     * if Directed advertisements are supported as peer address type will
     * only be used in that case. We don't support directed advertisements for
     * this application.
     */

#ifdef USE_STATIC_RANDOM_ADDRESS
    uint16 connect_flags = L2CAP_CONNECTION_SLAVE_UNDIRECTED |
                      L2CAP_OWN_ADDR_TYPE_RANDOM;
#else
    uint16 connect_flags = L2CAP_CONNECTION_SLAVE_UNDIRECTED |
                          L2CAP_OWN_ADDR_TYPE_PUBLIC;
#endif /* USE_STATIC_RANDOM_ADDRESS */

    /* Set UCID to INVALID_UCID */
    g_hr_data.st_ucid = GATT_INVALID_UCID;

    /* Set advertisement parameters */
    gattSetAdvertParams(fast_connection);

    /* If white list is enabled, set the controller's advertising filter policy
     * to "process scan and connection requests only from devices in the White
     * List"
     */
    if((g_hr_data.enable_white_list == TRUE) &&
        (!GattIsAddressResolvableRandom(&(g_hr_data.bonded_bd_addr))))
    {
#ifdef USE_STATIC_RANDOM_ADDRESS
        connect_flags = L2CAP_CONNECTION_SLAVE_WHITELIST |
                   L2CAP_OWN_ADDR_TYPE_RANDOM;
#else
        connect_flags = L2CAP_CONNECTION_SLAVE_WHITELIST |
                       L2CAP_OWN_ADDR_TYPE_PUBLIC;
#endif /* USE_STATIC_RANDOM_ADDRESS */
    }

    /* Start GATT connection in Slave role */
    GattConnectReq(NULL, connect_flags);

    /* Start advertisement timer */
    if(g_hr_data.advert_timer_value)
    {
        TimerDelete(g_hr_data.app_tid);

        /* Start advertisement timer  */
        g_hr_data.app_tid = TimerCreate(g_hr_data.advert_timer_value, TRUE,
                                        gattAdvertTimerHandler);
    }

}
Beispiel #2
0
/*----------------------------------------------------------------------------*
 *  NAME
 *      GattStartAdverts
 *
 *  DESCRIPTION
 *      This function is used to start undirected advertisements and moves to 
 *      ADVERTISING state.
 *
 *  PARAMETERS
 *      p_addr [in]             Bonded host address
 *      fast_connection [in]    TRUE:  Fast advertisements
 *                              FALSE: Slow advertisements
 *
 *  RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
extern void GattStartAdverts(TYPED_BD_ADDR_T *p_addr, bool fast_connection)
{
    /* Variable 'connect_flags' needs to be updated to have peer address type 
     * if Directed advertisements are supported as peer address type will 
     * only be used in that case. We don't support directed advertisements for 
     * this application.
     */
#ifdef USE_STATIC_RANDOM_ADDRESS
    uint16 connect_flags = L2CAP_CONNECTION_SLAVE_UNDIRECTED | 
                           L2CAP_OWN_ADDR_TYPE_RANDOM;
#else
    uint16 connect_flags = L2CAP_CONNECTION_SLAVE_UNDIRECTED | 
                           L2CAP_OWN_ADDR_TYPE_PUBLIC;
#endif /* USE_STATIC_RANDOM_ADDRESS */

    /* Set advertisement parameters */
    gattSetAdvertParams(p_addr, fast_connection);

    /* If white list is enabled, set the controller's advertising filter policy 
     * to "process scan and connection requests only from devices in the White 
     * List"
     */
    if(IsWhiteListEnabled() && !GattIsAddressResolvableRandom(p_addr))
    {
#ifdef USE_STATIC_RANDOM_ADDRESS
        connect_flags = L2CAP_CONNECTION_SLAVE_WHITELIST |
                        L2CAP_OWN_ADDR_TYPE_RANDOM;
#else
        connect_flags = L2CAP_CONNECTION_SLAVE_WHITELIST |
                        L2CAP_OWN_ADDR_TYPE_PUBLIC;
#endif /* USE_STATIC_RANDOM_ADDRESS */
    }

    /* Start GATT connection in Slave role */
    GattConnectReq(NULL, connect_flags);

     /* Start advertisement timer */
    if(g_gatt_data.advert_timer_value)
    {
        StartAdvertTimer(g_gatt_data.advert_timer_value);
    }
}
extern void GattTriggerFastAdverts(void)
{

    if(g_hr_data.bonded)
    {
        if(!GattIsAddressResolvableRandom(&g_hr_data.bonded_bd_addr))
        {
            /* Enable white list if the device is bonded and the bonded host
             * doesn't support resolvable random addresses
             */
            g_hr_data.enable_white_list = TRUE;
        }

        g_hr_data.advert_timer_value = BONDED_DEVICE_ADVERT_TIMEOUT_VALUE;
    }
    else
    {
        g_hr_data.advert_timer_value = FAST_CONNECTION_ADVERT_TIMEOUT_VALUE;
    }

    /* Trigger fast conections */
    GattStartAdverts(TRUE);

}
Beispiel #4
0
/*----------------------------------------------------------------------------*
 *  NAME
 *      readPersistentStore
 *
 *  DESCRIPTION
 *      This function is used to initialise and read NVM data.
 *
 *  PARAMETERS
 *      None
 *
 *  RETURNS
 *      Nothing
 *----------------------------------------------------------------------------*/
static void readPersistentStore(void)
{
    /* NVM offset for supported services */
    uint16 nvm_offset = NVM_MAX_APP_MEMORY_WORDS;
    uint16 nvm_sanity = 0xffff;

    /* Read persistent storage to find if the device was last bonded to another
     * device. If the device was bonded, trigger fast undirected advertisements
     * by setting the white list for bonded host. If the device was not bonded,
     * trigger undirected advertisements for any host to connect.
     */

    NvmRead(&nvm_sanity, sizeof(nvm_sanity), NVM_OFFSET_SANITY_WORD);

    if (nvm_sanity == NVM_SANITY_MAGIC)
    {
        /* Read bonded flag from NVM */
        NvmRead((uint16 *)&(IS_BONDED),
                 sizeof(IS_BONDED),
                 NVM_OFFSET_BONDED_FLAG);

        if (IS_BONDED)
        {
            /* Bonded host typed BD address will only be stored if bonded flag
             * is set to TRUE. Read last bonded device address.
             */
            NvmRead((uint16 *)&(CONN_HOST_ADDR),
                      sizeof(TYPED_BD_ADDR_T),
                      NVM_OFFSET_BONDED_ADDR);

            /* If device is bonded and bonded address is resolvable then read
             * the bonded device's IRK
             */
            if (GattIsAddressResolvableRandom(&(CONN_HOST_ADDR)))
            {
                NvmRead(CONNECTION_IRK, 
                        MAX_WORDS_IRK,
                        NVM_OFFSET_SM_IRK);
            }
        }
        else /* Case when we have only written NVM_SANITY_MAGIC to NVM but 
              * didn't get bonded to any host in the last powered session
              */
        {
            /* Any initialisation can be done here for non-bonded devices */
            
        }

        /* Read the diversifier associated with the presently bonded/last 
         * bonded device.
         */
        NvmRead(&(LINK_DIVERSIFIER),
                 sizeof(LINK_DIVERSIFIER),
                 NVM_OFFSET_SM_DIV);

        /* Read GATT data from NVM */
        GattReadDataFromNVM(&nvm_offset);
    }
    else /* NVM sanity check failed means either the device is being brought up 
          * for the first time or memory has been corrupted in which case 
          * discard the data and start fresh.
          */
    {
        nvm_sanity = NVM_SANITY_MAGIC;

        /* Write NVM Sanity word to the NVM */
        NvmWrite(&nvm_sanity, 
                 sizeof(nvm_sanity), 
                 NVM_OFFSET_SANITY_WORD);

        /* The device will not be bonded as it is coming up for the first time
         */
        IS_BONDED = FALSE;

        /* Write bonded status to NVM */
        NvmWrite((uint16 *)&(IS_BONDED),
                 sizeof(IS_BONDED), 
                 NVM_OFFSET_BONDED_FLAG);

        /* When the application is coming up for the first time after flashing 
         * the image to it, it will not have bonded to any device. So, no LTK 
         * will be associated with it. Hence, set the diversifier to 0.
         */
        LINK_DIVERSIFIER = 0;

        /* Write the same to NVM. */
        NvmWrite(&(LINK_DIVERSIFIER),
                 sizeof(LINK_DIVERSIFIER),
                 NVM_OFFSET_SM_DIV);

        /* Since device is being brought up first time, it won't be in
         * bonded state. Following function call will only initialise the NVM 
         * offset of GATT service.
         */
        GattReadDataFromNVM(&nvm_offset);
    }
}