source: source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp@ 4894

Last change on this file since 4894 was 4894, checked in by stud-florian, 15 years ago

Fixed a few minor things, bluetooth compiles again (but still not tested).

File size: 11.0 KB
Line 
1// [License]
2// The Ariba-Underlay Copyright
3//
4// Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
5//
6// Institute of Telematics
7// UniversitÀt Karlsruhe (TH)
8// Zirkel 2, 76128 Karlsruhe
9// Germany
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
22// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
25// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33// The views and conclusions contained in the software and documentation
34// are those of the authors and should not be interpreted as representing
35// official policies, either expressed or implied, of the Institute of
36// Telematics.
37// [License]
38
39#include "BluetoothSdp.h"
40
41#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
42
43// Attribute descriptors for SDP
44// base was chosen randomly
45#define SDP_SPOVNET_BASE 0x4000
46#define SDP_ATTR_SPOVNET_NAME 0x0000 + SDP_SPOVNET_BASE
47#define SDP_ATTR_SPOVNET_INFO1 0x0001 + SDP_SPOVNET_BASE
48#define SDP_ATTR_SPOVNET_INFO2 0x0002 + SDP_SPOVNET_BASE
49#define SDP_ATTR_SPOVNET_INFO3 0x0003 + SDP_SPOVNET_BASE
50
51// The SpoVNet unique identifier, this should be the same for all SpoVNet implementations
52const uint8_t svc_uuid_int[] = {0x59, 0x29, 0x24, 0x34, 0x69, 0x42, 0x11, 0xde, 0x94,
53 0x3e, 0x00, 0x21, 0x5d, 0xb4, 0xd8, 0x54};
54
55const char *service_name = "SpoVNet";
56const char *svc_dsc = "Spontaneous Virtual Network";
57const char *service_prov = "ITM Uni Karlsruhe";
58
59#endif // HAVE_BLUETOOTH_BLUETOOTH_H
60
61
62namespace ariba {
63namespace utility {
64use_logging_cpp(BluetoothSdp)
65;
66
67//TODO: figure out this compiler flag stuff
68
69BluetoothSdp::BluetoothSdp(BootstrapInformationCallback* _callback) :
70 BootstrapModule(_callback), scan_timer_(io_service_) {
71#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
72
73 // TODO: think about this!
74 channel_ = 7;
75 // TODO: what about the callback?
76
77#endif // HAVE_BLUETOOTH_BLUETOOTH_H
78}
79
80BluetoothSdp::~BluetoothSdp() {
81}
82
83string BluetoothSdp::getName() {
84 return "BluetoothSdp";
85}
86
87string BluetoothSdp::getInformation() {
88 return "bootstrap module based on bluetooth service discovery protocol";
89}
90
91bool BluetoothSdp::isFunctional() {
92#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
93 return false; // Not tested yet :)
94#else
95 return false;
96#endif
97}
98
99void BluetoothSdp::start() {
100#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
101
102 /*
103 * Initializes and forks the scanner.
104 */
105
106 io_service_.post(boost::bind(&BluetoothSdp::bt_scan, this));
107 t_ = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
108
109#endif // HAVE_BLUETOOTH_BLUETOOTH_H
110}
111
112void BluetoothSdp::stop() {
113#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
114
115 /*
116 * Stops the scanner.
117 */
118
119 // not sure if this is thread safe
120 io_service_.stop();
121 t_.join();
122
123#endif // HAVE_BLUETOOTH_BLUETOOTH_H
124}
125
126void BluetoothSdp::publishService(string name, string info1, string info2,
127 string info3) {
128#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
129
130 /*
131 * Publishes an SpoVNet SDP Service and
132 * adds the arguments as info attributes.
133 */
134
135 logging_info("Registering SDP service");
136
137 uint8_t rfcomm_channel = channel_;
138
139 uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid, svc_class_uuid;
140 sdp_list_t *l2cap_list = 0, *rfcomm_list = 0, *root_list = 0, *proto_list =
141 0, *access_proto_list = 0, *svc_class_list = 0, *profile_list = 0;
142 sdp_data_t *channel = 0;
143 sdp_profile_desc_t profile;
144 sdp_record_t record = {0};
145 sdp_session_ = 0;
146
147 bdaddr_t bdaddr_any = (bdaddr_t) { {0, 0, 0, 0, 0, 0}};
148 bdaddr_t bdaddr_local = (bdaddr_t) { {0, 0, 0, 0xff, 0xff, 0xff}};
149
150 // prepare the info attribute buffers
151 string namebuf, info1buf, info2buf, info3buf;
152 uint8_t namelen, info1len, info2len, info3len;
153
154 namelen = (uint8_t)name.length();
155 info1len = (uint8_t)info1.length();
156 info2len = (uint8_t)info2.length();
157 info3len = (uint8_t)info3.length();
158
159 if((namelen > 256) || (info1len > 256) || (info2len > 256) || (info3len > 256)) {
160 logging_error("String Argument too long, max size is 256!");
161 return;
162 }
163
164 // we need to save the string len for sdp
165 namebuf = (char)namelen;
166 namebuf.append(name);
167 info1buf = (char)info1len;
168 info1buf.append(info1);
169 info2buf = (char)info2len;
170 info2buf.append(info2);
171 info3buf = (char)info3len;
172 info3buf.append(info3);
173
174 // set the general service ID
175 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
176 sdp_set_service_id(&record, svc_uuid);
177
178 // set the service class
179 sdp_uuid16_create(&svc_class_uuid, SERIAL_PORT_SVCLASS_ID);
180 svc_class_list = sdp_list_append(0, &svc_class_uuid);
181 sdp_set_service_classes(&record, svc_class_list);
182
183 // set the Bluetooth profile information
184 sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
185 profile.version = 0x0100;
186 profile_list = sdp_list_append(0, &profile);
187 sdp_set_profile_descs(&record, profile_list);
188
189 // make the service record publicly browsable
190 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
191 root_list = sdp_list_append(0, &root_uuid);
192 sdp_set_browse_groups(&record, root_list);
193
194 // set l2cap informatiint argc, char* argv[]on
195 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
196 l2cap_list = sdp_list_append(0, &l2cap_uuid);
197 proto_list = sdp_list_append(0, l2cap_list);
198
199 // register the RFCOMM channel for RFCOMM sockets
200 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
201 channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
202 rfcomm_list = sdp_list_append(0, &rfcomm_uuid);
203 sdp_list_append(rfcomm_list, channel);
204 sdp_list_append(proto_list, rfcomm_list);
205
206 access_proto_list = sdp_list_append(0, proto_list);
207 sdp_set_access_protos(&record, access_proto_list);
208
209 // set the name, provider, and description
210 sdp_set_info_attr(&record, service_name, service_prov, svc_dsc);
211
212 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_NAME, SDP_TEXT_STR32,
213 namebuf.data());
214
215 info1len = info1.length();
216 info1buf.append((const char*) &info1len, 4);
217 info1buf.append(info1);
218 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO1, SDP_TEXT_STR32,
219 info1buf.data());
220
221 info2len = info2.length();
222 info2buf.append((const char*) &info2len, 4);
223 info2buf.append(info2);
224 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO2, SDP_TEXT_STR32,
225 info2buf.data());
226
227 info3len = info3.length();
228 info3buf.append((const char*) &info3len, 4);
229 info3buf.append(info3);
230 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO3, SDP_TEXT_STR32,
231 info3buf.data());
232
233 // connect to the local SDP server, register the service record,
234 // and disconnect
235 sdp_session_ = sdp_connect(&bdaddr_any, &bdaddr_local, SDP_RETRY_IF_BUSY);
236
237 if (sdp_session_ == 0) {
238 logging_error( "Something is wrong with your SDP server, nothing registered" );
239 } else {
240 sdp_record_register(sdp_session_, &record, 0);
241 }
242
243 // cleanup
244 sdp_data_free(channel);
245 sdp_list_free(l2cap_list, 0);
246 sdp_list_free(rfcomm_list, 0);
247 sdp_list_free(root_list, 0);
248 sdp_list_free(access_proto_list, 0);
249 sdp_list_free(svc_class_list, 0);
250 sdp_list_free(profile_list, 0);
251
252#endif // HAVE_BLUETOOTH_BLUETOOTH_H
253}
254
255void BluetoothSdp::revokeService(string name) {
256#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
257
258 logging_info("Unregistering SDP service");
259 sdp_close(sdp_session_);
260
261#endif // HAVE_BLUETOOTH_BLUETOOTH_H
262}
263
264#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
265
266void BluetoothSdp::bt_scan() {
267
268
269 /*
270 * Scans for other bluetooth devices and starts a SDP search on them.
271 * Repeats 20 seconds after the end of the scan.
272 */
273
274 logging_info("Scanning for peers");
275
276 inquiry_info *ii = NULL;
277 int max_rsp, num_rsp;
278 int dev_id, sock, len, flags;
279 int i;
280
281 /*
282 char addr[19] = { 0 };
283 char name[248] = { 0 };
284 */
285 bdaddr_t address;
286 uint8_t channel;
287
288 dev_id = hci_get_route(NULL);
289 sock = hci_open_dev(dev_id);
290 if (dev_id < 0 || sock < 0) {
291 logging_error("opening socket");
292 exit(1);
293 }
294
295 len = 8;
296 max_rsp = 255;
297 flags = IREQ_CACHE_FLUSH;
298 ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
299
300 num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
301 if (num_rsp < 0)
302 logging_error("hci_inquiry");
303
304 for (i = 0; i < num_rsp; i++) {
305 /*
306 ba2str(&(ii + i)->bdaddr, addr);
307 memset(name, 0, sizeof(name));
308 if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name),
309 name, 0) < 0)
310 strcpy(name, "[unknown]");
311 printf("%s %s\n", addr, name);
312 */
313
314 address = (ii + i)->bdaddr;
315 // TODO: sdp_search can be very slow, fork it!
316 sdp_search(address);
317 }
318
319 free(ii);
320 close(sock);
321
322 logging_info("Next scan in 20 seconds");
323
324 scan_timer_.expires_from_now(boost::posix_time::seconds(20));
325 scan_timer_.async_wait(boost::bind(&BluetoothSdp::bt_scan, this));
326}
327
328void BluetoothSdp::sdp_search(bdaddr_t target) {
329
330 /*
331 * Searches target for SDP records with the SpoVnet uuid
332 * and extracts its info attributes.
333 */
334
335 int status;
336 uuid_t svc_uuid;
337 sdp_list_t *response_list, *search_list, *attrid_list;
338 sdp_session_t *session = 0;
339 uint32_t range = 0x0000ffff;
340 uint8_t port = 0;
341 bdaddr_t any_addr = (bdaddr_t) { {0, 0, 0, 0, 0, 0}};
342
343 // prepare the buffers for the attributes
344 string name, info1, info2, info3;
345 name.reserve(256);
346
347 // connect to the SDP server running on the remote machine
348 session = sdp_connect(&any_addr, &target, 0);
349
350 if (session == 0) {
351 // TODO: put the remote's address here
352 logging_error("Failed to connect to remote SDP server.");
353 return;
354 }
355
356 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
357 search_list = sdp_list_append(0, &svc_uuid);
358 attrid_list = sdp_list_append(0, &range);
359
360 // get a list of service records that have UUID uuid_
361 response_list = NULL;
362 status = sdp_service_search_attr_req(session, search_list,
363 SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
364
365 if (status == 0) {
366 sdp_list_t *proto_list = NULL;
367 sdp_list_t *r = response_list;
368
369 // go through each of the service records
370 for (; r; r = r->next) {
371 sdp_record_t *rec = (sdp_record_t*) r->data;
372
373 // get a list of the protocol sequences
374 if (sdp_get_access_protos(rec, &proto_list) == 0) {
375
376 // get the RFCOMM port number
377 port = sdp_get_proto_port(proto_list, RFCOMM_UUID);
378
379 sdp_list_free(proto_list, 0);
380
381 //TODO: callback here + extract info's and stuff
382 }
383 sdp_record_free(rec);
384 }
385 }
386 sdp_list_free(response_list, 0);
387 sdp_list_free(search_list, 0);
388 sdp_list_free(attrid_list, 0);
389 sdp_close(session);
390
391}
392
393#endif // HAVE_BLUETOOTH_BLUETOOTH_H
394
395}} //namespace ariba, utility
Note: See TracBrowser for help on using the repository browser.