Debian Codegraph @

l2cap_writer_linux.go

raw symbols SCIP
1 package gatt
2
3 import "encoding/binary"
4
5 // l2capWriter helps create l2cap responses.
6 // It is not meant to be used with large writes.
7 // TODO: benchmark the number of allocs here.
8 // Reduce by letting WriteByteFit, WriteUint16Fit, etc.
9 // extend b/chunk and write into it directly.
10 type l2capWriter struct {
11 mtu int
12 b []byte
13 chunk []byte
14 chunked bool
15 }
16
17 func newL2capWriter(mtu uint16) *l2capWriter {
18 return &l2capWriter{mtu: int(mtu), b: make([]byte, 0, mtu)}
19 }
20
21 // Chunk starts writing a new chunk. This chunk
22 // is not committed until Commit is called.
23 // Chunk panics if another chunk has already been
24 // started and not committed.
25 func (w *l2capWriter) Chunk() {
26 if w.chunked {
27 panic("l2capWriter: chunk called twice without committing")
28 }
29 w.chunked = true
30 if w.chunk == nil {
31 w.chunk = make([]byte, 0, w.mtu)
32 }
33 }
34
35 // Commit writes the current chunk and reports whether the
36 // write succeeded. The write succeeds iff there is enough room.
37 // Commit panics if no chunk has been started.
38 func (w *l2capWriter) Commit() bool {
39 if !w.chunked {
40 panic("l2capWriter: commit without starting a chunk")
41 }
42 var success bool
43 if len(w.b)+len(w.chunk) <= w.mtu {
44 success = true
45 w.b = append(w.b, w.chunk...)
46 }
47 w.chunk = w.chunk[:0]
48 w.chunked = false
49 return success
50 }
51
52 // CommitFit writes as much of the current chunk as possible,
53 // truncating as needed.
54 // CommitFit panics if no chunk has been started.
55 func (w *l2capWriter) CommitFit() {
56 if !w.chunked {
57 panic("l2capWriter: CommitFit without starting a chunk")
58 }
59 writeable := w.mtu - len(w.b)
60 if writeable > len(w.chunk) {
61 writeable = len(w.chunk)
62 }
63 w.b = append(w.b, w.chunk[:writeable]...)
64 w.chunk = w.chunk[:0]
65 w.chunked = false
66 }
67
68 // WriteByteFit writes b.
69 // It reports whether the write succeeded,
70 // using the criteria of WriteFit.
71 func (w *l2capWriter) WriteByteFit(b byte) bool {
72 return w.WriteFit([]byte{b})
73 }
74
75 // WriteUint16Fit writes v using BLE (LittleEndian) encoding.
76 // It reports whether the write succeeded, using the
77 // criteria of WriteFit.
78 func (w *l2capWriter) WriteUint16Fit(v uint16) bool {
79 b := make([]byte, 2)
80 binary.LittleEndian.PutUint16(b, v)
81 return w.WriteFit(b)
82 }
83
84 // WriteUUIDFit writes uuid using BLE (reversed) encoding.
85 // It reports whether the write succeeded, using the
86 // criteria of WriteFit.
87 func (w *l2capWriter) WriteUUIDFit(u UUID) bool {
88 return w.WriteFit(u.b)
89 }
90
91 // Writeable returns the number of bytes from b
92 // that would be written if pad bytes were written,
93 // then as much of b as fits were written. When
94 // writing to a chunk, any amount of bytes may be
95 // written.
96 func (w *l2capWriter) Writeable(pad int, b []byte) int {
97 if w.chunked {
98 return len(b)
99 }
100 avail := w.mtu - len(w.b) - pad
101 if avail > len(b) {
102 return len(b)
103 }
104 if avail < 0 {
105 return 0
106 }
107 return avail
108 }
109
110 // WriteFit writes as much of b as fits.
111 // It reports whether the write succeeded without
112 // truncation. A write succeeds without truncation
113 // iff a chunk write is in progress or the entire
114 // contents were written (without exceeding the mtu).
115 func (w *l2capWriter) WriteFit(b []byte) bool {
116 if w.chunked {
117 w.chunk = append(w.chunk, b...)
118 return true
119 }
120 avail := w.mtu - len(w.b)
121 if avail >= len(b) {
122 w.b = append(w.b, b...)
123 return true
124 }
125 w.b = append(w.b, b[:avail]...)
126 return false
127 }
128
129 // ChunkSeek discards the first offset bytes from the
130 // current chunk. It reports whether there were at least
131 // offset bytes available to discard.
132 // It panics if a chunked write is not in progress.
133 func (w *l2capWriter) ChunkSeek(offset uint16) bool {
134 if !w.chunked {
135 panic("l2capWriter: ChunkSeek requested without chunked write in progress")
136 }
137 if len(w.chunk) < int(offset) {
138 w.chunk = w.chunk[:0]
139 return false
140 }
141 w.chunk = w.chunk[offset:]
142 return true
143 }
144
145 // Bytes returns the written bytes.
146 // It will panic if a chunked write
147 // is in progress.
148 // It is meant to be used when writing
149 // is completed. It does not return a copy.
150 // Don't abuse this, it's not worth it.
151 func (w *l2capWriter) Bytes() []byte {
152 if w.chunked {
153 panic("l2capWriter: Bytes requested while chunked write in progress")
154 }
155 return w.b
156 }