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

Last change on this file since 6939 was 6919, checked in by mies, 15 years ago

Fixed tons of warnings when using CXXFLAGS="-Wall"!

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