/** * @brief get a dual mode GAP Bond Manager Parameter. * * NOTE: You can call this function with a GAP Bond Manager Parameter ID and it will get a * GAP Bond Manager Parameter. GAP Bond Manager Parameters are defined in (gapbond_common.h). Also, the * "pValue" field must point to a "uint16". * * @param param - Profile parameter ID: @ref GAPBOND_MANAGER_PARAMETERS * @param pvalue - pointer to location to get the value. This is dependent on * the parameter ID and WILL be cast to the appropriate * data type (example: data type of uint16 will be cast to * uint16 pointer). * * @return gapAPI_CauseSuccess or gapAPI_InvalidPara (invalid paramID) */ TGAP_STATUS GAPBonddual_GetParameter(uint16_t param, void *pvalue) { TGAP_STATUS ret = gapAPI_CauseSuccess; switch (param) { case GAPBOND_BTMODE: case GAPBOND_OOB_DATA_R: case GAPBOND_OOB_DATA_C: ret = GAPBondlegacy_GetParameter(param, pvalue); break; case GAPBOND_PAIRING_MODE: case GAPBOND_MITM_PROTECTION: case GAPBOND_IO_CAPABILITIES: case GAPBOND_OOB_ENABLED: case GAPBOND_PASSKEY: ret = GAPBondCom_GetParameter(param, pvalue); break; case GAPBOND_OOB_DATA: case GAPBOND_FIXED_PASSKEY_ENABLE: case GAPBOND_SEC_REQ_ENABLE: case GAPBOND_SEC_REQ_REQUIREMENT: ret = GAPBondMgr_GetParameter(param, pvalue); break; default: ret = gapAPI_InvalidPara; break; } return ret; }
/********************************************************************* * @fn hidappBondCount * * @brief Gets the total number of bonded devices. * * @param none. * * @return number of bonded devices. */ static uint8 hidappBondCount( void ) { uint8 bondCnt = 0; VOID GAPBondMgr_GetParameter( GAPBOND_BOND_COUNT, &bondCnt ); return ( bondCnt ); }
/********************************************************************* * @fn SensorGapStateCB * * @brief Notification from the profile of a state change. * * @param newState - new state * * @return none */ static void SensorGapStateCB( gaprole_States_t newState ) { // if connected if (newState == GAPROLE_CONNECTED) { // get connection handle GAPRole_GetParameter(GAPROLE_CONNHANDLE, &gapConnHandle); // Set timer to update connection parameters // 5 seconds should allow enough time for Service Discovery by the collector to finish osal_start_timerEx( sensor_TaskID, RSC_CONN_PARAM_UPDATE_EVT, SVC_DISC_DELAY); } // if disconnected else if (gapProfileState == GAPROLE_CONNECTED && newState != GAPROLE_CONNECTED) { uint8 advState = TRUE; uint8 bondCount = 0; // stop periodic measurement osal_stop_timerEx( sensor_TaskID, RSC_PERIODIC_EVT ); // reset client characteristic configuration descriptors Running_HandleConnStatusCB( gapConnHandle, LINKDB_STATUS_UPDATE_REMOVED ); //Batt_HandleConnStatusCB( gapConnHandle, LINKDB_STATUS_UPDATE_REMOVED ); // If not already using white list, begin to do so. GAPBondMgr_GetParameter( GAPBOND_BOND_COUNT, &bondCount ); if(USING_WHITE_LIST && sensorUsingWhiteList == FALSE && bondCount > 0 ) { uint8 value = GAP_FILTER_POLICY_WHITE; GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof( uint8 ), &value); sensorUsingWhiteList = TRUE; } if ( newState == GAPROLE_WAITING_AFTER_TIMEOUT ) { // link loss timeout-- use fast advertising GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_WHITE_LIST_ADV_DURATION ); } else { // Else use slow advertising GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_WHITE_LIST_ADV_DURATION ); } // Enable advertising GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState ); } // if advertising stopped else if ( gapProfileState == GAPROLE_ADVERTISING && newState == GAPROLE_WAITING ) { uint8 whiteListUsed = FALSE; // if white list is in use, disable to allow general access if ( sensorUsingWhiteList == TRUE ) { uint8 value = GAP_FILTER_POLICY_ALL; GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof( uint8), &value); whiteListUsed = TRUE; sensorUsingWhiteList = FALSE; } // if advertising stopped by user if ( sensorAdvCancelled ) { sensorAdvCancelled = FALSE; } // if fast advertising interrupted to cancel white list else if ( ( (!USING_WHITE_LIST) || whiteListUsed) && (GAP_GetParamValue( TGAP_GEN_DISC_ADV_INT_MIN ) == DEFAULT_FAST_ADV_INTERVAL ) ) { uint8 advState = TRUE; GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_FAST_ADV_DURATION ); GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState ); } // if fast advertising switch to slow or if was already slow but using white list. else if ( ((!USING_WHITE_LIST) || whiteListUsed) || (GAP_GetParamValue( TGAP_GEN_DISC_ADV_INT_MIN ) == DEFAULT_FAST_ADV_INTERVAL) ) { uint8 advState = TRUE; GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION ); GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState ); } } // if started else if (newState == GAPROLE_STARTED) { // Set the system ID from the bd addr uint8 systemId[DEVINFO_SYSTEM_ID_LEN]; GAPRole_GetParameter(GAPROLE_BD_ADDR, systemId); // shift three bytes up systemId[7] = systemId[5]; systemId[6] = systemId[4]; systemId[5] = systemId[3]; // set middle bytes to zero systemId[4] = 0; systemId[3] = 0; DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId); } gapProfileState = newState; }
/********************************************************************* * @fn sensor_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void sensor_HandleKeys( uint8 shift, uint8 keys ) { if ( keys == ( HAL_KEY_SW_1 | HAL_KEY_SW_2 ) ) { // Reset in progress has started resetInProgress = TRUE; // Set OSAL timer for reset osal_start_timerEx( sensor_TaskID, RSC_RESET_EVT, RSC_RESET_DELAY ); } else if ( keys & HAL_KEY_SW_1 ) { if ( resetInProgress == TRUE ) { // Cancel the reset resetInProgress = FALSE; osal_stop_timerEx ( sensor_TaskID, RSC_RESET_EVT ); } // set simulated measurement flag index if (++sensorFlagsIdx == FLAGS_IDX_MAX) { sensorFlagsIdx = 0; } } else if ( keys & HAL_KEY_SW_2 ) { if ( resetInProgress == TRUE ) { // Cancel the reset resetInProgress = FALSE; osal_stop_timerEx ( sensor_TaskID, RSC_RESET_EVT ); } // if not in a connection, toggle advertising on and off if ( gapProfileState != GAPROLE_CONNECTED ) { uint8 status; // Set fast advertising interval for user-initiated connections GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_MIN, DEFAULT_WHITE_LIST_ADV_DURATION ); // toggle GAP advertisement status GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, &status ); status = !status; // If not already using white list, begin to do so. // Only do so if about to begin advertising if ( USING_WHITE_LIST && status == TRUE ) { uint8 bondCount = 0; GAPBondMgr_GetParameter( GAPBOND_BOND_COUNT, &bondCount ); if ((sensorUsingWhiteList == FALSE) && (bondCount > 0) ) { uint8 value = GAP_FILTER_POLICY_WHITE; GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof( uint8 ), &value); sensorUsingWhiteList = TRUE; } } GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &status ); // Set state variable if (status == FALSE) { sensorAdvCancelled = TRUE; } } else if ( gapProfileState == GAPROLE_CONNECTED ) { // If connected, change rate of motion if( (++motionIdx) == MOTION_IDX_MAX ) { motionIdx = 0; } motion = motionCycle[motionIdx]; } } // end of key press else if ( keys == 0x00 ) { if ( resetInProgress == TRUE ) { resetInProgress = FALSE; osal_stop_timerEx( sensor_TaskID, RSC_RESET_EVT ); } } }
/********************************************************************* * @fn RunningSensor_processStateChangeEvt * * @brief Notification from the profile of a state change. * * @param newState - new state * * @return none */ static void RunningSensor_processStateChangeEvt(gaprole_States_t newState) { // If connected if (newState == GAPROLE_CONNECTED) { // Get the connection handle. GAPRole_GetParameter(GAPROLE_CONNHANDLE, &gapConnHandle); // Begin delay for service discovery. Util_startClock(¶mUpdateClock); } // If disconnected else if (gapProfileState == GAPROLE_CONNECTED && newState != GAPROLE_CONNECTED) { uint8_t advState = TRUE; uint8_t bondCount = 0; // Stop periodic measurement. Util_stopClock(&periodicClock); // Get the bond count. GAPBondMgr_GetParameter(GAPBOND_BOND_COUNT, &bondCount); // If enabled and previously bonded, turn on white list if not already done. if(USING_WHITE_LIST && sensorUsingWhiteList == FALSE && bondCount > 0) { uint8_t value = GAP_FILTER_POLICY_WHITE; GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof(uint8_t), &value); sensorUsingWhiteList = TRUE; } // If waiting after a connection timeout if (newState == GAPROLE_WAITING_AFTER_TIMEOUT) { // Link loss timeout-- use fast advertising. GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_WHITE_LIST_ADV_DURATION); } else { // Use slow advertising. GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_WHITE_LIST_ADV_DURATION); } // Enable advertising. GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advState); } // If advertising stopped else if (gapProfileState == GAPROLE_ADVERTISING && newState == GAPROLE_WAITING) { uint8_t whiteListUsed = FALSE; // If white list is in use, turn off white list filtering to allow general // access. if (sensorUsingWhiteList == TRUE) { uint8_t value = GAP_FILTER_POLICY_ALL; GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof(uint8_t), &value); whiteListUsed = TRUE; sensorUsingWhiteList = FALSE; } // If advertising stopped by user if (sensorAdvCancelled) { sensorAdvCancelled = FALSE; } // If fast advertising was interrupted to cancel white list else if (((!USING_WHITE_LIST) || whiteListUsed) && (GAP_GetParamValue(TGAP_GEN_DISC_ADV_INT_MIN) == DEFAULT_FAST_ADV_INTERVAL)) { uint8_t advState = TRUE; GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_FAST_ADV_DURATION); GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advState); } // If fast advertising or was using white list switch to slow advertising. else if (((!USING_WHITE_LIST) || whiteListUsed) || (GAP_GetParamValue(TGAP_GEN_DISC_ADV_INT_MIN) == DEFAULT_FAST_ADV_INTERVAL)) { uint8_t advState = TRUE; GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_SLOW_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_SLOW_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_SLOW_ADV_DURATION); GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advState); } } // If started else if (newState == GAPROLE_STARTED) { // Set the system ID from the bd addr. uint8_t systemId[DEVINFO_SYSTEM_ID_LEN]; GAPRole_GetParameter(GAPROLE_BD_ADDR, systemId); // Shift three bytes up. systemId[7] = systemId[5]; systemId[6] = systemId[4]; systemId[5] = systemId[3]; // Set middle bytes to zero. systemId[4] = 0; systemId[3] = 0; DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId); } // Update state. gapProfileState = newState; }
/********************************************************************* * @fn RunningSensor_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * KEY_SELECT * KEY_UP * KEY_RIGHT * * @return none */ static void RunningSensor_handleKeys(uint8_t shift, uint8_t keys) { // If a reset was in progress, cancel it. if (resetInProgress == TRUE) { resetInProgress = FALSE; Util_stopClock(&resetClock); #if USING_NEGLECT_TIMEOUT // If using neglect time and in a connection. if (gapProfileState == GAPROLE_CONNECTED) { // Restart neglect timer. Util_startClock(&neglectClock); } #endif //USING_NEGLECT_TIMEOUT return; } if (keys & KEY_SELECT) { // Reset in progress has started. resetInProgress = TRUE; #if USING_NEGLECT_TIMEOUT // Stop the neglect timer. Util_stopClock(&neglectClock); #endif //USING_NEGLECT_TIMEOUT // Start reset timer. Util_startClock(&resetClock); } else if (keys & KEY_UP) { // Set simulated measurement flag index. if (++sensorFlagsIdx == FLAGS_IDX_MAX) { sensorFlagsIdx = 0; } } else if (keys & KEY_RIGHT) { // If not in a connection, toggle advertising on and off. if (gapProfileState != GAPROLE_CONNECTED) { uint8_t advStatus; // Set fast advertising interval for user-initiated connections. GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, DEFAULT_FAST_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, DEFAULT_FAST_ADV_INTERVAL); GAP_SetParamValue(TGAP_GEN_DISC_ADV_MIN, DEFAULT_WHITE_LIST_ADV_DURATION); // Toggle GAP advertisement status. GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED, &advStatus); advStatus = !advStatus; // If not already using white list, begin to do so. // Only do so if about to begin advertising. if (USING_WHITE_LIST && advStatus == TRUE) { uint8_t bondCount = 0; GAPBondMgr_GetParameter(GAPBOND_BOND_COUNT, &bondCount); if ((sensorUsingWhiteList == FALSE) && (bondCount > 0)) { uint8_t value = GAP_FILTER_POLICY_WHITE; GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof(uint8_t), &value); sensorUsingWhiteList = TRUE; } } GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advStatus); // Set state variable. if (advStatus == FALSE) { sensorAdvCancelled = TRUE; } } else if (gapProfileState == GAPROLE_CONNECTED) { // If connected, change rate of motion. if(++motionIdx == MOTION_IDX_MAX) { motionIdx = 0; } motion = motionCycle[motionIdx]; } } }