【转】http://people.csail.mit.edu/albert/bluez-intro/x559.html


An Introduction to Bluetooth Programming

Prev Chapter 4. Bluetooth programming in C with BlueZ Next

4.3. L2CAP sockets



As with RFCOMM, L2CAP communications are structured around socket programming. Example 4-4 and Example 4-5 demonstrate how to establish an L2CAP channel and transmit a short string of data. For simplicity, the client is hard-coded to connect to ``01:23:45:67:89:AB".


Example 4-4. l2cap-server.c



#include

#include

#include

#include

#include


int main(int argc, char **argv)

{

    struct sockaddr_l2 loc_addr = { 0 }, rem_addr = { 0 };

    char buf[1024] = { 0 };

    int s, client, bytes_read;

    socklen_t opt = sizeof(rem_addr);


    // allocate socket

    s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);


    // bind socket to port 0x1001 of the first available 

    // bluetooth adapter

    loc_addr.l2_family = AF_BLUETOOTH;

    loc_addr.l2_bdaddr = *BDADDR_ANY;

    loc_addr.l2_psm = htobs(0x1001);


    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));


    // put socket into listening mode

    listen(s, 1);


    // accept one connection

    client = accept(s, (struct sockaddr *)&rem_addr, &opt);


    ba2str( &rem_addr.l2_bdaddr, buf );

    fprintf(stderr, "accepted connection from %s\n", buf);


    memset(buf, 0, sizeof(buf));


    // read data from the client

    bytes_read = read(client, buf, sizeof(buf));

    if( bytes_read > 0 ) {

        printf("received [%s]\n", buf);

    }


    // close connection

    close(client);

    close(s);

}


Example 4-5. l2cap-client.c



#include

#include

#include

#include

#include


int main(int argc, char **argv)

{

    struct sockaddr_l2 addr = { 0 };

    int s, status;

    char *message = "hello!";

    char dest[18] = "01:23:45:67:89:AB";


    if(argc < 2)

    {

        fprintf(stderr, "usage: %s \n", argv[0]);

        exit(2);

    }


    strncpy(dest, argv[1], 18);


    // allocate a socket

    s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);


    // set the connection parameters (who to connect to)

    addr.l2_family = AF_BLUETOOTH;

    addr.l2_psm = htobs(0x1001);

    str2ba( dest, &addr.l2_bdaddr );


    // connect to server

    status = connect(s, (struct sockaddr *)&addr, sizeof(addr));


    // send a message

    if( status == 0 ) {

        status = write(s, "hello!", 6);

    }


    if( status < 0 ) perror("uh oh");


    close(s);

}



For simple usage scenarios, the only differences are the socket type specified, the protocol family, and the addressing structure. By default, L2CAP connections provide reliable datagram-oriented connections with packets delivered in order, so the socket type is SOCK_SEQPACKET, and the protocol is BTPROTO_L2CAP. The addressing structure struct sockaddr_l2 differs slightly from the RFCOMM addressing structure.



struct sockaddr_l2 {

    sa_family_t     l2_family;

    unsigned short  l2_psm;

    bdaddr_t        l2_bdaddr;

};


The l2_psm field specifies the L2CAP port number to use. Since it is a multibyte unsigned integer, byte ordering is significant. The htobs function, described earlier, is used here to convert numbers to Bluetooth byte order.


4.3.1. Maximum Transmission Unit



Occasionally, an application may need to adjust the maximum transmission unit (MTU) for an L2CAP connection and set it to something other than the default of 672 bytes. In BlueZ, this is done with the getsockopt and setsockopt functions.



struct l2cap_options {

    uint16_t    omtu;

    uint16_t    imtu;

    uint16_t    flush_to;

    uint8_t     mode;

};


