Debian Codegraph @

linux/l2cap.go

raw symbols SCIP
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:] // skip l2cap header
69 copy(b, d)
70 n := len(d)
71
72 // Keep receiving and reassemble continued l2cap segments
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, // Code (Connection Param Update)
87 0x02, // ID
88 0x08, 0x00, // DataLength
89 0x08, 0x00, // IntervalMin
90 0x18, 0x00, // IntervalMax
91 0x00, 0x00, // SlaveLatency
92 0xC8, 0x00} // TimeoutMultiplier
93 return c.write(0x05, b)
94 }
95
96 // write writes the l2cap payload to the controller.
97 // It first prepend the l2cap header (4-bytes), and diassemble the payload
98 // if it is larger than the HCI LE buffer size that the conntroller can support.
99 func (c *conn) write(cid int, b []byte) (int, error) {
100 flag := uint8(0) // ACL data continuation flag
101 tlen := len(b) // Total length of the l2cap payload
102
103 logger.Info("l2cap", "W", fmt.Sprintf("[% X]", b))
104 w := append(
105 []byte{
106 0, // packet type
107 0, 0, // attr
108 0, 0, // dlen
109 uint8(tlen), uint8(tlen >> 8), // l2cap header
110 uint8(cid), uint8(cid >> 8), // l2cap header
111 }, b...)
112
113 n := 4 + tlen // l2cap header + l2cap payload
114 for n > 0 {
115 dlen := n
116 if dlen > c.hci.bufSize {
117 dlen = c.hci.bufSize
118 }
119 w[0] = 0x02 // packetTypeACL
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 // make sure we don't send more buffers than the controller can handdle
126 c.hci.bufCnt <- struct{}{}
127
128 c.hci.d.Write(w[:5+dlen])
129 w = w[dlen:] // advance the pointer to the next segment, if any.
130 flag = 0x10 // the rest of iterations attr continued segments, if any.
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 // Close disconnects the connection by sending HCI disconnect command to the device.
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 // Signal Packets
172 // 0x00 Reserved Any
173 // 0x01 Command reject 0x0001 and 0x0005
174 // 0x02 Connection request 0x0001
175 // 0x03 Connection response 0x0001
176 // 0x04 Configure request 0x0001
177 // 0x05 Configure response 0x0001
178 // 0x06 Disconnection request 0x0001 and 0x0005
179 // 0x07 Disconnection response 0x0001 and 0x0005
180 // 0x08 Echo request 0x0001
181 // 0x09 Echo response 0x0001
182 // 0x0A Information request 0x0001
183 // 0x0B Information response 0x0001
184 // 0x0C Create Channel request 0x0001
185 // 0x0D Create Channel response 0x0001
186 // 0x0E Move Channel request 0x0001
187 // 0x0F Move Channel response 0x0001
188 // 0x10 Move Channel Confirmation 0x0001
189 // 0x11 Move Channel Confirmation response 0x0001
190 // 0x12 Connection Parameter Update request 0x0005
191 // 0x13 Connection Parameter Update response 0x0005
192 // 0x14 LE Credit Based Connection request 0x0005
193 // 0x15 LE Credit Based Connection response 0x0005
194 // 0x16 LE Flow Control Credit 0x0005
195 func (c *conn) handleSignal(a *aclData) error {
196 log.Printf("ignore l2cap signal:[ % X ]", a.b)
197 // FIXME: handle LE signaling channel (CID: 5)
198 return nil
199 }