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

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

-von außen konfigurierbare bootstrap module, -periodicbroadcast crash fix

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