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

Last change on this file since 7523 was 7043, checked in by Christoph Mayer, 15 years ago

-bluetooth optional

File size: 13.3 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#include "ariba/overlay/OverlayBootstrap.h"
41
42#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
43
44// Attribute descriptors for SDP
45// base was chosen randomly
46#define SDP_SPOVNET_BASE 0x4000
47#define SDP_ATTR_SPOVNET_NAME 0x0000 + SDP_SPOVNET_BASE
48#define SDP_ATTR_SPOVNET_INFO1 0x0001 + SDP_SPOVNET_BASE
49#define SDP_ATTR_SPOVNET_INFO2 0x0002 + SDP_SPOVNET_BASE
50#define SDP_ATTR_SPOVNET_INFO3 0x0003 + SDP_SPOVNET_BASE
51
52// The SpoVNet unique identifier, this should be the same for all SpoVNet implementations
53const uint8_t svc_uuid_int[] = {0x59, 0x29, 0x24, 0x34, 0x69, 0x42, 0x11, 0xde, 0x94,
54 0x3e, 0x00, 0x21, 0x5d, 0xb4, 0xd8, 0x54};
55
56const char *service_name = "SpoVNet";
57const char *svc_dsc = "www.ariba-underlay.org";
58const char *service_prov = "ITM Uni Karlsruhe";
59
60#endif // HAVE_BLUETOOTH_BLUETOOTH_H
61
62
63namespace ariba {
64namespace utility {
65
66#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
67static bdaddr_t bd_addr_any = {{0, 0, 0, 0, 0, 0}};
68static bdaddr_t bd_addr_local = {{0, 0, 0, 0xff, 0xff, 0xff}};
69#endif
70
71use_logging_cpp(BluetoothSdp);
72OverlayBootstrap* BluetoothSdp::CONNECTION_CHECKER = NULL;
73
74BluetoothSdp::BluetoothSdp(BootstrapInformationCallback* _callback) :
75 BootstrapModule(_callback), scan_timer_(io_service_) {
76 srand( time(NULL) );
77#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
78
79 // This can be ignored, as the channel we really be saved in one
80 // of the info strings (as an attribute)
81 channel_ = 1;
82
83#endif // HAVE_BLUETOOTH_BLUETOOTH_H
84}
85
86BluetoothSdp::~BluetoothSdp() {
87}
88
89string BluetoothSdp::getName() {
90 return "BluetoothSdp";
91}
92
93string BluetoothSdp::getInformation() {
94 return "bootstrap module based on bluetooth service discovery protocol";
95}
96
97bool BluetoothSdp::isFunctional() {
98#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
99 return true;
100#else
101 return false;
102#endif
103}
104
105void BluetoothSdp::start() {
106#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
107
108 /*
109 * Initializes and forks the scanner.
110 */
111
112 io_service_.post(boost::bind(&BluetoothSdp::bt_scan, this));
113 t_ = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
114
115#endif // HAVE_BLUETOOTH_BLUETOOTH_H
116}
117
118void BluetoothSdp::stop() {
119#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
120
121 /*
122 * Stops the scanner.
123 */
124
125 // not sure if this is thread safe
126 io_service_.stop();
127 t_.join();
128
129 if(sdp_session_ != NULL)
130 sdp_close(sdp_session_);
131
132#endif // HAVE_BLUETOOTH_BLUETOOTH_H
133}
134
135void BluetoothSdp::publishService(string name, string info1, string info2,
136 string info3) {
137#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
138
139 /*
140 * Publishes an SpoVNet SDP Service and
141 * adds the arguments as info attributes.
142 */
143
144 logging_debug("registering SDP service");
145
146 uint8_t rfcomm_channel = channel_;
147
148 uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid, svc_class_uuid;
149 sdp_list_t *l2cap_list = 0, *rfcomm_list = 0, *root_list = 0, *proto_list =
150 0, *access_proto_list = 0, *svc_class_list = 0, *profile_list = 0;
151 sdp_data_t *channel = 0;
152 sdp_profile_desc_t profile;
153 sdp_record_t record = {0};
154 sdp_session_ = 0;
155
156 if((name.length() > 256) || (info1.length() > 256) || (info2.length() > 256) || (info3.length() > 256)) {
157 logging_error("string argument too long, max size is 256");
158 return;
159 }
160
161 // prepare the info attribute buffers
162 //string namebuf, info1buf, info2buf, info3buf;
163 uint8_t namelen, info1len, info2len, info3len;
164
165 namelen = (uint8_t)name.length();
166 info1len = (uint8_t)info1.length();
167 info2len = (uint8_t)info2.length();
168 info3len = (uint8_t)info3.length();
169
170 // set the general service ID
171 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
172 sdp_set_service_id(&record, svc_uuid);
173
174 // set the service class
175 sdp_uuid16_create(&svc_class_uuid, SERIAL_PORT_SVCLASS_ID);
176 svc_class_list = sdp_list_append(0, &svc_class_uuid);
177 sdp_set_service_classes(&record, svc_class_list);
178
179 // set the Bluetooth profile information
180 sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
181 profile.version = 0x0100;
182 profile_list = sdp_list_append(0, &profile);
183 sdp_set_profile_descs(&record, profile_list);
184
185 // make the service record publicly browsable
186 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
187 root_list = sdp_list_append(0, &root_uuid);
188 sdp_set_browse_groups(&record, root_list);
189
190 // set l2cap informatiint argc, char* argv[]on
191 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
192 l2cap_list = sdp_list_append(0, &l2cap_uuid);
193 proto_list = sdp_list_append(0, l2cap_list);
194
195 // register the RFCOMM channel for RFCOMM sockets
196 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
197 channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
198 rfcomm_list = sdp_list_append(0, &rfcomm_uuid);
199 sdp_list_append(rfcomm_list, channel);
200 sdp_list_append(proto_list, rfcomm_list);
201
202 access_proto_list = sdp_list_append(0, proto_list);
203 sdp_set_access_protos(&record, access_proto_list);
204
205 // set the name, provider, and description
206 sdp_set_info_attr(&record, service_name, service_prov, svc_dsc);
207
208 // add the spovnet attributes
209 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_NAME, SDP_TEXT_STR8,
210 name.data());
211
212 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO1, SDP_TEXT_STR8,
213 info1.data());
214
215 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO2, SDP_TEXT_STR8,
216 info2.data());
217
218 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO3, SDP_TEXT_STR8,
219 info3.data());
220
221 // connect to the local SDP server, register the service record
222 if( sdp_session_ == NULL ){
223 sdp_session_ = sdp_connect(&bd_addr_any, &bd_addr_local, SDP_RETRY_IF_BUSY);
224 }
225
226 if (sdp_session_ == NULL) {
227 logging_error( "something is wrong with your SDP server, nothing registered: " << strerror(errno) );
228 } else {
229 int ret = sdp_record_register(sdp_session_, &record, 0);
230
231 if(ret < 0){
232 logging_error("failed registering sdp record: " << strerror(errno));
233 }else{
234 logging_debug("sdp record registered using session " << sdp_session_);
235 }
236 }
237
238 // cleanup
239 sdp_data_free(channel);
240 sdp_list_free(l2cap_list, 0);
241 sdp_list_free(rfcomm_list, 0);
242 sdp_list_free(root_list, 0);
243 sdp_list_free(access_proto_list, 0);
244 sdp_list_free(svc_class_list, 0);
245 sdp_list_free(profile_list, 0);
246
247#endif // HAVE_BLUETOOTH_BLUETOOTH_H
248}
249
250void BluetoothSdp::revokeService(string name) {
251#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
252
253 logging_debug("unregistering SDP service");
254 sdp_close(sdp_session_);
255
256#endif // HAVE_BLUETOOTH_BLUETOOTH_H
257}
258
259#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
260
261void BluetoothSdp::bt_scan() {
262
263 //
264 // scan for devices if we have no active rfcomm connections running.
265 // otherwise we would break existing connections due to chipping seq
266 //
267
268 if(!haveConnections()){
269
270 /*
271 * Scans for other bluetooth devices and starts a SDP search on them.
272 */
273
274 logging_debug("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 bdaddr_t address;
282// uint8_t channel;
283
284 dev_id = hci_get_route(NULL);
285 sock = hci_open_dev(dev_id);
286 if (dev_id < 0 || sock < 0) {
287 logging_error("opening socket for device "
288 << dev_id << " failed. can not scan for peers: " << strerror(errno));
289 return;
290 }
291
292 len = 8;
293 max_rsp = 255;
294 flags = IREQ_CACHE_FLUSH;
295 ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
296
297 num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
298 if (num_rsp < 0)
299 logging_error("hci_inquiry failed with " << num_rsp << ": " << strerror(errno));
300
301 for (i = 0; i < num_rsp; i++) {
302 address = (ii + i)->bdaddr;
303
304 string saddress = ba2string(&address);
305 string sname = ba2name(&address, sock);
306
307 logging_debug("found peer [" << saddress << "] [" << sname << "]");
308 sdp_search( address, sname );
309 }
310
311 free(ii);
312 close(sock);
313
314 } else {
315 logging_debug("have active connections, no sdp searching");
316 }
317
318 int nextscan = (rand() % 10) + 5;
319 logging_debug("next sdp scan try in " << nextscan << " seconds");
320
321 scan_timer_.expires_from_now( boost::posix_time::seconds(nextscan) );
322 scan_timer_.async_wait( boost::bind(&BluetoothSdp::bt_scan, this) );
323}
324
325void BluetoothSdp::sdp_search(bdaddr_t target, string devicename) {
326
327 /*
328 * Searches target for SDP records with the SpoVnet uuid
329 * and extracts its info attributes.
330 */
331
332 int status;
333 uuid_t svc_uuid;
334 sdp_list_t *response_list, *search_list, *attrid_list;
335 sdp_session_t *session = NULL;
336 uint32_t range = 0x0000ffff;
337 uint8_t port = 0;
338
339 // connect to the SDP server running on the remote machine
340 logging_debug("querying services from bt device ["
341 << ba2string(&target) << "] [" << devicename << "]");
342
343 // prepare the buffers for the attributes
344 char name[256], info1[256], info2[256], info3[256];
345
346 session = sdp_connect(&bd_addr_any, &target, SDP_RETRY_IF_BUSY);
347
348 if (session == NULL) {
349 logging_error("failed to connect to SDP server at "
350 << ba2string(&target) << ": " << strerror(errno));
351 return;
352 }
353
354 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
355 search_list = sdp_list_append(0, &svc_uuid);
356 attrid_list = sdp_list_append(0, &range);
357
358 // get a list of service records that have UUID uuid_
359 response_list = NULL;
360 status = sdp_service_search_attr_req(session, search_list,
361 SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
362
363 if (status == 0) {
364 sdp_list_t *proto_list = NULL;
365 sdp_list_t *r = response_list;
366
367 // go through each of the service records
368 for ( ; r != NULL; r = r->next) {
369 sdp_record_t *rec = (sdp_record_t*) r->data;
370
371 // get a list of the protocol sequences
372 if (sdp_get_access_protos(rec, &proto_list) == 0) {
373
374 // get the RFCOMM port number
375 port = sdp_get_proto_port(proto_list, RFCOMM_UUID);
376
377 sdp_list_free(proto_list, 0);
378
379 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_NAME, (char*)&name, 256);
380 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO1, (char*)&info1, 256);
381 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO2, (char*)&info2, 256);
382 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO3, (char*)&info3, 256);
383
384 logging_info("Remote peer name is: " << name);
385 logging_info("Remote peer info1 is: " << info1);
386 logging_info("Remote peer info2 is: " << info2);
387 logging_info("Remote peer info3 is: " << info3);
388
389 // Callback
390 callback->onBootstrapServiceFound(name, info1, info2, info3);
391 }
392 sdp_record_free(rec);
393 }
394 } else {
395 logging_error("sdp_service_search_attr_req failed with timeout: " << strerror(errno));
396 }
397
398 sdp_list_free(response_list, 0);
399 sdp_list_free(search_list, 0);
400 sdp_list_free(attrid_list, 0);
401 sdp_close(session);
402}
403
404string BluetoothSdp::ba2string(bdaddr_t* ba) {
405 /*
406 * Returns a string holding the bt adress in human readable form.
407 */
408 char str[32] = { 0 };
409 ba2str(ba, str);
410 string result = str;
411 return result;
412}
413
414string BluetoothSdp::ba2name(bdaddr_t* ba, int sock){
415
416 char name[256] = {0};
417 memset(name, 0, sizeof(name));
418
419 if( hci_read_remote_name(sock, ba, sizeof(name), name, 0) < 0 )
420 strcpy(name, "unknown");
421
422 string result = name;
423 return result;
424}
425
426bool BluetoothSdp::haveConnections(){
427
428 // TODO: currently we check for overlay connectivity
429
430 if(CONNECTION_CHECKER == NULL) return false;
431 return CONNECTION_CHECKER->haveOverlayConnections();
432
433
434 /* TODO: this will check for rfcomm connections
435 struct hci_conn_list_req* cl = NULL;
436 struct hci_conn_info* ci = NULL;
437
438 int btsock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
439 if(btsock < 0){
440 logging_error("failed getting bluetooth raw socket");
441 return true; // return true to be safe here and not perform sdp scan
442 }
443
444 cl = (struct hci_conn_list_req*)malloc(10 * sizeof(struct hci_conn_info) + sizeof(struct hci_conn_list_req));
445
446 cl->dev_id = hci_get_route(NULL);;
447 cl->conn_num = 10;
448 ci = cl->conn_info;
449
450 if(ioctl(btsock, HCIGETCONNLIST, (void*)cl)){
451 logging_warn("could not get active rfcomm connections");
452 return true; // return true to be safe here and not perform sdp scan
453 }
454
455 bool haveconn = (cl->conn_num > 0);
456 logging_debug("we have " << cl->conn_num << " active hci connections");
457 free(cl);
458 close(btsock);
459
460 return haveconn;
461 */
462}
463
464#endif // HAVE_BLUETOOTH_BLUETOOTH_H
465
466}} //namespace ariba, utility
Note: See TracBrowser for help on using the repository browser.