static void simpleBLECentral_HandleKeys( uint8 shift, uint8 keys )
{
  (void)shift;  // Intentionally unreferenced parameter

  if ( keys & HAL_KEY_UP )
  {
    // Start or stop discovery
    if ( simpleBLEState != BLE_STATE_CONNECTED )
    {
      if ( !simpleBLEScanning )
      {
        simpleBLEScanning = TRUE;
        simpleBLEScanRes = 0;
        
        LCD_WRITE_STRING( "Discovering...", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
        
        GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
                                       DEFAULT_DISCOVERY_ACTIVE_SCAN,
                                       DEFAULT_DISCOVERY_WHITE_LIST );      
      }
      else
      {
        GAPCentralRole_CancelDiscovery();
      }
    }
    else if ( simpleBLEState == BLE_STATE_CONNECTED &&
              simpleBLECharHdl != 0 &&
              simpleBLEProcedureInProgress == FALSE )
    {
      uint8 status;
      
      // Do a read or write as long as no other read or write is in progress
      if ( simpleBLEDoWrite )
      {
        // Do a write
        attWriteReq_t req;
        
        req.handle = simpleBLECharHdl;
        req.len = 1;
        req.value[0] = simpleBLECharVal;
        req.sig = 0;
        req.cmd = 0;
        status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );         
      }
      else
      {
        // Do a read
        attReadReq_t req;
        
        req.handle = simpleBLECharHdl;
        status = GATT_ReadCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );
      }
      
      if ( status == SUCCESS )
      {
        simpleBLEProcedureInProgress = TRUE;
        simpleBLEDoWrite = !simpleBLEDoWrite;
      }
    }    
  }

  if ( keys & HAL_KEY_LEFT )
  {
    // Display discovery results
    if ( !simpleBLEScanning && simpleBLEScanRes > 0 )
    {
        // Increment index of current result (with wraparound)
        simpleBLEScanIdx++;
        if ( simpleBLEScanIdx >= simpleBLEScanRes )
        {
          simpleBLEScanIdx = 0;
        }
        
        LCD_WRITE_STRING_VALUE( "Device", simpleBLEScanIdx + 1,
                                10, HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( simpleBLEDevList[simpleBLEScanIdx].addr ),
                          HAL_LCD_LINE_2 );
    }
  }

  if ( keys & HAL_KEY_RIGHT )
  {
    // Connection update
    if ( simpleBLEState == BLE_STATE_CONNECTED )
    {
      GAPCentralRole_UpdateLink( simpleBLEConnHandle,
                                 DEFAULT_UPDATE_MIN_CONN_INTERVAL,
                                 DEFAULT_UPDATE_MAX_CONN_INTERVAL,
                                 DEFAULT_UPDATE_SLAVE_LATENCY,
                                 DEFAULT_UPDATE_CONN_TIMEOUT );
    }
  }
  
  if ( keys & HAL_KEY_CENTER )
  {
    uint8 addrType;
    uint8 *peerAddr;
    
    // Connect or disconnect
    if ( simpleBLEState == BLE_STATE_IDLE )
    {
      // if there is a scan result
      if ( simpleBLEScanRes > 0 )
      {
        // connect to current device in scan result
        peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
        addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
      
        simpleBLEState = BLE_STATE_CONNECTING;
        
        GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
                                      DEFAULT_LINK_WHITE_LIST,
                                      addrType, peerAddr );
  
        LCD_WRITE_STRING( "Connecting", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( peerAddr ), HAL_LCD_LINE_2 ); 
      }
    }
    else if ( simpleBLEState == BLE_STATE_CONNECTING ||
              simpleBLEState == BLE_STATE_CONNECTED )
    {
      // disconnect
      simpleBLEState = BLE_STATE_DISCONNECTING;

      gStatus = GAPCentralRole_TerminateLink( simpleBLEConnHandle );
      
      LCD_WRITE_STRING( "Disconnecting", HAL_LCD_LINE_1 ); 
    }
  }
  
  if ( keys & HAL_KEY_DOWN )
  {
    // Start or cancel RSSI polling
    if ( simpleBLEState == BLE_STATE_CONNECTED )
    {
      if ( !simpleBLERssi )
      {
        simpleBLERssi = TRUE;
        GAPCentralRole_StartRssi( simpleBLEConnHandle, DEFAULT_RSSI_PERIOD );
      }
      else
      {
        simpleBLERssi = FALSE;
        GAPCentralRole_CancelRssi( simpleBLEConnHandle );
        
        LCD_WRITE_STRING( "RSSI Cancelled", HAL_LCD_LINE_1 );
      }
    }
  }
}
/*********************************************************************
 * @fn      simpleBLECentralRssiCB
 *
 * @brief   RSSI callback.
 *
 * @param   connHandle - connection handle
 * @param   rssi - RSSI
 *
 * @return  none
 */
static void simpleBLECentralRssiCB( uint16 connHandle, int8 rssi )
{
    LCD_WRITE_STRING_VALUE( "RSSI -dB:", (uint8) (-rssi), 10, HAL_LCD_LINE_1 );
}
Exemplo n.º 3
0
/*********************************************************************
 * @fn      timeAppIndGattMsg
 *
 * @brief   Handle indications and notifications. 
 *
 * @param   pMsg - GATT message.
 *
 * @return  none
 */
