/
main.go
464 lines (423 loc) · 11.2 KB
/
main.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package main
// TODO:
// * when settings gain, pick gain closest to an available value as returned by GetRunerGains
// * fix setting (or maybe getting) negative gains
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"github.com/samuel/go-dsp/dsp"
)
const (
debug = true
defaultPort = 28888
eol = "\n"
samplesPerPacket = 4096
packetsPerBuffer = 4
sampleBufferSize = samplesPerPacket * packetsPerBuffer
defaultCenterFreq = 144.1e6
defaultSampleRate = 1000000
headerSize = 4
nBuffers = 32
// deviceCacheUpdateInterval = time.Second * 60
flagNone = 0x00
flagHardwareOverrun = 0x01 // Used at hardware interface
flagNetworkOverrun = 0x02 // Used at client (network too slow)
flagBufferOverrun = 0x04 // Used at client (client consumer too slow)
flagEmptyPayload = 0x08 // Reserved
flagStreamStart = 0x10 // Used for first packet of newly started stream
flagStreamEnd = 0x20 // Reserved (TO DO: Server sends BF_EMPTY_PAYLOAD | BF_STREAM_END)
flagBufferUnderrun = 0x40 // Used at hardware interface
flagHardwareTimeout = 0x80 // Used at hardware interface
cmdAntenna = "ANTENNA"
cmdDest = "DEST"
cmdDevice = "DEVICE"
cmdFreq = "FREQ"
cmdGain = "GAIN"
cmdGo = "GO"
cmdRate = "RATE"
cmdStop = "STOP"
resOK = "OK"
resFail = "FAIL"
resUnknown = "UNKNOWN"
resDevice = "DEVICE"
)
var (
flagCPUProfile = flag.Bool("profile.cpu", false, "Enable CPU profiling")
)
var (
ErrNoDestination = errors.New("no destination")
)
type client struct {
conn net.Conn
rd *bufio.Reader
wr *bufio.Writer
dest *net.UDPAddr
dev *device
closeChan chan bool
}
func (cli *client) sendResponse(cmd string, args ...string) error {
str := cmd
if len(args) > 0 {
str += " " + strings.Join(args, " ")
}
if debug {
log.Printf("SERVER: %s", str)
}
_, err := cli.wr.WriteString(str + eol)
if err == nil {
err = cli.wr.Flush()
}
return err
}
func (cli *client) handleCommand(cmd string, args []string) error {
switch cmd {
default:
if err := cli.sendResponse(cmd, resUnknown); err != nil {
return err
}
case cmdFreq:
if cli.dev == nil {
return cli.sendResponse(cmd, resDevice, "no active device")
}
if len(args) == 0 {
if curFreq, err := cli.dev.rtlDev.GetCenterFreq(); err != nil {
return cli.sendResponse(cmd, "-", "failed to get frequency")
}
return cli.sendResponse(cmd, strconv.FormatUint(uint64(curFreq), 10))
}
freq, err := strconv.ParseFloat(args[0], 64)
if err != nil {
return cli.sendResponse(cmd, resFail, "invalid format for frequency -- expected float")
}
if err := cli.dev.rtlDev.SetCenterFreq(uint(freq)); err != nil {
return cli.sendResponse(cmd, resFail, "failed to set frequency")
}
curFreq, err := cli.dev.rtlDev.GetCenterFreq()
if err != nil {
return cli.sendResponse(cmd, resFail, "failed to get frequency")
}
return cli.sendResponse(cmd, resOK, fmt.Sprintf("%f %d %f %f", freq, curFreq, 0.0, 0.0))
case cmdAntenna:
if cli.dev == nil {
return cli.sendResponse(cmd, resDevice, "no active device")
}
if len(args) > 0 {
return cli.sendResponse(cmd, resOK)
}
return cli.sendResponse(cmd, resOK, "default")
case cmdRate:
if cli.dev == nil {
return cli.sendResponse(cmd, resDevice, "no active device")
}
if len(args) == 0 {
rate, err := cli.dev.rtlDev.GetSampleRate()
if err != nil {
return cli.sendResponse(cmd, "-", "failed to get sample rate")
}
return cli.sendResponse(cmd, strconv.Itoa(rate))
}
rate, err := strconv.ParseFloat(args[0], 64)
if err != nil {
return cli.sendResponse(cmd, resFail, "invalid format for sample rate -- expected float")
}
if err := cli.dev.rtlDev.SetSampleRate(uint(rate)); err != nil {
return cli.sendResponse(cmd, resFail, "failed to set sample rate")
}
curRate, err := cli.dev.rtlDev.GetSampleRate()
if err != nil {
return cli.sendResponse(cmd, resFail, "failed to get sample rate")
}
return cli.sendResponse(cmd, resOK, strconv.FormatUint(uint64(curRate), 10))
case cmdGain:
if cli.dev == nil {
return cli.sendResponse(cmd, resDevice, "no active device")
}
if len(args) == 0 {
gain, err := cli.dev.rtlDev.GetTunerGain()
if err != nil {
return cli.sendResponse(cmd, "-", "failed to get gain")
}
return cli.sendResponse(cmd, strconv.Itoa(gain))
}
gain, err := strconv.ParseFloat(args[0], 64)
if err != nil {
return cli.sendResponse(cmd, resFail, "invalid format for gain -- expected float")
}
if err := cli.dev.rtlDev.SetTunerGain(int(gain)); err != nil {
return cli.sendResponse(cmd, resFail, "failed to set gain")
}
curGain, err := cli.dev.rtlDev.GetTunerGain()
if err != nil {
return cli.sendResponse(cmd, resFail, "failed to get gain")
}
return cli.sendResponse(cmd, resOK, strconv.FormatUint(uint64(curGain), 10))
case cmdDest:
if len(args) == 0 {
if cli.dest == nil {
return cli.sendResponse(cmd, "-", "no DEST set")
}
return cli.sendResponse(cmd, cli.dest.String())
}
addr, err := net.ResolveUDPAddr("udp", args[0])
if err != nil {
log.Printf("Failed to resolve UDP address %s: %s", args[0], err.Error())
return cli.sendResponse(cmd, resFail, "failed to resolve address")
}
cli.dest = addr
return cli.sendResponse(cmd, resOK)
case cmdGo:
if cli.dest == nil {
return cli.sendResponse(cmd, resFail, "no DEST set")
}
if cli.dev == nil {
return cli.sendResponse(cmd, resDevice, "no active device")
}
if cli.isStreaming() {
return cli.sendResponse(cmd, "RUNNING")
}
if err := cli.startStreaming(); err != nil {
return cli.sendResponse(cmd, resFail, err.Error())
}
return cli.sendResponse(cmd, resOK)
case cmdStop:
if !cli.isStreaming() {
return cli.sendResponse(cmd, "STOPPED")
}
cli.stopStreaming()
return cli.sendResponse(cmd, resOK)
case cmdDevice:
hint := "-"
if len(args) > 0 {
hint = args[0]
}
// DEVICE RTL tuner=e4k
switch hint {
case "-": // default UHD device
if cli.isStreaming() {
cli.stopStreaming()
}
if cli.dev != nil {
cli.dev.close()
cli.dev = nil
}
devices := deviceList()
var dev *device
for _, d := range devices {
if err := d.open(); err == nil {
dev = d
break
} else if err != ErrDeviceNotAvailable {
log.Printf("Failed to open device %s: %s", d.name, err.Error())
}
}
if dev == nil {
return cli.sendResponse(cmd, "-", "no devices available")
}
cli.dev = dev
if err := dev.rtlDev.SetCenterFreq(defaultCenterFreq); err != nil {
log.Printf("Failed to set default center frequency: %+v", err)
}
if err := dev.rtlDev.SetSampleRate(defaultSampleRate); err != nil {
log.Printf("Failed to set default sample rate: %+v", err)
}
if err := dev.rtlDev.SetAGCMode(false); err != nil {
log.Printf("Failed to diable AGC mode: %+v", err)
}
minGain := 0.0
maxGain := 1.0
gainStep := 1.0
gains, err := dev.rtlDev.GetTunerGains()
if err != nil {
log.Printf("Failed to get tuner gains: %s", err.Error())
} else {
minGain = float64(gains[0])
maxGain = float64(gains[len(gains)-1])
gainStep = float64(gains[1] - gains[0])
}
// if debug {
// log.Printf("Gains: %+v", gains)
// }
_, tunerFreq, err := dev.rtlDev.GetXtalFreq()
if err != nil {
log.Printf("Failed to get tuner frequency: %s", err.Error())
} else {
tunerFreq = 0
}
// <DEVICE NAME>|<MIN GAIN>|<MAX GAIN>|<GAIN STEP>|<FPGA FREQ IN HZ>|<COMPLEX SAMPLE PAIRS PER PACKET>|<CSV LIST OF VALID ANTENNAS>[|<DEVICE SERIAL NUMBER>]
if err := cli.sendResponse(cmd, fmt.Sprintf("%s|%f|%f|%f|%f|%d|default", dev.name, minGain, maxGain, gainStep, float64(tunerFreq), samplesPerPacket)); err != nil {
return err
}
case "!": // release current device
if cli.isStreaming() {
cli.stopStreaming()
}
if cli.dev != nil {
cli.dev.close()
cli.dev = nil
}
if err := cli.sendResponse(cmd, "-"); err != nil {
return err
}
default:
if err := cli.sendResponse(cmd, "-", "unknown hint"); err != nil {
return err
}
}
}
return nil
}
func (cli *client) isStreaming() bool {
return cli.closeChan != nil
}
func (cli *client) stopStreaming() {
if !cli.isStreaming() {
return
}
close(cli.closeChan)
}
func (cli *client) startStreaming() error {
if cli.isStreaming() {
return nil
}
if cli.dest == nil {
return ErrNoDestination
}
conn, err := net.DialUDP("udp", nil, cli.dest)
if err != nil {
return err
}
if err := cli.dev.rtlDev.ResetBuffer(); err != nil {
return err
}
cli.closeChan = make(chan bool, 1)
bufOut := make([]byte, headerSize+samplesPerPacket*2*2)
bufOut[0] = flagStreamStart
bufOut[1] = 0 // notification: reserved (currently 0)
first := true
seq := 0
// cli.dev.rtlDev.ReadAsyncUsingSync(nBuffers, sampleBufferSize*2, func(buf []byte) bool {
cli.dev.rtlDev.ReadAsync(nBuffers, sampleBufferSize*2, func(buf []byte) bool {
if buf == nil {
cli.closeChan = nil
return true
}
select {
case _ = <-cli.closeChan:
cli.closeChan = nil
return true
default:
}
for bufferOffset := 0; bufferOffset < len(buf); bufferOffset += samplesPerPacket * 2 {
nValues := len(buf) - bufferOffset
if nValues > samplesPerPacket*2 {
nValues = samplesPerPacket * 2
}
bufOut[2] = uint8(seq & 0xff)
bufOut[3] = uint8(seq >> 8)
seq++
dsp.Ui8toi16b(buf[:nValues], bufOut[headerSize:])
if _, err := conn.Write(bufOut[:headerSize+nValues*2]); err != nil {
// TODO: what to do if not "connection refused"?
}
if first {
bufOut[0] = 0
first = false
}
}
return false
})
return nil
}
func (cli *client) loop() error {
if err := cli.sendResponse("DEVICE", "-"); err != nil {
return err
}
for {
lineBytes, err := cli.rd.ReadSlice('\n')
if err != nil {
return err
}
line := string(bytes.TrimSpace(lineBytes))
if len(line) == 0 {
continue
}
if debug {
log.Printf("CLIENT: %s", line)
}
parts := strings.Split(line, " ")
cmd := strings.ToUpper(parts[0])
args := parts[1:]
if err := cli.handleCommand(cmd, args); err != nil {
return err
}
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
cli := &client{
conn: conn,
wr: bufio.NewWriter(conn),
rd: bufio.NewReader(conn),
}
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
cli.dest = &net.UDPAddr{
IP: addr.IP,
Zone: addr.Zone,
Port: defaultPort,
}
}
if err := cli.loop(); err != nil && err != io.EOF {
log.Printf("Client handling error: %s", err.Error())
}
cli.stopStreaming()
if cli.dev != nil {
cli.dev.close()
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
if *flagCPUProfile {
wr, err := os.Create("cpu.prof")
if err != nil {
log.Fatal(err)
}
defer wr.Close()
if err := pprof.StartCPUProfile(wr); err != nil {
log.Fatal(err)
}
}
ln, err := net.Listen("tcp", "0.0.0.0:28888")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
go func() {
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err.Error())
continue
}
go handleConnection(conn)
}
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, os.Kill)
_ = <-signalChan
if *flagCPUProfile {
pprof.StopCPUProfile()
}
}