| 1 |
package linux |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"log" |
| 7 |
|
| 8 |
"github.com/paypal/gatt/linux/cmd" |
| 9 |
) |
| 10 |
|
| 11 |
type aclData struct { |
| 12 |
attr uint16 |
| 13 |
flags uint8 |
| 14 |
dlen uint16 |
| 15 |
b []byte |
| 16 |
} |
| 17 |
|
| 18 |
func (a *aclData) unmarshal(b []byte) error { |
| 19 |
if len(b) < 4 { |
| 20 |
return fmt.Errorf("malformed acl packet") |
| 21 |
} |
| 22 |
attr := uint16(b[0]) | (uint16(b[1]&0x0f) << 8) |
| 23 |
flags := b[1] >> 4 |
| 24 |
dlen := uint16(b[2]) | (uint16(b[3]) << 8) |
| 25 |
if len(b) != 4+int(dlen) { |
| 26 |
return fmt.Errorf("malformed acl packet") |
| 27 |
} |
| 28 |
|
| 29 |
*a = aclData{attr: attr, flags: flags, dlen: dlen, b: b[4:]} |
| 30 |
return nil |
| 31 |
} |
| 32 |
|
| 33 |
type conn struct { |
| 34 |
hci *HCI |
| 35 |
attr uint16 |
| 36 |
aclc chan *aclData |
| 37 |
} |
| 38 |
|
| 39 |
func newConn(hci *HCI, hh uint16) *conn { |
| 40 |
return &conn{ |
| 41 |
hci: hci, |
| 42 |
attr: hh, |
| 43 |
aclc: make(chan *aclData), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func (c *conn) updateConnection() (int, error) { |
| 48 |
b := []byte{ |
| 49 |
0x12, |
| 50 |
0x02, |
| 51 |
0x08, 0x00, |
| 52 |
0x08, 0x00, |
| 53 |
0x18, 0x00, |
| 54 |
0x00, 0x00, |
| 55 |
0xC8, 0x00} |
| 56 |
return c.write(0x05, b) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
func (c *conn) write(cid int, b []byte) (int, error) { |
| 63 |
flag := uint8(0) |
| 64 |
tlen := len(b) |
| 65 |
|
| 66 |
|
| 67 |
w := append( |
| 68 |
[]byte{ |
| 69 |
0, |
| 70 |
0, 0, |
| 71 |
0, 0, |
| 72 |
uint8(tlen), uint8(tlen >> 8), |
| 73 |
uint8(cid), uint8(cid >> 8), |
| 74 |
}, b...) |
| 75 |
|
| 76 |
n := 4 + tlen |
| 77 |
for n > 0 { |
| 78 |
dlen := n |
| 79 |
if dlen > c.hci.bufSize { |
| 80 |
dlen = c.hci.bufSize |
| 81 |
} |
| 82 |
w[0] = 0x02 |
| 83 |
w[1] = uint8(c.attr) |
| 84 |
w[2] = uint8(c.attr>>8) | flag |
| 85 |
w[3] = uint8(dlen) |
| 86 |
w[4] = uint8(dlen >> 8) |
| 87 |
|
| 88 |
|
| 89 |
c.hci.bufCnt <- struct{}{} |
| 90 |
|
| 91 |
c.hci.d.Write(w[:5+dlen]) |
| 92 |
w = w[dlen:] |
| 93 |
flag = 0x10 |
| 94 |
n -= dlen |
| 95 |
} |
| 96 |
|
| 97 |
return len(b), nil |
| 98 |
} |
| 99 |
|
| 100 |
func (c *conn) Read(b []byte) (int, error) { |
| 101 |
a, ok := <-c.aclc |
| 102 |
if !ok { |
| 103 |
return 0, io.EOF |
| 104 |
} |
| 105 |
tlen := int(uint16(a.b[0]) | uint16(a.b[1])<<8) |
| 106 |
if tlen > len(b) { |
| 107 |
return 0, io.ErrShortBuffer |
| 108 |
} |
| 109 |
d := a.b[4:] |
| 110 |
copy(b, d) |
| 111 |
n := len(d) |
| 112 |
|
| 113 |
|
| 114 |
for n != tlen { |
| 115 |
if a, ok = <-c.aclc; !ok || (a.flags&0x1) == 0 { |
| 116 |
return n, io.ErrUnexpectedEOF |
| 117 |
} |
| 118 |
copy(b[n:], a.b) |
| 119 |
n += len(a.b) |
| 120 |
} |
| 121 |
|
| 122 |
return n, nil |
| 123 |
} |
| 124 |
|
| 125 |
func (c *conn) Write(b []byte) (int, error) { |
| 126 |
return c.write(0x04, b) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func (c *conn) Close() error { |
| 131 |
h := c.hci |
| 132 |
hh := c.attr |
| 133 |
h.connsmu.Lock() |
| 134 |
defer h.connsmu.Unlock() |
| 135 |
_, found := h.conns[hh] |
| 136 |
if !found { |
| 137 |
|
| 138 |
return nil |
| 139 |
} |
| 140 |
if err, _ := h.c.Send(cmd.Disconnect{ConnectionHandle: hh, Reason: 0x13}); err != nil { |
| 141 |
return fmt.Errorf("l2conn: failed to disconnect, %s", err) |
| 142 |
} |
| 143 |
return nil |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
func (c *conn) handleSignal(a *aclData) error { |
| 171 |
log.Printf("ignore l2cap signal:[ % X ]", a.b) |
| 172 |
|
| 173 |
return nil |
| 174 |
} |