-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
357 lines (321 loc) · 7.09 KB
/
client.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package gpsd
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"net"
"sync"
)
// DefaultAddress is the default address gpsd listens to.
const DefaultAddress = ":2947"
// Option is a client configuration option.
type Option func(c *Client)
// WithConn sets client connection.
func WithConn(conn net.Conn) Option {
return func(c *Client) {
c.conn = conn
}
}
// WithLogger sets logger.
func WithLogger(logger Logger) Option {
return func(c *Client) {
c.logger = logger
}
}
// WithChannel changes reports channel for adjusting buffer size, default is 10.
func WithChannel(ch chan Report) Option {
return func(c *Client) {
c.evch = ch
}
}
// New creates a new gpsd client.
func New(opts ...Option) (*Client, error) {
c := &Client{
evch: make(chan Report, 1),
done: make(chan struct{}),
logger: newStdLogger(),
}
for _, opt := range opts {
opt(c)
}
if c.conn == nil {
return nil, errors.New("conn is nil")
}
go c.rx()
return c, nil
}
// Dial dials the named address and returns a gpsd client for the connection.
func Dial(addr string, opts ...Option) (*Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return New(append(opts, WithConn(conn))...)
}
// Client is a gpsd client implementation.
//
// https://gpsd.gitlab.io/gpsd/gpsd_json.html
// https://gpsd.gitlab.io/gpsd/client-howto.html
type Client struct {
mu sync.Mutex
err error
conn net.Conn
evch chan Report
done chan struct{}
logger Logger
}
func (c *Client) rx() {
defer close(c.evch)
r := bufio.NewReader(c.conn)
var b []byte
var err error
var v Report
for {
b, err = r.ReadSlice('\n')
if err != nil {
_ = c.close(err)
return
}
// trim '\r' if present
if b[len(b)-1] == '\r' {
b = b[:len(b)-2]
}
if b[0] == '{' { // json
c.debugf("RX %s", b)
v, err = unmarshal(class(b), b)
if err != nil {
c.errorf("unmarshal error: %q", err)
continue
}
} else {
c.debugf("RX [RAW]")
v = RAW(b) // raw data
}
select {
case c.evch <- v:
case <-c.done:
return
}
}
}
// C returns a channel where report structures can be read from.
func (c *Client) C() <-chan Report {
return c.evch
}
// Done reports where the client is stopped.
func (c *Client) Done() <-chan struct{} {
return c.done
}
// Err returns client errors, makes sense to call it only when C is closed.
func (c *Client) Err() error {
return c.err
}
// WatchFlag configures watch parameters.
type WatchFlag uint32
const (
// WATCH_ENABLE enable streaming.
WATCH_ENABLE = 0x0001
// WATCH_DISABLE disable watching.
WATCH_DISABLE = 0x0002
// WATCH_JSON JSON output.
WATCH_JSON = 0x0010
// WATCH_NMEA output in NMEA.
WATCH_NMEA = 0x0020
// WATCH_RARE output of packets in hex.
WATCH_RARE = 0x0040
// WATCH_RAW output of raw packets.
WATCH_RAW = 0x0080
// WATCH_SCALED scale output to floats.
WATCH_SCALED = 0x0100
// WATCH_TIMING timing information.
WATCH_TIMING = 0x0200
// WATCH_DEVICE watch specific device.
WATCH_DEVICE = 0x0800
// WATCH_SPLIT24 split AIS Type 24s.
WATCH_SPLIT24 = 0x1000
// WATCH_PPS enable PPS JSON.
WATCH_PPS = 0x2000
)
// Stream changes watch policy the second argument can only
// be a device path and it only implies WATCH_DEVICE flag.
func (c *Client) Stream(flags WatchFlag, devpath string) error {
// cannot use WATCH struct unless all its attributes are pointers
// that will force users to dereference each value and check if it's not a nil,
// otherwise json.Marshall will discard zero values because of the omitempty option
// or will send zero values every time if we get rid of it.
s := "?WATCH={"
if flags&WATCH_DISABLE != 0 {
s += `"enable":false`
if flags&WATCH_JSON != 0 {
s += `,"json":false`
}
if flags&WATCH_NMEA != 0 {
s += `,"nmea":false`
}
if flags&WATCH_RARE != 0 {
s += `,"raw":1`
}
if flags&WATCH_RAW != 0 {
s += `,"raw":2`
}
if flags&WATCH_SCALED != 0 {
s += `,"scaled":false`
}
if flags&WATCH_TIMING != 0 {
s += `,"scaled":false`
}
if flags&WATCH_SPLIT24 != 0 {
s += `,"split24":false`
}
if flags&WATCH_PPS != 0 {
s += `,"pps":false`
}
} else { // flags&WATCH_ENABLE
s += `"enable":true`
if flags&WATCH_JSON != 0 {
s += `,"json":true`
}
if flags&WATCH_NMEA != 0 {
s += `,"nmea":true`
}
if flags&WATCH_RARE != 0 {
s += `,"raw":1`
}
if flags&WATCH_RAW != 0 {
s += `,"raw":2`
}
if flags&WATCH_SCALED != 0 {
s += `,"scaled":true`
}
if flags&WATCH_TIMING != 0 {
s += `,"scaled":true`
}
if flags&WATCH_SPLIT24 != 0 {
s += `,"split24":true`
}
if flags&WATCH_PPS != 0 {
s += `,"pps":true`
}
if flags&WATCH_DEVICE != 0 {
s += fmt.Sprintf(`,"device":%q`, devpath)
}
}
s += "}"
return c.Send([]byte(s))
}
// Send sends the given raw data to gpsd,
// use it only if you know what you're doing.
//
// Do not use marshalled report structs here, because they're
// supposed to be used to receive data from the daemon only.
func (c *Client) Send(b []byte) error {
c.mu.Lock()
defer c.mu.Unlock()
if c.err != nil {
return c.err
}
if _, err := c.conn.Write(b); err != nil {
_ = c.close(err)
return err
}
c.debugf("TX %s", b)
return nil
}
func (c *Client) errorf(format string, v ...interface{}) {
if c.logger != nil {
c.logger.Errorf(format, v...)
}
}
func (c *Client) debugf(format string, v ...interface{}) {
if c.logger != nil {
c.logger.Debugf(format, v...)
}
}
// ErrClosed signalizes that client is closed.
var ErrClosed = errors.New("closed")
func (c *Client) close(err error) error {
c.mu.Lock()
defer c.mu.Unlock()
select {
case <-c.done:
return nil
default:
}
c.err = err
close(c.done)
if c.conn != nil {
return c.conn.Close()
}
return nil
}
// Close closes underlying gpsd connection.
func (c *Client) Close() error {
return c.close(ErrClosed)
}
// unmarshal unmarshals the given json into a report corresponding to the class attribute.
func unmarshal(cls string, b []byte) (Report, error) {
var v Report
switch cls {
case "TPV":
v = &TPV{}
case "SKY":
v = &SKY{}
case "GST":
v = &GST{}
case "ATT":
v = &ATT{}
case "VERSION":
v = &VERSION{}
case "DEVICES":
v = &DEVICES{}
case "WATCH":
v = &WATCH{}
case "POLL":
v = &POLL{}
case "TOFF":
v = &TOFF{}
case "PPS":
v = &PPS{}
case "OSC":
v = &OSC{}
case "DEVICE":
v = &DEVICE{}
case "ERROR":
v = &ERROR{}
default:
return nil, fmt.Errorf("unknown class %q", cls)
}
if err := json.Unmarshal(b, v); err != nil {
return nil, err
}
return v, nil
}
// class detects the given json report class name or returns an empty string when it fails.
// It's needed to avoid parsing json into map[string]interface{} for performance reasons.
func class(b []byte) string {
const prefix = `"class":"`
for i, j := 0, 0; i < len(b); i++ {
// skip whitespace chars
if b[i] == ' ' || b[i] == '\r' || b[i] == '\n' || b[i] == '\t' {
continue
}
if b[i] == prefix[j] {
// prefix detected
if len(prefix) == j+1 {
for k := i + 1; k < len(b); k++ {
// end of name reached
if b[k] == '"' {
return string(b[i+1 : k])
}
}
// '"' is not found
break
}
j++
} else {
j = 0
}
}
return ""
}