Debian Codegraph @

linux/l2cap.go

raw symbols SCIP
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, // Code (Connection Param Update)
50 0x02, // ID
51 0x08, 0x00, // DataLength
52 0x08, 0x00, // IntervalMin
53 0x18, 0x00, // IntervalMax
54 0x00, 0x00, // SlaveLatency
55 0xC8, 0x00} // TimeoutMultiplier
56 return c.write(0x05, b)
57 }
58
59 // write writes the l2cap payload to the controller.
60 // It first prepend the l2cap header (4-bytes), and diassemble the payload
61 // if it is larger than the HCI LE buffer size that the conntroller can support.
62 func (c *conn) write(cid int, b []byte) (int, error) {
63 flag := uint8(0) // ACL data continuation flag
64 tlen := len(b) // Total length of the l2cap payload
65
66 // log.Printf("W: [ % X ]", b)
67 w := append(
68 []byte{
69 0, // packet type
70 0, 0, // attr
71 0, 0, // dlen
72 uint8(tlen), uint8(tlen >> 8), // l2cap header
73 uint8(cid), uint8(cid >> 8), // l2cap header
74 }, b...)
75
76 n := 4 + tlen // l2cap header + l2cap payload
77 for n > 0 {
78 dlen := n
79 if dlen > c.hci.bufSize {
80 dlen = c.hci.bufSize
81 }
82 w[0] = 0x02 // packetTypeACL
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 // make sure we don't send more buffers than the controller can handdle
89 c.hci.bufCnt <- struct{}{}
90
91 c.hci.d.Write(w[:5+dlen])
92 w = w[dlen:] // advance the pointer to the next segment, if any.
93 flag = 0x10 // the rest of iterations attr continued segments, if any.
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:] // skip l2cap header
110 copy(b, d)
111 n := len(d)
112
113 // Keep receiving and reassemble continued l2cap segments
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 // log.Printf("R: [ % X ]", b[:n])
122 return n, nil
123 }
124
125 func (c *conn) Write(b []byte) (int, error) {
126 return c.write(0x04, b)
127 }
128
129 // Close disconnects the connection by sending HCI disconnect command to the device.
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 // log.Printf("l2conn: 0x%04x already disconnected", hh)
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 // Signal Packets
147 // 0x00 Reserved Any
148 // 0x01 Command reject 0x0001 and 0x0005
149 // 0x02 Connection request 0x0001
150 // 0x03 Connection response 0x0001
151 // 0x04 Configure request 0x0001
152 // 0x05 Configure response 0x0001
153 // 0x06 Disconnection request 0x0001 and 0x0005
154 // 0x07 Disconnection response 0x0001 and 0x0005
155 // 0x08 Echo request 0x0001
156 // 0x09 Echo response 0x0001
157 // 0x0A Information request 0x0001
158 // 0x0B Information response 0x0001
159 // 0x0C Create Channel request 0x0001
160 // 0x0D Create Channel response 0x0001
161 // 0x0E Move Channel request 0x0001
162 // 0x0F Move Channel response 0x0001
163 // 0x10 Move Channel Confirmation 0x0001
164 // 0x11 Move Channel Confirmation response 0x0001
165 // 0x12 Connection Parameter Update request 0x0005
166 // 0x13 Connection Parameter Update response 0x0005
167 // 0x14 LE Credit Based Connection request 0x0005
168 // 0x15 LE Credit Based Connection response 0x0005
169 // 0x16 LE Flow Control Credit 0x0005
170 func (c *conn) handleSignal(a *aclData) error {
171 log.Printf("ignore l2cap signal:[ % X ]", a.b)
172 // FIXME: handle LE signaling channel (CID: 5)
173 return nil
174 }