Пример #1
0
/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
  switch (p_ble_evt->header.evt_id)
  {
  case BLE_GAP_EVT_CONNECTED:
      m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
      ble_device_on_connect();
      break;

  case BLE_GAP_EVT_DISCONNECTED:
  {
      uint8_t reason = p_ble_evt->evt.gap_evt.params.disconnected.reason;

      m_conn_handle = BLE_CONN_HANDLE_INVALID;
      
  if (reason == BLE_HCI_CONNECTION_TIMEOUT ) {
          ble_device_on_disconnect(BLE_DEVICE_LINK_LOST);
  } else {
          ble_device_on_disconnect(BLE_DEVICE_NORMAL);
      }

      if (app == NULL) {
          ble_device_start_advertising();
      }
      
      break;
  }
  case BLE_GAP_EVT_TIMEOUT:
      break;

  default:
      // No implementation needed.
      break;
  }
}
Пример #2
0
/**
 * @brief  Callback processing the ACI events.
 * @note   Inside this function each event must be identified and correctly
 *         parsed.
 * @param  void* Pointer to the ACI packet
 * @retval None
 */
void HCI_Event_CB(void *pckt)
{
    hci_uart_pckt *hci_pckt = pckt;
    /* obtain event packet */
    hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data;
    if(hci_pckt->type != HCI_EVENT_PKT)
        return;

    switch(event_pckt->evt) {

    case EVT_DISCONN_COMPLETE:
    {
     disconn_event_pckt *evt = (void *)event_pckt->data;
     printf("conn handle: %x\n", evt->conn_handle);
#if defined (CLIENT_ROLE) || defined (CLIENT_SERVER_ROLE)
        host_notification_enabled = 0;
#endif
        /*Host*/
       GAP_DisconnectionComplete_CB();
       /*device*/
       notification_enabled = 0;
       ble_device_on_disconnect(evt->disconn_reason);
     
    }
    break;
    
    case EVT_LE_META_EVENT:
    {
        evt_le_meta_event *evt = (void *)event_pckt->data;
        switch(evt->subevent) {
        case EVT_LE_CONN_COMPLETE:
        {
            evt_le_connection_complete *cc = (void *)evt->data;
            if(cc->role == SLAVE_ROLE){
                /*device*/
                ble_device_on_connect();
                connection_information(cc->handle);
            }
            if(cc->role == MASTER_ROLE){
                /*host*/
                GAP_ConnectionComplete_CB(cc->peer_bdaddr, cc->handle);
            }
        }
        break;
        case EVT_LE_ADVERTISING_REPORT:
        {
#if defined (CLIENT_ROLE) || defined (CLIENT_SERVER_ROLE)
            le_advertising_info* adv_data = (void *)((event_pckt->data)+2);
            run_after_delay(ble_host_device_found, adv_data, 2);
#endif
        }
        break;
        }
    }
    break;

    case EVT_VENDOR:
    {
        evt_blue_aci *blue_evt = (void*)event_pckt->data;
        switch(blue_evt->ecode) {
        case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED:
        {
            /* this callback is invoked when a GATT attribute is modified
            extract callback data and pass to suitable handler function */
            evt_gatt_attr_modified *evt = (evt_gatt_attr_modified*)blue_evt->data;
            ///on message
            if(evt->att_data[1] > 0) {
                ble_device_on_message(evt->att_data[0], evt->att_data[1], (evt->att_data)+2);
            } else if(evt->att_data[0] == 1) {
                notification_enabled = 1;
#if defined (CLIENT_ROLE) || defined (CLIENT_SERVER_ROLE)
                host_notification_enabled = 1;
#endif
            }
        }
        break;
        case EVT_BLUE_GATT_NOTIFICATION:
        {
            evt_gatt_attr_notification *evt = (evt_gatt_attr_notification*)blue_evt->data;
            GATT_Notification_CB(evt->attr_handle, evt->event_data_length - 2, evt->attr_value);
        }
        break;
        case EVT_BLUE_GATT_READ_PERMIT_REQ:
        {
            evt_gatt_read_permit_req *pr = (void*)blue_evt->data;
            Read_Request_CB(pr->attr_handle);
        }
        break;
        case EVT_BLUE_GAP_DEVICE_FOUND:
        {
            printf("scanned one device\n\r");
        }
        break;
        case EVT_BLUE_GAP_PROCEDURE_COMPLETE:
        {
#ifdef CLIENT_SERVER_ROLE
            run_after_delay(ble_host_start_scan, NULL, 10);
            printf("EVT_BLUE_GAP_PROCEDURE_COMPLETE \n\r");
#endif
        }
        break;
        case EVT_BLUE_GATT_DISC_READ_CHAR_BY_UUID_RESP:
#if defined (CLIENT_ROLE) || defined (CLIENT_SERVER_ROLE)
            if(BLE_Role & CLIENT) {
                printf("EVT_BLUE_GATT_DISC_READ_CHAR_BY_UUID_RESP\n");

                evt_gatt_disc_read_char_by_uuid_resp *resp = (void*)blue_evt->data;

                if (start_read_write_char_handle && !end_read_write_char_handle)
                {
                    write_handle = resp->attr_handle;
                    printf("write_handle  %04X\n", write_handle);
                }
                else if (start_read_notify_read_char_handle && !end_read_notify_read_char_handle)
                {
                    notify_read_handle = resp->attr_handle;
                    printf("notify_read_handle  %04X\n", notify_read_handle);
                }
                else if (start_read_write_without_rsp_char_handle && !end_read_write_without_rsp_char_handle)
                {
                    write_without_rsp_handle = resp->attr_handle;
                    printf("write_without_rsp_handle  %04X\n", write_without_rsp_handle);
                }
                else if (start_read_notify_char_handle && !end_read_notify_char_handle)
                {
                    notify_handle = resp->attr_handle;
                    printf("notify_handle %04X\n", notify_handle);
                }
            }
#endif
            break;

        case EVT_BLUE_GATT_PROCEDURE_COMPLETE:
#if defined (CLIENT_ROLE) || defined (CLIENT_SERVER_ROLE)
            if(BLE_Role & CLIENT) {
                /* Wait for gatt procedure complete event trigger related to Discovery Charac by UUID */
                //evt_gatt_procedure_complete *pr = (void*)blue_evt->data;

                if (start_read_write_char_handle && !end_read_write_char_handle)
                {
                    end_read_write_char_handle = TRUE;
                    run_when_idle(ble_host_discover_char, NULL);

                }
                else if (start_read_notify_read_char_handle && !end_read_notify_read_char_handle)
                {
                    end_read_notify_read_char_handle = TRUE;
                    run_when_idle(ble_host_discover_char, NULL);
                }
                else if (start_read_write_without_rsp_char_handle && !end_read_write_without_rsp_char_handle)
                {
                    end_read_write_without_rsp_char_handle = TRUE;
                    run_when_idle(ble_host_discover_char, NULL);
                }
                else if (start_read_notify_char_handle && !end_read_notify_char_handle)
                {
                    end_read_notify_char_handle = TRUE;
                    run_when_idle(ble_host_discover_char, NULL);
                }
            }
#endif
            break;
        }
    }
    break;
    }
}
Пример #3
0
/**
 * @brief  Callback processing the ACI events.
 * @note   Inside this function each event must be identified and correctly
 *         parsed.
 * @param  void* Pointer to the ACI packet
 * @retval None
 */
void HCI_Event_CB(void *pckt)
{
    hci_uart_pckt *hci_pckt = pckt;
    /* obtain event packet */
    hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data;
    if(hci_pckt->type != HCI_EVENT_PKT)
        return;

    switch(event_pckt->evt) {

    case EVT_DISCONN_COMPLETE:
    {
        notification_enabled = 0;
        ble_device_on_disconnect(event_pckt->data[3]);
    }
    break;

    case EVT_LE_META_EVENT:
    {
        evt_le_meta_event *evt = (void *)event_pckt->data;

        switch(evt->subevent) {
        case EVT_LE_CONN_COMPLETE:
        {
            ble_device_on_connect();
            evt_le_connection_complete *cc = (void *)evt->data;
            connection_information(cc->handle);

        }
        break;
        }
    }
    break;

    case EVT_VENDOR:
    {
        evt_blue_aci *blue_evt = (void*)event_pckt->data;
        switch(blue_evt->ecode) {

        case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED:
        {
            /* this callback is invoked when a GATT attribute is modified
               extract callback data and pass to suitable handler function */
            evt_gatt_attr_modified *evt = (evt_gatt_attr_modified*)blue_evt->data;
            ///on message
            if(evt->att_data[1] > 0) {
                ble_device_on_message(evt->att_data[0], evt->att_data[1], (evt->att_data)+2);
            } else if(evt->att_data[0] == 1) {
                notification_enabled = 1;
            }
        }
        break;

        case EVT_BLUE_GATT_READ_PERMIT_REQ:
        {
            evt_gatt_read_permit_req *pr = (void*)blue_evt->data;
            Read_Request_CB(pr->attr_handle);
        }
        break;

        }
    }
    break;
    }
}