diff --git a/src/servers/bluetooth/BluetoothServer.cpp b/src/servers/bluetooth/BluetoothServer.cpp index 71e0ef5ad6..7b5458ba20 100644 --- a/src/servers/bluetooth/BluetoothServer.cpp +++ b/src/servers/bluetooth/BluetoothServer.cpp @@ -77,10 +77,10 @@ bool BluetoothServer::QuitRequested(void) Output::Instance()->Lock(); Output::Instance()->Quit(); - LocalDeviceImpl* ldi = NULL; - while ((ldi = (LocalDeviceImpl *)fLocalDevicesList.RemoveItem((int32)0)) + LocalDeviceImpl* lDeviceImpl = NULL; + while ((lDeviceImpl = (LocalDeviceImpl *)fLocalDevicesList.RemoveItem((int32)0)) != NULL) - delete ldi; + delete lDeviceImpl; printf("Accepting quitting of the application\n"); return BApplication::QuitRequested(); @@ -132,13 +132,13 @@ void BluetoothServer::MessageReceived(BMessage *message) BPath path(str.String()); Output::Instance()->Postf(BLACKBOARD_GENERAL, "Requested LocalDevice %s\n", str.String()); - LocalDeviceImpl* ldi = LocalDeviceImpl::CreateTransportAccessor(&path); + LocalDeviceImpl* lDeviceImpl = LocalDeviceImpl::CreateTransportAccessor(&path); - if (ldi->GetID() >= 0) { - fLocalDevicesList.AddItem(ldi); - Output::Instance()->AddTab("Local Device", BLACKBOARD_LD(ldi->GetID())); - Output::Instance()->Postf(BLACKBOARD_LD(ldi->GetID()), - "LocalDevice %s id=%x added\n", str.String(), ldi->GetID()); + if (lDeviceImpl->GetID() >= 0) { + fLocalDevicesList.AddItem(lDeviceImpl); + Output::Instance()->AddTab("Local Device", BLACKBOARD_LD(lDeviceImpl->GetID())); + Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()), + "LocalDevice %s id=%x added\n", str.String(), lDeviceImpl->GetID()); } else { Output::Instance()->Post("Adding LocalDevice failed\n", @@ -147,16 +147,16 @@ void BluetoothServer::MessageReceived(BMessage *message) status = B_WOULD_BLOCK; /* TODO: This should be by user request only! */ - ldi->Launch(); + lDeviceImpl->Launch(); break; } case BT_MSG_REMOVE_DEVICE: { - LocalDeviceImpl* ldi; - message->FindPointer("device", (void**)&ldi); - fLocalDevicesList.RemoveItem(ldi); - delete ldi; + LocalDeviceImpl* lDeviceImpl = LocateDelegateFromMessage(message); + if (lDeviceImpl != NULL) + fLocalDevicesList.RemoveItem(lDeviceImpl); + delete lDeviceImpl; break; } @@ -212,20 +212,20 @@ void BluetoothServer::MessageReceived(BMessage *message) LocalDeviceImpl* BluetoothServer::LocateDelegateFromMessage(BMessage* message) { - LocalDeviceImpl* ldi = NULL; + LocalDeviceImpl* lDeviceImpl = NULL; hci_id hid; if (message->FindInt32("hci_id", &hid) == B_OK) { /* Try to find out when a ID was specified */ int index; for (index = 0; index < fLocalDevicesList.CountItems(); index ++) { - ldi = fLocalDevicesList.ItemAt(index); - if (ldi->GetID() == hid) + lDeviceImpl = fLocalDevicesList.ItemAt(index); + if (lDeviceImpl->GetID() == hid) break; } } - return ldi; + return lDeviceImpl; } @@ -236,9 +236,9 @@ BluetoothServer::LocateLocalDeviceImpl(hci_id hid) int index; for (index = 0; index < fLocalDevicesList.CountItems(); index ++) { - LocalDeviceImpl* ldi = fLocalDevicesList.ItemAt(index); - if (ldi->GetID() == hid) - return ldi; + LocalDeviceImpl* lDeviceImpl = fLocalDevicesList.ItemAt(index); + if (lDeviceImpl->GetID() == hid) + return lDeviceImpl; } return NULL; @@ -262,14 +262,14 @@ BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply) hci_id hid; ssize_t size; bdaddr_t bdaddr; - LocalDeviceImpl* ldi = NULL; + LocalDeviceImpl* lDeviceImpl = NULL; static int32 lastIndex = 0; if (message->FindInt32("hci_id", &hid) == B_OK) { Output::Instance()->Post("GetLocalDevice requested with id\n", BLACKBOARD_KIT); - ldi = LocateDelegateFromMessage(message); + lDeviceImpl = LocateDelegateFromMessage(message); } else if (message->FindData("bdaddr", B_ANY_TYPE, (const void**)&bdaddr, &size ) == B_OK) { @@ -279,8 +279,8 @@ BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply) for (lastIndex = 0; lastIndex < fLocalDevicesList.CountItems(); lastIndex ++) { //TODO: Only possible if the property is available //bdaddr_t local; - //ldi = fLocalDevicesList.ItemAt(lastIndex); - //if ((ldi->GetAddress(&local, message) == B_OK) + //lDeviceImpl = fLocalDevicesList.ItemAt(lastIndex); + //if ((lDeviceImpl->GetAddress(&local, message) == B_OK) // && bacmp(&local, &bdaddr)) { // break; //} @@ -291,24 +291,24 @@ BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply) Output::Instance()->Post("GetLocalDevice plain request\n", BLACKBOARD_KIT); // from last assigned till end for ( int index = lastIndex + 1; index < fLocalDevicesList.CountItems(); index ++) { - ldi = fLocalDevicesList.ItemAt(index); - printf("Requesting local device %ld\n", ldi->GetID()); - if (ldi != NULL && ldi->Available()) + lDeviceImpl = fLocalDevicesList.ItemAt(index); + printf("Requesting local device %ld\n", lDeviceImpl->GetID()); + if (lDeviceImpl != NULL && lDeviceImpl->Available()) { - Output::Instance()->Postf(BLACKBOARD_KIT, "Device available: %lx\n", ldi->GetID()); + Output::Instance()->Postf(BLACKBOARD_KIT, "Device available: %lx\n", lDeviceImpl->GetID()); lastIndex = index; break; } } // from starting till last assigned if not yet found - if (ldi == NULL) { + if (lDeviceImpl == NULL) { for ( int index = 0; index <= lastIndex ; index ++) { - ldi = fLocalDevicesList.ItemAt(index); - printf("Requesting local device %ld\n", ldi->GetID()); - if (ldi != NULL && ldi->Available()) + lDeviceImpl = fLocalDevicesList.ItemAt(index); + printf("Requesting local device %ld\n", lDeviceImpl->GetID()); + if (lDeviceImpl != NULL && lDeviceImpl->Available()) { - Output::Instance()->Postf(BLACKBOARD_KIT, "Device available: %lx\n", ldi->GetID()); + Output::Instance()->Postf(BLACKBOARD_KIT, "Device available: %lx\n", lDeviceImpl->GetID()); lastIndex = index; break; } @@ -316,9 +316,9 @@ BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply) } } - if (lastIndex <= fLocalDevicesList.CountItems() && ldi != NULL && ldi->Available()) { - hid = ldi->GetID(); - ldi->Acquire(); + if (lastIndex <= fLocalDevicesList.CountItems() && lDeviceImpl != NULL && lDeviceImpl->Available()) { + hid = lDeviceImpl->GetID(); + lDeviceImpl->Acquire(); Output::Instance()->Postf(BLACKBOARD_KIT, "Device acquired %lx\n", hid); return reply->AddInt32("hci_id", hid); @@ -332,24 +332,26 @@ BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply) status_t BluetoothServer::HandleSimpleRequest(BMessage* message, BMessage* reply) { - LocalDeviceImpl* ldi = LocateDelegateFromMessage(message); + LocalDeviceImpl* lDeviceImpl = LocateDelegateFromMessage(message); const char* propertyRequested; // Find out if there is a property being requested, if (message->FindString("property", &propertyRequested) == B_OK) { // Check if the property has been already retrieved - if (ldi->IsPropertyAvailable(propertyRequested)) { + if (lDeviceImpl->IsPropertyAvailable(propertyRequested)) { // Dump everything - reply->AddMessage("properties",ldi->GetPropertiesMessage()); + reply->AddMessage("properties", lDeviceImpl->GetPropertiesMessage()); return B_OK; } } // we are gonna need issue the command ... - if (ldi->ProcessSimpleRequest(DetachCurrentMessage()) == B_OK) + if (lDeviceImpl->ProcessSimpleRequest(DetachCurrentMessage()) == B_OK) return B_WOULD_BLOCK; - else + else { + lDeviceImpl->Unregister(); return B_ERROR; + } } @@ -361,22 +363,22 @@ BluetoothServer::HandleGetProperty(BMessage* message, BMessage* reply) * and will not care about status fields, therefore we return OK in all cases */ - LocalDeviceImpl* ldi = LocateDelegateFromMessage(message); + LocalDeviceImpl* lDeviceImpl = LocateDelegateFromMessage(message); const char* propertyRequested; // Find out if there is a property being requested, if (message->FindString("property", &propertyRequested) == B_OK) { - Output::Instance()->Postf(BLACKBOARD_LD(ldi->GetID()), "Searching %s property...\n", + Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()), "Searching %s property...\n", propertyRequested); // Check if the property has been already retrieved - if (ldi->IsPropertyAvailable(propertyRequested)) { + if (lDeviceImpl->IsPropertyAvailable(propertyRequested)) { if (strcmp(propertyRequested, "hci_version") == 0 || strcmp(propertyRequested, "lmp_version") == 0 || strcmp(propertyRequested, "sco_mtu") == 0) { - uint8 result = ldi->GetPropertiesMessage()->FindInt8(propertyRequested); + uint8 result = lDeviceImpl->GetPropertiesMessage()->FindInt8(propertyRequested); reply->AddInt32("result", result); } else if (strcmp(propertyRequested, "hci_revision") == 0 @@ -386,11 +388,11 @@ BluetoothServer::HandleGetProperty(BMessage* message, BMessage* reply) || strcmp(propertyRequested, "acl_max_pkt") == 0 || strcmp(propertyRequested, "sco_max_pkt") == 0 ) { - uint16 result = ldi->GetPropertiesMessage()->FindInt16(propertyRequested); + uint16 result = lDeviceImpl->GetPropertiesMessage()->FindInt16(propertyRequested); reply->AddInt32("result", result); } else { - Output::Instance()->Postf(BLACKBOARD_LD(ldi->GetID()), "Property %s could not be satisfied\n", + Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()), "Property %s could not be satisfied\n", propertyRequested); } } diff --git a/src/servers/bluetooth/HCIControllerAccessor.cpp b/src/servers/bluetooth/HCIControllerAccessor.cpp index 3389f880dc..f82c446851 100644 --- a/src/servers/bluetooth/HCIControllerAccessor.cpp +++ b/src/servers/bluetooth/HCIControllerAccessor.cpp @@ -1,28 +1,30 @@ /* * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com - * * All rights reserved. Distributed under the terms of the MIT License. - * */ - #include "HCIControllerAccessor.h" HCIControllerAccessor::HCIControllerAccessor(BPath* path) : HCIDelegate(path) { +} + + +HCIControllerAccessor::~HCIControllerAccessor() +{ } + status_t HCIControllerAccessor::IssueCommand(raw_command rc, size_t size) { - if (GetID() < 0) + if (Id() < 0) return B_ERROR; - - return B_ERROR; + return B_OK; } diff --git a/src/servers/bluetooth/HCIControllerAccessor.h b/src/servers/bluetooth/HCIControllerAccessor.h index 6b38daaf2a..8294c4bceb 100644 --- a/src/servers/bluetooth/HCIControllerAccessor.h +++ b/src/servers/bluetooth/HCIControllerAccessor.h @@ -1,12 +1,7 @@ /* * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com - * * All rights reserved. Distributed under the terms of the MIT License. - * */ - - - #ifndef _HCICONTROLLER_ACCESSOR_H_ #define _HCICONTROLLER_ACCESSOR_H_ @@ -17,8 +12,12 @@ class HCIControllerAccessor : public HCIDelegate { public: HCIControllerAccessor(BPath* path); + ~HCIControllerAccessor(); status_t IssueCommand(raw_command rc, size_t size); status_t Launch(); + private: + int fSocket; + }; #endif diff --git a/src/servers/bluetooth/HCIDelegate.h b/src/servers/bluetooth/HCIDelegate.h index 901c25ed17..be9c34f744 100644 --- a/src/servers/bluetooth/HCIDelegate.h +++ b/src/servers/bluetooth/HCIDelegate.h @@ -13,7 +13,6 @@ #include - typedef void* raw_command; @@ -22,39 +21,20 @@ class HCIDelegate { public: HCIDelegate(BPath* path) { - status_t status; - - fFD = open (path->Path(), O_RDWR); - if (fFD > 0) { - // find out which ID was assigned - status = ioctl(fFD, GET_HCI_ID, &fHID, 0); - printf("%s: hid retrieved %lx status=%ld\n", __FUNCTION__, - fHID, status); - } else { - printf("%s: Device driver could not be opened %ld\n", __FUNCTION__, - fHID); - fHID = B_ERROR; - } - //TODO create such queue - + fIdentifier = -1; } - - hci_id GetID(void) + + hci_id Id(void) const { - return fHID; + return fIdentifier; } virtual ~HCIDelegate() { - if (fFD > 0) - { - close (fFD); - fFD = -1; - fHID = B_ERROR; - } + } virtual status_t IssueCommand(raw_command rc, size_t size)=0; @@ -71,18 +51,16 @@ class HCIDelegate { status_t QueueCommand(raw_command rc, size_t size) { // TODO: this is suposed to queue the command in a queue so all - // are actually send to HW to implement HCI FlowControl requeriments + // are actually send to HW to implement HCI FlowControl requeriments return IssueCommand(rc, size); } protected: - hci_id fHID; - int fFD; + hci_id fIdentifier; private: - }; #endif diff --git a/src/servers/bluetooth/HCITransportAccessor.cpp b/src/servers/bluetooth/HCITransportAccessor.cpp index 169c57974f..5b1d09376d 100644 --- a/src/servers/bluetooth/HCITransportAccessor.cpp +++ b/src/servers/bluetooth/HCITransportAccessor.cpp @@ -14,14 +14,38 @@ HCITransportAccessor::HCITransportAccessor(BPath* path) : HCIDelegate(path) { + status_t status; + fDescriptor = open (path->Path(), O_RDWR); + if (fDescriptor > 0) { + // find out which ID was assigned + status = ioctl(fDescriptor, GET_HCI_ID, &fIdentifier, 0); + printf("%s: hid retrieved %lx status=%ld\n", __FUNCTION__, + fIdentifier, status); + } else { + printf("%s: Device driver could not be opened %ld\n", __FUNCTION__, + fIdentifier); + fIdentifier = B_ERROR; + } } + +HCITransportAccessor::~HCITransportAccessor() +{ + if (fDescriptor > 0) + { + close(fDescriptor); + fDescriptor = -1; + fIdentifier = B_ERROR; + } +} + + status_t HCITransportAccessor::IssueCommand(raw_command rc, size_t size) { - if (GetID() < 0 || fFD < 0) + if (Id() < 0 || fDescriptor < 0) return B_ERROR; printf("### Command going: len = %ld\n", size); @@ -31,7 +55,7 @@ for (uint16 index = 0 ; index < size; index++ ) { printf("### \n"); - return ioctl(fFD, ISSUE_BT_COMMAND, rc, size); + return ioctl(fDescriptor, ISSUE_BT_COMMAND, rc, size); } @@ -39,6 +63,6 @@ status_t HCITransportAccessor::Launch() { uint32 dummy; - return ioctl(fFD, BT_UP, &dummy, sizeof(uint32)); + return ioctl(fDescriptor, BT_UP, &dummy, sizeof(uint32)); } diff --git a/src/servers/bluetooth/HCITransportAccessor.h b/src/servers/bluetooth/HCITransportAccessor.h index 7e076cdf6a..3d8d3b9c49 100644 --- a/src/servers/bluetooth/HCITransportAccessor.h +++ b/src/servers/bluetooth/HCITransportAccessor.h @@ -16,9 +16,11 @@ class HCITransportAccessor : public HCIDelegate { public: HCITransportAccessor(BPath* path); + ~HCITransportAccessor(); status_t IssueCommand(raw_command rc, size_t size); status_t Launch(); - + private: + int fDescriptor; }; #endif diff --git a/src/servers/bluetooth/LocalDeviceHandler.cpp b/src/servers/bluetooth/LocalDeviceHandler.cpp index 492e942329..2ec2923488 100644 --- a/src/servers/bluetooth/LocalDeviceHandler.cpp +++ b/src/servers/bluetooth/LocalDeviceHandler.cpp @@ -16,6 +16,7 @@ LocalDeviceHandler::LocalDeviceHandler(HCIDelegate* hd) LocalDeviceHandler::~LocalDeviceHandler() { + delete fHCIDelegate; delete fProperties; } @@ -23,7 +24,7 @@ LocalDeviceHandler::~LocalDeviceHandler() hci_id LocalDeviceHandler::GetID() { - return fHCIDelegate->GetID(); + return fHCIDelegate->Id(); } @@ -101,8 +102,8 @@ LocalDeviceHandler::ClearWantedEvent(BMessage* msg, uint16 event, uint16 opcode) if (opcode != 0) { // The opcode matches - if ( (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) - && ((uint16)opcodeFound == opcode) ) { + if ((msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) + && ((uint16)opcodeFound == opcode)) { // this should remove only the entry printf("Removed event %#x and opcode %d from request %p\n", @@ -122,7 +123,7 @@ LocalDeviceHandler::ClearWantedEvent(BMessage* msg, uint16 event, uint16 opcode) } eventIndex++; } - printf("%s:Nothing Found/Removed\n",__FUNCTION__); + printf("%s:Nothing Found/Removed\n", __FUNCTION__); finish: fEventsWanted.Unlock(); diff --git a/src/servers/bluetooth/LocalDeviceImpl.cpp b/src/servers/bluetooth/LocalDeviceImpl.cpp index 21694918f2..e0b5318284 100644 --- a/src/servers/bluetooth/LocalDeviceImpl.cpp +++ b/src/servers/bluetooth/LocalDeviceImpl.cpp @@ -70,6 +70,25 @@ LocalDeviceImpl::LocalDeviceImpl(HCIDelegate* hd) : LocalDeviceHandler(hd) } + +LocalDeviceImpl::~LocalDeviceImpl() +{ + +} + +void +LocalDeviceImpl::Unregister() +{ + BMessage* msg = new BMessage(BT_MSG_REMOVE_DEVICE); + + msg->AddInt32("hci_id", fHCIDelegate->Id()); + + Output::Instance()->Postf(BLACKBOARD_DEVICEMANAGER, "Unregistering %x\n", fHCIDelegate->Id()); + + be_app_messenger.SendMessage(msg); +} + + #if 0 #pragma mark - Event handling methods - #endif @@ -751,11 +770,13 @@ LocalDeviceImpl::ProcessSimpleRequest(BMessage* request) AddWantedEvent(request); // LEAK: is command buffer freed within the Message? - if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) + if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) == B_ERROR) { // TODO: - Reply the request with error! // - Remove the just added request - (Output::Instance()->Post("Command issue error\n", BLACKBOARD_KIT)); + (Output::Instance()->Post("## ERROR Command issue, REMOVING!\n", BLACKBOARD_KIT)); + ClearWantedEvent(request); + } else { return B_OK; } diff --git a/src/servers/bluetooth/LocalDeviceImpl.h b/src/servers/bluetooth/LocalDeviceImpl.h index b69a43381d..c884f1bec1 100644 --- a/src/servers/bluetooth/LocalDeviceImpl.h +++ b/src/servers/bluetooth/LocalDeviceImpl.h @@ -25,19 +25,21 @@ private: public: - // Factory methods - static LocalDeviceImpl* CreateControllerAccessor(BPath* path); - static LocalDeviceImpl* CreateTransportAccessor(BPath* path); + // Factory methods + static LocalDeviceImpl* CreateControllerAccessor(BPath* path); + static LocalDeviceImpl* CreateTransportAccessor(BPath* path); + ~LocalDeviceImpl(); + void Unregister(); void HandleEvent(struct hci_event_header* event); - - /* Request handling */ - status_t GetAddress(bdaddr_t* bdaddr, BMessage* request); + + // Request handling + status_t GetAddress(bdaddr_t* bdaddr, BMessage* request); status_t GetFriendlyName(BString str, BMessage* request); status_t ProcessSimpleRequest(BMessage* request); - /* Events handling */ - void CommandComplete(struct hci_ev_cmd_complete* event, BMessage* request, int32 index); + // Events handling + void CommandComplete(struct hci_ev_cmd_complete* event, BMessage* request, int32 index); void CommandStatus(struct hci_ev_cmd_status* event, BMessage* request, int32 index); // Inquiry @@ -47,11 +49,11 @@ public: // Connection void ConnectionComplete(struct hci_ev_conn_complete* event, BMessage* request); - void ConnectionRequest(struct hci_ev_conn_request* event, BMessage* request); - void DisconnectionComplete(struct hci_ev_disconnection_complete_reply* event, BMessage* request); + void ConnectionRequest(struct hci_ev_conn_request* event, BMessage* request); + void DisconnectionComplete(struct hci_ev_disconnection_complete_reply* event, BMessage* request); // Pairing - void PinCodeRequest(struct hci_ev_pin_code_req* event, BMessage* request); + void PinCodeRequest(struct hci_ev_pin_code_req* event, BMessage* request); void RoleChange(struct hci_ev_role_change* event, BMessage* request, int32 index); void LinkKeyNotify(struct hci_ev_link_key_notify* event, BMessage* request, int32 index); void PageScanRepetitionModeChange(struct hci_ev_page_scan_rep_mode_change* event, BMessage* request, int32 index);