xref: /haiku/src/kits/bluetooth/LocalDevice.cpp (revision b30304acc8c37e678a1bf66976d15bdab103f931)
1 /*
2  * Copyright 2007 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 
9 #include <bluetooth/bluetooth_error.h>
10 
11 #include <bluetooth/HCI/btHCI_command.h>
12 #include <bluetooth/HCI/btHCI_event.h>
13 
14 #include <bluetooth/LocalDevice.h>
15 #include <bluetooth/RemoteDevice.h>
16 #include <bluetooth/DeviceClass.h>
17 #include <bluetooth/DiscoveryAgent.h>
18 
19 #include <bluetooth/bdaddrUtils.h>
20 #include <bluetoothserver_p.h>
21 #include <CommandManager.h>
22 
23 #include "KitSupport.h"
24 
25 /* TODO: remove me */
26 #include <stdio.h>
27 
28 namespace Bluetooth {
29 
30 BMessenger*  LocalDevice::sfMessenger = NULL;
31 
32 
33 LocalDevice*
34 LocalDevice::RequestLocalDeviceID(BMessage* request)
35 {
36     BMessage reply;
37     hci_id hid;
38 
39     if (sfMessenger->SendMessage(request, &reply) == B_OK &&
40         reply.FindInt32("hci_id", &hid) == B_OK ){
41 
42 	    if (hid >= 0) {
43 		    return new LocalDevice(hid);
44 		}
45     }
46 
47     return NULL;
48 }
49 
50 
51 #if 0
52 #pragma -
53 #endif
54 
55 
56 LocalDevice*
57 LocalDevice::GetLocalDevice()
58 {
59     if ((sfMessenger = _RetrieveBluetoothMessenger()) == NULL)
60         return NULL;
61 
62     BMessage request(BT_MSG_ACQUIRE_LOCAL_DEVICE);
63 
64     return RequestLocalDeviceID(&request);
65 }
66 
67 
68 LocalDevice*
69 LocalDevice::GetLocalDevice(hci_id hid)
70 {
71     if ((sfMessenger = _RetrieveBluetoothMessenger()) == NULL)
72         return NULL;
73 
74     BMessage request(BT_MSG_ACQUIRE_LOCAL_DEVICE);
75     request.AddInt32("hci_id", hid);
76 
77     return RequestLocalDeviceID(&request);
78 
79 }
80 
81 
82 LocalDevice*
83 LocalDevice::GetLocalDevice(bdaddr_t bdaddr)
84 {
85     if ((sfMessenger = _RetrieveBluetoothMessenger()) == NULL)
86         return NULL;
87 
88     BMessage request(BT_MSG_ACQUIRE_LOCAL_DEVICE);
89     request.AddData("bdaddr", B_ANY_TYPE, &bdaddr, sizeof(bdaddr_t));
90 
91 
92     return RequestLocalDeviceID(&request);
93 }
94 
95 
96 uint32
97 LocalDevice::GetLocalDeviceCount()
98 {
99     if ((sfMessenger = _RetrieveBluetoothMessenger()) == NULL)
100         return 0;
101 
102 	BMessage request(BT_MSG_COUNT_LOCAL_DEVICES);
103 	BMessage reply;
104 
105 	if (sfMessenger->SendMessage(&request, &reply) == B_OK)
106 		return reply.FindInt32("count");
107     else
108         return 0;
109 
110 }
111 
112 
113 DiscoveryAgent*
114 LocalDevice::GetDiscoveryAgent()
115 {
116 	/* TODO: Study a singleton here */
117 	return new DiscoveryAgent(this);
118 }
119 
120 
121 BString
122 LocalDevice::GetProperty(const char* property)
123 {
124 
125 	return NULL;
126 
127 }
128 
129 
130 void
131 LocalDevice::GetProperty(const char* property, uint32* value)
132 {
133 
134 	*value = 0;
135 }
136 
137 
138 int
139 LocalDevice::GetDiscoverable()
140 {
141 
142 	return 0;
143 }
144 
145 
146 status_t
147 LocalDevice::SetDiscoverable(int mode)
148 {
149     if ((fMessenger = _RetrieveBluetoothMessenger()) == NULL)
150         return B_ERROR;
151 
152     BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
153     BMessage reply;
154 
155 	size_t   size;
156 	int8	 bt_status = BT_ERROR;
157 
158 
159     request.AddInt32("hci_id", hid);
160 
161 
162 	void* command = buildWriteScan(mode, &size);
163 
164 	if (command == NULL) {
165 		return B_NO_MEMORY;
166 	}
167 
168 	request.AddData("raw command", B_ANY_TYPE, command, size);
169     request.AddInt16("eventExpected",  HCI_EVENT_CMD_COMPLETE);
170     request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_CONTROL_BASEBAND, OCF_WRITE_SCAN_ENABLE));
171 
172     if (fMessenger->SendMessage(&request, &reply) == B_OK) {
173     	if (reply.FindInt8("status", &bt_status ) == B_OK ) {
174 			return bt_status;
175 
176 		}
177 
178 	}
179 
180 	return B_ERROR;
181 }
182 
183 
184 bdaddr_t
185 LocalDevice::GetBluetoothAddress()
186 {
187     if ((fMessenger = _RetrieveBluetoothMessenger()) == NULL)
188         return bdaddrUtils::NullAddress();
189 
190     const bdaddr_t* bdaddr;
191     BMessage request(BT_MSG_GET_ADDRESS);
192     BMessage reply;
193  	ssize_t	size;
194 
195     /* ADD ID */
196     request.AddInt32("hci_id", hid);
197 
198     if (fMessenger->SendMessage(&request, &reply) == B_OK) {
199 
200     	if (reply.FindData("bdaddr", B_ANY_TYPE, 0, (const void**)&bdaddr, &size) == B_OK ){
201 
202 	    	return *bdaddr;
203 
204 	    } else {
205 	    	return bdaddrUtils::NullAddress();
206 	    }
207 
208     }
209 
210 	return bdaddrUtils::NullAddress();
211 }
212 
213 
214 BString
215 LocalDevice::GetFriendlyName()
216 {
217     if ((fMessenger = _RetrieveBluetoothMessenger()) == NULL)
218         return NULL;
219 
220     BString friendlyname;
221     BMessage request(BT_MSG_GET_FRIENDLY_NAME);
222     BMessage reply;
223 
224     /* ADD ID */
225     request.AddInt32("hci_id", hid);
226 
227     if (fMessenger->SendMessage(&request, &reply) == B_OK &&
228         reply.FindString("friendlyname", &friendlyname) == B_OK ){
229 
230 	    return friendlyname;
231     }
232 
233 	return BString("Unknown");
234 }
235 
236 
237 DeviceClass
238 LocalDevice::GetDeviceClass()
239 {
240 
241 	return DeviceClass(0);
242 
243 }
244 
245 
246 /*
247 ServiceRecord
248 LocalDevice::getRecord(Connection notifier) {
249 
250 }
251 
252 void
253 LocalDevice::updateRecord(ServiceRecord srvRecord) {
254 
255 }
256 */
257 
258 
259 LocalDevice::LocalDevice(hci_id hid) : hid(hid)
260 {
261 
262 }
263 
264 
265 }
266