Skip to content

Commit

Permalink
Enable simple paring mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauszus committed Nov 15, 2020
1 parent 30ac619 commit a347b3b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
41 changes: 30 additions & 11 deletions BTD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions BTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 */
Expand Down Expand Up @@ -333,7 +334,9 @@ class BTD : public USBDeviceConfig, public UsbConfigXtracter {
* 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. */
Expand Down
14 changes: 13 additions & 1 deletion BTHID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand Down Expand Up @@ -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<uint8_t > (l2capinbuf[7], 0x80);
Notify(PSTR(" "), 0x80);
D_PrintHex<uint8_t > (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<uint8_t > (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) {
Expand Down

0 comments on commit a347b3b

Please sign in to comment.