Ignore:
Timestamp:
Aug 4, 2009, 10:17:33 AM (15 years ago)
Author:
Christoph Mayer
Message:

adress detection aufgeräumt, network info für bleutooth, data stream (hopeful crash fix), logging auf maemo nur warn, ...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/utility/bootstrap/modules/bluetoothsdp/BluetoothSdp.cpp

    r5633 r5638  
    4141#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
    4242
    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
    52         const uint8_t svc_uuid_int[] = {0x59, 0x29, 0x24, 0x34, 0x69, 0x42, 0x11, 0xde, 0x94,
     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,
    5353                0x3e, 0x00, 0x21, 0x5d, 0xb4, 0xd8, 0x54};
    5454
    55         const char *service_name = "SpoVNet";
    56         const char *svc_dsc = "www.ariba-underlay.org";
    57         const char *service_prov = "ITM Uni Karlsruhe";
     55const char *service_name = "SpoVNet";
     56const char *svc_dsc = "www.ariba-underlay.org";
     57const char *service_prov = "ITM Uni Karlsruhe";
    5858
    5959#endif // HAVE_BLUETOOTH_BLUETOOTH_H
     
    6767        BootstrapModule(_callback), scan_timer_(io_service_) {
    6868        srand( time(NULL) );
    69         found = false;
    7069#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
    7170
     
    141140        uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid, svc_class_uuid;
    142141        sdp_list_t *l2cap_list = 0, *rfcomm_list = 0, *root_list = 0, *proto_list =
    143         0, *access_proto_list = 0, *svc_class_list = 0, *profile_list = 0;
     142                0, *access_proto_list = 0, *svc_class_list = 0, *profile_list = 0;
    144143        sdp_data_t *channel = 0;
    145144        sdp_profile_desc_t profile;
     
    253252
    254253void BluetoothSdp::bt_scan() {
    255         /*
    256          * Scans for other bluetooth devices and starts a SDP search on them.
    257          */
    258 
    259         logging_debug("scanning for peers");
    260 
    261         inquiry_info *ii = NULL;
    262         int max_rsp, num_rsp;
    263         int dev_id, sock, len, flags;
    264         int i;
    265 
    266         bdaddr_t address;
    267         uint8_t channel;
    268 
    269         dev_id = hci_get_route(NULL);
    270         sock = hci_open_dev(dev_id);
    271         if (dev_id < 0 || sock < 0) {
    272                 logging_error("opening socket for device "
    273                                 << dev_id << " failed. can not scan for peers: " << strerror(errno));
    274                 return;
    275         }
    276 
    277         len = 8;
    278         max_rsp = 255;
    279         flags = IREQ_CACHE_FLUSH;
    280         ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
    281 
    282         num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
    283         if (num_rsp < 0)
    284                 logging_error("hci_inquiry failed with " << num_rsp << ": " << strerror(errno));
    285 
    286         for (i = 0; i < num_rsp; i++) {
    287                 address = (ii + i)->bdaddr;
    288 
    289                 string saddress = ba2string(&address);
    290                 string sname = ba2name(&address, sock);
    291 
    292                 logging_debug("found peer [" << saddress << "] [" << sname << "]");
    293                 sdp_search( address, sname );
    294         }
    295 
    296         free(ii);
    297         close(sock);
    298 
    299         if(!found){
    300                 int nextscan = (rand() % 30) + 5;
    301                 logging_debug("next sdp scan in " << nextscan << " seconds");
    302 
    303 
    304                 scan_timer_.expires_from_now( boost::posix_time::seconds(nextscan) );
    305                 scan_timer_.async_wait( boost::bind(&BluetoothSdp::bt_scan, this) );
    306         }
     254
     255        //
     256        // scan for devices if we have no active rfcomm connections running.
     257        // otherwise we would break existing connections due to chipping seq
     258        //
     259
     260        if(!haveConnections()){
     261
     262                /*
     263                 * Scans for other bluetooth devices and starts a SDP search on them.
     264                 */
     265
     266                logging_debug("scanning for peers");
     267
     268                inquiry_info *ii = NULL;
     269                int max_rsp, num_rsp;
     270                int dev_id, sock, len, flags;
     271                int i;
     272
     273                bdaddr_t address;
     274                uint8_t channel;
     275
     276                dev_id = hci_get_route(NULL);
     277                sock = hci_open_dev(dev_id);
     278                if (dev_id < 0 || sock < 0) {
     279                        logging_error("opening socket for device "
     280                                        << dev_id << " failed. can not scan for peers: " << strerror(errno));
     281                        return;
     282                }
     283
     284                len = 8;
     285                max_rsp = 255;
     286                flags = IREQ_CACHE_FLUSH;
     287                ii = (inquiry_info*) malloc(max_rsp * sizeof(inquiry_info));
     288
     289                num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
     290                if (num_rsp < 0)
     291                        logging_error("hci_inquiry failed with " << num_rsp << ": " << strerror(errno));
     292
     293                for (i = 0; i < num_rsp; i++) {
     294                        address = (ii + i)->bdaddr;
     295
     296                        string saddress = ba2string(&address);
     297                        string sname = ba2name(&address, sock);
     298
     299                        logging_debug("found peer [" << saddress << "] [" << sname << "]");
     300                        sdp_search( address, sname );
     301                }
     302
     303                free(ii);
     304                close(sock);
     305
     306        } else {
     307                logging_debug("have active rfcomm connectinos, not searching");
     308        }
     309
     310        int nextscan = (rand() % 30) + 5;
     311        logging_debug("next sdp scan try in " << nextscan << " seconds");
     312
     313        scan_timer_.expires_from_now( boost::posix_time::seconds(nextscan) );
     314        scan_timer_.async_wait( boost::bind(&BluetoothSdp::bt_scan, this) );
    307315}
    308316
     
    372380
    373381                                // Callback
    374                                 found = true;
    375382                                callback->onBootstrapServiceFound(name, info1, info2, info3);
    376383                        }
     
    410417}
    411418
     419bool BluetoothSdp::haveConnections(){
     420
     421        struct hci_conn_list_req* cl = NULL;
     422        struct hci_conn_info* ci = NULL;
     423
     424        int btsock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
     425        if(btsock <  0){
     426                logging_error("failed getting bluetooth raw socket");
     427                return true; // return true to be safe here and not perform sdp scan
     428        }
     429
     430        cl = (struct hci_conn_list_req*)malloc(10 * sizeof(struct hci_conn_info) + sizeof(struct hci_conn_list_req));
     431
     432        cl->dev_id = hci_get_route(NULL);;
     433        cl->conn_num = 10;
     434        ci = cl->conn_info;
     435
     436        if(ioctl(btsock, HCIGETCONNLIST, (void*)cl)){
     437                logging_warn("could not get active rfcomm connections");
     438                return true; // return true to be safe here and not perform sdp scan
     439        }
     440
     441        bool haveconn = (cl->conn_num > 0);
     442        logging_error("we have " << cl->conn_num << " active connections");
     443        free(cl);
     444        close(btsock);
     445
     446        return haveconn;
     447}
     448
    412449#endif // HAVE_BLUETOOTH_BLUETOOTH_H
    413450
Note: See TracChangeset for help on using the changeset viewer.