Enable simple paring mode

This commit is contained in:
Kristian Sloth Lauszus 2019-07-02 20:30:33 +02:00
parent 30ac619331
commit a347b3bace
3 changed files with 51 additions and 17 deletions

41
BTD.cpp
View file

@ -430,19 +430,19 @@ void BTD::HCI_event_task() {
#endif #endif
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
if(hcibuf[6] == 0) { // Page 0 if(hcibuf[6] == 0) { // Page 0
Notify(PSTR("\r\nLocal "), 0x80); Notify(PSTR("\r\nDongle "), 0x80);
if(hcibuf[8 + 6] & (1U << 3)) if(hcibuf[8 + 6] & (1U << 3))
Notify(PSTR("supports"), 0x80); Notify(PSTR("supports"), 0x80);
else else
Notify(PSTR("does NOT support"), 0x80); 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 } else if(hcibuf[6] == 1) { // Page 1
Notify(PSTR("\r\nLocal "), 0x80); Notify(PSTR("\r\nDongle "), 0x80);
if(hcibuf[8 + 0] & (1U << 0)) if(hcibuf[8 + 0] & (1U << 0))
Notify(PSTR("supports"), 0x80); Notify(PSTR("supports"), 0x80);
else else
Notify(PSTR("does NOT support"), 0x80); Notify(PSTR("does NOT support"), 0x80);
Notify(PSTR(" secure simple paring (host support)"), 0x80); Notify(PSTR(" secure simple pairing (host support)"), 0x80);
} }
#endif #endif
} }
@ -671,14 +671,14 @@ void BTD::HCI_event_task() {
Notify(PSTR("supports"), 0x80); Notify(PSTR("supports"), 0x80);
else else
Notify(PSTR("does NOT support"), 0x80); 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 } else if(hcibuf[5] == 1) { // Page 1
Notify(PSTR("\r\nRemote "), 0x80); Notify(PSTR("\r\nRemote "), 0x80);
if(hcibuf[7 + 0] & (1U << 0)) if(hcibuf[7 + 0] & (1U << 0))
Notify(PSTR("supports"), 0x80); Notify(PSTR("supports"), 0x80);
else else
Notify(PSTR("\r\ndoes NOT support"), 0x80); Notify(PSTR("\r\ndoes NOT support"), 0x80);
Notify(PSTR(" secure simple paring (host support)"), 0x80); Notify(PSTR(" secure simple pairing (host support)"), 0x80);
} }
#endif #endif
} }
@ -814,18 +814,28 @@ void BTD::HCI_task() {
case HCI_LOCAL_EXTENDED_FEATURES_STATE: case HCI_LOCAL_EXTENDED_FEATURES_STATE:
if(hci_check_flag(HCI_FLAG_LOCAL_EXTENDED_FEATURES)) { 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) { if(btdName != NULL) {
hci_set_local_name(btdName); hci_write_local_name(btdName);
hci_state = HCI_SET_NAME_STATE; hci_state = HCI_WRITE_NAME_STATE;
} else } else
hci_state = HCI_CHECK_DEVICE_SERVICE; hci_state = HCI_CHECK_DEVICE_SERVICE;
} }
break; break;
case HCI_SET_NAME_STATE: case HCI_WRITE_NAME_STATE:
if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) { if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
#ifdef DEBUG_USB_HOST #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); NotifyStr(btdName, 0x80);
#endif #endif
hci_state = HCI_CHECK_DEVICE_SERVICE; 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); 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[0] = 0x13; // HCI OCF = 13
hcibuf[1] = 0x03 << 2; // HCI OGF = 3 hcibuf[1] = 0x03 << 2; // HCI OGF = 3
hcibuf[2] = strlen(name) + 1; // parameter length = the length of the string + end byte 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)); 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() { void BTD::hci_inquiry() {
hci_clear_flag(HCI_FLAG_DEVICE_FOUND); hci_clear_flag(HCI_FLAG_DEVICE_FOUND);
hcibuf[0] = 0x01; hcibuf[0] = 0x01;

13
BTD.h
View file

@ -45,7 +45,7 @@
#define HCI_CLASS_STATE 2 #define HCI_CLASS_STATE 2
#define HCI_BDADDR_STATE 3 #define HCI_BDADDR_STATE 3
#define HCI_LOCAL_VERSION_STATE 4 #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_CHECK_DEVICE_SERVICE 6
#define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device #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_DISABLE_SCAN_STATE 14
#define HCI_DONE_STATE 15 #define HCI_DONE_STATE 15
#define HCI_DISCONNECT_STATE 16 #define HCI_DISCONNECT_STATE 16
#define HCI_LOCAL_EXTENDED_FEATURES_STATE 17 #define HCI_LOCAL_EXTENDED_FEATURES_STATE 17
#define HCI_REMOTE_EXTENDED_FEATURES_STATE 18 #define HCI_WRITE_SIMPLE_PAIRING_STATE 18
#define HCI_REMOTE_EXTENDED_FEATURES_STATE 19
/* HCI event flags*/ /* HCI event flags*/
#define HCI_FLAG_CMD_COMPLETE (1UL << 0) #define HCI_FLAG_CMD_COMPLETE (1UL << 0)
@ -72,7 +73,7 @@
#define HCI_FLAG_READ_VERSION (1UL << 6) #define HCI_FLAG_READ_VERSION (1UL << 6)
#define HCI_FLAG_DEVICE_FOUND (1UL << 7) #define HCI_FLAG_DEVICE_FOUND (1UL << 7)
#define HCI_FLAG_CONNECT_EVENT (1UL << 8) #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) #define HCI_FLAG_REMOTE_EXTENDED_FEATURES (1UL << 10)
/* Macros for HCI event flag tests */ /* Macros for HCI event flag tests */
@ -333,7 +334,9 @@ public:
* Set the local name of the Bluetooth dongle. * Set the local name of the Bluetooth dongle.
* @param name Desired name. * @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. */ /** Enable visibility to other Bluetooth devices. */
void hci_write_scan_enable(); void hci_write_scan_enable();
/** Disable visibility to other Bluetooth devices. */ /** Disable visibility to other Bluetooth devices. */

View file

@ -17,7 +17,7 @@
#include "BTHID.h" #include "BTHID.h"
// To enable serial debugging see "settings.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 //#define PRINTREPORT // Uncomment to print the report send by the HID device
BTHID::BTHID(BTD *p, bool pair, const char *pin) : 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) { 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(!pBtd->l2capConnectionClaimed && pBtd->incomingHIDDevice && !connected && !activeConnection) {
if(l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) { if(l2capinbuf[8] == L2CAP_CMD_CONNECTION_REQUEST) {
if((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) { if((l2capinbuf[12] | (l2capinbuf[13] << 8)) == HID_CTRL_PSM) {