diff --git a/src/servers/bluetooth/HCIControllerAccessor.cpp b/src/servers/bluetooth/HCIControllerAccessor.cpp index 0314ef8d39..0ca1451809 100644 --- a/src/servers/bluetooth/HCIControllerAccessor.cpp +++ b/src/servers/bluetooth/HCIControllerAccessor.cpp @@ -1,26 +1,26 @@ -/* - * 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) -{ - - -} - -status_t -HCIControllerAccessor::IssueCommand(raw_command* rc, size_t size) -{ - - if (GetID() < 0) - return B_ERROR; - - - return B_ERROR; -} +/* + * 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) +{ + + +} + +status_t +HCIControllerAccessor::IssueCommand(raw_command* rc, size_t size) +{ + + if (GetID() < 0) + return B_ERROR; + + + return B_ERROR; +} diff --git a/src/servers/bluetooth/HCIControllerAccessor.h b/src/servers/bluetooth/HCIControllerAccessor.h index 87fe9ee2eb..cf2316af16 100644 --- a/src/servers/bluetooth/HCIControllerAccessor.h +++ b/src/servers/bluetooth/HCIControllerAccessor.h @@ -1,23 +1,23 @@ -/* - * 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_ - -#include "HCIDelegate.h" - - -class HCIControllerAccessor : public HCIDelegate { - - public: - HCIControllerAccessor(BPath* path); - status_t IssueCommand(raw_command* rc, size_t size); -}; - -#endif +/* + * 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_ + +#include "HCIDelegate.h" + + +class HCIControllerAccessor : public HCIDelegate { + + public: + HCIControllerAccessor(BPath* path); + status_t IssueCommand(raw_command* rc, size_t size); +}; + +#endif diff --git a/src/servers/bluetooth/HCIDelegate.h b/src/servers/bluetooth/HCIDelegate.h index d02df9e368..b42a6ba0c7 100644 --- a/src/servers/bluetooth/HCIDelegate.h +++ b/src/servers/bluetooth/HCIDelegate.h @@ -1,65 +1,65 @@ -/* - * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com - * - * All rights reserved. Distributed under the terms of the MIT License. - * - */ - -#ifndef _HCIDELEGATE_H_ -#define _HCIDELEGATE_H_ - -#include -#include -#include -#include - -#include - - -typedef void* raw_command; - - -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 %ld status=%ld\n", __FUNCTION__, fHID, status); - } - else { - printf("%s: Device driver could not be opened %ld\n", __FUNCTION__, fHID); - fHID = B_ERROR; - } - - - - } - - hci_id GetID(void) - { - return fHID; - } - - virtual status_t IssueCommand(raw_command rc, size_t size) - { - return B_ERROR; - } - - protected: - - - hci_id fHID; - int fFD; - - private: - - -}; - -#endif +/* + * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com + * + * All rights reserved. Distributed under the terms of the MIT License. + * + */ + +#ifndef _HCIDELEGATE_H_ +#define _HCIDELEGATE_H_ + +#include +#include +#include +#include + +#include + + +typedef void* raw_command; + + +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 %ld status=%ld\n", __FUNCTION__, fHID, status); + } + else { + printf("%s: Device driver could not be opened %ld\n", __FUNCTION__, fHID); + fHID = B_ERROR; + } + + + + } + + hci_id GetID(void) + { + return fHID; + } + + virtual status_t IssueCommand(raw_command rc, size_t size) + { + return B_ERROR; + } + + protected: + + + hci_id fHID; + int fFD; + + private: + + +}; + +#endif diff --git a/src/servers/bluetooth/HCITransportAccessor.h b/src/servers/bluetooth/HCITransportAccessor.h index b3d876413c..85ae7d0123 100644 --- a/src/servers/bluetooth/HCITransportAccessor.h +++ b/src/servers/bluetooth/HCITransportAccessor.h @@ -1,22 +1,22 @@ -/* - * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com - * - * All rights reserved. Distributed under the terms of the MIT License. - * - */ - - -#ifndef _HCITRANSPORT_ACCESSOR_H_ -#define _HCITRANSPORT_ACCESSOR_H_ - -#include "HCIDelegate.h" - - -class HCITransportAccessor : public HCIDelegate { - - public: - HCITransportAccessor(BPath* path); - status_t IssueCommand(raw_command rc, size_t size); -}; - -#endif +/* + * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com + * + * All rights reserved. Distributed under the terms of the MIT License. + * + */ + + +#ifndef _HCITRANSPORT_ACCESSOR_H_ +#define _HCITRANSPORT_ACCESSOR_H_ + +#include "HCIDelegate.h" + + +class HCITransportAccessor : public HCIDelegate { + + public: + HCITransportAccessor(BPath* path); + status_t IssueCommand(raw_command rc, size_t size); +}; + +#endif diff --git a/src/servers/bluetooth/LocalDeviceHandler.cpp b/src/servers/bluetooth/LocalDeviceHandler.cpp index eb835a762b..539745be74 100644 --- a/src/servers/bluetooth/LocalDeviceHandler.cpp +++ b/src/servers/bluetooth/LocalDeviceHandler.cpp @@ -1,170 +1,170 @@ -/* - * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com - * - * All rights reserved. Distributed under the terms of the MIT License. - * - */ - - -#include "LocalDeviceHandler.h" - - - -LocalDeviceHandler::LocalDeviceHandler(HCIDelegate* hd) -{ - fHCIDelegate = hd; - fProperties = new BMessage(); - -} - -LocalDeviceHandler::~LocalDeviceHandler() -{ - -} - - -hci_id -LocalDeviceHandler::GetID() -{ - return fHCIDelegate->GetID(); -} - - -bool -LocalDeviceHandler::Available() { - - return true; -} - - -void -LocalDeviceHandler::Acquire(void) { - -} - - -bool -LocalDeviceHandler::IsPropertyAvailable(const BString& property) { - - type_code typeFound; - int32 countFound; - - return (fProperties->GetInfo(property.String(), &typeFound, &countFound) == B_OK ); - -} - - -void -LocalDeviceHandler::AddWantedEvent(BMessage* msg) -{ - fEventsWanted.Lock(); - // TODO: review why it is neede to replicate the msg - fEventsWanted.AddMessage(msg); - fEventsWanted.Unlock(); -} - -void -LocalDeviceHandler::ClearWantedEvent(BMessage* msg) -{ - fEventsWanted.Lock(); - fEventsWanted.RemoveMessage(msg); - fEventsWanted.Unlock(); - -} - - -void -LocalDeviceHandler::ClearWantedEvent(BMessage* msg, uint16 event, uint16 opcode) -{ - // Remove the whole petition from queue - fEventsWanted.Lock(); - - int16 eventFound; - int16 opcodeFound; - int32 eventIndex = 0; - - // for each Event - while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) { - - printf("@ Event expected found @%ld\n", eventIndex); - - if (eventFound == event) { - - printf("@ Event matches %ld\n", eventIndex); - // there is an opcode specified - if (opcode != 0) { - - // The opcode matches - if ( (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) && - ((uint16)opcodeFound == opcode) ) { - - // this should remove only the entry - printf("@ Removed event %d and opcoce %d from message %p\n", event, opcode, msg); - (void)msg->RemoveData("eventExpected", eventIndex); - (void)msg->RemoveData("opcodeExpected", eventIndex); - goto bail; - } - } else { - // Event matches so far - printf("@ Removed event %d from message %p\n", event, msg); - (void)msg->RemoveData("eventExpected", eventIndex); - goto bail; - } - - } - eventIndex++; - } - -bail: - - fEventsWanted.Unlock(); - -} - - -BMessage* -LocalDeviceHandler::FindPetition(uint16 event, uint16 opcode, int32* indexFound) -{ - int16 eventFound; - int16 opcodeFound; - int32 eventIndex; - - fEventsWanted.Lock(); - // for each Petition - for (int32 index = 0 ; index < fEventsWanted.CountMessages() ; index++) { - BMessage* msg = fEventsWanted.FindMessage(index); - - printf("Petition %ld ... of %ld msg #%p\n", index, fEventsWanted.CountMessages(), msg); - msg->PrintToStream(); - eventIndex = 0; - - // for each Event - while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) { - if (eventFound == event) { - - printf("Event found %ld\n", eventIndex); - // there is an opcode specified.. - if (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) { - - // ensure the opcode - if ((uint16)opcodeFound != opcode) { - printf("opcode does not match %d\n", opcode); - break; - } - printf("Opcdodes match %d %d \n", opcode , opcodeFound); - } - - fEventsWanted.Unlock(); - if (indexFound != NULL) - *indexFound = eventIndex; - return msg; - - - } - eventIndex++; - } - } - - fEventsWanted.Unlock(); - return NULL; -} +/* + * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com + * + * All rights reserved. Distributed under the terms of the MIT License. + * + */ + + +#include "LocalDeviceHandler.h" + + + +LocalDeviceHandler::LocalDeviceHandler(HCIDelegate* hd) +{ + fHCIDelegate = hd; + fProperties = new BMessage(); + +} + +LocalDeviceHandler::~LocalDeviceHandler() +{ + +} + + +hci_id +LocalDeviceHandler::GetID() +{ + return fHCIDelegate->GetID(); +} + + +bool +LocalDeviceHandler::Available() { + + return true; +} + + +void +LocalDeviceHandler::Acquire(void) { + +} + + +bool +LocalDeviceHandler::IsPropertyAvailable(const BString& property) { + + type_code typeFound; + int32 countFound; + + return (fProperties->GetInfo(property.String(), &typeFound, &countFound) == B_OK ); + +} + + +void +LocalDeviceHandler::AddWantedEvent(BMessage* msg) +{ + fEventsWanted.Lock(); + // TODO: review why it is neede to replicate the msg + fEventsWanted.AddMessage(msg); + fEventsWanted.Unlock(); +} + +void +LocalDeviceHandler::ClearWantedEvent(BMessage* msg) +{ + fEventsWanted.Lock(); + fEventsWanted.RemoveMessage(msg); + fEventsWanted.Unlock(); + +} + + +void +LocalDeviceHandler::ClearWantedEvent(BMessage* msg, uint16 event, uint16 opcode) +{ + // Remove the whole petition from queue + fEventsWanted.Lock(); + + int16 eventFound; + int16 opcodeFound; + int32 eventIndex = 0; + + // for each Event + while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) { + + printf("@ Event expected found @%ld\n", eventIndex); + + if (eventFound == event) { + + printf("@ Event matches %ld\n", eventIndex); + // there is an opcode specified + if (opcode != 0) { + + // The opcode matches + if ( (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) && + ((uint16)opcodeFound == opcode) ) { + + // this should remove only the entry + printf("@ Removed event %d and opcoce %d from message %p\n", event, opcode, msg); + (void)msg->RemoveData("eventExpected", eventIndex); + (void)msg->RemoveData("opcodeExpected", eventIndex); + goto bail; + } + } else { + // Event matches so far + printf("@ Removed event %d from message %p\n", event, msg); + (void)msg->RemoveData("eventExpected", eventIndex); + goto bail; + } + + } + eventIndex++; + } + +bail: + + fEventsWanted.Unlock(); + +} + + +BMessage* +LocalDeviceHandler::FindPetition(uint16 event, uint16 opcode, int32* indexFound) +{ + int16 eventFound; + int16 opcodeFound; + int32 eventIndex; + + fEventsWanted.Lock(); + // for each Petition + for (int32 index = 0 ; index < fEventsWanted.CountMessages() ; index++) { + BMessage* msg = fEventsWanted.FindMessage(index); + + printf("Petition %ld ... of %ld msg #%p\n", index, fEventsWanted.CountMessages(), msg); + msg->PrintToStream(); + eventIndex = 0; + + // for each Event + while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) { + if (eventFound == event) { + + printf("Event found %ld\n", eventIndex); + // there is an opcode specified.. + if (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) { + + // ensure the opcode + if ((uint16)opcodeFound != opcode) { + printf("opcode does not match %d\n", opcode); + break; + } + printf("Opcdodes match %d %d \n", opcode , opcodeFound); + } + + fEventsWanted.Unlock(); + if (indexFound != NULL) + *indexFound = eventIndex; + return msg; + + + } + eventIndex++; + } + } + + fEventsWanted.Unlock(); + return NULL; +} diff --git a/src/servers/bluetooth/LocalDeviceImpl.cpp b/src/servers/bluetooth/LocalDeviceImpl.cpp index bf91cfc826..a7cf055249 100644 --- a/src/servers/bluetooth/LocalDeviceImpl.cpp +++ b/src/servers/bluetooth/LocalDeviceImpl.cpp @@ -1,551 +1,551 @@ -/* - * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com - * - * All rights reserved. Distributed under the terms of the MIT License. - * - */ - - -#include "BluetoothServer.h" - -#include "LocalDeviceImpl.h" -#include "CommandManager.h" -#include "Output.h" - -#include -#include - -#include -#include -#include - -#include - - -#if 0 -#pragma mark - Class methods - -#endif - - -// Factory methods -LocalDeviceImpl* -LocalDeviceImpl::CreateControllerAccessor(BPath* path) -{ - HCIDelegate* hd = new HCIControllerAccessor(path); - - if ( hd != NULL) - return new LocalDeviceImpl(hd); - else - return NULL; -} - - -LocalDeviceImpl* -LocalDeviceImpl::CreateTransportAccessor(BPath* path) -{ - HCIDelegate* hd = new HCITransportAccessor(path); - - if ( hd != NULL) - return new LocalDeviceImpl(hd); - else - return NULL; -} - - -LocalDeviceImpl::LocalDeviceImpl(HCIDelegate* hd) : LocalDeviceHandler(hd) -{ - -} - -#if 0 -#pragma mark - Event handling methods - -#endif - -void -LocalDeviceImpl::HandleEvent(struct hci_event_header* event) -{ - -printf("### Event comming: len = %d\n", event->elen); -for (int16 index = 0 ; index < event->elen + 2; index++ ) { - printf("%x:",((uint8*)event)[index]); -} -printf("### \n"); - - // Events here might have not been initated by us - switch (event->ecode) { - case HCI_EVENT_HARDWARE_ERROR: - //HardwareError(event); - return; - case HCI_EVENT_CONN_REQUEST: - - break; - - case HCI_EVENT_CONN_COMPLETE: - ConnectionComplete((struct hci_ev_conn_complete*)(event+1), NULL); // should belong to a request? - - break; - - case HCI_EVENT_PIN_CODE_REQ: - PinCodeRequest((struct hci_ev_pin_code_req*)(event+1), NULL); - break; - - - default: - // lets go on - break; - } - - - - BMessage* request = NULL; - int32 eventIndexLocation; - - // Check if its a requested one - if ( event->ecode == HCI_EVENT_CMD_COMPLETE ) { - - (Output::Instance()->Post("Incoming Command Complete\n", BLACKBOARD_EVENTS)); - request = FindPetition(event->ecode, ((struct hci_ev_cmd_complete*)(event+1))->opcode, &eventIndexLocation ); - - } else if ( event->ecode == HCI_EVENT_CMD_STATUS ) { - - (Output::Instance()->Post("Incoming Command Status\n", BLACKBOARD_EVENTS)); - request = FindPetition(event->ecode, ((struct hci_ev_cmd_status*)(event+1))->opcode, &eventIndexLocation ); - - } else - { - request = FindPetition(event->ecode); - } - - if ( request == NULL) { - (Output::Instance()->Post("Event could not be understood or delivered\n", BLACKBOARD_EVENTS)); - return; - } - - // we are waiting for a reply - switch (event->ecode) { - case HCI_EVENT_INQUIRY_COMPLETE: - InquiryComplete((uint8*)(event+1), request); - break; - - case HCI_EVENT_INQUIRY_RESULT: - InquiryResult((uint8*)(event+1), request); - break; - - case HCI_EVENT_DISCONNECTION_COMPLETE: - - break; - - case HCI_EVENT_AUTH_COMPLETE: - break; - - case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: - RemoteNameRequestComplete((struct hci_remote_name_request_complete_reply*)(event+1), request); - break; - - case HCI_EVENT_ENCRYPT_CHANGE: - break; - - case HCI_EVENT_CHANGE_CONN_LINK_KEY_COMPLETE: - break; - - case HCI_EVENT_MASTER_LINK_KEY_COMPL: - break; - - case HCI_EVENT_RMT_FEATURES: - break; - - case HCI_EVENT_RMT_VERSION: - break; - - case HCI_EVENT_QOS_SETUP_COMPLETE: - break; - - case HCI_EVENT_CMD_COMPLETE: - CommandComplete((struct hci_ev_cmd_complete*)(event+1), request, eventIndexLocation); - break; - - case HCI_EVENT_CMD_STATUS: - CommandStatus((struct hci_ev_cmd_status*)(event+1), request, eventIndexLocation); - break; - - case HCI_EVENT_FLUSH_OCCUR: - break; - - case HCI_EVENT_ROLE_CHANGE: - break; - - case HCI_EVENT_NUM_COMP_PKTS: - break; - - case HCI_EVENT_MODE_CHANGE: - break; - - case HCI_EVENT_RETURN_LINK_KEYS: - break; - - case HCI_EVENT_LINK_KEY_REQ: - break; - - case HCI_EVENT_LINK_KEY_NOTIFY: - break; - - case HCI_EVENT_LOOPBACK_COMMAND: - break; - - case HCI_EVENT_DATA_BUFFER_OVERFLOW: - break; - - case HCI_EVENT_MAX_SLOT_CHANGE: - break; - - case HCI_EVENT_READ_CLOCK_OFFSET_COMPL: - break; - - case HCI_EVENT_CON_PKT_TYPE_CHANGED: - break; - - case HCI_EVENT_QOS_VIOLATION: - break; - - case HCI_EVENT_PAGE_SCAN_REP_MODE_CHANGE: - break; - - case HCI_EVENT_FLOW_SPECIFICATION: - break; - - case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: - break; - - case HCI_EVENT_REMOTE_EXTENDED_FEATURES: - break; - - case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETED: - break; - - case HCI_EVENT_SYNCHRONOUS_CONNECTION_CHANGED: - - break; - } - -} - - -void -LocalDeviceImpl::CommandComplete(struct hci_ev_cmd_complete* event, BMessage* request, int32 index) { - - int16 opcodeExpected; - BMessage reply; - - Output::Instance()->Post(__FUNCTION__, BLACKBOARD_LD_OFFSET + GetID()); - Output::Instance()->Post("\n", BLACKBOARD_LD_OFFSET + GetID()); - - // Handle command complete information - // FIX ME! the expected code might me in another - // index as is relative to the event not the request - request->FindInt16("opcodeExpected", index, &opcodeExpected); - - - if (request->IsSourceWaiting() == false) - Output::Instance()->Post("Nobody waiting for the event\n", BLACKBOARD_KIT); - - switch (opcodeExpected) { - - case PACK_OPCODE(OGF_INFORMATIONAL_PARAM, OCF_READ_BD_ADDR): - { - struct hci_rp_read_bd_addr* readbdaddr = (struct hci_rp_read_bd_addr*)(event+1); - - if (readbdaddr->status == BT_OK) { - - reply.AddData("bdaddr", B_ANY_TYPE, &readbdaddr->bdaddr, sizeof(bdaddr_t)); - reply.AddInt32("status", readbdaddr->status); - - printf("Sending reply ... %ld\n",request->SendReply(&reply)); - reply.PrintToStream(); - - Output::Instance()->Post("Positive reply for getAdress\n", BLACKBOARD_KIT); - - } else { - reply.AddInt8("status", readbdaddr->status); - request->SendReply(&reply); - Output::Instance()->Post("Negative reply for getAdress\n", BLACKBOARD_KIT); - } - - // This request is not genna be used anymore - ClearWantedEvent(request); - } - break; - - case PACK_OPCODE(OGF_CONTROL_BASEBAND, OCF_READ_LOCAL_NAME): - { - struct hci_rp_read_local_name* readLocalName = (struct hci_rp_read_local_name*)(event+1); - - reply.AddInt8("status", readLocalName->status); - - if (readLocalName->status == BT_OK) { - - reply.AddString("friendlyname", (const char*)readLocalName->local_name ); - Output::Instance()->Post("Positive reply for friendly name\n", BLACKBOARD_KIT); - - } else { - - Output::Instance()->Post("Negative reply for friendly name\n", BLACKBOARD_KIT); - - } - - printf("Sending reply ... %ld\n",request->SendReply(&reply)); - reply.PrintToStream(); - - // This request is not genna be used anymore - ClearWantedEvent(request); - } - break; - - case PACK_OPCODE(OGF_CONTROL_BASEBAND, OCF_WRITE_SCAN_ENABLE): - { - uint8* statusReply = (uint8*)(event+1); - - reply.AddInt8("status", *statusReply); - - if (*statusReply == BT_OK) { - - Output::Instance()->Post("Positive reply for scanmode\n", BLACKBOARD_KIT); - - } else { - - Output::Instance()->Post("Negative reply for scanmode\n", BLACKBOARD_KIT); - - } - - printf("Sending reply ... %ld\n",request->SendReply(&reply)); - reply.PrintToStream(); - - // This request is not genna be used anymore - ClearWantedEvent(request); - } - break; - - default: - Output::Instance()->Post("Command Complete not handled\n", BLACKBOARD_KIT); - break; - - } -} - - -void -LocalDeviceImpl::CommandStatus(struct hci_ev_cmd_status* event, BMessage* request, int32 index) { - - int16 opcodeExpected; - BMessage reply; - - Output::Instance()->Post(__FUNCTION__, BLACKBOARD_LD_OFFSET + GetID()); - Output::Instance()->Post("\n", BLACKBOARD_LD_OFFSET + GetID()); - - // Handle command complete information - request->FindInt16("opcodeExpected", index, &opcodeExpected); - - - if (request->IsSourceWaiting() == false) - Output::Instance()->Post("Nobody waiting for the event\n", BLACKBOARD_KIT); - - switch (opcodeExpected) { - - case PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY): - { - reply.what = BT_MSG_INQUIRY_STARTED; - reply.AddInt8("status", event->status); - - if (event->status == BT_OK) { - Output::Instance()->Post("Positive reply for inquiry status\n", BLACKBOARD_KIT); - } else { - Output::Instance()->Post("Negative reply for inquiry status\n", BLACKBOARD_KIT); - } - - printf("Sending reply ... %ld\n", request->SendReply(&reply)); - reply.PrintToStream(); - - ClearWantedEvent(request, HCI_EVENT_CMD_STATUS, PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY)); - } - break; - - case PACK_OPCODE(OGF_LINK_CONTROL, OCF_REMOTE_NAME_REQUEST): - { - ClearWantedEvent(request, HCI_EVENT_CMD_STATUS, PACK_OPCODE(OGF_LINK_CONTROL, OCF_REMOTE_NAME_REQUEST)); - } - break; - default: - Output::Instance()->Post("Command Status not handled\n", BLACKBOARD_KIT); - break; - } - -} - - -void -LocalDeviceImpl::InquiryResult(uint8* numberOfResponses, BMessage* request) -{ - - BMessage reply(BT_MSG_INQUIRY_DEVICE); - - - reply.AddData("info", B_ANY_TYPE, numberOfResponses+1 // skiping here the number of responses - , (*numberOfResponses) * sizeof(struct inquiry_info) ); - - printf("%s: Sending reply ... %ld\n",__FUNCTION__, request->SendReply(&reply)); - -} - - -void -LocalDeviceImpl::InquiryComplete(uint8* status, BMessage* request) -{ - BMessage reply(BT_MSG_INQUIRY_COMPLETED); - - reply.AddInt8("status", *status); - - - printf("%s: Sending reply ... %ld\n",__FUNCTION__, request->SendReply(&reply)); -// (request->ReturnAddress()).SendMessage(&reply); - - ClearWantedEvent(request); -} - - -void -LocalDeviceImpl::RemoteNameRequestComplete(struct hci_remote_name_request_complete_reply* remotename, BMessage* request) -{ - BMessage reply; - - reply.AddInt8("status", remotename->status); - - if (remotename->status == BT_OK) { - - reply.AddString("friendlyname", (const char*)remotename->remote_name ); - Output::Instance()->Post("Positive reply for remote friendly name\n", BLACKBOARD_KIT); - - } else { - - Output::Instance()->Post("Negative reply for remote friendly name\n", BLACKBOARD_KIT); - - } - - printf("Sending reply ... %ld\n", request->SendReply(&reply)); - reply.PrintToStream(); - - // This request is not genna be used anymore - // Although there are many middle events that should be tracked todo: ticket 2377 - ClearWantedEvent(request); - -} - - -void -LocalDeviceImpl::ConnectionComplete(struct hci_ev_conn_complete* event, BMessage* request) -{ - ConnectionIncoming* iConnection = new ConnectionIncoming(NULL); - iConnection->Show(); - -} - - -void -LocalDeviceImpl::PinCodeRequest(struct hci_ev_pin_code_req* event, BMessage* request) -{ - PincodeWindow* iPincode = new PincodeWindow(NULL); - iPincode->Show(); - -} - -#if 0 -#pragma mark - Request Methods - -#endif - - -status_t -LocalDeviceImpl::GetAddress(bdaddr_t* bdaddr, BMessage* request) -{ - ssize_t size; - - if (fProperties->FindData("bdaddr", B_ANY_TYPE, 0, (const void **)bdaddr, &size) == B_OK) { - - (Output::Instance()->Post("BDADDR already present in server\n", BLACKBOARD_EVENTS)); - /* We have this info, returning back */ - return B_OK; - - } else { - size_t size; - - void* command = buildReadBdAddr(&size); - - /* Adding a wanted event in the queue */ - request->AddInt16("eventExpected", HCI_EVENT_CMD_COMPLETE); - request->AddInt16("opcodeExpected", PACK_OPCODE(OGF_INFORMATIONAL_PARAM, OCF_READ_BD_ADDR)); - - printf("Adding request... %p\n", request); - AddWantedEvent(request); - request->PrintToStream(); - - if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) == B_ERROR) - (Output::Instance()->Post("Command issue error\n", BLACKBOARD_EVENTS)); - - (Output::Instance()->Post("Command issued for GetAddress\n", BLACKBOARD_EVENTS)); - return B_WOULD_BLOCK; - } - -} - - -status_t -LocalDeviceImpl::GetFriendlyName(BString str, BMessage* request) -{ - - if (fProperties->FindString("friendlyname", &str) == B_OK) { - - (Output::Instance()->Post("Friendly name already present in server\n", BLACKBOARD_EVENTS)); - /* We have this info, returning back */ - return B_OK; - - } else { - size_t size; - - void* command = buildReadLocalName(&size); - - /* Adding a wanted event in the queue */ - request->AddInt16("eventExpected", HCI_EVENT_CMD_COMPLETE); - request->AddInt16("opcodeExpected", PACK_OPCODE(OGF_CONTROL_BASEBAND, OCF_READ_LOCAL_NAME)); - - printf("Adding request... %p\n", request); - AddWantedEvent(request); - request->PrintToStream(); - - if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) == B_ERROR) - (Output::Instance()->Post("Command issue error\n", BLACKBOARD_EVENTS)); - - (Output::Instance()->Post("Command issued for GetFriendlyname\n", BLACKBOARD_EVENTS)); - - return B_WOULD_BLOCK; - } - -} - - -status_t -LocalDeviceImpl::ProcessSimpleRequest(BMessage* request) -{ - ssize_t size; - void* command = NULL; - - if (request->FindData("raw command", B_ANY_TYPE, 0, (const void **)&command, &size) == B_OK) - if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) == B_ERROR) - (Output::Instance()->Post("Command issue error\n", BLACKBOARD_EVENTS)); - else - { - AddWantedEvent(request); - return B_OK; - } - else { - (Output::Instance()->Post("No command specified for simple request\n", BLACKBOARD_KIT)); - } - - return B_ERROR; -} +/* + * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com + * + * All rights reserved. Distributed under the terms of the MIT License. + * + */ + + +#include "BluetoothServer.h" + +#include "LocalDeviceImpl.h" +#include "CommandManager.h" +#include "Output.h" + +#include +#include + +#include +#include +#include + +#include + + +#if 0 +#pragma mark - Class methods - +#endif + + +// Factory methods +LocalDeviceImpl* +LocalDeviceImpl::CreateControllerAccessor(BPath* path) +{ + HCIDelegate* hd = new HCIControllerAccessor(path); + + if ( hd != NULL) + return new LocalDeviceImpl(hd); + else + return NULL; +} + + +LocalDeviceImpl* +LocalDeviceImpl::CreateTransportAccessor(BPath* path) +{ + HCIDelegate* hd = new HCITransportAccessor(path); + + if ( hd != NULL) + return new LocalDeviceImpl(hd); + else + return NULL; +} + + +LocalDeviceImpl::LocalDeviceImpl(HCIDelegate* hd) : LocalDeviceHandler(hd) +{ + +} + +#if 0 +#pragma mark - Event handling methods - +#endif + +void +LocalDeviceImpl::HandleEvent(struct hci_event_header* event) +{ + +printf("### Event comming: len = %d\n", event->elen); +for (int16 index = 0 ; index < event->elen + 2; index++ ) { + printf("%x:",((uint8*)event)[index]); +} +printf("### \n"); + + // Events here might have not been initated by us + switch (event->ecode) { + case HCI_EVENT_HARDWARE_ERROR: + //HardwareError(event); + return; + case HCI_EVENT_CONN_REQUEST: + + break; + + case HCI_EVENT_CONN_COMPLETE: + ConnectionComplete((struct hci_ev_conn_complete*)(event+1), NULL); // should belong to a request? + + break; + + case HCI_EVENT_PIN_CODE_REQ: + PinCodeRequest((struct hci_ev_pin_code_req*)(event+1), NULL); + break; + + + default: + // lets go on + break; + } + + + + BMessage* request = NULL; + int32 eventIndexLocation; + + // Check if its a requested one + if ( event->ecode == HCI_EVENT_CMD_COMPLETE ) { + + (Output::Instance()->Post("Incoming Command Complete\n", BLACKBOARD_EVENTS)); + request = FindPetition(event->ecode, ((struct hci_ev_cmd_complete*)(event+1))->opcode, &eventIndexLocation ); + + } else if ( event->ecode == HCI_EVENT_CMD_STATUS ) { + + (Output::Instance()->Post("Incoming Command Status\n", BLACKBOARD_EVENTS)); + request = FindPetition(event->ecode, ((struct hci_ev_cmd_status*)(event+1))->opcode, &eventIndexLocation ); + + } else + { + request = FindPetition(event->ecode); + } + + if ( request == NULL) { + (Output::Instance()->Post("Event could not be understood or delivered\n", BLACKBOARD_EVENTS)); + return; + } + + // we are waiting for a reply + switch (event->ecode) { + case HCI_EVENT_INQUIRY_COMPLETE: + InquiryComplete((uint8*)(event+1), request); + break; + + case HCI_EVENT_INQUIRY_RESULT: + InquiryResult((uint8*)(event+1), request); + break; + + case HCI_EVENT_DISCONNECTION_COMPLETE: + + break; + + case HCI_EVENT_AUTH_COMPLETE: + break; + + case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: + RemoteNameRequestComplete((struct hci_remote_name_request_complete_reply*)(event+1), request); + break; + + case HCI_EVENT_ENCRYPT_CHANGE: + break; + + case HCI_EVENT_CHANGE_CONN_LINK_KEY_COMPLETE: + break; + + case HCI_EVENT_MASTER_LINK_KEY_COMPL: + break; + + case HCI_EVENT_RMT_FEATURES: + break; + + case HCI_EVENT_RMT_VERSION: + break; + + case HCI_EVENT_QOS_SETUP_COMPLETE: + break; + + case HCI_EVENT_CMD_COMPLETE: + CommandComplete((struct hci_ev_cmd_complete*)(event+1), request, eventIndexLocation); + break; + + case HCI_EVENT_CMD_STATUS: + CommandStatus((struct hci_ev_cmd_status*)(event+1), request, eventIndexLocation); + break; + + case HCI_EVENT_FLUSH_OCCUR: + break; + + case HCI_EVENT_ROLE_CHANGE: + break; + + case HCI_EVENT_NUM_COMP_PKTS: + break; + + case HCI_EVENT_MODE_CHANGE: + break; + + case HCI_EVENT_RETURN_LINK_KEYS: + break; + + case HCI_EVENT_LINK_KEY_REQ: + break; + + case HCI_EVENT_LINK_KEY_NOTIFY: + break; + + case HCI_EVENT_LOOPBACK_COMMAND: + break; + + case HCI_EVENT_DATA_BUFFER_OVERFLOW: + break; + + case HCI_EVENT_MAX_SLOT_CHANGE: + break; + + case HCI_EVENT_READ_CLOCK_OFFSET_COMPL: + break; + + case HCI_EVENT_CON_PKT_TYPE_CHANGED: + break; + + case HCI_EVENT_QOS_VIOLATION: + break; + + case HCI_EVENT_PAGE_SCAN_REP_MODE_CHANGE: + break; + + case HCI_EVENT_FLOW_SPECIFICATION: + break; + + case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: + break; + + case HCI_EVENT_REMOTE_EXTENDED_FEATURES: + break; + + case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETED: + break; + + case HCI_EVENT_SYNCHRONOUS_CONNECTION_CHANGED: + + break; + } + +} + + +void +LocalDeviceImpl::CommandComplete(struct hci_ev_cmd_complete* event, BMessage* request, int32 index) { + + int16 opcodeExpected; + BMessage reply; + + Output::Instance()->Post(__FUNCTION__, BLACKBOARD_LD_OFFSET + GetID()); + Output::Instance()->Post("\n", BLACKBOARD_LD_OFFSET + GetID()); + + // Handle command complete information + // FIX ME! the expected code might me in another + // index as is relative to the event not the request + request->FindInt16("opcodeExpected", index, &opcodeExpected); + + + if (request->IsSourceWaiting() == false) + Output::Instance()->Post("Nobody waiting for the event\n", BLACKBOARD_KIT); + + switch (opcodeExpected) { + + case PACK_OPCODE(OGF_INFORMATIONAL_PARAM, OCF_READ_BD_ADDR): + { + struct hci_rp_read_bd_addr* readbdaddr = (struct hci_rp_read_bd_addr*)(event+1); + + if (readbdaddr->status == BT_OK) { + + reply.AddData("bdaddr", B_ANY_TYPE, &readbdaddr->bdaddr, sizeof(bdaddr_t)); + reply.AddInt32("status", readbdaddr->status); + + printf("Sending reply ... %ld\n",request->SendReply(&reply)); + reply.PrintToStream(); + + Output::Instance()->Post("Positive reply for getAdress\n", BLACKBOARD_KIT); + + } else { + reply.AddInt8("status", readbdaddr->status); + request->SendReply(&reply); + Output::Instance()->Post("Negative reply for getAdress\n", BLACKBOARD_KIT); + } + + // This request is not genna be used anymore + ClearWantedEvent(request); + } + break; + + case PACK_OPCODE(OGF_CONTROL_BASEBAND, OCF_READ_LOCAL_NAME): + { + struct hci_rp_read_local_name* readLocalName = (struct hci_rp_read_local_name*)(event+1); + + reply.AddInt8("status", readLocalName->status); + + if (readLocalName->status == BT_OK) { + + reply.AddString("friendlyname", (const char*)readLocalName->local_name ); + Output::Instance()->Post("Positive reply for friendly name\n", BLACKBOARD_KIT); + + } else { + + Output::Instance()->Post("Negative reply for friendly name\n", BLACKBOARD_KIT); + + } + + printf("Sending reply ... %ld\n",request->SendReply(&reply)); + reply.PrintToStream(); + + // This request is not genna be used anymore + ClearWantedEvent(request); + } + break; + + case PACK_OPCODE(OGF_CONTROL_BASEBAND, OCF_WRITE_SCAN_ENABLE): + { + uint8* statusReply = (uint8*)(event+1); + + reply.AddInt8("status", *statusReply); + + if (*statusReply == BT_OK) { + + Output::Instance()->Post("Positive reply for scanmode\n", BLACKBOARD_KIT); + + } else { + + Output::Instance()->Post("Negative reply for scanmode\n", BLACKBOARD_KIT); + + } + + printf("Sending reply ... %ld\n",request->SendReply(&reply)); + reply.PrintToStream(); + + // This request is not genna be used anymore + ClearWantedEvent(request); + } + break; + + default: + Output::Instance()->Post("Command Complete not handled\n", BLACKBOARD_KIT); + break; + + } +} + + +void +LocalDeviceImpl::CommandStatus(struct hci_ev_cmd_status* event, BMessage* request, int32 index) { + + int16 opcodeExpected; + BMessage reply; + + Output::Instance()->Post(__FUNCTION__, BLACKBOARD_LD_OFFSET + GetID()); + Output::Instance()->Post("\n", BLACKBOARD_LD_OFFSET + GetID()); + + // Handle command complete information + request->FindInt16("opcodeExpected", index, &opcodeExpected); + + + if (request->IsSourceWaiting() == false) + Output::Instance()->Post("Nobody waiting for the event\n", BLACKBOARD_KIT); + + switch (opcodeExpected) { + + case PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY): + { + reply.what = BT_MSG_INQUIRY_STARTED; + reply.AddInt8("status", event->status); + + if (event->status == BT_OK) { + Output::Instance()->Post("Positive reply for inquiry status\n", BLACKBOARD_KIT); + } else { + Output::Instance()->Post("Negative reply for inquiry status\n", BLACKBOARD_KIT); + } + + printf("Sending reply ... %ld\n", request->SendReply(&reply)); + reply.PrintToStream(); + + ClearWantedEvent(request, HCI_EVENT_CMD_STATUS, PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY)); + } + break; + + case PACK_OPCODE(OGF_LINK_CONTROL, OCF_REMOTE_NAME_REQUEST): + { + ClearWantedEvent(request, HCI_EVENT_CMD_STATUS, PACK_OPCODE(OGF_LINK_CONTROL, OCF_REMOTE_NAME_REQUEST)); + } + break; + default: + Output::Instance()->Post("Command Status not handled\n", BLACKBOARD_KIT); + break; + } + +} + + +void +LocalDeviceImpl::InquiryResult(uint8* numberOfResponses, BMessage* request) +{ + + BMessage reply(BT_MSG_INQUIRY_DEVICE); + + + reply.AddData("info", B_ANY_TYPE, numberOfResponses+1 // skiping here the number of responses + , (*numberOfResponses) * sizeof(struct inquiry_info) ); + + printf("%s: Sending reply ... %ld\n",__FUNCTION__, request->SendReply(&reply)); + +} + + +void +LocalDeviceImpl::InquiryComplete(uint8* status, BMessage* request) +{ + BMessage reply(BT_MSG_INQUIRY_COMPLETED); + + reply.AddInt8("status", *status); + + + printf("%s: Sending reply ... %ld\n",__FUNCTION__, request->SendReply(&reply)); +// (request->ReturnAddress()).SendMessage(&reply); + + ClearWantedEvent(request); +} + + +void +LocalDeviceImpl::RemoteNameRequestComplete(struct hci_remote_name_request_complete_reply* remotename, BMessage* request) +{ + BMessage reply; + + reply.AddInt8("status", remotename->status); + + if (remotename->status == BT_OK) { + + reply.AddString("friendlyname", (const char*)remotename->remote_name ); + Output::Instance()->Post("Positive reply for remote friendly name\n", BLACKBOARD_KIT); + + } else { + + Output::Instance()->Post("Negative reply for remote friendly name\n", BLACKBOARD_KIT); + + } + + printf("Sending reply ... %ld\n", request->SendReply(&reply)); + reply.PrintToStream(); + + // This request is not genna be used anymore + // Although there are many middle events that should be tracked todo: ticket 2377 + ClearWantedEvent(request); + +} + + +void +LocalDeviceImpl::ConnectionComplete(struct hci_ev_conn_complete* event, BMessage* request) +{ + ConnectionIncoming* iConnection = new ConnectionIncoming(NULL); + iConnection->Show(); + +} + + +void +LocalDeviceImpl::PinCodeRequest(struct hci_ev_pin_code_req* event, BMessage* request) +{ + PincodeWindow* iPincode = new PincodeWindow(NULL); + iPincode->Show(); + +} + +#if 0 +#pragma mark - Request Methods - +#endif + + +status_t +LocalDeviceImpl::GetAddress(bdaddr_t* bdaddr, BMessage* request) +{ + ssize_t size; + + if (fProperties->FindData("bdaddr", B_ANY_TYPE, 0, (const void **)bdaddr, &size) == B_OK) { + + (Output::Instance()->Post("BDADDR already present in server\n", BLACKBOARD_EVENTS)); + /* We have this info, returning back */ + return B_OK; + + } else { + size_t size; + + void* command = buildReadBdAddr(&size); + + /* Adding a wanted event in the queue */ + request->AddInt16("eventExpected", HCI_EVENT_CMD_COMPLETE); + request->AddInt16("opcodeExpected", PACK_OPCODE(OGF_INFORMATIONAL_PARAM, OCF_READ_BD_ADDR)); + + printf("Adding request... %p\n", request); + AddWantedEvent(request); + request->PrintToStream(); + + if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) == B_ERROR) + (Output::Instance()->Post("Command issue error\n", BLACKBOARD_EVENTS)); + + (Output::Instance()->Post("Command issued for GetAddress\n", BLACKBOARD_EVENTS)); + return B_WOULD_BLOCK; + } + +} + + +status_t +LocalDeviceImpl::GetFriendlyName(BString str, BMessage* request) +{ + + if (fProperties->FindString("friendlyname", &str) == B_OK) { + + (Output::Instance()->Post("Friendly name already present in server\n", BLACKBOARD_EVENTS)); + /* We have this info, returning back */ + return B_OK; + + } else { + size_t size; + + void* command = buildReadLocalName(&size); + + /* Adding a wanted event in the queue */ + request->AddInt16("eventExpected", HCI_EVENT_CMD_COMPLETE); + request->AddInt16("opcodeExpected", PACK_OPCODE(OGF_CONTROL_BASEBAND, OCF_READ_LOCAL_NAME)); + + printf("Adding request... %p\n", request); + AddWantedEvent(request); + request->PrintToStream(); + + if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) == B_ERROR) + (Output::Instance()->Post("Command issue error\n", BLACKBOARD_EVENTS)); + + (Output::Instance()->Post("Command issued for GetFriendlyname\n", BLACKBOARD_EVENTS)); + + return B_WOULD_BLOCK; + } + +} + + +status_t +LocalDeviceImpl::ProcessSimpleRequest(BMessage* request) +{ + ssize_t size; + void* command = NULL; + + if (request->FindData("raw command", B_ANY_TYPE, 0, (const void **)&command, &size) == B_OK) + if (((HCITransportAccessor*)fHCIDelegate)->IssueCommand(command, size) == B_ERROR) + (Output::Instance()->Post("Command issue error\n", BLACKBOARD_EVENTS)); + else + { + AddWantedEvent(request); + return B_OK; + } + else { + (Output::Instance()->Post("No command specified for simple request\n", BLACKBOARD_KIT)); + } + + return B_ERROR; +}