gokanmadan / pybluez

Automatically exported from code.google.com/p/pybluez
GNU General Public License v2.0
0 stars 0 forks source link

Outdated handling of L2CAP options #32

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Taking a look into code that sets MTU for L2CAP options, I could see that 
L2CAP_OPTIONS 
structure handling is outdated. I has 12 bytes in current version, not 7. The 
MTUs are still set 
correctly because they are at the beginning of structure, but FCS and mode have 
exchanged 
positions.

The following code is something I have developed to work around:

options_len = 12
pos = ["omtu", "imtu", "flush_to", "mode", "fcs", "max_tx", "txwin_size"]
mask = "HHHBBBH"

def get_options(sock):
        s = sock.getsockopt(SOL_L2CAP, L2CAP_OPTIONS, options_len)
        options = struct.unpack(mask, s)
        return list(options)

def set_options(sock, options):
        s = struct.pack(mask, *options)
        sock.setsockopt(SOL_L2CAP, L2CAP_OPTIONS, s)

def set_ertm(sock):
        options = get_options(sock)
        options[i_fcs] = 1
        options[i_mode] = L2CAP_MODE_ERTM
        set_options(sock, options)

def set_streaming(sock):
        options = get_options(sock)
        options[i_fcs] = 1
        options[i_mode] = L2CAP_MODE_STREAMING
        set_options(sock, options)

def set_mtu(sock, mtu):
        options = get_options(sock)
        options[i_omtu] = options[i_imtu] = mtu
        set_options(sock, options)

Current struct in BlueZ/lib/l2cap.h:
/* L2CAP socket options */

#define L2CAP_OPTIONS   0x01
struct l2cap_options {
        uint16_t        omtu;
        uint16_t        imtu;
        uint16_t        flush_to;
        uint8_t         mode;
        uint8_t         fcs;
        uint8_t         max_tx;
        uint16_t        txwin_size;
};

Attached is a tentative patch. Best fix would be to move this into a .c file, 
because then any 
change in struct would not affect Python code (or cause a compilation error, 
then it's fixed or 
#ifdef'ed)

Original issue reported on code.google.com by elvis.pf...@gmail.com on 28 May 2010 at 9:36

Attachments:

GoogleCodeExporter commented 8 years ago
Commit #33 fixes this. Not the perfect fix, but should suffice for quite some 
time, and adds to handy functions 
to manipulate L2CAP options (useful for the folks that want to use the newest 
ERTM and streaming modes).

Original comment by elvis.pf...@gmail.com on 31 May 2010 at 5:41