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

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

Fixed some minor things

File size: 11.4 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
66BluetoothSdp::BluetoothSdp(BootstrapInformationCallback* _callback) :
67 BootstrapModule(_callback), scan_timer_(io_service_) {
68#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
69
70 // This can be ignored, as the channel we really be saved in one
71 // of the info strings (as an attribute)
72 channel_ = 1;
73
74#endif // HAVE_BLUETOOTH_BLUETOOTH_H
75}
76
77BluetoothSdp::~BluetoothSdp() {
78}
79
80string BluetoothSdp::getName() {
81 return "BluetoothSdp";
82}
83
84string BluetoothSdp::getInformation() {
85 return "bootstrap module based on bluetooth service discovery protocol";
86}
87
88bool BluetoothSdp::isFunctional() {
89#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
90 return true; // Not tested yet :)
91#else
92 return false;
93#endif
94}
95
96void BluetoothSdp::start() {
97#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
98
99 /*
100 * Initializes and forks the scanner.
101 */
102
103 io_service_.post(boost::bind(&BluetoothSdp::bt_scan, this));
104 t_ = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_));
105
106#endif // HAVE_BLUETOOTH_BLUETOOTH_H
107}
108
109void BluetoothSdp::stop() {
110#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
111
112 /*
113 * Stops the scanner.
114 */
115
116 // not sure if this is thread safe
117 io_service_.stop();
118 t_.join();
119
120#endif // HAVE_BLUETOOTH_BLUETOOTH_H
121}
122
123void BluetoothSdp::publishService(string name, string info1, string info2,
124 string info3) {
125#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
126
127 /*
128 * Publishes an SpoVNet SDP Service and
129 * adds the arguments as info attributes.
130 */
131
132 logging_info("Registering SDP service");
133
134 uint8_t rfcomm_channel = channel_;
135
136 uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid, svc_class_uuid;
137 sdp_list_t *l2cap_list = 0, *rfcomm_list = 0, *root_list = 0, *proto_list =
138 0, *access_proto_list = 0, *svc_class_list = 0, *profile_list = 0;
139 sdp_data_t *channel = 0;
140 sdp_profile_desc_t profile;
141 sdp_record_t record = {0};
142 sdp_session_ = 0;
143
144 bdaddr_t bdaddr_any = (bdaddr_t) { {0, 0, 0, 0, 0, 0}};
145 bdaddr_t bdaddr_local = (bdaddr_t) { {0, 0, 0, 0xff, 0xff, 0xff}};
146
147 // prepare the info attribute buffers
148 //string namebuf, info1buf, info2buf, info3buf;
149 uint8_t namelen, info1len, info2len, info3len;
150
151 namelen = (uint8_t)name.length();
152 info1len = (uint8_t)info1.length();
153 info2len = (uint8_t)info2.length();
154 info3len = (uint8_t)info3.length();
155
156 if((namelen > 256) || (info1len > 256) || (info2len > 256) || (info3len > 256)) {
157 logging_error("String Argument too long, max size is 256!");
158 return;
159 }
160
161 // we need to save the string len for sdp; do we?
162 /*
163 namebuf = (char)namelen;
164 namebuf.append(name);
165 info1buf = (char)info1len;
166 info1buf.append(info1);
167 info2buf = (char)info2len;
168 info2buf.append(info2);
169 info3buf = (char)info3len;
170 info3buf.append(info3);
171 */
172
173 // set the general service ID
174 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
175 sdp_set_service_id(&record, svc_uuid);
176
177 // set the service class
178 sdp_uuid16_create(&svc_class_uuid, SERIAL_PORT_SVCLASS_ID);
179 svc_class_list = sdp_list_append(0, &svc_class_uuid);
180 sdp_set_service_classes(&record, svc_class_list);
181
182 // set the Bluetooth profile information
183 sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
184 profile.version = 0x0100;
185 profile_list = sdp_list_append(0, &profile);
186 sdp_set_profile_descs(&record, profile_list);
187
188 // make the service record publicly browsable
189 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
190 root_list = sdp_list_append(0, &root_uuid);
191 sdp_set_browse_groups(&record, root_list);
192
193 // set l2cap informatiint argc, char* argv[]on
194 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
195 l2cap_list = sdp_list_append(0, &l2cap_uuid);
196 proto_list = sdp_list_append(0, l2cap_list);
197
198 // register the RFCOMM channel for RFCOMM sockets
199 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
200 channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
201 rfcomm_list = sdp_list_append(0, &rfcomm_uuid);
202 sdp_list_append(rfcomm_list, channel);
203 sdp_list_append(proto_list, rfcomm_list);
204
205 access_proto_list = sdp_list_append(0, proto_list);
206 sdp_set_access_protos(&record, access_proto_list);
207
208 // set the name, provider, and description
209 sdp_set_info_attr(&record, service_name, service_prov, svc_dsc);
210
211 // add the spovnet attributes
212 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_NAME, SDP_TEXT_STR8,
213 name.data());
214
215 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO1, SDP_TEXT_STR8,
216 info1.data());
217
218 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO2, SDP_TEXT_STR8,
219 info2.data());
220
221 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO3, SDP_TEXT_STR8,
222 info3.data());
223
224 // connect to the local SDP server, register the service record,
225 // and disconnect
226 sdp_session_ = sdp_connect(&bdaddr_any, &bdaddr_local, SDP_RETRY_IF_BUSY);
227
228 if (sdp_session_ == 0) {
229 logging_error( "Something is wrong with your SDP server, nothing registered" );
230 } else {
231 sdp_record_register(sdp_session_, &record, 0);
232 }
233
234 // cleanup
235 sdp_data_free(channel);
236 sdp_list_free(l2cap_list, 0);
237 sdp_list_free(rfcomm_list, 0);
238 sdp_list_free(root_list, 0);
239 sdp_list_free(access_proto_list, 0);
240 sdp_list_free(svc_class_list, 0);
241 sdp_list_free(profile_list, 0);
242
243#endif // HAVE_BLUETOOTH_BLUETOOTH_H
244}
245
246void BluetoothSdp::revokeService(string name) {
247#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
248
249 logging_info("Unregistering SDP service");
250 sdp_close(sdp_session_);
251
252#endif // HAVE_BLUETOOTH_BLUETOOTH_H
253}
254
255#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
256
257void BluetoothSdp::bt_scan() {
258
259
260 /*
261 * Scans for other bluetooth devices and starts a SDP search on them.
262 * Repeats 20 seconds after the end of the scan.
263 */
264
265 logging_info("Scanning for peers");
266
267 inquiry_info *ii = NULL;
268 int max_rsp, num_rsp;
269 int dev_id, sock, len, flags;
270 int i;
271
272 bdaddr_t address;
273 uint8_t channel;
274
275 dev_id = hci_get_route(NULL);
276 sock = hci_open_dev(dev_id);
277 if (dev_id < 0 || sock < 0) {
278 logging_error("opening socket");
279 exit(1);
280 }
281
282 len = 8;
283 max_rsp = 255;
284 flags = IREQ_CACHE_FLUSH;
285 ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
286
287 num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
288 if (num_rsp < 0)
289 logging_error("hci_inquiry");
290
291 for (i = 0; i < num_rsp; i++) {
292 /*
293 ba2str(&(ii + i)->bdaddr, addr);
294 memset(name, 0, sizeof(name));
295 if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name),
296 name, 0) < 0)
297 strcpy(name, "[unknown]");
298 printf("%s %s\n", addr, name);
299 */
300
301 address = (ii + i)->bdaddr;
302
303 logging_info("Found peer " << ba2string(&address) << ", querying SDP.")
304
305 // TODO: sdp_search can be very slow, fork it!
306 sdp_search(address);
307 }
308
309 free(ii);
310 close(sock);
311
312 logging_info("Next scan in 20 seconds");
313
314 scan_timer_.expires_from_now(boost::posix_time::seconds(20));
315 scan_timer_.async_wait(boost::bind(&BluetoothSdp::bt_scan, this));
316}
317
318void BluetoothSdp::sdp_search(bdaddr_t target) {
319
320 /*
321 * Searches target for SDP records with the SpoVnet uuid
322 * and extracts its info attributes.
323 */
324
325 int status;
326 uuid_t svc_uuid;
327 sdp_list_t *response_list, *search_list, *attrid_list;
328 sdp_session_t *session = 0;
329 uint32_t range = 0x0000ffff;
330 uint8_t port = 0;
331 bdaddr_t any_addr = (bdaddr_t) { {0, 0, 0, 0, 0, 0}};
332
333 // prepare the buffers for the attributes
334 char name[256], info1[256], info2[256], info3[256];
335
336 // connect to the SDP server running on the remote machine
337 session = sdp_connect(&any_addr, &target, 0);
338
339 if (session == 0) {
340 logging_error("Failed to connect to SDP server at " << ba2string(&target) <<".");
341 return;
342 }
343
344 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
345 search_list = sdp_list_append(0, &svc_uuid);
346 attrid_list = sdp_list_append(0, &range);
347
348 // get a list of service records that have UUID uuid_
349 response_list = NULL;
350 status = sdp_service_search_attr_req(session, search_list,
351 SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
352
353 if (status == 0) {
354 sdp_list_t *proto_list = NULL;
355 sdp_list_t *r = response_list;
356
357 // go through each of the service records
358 for (; r; r = r->next) {
359 sdp_record_t *rec = (sdp_record_t*) r->data;
360
361 // get a list of the protocol sequences
362 if (sdp_get_access_protos(rec, &proto_list) == 0) {
363
364 // get the RFCOMM port number
365 port = sdp_get_proto_port(proto_list, RFCOMM_UUID);
366
367 sdp_list_free(proto_list, 0);
368
369 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_NAME, (char*)&name, 256);
370 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO1, (char*)&name, 256);
371 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO2, (char*)&name, 256);
372 sdp_get_string_attr(rec, SDP_ATTR_SPOVNET_INFO3, (char*)&name, 256);
373
374 logging_info("Remote peer name is: " << name);
375 logging_info("Remote peer info1 is: " << info1);
376 logging_info("Remote peer info2 is: " << info2);
377 logging_info("Remote peer info3 is: " << info3);
378
379 // Callback
380 callback->onBootstrapServiceFound(name, info1, info2, info3);
381
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
393string BluetoothSdp::ba2string(bdaddr_t *ba) {
394 /*
395 * Returns a string holding the bt adress in human readable form.
396 */
397 char *str;
398 ba2str(ba, str);
399 string result = str;
400 return result;
401}
402
403#endif // HAVE_BLUETOOTH_BLUETOOTH_H
404
405}} //namespace ariba, utility
Note: See TracBrowser for help on using the repository browser.