void timeAppIndGattMsg( gattMsgEvent_t *pMsg )
{
  uint8 i;
  
  // Look up the handle in the handle cache
  for ( i = 0; i < HDL_CACHE_LEN; i++ )
  {
    if ( pMsg->msg.handleValueNoti.handle == timeAppHdlCache[i] )
    {
      break;
    }
  }

  // Perform processing for this handle 
  switch ( i )
  {
    case HDL_CURR_TIME_CT_TIME_START:
      // Set clock to time read from time server
      timeAppClockSet( pMsg->msg.handleValueNoti.value );
      break;
      
    case HDL_NWA_NWA_START:             
      // Display network availability state
      if ( pMsg->msg.handleValueInd.value[0] == 1 )
      {
        LCD_WRITE_STRING( "Network: Yes", HAL_LCD_LINE_1 );
      }
      else
      {
        LCD_WRITE_STRING( "Network: None", HAL_LCD_LINE_1 );
      }
      break;
      
    case HDL_ALERT_NTF_UNREAD_START:
      // Display unread message alerts
      {
        uint8 *p = pMsg->msg.handleValueNoti.value;
        uint8 len = pMsg->msg.handleValueNoti.len;
        uint8 line;
        
        for ( line = 1; line <= 2 && len >= 2; line++, len -= 2 )
        {
          if ( ALERT_MAJOR_CAT(p[0]) <= ALERT_CAT_ID_MAX )
          {
            LCD_WRITE_STRING_VALUE( (char *) timeAppAlertCatStr[ALERT_MAJOR_CAT(p[0])],
                                    p[1], 10, line );
          }
          p += 2;
        }
      }
      break;
      
    case HDL_ALERT_NTF_INCOM_START:
      // Display incoming message
      timeAppDisplayAlert( pMsg->msg.handleValueNoti.value,
                           pMsg->msg.handleValueNoti.len );
      break;

    case HDL_BATT_LEVEL_STATE_START:
      // Display battery level
      LCD_WRITE_STRING_VALUE( "Battery%", pMsg->msg.handleValueNoti.value[0], 10, HAL_LCD_LINE_2 );
      break;
        
     default:
      break;
  }
  
  // Send confirm for indication
  if ( pMsg->method == ATT_HANDLE_VALUE_IND )
  {
    ATT_HandleValueCfm( pMsg->connHandle );
  }
}
Exemplo n.º 4
0
/*********************************************************************
 * @fn      timeApp_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 timeApp_HandleKeys( uint8 shift, uint8 keys )
{
  if ( keys & HAL_KEY_UP )
  {
    // Start or stop advertising
    if ( timeAppGapState != GAPROLE_CONNECTED )
    {
      uint8 advState;
      
      // 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_FAST_ADV_DURATION );

      // Toggle advertising state      
      GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, &advState );
      advState = !advState;
      GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &advState );
      
      // Set state variable
      if (advState == FALSE)
      {
        timeAppAdvCancelled = TRUE;
      }
    }
  }

  if ( keys & HAL_KEY_LEFT )
  {
    attWriteReq_t req;

    // Send command to alert notificaton control point
    if ( ( timeAppGapState == GAPROLE_CONNECTED ) &&
         ( timeAppHdlCache[HDL_ALERT_NTF_CTRL] != 0 ) )
    {
      // Send write request
      req.len = 2;
      req.value[0] = *pTimeAppAlertCmd;
      req.value[1] = ALERT_NOTIF_CAT_ALL;
      req.sig = 0;
      req.cmd = 0;
      req.handle = timeAppHdlCache[HDL_ALERT_NTF_CTRL];
      GATT_WriteCharValue( timeAppConnHandle, &req, timeAppTaskId );
      LCD_WRITE_STRING_VALUE( "Alert cmd:", *pTimeAppAlertCmd, 10, HAL_LCD_LINE_1);
    }
    
    // Cycle through command test values
    if ( *pTimeAppAlertCmd == ALERT_NOTIF_DISABLE_UNREAD )
    {
      pTimeAppAlertCmd = timeAppAlertCmd;
    }
    else
    {
      pTimeAppAlertCmd++;
    }
  }

  if ( keys & HAL_KEY_RIGHT )
  {
    attWriteReq_t req;
    
    // Do a reference time update
    if ( ( timeAppGapState == GAPROLE_CONNECTED ) &&
         ( timeAppHdlCache[HDL_REF_TIME_UPD_CTRL] != 0 ) )
    {
      // Send write command
      req.len = 1;
      req.value[0] = timeAppRefUpdateVal;
      req.sig = 0;
      req.cmd = 1;
      req.handle = timeAppHdlCache[HDL_REF_TIME_UPD_CTRL];
      GATT_WriteNoRsp( timeAppConnHandle, &req );

      LCD_WRITE_STRING_VALUE( "Time update:", timeAppRefUpdateVal, 10, HAL_LCD_LINE_1);
      
      // Toggle between two reference time update values
      if ( timeAppRefUpdateVal == REF_TIME_UPDATE_GET )
      {
        timeAppRefUpdateVal = REF_TIME_UPDATE_CANCEL;
      }
      else
      {
        timeAppRefUpdateVal = REF_TIME_UPDATE_GET;
      }
    }
  }
  
  if ( keys & HAL_KEY_CENTER )
  {
    // If connected, terminate connection
    if ( timeAppGapState == GAPROLE_CONNECTED )
    {
      GAPRole_TerminateConnection();
    }
  }
  
  if ( keys & HAL_KEY_DOWN )
  {
    attWriteReq_t req;
    
    // Write ringer control point
    if ( ( timeAppGapState == GAPROLE_CONNECTED ) &&
         ( timeAppHdlCache[HDL_PAS_CTRL] != 0 ) )
    {
      // Send write command
      req.len = 1;
      req.value[0] = timeAppRingerCmd;
      req.sig = 0;
      req.cmd = 1;
      req.handle = timeAppHdlCache[HDL_PAS_CTRL];
      GATT_WriteNoRsp( timeAppConnHandle, &req );

      LCD_WRITE_STRING_VALUE( "Ringer ctrl:", timeAppRingerCmd, 10, HAL_LCD_LINE_1);
      
      // Toggle between values
      if ( ++timeAppRingerCmd > RINGER_CANCEL_SILENT )
      {
        timeAppRingerCmd = RINGER_SILENT_MODE;
      }
    }
  }
  
  if ( keys & HAL_KEY_SW_6 )
  {

  }

}
Exemplo n.º 5
0
static void simpleBLECentral_HandleKeys( uint8 shift, uint8 keys )
{
  (void)shift;  // Intentionally unreferenced parameter

  if ( keys & HAL_KEY_UP )
  {
    // Start or stop discovery
    if ( simpleBLEState != BLE_STATE_CONNECTED )
    {
      if ( !simpleBLEScanning )
      {  uint8 adc7 = HalAdcRead (HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_8); 
    LCD_WRITE_STRING_VALUE( "Value",adc7,
                                16, HAL_LCD_LINE_2);
    advertData[6]= var1[6];
    advertData[8]= 0xA9;
    if(adc7>0x7E)
       advertData[9]= 0x01;
    else 
        advertData[9]= 0x02;
    advertData[10]= 0xB9;
    GAP_UpdateAdvertisingData( GAPROLE_ADVERT_ENABLED,TRUE, sizeof(advertData),advertData);
        simpleBLEScanning = TRUE;
        simpleBLEScanRes = 0;
        
        LCD_WRITE_STRING( "Discovering...", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
        
        GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
                                       DEFAULT_DISCOVERY_ACTIVE_SCAN,
                                       DEFAULT_DISCOVERY_WHITE_LIST );      
      }
      else
      {
        GAPCentralRole_CancelDiscovery();
      }
    }
    else if ( simpleBLEState == BLE_STATE_CONNECTED &&
              simpleBLECharHdl != 0 &&
              simpleBLEProcedureInProgress == FALSE )
    {
      uint8 status;
      
      // Do a read or write as long as no other read or write is in progress
      if ( simpleBLEDoWrite )
      {
        // Do a write
        attWriteReq_t req;
        
        req.handle = simpleBLECharHdl;
        req.len = 1;
        req.value[0] = simpleBLECharVal;
        req.sig = 0;
        req.cmd = 0;
        status = GATT_WriteCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );         
      }
      else
      {
        // Do a read
        attReadReq_t req;
        
        req.handle = simpleBLECharHdl;
        status = GATT_ReadCharValue( simpleBLEConnHandle, &req, simpleBLETaskId );
      }
      
      if ( status == SUCCESS )
      {
        simpleBLEProcedureInProgress = TRUE;
        simpleBLEDoWrite = !simpleBLEDoWrite;
      }
    }    
  }

  if ( keys & HAL_KEY_LEFT )
  {
    // Display discovery results
    if ( !simpleBLEScanning && simpleBLEScanRes > 0 )
    {
        // Increment index of current result (with wraparound)
        simpleBLEScanIdx++;
        if ( simpleBLEScanIdx >= simpleBLEScanRes )
        {
          simpleBLEScanIdx = 0;
        }
        
        LCD_WRITE_STRING_VALUE( "Device", simpleBLEScanIdx + 1,
                                10, HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( simpleBLEDevList[simpleBLEScanIdx].addr ),
                          HAL_LCD_LINE_2 );
    }
  }
////////////////////////////////////////////////////////////////////////////////
// Advertising
  if ( keys & HAL_KEY_RIGHT )
  {
    // ressing the right key should toggle advertising on and off
    uint8 current_adv_enabled_status;
    uint8 new_adv_enabled_status;
    uint8 adc7 = HalAdcRead (HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_8); 
    LCD_WRITE_STRING_VALUE( "Value",adc7,
                                16, HAL_LCD_LINE_2);
    advertData[6]= var1[6];
    advertData[8]= 0xA9;
    if(adc7>0x7E)
       advertData[9]= 0x01;
    else 
        advertData[9]= 0x02;
    advertData[10]= 0xB9;
    GAP_UpdateAdvertisingData( GAPROLE_ADVERT_ENABLED,TRUE, sizeof(advertData),advertData);
    //Find the current GAP advertisement status
    
    
    if( current_adv_enabled_status == FALSE )
    {
      new_adv_enabled_status = TRUE;
    }
    else
    {
      new_adv_enabled_status = FALSE;
    }
   // if(var1[2] == 0x53) {
    //scanRspData[18] =0xAA;  
    //scanRspData[19] =(uint8) var1[2];
    //bStatus_t stat;
    //stat = GAP_UpdateAdvertisingData( 0, TRUE, 1, scanRspData); 
     //}
    
    
    //change the GAP advertisement status to opposite of current status
    GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &new_adv_enabled_status );
  }
////////////////////////////////////////////////////////////////////////////////
  
  /*if ( keys & HAL_KEY_CENTER )
  {
    uint8 addrType;
    uint8 *peerAddr;
    
    // Connect or disconnect
    if ( simpleBLEState == BLE_STATE_IDLE )
    {
      // if there is a scan result
      if ( simpleBLEScanRes > 0 )
      {
        // connect to current device in scan result
        peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
        addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
      
        simpleBLEState = BLE_STATE_CONNECTING;
        
        GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
                                      DEFAULT_LINK_WHITE_LIST,
                                      addrType, peerAddr );
  
        LCD_WRITE_STRING( "Connecting", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( peerAddr ), HAL_LCD_LINE_2 ); 
      }
    }
    else if ( simpleBLEState == BLE_STATE_CONNECTING ||
              simpleBLEState == BLE_STATE_CONNECTED )
    {
      // disconnect
      simpleBLEState = BLE_STATE_DISCONNECTING;

      gStatus = GAPCentralRole_TerminateLink( simpleBLEConnHandle );
      
      LCD_WRITE_STRING( "Disconnecting", HAL_LCD_LINE_1 ); 
    }
  }*/
  
  if ( keys & HAL_KEY_DOWN )
  {
    // Start or cancel RSSI polling
    if ( simpleBLEState == BLE_STATE_CONNECTED )
    {
      if ( !simpleBLERssi )
      {
        simpleBLERssi = TRUE;
        GAPCentralRole_StartRssi( simpleBLEConnHandle, DEFAULT_RSSI_PERIOD );
      }
      else
      {
        simpleBLERssi = FALSE;
        GAPCentralRole_CancelRssi( simpleBLEConnHandle );
        
        LCD_WRITE_STRING( "RSSI Cancelled", HAL_LCD_LINE_1 );
      }
    }
  }
}
Exemplo n.º 6
0
/*********************************************************************
 * @fn      simpleBLECentralEventCB
 *
 * @brief   Central event callback function.
 *
 * @param   pEvent - pointer to event structure
 *
 * @return  none
 */
