Advertisement
phr3ncj

bt_server.cpp

Feb 19th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.24 KB | None | 0 0
  1.     //g++ -o server bt_server.cpp `pkg-config --cflags --libs glib-2.0` -lbluetooth
  2.     #include <stdio.h>
  3.     #include <errno.h>
  4.     #include <fcntl.h>
  5.     #include <stdlib.h>
  6.     #include <unistd.h>
  7.     #include <sys/ioctl.h>
  8.     #include <sys/socket.h>
  9.     #include <bluetooth/bluetooth.h>
  10.     #include <bluetooth/bluetooth.h>
  11.     #include <bluetooth/sdp.h>
  12.     #include <bluetooth/sco.h>
  13.     #include <bluetooth/sdp_lib.h>
  14.     #include <bluetooth/rfcomm.h>
  15.     #include <bluetooth/l2cap.h>
  16.     //#include <glib.h>
  17.      
  18.     char btaddr_any[6] = { 0 };
  19.     char btaddr_local[6] = { 0, 0, 0, 0xff, 0xff, 0xff };
  20.     #define BDADDR_ANY (bdaddr_t*) btaddr_any
  21.     #define BDADDR_LOCAL (bdaddr_t*) btaddr_local
  22.      
  23.      
  24.     sdp_session_t* register_service(uint8_t rfcomm_channel)
  25.     {
  26.             // Adapted from http://www.btessentials.com/examples/bluez/sdp-register.c
  27.             //uint32_t svc_uuid_int[] = {   0x01110000, 0x00100000, 0x80000080, 0xFB349B5F };
  28.             uint32_t svc_uuid_int[] = {   0x00001101, 0x00001000, 0x80000080, 0x5F9B34FB };
  29.             const char *service_name = "Roto-Rooter Data Router";
  30.             const char *svc_dsc = "An experimental plumbing router";
  31.             const char *service_prov = "Roto-Rooter";
  32.      
  33.             uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid,
  34.                    svc_class_uuid;
  35.             sdp_list_t *l2cap_list = 0,
  36.                        *rfcomm_list = 0,
  37.                        *root_list = 0,
  38.                        *proto_list = 0,
  39.                        *access_proto_list = 0,
  40.                        *svc_class_list = 0,
  41.                        *profile_list = 0;
  42.             sdp_data_t *channel = 0;
  43.             sdp_profile_desc_t profile;
  44.             sdp_record_t record = { 0 };
  45.             sdp_session_t *session = 0;
  46.      
  47.             // set the general service ID
  48.             sdp_uuid128_create( &svc_uuid, &svc_uuid_int );
  49.             sdp_set_service_id( &record, svc_uuid );
  50.      
  51.             char str[256] = "";
  52.             sdp_uuid2strn(&svc_uuid, str, 256);
  53.             printf("Registering UUID %s\n", str);
  54.      
  55.             // set the service class
  56.             sdp_uuid16_create(&svc_class_uuid, SERIAL_PORT_SVCLASS_ID);
  57.             svc_class_list = sdp_list_append(0, &svc_class_uuid);
  58.             sdp_set_service_classes(&record, svc_class_list);
  59.      
  60.             // set the Bluetooth profile information
  61.             sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
  62.             profile.version = 0x0100;
  63.             profile_list = sdp_list_append(0, &profile);
  64.             sdp_set_profile_descs(&record, profile_list);
  65.      
  66.             // make the service record publicly browsable
  67.             sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
  68.             root_list = sdp_list_append(0, &root_uuid);
  69.             sdp_set_browse_groups( &record, root_list );
  70.      
  71.             // set l2cap information
  72.             sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  73.             l2cap_list = sdp_list_append( 0, &l2cap_uuid );
  74.             proto_list = sdp_list_append( 0, l2cap_list );
  75.      
  76.             // register the RFCOMM channel for RFCOMM sockets
  77.             sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
  78.             channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
  79.             rfcomm_list = sdp_list_append( 0, &rfcomm_uuid );
  80.             sdp_list_append( rfcomm_list, channel );
  81.             sdp_list_append( proto_list, rfcomm_list );
  82.      
  83.             access_proto_list = sdp_list_append( 0, proto_list );
  84.             sdp_set_access_protos( &record, access_proto_list );
  85.      
  86.             // set the name, provider, and description
  87.             sdp_set_info_attr(&record, service_name, service_prov, svc_dsc);
  88.      
  89.             // connect to the local SDP server, register the service record,
  90.             // and disconnect
  91.             //session = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
  92.             session = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
  93.             sdp_record_register(session, &record, 0);
  94.      
  95.             // cleanup
  96.             sdp_data_free( channel );
  97.             sdp_list_free( l2cap_list, 0 );
  98.             sdp_list_free( rfcomm_list, 0 );
  99.             sdp_list_free( root_list, 0 );
  100.             sdp_list_free( access_proto_list, 0 );
  101.             sdp_list_free( svc_class_list, 0 );
  102.             sdp_list_free( profile_list, 0 );
  103.      
  104.             return session;
  105.     }
  106.      
  107.     int main(int argc, char **argv)
  108.     {
  109.             int port = 3;
  110.             sdp_session_t* session = register_service(port);
  111.      
  112.      
  113.             struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
  114.             char buf[1024] = { 0 };
  115.             int s, client, bytes_read;
  116.             socklen_t opt = sizeof(rem_addr);
  117.             // allocate socket
  118.             s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  119.             printf("socket() returned %d\n", s);
  120.      
  121.             // bind socket to port 1 of the first available
  122.             // local bluetooth adapter
  123.             loc_addr.rc_family = AF_BLUETOOTH;
  124.             loc_addr.rc_bdaddr = *BDADDR_ANY;
  125.             loc_addr.rc_channel = (uint8_t) port;
  126.             int r;
  127.             r = bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
  128.             printf("bind() on channel %d returned %d\n", port, r);
  129.      
  130.             // put socket into listening mode
  131.             r = listen(s, 1);
  132.             printf("listen() returned %d\n", r);
  133.      
  134.             //sdpRegisterL2cap(port);
  135.      
  136.             // accept one connection
  137.             printf("calling accept()\n");
  138.             client = accept(s, (struct sockaddr *)&rem_addr, &opt);
  139.             printf("accept() returned %d\n", client);
  140.      
  141.             ba2str( &rem_addr.rc_bdaddr, buf );
  142.             fprintf(stderr, "accepted connection from %s\n", buf);
  143.             memset(buf, 0, sizeof(buf));
  144.      
  145.             // read data from the client
  146.             bytes_read = read(client, buf, sizeof(buf));
  147.             if( bytes_read > 0 ) {
  148.                     printf("received [%s]\n", buf);
  149.             }
  150.      
  151.             // close connection
  152.             close(client);
  153.             close(s);
  154.             sdp_close( session );
  155.      
  156.             return 0;
  157.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement