| 1 |
package linux |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"log" |
| 7 |
|
| 8 |
"github.com/bettercap/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: make([]byte, dlen)} |
| 30 |
if c := copy(a.b, b[4:]); c != int(dlen) { |
| 31 |
return fmt.Errorf("expected to copy %d bytes, copied %d", dlen, c) |
| 32 |
} |
| 33 |
return nil |
| 34 |
} |
| 35 |
|
| 36 |
type conn struct { |
| 37 |
hci *HCI |
| 38 |
attr uint16 |
| 39 |
aclc chan *aclData |
| 40 |
datac chan []byte |
| 41 |
} |
| 42 |
|
| 43 |
func newConn(hci *HCI, hh uint16) *conn { |
| 44 |
c := &conn{ |
| 45 |
hci: hci, |
| 46 |
attr: hh, |
| 47 |
aclc: make(chan *aclData), |
| 48 |
datac: make(chan []byte, 32), |
| 49 |
} |
| 50 |
go c.loop() |
| 51 |
return c |
| 52 |
} |
| 53 |
|
| 54 |
func (c *conn) loop() { |
| 55 |
defer close(c.datac) |
| 56 |
for a := range c.aclc { |
| 57 |
if len(a.b) < 4 { |
| 58 |
log.Printf("l2conn: short/corrupt packet, %v [% X]", a, a.b) |
| 59 |
return |
| 60 |
} |
| 61 |
cid := uint16(a.b[2]) | (uint16(a.b[3]) << 8) |
| 62 |
if cid == 5 { |
| 63 |
c.handleSignal(a) |
| 64 |
continue |
| 65 |
} |
| 66 |
b := make([]byte, 512) |
| 67 |
tlen := int(uint16(a.b[0]) | uint16(a.b[1])<<8) |
| 68 |
d := a.b[4:] |
| 69 |
copy(b, d) |
| 70 |
n := len(d) |
| 71 |
|
| 72 |
|
| 73 |
for n != tlen { |
| 74 |
a, ok := <-c.aclc |
| 75 |
if !ok || (a.flags&0x1) == 0 { |
| 76 |
return |
| 77 |
} |
| 78 |
n += copy(b[n:], a.b) |
| 79 |
} |
| 80 |
c.datac <- b[:n] |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func (c *conn) updateConnection() (int, error) { |
| 85 |
b := []byte{ |
| 86 |
0x12, |
| 87 |
0x02, |
| 88 |
0x08, 0x00, |
| 89 |
0x08, 0x00, |
| 90 |
0x18, 0x00, |
| 91 |
0x00, 0x00, |
| 92 |
0xC8, 0x00} |
| 93 |
return c.write(0x05, b) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
func (c *conn) write(cid int, b []byte) (int, error) { |
| 100 |
flag := uint8(0) |
| 101 |
tlen := len(b) |
| 102 |
|
| 103 |
logger.Info("l2cap", "W", fmt.Sprintf("[% X]", b)) |
| 104 |
w := append( |
| 105 |
[]byte{ |
| 106 |
0, |
| 107 |
0, 0, |
| 108 |
0, 0, |
| 109 |
uint8(tlen), uint8(tlen >> 8), |
| 110 |
uint8(cid), uint8(cid >> 8), |
| 111 |
}, b...) |
| 112 |
|
| 113 |
n := 4 + tlen |
| 114 |
for n > 0 { |
| 115 |
dlen := n |
| 116 |
if dlen > c.hci.bufSize { |
| 117 |
dlen = c.hci.bufSize |
| 118 |
} |
| 119 |
w[0] = 0x02 |
| 120 |
w[1] = uint8(c.attr) |
| 121 |
w[2] = uint8(c.attr>>8) | flag |
| 122 |
w[3] = uint8(dlen) |
| 123 |
w[4] = uint8(dlen >> 8) |
| 124 |
|
| 125 |
|
| 126 |
c.hci.bufCnt <- struct{}{} |
| 127 |
|
| 128 |
c.hci.d.Write(w[:5+dlen]) |
| 129 |
w = w[dlen:] |
| 130 |
flag = 0x10 |
| 131 |
n -= dlen |
| 132 |
} |
| 133 |
|
| 134 |
return len(b), nil |
| 135 |
} |
| 136 |
|
| 137 |
func (c *conn) Read(b []byte) (int, error) { |
| 138 |
d, ok := <-c.datac |
| 139 |
if !ok { |
| 140 |
return 0, io.EOF |
| 141 |
} |
| 142 |
if len(d) > len(b) { |
| 143 |
return copy(b, d), io.ErrShortBuffer |
| 144 |
} |
| 145 |
logger.Info("l2cap", "R", fmt.Sprintf("[% X]", d)) |
| 146 |
n := copy(b, d) |
| 147 |
return n, nil |
| 148 |
} |
| 149 |
|
| 150 |
func (c *conn) Write(b []byte) (int, error) { |
| 151 |
return c.write(0x04, b) |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
func (c *conn) Close() error { |
| 156 |
h := c.hci |
| 157 |
hh := c.attr |
| 158 |
h.connsmu.Lock() |
| 159 |
defer h.connsmu.Unlock() |
| 160 |
_, found := h.conns[hh] |
| 161 |
if !found { |
| 162 |
log.Printf("l2conn: 0x%04x already disconnected", hh) |
| 163 |
return nil |
| 164 |
} |
| 165 |
if err, _ := h.c.Send(cmd.Disconnect{ConnectionHandle: hh, Reason: 0x13}); err != nil { |
| 166 |
return fmt.Errorf("l2conn: failed to disconnect, %s", err) |
| 167 |
} |
| 168 |
return nil |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
func (c *conn) handleSignal(a *aclData) error { |
| 196 |
log.Printf("ignore l2cap signal:[ % X ]", a.b) |
| 197 |
|
| 198 |
return nil |
| 199 |
} |