From a347b3bacea4157eac012babf598d4733861b2e0 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Tue, 2 Jul 2019 20:30:33 +0200 Subject: [PATCH] Enable simple paring mode --- BTD.cpp | 41 ++++++++++++++++++++++++++++++----------- BTD.h | 13 ++++++++----- BTHID.cpp | 14 +++++++++++++- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/BTD.cpp b/BTD.cpp index f74cda7a..36e7e705 100644 --- a/BTD.cpp +++ b/BTD.cpp @@ -430,19 +430,19 @@ void BTD::HCI_event_task() { #endif #ifdef DEBUG_USB_HOST if(hcibuf[6] == 0) { // Page 0 - Notify(PSTR("\r\nLocal "), 0x80); + Notify(PSTR("\r\nDongle "), 0x80); if(hcibuf[8 + 6] & (1U << 3)) Notify(PSTR("supports"), 0x80); else Notify(PSTR("does NOT support"), 0x80); - Notify(PSTR(" secure simple paring (controller support)"), 0x80); + Notify(PSTR(" secure simple pairing (controller support)"), 0x80); } else if(hcibuf[6] == 1) { // Page 1 - Notify(PSTR("\r\nLocal "), 0x80); + Notify(PSTR("\r\nDongle "), 0x80); if(hcibuf[8 + 0] & (1U << 0)) Notify(PSTR("supports"), 0x80); else Notify(PSTR("does NOT support"), 0x80); - Notify(PSTR(" secure simple paring (host support)"), 0x80); + Notify(PSTR(" secure simple pairing (host support)"), 0x80); } #endif } @@ -671,14 +671,14 @@ void BTD::HCI_event_task() { Notify(PSTR("supports"), 0x80); else Notify(PSTR("does NOT support"), 0x80); - Notify(PSTR(" secure simple paring (controller support)"), 0x80); + Notify(PSTR(" secure simple pairing (controller support)"), 0x80); } else if(hcibuf[5] == 1) { // Page 1 Notify(PSTR("\r\nRemote "), 0x80); if(hcibuf[7 + 0] & (1U << 0)) Notify(PSTR("supports"), 0x80); else Notify(PSTR("\r\ndoes NOT support"), 0x80); - Notify(PSTR(" secure simple paring (host support)"), 0x80); + Notify(PSTR(" secure simple pairing (host support)"), 0x80); } #endif } @@ -814,18 +814,28 @@ void BTD::HCI_task() { case HCI_LOCAL_EXTENDED_FEATURES_STATE: if(hci_check_flag(HCI_FLAG_LOCAL_EXTENDED_FEATURES)) { + hci_write_simple_pairing_mode(true); + hci_state = HCI_WRITE_SIMPLE_PAIRING_STATE; + } + break; + + case HCI_WRITE_SIMPLE_PAIRING_STATE: + if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) { +#ifdef DEBUG_USB_HOST + Notify(PSTR("\r\nSimple pairing was enabled"), 0x80); +#endif if(btdName != NULL) { - hci_set_local_name(btdName); - hci_state = HCI_SET_NAME_STATE; + hci_write_local_name(btdName); + hci_state = HCI_WRITE_NAME_STATE; } else hci_state = HCI_CHECK_DEVICE_SERVICE; } break; - case HCI_SET_NAME_STATE: + case HCI_WRITE_NAME_STATE: if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) { #ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nThe name is set to: "), 0x80); + Notify(PSTR("\r\nThe name was set to: "), 0x80); NotifyStr(btdName, 0x80); #endif hci_state = HCI_CHECK_DEVICE_SERVICE; @@ -1179,7 +1189,7 @@ void BTD::hci_read_remote_extended_features(uint8_t page_number) { HCI_Command(hcibuf, 6); } -void BTD::hci_set_local_name(const char* name) { +void BTD::hci_write_local_name(const char* name) { hcibuf[0] = 0x13; // HCI OCF = 13 hcibuf[1] = 0x03 << 2; // HCI OGF = 3 hcibuf[2] = strlen(name) + 1; // parameter length = the length of the string + end byte @@ -1191,6 +1201,15 @@ void BTD::hci_set_local_name(const char* name) { HCI_Command(hcibuf, 4 + strlen(name)); } +void BTD::hci_write_simple_pairing_mode(bool enable) { + hcibuf[0] = 0x56; // HCI OCF = 56 + hcibuf[1] = 0x03 << 2; // HCI OGF = 3 + hcibuf[2] = 1; // parameter length = 1 + hcibuf[3] = enable ? 1 : 0; + + HCI_Command(hcibuf, 4); +} + void BTD::hci_inquiry() { hci_clear_flag(HCI_FLAG_DEVICE_FOUND); hcibuf[0] = 0x01; diff --git a/BTD.h b/BTD.h index e7ab9f96..ed179937 100644 --- a/BTD.h +++ b/BTD.h @@ -45,7 +45,7 @@ #define HCI_CLASS_STATE 2 #define HCI_BDADDR_STATE 3 #define HCI_LOCAL_VERSION_STATE 4 -#define HCI_SET_NAME_STATE 5 +#define HCI_WRITE_NAME_STATE 5 #define HCI_CHECK_DEVICE_SERVICE 6 #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device @@ -59,8 +59,9 @@ #define HCI_DISABLE_SCAN_STATE 14 #define HCI_DONE_STATE 15 #define HCI_DISCONNECT_STATE 16 -#define HCI_LOCAL_EXTENDED_FEATURES_STATE 17 -#define HCI_REMOTE_EXTENDED_FEATURES_STATE 18 +#define HCI_LOCAL_EXTENDED_FEATURES_STATE 17 +#define HCI_WRITE_SIMPLE_PAIRING_STATE 18 +#define HCI_REMOTE_EXTENDED_FEATURES_STATE 19 /* HCI event flags*/ #define HCI_FLAG_CMD_COMPLETE (1UL << 0) @@ -72,7 +73,7 @@ #define HCI_FLAG_READ_VERSION (1UL << 6) #define HCI_FLAG_DEVICE_FOUND (1UL << 7) #define HCI_FLAG_CONNECT_EVENT (1UL << 8) -#define HCI_FLAG_LOCAL_EXTENDED_FEATURES (1UL << 9) +#define HCI_FLAG_LOCAL_EXTENDED_FEATURES (1UL << 9) #define HCI_FLAG_REMOTE_EXTENDED_FEATURES (1UL << 10) /* Macros for HCI event flag tests */ @@ -333,7 +334,9 @@ public: * Set the local name of the Bluetooth dongle. * @param name Desired name. */ - void hci_set_local_name(const char* name); + void hci_write_local_name(const char* name); + + void hci_write_simple_pairing_mode(bool enable); /** Enable visibility to other Bluetooth devices. */ void hci_write_scan_enable(); /** Disable visibility to other Bluetooth devices. */ diff --git a/BTHID.cpp b/BTHID.cpp index d32e9aa6..225aa1f8 100644 --- a/BTHID.cpp +++ b/BTHID.cpp @@ -17,7 +17,7 @@ #include "BTHID.h" // To enable serial debugging see "settings.h" -//#define EXTRADEBUG // Uncomment to get even more debugging data +#define EXTRADEBUG // Uncomment to get even more debugging data //#define PRINTREPORT // Uncomment to print the report send by the HID device BTHID::BTHID(BTD *p, bool pair, const char *pin) : @@ -54,6 +54,18 @@ void BTHID::disconnect() { // Use this void to disconnect the device } void BTHID::ACLData(uint8_t* l2capinbuf) { + Notify(PSTR("\r\nL2CAP Data - Channel ID: "), 0x80); + D_PrintHex (l2capinbuf[7], 0x80); + Notify(PSTR(" "), 0x80); + D_PrintHex (l2capinbuf[6], 0x80); + + Notify(PSTR("\r\nData: "), 0x80); + Notify(PSTR("\r\n"), 0x80); + for(uint16_t i = 0; i < ((uint16_t)l2capinbuf[5] << 8 | l2capinbuf[4]); i++) { + D_PrintHex (l2capinbuf[i + 8], 0x80); + Notify(PSTR(" "), 0x80); + } + if(!pBtd->l2capConnectionClaimed && pBtd->incomingHIDDevice && !connected && !activeConnection) { if(l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) { if((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) {