static void simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
{
  switch ( pEvent->gap.opcode )
  {
    case GAP_DEVICE_INIT_DONE_EVENT:  

      {
       
        LCD_WRITE_STRING( "BLE Central", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( pEvent->initDone.devAddr ),  HAL_LCD_LINE_2 );
      }
      break;

    case GAP_DEVICE_INFO_EVENT:
      {
        var1= pEvent->deviceInfo.pEvtData;
        dataToPrint = var1[5];
        //LCD_WRITE_STRING( bdAddr2Str(var1), HAL_LCD_LINE_3 ); 
      if(dataToPrint == 0xA1)   
      {

         LCD_WRITE_STRING_VALUE( "Value",var1[6],
                                16, HAL_LCD_LINE_3);


       }

        // if filtering device discovery results based on service UUID
        if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
        {
          if ( simpleBLEFindSvcUuid( SIMPLEPROFILE_SERV_UUID,
                                     pEvent->deviceInfo.pEvtData,
                                     pEvent->deviceInfo.dataLen ) )
          {
            simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
          }
        }
      }
      break;
      
    case GAP_DEVICE_DISCOVERY_EVENT:
      {
        // discovery complete
        simpleBLEScanning = FALSE;

        // if not filtering device discovery results based on service UUID
        if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE )
        {
          // Copy results
          simpleBLEScanRes = pEvent->discCmpl.numDevs;
          osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList,
                       (sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );
        }
        
        LCD_WRITE_STRING_VALUE( "Devices Found", simpleBLEScanRes,
                                10, HAL_LCD_LINE_1 );
        if ( simpleBLEScanRes > 0 )
        {
          LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 );
          simpleBLEState = BLE_STATE_IDLE;
          simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
          simpleBLERssi = FALSE;
          simpleBLEDiscState = BLE_DISC_STATE_IDLE;

        }
        if ( simpleBLEState != BLE_STATE_CONNECTED )
    {
      if ( !simpleBLEScanning )
      {
        uint8 adc7 = HalAdcRead (HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_8); 
    LCD_WRITE_STRING_VALUE( "Value",adc7,
                                16, HAL_LCD_LINE_2);
    advertData[6]= var1[6];
    advertData[8]= 0xA9;
    if(adc7>0x7E)
       advertData[9]= 0x01;
    else 
        advertData[9]= 0x02;
    advertData[10]= 0xB9;
    GAP_UpdateAdvertisingData( GAPROLE_ADVERT_ENABLED,TRUE, sizeof(advertData),advertData);
        simpleBLEScanning = TRUE;
        simpleBLEScanRes = 0;
        
        LCD_WRITE_STRING( "Discovering...", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
        
        GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
                                       DEFAULT_DISCOVERY_ACTIVE_SCAN,
                                       DEFAULT_DISCOVERY_WHITE_LIST );      
      }
      else
      {
        GAPCentralRole_CancelDiscovery();

      }
    }

        // initialize scan index to last device
        simpleBLEScanIdx = simpleBLEScanRes;

      }
      break;

    case GAP_LINK_ESTABLISHED_EVENT:
      {
        if ( pEvent->gap.hdr.status == SUCCESS )
        {          
          simpleBLEState = BLE_STATE_CONNECTED;
          simpleBLEConnHandle = pEvent->linkCmpl.connectionHandle;
          simpleBLEProcedureInProgress = TRUE;    

          // If service discovery not performed initiate service discovery
          if ( simpleBLECharHdl == 0 )
          {
            osal_start_timerEx( simpleBLETaskId, START_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY );
          }

          LCD_WRITE_STRING( bdAddr2Str(var1), HAL_LCD_LINE_3 );          
          LCD_WRITE_STRING( "Connected", HAL_LCD_LINE_1 );
          LCD_WRITE_STRING( bdAddr2Str( pEvent->linkCmpl.devAddr ), HAL_LCD_LINE_2 );   
        }
        else
        {
          simpleBLEState = BLE_STATE_IDLE;
          simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
          simpleBLERssi = FALSE;
          simpleBLEDiscState = BLE_DISC_STATE_IDLE;
          
          LCD_WRITE_STRING( "Connect Failed", HAL_LCD_LINE_1 );
          LCD_WRITE_STRING_VALUE( "Reason:", pEvent->gap.hdr.status, 10, HAL_LCD_LINE_2 );
        }
      }
      break;

    case GAP_LINK_TERMINATED_EVENT:
      {
        simpleBLEState = BLE_STATE_IDLE;
        simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
        simpleBLERssi = FALSE;
        simpleBLEDiscState = BLE_DISC_STATE_IDLE;
        simpleBLECharHdl = 0;
        simpleBLEProcedureInProgress = FALSE;
          
        LCD_WRITE_STRING( "Disconnected", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING_VALUE( "Reason:", pEvent->linkTerminate.reason,
                                10, HAL_LCD_LINE_2 );
      }
      break;

    case GAP_LINK_PARAM_UPDATE_EVENT:
      {
        LCD_WRITE_STRING( "Connected Update", HAL_LCD_LINE_1 );
      }
      break;
      
    default:
      break;
  }
}
Exemplo n.º 7
0
/*********************************************************************
 * @fn      oadManagerEventCB
 *
 * @brief   Central event callback function.
 *
 * @param   pEvent - pointer to event structure
 *
 * @return  none
 */
