libraries/Bluefruit52Lib/src/bluefruit.cpp
/**
 * BLE event handler
 * @param evt event
 */
BLE event handler
@param evt event
BLE event handler @param evt event
void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
{
  uint16_t const conn_hdl = evt->evt.common_evt.conn_handle;
  BLEConnection* conn = this->Connection(conn_hdl);
  LOG_LV2("BLE", "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), conn_hdl);
  if ( conn ) conn->_eventHandler(evt);
  switch(evt->header.evt_id)
  {
    case BLE_GAP_EVT_CONNECTED:
    {
      _stopConnLed();
      _setConnLed(true);
      ble_gap_evt_connected_t const * para = &evt->evt.gap_evt.params.connected;
      LOG_LV1("GAP", "Conn Interval= %.2f ms, Latency = %d, Supervisor Timeout = %d ms",
              para->conn_params.max_conn_interval*1.25f, para->conn_params.slave_latency, 10*para->conn_params.conn_sup_timeout);
      if ( _connection[conn_hdl] )
      {
        LOG_LV1("GAP", "Connection is already in used, something wrong !!");
        delete _connection[conn_hdl];
        _connection[conn_hdl] = NULL;
      }
      uint8_t const hvn_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _sd_cfg.prph.hvn_qsize : _sd_cfg.central.hvn_qsize;
      uint8_t const wrcmd_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _sd_cfg.prph.wrcmd_qsize : _sd_cfg.central.wrcmd_qsize;
      _connection[conn_hdl] = new BLEConnection(conn_hdl, para, hvn_qsize, wrcmd_qsize);
      conn = _connection[conn_hdl];
      if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
      {
        if ( Periph._connect_cb ) ada_callback(NULL, 0, Periph._connect_cb, conn_hdl);
      }else
      {
        if ( Central._connect_cb ) ada_callback(NULL, 0, Central._connect_cb, conn_hdl);
      }
    }
    break;
    case BLE_GAP_EVT_DISCONNECTED:
    {
      ble_gap_evt_disconnected_t const* para = &evt->evt.gap_evt.params.disconnected;
      LOG_LV2("GAP", "Disconnect Reason: %s", dbg_hci_str(evt->evt.gap_evt.params.disconnected.reason));
      if ( !this->connected() ) _setConnLed(false);
      if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
      {
        if ( Periph._disconnect_cb ) ada_callback(NULL, 0, Periph._disconnect_cb, conn_hdl, para->reason);
      }else
      {
        if ( Central._disconnect_cb ) ada_callback(NULL, 0, Central._disconnect_cb, conn_hdl, para->reason);
      }
      delete _connection[conn_hdl];
      _connection[conn_hdl] = NULL;
    }
    break;
    case BLE_GAP_EVT_RSSI_CHANGED:
    {
      ble_gap_evt_rssi_changed_t const * rssi_changed = &evt->evt.gap_evt.params.rssi_changed;
      if ( _rssi_cb )
      {
         ada_callback(NULL, 0, _rssi_cb, conn_hdl, rssi_changed->rssi);
      }
    }
    break;
    case BLE_EVT_USER_MEM_REQUEST:
      sd_ble_user_mem_reply(conn_hdl, NULL);
    break;
    case BLE_EVT_USER_MEM_RELEASE:
    break;
    default: break;
  }
  Advertising._eventHandler(evt);
  Scanner._eventHandler(evt);
  if ( conn_hdl       == _conn_hdl             ||
       evt->header.evt_id == BLE_GAP_EVT_CONNECTED)
  {
    switch ( evt->header.evt_id  )
    {
      case BLE_GAP_EVT_CONNECTED:
      {
        ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected;
        if (para->role == BLE_GAP_ROLE_PERIPH)
        {
          _conn_hdl = evt->evt.gap_evt.conn_handle;
        }
      }
      break;
      case BLE_GAP_EVT_DISCONNECTED:
        _conn_hdl = BLE_CONN_HANDLE_INVALID;
      break;
      default: break;
    }
  }
  if ( (conn == NULL) || (conn->getRole() == BLE_GAP_ROLE_PERIPH) )
  {
    Periph._eventHandler(evt);
  }
  if ( (conn == NULL) || (conn->getRole() == BLE_GAP_ROLE_CENTRAL) )
  {
    Central._eventHandler(evt);
  }
  if ( Discovery.begun() ) Discovery._eventHandler(evt);
  Gatt._eventHandler(evt);
  if (_event_cb) _event_cb(evt);
}
void AdafruitBluefruit :: _ble_handler ( ble_evt_t * evt ) { uint16_t const conn_hdl = evt -> evt . common_evt . conn_handle ; BLEConnection * conn = this -> Connection ( conn_hdl ) ; LOG_LV2 ( " " , " " , dbg_ble_event_str ( evt -> header . evt_id ) , conn_hdl ) ; if ( conn ) conn -> _eventHandler ( evt ) ; switch ( evt -> header . evt_id ) { case BLE_GAP_EVT_CONNECTED : { _stopConnLed ( ) ; _setConnLed ( true ) ; ble_gap_evt_connected_t const * para = & evt -> evt . gap_evt . params . connected ; LOG_LV1 ( " " , " " , para -> conn_params . max_conn_interval * 1.25f , para -> conn_params . slave_latency , 10 * para -> conn_params . conn_sup_timeout ) ; if ( _connection [ conn_hdl ] ) { LOG_LV1 ( " " , " " ) ; delete _connection [ conn_hdl ] ; _connection [ conn_hdl ] = NULL ; } uint8_t const hvn_qsize = ( para -> role == BLE_GAP_ROLE_PERIPH ) ? _sd_cfg . prph . hvn_qsize : _sd_cfg . central . hvn_qsize ; uint8_t const wrcmd_qsize = ( para -> role == BLE_GAP_ROLE_PERIPH ) ? _sd_cfg . prph . wrcmd_qsize : _sd_cfg . central . wrcmd_qsize ; _connection [ conn_hdl ] = new BLEConnection ( conn_hdl , para , hvn_qsize , wrcmd_qsize ) ; conn = _connection [ conn_hdl ] ; if ( conn -> getRole ( ) == BLE_GAP_ROLE_PERIPH ) { if ( Periph . _connect_cb ) ada_callback ( NULL , 0 , Periph . _connect_cb , conn_hdl ) ; } else { if ( Central . _connect_cb ) ada_callback ( NULL , 0 , Central . _connect_cb , conn_hdl ) ; } } break ; case BLE_GAP_EVT_DISCONNECTED : { ble_gap_evt_disconnected_t const * para = & evt -> evt . gap_evt . params . disconnected ; LOG_LV2 ( " " , " " , dbg_hci_str ( evt -> evt . gap_evt . params . disconnected . reason ) ) ; if ( ! this -> connected ( ) ) _setConnLed ( false ) ; if ( conn -> getRole ( ) == BLE_GAP_ROLE_PERIPH ) { if ( Periph . _disconnect_cb ) ada_callback ( NULL , 0 , Periph . _disconnect_cb , conn_hdl , para -> reason ) ; } else { if ( Central . _disconnect_cb ) ada_callback ( NULL , 0 , Central . _disconnect_cb , conn_hdl , para -> reason ) ; } delete _connection [ conn_hdl ] ; _connection [ conn_hdl ] = NULL ; } break ; case BLE_GAP_EVT_RSSI_CHANGED : { ble_gap_evt_rssi_changed_t const * rssi_changed = & evt -> evt . gap_evt . params . rssi_changed ; if ( _rssi_cb ) { ada_callback ( NULL , 0 , _rssi_cb , conn_hdl , rssi_changed -> rssi ) ; } } break ; case BLE_EVT_USER_MEM_REQUEST : sd_ble_user_mem_reply ( conn_hdl , NULL ) ; break ; case BLE_EVT_USER_MEM_RELEASE : break ; default : break ; } Advertising . _eventHandler ( evt ) ; Scanner . _eventHandler ( evt ) ; if ( conn_hdl == _conn_hdl || evt -> header . evt_id == BLE_GAP_EVT_CONNECTED ) { switch ( evt -> header . evt_id ) { case BLE_GAP_EVT_CONNECTED : { ble_gap_evt_connected_t * para = & evt -> evt . gap_evt . params . connected ; if ( para -> role == BLE_GAP_ROLE_PERIPH ) { _conn_hdl = evt -> evt . gap_evt . conn_handle ; } } break ; case BLE_GAP_EVT_DISCONNECTED : _conn_hdl = BLE_CONN_HANDLE_INVALID ; break ; default : break ; } } if ( ( conn == NULL ) || ( conn -> getRole ( ) == BLE_GAP_ROLE_PERIPH ) ) { Periph . _eventHandler ( evt ) ; } if ( ( conn == NULL ) || ( conn -> getRole ( ) == BLE_GAP_ROLE_CENTRAL ) ) { Central . _eventHandler ( evt ) ; } if ( Discovery . begun ( ) ) Discovery . _eventHandler ( evt ) ; Gatt . _eventHandler ( evt ) ; if ( _event_cb ) _event_cb ( evt ) ; }
BLE event handler
 @param evt event
BLE event handler @param evt event
// conn handle has fixed offset for all events // GAP handler // Turn on Conn LED // Transmission buffer pool // Invoke connect callback // Turn off Conn LED If not connected at all // Invoke disconnect callback // We will handle Long Write sequence (RW Authorize PREP_WRITE_REQ) // nothing to do /*------------- BLE Peripheral Events -------------*/ /* Only handle Peripheral events with matched connection handle
   * or a few special one
   * - Connected event
   * - Advertising timeout (could be connected and advertising at the same time)
   */ // Periph event handler (also handle generic non-connection event) // Central Event Handler (also handle generic non-connection event) // Discovery Event Handler // GATTs characteristics event handler // User callback if set