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

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

bluetooth zeugs leicht gefixt

File size: 11.0 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
93 return false; // Not tested yet :)
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
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
162 namebuf = (char)namelen;
163 namebuf.append(name);
164 info1buf = (char)info1len;
165 info1buf.append(info1);
166 info2buf = (char)info2len;
167 info2buf.append(info2);
168 info3buf = (char)info3len;
169 info3buf.append(info3);
170
171 // set the general service ID
172 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
173 sdp_set_service_id(&record, svc_uuid);
174
175 // set the service class
176 sdp_uuid16_create(&svc_class_uuid, SERIAL_PORT_SVCLASS_ID);
177 svc_class_list = sdp_list_append(0, &svc_class_uuid);
178 sdp_set_service_classes(&record, svc_class_list);
179
180 // set the Bluetooth profile information
181 sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
182 profile.version = 0x0100;
183 profile_list = sdp_list_append(0, &profile);
184 sdp_set_profile_descs(&record, profile_list);
185
186 // make the service record publicly browsable
187 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
188 root_list = sdp_list_append(0, &root_uuid);
189 sdp_set_browse_groups(&record, root_list);
190
191 // set l2cap informatiint argc, char* argv[]on
192 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
193 l2cap_list = sdp_list_append(0, &l2cap_uuid);
194 proto_list = sdp_list_append(0, l2cap_list);
195
196 // register the RFCOMM channel for RFCOMM sockets
197 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
198 channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
199 rfcomm_list = sdp_list_append(0, &rfcomm_uuid);
200 sdp_list_append(rfcomm_list, channel);
201 sdp_list_append(proto_list, rfcomm_list);
202
203 access_proto_list = sdp_list_append(0, proto_list);
204 sdp_set_access_protos(&record, access_proto_list);
205
206 // set the name, provider, and description
207 sdp_set_info_attr(&record, service_name, service_prov, svc_dsc);
208
209
210 // add the info attributes
211 uint8_t namelen, info1len, info2len, info3len;
212 string namebuf, info1buf, info2buf, info3buf;
213
214
215 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_NAME, SDP_TEXT_STR32,
216 namebuf.data());
217
218 info1len = info1.length();
219 info1buf.append((const char*) &info1len, 4);
220 info1buf.append(info1);
221 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO1, SDP_TEXT_STR32,
222 info1buf.data());
223
224 info2len = info2.length();
225 info2buf.append((const char*) &info2len, 4);
226 info2buf.append(info2);
227 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO2, SDP_TEXT_STR32,
228 info2buf.data());
229
230 info3len = info3.length();
231 info3buf.append((const char*) &info3len, 4);
232 info3buf.append(info3);
233 sdp_attr_add_new(&record, SDP_ATTR_SPOVNET_INFO3, SDP_TEXT_STR32,
234 info3buf.data());
235
236 // connect to the local SDP server, register the service record,
237 // and disconnect
238 sdp_session_ = sdp_connect(&bdaddr_any, &bdaddr_local, SDP_RETRY_IF_BUSY);
239
240 if (sdp_session_ == 0) {
[4879]241 logging_error( "Something is wrong with your SDP server, nothing registered" );
[4870]242 } else {
243 sdp_record_register(sdp_session_, &record, 0);
244 }
245
246 // cleanup
247 sdp_data_free(channel);
248 sdp_list_free(l2cap_list, 0);
249 sdp_list_free(rfcomm_list, 0);
250 sdp_list_free(root_list, 0);
251 sdp_list_free(access_proto_list, 0);
252 sdp_list_free(svc_class_list, 0);
253 sdp_list_free(profile_list, 0);
254
255#endif // HAVE_BLUETOOTH_BLUETOOTH_H
[4840]256}
257
[4870]258void BluetoothSdp::revokeService(string name) {
259#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
260
[4879]261 logging_info("Unregistering SDP service");
[4870]262 sdp_close(sdp_session_);
263
264#endif // HAVE_BLUETOOTH_BLUETOOTH_H
[4840]265}
266
[4870]267#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
268
269void BluetoothSdp::bt_scan() {
270
271
272 /*
273 * Scans for other bluetooth devices and starts a SDP search on them.
274 * Repeats 20 seconds after the end of the scan.
275 */
276
[4879]277 logging_info("Scanning for peers");
[4870]278
279 inquiry_info *ii = NULL;
280 int max_rsp, num_rsp;
281 int dev_id, sock, len, flags;
282 int i;
283
284 /*
285 char addr[19] = { 0 };
286 char name[248] = { 0 };
287 */
288 bdaddr_t address;
289 uint8_t channel;
290
291 dev_id = hci_get_route(NULL);
292 sock = hci_open_dev(dev_id);
293 if (dev_id < 0 || sock < 0) {
[4879]294 logging_error("opening socket");
[4870]295 exit(1);
296 }
297
298 len = 8;
299 max_rsp = 255;
300 flags = IREQ_CACHE_FLUSH;
301 ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
302
303 num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
304 if (num_rsp < 0)
[4879]305 logging_error("hci_inquiry");
[4870]306
307 for (i = 0; i < num_rsp; i++) {
308 /*
309 ba2str(&(ii + i)->bdaddr, addr);
310 memset(name, 0, sizeof(name));
311 if (hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name),
312 name, 0) < 0)
313 strcpy(name, "[unknown]");
314 printf("%s %s\n", addr, name);
315 */
316
317 address = (ii + i)->bdaddr;
318 // TODO: sdp_search can be very slow, fork it!
319 sdp_search(address);
320 }
321
322 free(ii);
323 close(sock);
324
[4879]325 logging_info("Next scan in 20 seconds");
[4870]326
327 scan_timer_.expires_from_now(boost::posix_time::seconds(20));
328 scan_timer_.async_wait(boost::bind(&BluetoothSdp::bt_scan, this));
329}
330
331void BluetoothSdp::sdp_search(bdaddr_t target) {
332
333 /*
334 * Searches target for SDP records with the SpoVnet uuid
335 * and extracts its info attributes.
336 */
337
338 int status;
339 uuid_t svc_uuid;
340 sdp_list_t *response_list, *search_list, *attrid_list;
341 sdp_session_t *session = 0;
342 uint32_t range = 0x0000ffff;
343 uint8_t port = 0;
344 bdaddr_t any_addr = (bdaddr_t) { {0, 0, 0, 0, 0, 0}};
345
346 // prepare the buffers for the attributes
347 string name, info1, info2, info3;
348 name.reserve(2**32);
349
350 // connect to the SDP server running on the remote machine
351 session = sdp_connect(&any_addr, &target, 0);
352
353 if (session == 0) {
354 // TODO: put the remote's address here
355 logging_error("Failed to connect to remote SDP server.");
356 return;
357 }
358
359 sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
360 search_list = sdp_list_append(0, &svc_uuid);
361 attrid_list = sdp_list_append(0, &range);
362
363 // get a list of service records that have UUID uuid_
364 response_list = NULL;
365 status = sdp_service_search_attr_req(session, search_list,
366 SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
367
368 if (status == 0) {
369 sdp_list_t *proto_list = NULL;
370 sdp_list_t *r = response_list;
371
372 // go through each of the service records
373 for (; r; r = r->next) {
374 sdp_record_t *rec = (sdp_record_t*) r->data;
375
376 // get a list of the protocol sequences
377 if (sdp_get_access_protos(rec, &proto_list) == 0) {
378
379 // get the RFCOMM port number
380 port = sdp_get_proto_port(proto_list, RFCOMM_UUID);
381
382 sdp_list_free(proto_list, 0);
383
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.