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

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

The attribute stuff in BluetoothSdp should be working now.

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