Debian Codegraph @

central.go

raw symbols SCIP
1 package gatt
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "sync"
8 )
9
10 // Central is the interface that represent a remote central device.
11 type Central interface {
12 ID() string // ID returns platform specific ID of the remote central device.
13 Close() error // Close disconnects the connection.
14 MTU() int // MTU returns the current connection mtu.
15 }
16
17 type ResponseWriter interface {
18 // Write writes data to return as the characteristic value.
19 Write([]byte) (int, error)
20
21 // SetStatus reports the result of the read operation. See the Status* constants.
22 SetStatus(byte)
23 }
24
25 // responseWriter is the default implementation of ResponseWriter.
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 // A ReadHandler handles GATT read requests.
51 type ReadHandler interface {
52 ServeRead(resp ResponseWriter, req *ReadRequest)
53 }
54
55 // ReadHandlerFunc is an adapter to allow the use of
56 // ordinary functions as ReadHandlers. If f is a function
57 // with the appropriate signature, ReadHandlerFunc(f) is a
58 // ReadHandler that calls f.
59 type ReadHandlerFunc func(resp ResponseWriter, req *ReadRequest)
60
61 // ServeRead returns f(r, maxlen, offset).
62 func (f ReadHandlerFunc) ServeRead(resp ResponseWriter, req *ReadRequest) {
63 f(resp, req)
64 }
65
66 // A WriteHandler handles GATT write requests.
67 // Write and WriteNR requests are presented identically;
68 // the server will ensure that a response is sent if appropriate.
69 type WriteHandler interface {
70 ServeWrite(r Request, data []byte) (status byte)
71 }
72
73 // WriteHandlerFunc is an adapter to allow the use of
74 // ordinary functions as WriteHandlers. If f is a function
75 // with the appropriate signature, WriteHandlerFunc(f) is a
76 // WriteHandler that calls f.
77 type WriteHandlerFunc func(r Request, data []byte) byte
78
79 // ServeWrite returns f(r, data).
80 func (f WriteHandlerFunc) ServeWrite(r Request, data []byte) byte {
81 return f(r, data)
82 }
83
84 // A NotifyHandler handles GATT notification requests.
85 // Notifications can be sent using the provided notifier.
86 type NotifyHandler interface {
87 ServeNotify(r Request, n Notifier)
88 }
89
90 // NotifyHandlerFunc is an adapter to allow the use of
91 // ordinary functions as NotifyHandlers. If f is a function
92 // with the appropriate signature, NotifyHandlerFunc(f) is a
93 // NotifyHandler that calls f.
94 type NotifyHandlerFunc func(r Request, n Notifier)
95
96 // ServeNotify calls f(r, n).
97 func (f NotifyHandlerFunc) ServeNotify(r Request, n Notifier) {
98 f(r, n)
99 }
100
101 // A Notifier provides a means for a GATT server to send
102 // notifications about value changes to a connected device.
103 // Notifiers are provided by NotifyHandlers.
104 type Notifier interface {
105 // Write sends data to the central.
106 Write(data []byte) (int, error)
107
108 // Done reports whether the central has requested not to
109 // receive any more notifications with this notifier.
110 Done() bool
111
112 // Cap returns the maximum number of bytes that may be sent
113 // in a single notification.
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 &notifier{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 }