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.
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 #include <bluetooth/debug.h>
13 #include <bluetooth/HCI/btHCI_command.h>
14 #include <bluetooth/HCI/btHCI_event.h>
16 #include <bluetoothserver_p.h>
17 #include <CommandManager.h>
19 #include "KitSupport.h"
25 DiscoveryAgent::RetrieveDevices(int option
)
28 // No inquiry process initiated
29 if (fLastUsedListener
== NULL
)
30 return RemoteDevicesList();
32 return fLastUsedListener
->GetRemoteDevicesList();
37 DiscoveryAgent::StartInquiry(int accessCode
, DiscoveryListener
* listener
)
40 return StartInquiry(accessCode
, listener
, GetInquiryTime());
45 DiscoveryAgent::StartInquiry(uint32 accessCode
, DiscoveryListener
* listener
, bigtime_t secs
)
50 if (fMessenger
== NULL
)
53 if (secs
< 1 || secs
> 61 )
56 void* startInquiryCommand
= NULL
;
58 // keep the listener whats the current listener for our inquiry state
59 fLastUsedListener
= listener
;
61 // Inform the listener who is gonna be its owner LocalDevice
62 // and its discovered devices
63 listener
->SetLocalDeviceOwner(fLocalDevice
);
65 /* Issue inquiry command */
66 BMessage
request(BT_MSG_HANDLE_SIMPLE_REQUEST
);
69 request
.AddInt32("hci_id", fLocalDevice
->ID());
71 startInquiryCommand
= buildInquiry(accessCode
, secs
, BT_MAX_RESPONSES
, &size
);
73 // For stating the inquiry
74 request
.AddData("raw command", B_ANY_TYPE
, startInquiryCommand
, size
);
75 request
.AddInt16("eventExpected", HCI_EVENT_CMD_STATUS
);
76 request
.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL
, OCF_INQUIRY
));
78 // For getting each discovered message
79 request
.AddInt16("eventExpected", HCI_EVENT_INQUIRY_RESULT
);
81 // For finishing each discovered message
82 request
.AddInt16("eventExpected", HCI_EVENT_INQUIRY_COMPLETE
);
85 if (fMessenger
->SendMessage(&request
, listener
) == B_OK
)
96 DiscoveryAgent::CancelInquiry(DiscoveryListener
* listener
)
101 if (fMessenger
== NULL
)
104 void* cancelInquiryCommand
= NULL
;
105 int8 bt_status
= BT_ERROR
;
107 /* Issue inquiry command */
108 BMessage
request(BT_MSG_HANDLE_SIMPLE_REQUEST
);
111 request
.AddInt32("hci_id", fLocalDevice
->ID());
113 cancelInquiryCommand
= buildInquiryCancel(&size
);
114 request
.AddData("raw command", B_ANY_TYPE
, cancelInquiryCommand
, size
);
115 request
.AddInt16("eventExpected", HCI_EVENT_CMD_STATUS
);
116 request
.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL
, OCF_INQUIRY_CANCEL
));
118 if (fMessenger
->SendMessage(&request
, &reply
) == B_OK
) {
119 if (reply
.FindInt8("status", &bt_status
) == B_OK
) {
129 DiscoveryAgent::SetLocalDeviceOwner(LocalDevice
* ld
)
136 DiscoveryAgent::DiscoveryAgent(LocalDevice
* ld
)
140 fMessenger
= _RetrieveBluetoothMessenger();
144 DiscoveryAgent::~DiscoveryAgent()