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

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

rfcomm sdp fixing

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