/*********************************************************************
 * @fn      CyclingSensor_handleResetEvt
 *
 * @brief   "soft" resets the device.  This puts the device into a waiting 
 *           state, clears all white list, bonding and GATT service handle 
 *           information about previously previously connected devices.
 *
 * @param   none
 *
 * @return  none
 */
static void CyclingSensor_handleResetEvt(void)
{
  static uint8_t isWLClear = FALSE;
  
  if (gapProfileState == GAPROLE_CONNECTED)
  { 
    // Exit the connection.
    GAPRole_TerminateConnection();
  }
  else if (gapProfileState == GAPROLE_ADVERTISING)
  {
    uint8_t value = FALSE;

    // Turn off advertising.
    GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &value);
  }
  else if (USING_WHITE_LIST == TRUE && isWLClear == FALSE)
  {
    // Set internal white list flag to true.
    isWLClear = TRUE;
    
    // Disable white list use with advertising.
    sensorUsingWhiteList = FALSE;
    
    // Temporary variable.
    uint8_t value = GAP_FILTER_POLICY_ALL;

    // Turn off white list filter policy.
    GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof(uint8_t), &value);

    // Clear the white list.
    HCI_LE_ClearWhiteListCmd();
  }
  else if ((gapProfileState == GAPROLE_STARTED) ||
            (gapProfileState == GAPROLE_WAITING) ||
            (gapProfileState == GAPROLE_WAITING_AFTER_TIMEOUT))
  {
    uint8_t eraseBonds = TRUE;

    // Stop the periodic expirations of the reset clock.
    Util_stopClock(&resetClock);
    
    // Set internal white list flag to false for next reset event.
    isWLClear = FALSE;
    
    // Erase all bonds.
    GAPBondMgr_SetParameter(GAPBOND_ERASE_ALLBONDS, 0, &eraseBonds);
    
    // Turn on GREEN LED for set time.
    //HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK);
  }  
}
예제 #2
0
/*********************************************************************
 * @fn      RunningSensor_ProcessEvent
 *
 * @brief   Running Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  events not processed
 */
uint16 RunningSensor_ProcessEvent( uint8 task_id, uint16 events )
{

  VOID task_id; // OSAL required parameter that isn't used in this function

  if ( events & SYS_EVENT_MSG )
  {
    uint8 *pMsg;

    if ( (pMsg = osal_msg_receive( sensor_TaskID )) != NULL )
    {
      sensor_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );

      // Release the OSAL message
      VOID osal_msg_deallocate( pMsg );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

  if ( events & START_DEVICE_EVT )
  {
    // Start the Device
    VOID GAPRole_StartDevice( &runningPeripheralCB );

    // Register with bond manager after starting device
    GAPBondMgr_Register( (gapBondCBs_t *) &runningBondCB );

    return ( events ^ START_DEVICE_EVT );
  }

  if ( events & RSC_PERIODIC_EVT )
  {
    // Perform periodic sensor's periodic task
    sensorPeriodicTask();

    return (events ^ RSC_PERIODIC_EVT);
  }

  if ( events & RSC_CONN_PARAM_UPDATE_EVT )
  {
    // Send param update.  If it fails, retry until successful.
    GAPRole_SendUpdateParam( DEFAULT_DESIRED_MIN_CONN_INTERVAL, DEFAULT_DESIRED_MAX_CONN_INTERVAL,
                             DEFAULT_DESIRED_SLAVE_LATENCY, DEFAULT_DESIRED_CONN_TIMEOUT,
                             GAPROLE_RESEND_PARAM_UPDATE );

    // Assuming service discovery complete, start neglect timer
    if ( USING_NEGLECT_TIMEOUT )
    {
      osal_start_timerEx( sensor_TaskID, RSC_NEGLECT_TIMEOUT_EVT, NEGLECT_TIMEOUT_DELAY );
    }

    return (events ^ RSC_CONN_PARAM_UPDATE_EVT);
  }

  if ( events & RSC_NEGLECT_TIMEOUT_EVT )
  {
    // No user input, terminate connection.
    GAPRole_TerminateConnection();

    return ( events ^ RSC_NEGLECT_TIMEOUT_EVT );
  }

  if ( events & RSC_RESET_EVT )
  {
     if ( gapProfileState == GAPROLE_CONNECTED )
     {
       // Exit the connection
       GAPRole_TerminateConnection();

       // There is no callback for manual termination of the link.  change state variable here.
       gapProfileState = GAPROLE_WAITING;

       // Set timer to give the end advertising event time to finish
       osal_start_timerEx( sensor_TaskID, RSC_RESET_EVT, 500 );
     }
     else if ( gapProfileState == GAPROLE_ADVERTISING )
     {
       uint8 value = FALSE;

       // Turn off advertising
       GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &value );

       // Set timer to give the end advertising event time to finish
       osal_start_timerEx( sensor_TaskID, RSC_RESET_EVT, 500 );
     }
     else if ( USING_WHITE_LIST == TRUE )
     {
       //temporary variable
       uint8 value = GAP_FILTER_POLICY_ALL;

       // Turn off white list filter policy
       GAPRole_SetParameter(GAPROLE_ADV_FILTER_POLICY, sizeof( uint8 ), &value);

       sensorUsingWhiteList = FALSE;

       // Clear the white list
       HCI_LE_ClearWhiteListCmd();

       // Set timer to give the end advertising event time to finish
       osal_start_timerEx( sensor_TaskID, RSC_RESET_EVT, 500 );
     }
     else if ( (gapProfileState == GAPROLE_STARTED) ||
               (gapProfileState == GAPROLE_WAITING) ||
               (gapProfileState == GAPROLE_WAITING_AFTER_TIMEOUT) )
     {
       uint8 eraseBonds = TRUE;

       // Erase all bonds
       GAPBondMgr_SetParameter( GAPBOND_ERASE_ALLBONDS, 0, &eraseBonds );

       // Turn on GREEN LED for set time
       HalLedSet( HAL_LED_1, HAL_LED_MODE_BLINK );

       return (events ^ RSC_RESET_EVT);
     }
  }

  // Discard unknown events
  return 0;
}