static void oadManagerEventCB( gapCentralRoleEvent_t *pEvent )
{
  switch ( pEvent->gap.opcode )
  {
    case GAP_DEVICE_INIT_DONE_EVENT:
      {
        VOID osal_memcpy( oadManagerAddr, pEvent->initDone.devAddr, B_ADDR_LEN );

#if (defined HAL_LCD) && (HAL_LCD == TRUE)
        LCD_WRITE_STRING( "OAD Manager", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( oadManagerAddr ),  HAL_LCD_LINE_2 );
#endif
      }
      break;

    case GAP_DEVICE_INFO_EVENT:
      {
        // if filtering device discovery results based on service UUID
        if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
        {
          if ( oadManagerFindServUUID( pEvent->deviceInfo.pEvtData, pEvent->deviceInfo.dataLen ) )
          {
            oadManagerAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
          }
        }
      }
      break;

    case GAP_DEVICE_DISCOVERY_EVENT:
      {
        // discovery complete
        oadManagerScanning = FALSE;

        // if not filtering device discovery results based on service UUID
        if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE )
        {
          // Copy results
          oadManagerScanRes = pEvent->discCmpl.numDevs;
          osal_memcpy( oadManagerDevList, pEvent->discCmpl.pDevList,
                       (sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );
        }

#if (defined HAL_LCD) && (HAL_LCD == TRUE)
        LCD_WRITE_STRING_VALUE( "Devices Found", oadManagerScanRes, 10, HAL_LCD_LINE_1 );
        if ( oadManagerScanRes > 0 )
        {
          LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 );
        }
#endif
        // initialize scan index to last device
        oadManagerScanIdx = oadManagerScanRes;
      }
      break;

    case GAP_LINK_ESTABLISHED_EVENT:
      {
        if ( pEvent->gap.hdr.status == SUCCESS )
        {
          oadManagerState = BLE_STATE_CONNECTED;
          oadManagerConnHandle = pEvent->linkCmpl.connectionHandle;

          (void)osal_set_event(oadManagerTaskId, CONN_INTERVAL_EVT);

#if (defined HAL_LCD) && (HAL_LCD == TRUE)
          LCD_WRITE_STRING( "Connected", HAL_LCD_LINE_1 );
          LCD_WRITE_STRING( "", HAL_LCD_LINE_3 );
#endif
        }
        else
        {
          oadManagerState = BLE_STATE_IDLE;
          oadManagerConnHandle = GAP_CONNHANDLE_INIT;
        }
      }
      break;

    case GAP_LINK_TERMINATED_EVENT:
      {
        oadManagerState = BLE_STATE_IDLE;
        oadManagerConnHandle = GAP_CONNHANDLE_INIT;

#if (defined HAL_LCD) && (HAL_LCD == TRUE)
        LCD_WRITE_STRING( "OAD Manager", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( oadManagerAddr ),  HAL_LCD_LINE_2 );
        LCD_WRITE_STRING( "Disconnected", HAL_LCD_LINE_3 );
#endif
      }
      break;

    default:
      break;
  }
}
Exemplo n.º 8
0
/*********************************************************************
 * @fn      glucoseIndGattMsg
 *
 * @brief   Handle indications and notifications. 
 *
 * @param   pMsg - GATT message.
 *
 * @return  none
 */
