| 1 |
package gatt |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"sync" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type Central interface { |
| 12 |
ID() string |
| 13 |
Close() error |
| 14 |
MTU() int |
| 15 |
} |
| 16 |
|
| 17 |
type ResponseWriter interface { |
| 18 |
|
| 19 |
Write([]byte) (int, error) |
| 20 |
|
| 21 |
|
| 22 |
SetStatus(byte) |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
type responseWriter struct { |
| 27 |
capacity int |
| 28 |
buf *bytes.Buffer |
| 29 |
status byte |
| 30 |
} |
| 31 |
|
| 32 |
func newResponseWriter(c int) *responseWriter { |
| 33 |
return &responseWriter{ |
| 34 |
capacity: c, |
| 35 |
buf: new(bytes.Buffer), |
| 36 |
status: StatusSuccess, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func (w *responseWriter) Write(b []byte) (int, error) { |
| 41 |
if avail := w.capacity - w.buf.Len(); avail < len(b) { |
| 42 |
return 0, fmt.Errorf("requested write %d bytes, %d available", len(b), avail) |
| 43 |
} |
| 44 |
return w.buf.Write(b) |
| 45 |
} |
| 46 |
|
| 47 |
func (w *responseWriter) SetStatus(status byte) { w.status = status } |
| 48 |
func (w *responseWriter) bytes() []byte { return w.buf.Bytes() } |
| 49 |
|
| 50 |
|
| 51 |
type ReadHandler interface { |
| 52 |
ServeRead(resp ResponseWriter, req *ReadRequest) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
type ReadHandlerFunc func(resp ResponseWriter, req *ReadRequest) |
| 60 |
|
| 61 |
|
| 62 |
func (f ReadHandlerFunc) ServeRead(resp ResponseWriter, req *ReadRequest) { |
| 63 |
f(resp, req) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
type WriteHandler interface { |
| 70 |
ServeWrite(r Request, data []byte) (status byte) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
type WriteHandlerFunc func(r Request, data []byte) byte |
| 78 |
|
| 79 |
|
| 80 |
func (f WriteHandlerFunc) ServeWrite(r Request, data []byte) byte { |
| 81 |
return f(r, data) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
type NotifyHandler interface { |
| 87 |
ServeNotify(r Request, n Notifier) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
type NotifyHandlerFunc func(r Request, n Notifier) |
| 95 |
|
| 96 |
|
| 97 |
func (f NotifyHandlerFunc) ServeNotify(r Request, n Notifier) { |
| 98 |
f(r, n) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
type Notifier interface { |
| 105 |
|
| 106 |
Write(data []byte) (int, error) |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
Done() bool |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
Cap() int |
| 115 |
} |
| 116 |
|
| 117 |
type notifier struct { |
| 118 |
central *central |
| 119 |
a *attr |
| 120 |
maxlen int |
| 121 |
donemu sync.RWMutex |
| 122 |
done bool |
| 123 |
} |
| 124 |
|
| 125 |
func newNotifier(c *central, a *attr, maxlen int) *notifier { |
| 126 |
return ¬ifier{central: c, a: a, maxlen: maxlen} |
| 127 |
} |
| 128 |
|
| 129 |
func (n *notifier) Write(b []byte) (int, error) { |
| 130 |
n.donemu.RLock() |
| 131 |
defer n.donemu.RUnlock() |
| 132 |
if n.done { |
| 133 |
return 0, errors.New("central stopped notifications") |
| 134 |
} |
| 135 |
return n.central.sendNotification(n.a, b) |
| 136 |
} |
| 137 |
|
| 138 |
func (n *notifier) Cap() int { |
| 139 |
return n.maxlen |
| 140 |
} |
| 141 |
|
| 142 |
func (n *notifier) Done() bool { |
| 143 |
n.donemu.RLock() |
| 144 |
defer n.donemu.RUnlock() |
| 145 |
return n.done |
| 146 |
} |
| 147 |
|
| 148 |
func (n *notifier) stop() { |
| 149 |
n.donemu.Lock() |
| 150 |
n.done = true |
| 151 |
n.donemu.Unlock() |
| 152 |
} |