From 106aff64118dc83a89bb1974fe6ff83ee727ebef Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Tue, 9 Sep 2014 16:32:46 -0700 Subject: [PATCH] Call registerBluetoothService in BluetoothService constructor I needed to move the BluetoothService class down after the BTD class in order for it to work --- BTD.cpp | 6 +++ BTD.h | 115 ++++++++++++++++++++++++++---------------------------- BTHID.cpp | 3 -- PS3BT.cpp | 3 -- SPP.cpp | 3 -- Wii.cpp | 7 +--- 6 files changed, 64 insertions(+), 73 deletions(-) diff --git a/BTD.cpp b/BTD.cpp index 0bc8591f..42cf3d62 100755 --- a/BTD.cpp +++ b/BTD.cpp @@ -380,6 +380,12 @@ uint8_t BTD::Poll() { return 0; } +void BTD::disconnect() { + for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) + if(btService[i]) + btService[i]->disconnect(); +}; + void BTD::HCI_event_task() { uint16_t length = BULK_MAXPKTSIZE; // Request more than 16 bytes anyway, the inTransfer routine will take care of this uint8_t rcode = pUsb->inTransfer(bAddress, epInfo[ BTD_EVENT_PIPE ].epAddr, &length, hcibuf); // Input on endpoint 1 diff --git a/BTD.h b/BTD.h index 37191827..806bb63e 100755 --- a/BTD.h +++ b/BTD.h @@ -204,58 +204,7 @@ */ #define UHS_ACL_HANDLE_OK(x, y) ((x[0] == (y & 0xff)) && (x[1] == ((y >> 8) | 0x20))) #endif - -class BTD; - -/** All Bluetooth services should inherit this class. */ -class BluetoothService { -public: - BluetoothService(BTD *p) : pBtd(p) {}; - /** - * Used to pass acldata to the Bluetooth service. - * @param ACLData Pointer to the incoming acldata. - */ - virtual void ACLData(uint8_t* ACLData) = 0; - /** Used to run the different state machines in the Bluetooth service. */ - virtual void Run() = 0; - /** Used to reset the Bluetooth service. */ - virtual void Reset() = 0; - /** Used to disconnect both the L2CAP Channel and the HCI Connection for the Bluetooth service. */ - virtual void disconnect() = 0; - - /** - * Used to call your own function when the device is successfully initialized. - * @param funcOnInit Function to call. - */ - void attachOnInit(void (*funcOnInit)(void)) { - pFuncOnInit = funcOnInit; // TODO: This really belong in a class of it's own as it is repeated several times - }; - -protected: - /** - * Called when a device is successfully initialized. - * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. - * This is useful for instance if you want to set the LEDs in a specific way. - */ - virtual void onInit() = 0; - - // TODO: Implement "UHS_ACL_HANDLE_OK" function - - /** Pointer to function called in onInit(). */ - void (*pFuncOnInit)(void); - - /** Pointer to BTD instance. */ - BTD *pBtd; - - /** The HCI Handle for the connection. */ - uint16_t hci_handle; - - /** L2CAP flags of received Bluetooth events. */ - uint32_t l2cap_event_flag; - - /** Identifier for L2CAP commands. */ - uint8_t identifier; -}; +class BluetoothService; /** * The Bluetooth Dongle class will take care of all the USB communication @@ -353,25 +302,21 @@ public: /**@}*/ /** Disconnects both the L2CAP Channel and the HCI Connection for all Bluetooth services. */ - void disconnect() { - for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) - if(btService[i]) - btService[i]->disconnect(); - }; + void disconnect(); /** * Register Bluetooth dongle members/services. * @param pService Pointer to BluetoothService class instance. * @return The service ID on success or -1 on fail. */ - int8_t registerServiceClass(BluetoothService *pService) { + int8_t registerBluetoothService(BluetoothService *pService) { for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) { if(!btService[i]) { btService[i] = pService; return i; // Return ID } } - return -1; // ErrorregisterServiceClass + return -1; // Error registering BluetoothService }; /** @name HCI Commands */ @@ -628,4 +573,56 @@ private: void setBdaddr(uint8_t* BDADDR); void setMoveBdaddr(uint8_t* BDADDR); }; + +/** All Bluetooth services should inherit this class. */ +class BluetoothService { +public: + BluetoothService(BTD *p) : pBtd(p) { + if(pBtd) + pBtd->registerBluetoothService(this); // Register it as a Bluetooth service + }; + /** + * Used to pass acldata to the Bluetooth service. + * @param ACLData Pointer to the incoming acldata. + */ + virtual void ACLData(uint8_t* ACLData) = 0; + /** Used to run the different state machines in the Bluetooth service. */ + virtual void Run() = 0; + /** Used to reset the Bluetooth service. */ + virtual void Reset() = 0; + /** Used to disconnect both the L2CAP Channel and the HCI Connection for the Bluetooth service. */ + virtual void disconnect() = 0; + + /** + * Used to call your own function when the device is successfully initialized. + * @param funcOnInit Function to call. + */ + void attachOnInit(void (*funcOnInit)(void)) { + pFuncOnInit = funcOnInit; // TODO: This really belong in a class of it's own as it is repeated several times + }; + +protected: + /** + * Called when a device is successfully initialized. + * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. + * This is useful for instance if you want to set the LEDs in a specific way. + */ + virtual void onInit() = 0; + + /** Pointer to function called in onInit(). */ + void (*pFuncOnInit)(void); + + /** Pointer to BTD instance. */ + BTD *pBtd; + + /** The HCI Handle for the connection. */ + uint16_t hci_handle; + + /** L2CAP flags of received Bluetooth events. */ + uint32_t l2cap_event_flag; + + /** Identifier for L2CAP commands. */ + uint8_t identifier; +}; + #endif diff --git a/BTHID.cpp b/BTHID.cpp index 3a625b2d..e5a86b47 100644 --- a/BTHID.cpp +++ b/BTHID.cpp @@ -26,9 +26,6 @@ protocolMode(HID_BOOT_PROTOCOL) { for(uint8_t i = 0; i < NUM_PARSERS; i++) pRptParser[i] = NULL; - if(pBtd) - pBtd->registerServiceClass(this); // Register it as a Bluetooth service - pBtd->pairWithHIDDevice = pair; pBtd->btdPin = pin; diff --git a/PS3BT.cpp b/PS3BT.cpp index 74928ca7..73e2c1e6 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -23,9 +23,6 @@ PS3BT::PS3BT(BTD *p, uint8_t btadr5, uint8_t btadr4, uint8_t btadr3, uint8_t btadr2, uint8_t btadr1, uint8_t btadr0) : BluetoothService(p) // Pointer to USB class instance - mandatory { - if(pBtd) - pBtd->registerServiceClass(this); // Register it as a Bluetooth service - pBtd->my_bdaddr[5] = btadr5; // Change to your dongle's Bluetooth address instead pBtd->my_bdaddr[4] = btadr4; pBtd->my_bdaddr[3] = btadr3; diff --git a/SPP.cpp b/SPP.cpp index 3262a3e3..cd5072da 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -45,9 +45,6 @@ const uint8_t rfcomm_crc_table[256] PROGMEM = {/* reversed, 8-bit, poly=0x07 */ SPP::SPP(BTD *p, const char* name, const char* pin) : BluetoothService(p) // Pointer to BTD class instance - mandatory { - if(pBtd) - pBtd->registerServiceClass(this); // Register it as a Bluetooth service - pBtd->btdName = name; pBtd->btdPin = pin; diff --git a/Wii.cpp b/Wii.cpp index 25a760f7..742a67b5 100755 --- a/Wii.cpp +++ b/Wii.cpp @@ -85,17 +85,14 @@ const uint32_t WII_PROCONTROLLER_BUTTONS[] PROGMEM = { WII::WII(BTD *p, bool pair) : BluetoothService(p) // Pointer to USB class instance - mandatory { - if(pBtd) - pBtd->registerServiceClass(this); // Register it as a Bluetooth service - pBtd->pairWithWii = pair; HIDBuffer[0] = 0xA2; // HID BT DATA_request (0xA0) | Report Type (Output 0x02) /* Set device cid for the control and intterrupt channelse - LSB */ - control_dcid[0] = 0x60; //0x0060 + control_dcid[0] = 0x60; // 0x0060 control_dcid[1] = 0x00; - interrupt_dcid[0] = 0x61; //0x0061 + interrupt_dcid[0] = 0x61; // 0x0061 interrupt_dcid[1] = 0x00; Reset();