void glucoseIndGattMsg( gattMsgEvent_t *pMsg )
{
  uint8 i;
  
  // Look up the handle in the handle cache
  for ( i = 0; i < HDL_CACHE_LEN; i++ )
  {
    if ( pMsg->msg.handleValueInd.handle == glucoseHdlCache[i] )
    {
      break;
    }
  }

  // Perform processing for this handle 
  switch ( i )
  {
    case HDL_GLUCOSE_START:     
      {
        uint8* p = pMsg->msg.handleValueNoti.value;

        // restart procedure timer
        if (glucCollWritePending == true)
        {
          osal_start_timerEx( glucCollTaskId, PROCEDURE_TIMEOUT_EVT, GLUCOSE_PROCEDURE_TIMEOUT );          
        }
        
        memset(&glucoseMeas, 0, sizeof(glucoseMeas));
        
        // Flags
        glucoseMeas.flags = *p++;
        
        // Sequence number
        glucoseMeas.seqNum = BUILD_UINT16(p[0], p[1]);
        LCD_WRITE_STRING_VALUE( "SeqNum:", glucoseMeas.seqNum, 10, HAL_LCD_LINE_1 );
        p += 2;
        
        // Base time
        memcpy(glucoseMeas.baseTime, p, 7);
        p += 7;
       
        // Time offset;
        if (glucoseMeas.flags & GLUCOSE_MEAS_FLAG_TIME_OFFSET)
        {
          glucoseMeas.timeOffset = BUILD_UINT16(p[0], p[1]);
          p += 2;
        }
        
        // Glucose concentration
        if(glucoseMeas.flags & GLUCOSE_MEAS_FLAG_CONCENTRATION)
        {
          glucoseMeas.concentration = BUILD_UINT16(p[0], p[1]);
          if(glucoseMeas.flags & GLUCOSE_MEAS_FLAG_UNITS)
            LCD_WRITE_STRING_VALUE( STR_MMOL_PER_L, glucoseMeas.concentration, 10, HAL_LCD_LINE_2 );
          else
            LCD_WRITE_STRING_VALUE( STR_MG_PER_DL, glucoseMeas.concentration, 10, HAL_LCD_LINE_2 );
          p += 2;
          
          // Type sample location
          glucoseMeas.typeSampleLocation = *p++;
        }
        
        // Sensor status annunciation
        if (glucoseMeas.flags & GLUCOSE_MEAS_FLAG_STATUS_ANNUNCIATION)
        {
          glucoseMeas.sensorStatus = BUILD_UINT16(p[0], p[1]);
          p += 2;
        }
      }
      break;
      
    case HDL_GLUCOSE_CONTEXT_START:  
      {
        uint8* p = pMsg->msg.handleValueNoti.value;

        // restart procedure timer
        if (glucCollWritePending == true)
        {
          osal_start_timerEx( glucCollTaskId, PROCEDURE_TIMEOUT_EVT, GLUCOSE_PROCEDURE_TIMEOUT );          
        }
        
        memset(&glucoseContext, 0, sizeof(glucoseContext));
        
        // Flags
        glucoseContext.flags = *p++;

        // Sequence number
        glucoseContext.seqNum = BUILD_UINT16(p[0], p[1]);
        p += 2;

        // Extended flags
        if(glucoseContext.flags & GLUCOSE_CONTEXT_FLAG_EXTENDED)
        {
          glucoseContext.extendedFlags = *p++;
        }
        
        // Carbohydrate
        if(glucoseContext.flags & GLUCOSE_CONTEXT_FLAG_CARBO)
        {
          // carbohydrate ID
          glucoseContext.carboId = *p++;
          
          // Carbohydrate
          glucoseContext.carboVal = BUILD_UINT16(p[0], p[1]);          
          p += 2;
        }
        
        // Meal
        if(glucoseContext.flags & GLUCOSE_CONTEXT_FLAG_MEAL)
        {
          glucoseContext.mealVal = *p++;
        }
        
        // Tester health
        if(glucoseContext.flags & GLUCOSE_CONTEXT_FLAG_TESTER_HEALTH)
        {
          glucoseContext.TesterHealthVal = *p++;
        }
        
        // Exercise
        if(glucoseContext.flags & GLUCOSE_CONTEXT_FLAG_EXERCISE)
        {
          // Duration
          glucoseContext.exerciseDuration = BUILD_UINT16(p[0], p[1]);              
          p += 2;
          
          // Intensity
          glucoseContext.exerciseIntensity = *p++;
        }
        
        // Medication
        if(glucoseContext.flags & GLUCOSE_CONTEXT_FLAG_MEDICATION)
        {
          // Medication ID
          glucoseContext.medId = *p++;

          // Medication
          glucoseContext.medVal = BUILD_UINT16(p[0], p[1]);  
          p += 2;
        }
        
        // HbA1c
        if(glucoseContext.flags & GLUCOSE_CONTEXT_FLAG_HbA1c)
        {
          glucoseContext.HbA1cVal = BUILD_UINT16(p[0], p[1]);             
          LCD_WRITE_STRING_VALUE( "HbA1c:",  glucoseContext.HbA1cVal, 10, HAL_LCD_LINE_3 );
          p += 2;
        }
        
      }
      break;
      
    case HDL_GLUCOSE_CTL_PNT_START:
      {
        uint8* pValue = pMsg->msg.handleValueInd.value;

        // stop procedure timer
        osal_stop_timerEx( glucCollTaskId, PROCEDURE_TIMEOUT_EVT );  
        
        if(pValue[0] == CTL_PNT_OP_NUM_RSP)
        {
          if(pMsg->msg.handleValueInd.len >= 3)
          {
            LCD_WRITE_STRING("Matching ",  HAL_LCD_LINE_1);
            LCD_WRITE_STRING( "Records:", HAL_LCD_LINE_2 );
            LCD_WRITE_STRING_VALUE("", BUILD_UINT16(pValue[2], pValue[3]), 10, HAL_LCD_LINE_3 );
          }
        }
        else if(pValue[0] == CTL_PNT_OP_REQ_RSP && glucCollClearPending)
        {
          glucCollClearPending = false;
          
          if(pMsg->msg.handleValueInd.len >= 3)
          {
            switch(pValue[3])
            {
            case CTL_PNT_RSP_SUCCESS:
              LCD_WRITE_STRING("Records",  HAL_LCD_LINE_1);
              LCD_WRITE_STRING("Cleared", HAL_LCD_LINE_2 );
              LCD_WRITE_STRING("", HAL_LCD_LINE_3 );
              break;
            case CTL_PNT_RSP_NO_RECORDS:
              LCD_WRITE_STRING("No Matching",  HAL_LCD_LINE_1);
              LCD_WRITE_STRING("Records", HAL_LCD_LINE_2 );
              LCD_WRITE_STRING("to Delete", HAL_LCD_LINE_3 );
              break;
            default:
              LCD_WRITE_STRING("Error:",  HAL_LCD_LINE_1);
              LCD_WRITE_STRING_VALUE("", pValue[3], 10, HAL_LCD_LINE_2 );  
              LCD_WRITE_STRING( "", HAL_LCD_LINE_3 );
              break;
            }
          }
        }
        else if(pValue[0] == CTL_PNT_OP_REQ_RSP)
        {
          if(pMsg->msg.handleValueInd.len >= 3)
          {
            switch(pValue[3])
            {
            case CTL_PNT_RSP_SUCCESS:
              break;
            case CTL_PNT_RSP_NO_RECORDS:
              LCD_WRITE_STRING("No Matching",  HAL_LCD_LINE_1);
              LCD_WRITE_STRING("Records", HAL_LCD_LINE_2 );
              LCD_WRITE_STRING("Found", HAL_LCD_LINE_3 );
              break;
            default:
              LCD_WRITE_STRING("Error:",  HAL_LCD_LINE_1);
              LCD_WRITE_STRING_VALUE("", pValue[3], 10, HAL_LCD_LINE_2 );  
              LCD_WRITE_STRING( "", HAL_LCD_LINE_3 );
              break;
            }
          }
        }
      }
      break;      
        
    default:
      break;
  }
  
  // Send confirm for indication
  if ( pMsg->method == ATT_HANDLE_VALUE_IND )
  {
    ATT_HandleValueCfm( pMsg->connHandle );
  }
  
}
Exemplo n.º 9
0
/*********************************************************************
 * @fn      oadManager_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 oadManager_HandleKeys( uint8 shift, uint8 keys )
{
  (void)shift;  // Intentionally unreferenced parameter

  if ( keys & HAL_KEY_UP )  // Start or stop discovery
  {
    if ( oadManagerState != BLE_STATE_CONNECTED )
    {
      if ( !oadManagerScanning )
      {
        (void)osal_set_event(oadManagerTaskId, DEV_DISCOVERY_EVT);
      }
      else
      {
        GAPCentralRole_CancelDiscovery();
      }
    }
  }

  if ( keys & HAL_KEY_LEFT )
  {
    // Display discovery results
    if ( !oadManagerScanning && oadManagerScanRes > 0 )
    {
      // Increment index of current result (with wraparound)
      oadManagerScanIdx++;
      if ( oadManagerScanIdx >= oadManagerScanRes )
      {
        oadManagerScanIdx = 0;
      }

#if (defined HAL_LCD) && (HAL_LCD == TRUE)
      LCD_WRITE_STRING_VALUE( "Device", oadManagerScanIdx + 1, 10, HAL_LCD_LINE_1 );
      LCD_WRITE_STRING( bdAddr2Str( oadManagerDevList[oadManagerScanIdx].addr ), HAL_LCD_LINE_2 );
#endif
    }
  }

  if ( keys & HAL_KEY_RIGHT )
  {
  }

  if ( keys & HAL_KEY_CENTER )
  {
    uint8 addrType;
    uint8 *peerAddr;

    // Connect or disconnect
    if ( oadManagerState == BLE_STATE_IDLE )
    {
      // if there is a scan result
      if ( oadManagerScanRes > 0 )
      {
        if ( oadManagerScanIdx < oadManagerScanRes )
        {
          // connect to current device in scan result
          peerAddr = oadManagerDevList[oadManagerScanIdx].addr;
          addrType = oadManagerDevList[oadManagerScanIdx].addrType;

          oadManagerState = BLE_STATE_CONNECTING;

          GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
                                        DEFAULT_LINK_WHITE_LIST,
                                        addrType, peerAddr );

#if (defined HAL_LCD) && (HAL_LCD == TRUE)
          LCD_WRITE_STRING( "Connecting", HAL_LCD_LINE_1 );
          LCD_WRITE_STRING( bdAddr2Str( peerAddr ), HAL_LCD_LINE_2 );
        }
        else
        {
          LCD_WRITE_STRING( "Select device", HAL_LCD_LINE_3 );
#endif
        }
      }
    }
    else if ( oadManagerState == BLE_STATE_CONNECTING || oadManagerState == BLE_STATE_CONNECTED )
    {
      // disconnect
      oadManagerState = BLE_STATE_DISCONNECTING;

      VOID GAPCentralRole_TerminateLink( oadManagerConnHandle );

#if (defined HAL_LCD) && (HAL_LCD == TRUE)
      LCD_WRITE_STRING( "Disconnecting", HAL_LCD_LINE_1 );
      LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
      LCD_WRITE_STRING( "", HAL_LCD_LINE_3 );
#endif
    }
  }

  if ( keys & HAL_KEY_DOWN )
  {
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
    LCD_WRITE_STRING("SBL Mode...", HAL_LCD_LINE_3);
#endif
    EA = 0;
    sblRun();
    EA = 1;
#if (defined HAL_LCD) && (HAL_LCD == TRUE)
    LCD_WRITE_STRING("SBL Done.", HAL_LCD_LINE_3);
#endif
  }
}
/*********************************************************************
 * @fn      HostTestApp_init
 *
 * @brief   Called during initialization and contains application
 *          specific initialization (ie. hardware initialization/setup,
 *          table initialization, power up notification, etc), and
 *          profile initialization/setup.
 *
 * @param   none
 *
 * @return  none
 */
