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