1 /* 2 * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #include <bluetooth/bluetooth_error.h> 7 #include <bluetooth/DiscoveryAgent.h> 8 #include <bluetooth/DiscoveryListener.h> 9 #include <bluetooth/LocalDevice.h> 10 #include <bluetooth/RemoteDevice.h> 11 12 #include <bluetooth/HCI/btHCI_command.h> 13 #include <bluetooth/HCI/btHCI_event.h> 14 15 #include <bluetoothserver_p.h> 16 #include <CommandManager.h> 17 18 #include "KitSupport.h" 19 20 namespace Bluetooth { 21 22 23 RemoteDevicesList 24 DiscoveryAgent::RetrieveDevices(int option) 25 { 26 // No inquiry process initiated 27 if (fLastUsedListener == NULL) 28 return RemoteDevicesList(); 29 30 return fLastUsedListener->GetRemoteDevicesList(); 31 } 32 33 34 status_t 35 DiscoveryAgent::StartInquiry(int accessCode, DiscoveryListener* listener) 36 { 37 return StartInquiry(accessCode, listener, GetInquiryTime()); 38 } 39 40 41 status_t 42 DiscoveryAgent::StartInquiry(uint32 accessCode, DiscoveryListener* listener, bigtime_t secs) 43 { 44 size_t size; 45 46 if (fMessenger == NULL) 47 return B_ERROR; 48 49 if (secs < 1 || secs > 61 ) 50 return B_TIMED_OUT; 51 52 void* startInquiryCommand = NULL; 53 54 // keep the listener whats the current listener for our inquiry state 55 fLastUsedListener = listener; 56 57 // Inform the listener who is gonna be its owner LocalDevice 58 // and its discovered devices 59 listener->SetLocalDeviceOwner(fLocalDevice); 60 61 /* Issue inquiry command */ 62 BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST); 63 BMessage reply; 64 65 request.AddInt32("hci_id", fLocalDevice->ID()); 66 67 startInquiryCommand = buildInquiry(accessCode, secs, BT_MAX_RESPONSES, &size); 68 69 // For stating the inquiry 70 request.AddData("raw command", B_ANY_TYPE, startInquiryCommand, size); 71 request.AddInt16("eventExpected", HCI_EVENT_CMD_STATUS); 72 request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY)); 73 74 // For getting each discovered message 75 request.AddInt16("eventExpected", HCI_EVENT_INQUIRY_RESULT); 76 77 // For finishing each discovered message 78 request.AddInt16("eventExpected", HCI_EVENT_INQUIRY_COMPLETE); 79 80 81 if (fMessenger->SendMessage(&request, listener) == B_OK) 82 { 83 return B_OK; 84 } 85 86 return B_ERROR; 87 88 } 89 90 91 status_t 92 DiscoveryAgent::CancelInquiry(DiscoveryListener* listener) 93 { 94 size_t size; 95 96 if (fMessenger == NULL) 97 return B_ERROR; 98 99 void* cancelInquiryCommand = NULL; 100 int8 bt_status = BT_ERROR; 101 102 /* Issue inquiry command */ 103 BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST); 104 BMessage reply; 105 106 request.AddInt32("hci_id", fLocalDevice->ID()); 107 108 cancelInquiryCommand = buildInquiryCancel(&size); 109 request.AddData("raw command", B_ANY_TYPE, cancelInquiryCommand, size); 110 request.AddInt16("eventExpected", HCI_EVENT_CMD_STATUS); 111 request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY_CANCEL)); 112 113 if (fMessenger->SendMessage(&request, &reply) == B_OK) { 114 if (reply.FindInt8("status", &bt_status ) == B_OK ) { 115 return bt_status; 116 } 117 } 118 119 return B_ERROR; 120 } 121 122 123 void 124 DiscoveryAgent::SetLocalDeviceOwner(LocalDevice* ld) 125 { 126 fLocalDevice = ld; 127 } 128 129 130 DiscoveryAgent::DiscoveryAgent(LocalDevice* ld) 131 { 132 fLocalDevice = ld; 133 fMessenger = _RetrieveBluetoothMessenger(); 134 } 135 136 137 DiscoveryAgent::~DiscoveryAgent() 138 { 139 delete fMessenger; 140 } 141 142 143 } 144