static void HostTestApp_init(void)
{
  // Register the current thread as an ICall dispatcher application
  // so that the application can send and receive messages.
  ICall_registerApp(&selfEntity, &sem);

  // Set device's Sleep Clock Accuracy
  //HCI_EXT_SetSCACmd(40);

  Board_openLCD();

  // Register for unprocessed HCI/Host event messages
  GAP_RegisterForMsgs(selfEntity);

  // Initialize GATT Client
  VOID GATT_InitClient();

  // Get build revision
  VOID Util_buildRevision(&buildRev);
  
#if !defined ( GATT_DB_OFF_CHIP )

  #if defined ( GATT_QUAL )
    VOID GATTQual_AddService( GATT_ALL_SERVICES ); // Includes GAP and GATT Services
  #else
    // Add our services to GATT Server
    VOID GGS_AddService( GATT_ALL_SERVICES );
    VOID GATTServApp_AddService( GATT_ALL_SERVICES );
    #if defined ( GATT_TEST )
      VOID GATTTest_AddService( GATT_ALL_SERVICES );
    #endif
  #endif

  // Set device name
  if ((buildRev.hostInfo & CENTRAL_CFG) && (buildRev.hostInfo & PERIPHERAL_CFG))
  {
    memcpy(deviceName, "TI BLE All", 10);
  }
  else if (buildRev.hostInfo & CENTRAL_CFG)
  {
    memcpy(deviceName, "TI BLE Central", 14);
  }
  else if (buildRev.hostInfo & PERIPHERAL_CFG)
  {
    memcpy(deviceName, "TI BLE Peripheral",  17);
  }
  else
  {
    memcpy(deviceName, "TI BLE Unknown",  14);
  }

  VOID GGS_SetParameter(GGS_DEVICE_NAME_ATT, strlen((char *)deviceName), deviceName);
  VOID GGS_SetParameter(GGS_APPEARANCE_ATT, sizeof(uint16), (void*)&appearance);

#endif // GATT_DB_OFF_CHIP

  LCD_WRITE_STRING("TI BLEv2.0", LCD_PAGE0);
  LCD_WRITE_STRING("HostTestApp", LCD_PAGE1);

  // Display Host build configuration
  if ((buildRev.hostInfo & CENTRAL_CFG) && (buildRev.hostInfo & PERIPHERAL_CFG))
  {
    LCD_WRITE_STRING("All", LCD_PAGE2);
  }
  else if ((buildRev.hostInfo & CENTRAL_CFG) && 
           (buildRev.hostInfo & BROADCASTER_CFG))
  {
    LCD_WRITE_STRING("Cent+Bcast", LCD_PAGE2);
  }
  else if ((buildRev.hostInfo & PERIPHERAL_CFG) && 
           (buildRev.hostInfo & OBSERVER_CFG))
  {
    LCD_WRITE_STRING("Peri+Observ", LCD_PAGE2);
  }
  else if (buildRev.hostInfo & CENTRAL_CFG)
  {
    LCD_WRITE_STRING("Central", LCD_PAGE2);
  }
  else if (buildRev.hostInfo & PERIPHERAL_CFG)
  {
    LCD_WRITE_STRING("Peripheral", LCD_PAGE2);
  }
  else
  {
    LCD_WRITE_STRING_VALUE("Unknown build cfg", buildRev.hostInfo, 10, LCD_PAGE2);
  }
}
Exemplo n.º 11
0
/*********************************************************************
 * @fn      timeAppConfigGattMsg()
   *
 * @brief   Handle GATT messages for characteristic configuration.
 *
 * @param   state - Discovery state.
 * @param   pMsg - GATT message.
 *
 * @return  New configuration state.
 */
