Skip to content

Commit

Permalink
Use more readable macros in the BTD class as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauszus committed Dec 11, 2013
1 parent 712bc38 commit 6909ecd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
66 changes: 34 additions & 32 deletions BTD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,14 @@ void BTD::HCI_event_task() {
{
case EV_COMMAND_COMPLETE:
if (!hcibuf[5]) { // Check if command succeeded
hci_event_flag |= HCI_FLAG_CMD_COMPLETE; // Set command complete flag
hci_set_flag(HCI_FLAG_CMD_COMPLETE); // Set command complete flag
if ((hcibuf[3] == 0x01) && (hcibuf[4] == 0x10)) { // Parameters from read local version information
hci_version = hcibuf[6]; // Used to check if it supports 2.0+EDR - see http://www.bluetooth.org/Technical/AssignedNumbers/hci.htm
hci_event_flag |= HCI_FLAG_READ_VERSION;
hci_set_flag(HCI_FLAG_READ_VERSION);
} else if ((hcibuf[3] == 0x09) && (hcibuf[4] == 0x10)) { // Parameters from read local bluetooth address
for (uint8_t i = 0; i < 6; i++)
my_bdaddr[i] = hcibuf[6 + i];
hci_event_flag |= HCI_FLAG_READ_BDADDR;
hci_set_flag(HCI_FLAG_READ_BDADDR);
}
}
break;
Expand Down Expand Up @@ -449,7 +449,7 @@ void BTD::HCI_event_task() {
for (uint8_t j = 0; j < 6; j++)
disc_bdaddr[j] = hcibuf[j + 3 + 6 * i];

hci_event_flag |= HCI_FLAG_DEVICE_FOUND;
hci_set_flag(HCI_FLAG_DEVICE_FOUND);
break;
} else if (pairWithHIDDevice && (classOfDevice[1] & 0x05) && (classOfDevice[0] & 0xC0)) { // Check if it is a mouse or keyboard - see: http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
#ifdef DEBUG_USB_HOST
Expand All @@ -462,7 +462,7 @@ void BTD::HCI_event_task() {
for (uint8_t j = 0; j < 6; j++)
disc_bdaddr[j] = hcibuf[j + 3 + 6 * i];

hci_event_flag |= HCI_FLAG_DEVICE_FOUND;
hci_set_flag(HCI_FLAG_DEVICE_FOUND);
}
#ifdef EXTRADEBUG
else {
Expand All @@ -479,13 +479,13 @@ void BTD::HCI_event_task() {
break;

case EV_CONNECT_COMPLETE:
hci_event_flag |= HCI_FLAG_CONNECT_EVENT;
hci_set_flag(HCI_FLAG_CONNECT_EVENT);
if (!hcibuf[2]) { // Check if connected OK
#ifdef EXTRADEBUG
Notify(PSTR("\r\nConnection established"), 0x80);
#endif
hci_handle = hcibuf[3] | ((hcibuf[4] & 0x0F) << 8); // Store the handle for the ACL connection
hci_event_flag |= HCI_FLAG_CONN_COMPLETE; // Set connection complete flag
hci_set_flag(HCI_FLAG_CONNECT_COMPLETE); // Set connection complete flag
} else {
hci_state = HCI_CHECK_DEVICE_SERVICE;
#ifdef DEBUG_USB_HOST
Expand All @@ -497,8 +497,8 @@ void BTD::HCI_event_task() {

case EV_DISCONNECT_COMPLETE:
if (!hcibuf[2]) { // Check if disconnected OK
hci_event_flag |= HCI_FLAG_DISCONN_COMPLETE; // Set disconnect command complete flag
hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE; // Clear connection complete flag
hci_set_flag(HCI_FLAG_DISCONNECT_COMPLETE); // Set disconnect command complete flag
hci_clear_flag(HCI_FLAG_CONNECT_COMPLETE); // Clear connection complete flag
}
break;

Expand All @@ -509,7 +509,7 @@ void BTD::HCI_event_task() {
if (remote_name[i] == '\0') // End of string
break;
}
hci_event_flag |= HCI_FLAG_REMOTE_NAME_COMPLETE;
hci_set_flag(HCI_FLAG_REMOTE_NAME_COMPLETE);
}
break;

Expand All @@ -535,7 +535,7 @@ void BTD::HCI_event_task() {
Notify(PSTR(" "), 0x80);
D_PrintHex<uint8_t > (hcibuf[8], 0x80);
#endif
hci_event_flag |= HCI_FLAG_INCOMING_REQUEST;
hci_set_flag(HCI_FLAG_INCOMING_REQUEST);
break;

case EV_PIN_CODE_REQUEST:
Expand Down Expand Up @@ -624,7 +624,7 @@ void BTD::HCI_task() {

case HCI_RESET_STATE:
hci_counter++;
if (hci_cmd_complete) {
if (hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
hci_counter = 0;
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nHCI Reset complete"), 0x80);
Expand All @@ -644,7 +644,7 @@ void BTD::HCI_task() {
break;

case HCI_CLASS_STATE:
if (hci_cmd_complete) {
if (hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nWrite class of device"), 0x80);
#endif
Expand All @@ -654,7 +654,7 @@ void BTD::HCI_task() {
break;

case HCI_BDADDR_STATE:
if (hci_read_bdaddr_complete) {
if (hci_check_flag(HCI_FLAG_READ_BDADDR)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nLocal Bluetooth Address: "), 0x80);
for (int8_t i = 5; i > 0; i--) {
Expand All @@ -669,7 +669,7 @@ void BTD::HCI_task() {
break;

case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class
if (hci_read_version_complete) {
if (hci_check_flag(HCI_FLAG_READ_VERSION)) {
if (btdName != NULL) {
hci_set_local_name(btdName);
hci_state = HCI_SET_NAME_STATE;
Expand All @@ -679,7 +679,7 @@ void BTD::HCI_task() {
break;

case HCI_SET_NAME_STATE:
if (hci_cmd_complete) {
if (hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nThe name is set to: "), 0x80);
NotifyStr(btdName, 0x80);
Expand All @@ -703,7 +703,7 @@ void BTD::HCI_task() {
break;

case HCI_INQUIRY_STATE:
if (hci_device_found) {
if (hci_check_flag(HCI_FLAG_DEVICE_FOUND)) {
hci_inquiry_cancel(); // Stop inquiry
#ifdef DEBUG_USB_HOST
if (pairWithWii)
Expand Down Expand Up @@ -732,7 +732,7 @@ void BTD::HCI_task() {
break;

case HCI_CONNECT_DEVICE_STATE:
if (hci_cmd_complete) {
if (hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
#ifdef DEBUG_USB_HOST
if (pairWithWii)
Notify(PSTR("\r\nConnecting to Wiimote"), 0x80);
Expand All @@ -745,8 +745,8 @@ void BTD::HCI_task() {
break;

case HCI_CONNECTED_DEVICE_STATE:
if (hci_connect_event) {
if (hci_connect_complete) {
if (hci_check_flag(HCI_FLAG_CONNECT_EVENT)) {
if (hci_check_flag(HCI_FLAG_CONNECT_COMPLETE)) {
#ifdef DEBUG_USB_HOST
if (pairWithWii)
Notify(PSTR("\r\nConnected to Wiimote"), 0x80);
Expand Down Expand Up @@ -776,19 +776,19 @@ void BTD::HCI_task() {
break;

case HCI_CONNECT_IN_STATE:
if (hci_incoming_connect_request) {
if (hci_check_flag(HCI_FLAG_INCOMING_REQUEST)) {
watingForConnection = false;
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nIncoming Connection Request"), 0x80);
#endif
hci_remote_name();
hci_state = HCI_REMOTE_NAME_STATE;
} else if (hci_disconnect_complete)
} else if (hci_check_flag(HCI_FLAG_DISCONNECT_COMPLETE))
hci_state = HCI_DISCONNECT_STATE;
break;

case HCI_REMOTE_NAME_STATE:
if (hci_remote_name_complete) {
if (hci_check_flag(HCI_FLAG_REMOTE_NAME_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nRemote Name: "), 0x80);
for (uint8_t i = 0; i < 30; i++) {
Expand Down Expand Up @@ -828,7 +828,7 @@ void BTD::HCI_task() {
break;

case HCI_CONNECTED_STATE:
if (hci_connect_complete) {
if (hci_check_flag(HCI_FLAG_CONNECT_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nConnected to Device: "), 0x80);
for (int8_t i = 5; i > 0; i--) {
Expand Down Expand Up @@ -856,7 +856,7 @@ void BTD::HCI_task() {
break;

case HCI_DISCONNECT_STATE:
if (hci_disconnect_complete) {
if (hci_check_flag(HCI_FLAG_DISCONNECT_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nHCI Disconnected from Device"), 0x80);
#endif
Expand Down Expand Up @@ -901,7 +901,7 @@ void BTD::ACL_event_task() {

/************************************************************/
void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) {
hci_event_flag &= ~HCI_FLAG_CMD_COMPLETE;
hci_clear_flag(HCI_FLAG_CMD_COMPLETE);
pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
}

Expand All @@ -915,7 +915,7 @@ void BTD::hci_reset() {
}

void BTD::hci_write_scan_enable() {
hci_event_flag &= ~HCI_FLAG_INCOMING_REQUEST;
hci_clear_flag(HCI_FLAG_INCOMING_REQUEST);
hcibuf[0] = 0x1A; // HCI OCF = 1A
hcibuf[1] = 0x03 << 2; // HCI OGF = 3
hcibuf[2] = 0x01; // parameter length = 1
Expand All @@ -937,6 +937,7 @@ void BTD::hci_write_scan_disable() {
}

void BTD::hci_read_bdaddr() {
hci_clear_flag(HCI_FLAG_READ_BDADDR);
hcibuf[0] = 0x09; // HCI OCF = 9
hcibuf[1] = 0x04 << 2; // HCI OGF = 4
hcibuf[2] = 0x00;
Expand All @@ -945,6 +946,7 @@ void BTD::hci_read_bdaddr() {
}

void BTD::hci_read_local_version_information() {
hci_clear_flag(HCI_FLAG_READ_VERSION);
hcibuf[0] = 0x01; // HCI OCF = 1
hcibuf[1] = 0x04 << 2; // HCI OGF = 4
hcibuf[2] = 0x00;
Expand All @@ -953,7 +955,7 @@ void BTD::hci_read_local_version_information() {
}

void BTD::hci_accept_connection() {
hci_event_flag &= ~HCI_FLAG_CONN_COMPLETE;
hci_clear_flag(HCI_FLAG_CONNECT_COMPLETE);
hcibuf[0] = 0x09; // HCI OCF = 9
hcibuf[1] = 0x01 << 2; // HCI OGF = 1
hcibuf[2] = 0x07; // parameter length 7
Expand All @@ -969,7 +971,7 @@ void BTD::hci_accept_connection() {
}

void BTD::hci_remote_name() {
hci_event_flag &= ~HCI_FLAG_REMOTE_NAME_COMPLETE;
hci_clear_flag(HCI_FLAG_REMOTE_NAME_COMPLETE);
hcibuf[0] = 0x19; // HCI OCF = 19
hcibuf[1] = 0x01 << 2; // HCI OGF = 1
hcibuf[2] = 0x0A; // parameter length = 10
Expand Down Expand Up @@ -1000,7 +1002,7 @@ void BTD::hci_set_local_name(const char* name) {
}

void BTD::hci_inquiry() {
hci_event_flag &= ~HCI_FLAG_DEVICE_FOUND;
hci_clear_flag(HCI_FLAG_DEVICE_FOUND);
hcibuf[0] = 0x01;
hcibuf[1] = 0x01 << 2; // HCI OGF = 1
hcibuf[2] = 0x05; // Parameter Total Length = 5
Expand All @@ -1026,7 +1028,7 @@ void BTD::hci_connect() {
}

void BTD::hci_connect(uint8_t *bdaddr) {
hci_event_flag &= ~(HCI_FLAG_CONN_COMPLETE | HCI_FLAG_CONNECT_EVENT);
hci_clear_flag(HCI_FLAG_CONNECT_COMPLETE | HCI_FLAG_CONNECT_EVENT);
hcibuf[0] = 0x05;
hcibuf[1] = 0x01 << 2; // HCI OGF = 1
hcibuf[2] = 0x0D; // parameter Total Length = 13
Expand Down Expand Up @@ -1122,7 +1124,7 @@ void BTD::hci_authentication_request() {
}

void BTD::hci_disconnect(uint16_t handle) { // This is called by the different services
hci_event_flag &= ~HCI_FLAG_DISCONN_COMPLETE;
hci_clear_flag(HCI_FLAG_DISCONNECT_COMPLETE);
hcibuf[0] = 0x06; // HCI OCF = 6
hcibuf[1] = 0x01 << 2; // HCI OGF = 1
hcibuf[2] = 0x03; // parameter length = 3
Expand Down
25 changes: 11 additions & 14 deletions BTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,19 @@

/* HCI event flags*/
#define HCI_FLAG_CMD_COMPLETE 0x01
#define HCI_FLAG_CONN_COMPLETE 0x02
#define HCI_FLAG_DISCONN_COMPLETE 0x04
#define HCI_FLAG_CONNECT_COMPLETE 0x02
#define HCI_FLAG_DISCONNECT_COMPLETE 0x04
#define HCI_FLAG_REMOTE_NAME_COMPLETE 0x08
#define HCI_FLAG_INCOMING_REQUEST 0x10
#define HCI_FLAG_READ_BDADDR 0x20
#define HCI_FLAG_READ_VERSION 0x40
#define HCI_FLAG_DEVICE_FOUND 0x80
#define HCI_FLAG_CONNECT_EVENT 0x100

/*Macros for HCI event flag tests */
#define hci_cmd_complete (hci_event_flag & HCI_FLAG_CMD_COMPLETE)
#define hci_connect_complete (hci_event_flag & HCI_FLAG_CONN_COMPLETE)
#define hci_disconnect_complete (hci_event_flag & HCI_FLAG_DISCONN_COMPLETE)
#define hci_remote_name_complete (hci_event_flag & HCI_FLAG_REMOTE_NAME_COMPLETE)
#define hci_incoming_connect_request (hci_event_flag & HCI_FLAG_INCOMING_REQUEST)
#define hci_read_bdaddr_complete (hci_event_flag & HCI_FLAG_READ_BDADDR)
#define hci_read_version_complete (hci_event_flag & HCI_FLAG_READ_VERSION)
#define hci_device_found (hci_event_flag & HCI_FLAG_DEVICE_FOUND)
#define hci_connect_event (hci_event_flag & HCI_FLAG_CONNECT_EVENT)
/* Macros for HCI event flag tests */
#define hci_check_flag(flag) (hci_event_flag & flag)
#define hci_set_flag(flag) (hci_event_flag |= flag)
#define hci_clear_flag(flag) (hci_event_flag &= ~flag)

/* HCI Events managed */
#define EV_INQUIRY_COMPLETE 0x01
Expand Down Expand Up @@ -141,22 +135,25 @@
#define WII_CHECK_EXTENSION_STATE 22
#define WII_INIT_MOTION_PLUS_STATE 23

/* L2CAP event flags */
/* L2CAP event flags for HID Control channel */
#define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x00000001
#define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x00000002
#define L2CAP_FLAG_CONTROL_CONNECTED 0x00000004
#define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x00000008

/* L2CAP event flags for HID Interrupt channel */
#define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x00000010
#define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x00000020
#define L2CAP_FLAG_INTERRUPT_CONNECTED 0x00000040
#define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x00000080

/* L2CAP event flags for SDP channel */
#define L2CAP_FLAG_CONNECTION_SDP_REQUEST 0x00000100
#define L2CAP_FLAG_CONFIG_SDP_REQUEST 0x00000200
#define L2CAP_FLAG_CONFIG_SDP_SUCCESS 0x00000400
#define L2CAP_FLAG_DISCONNECT_SDP_REQUEST 0x00000800

/* L2CAP event flags for RFCOMM channel */
#define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST 0x00001000
#define L2CAP_FLAG_CONFIG_RFCOMM_REQUEST 0x00002000
#define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS 0x00004000
Expand Down Expand Up @@ -467,7 +464,7 @@ class BTD : public USBDeviceConfig, public UsbConfigXtracter {

/** The name you wish to make the dongle show up as. It is set automatically by the SPP library. */
const char* btdName;
/** The pin you wish to make the dongle use for authentication. It is set automatically by the SPP library. */
/** The pin you wish to make the dongle use for authentication. It is set automatically by the SPP and BTHID library. */
const char* btdPin;

/** The bluetooth dongles Bluetooth address. */
Expand Down

0 comments on commit 6909ecd

Please sign in to comment.