| 1 |
package gatt |
| 2 |
|
| 3 |
import "encoding/binary" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
func (w *l2capWriter) WriteByteFit(b byte) bool { |
| 72 |
return w.WriteFit([]byte{b}) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
func (w *l2capWriter) WriteUUIDFit(u UUID) bool { |
| 88 |
return w.WriteFit(u.b) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
} |