uint8 timeAppConfigGattMsg( uint8 state, gattMsgEvent_t *pMsg )
{
  if ( state > TIMEAPP_CONFIG_MAX )
  {
    return TIMEAPP_CONFIG_CMPL;
  }
  
  if ( (pMsg->method == ATT_READ_RSP || pMsg->method == ATT_WRITE_RSP) &&
       (pMsg->hdr.status == SUCCESS) )
  {
    // Process response
    switch ( timeAppConfigList[state] )
    {
      case HDL_CURR_TIME_CT_TIME_START:
        // Set clock to time read from time server
        timeAppClockSet( pMsg->msg.readRsp.value );
        break;

      case HDL_CURR_TIME_LOC_INFO:
        break;

      case HDL_CURR_TIME_REF_INFO:
        break;

      case HDL_DST_CHG_TIME_DST:
        break;

      case HDL_NWA_NWA_START:
        // Display network availability state
        if ( pMsg->msg.readRsp.value[0] == 1 )
        {
          LCD_WRITE_STRING( "Network: Yes", HAL_LCD_LINE_1 );
        }
        else
        {
          LCD_WRITE_STRING( "Network: None", HAL_LCD_LINE_1 );
        }    
        break;

      case HDL_BATT_LEVEL_START:
        // Display battery level
        LCD_WRITE_STRING_VALUE( "Battery%", pMsg->msg.readRsp.value[0], 10, HAL_LCD_LINE_2 );
        break;

      case HDL_CURR_TIME_CT_TIME_CCCD:
        break;

      case HDL_ALERT_NTF_NEW_CAT:
        break;

      case HDL_ALERT_NTF_UNREAD_CAT:
        break;
        
      case HDL_ALERT_NTF_UNREAD_CCCD:
        break;

      case HDL_ALERT_NTF_NEW_CCCD:
        break;

      case HDL_NWA_NWA_CCCD:
        break;

      case HDL_PAS_ALERT_START:
        // Display phone alert status
        LCD_WRITE_STRING_VALUE( "Phone Alert:", pMsg->msg.readRsp.value[0], 16, HAL_LCD_LINE_1 );
        break;

      case HDL_PAS_RINGER_START:
        // Display ringer state
        if ( pMsg->msg.readRsp.value[0] == 0 )
        {
          LCD_WRITE_STRING( "Ringer Off", HAL_LCD_LINE_2 );
        }
        else
        {
          LCD_WRITE_STRING( "Ringer On", HAL_LCD_LINE_2 );
        }    
        break;

      default:
        break;
    }
  }

  return timeAppConfigNext( state + 1 );
}
Exemplo n.º 12
0
/*********************************************************************
 * @fn      simpleBLECentralEventCB
 *
 * @brief   Central event callback function.
 *
 * @param   pEvent - pointer to event structure
 *
 * @return  none
 */