int set_l2cap_mtu( int sock, uint16_t mtu ) {

struct l2cap_options opts;

    int optlen = sizeof(opts), err;

    err = getsockopt( s, SOL_L2CAP, L2CAP_OPTIONS, &opts, &optlen );

    if( ! err ) {

        opts.omtu = opts.imtu = mtu;

        err = setsockopt( s, SOL_L2CAP, L2CAP_OPTIONS, &opts, optlen );

    }

    return err;

};



The omtu and imtu fields of the struct l2cap_options are used to specify the outgoing MTU and incoming MTU, respectively. The other two fields are currently unused and reserved for future use. To adjust the connection-wide MTU, both clients must adjust their outgoing and incoming MTUs. Bluetooth allows the MTU to range from a minimum of 48 bytes to a maximum of 65,535 bytes.


4.3.2. Unreliable sockets



It is slightly misleading to say that L2CAP sockets are reliable by default. Multiple L2CAP and RFCOMM connections between two devices are actually logical connections multiplexed on a single, lower level connection [1] established between them. The only way to adjust delivery semantics is to adjust them for the lower level connection, which in turn affects all L2CAP and RFCOMM connections between the two devices.



As we delve deeper into the more complex aspects of Bluetooth programming, the interface becomes a little harder to manage. Unfortunately, BlueZ does not provide an easy way to change the packet timeout for a connection. A handle to the underlying connection is first needed to make this change, but the only way to obtain a handle to the underlying connection is to query the microcontroller on the local Bluetooth adapter. Once the connection handle has been determined, a command can be issued to the microcontroller instructing it to make the appropriate adjustments. Example 4-6 shows how to do this.


Example 4-6. set-flush-to.c



#include

#include

#include

#include

#include

#include

#include

#include


int set_flush_timeout(bdaddr_t *ba, int timeout)

{

    int err = 0, dd;

    struct hci_conn_info_req *cr = 0;

    struct hci_request rq = { 0 };


    struct {

        uint16_t handle;

        uint16_t flush_timeout;

    } cmd_param;


    struct {

        uint8_t  status;

        uint16_t handle;

    } cmd_response;


    // find the connection handle to the specified bluetooth device

    cr = (struct hci_conn_info_req*) malloc(

            sizeof(struct hci_conn_info_req) + 

            sizeof(struct hci_conn_info));

    bacpy( &cr->bdaddr, ba );

    cr->type = ACL_LINK;

    dd = hci_open_dev( hci_get_route( &cr->bdaddr ) );

    if( dd < 0 ) {

        err = dd;

        goto cleanup;

    }

    err = ioctl(dd, HCIGETCONNINFO, (unsigned long) cr );

    if( err ) goto cleanup;


    // build a command packet to send to the bluetooth microcontroller

    cmd_param.handle = cr->conn_info->handle;

    cmd_param.flush_timeout = htobs(timeout);

    rq.ogf = OGF_HOST_CTL;

    rq.ocf = 0x28;

    rq.cparam = &cmd_param;

    rq.clen = sizeof(cmd_param);

    rq.rparam = &cmd_response;

    rq.rlen = sizeof(cmd_response);

    rq.event = EVT_CMD_COMPLETE;


    // send the command and wait for the response

    err = hci_send_req( dd, &rq, 0 );

    if( err ) goto cleanup;


    if( cmd_response.status ) {

        err = -1;

        errno = bt_error(cmd_response.status);

    }


cleanup:

    free(cr);

    if( dd >= 0) close(dd);

    return err;

}



On success, the packet timeout for the low level connection to the specified device is set to timeout * 0.625 milliseconds. A timeout of 0 is used to indicate infinity, and is how to revert back to a reliable connection. The bulk of this function is comprised of code to construct the command packets and response packets used in communicating with the Bluetooth controller. The Bluetooth Specification defines the structure of these packets and the magic number 0x28. In most cases, BlueZ provides convenience functions to construct the packets, send them, and wait for the response. Setting the packet timeout, however, seems to be so rarely used that no convenience function for it currently exists.


Notes


[1]


Bluetooth terminology refers to this as the ACL connection


Prev Home Next

RFCOMM sockets Up Service Discovery Protocol