HID Boot Host LE

Source Code: hog_boot_host_demo.c

This example implements a minimal HID-over-GATT Boot Host. It scans for LE HID devices, connects to it, discovers the Characteristics relevant for the HID Service and enables Notifications on them. It then dumps all Boot Keyboard and Mouse Input Reports

HOG Boot Keyboard Handler

Boot Keyboard Input Report contains a report of format [ modifier, reserved, 6 x usage for key 1..6 from keyboard usage] Track new usages, map key usage to actual character and simulate terminal

HOG Boot Mouse Handler

Boot Mouse Input Report contains a report of format [ buttons, dx, dy, dz = scroll wheel] Decode packet and print on stdout

@param packet_type @param channel @param packet @param size

Test if advertisement contains HID UUID

static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
...
  switch (packet_type) {
    case HCI_EVENT_PACKET:
      event = hci_event_packet_get_type(packet);
      switch (event) {
        case BTSTACK_EVENT_STATE:
          if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
          btstack_assert(app_state == W4_WORKING);
          hog_start_connect();
          break;
        case GAP_EVENT_ADVERTISING_REPORT:
          if (app_state != W4_HID_DEVICE_FOUND) break;
          if (adv_event_contains_hid_service(packet) == false) break;
          // stop scan
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L668">gap_stop_scan</a>();
          // store remote device address and type
          gap_event_advertising_report_get_address(packet, remote_device.addr);
          remote_device.addr_type = gap_event_advertising_report_get_address_type(packet);
          // connect
          printf("Found, connect to device with %s address %s ...\n", remote_device.addr_type == 0 ? "public" : "random" , <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L271">bd_addr_to_str</a>(remote_device.addr));
          hog_connect();
          break;
        case HCI_EVENT_DISCONNECTION_COMPLETE:
          if (app_state != READY) break;

          connection_handle = HCI_CON_HANDLE_INVALID;
          switch (app_state){
            case READY:
              printf("\nDisconnected, try to reconnect...\n");
              app_state = W4_TIMEOUT_THEN_RECONNECT;
              break;
            default:
              printf("\nDisconnected, start over...\n");
              app_state = W4_TIMEOUT_THEN_SCAN;
              break;
          }
          // set timer
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L233">btstack_run_loop_set_timer</a>(&amp;connection_timer, 100);
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L233">btstack_run_loop_set_timer</a>_handler(&amp;connection_timer, &amp;hog_reconnect_timeout);
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L260">btstack_run_loop_add_timer</a>(&amp;connection_timer);
          break;
        case HCI_EVENT_META_GAP:
          // wait for connection complete
          if (hci_event_gap_meta_get_subevent_code(packet) != GAP_SUBEVENT_LE_CONNECTION_COMPLETE) break;
          if (app_state != W4_CONNECTED) return;
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_run_loop.h#L267">btstack_run_loop_remove_timer</a>(&amp;connection_timer);
          connection_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
          // request security
          app_state = W4_ENCRYPTED;
          <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L182">sm_request_pairing</a>(connection_handle);
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}

HCI packet handler

The SM packet handler receives Security Manager Events required for pairing. It also receives events generated during Identity Resolving see Listing here.

static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  UNUSED(channel);
  UNUSED(size);

  if (packet_type != HCI_EVENT_PACKET) return;

  bool connect_to_service = false;

  switch (hci_event_packet_get_type(packet)) {
    case SM_EVENT_JUST_WORKS_REQUEST:
      printf("Just works requested\n");
      <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L156">sm_just_works_confirm</a>(sm_event_just_works_request_get_handle(packet));
      break;
    case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
      printf("Confirming numeric comparison: %"PRIu32"\n", sm_event_numeric_comparison_request_get_passkey(packet));
      <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L162">sm_numeric_comparison_confirm</a>(sm_event_passkey_display_number_get_handle(packet));
      break;
    case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
      printf("Display Passkey: %"PRIu32"\n", sm_event_passkey_display_number_get_passkey(packet));
      break;
    case SM_EVENT_PAIRING_COMPLETE:
      switch (sm_event_pairing_complete_get_status(packet)){
        case ERROR_CODE_SUCCESS:
          printf("Pairing complete, success\n");
          connect_to_service = true;
          break;
        case ERROR_CODE_CONNECTION_TIMEOUT:
          printf("Pairing failed, timeout\n");
          break;
        case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
          printf("Pairing failed, disconnected\n");
          break;
        case ERROR_CODE_AUTHENTICATION_FAILURE:
          printf("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet));
          break;
        default:
          break;
      }
      break;
    case SM_EVENT_REENCRYPTION_COMPLETE:
      printf("Re-encryption complete, success\n");
      connect_to_service = true;
      break;
    default:
      break;
  }

  if (connect_to_service){
    // continue - query primary services
    app_state = W2_QUERY_HID_SERVICE;
    hog_host_request_to_send();
  }
}

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();

  // setup SM: Display only
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L125">sm_set_io_capabilities</a>(IO_CAPABILITY_DISPLAY_ONLY);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L119">sm_set_authentication_requirements</a>(SM_AUTHREQ_SECURE_CONNECTION | SM_AUTHREQ_BONDING);

  //
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/gatt_client.h#L359">gatt_client_init</a>();

  // register for events from HCI
  hci_event_callback_registration.callback = &amp;packet_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&amp;hci_event_callback_registration);

  // register for events from Security Manager
  sm_event_callback_registration.callback = &amp;sm_packet_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L94">sm_add_event_handler</a>(&amp;sm_event_callback_registration);