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