xref: /haiku/src/kits/bluetooth/DiscoveryAgent.cpp (revision aa3083e086e5a929c061c72983e09d916c548a38)
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 #include <bluetooth/debug.h>
12 
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 #include "KitSupport.h"
20 
21 namespace Bluetooth {
22 
23 
24 RemoteDevicesList
25 DiscoveryAgent::RetrieveDevices(int option)
26 {
27 	CALLED();
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 	CALLED();
40     return StartInquiry(accessCode, listener, GetInquiryTime());
41 }
42 
43 
44 status_t
45 DiscoveryAgent::StartInquiry(uint32 accessCode, DiscoveryListener* listener, bigtime_t secs)
46 {
47 	CALLED();
48     size_t size;
49 
50 	if (fMessenger == NULL)
51 		return B_ERROR;
52 
53 	if (secs < 1 || secs > 61 )
54 		return B_TIMED_OUT;
55 
56     void*  startInquiryCommand = NULL;
57 
58     // keep the listener whats the current listener for our inquiry state
59     fLastUsedListener = listener;
60 
61     // Inform the listener who is gonna be its owner LocalDevice
62     // and its discovered devices
63     listener->SetLocalDeviceOwner(fLocalDevice);
64 
65     /* Issue inquiry command */
66     BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
67     BMessage reply;
68 
69     request.AddInt32("hci_id", fLocalDevice->ID());
70 
71     startInquiryCommand = buildInquiry(accessCode, secs, BT_MAX_RESPONSES, &size);
72 
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));
77 
78 	// For getting each discovered message
79     request.AddInt16("eventExpected",  HCI_EVENT_INQUIRY_RESULT);
80 
81 	// For finishing each discovered message
82     request.AddInt16("eventExpected",  HCI_EVENT_INQUIRY_COMPLETE);
83 
84 
85     if (fMessenger->SendMessage(&request, listener) == B_OK)
86     {
87     	return B_OK;
88     }
89 
90 	return B_ERROR;
91 
92 }
93 
94 
95 status_t
96 DiscoveryAgent::CancelInquiry(DiscoveryListener* listener)
97 {
98 	CALLED();
99     size_t size;
100 
101     if (fMessenger == NULL)
102     	return B_ERROR;
103 
104     void* cancelInquiryCommand = NULL;
105     int8  bt_status = BT_ERROR;
106 
107     /* Issue inquiry command */
108     BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
109     BMessage reply;
110 
111     request.AddInt32("hci_id", fLocalDevice->ID());
112 
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));
117 
118     if (fMessenger->SendMessage(&request, &reply) == B_OK) {
119         if (reply.FindInt8("status", &bt_status ) == B_OK ) {
120 			return bt_status;
121 		}
122     }
123 
124     return B_ERROR;
125 }
126 
127 
128 void
129 DiscoveryAgent::SetLocalDeviceOwner(LocalDevice* ld)
130 {
131 	CALLED();
132     fLocalDevice = ld;
133 }
134 
135 
136 DiscoveryAgent::DiscoveryAgent(LocalDevice* ld)
137 {
138 	CALLED();
139 	fLocalDevice = ld;
140 	fMessenger = _RetrieveBluetoothMessenger();
141 }
142 
143 
144 DiscoveryAgent::~DiscoveryAgent()
145 {
146 	CALLED();
147 	delete fMessenger;
148 }
149 
150 
151 }
152