static void simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
{
  switch ( pEvent->gap.opcode )
  {
    case GAP_DEVICE_INIT_DONE_EVENT:  
      {
        LCD_WRITE_STRING( "BLE Central", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( pEvent->initDone.devAddr ),  HAL_LCD_LINE_2 );
        
#if 1
        // Start or stop discovery
    if ( simpleBLEState != BLE_STATE_CONNECTED )
    {
      if ( !simpleBLEScanning )
      {
        simpleBLEScanning = TRUE;
        simpleBLEScanRes = 0;
        
        LCD_WRITE_STRING( "Discovering...", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
        
        //HCI_EXT_SetRxGainCmd(HCI_EXT_RX_GAIN_HIGH);
        //HCI_EXT_SetTxPowerCmd(HCI_EXT_TX_POWER_0_DBM);
        
        GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
                                       DEFAULT_DISCOVERY_ACTIVE_SCAN,
                                       DEFAULT_DISCOVERY_WHITE_LIST );    
        HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);
        osal_start_timerEx( simpleBLETaskId, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
      }
      else
      {
        HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
        GAPCentralRole_CancelDiscovery();
      }
    }
#endif
      }
      break;

    case GAP_DEVICE_INFO_EVENT:
      {
        // if filtering device discovery results based on service UUID
        //HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
        if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
        {
          if ( simpleBLEFindSvcUuid( SIMPLEPROFILE_SERV_UUID,
                                     pEvent->deviceInfo.pEvtData,
                                     pEvent->deviceInfo.dataLen ) )
          {
            //HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
            simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
          }
        }
      }
      break;
      
    case GAP_DEVICE_DISCOVERY_EVENT:
      {
        // discovery complete
        simpleBLEScanning = FALSE;
        

        // if not filtering device discovery results based on service UUID
        if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE )
        {
          // Copy results
          simpleBLEScanRes = pEvent->discCmpl.numDevs;
          osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList,
                       (sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );
          //HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
        }
        
        LCD_WRITE_STRING_VALUE( "Devices Found", simpleBLEScanRes,
                                10, HAL_LCD_LINE_1 );
        if ( simpleBLEScanRes > 0 )
        {
          LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 );
          //HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);
        }

        // initialize scan index to last device
        simpleBLEScanIdx = simpleBLEScanRes;
#if 1        
     
    // Display discovery results
    if ( !simpleBLEScanning && simpleBLEScanRes > 0 )
    {
        // Increment index of current result (with wraparound)
        simpleBLEScanIdx++;
        if ( simpleBLEScanIdx >= simpleBLEScanRes )
        {
          simpleBLEScanIdx = 0;
        }
        
        LCD_WRITE_STRING_VALUE( "Device", simpleBLEScanIdx + 1,
                                10, HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( simpleBLEDevList[simpleBLEScanIdx].addr ),
                          HAL_LCD_LINE_2 );
    }
    uint8 addrType;
    uint8 *peerAddr;
    
    // Connect or disconnect
    if ( simpleBLEState == BLE_STATE_IDLE )
    {
      // if there is a scan result
      if ( simpleBLEScanRes > 0 )
      {
        // connect to current device in scan result
        peerAddr = simpleBLEDevList[simpleBLEScanIdx].addr;
        addrType = simpleBLEDevList[simpleBLEScanIdx].addrType;
      
        simpleBLEState = BLE_STATE_CONNECTING;
        
        GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
                                      DEFAULT_LINK_WHITE_LIST,
                                      addrType, peerAddr );
  
        LCD_WRITE_STRING( "Connecting", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( bdAddr2Str( peerAddr ), HAL_LCD_LINE_2 ); 
      } else {
#if 1
        // Start or stop discovery
    if ( simpleBLEState != BLE_STATE_CONNECTED )
    {
      if ( !simpleBLEScanning )
      {
        simpleBLEScanning = TRUE;
        simpleBLEScanRes = 0;
        
        LCD_WRITE_STRING( "Discovering...", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING( "", HAL_LCD_LINE_2 );
        
        //HCI_EXT_SetRxGainCmd(HCI_EXT_RX_GAIN_HIGH);
        //HCI_EXT_SetTxPowerCmd(HCI_EXT_TX_POWER_0_DBM);
        
        GAPCentralRole_StartDiscovery( DEFAULT_DISCOVERY_MODE,
                                       DEFAULT_DISCOVERY_ACTIVE_SCAN,
                                       DEFAULT_DISCOVERY_WHITE_LIST );    
        //HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);
      }
      else
      {
        HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
        GAPCentralRole_CancelDiscovery();
      }
    }
#endif
      }
      
    }
#endif
      }
      break;

    case GAP_LINK_ESTABLISHED_EVENT:
      {
        if ( pEvent->gap.hdr.status == SUCCESS )
        {          
          simpleBLEState = BLE_STATE_CONNECTED;
          simpleBLEConnHandle = pEvent->linkCmpl.connectionHandle;
          simpleBLEProcedureInProgress = TRUE;    

          // If service discovery not performed initiate service discovery
          if ( simpleBLECharHdl == 0 )
          {
            osal_start_timerEx( simpleBLETaskId, START_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY );
          }
                    
          LCD_WRITE_STRING( "Connected", HAL_LCD_LINE_1 );
          LCD_WRITE_STRING( bdAddr2Str( pEvent->linkCmpl.devAddr ), HAL_LCD_LINE_2 );
          osal_start_timerEx( simpleBLETaskId, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
          HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
        }
        else
        {
          simpleBLEState = BLE_STATE_IDLE;
          simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
          simpleBLERssi = FALSE;
          simpleBLEDiscState = BLE_DISC_STATE_IDLE;
          
          LCD_WRITE_STRING( "Connect Failed", HAL_LCD_LINE_1 );
          LCD_WRITE_STRING_VALUE( "Reason:", pEvent->gap.hdr.status, 10, HAL_LCD_LINE_2 );
        }
      }
      break;

    case GAP_LINK_TERMINATED_EVENT:
      {
        simpleBLEState = BLE_STATE_IDLE;
        simpleBLEConnHandle = GAP_CONNHANDLE_INIT;
        simpleBLERssi = FALSE;
        simpleBLEDiscState = BLE_DISC_STATE_IDLE;
        simpleBLECharHdl = 0;
        simpleBLEProcedureInProgress = FALSE;
          
        LCD_WRITE_STRING( "Disconnected", HAL_LCD_LINE_1 );
        LCD_WRITE_STRING_VALUE( "Reason:", pEvent->linkTerminate.reason,
                                10, HAL_LCD_LINE_2 );
        HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);
      }
      break;

    case GAP_LINK_PARAM_UPDATE_EVENT:
      {
        LCD_WRITE_STRING( "Param Update", HAL_LCD_LINE_1 );
      }
      break;
      
    default:
      break;
  }
}
Exemplo n.º 13
0
/*********************************************************************
 * @fn      simpleBLECentralProcessGATTMsg
 *
 * @brief   Process GATT messages
 *
 * @return  none
 */
static void simpleBLECentralProcessGATTMsg( gattMsgEvent_t *pMsg )
{
  if ( simpleBLEState != BLE_STATE_CONNECTED )
  {
    // In case a GATT message came after a connection has dropped,
    // ignore the message
    return;
  }
  
  if ( ( pMsg->method == ATT_READ_RSP ) ||
       ( ( pMsg->method == ATT_ERROR_RSP ) &&
         ( pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ ) ) )
  {
    if ( pMsg->method == ATT_ERROR_RSP )
    {
      uint8 status = pMsg->msg.errorRsp.errCode;
      
      LCD_WRITE_STRING_VALUE( "Read Error", status, 10, HAL_LCD_LINE_1 );
    }
    else
    {
      // After a successful read, display the read value
      uint8 valueRead = pMsg->msg.readRsp.value[0];
      
      if (valueRead == 0x55)
      {
        P0_4 = 1;
      }
#if 0      
      char strTemp[35] = {0};
      sprintf(strTemp, "raad value = %d\r\n",valueRead);
      NPI_WriteTransport((uint8*)strTemp, osal_strlen(strTemp));
#endif
      LCD_WRITE_STRING_VALUE( "Read rsp:", valueRead, 10, HAL_LCD_LINE_1 );
    }
#if 0    
    char strTemp[35] = {0};
    sprintf(strTemp, "raad process function\r\n");
    NPI_WriteTransport((uint8*)strTemp, osal_strlen(strTemp));
#endif    
    simpleBLEProcedureInProgress = FALSE;
  }
  else if ( ( pMsg->method == ATT_WRITE_RSP ) ||
       ( ( pMsg->method == ATT_ERROR_RSP ) &&
         ( pMsg->msg.errorRsp.reqOpcode == ATT_WRITE_REQ ) ) )
  {
    
    if ( pMsg->method == ATT_ERROR_RSP == ATT_ERROR_RSP )
    {
      uint8 status = pMsg->msg.errorRsp.errCode;
      
      LCD_WRITE_STRING_VALUE( "Write Error", status, 10, HAL_LCD_LINE_1 );
    }
    else
    {
      // After a succesful write, display the value that was written and increment value
      LCD_WRITE_STRING_VALUE( "Write sent:", simpleBLECharVal++, 10, HAL_LCD_LINE_1 );      
    }
    
    simpleBLEProcedureInProgress = FALSE;    

  }
  else if ( simpleBLEDiscState != BLE_DISC_STATE_IDLE )
  {
    simpleBLEGATTDiscoveryEvent( pMsg );
  }
  
}