-
Notifications
You must be signed in to change notification settings - Fork 2
/
tinkerforge.go
291 lines (240 loc) · 5.58 KB
/
tinkerforge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Package tinkerforge implements the tinkerforge protocol
// Author: Tim Scheuermann (https://github.com/noxer)
package tinkerforge
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"sync"
"time"
)
// Handler to get callbacks
type Handler interface {
Handle(packet *Packet)
}
// respHandler for getting responses back
type respHandler struct {
c chan *Packet
t time.Duration
}
// Handles responses
func (r respHandler) Handle(p *Packet) {
// No timeout provided (don't!)
if r.t == 0 {
r.c <- p
return
}
timer := time.NewTimer(r.t)
select {
case r.c <- p:
timer.Stop()
case <-timer.C:
close(r.c)
}
}
// Tinkerforge interface
type Tinkerforge interface {
io.Closer
Handler(uid uint32, funcID uint8, handler Handler)
Send(packet *Packet) (*Packet, error)
}
// Tinkerforge structure
type tinkerforge struct {
conn io.ReadWriteCloser
seqNum chan byte
handlers map[handlerID]Handler
handlersMutex sync.RWMutex
sendQueue chan func()
done chan struct{}
wait sync.WaitGroup
Timeout time.Duration
}
type handlerID struct {
uid uint32
funcID uint8
seqNum uint8
}
var (
// ErrTimeout represents a timeout while waiting for a callback
ErrTimeout = errors.New("Timeout while waiting for callback")
)
// New creates a new tinkerforge client
func New(host string) (Tinkerforge, error) {
// Set standard host
if host == "" {
host = "localhost:4223"
}
// Resolve service address
addr, err := net.ResolveTCPAddr("tcp", host)
if err != nil {
return nil, err
}
// Connect to service
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return nil, err
}
// Build up structure
tf := &tinkerforge{
conn: conn,
seqNum: make(chan byte, 8),
handlers: make(map[handlerID]Handler),
sendQueue: make(chan func(), 8),
done: make(chan struct{}),
Timeout: 10 * time.Second,
}
// Start the go routines
tf.wait.Add(3)
go tf.seqNumGenerator() // Sequence number generator
go tf.sender() // Sender (queue for functions to send packets)
go tf.receiver() // Receiver
// Okay
return tf, nil
}
// Close closes the connection to the tinkerforge service
func (t *tinkerforge) Close() error {
// Close the channels
close(t.done)
close(t.sendQueue)
// Close the tcp connection
if err := t.conn.Close(); err != nil {
return err
}
// Wait for termination
t.wait.Wait()
return nil
}
// Send sends a new packet to the service and returns the answer (if an answer is expected)
func (t *tinkerforge) Send(p *Packet) (*Packet, error) {
var packets chan *Packet
errors := make(chan error, 1)
// Create response channel in case we need it
if p.ResponseExpected() {
packets = make(chan *Packet, 1)
}
f := func() {
// Generate sequence number
seqNum := <-t.seqNum
// Register callback for expected response (if any)
if p.ResponseExpected() {
t.handler(p.UID(), p.FunctionID(), seqNum, respHandler{c: packets, t: t.Timeout})
}
// Send packet
if err := p.Serialize(t.conn, seqNum); err != nil {
errors <- err
return
}
// Close the error channel
close(errors)
}
// Dispatch f
t.sendQueue <- f
// An error occurred
if err := <-errors; err != nil {
return nil, err
}
// Return depending of the expected response
if p.ResponseExpected() {
result, ok := <-packets
if ok {
return result, nil
}
// Timeout
return nil, ErrTimeout
}
return nil, nil
}
// Handler registers a new handler for a packet
func (t *tinkerforge) Handler(uid uint32, funcID uint8, h Handler) {
t.handler(uid, funcID, 0, h)
}
// handler registers any handler (internal)
func (t *tinkerforge) handler(uid uint32, funcID, seqNum uint8, h Handler) {
t.handlersMutex.Lock()
defer t.handlersMutex.Unlock()
// Make the handler removable
if h == nil {
delete(t.handlers, handlerIDFromParam(uid, funcID, seqNum))
return
}
// Add handler
t.handlers[handlerIDFromParam(uid, funcID, seqNum)] = h
}
// Sequence number generator
func (t *tinkerforge) seqNumGenerator() {
defer t.wait.Done()
num := byte(1)
for {
select {
case t.seqNum <- num:
case <-t.done:
return
}
num++
if num == 16 {
num = 1
}
}
}
// Sender executes the funktions in sendQueue
func (t *tinkerforge) sender() {
defer t.wait.Done()
// Execute all functions
for f := range t.sendQueue {
f()
}
}
// Receiver listens on the TCP connection and exeecutes the handlers accordingly
func (t *tinkerforge) receiver() {
defer t.wait.Done()
// Set up scanner
scanner := bufio.NewScanner(t.conn)
scanner.Split(scanPacket)
// Scan for packets
for scanner.Scan() {
// Parse the packet
p, err := readPacket(scanner.Bytes())
if err != nil {
fmt.Println(err)
continue
}
// Call the handler
t.handle(p)
// Remove handler if it was not a callback
if !p.Callback() {
t.handler(p.UID(), p.FunctionID(), p.SequenceNum(), nil)
}
}
}
// handle searches for a matching hander for p and executes it
func (t *tinkerforge) handle(p *Packet) {
t.handlersMutex.RLock()
var handler Handler
handler, ok := t.handlers[handlerIDFromPacket(p)]
if !ok {
// Maybe a wildcard?
handler = t.handlers[handlerIDFromParam(0, p.FunctionID(), p.SequenceNum())]
}
t.handlersMutex.RUnlock()
if handler != nil {
handler.Handle(p)
}
}
// handlerIdFromParam creates a new handler ID from the params
func handlerIDFromParam(uid uint32, funcID, seqNum uint8) handlerID {
return handlerID{
uid: uid,
funcID: funcID,
seqNum: seqNum,
}
}
// handlerIdFromPacket creates a new handler ID from packet p
func handlerIDFromPacket(p *Packet) handlerID {
return handlerID{
uid: p.UID(),
funcID: p.FunctionID(),
seqNum: p.SequenceNum(),
}
}