-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnector.go
689 lines (645 loc) · 22.9 KB
/
connector.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//go:build !windows
// Copyright 2020, 2021 James P. Ancona
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main // "github.com/jancona/hpsdrconnector"
import (
"bufio"
"context"
"encoding/binary"
"errors"
"flag"
"fmt"
"log"
"math"
"net"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/hashicorp/logutils"
"github.com/jancona/hpsdr"
"github.com/jancona/hpsdr/protocol1"
)
var (
iqPortArg *uint = flag.Uint("port", 4590, "IQ listen port")
frequencyArg *uint = flag.Uint("frequency", 7100000, "Tune to specified frequency in Hz")
sampleRateArg *uint = flag.Uint("samplerate", 96000, "Use the specified samplerate: one of 48000, 96000, 192000, 384000")
lnaGainArg *uint = flag.Uint("gain", 20, "LNA gain between 0 (-12dB) and 60 (48dB)")
controlPortArg *uint = flag.Uint("control", 4591, "control socket port (default 4591)")
radioIPArg *string = flag.String("radio", "", "IP address of radio (default use first radio discovered)")
isDebugArg *bool = flag.Bool("debug", false, "Emit debug log messages on stdout")
isServerArg *bool = flag.Bool("server", false, "Run as the server process")
serverPortArg *uint = flag.Uint("serverPort", 7300, "Server port for this radio")
serverLogArg *string = flag.String("serverLog", "", "Device/file for server log (default stdout)")
)
func init() {
flag.UintVar(iqPortArg, "p", 4590, "IQ listen port")
flag.UintVar(frequencyArg, "f", 7100000, "Tune to specified frequency in Hz")
flag.UintVar(sampleRateArg, "s", 96000, "Use the specified samplerate: one of 48000, 96000, 192000, 384000")
flag.UintVar(lnaGainArg, "g", 20, "LNA gain between 0 (-12dB) and 60 (48dB)")
flag.UintVar(controlPortArg, "c", 4591, "control socket port (default 4591)")
flag.StringVar(radioIPArg, "r", "", "IP address of radio (default use first radio discovered)")
flag.BoolVar(isDebugArg, "d", false, "Emit debug log messages on stdout")
}
func main() {
var err error
flag.Parse()
minLogLevel := "INFO"
if *isDebugArg {
minLogLevel = "DEBUG"
}
logWriter := os.Stdout
if *serverLogArg != "" {
logWriter, err = os.OpenFile(*serverLogArg, os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0644)
if err != nil {
log.Fatalf("client: Error opening server output, exiting: %v", err)
}
}
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "ERROR"},
MinLevel: logutils.LogLevel(minLogLevel),
Writer: logWriter,
}
log.SetOutput(filter)
log.Print("[DEBUG] Debug is on")
serverConn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", *serverPortArg))
if *isServerArg {
if err != nil {
// Run as a server
runAsServer()
os.Exit(0)
} else {
log.Fatalf("Attempting to start a server on port %d, but one is already running", *serverPortArg)
}
} else {
runAsClient(serverConn, err)
}
}
func runAsClient(serverConn net.Conn, err error) {
if err != nil {
log.Printf("[DEBUG] client: No server running, starting one on port %d", *serverPortArg)
// No server running, so start one
args := []string{
"--server",
"--serverPort", strconv.FormatUint(uint64(*serverPortArg), 10),
"--samplerate", strconv.FormatUint(uint64(*sampleRateArg), 10),
"--gain", strconv.FormatUint(uint64(*lnaGainArg), 10),
}
if *radioIPArg != "" {
args = append(args, "--radio", *radioIPArg)
}
if *isDebugArg {
args = append(args, "--debug")
}
log.Printf("[DEBUG] client: Server args: %v", args)
cmd := exec.Command("hpsdrconnector", args...)
cmd.SysProcAttr =
&syscall.SysProcAttr{
// Start server in its own process group so it survives the death of the client
Setpgid: true,
Pgid: 0,
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
log.Fatalf("client: Unable to launch hpsdrconnector server, exiting: %v", err)
}
for i := 0; i < 10; i++ {
serverConn, err = net.Dial("tcp", fmt.Sprintf("localhost:%d", *serverPortArg))
if err != nil {
log.Printf("client: Failed to connect to hpsdrconnector server, exiting: %v", err)
}
if serverConn != nil {
break
}
time.Sleep(2 * time.Second)
}
if err != nil {
log.Fatalf("client: Unable to connect to hpsdrconnector server, exiting: %v", err)
}
}
// We should now have a server to talk to
cmd := fmt.Sprintf("new_receiver:%d:%d:%d", *iqPortArg, *controlPortArg, *frequencyArg)
log.Printf("[DEBUG] client: Sending command '%s' to server", cmd)
_, err = serverConn.Write([]byte(cmd))
if err != nil {
log.Fatalf("client: Error sending command '%s' to server, exiting: %v", cmd, err)
}
// Run until we're terminated then tell server to close receiver
log.Print("[DEBUG] client: Waiting for close signal")
// wait for a close signal then clean up
signalChan := make(chan os.Signal, 1)
cleanupDone := make(chan struct{})
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
log.Print("client: Received an interrupt, stopping...")
// Send close_receiver command to server
cmd := fmt.Sprintf("close_receiver:%d", *controlPortArg)
_, err = serverConn.Write([]byte(cmd))
if err != nil {
log.Fatalf("client: Error sending command '%s' to server, exiting: %v", cmd, err)
}
close(cleanupDone)
}()
<-cleanupDone
}
func runAsServer() {
log.Printf("[DEBUG] server(%d): Starting", *serverPortArg)
switch *sampleRateArg {
case 48000:
case 96000:
case 192000:
case 384000:
default:
log.Fatalf("Invalid sample rate %d. Must be one of 48000, 96000, 192000, 384000", *sampleRateArg)
}
if *lnaGainArg > 60 {
log.Fatalf("Invalid LNA gain %d. Must be between 0 and 60", *lnaGainArg)
}
device, err := discoverDevice(*radioIPArg)
if err != nil {
log.Fatalf("Error discovering device, exiting: %v", err)
}
server := &Server{
port: *serverPortArg,
receivers: map[uint]hpsdr.Receiver{},
stopReceivers: map[uint]chan struct{}{},
}
server.radio = protocol1.NewRadio(device)
server.radio.SetSampleRate(*sampleRateArg)
server.radio.SetTXFrequency(*frequencyArg)
server.radio.SetLNAGain(*lnaGainArg)
done := make(chan struct{})
log.Printf("[INFO] server(%d): Listening to radio on %v", server.port, device.Network.Address.String())
config := newReuseAddrListenConfig()
l, err := config.Listen(context.Background(), "tcp", fmt.Sprintf("127.0.0.1:%d", server.port))
if err != nil {
log.Fatalf("Error listening on server port %d: %v", server.port, err)
}
serverListener, ok := l.(*net.TCPListener)
if !ok {
log.Fatalf("serverListener on port %d is not a TCPListener", server.port)
}
go server.handleServerListener(serverListener, done)
log.Printf("[INFO] server(%d): Starting %s radio with up to %d receivers on %s", server.port, device.Name, device.SupportedReceivers, device.Network.Address.String())
err = server.radio.Start()
if err != nil {
log.Fatalf("Error starting radio %s: %v", device.Name, err)
}
go server.sendTransmitSamples()
<-done
log.Printf("[INFO] server(%d): Exiting...", server.port)
server.radio.Close()
log.Printf("[INFO] server(%d): Done", server.port)
}
func discoverDevice(radioIP string) (*hpsdr.Device, error) {
var device *hpsdr.Device
var err error
if radioIP != "" {
device, err = hpsdr.DiscoverDevice(radioIP)
if err != nil {
return nil, fmt.Errorf("error locating device at %s: %v", radioIP, err)
}
if device == nil {
return nil, fmt.Errorf("no device found at %s", radioIP)
}
} else {
devices, err := hpsdr.DiscoverDevices()
if err != nil {
log.Printf("[ERROR] Error discovering devices: %v", err)
}
log.Printf("[DEBUG] main: devices: %#v", devices)
if len(devices) == 0 {
return nil, errors.New("no devices detected")
}
device = devices[0]
}
return device, nil
}
// Server represents the radio server
type Server struct {
port uint
radio hpsdr.Radio
receiverMutex sync.RWMutex
receivers map[uint]hpsdr.Receiver
stopReceivers map[uint]chan struct{}
}
func (s *Server) handleServerListener(serverListener *net.TCPListener, done chan struct{}) {
log.Printf("[DEBUG] server(%d): handleServerListener starting", s.port)
timeoutCount := 0
for {
err := serverListener.SetDeadline(time.Now().Add(10 * time.Second))
if err != nil {
log.Printf("[DEBUG] server(%d): serverListener.SetDeadline error: %v", s.port, err)
}
// log.Printf("[DEBUG] server(%d): Calling Accept", s.port)
serverConn, err := serverListener.Accept()
if errors.Is(err, os.ErrDeadlineExceeded) {
if s.receiverCount() == 0 {
timeoutCount++
log.Printf("[DEBUG] server(%d): serverListener.Accept() timeout with no receivers, timeoutCount=%d", s.port, timeoutCount)
if timeoutCount > 1 {
// we waited at least 10 seconds with no receivers added, so exit
log.Printf("[DEBUG] server(%d): serverListener.Accept() timeout with no receivers, exiting...", s.port)
break
}
} else {
timeoutCount = 0
}
continue
} else if err != nil {
log.Fatalf("server(%d): Error accepting connection on server port: %v", s.port, err)
}
log.Printf("[DEBUG] server(%d): Opened connection on server port", s.port)
go s.handleServerConn(serverConn)
}
log.Printf("[DEBUG] server(%d): All receivers closed for more than 10s, so exiting", s.port)
close(done)
}
func (s *Server) handleServerConn(c net.Conn) {
defer c.Close()
buf := make([]byte, 128)
for run := true; run; {
cnt, err := c.Read(buf)
if err != nil {
log.Printf("[DEBUG] server(%d): Error reading server socket: %v", s.port, err)
run = false
break
}
if cnt <= 0 {
log.Printf("[DEBUG] server(%d): c.Read() count=%d, closing", s.port, cnt)
run = false
break
}
cmd := strings.Trim(string(buf[:cnt]), "\x00\r\n ")
log.Printf("[DEBUG] server(%d): Received server command '%s'", s.port, cmd)
tok := strings.Split(cmd, ":")
switch tok[0] {
case "new_receiver":
if len(tok) != 4 {
log.Printf("[DEBUG] server(%d): Ignoring bad new_receiver command: %s", s.port, cmd)
continue
}
iqPort, err := strconv.ParseUint(tok[1], 10, 64)
if err != nil {
log.Printf("[DEBUG] server(%d): Ignoring new_receiver command with bad IQ port value '%s': %v", s.port, cmd, err)
continue
}
controlPort, err := strconv.ParseUint(tok[2], 10, 64)
if err != nil {
log.Printf("[DEBUG] server(%d): Ignoring new_receiver command with bad control port value '%s': %v", s.port, cmd, err)
continue
}
frequency, err := strconv.ParseUint(tok[3], 10, 64)
if err != nil {
log.Printf("[DEBUG] server(%d): Ignoring new_receiver command with bad frequency value '%s': %v", s.port, cmd, err)
continue
}
err = s.addReceiver(uint(iqPort), uint(controlPort), uint(frequency))
if err != nil {
log.Printf("[DEBUG] server(%d): Unable to execute server command '%s': %v", s.port, cmd, err)
continue
}
case "close_receiver":
if len(tok) != 2 {
log.Printf("[DEBUG] server(%d): Ignoring bad close_receiver command: %s", s.port, cmd)
continue
}
controlPort, err := strconv.ParseUint(tok[1], 10, 64)
if err != nil {
log.Printf("[DEBUG] server(%d): Ignoring close_receiver command with bad control port value '%s': %v", s.port, cmd, err)
continue
}
s.closeReceiver(uint(controlPort))
log.Printf("[INFO] server(%d): Closed receiver listening on control port %d, %d receivers left", s.port, controlPort, s.receiverCount())
default:
log.Printf("[DEBUG] server(%d): Ignoring unsupported server command: %s", s.port, cmd)
}
}
}
func (s *Server) addReceiver(iqPort uint, controlPort uint, frequency uint) error {
log.Printf("[DEBUG] server(%d): addReceiver(%d, %d, %d)", s.port, iqPort, controlPort, frequency)
distributor := NewDistributor(s.port)
rec, err := s.radio.AddReceiver(distributor.Distribute)
if err != nil {
log.Printf("[DEBUG] server(%d): radio.AddReceiver returned error: %v", s.port, err)
return err
}
log.Printf("[DEBUG] server(%d): Added receiver on control port %d, closed: %v", s.port, controlPort, rec.IsClosed())
stopReceiver := make(chan struct{})
s.receiverMutex.Lock()
s.receivers[controlPort] = rec
s.stopReceivers[controlPort] = stopReceiver
s.receiverMutex.Unlock()
log.Printf("[INFO] server(%d): Added receiver listening on control port %d", s.port, controlPort)
l, err := s.retrySocketListen(controlPort)
if err != nil {
s.closeReceiver(controlPort)
return fmt.Errorf("error listening on control port %d: %w", controlPort, err)
}
controlListener, ok := l.(*net.TCPListener)
if !ok {
log.Fatal("controlListener is not a TCPListener")
}
go s.handleControlListener(controlListener, controlPort, rec, stopReceiver)
log.Printf("[INFO] server(%d): Listening on IQ port %d", s.port, iqPort)
l, err = s.retrySocketListen(iqPort)
if err != nil {
s.closeReceiver(controlPort)
return fmt.Errorf("error listening on IQ port %d: %w", iqPort, err)
}
iqListener, ok := l.(*net.TCPListener)
if !ok {
log.Fatal("iqListener is not a TCPListener")
}
go s.handleIQListener(iqListener, iqPort, distributor, stopReceiver)
rec.SetFrequency(frequency)
s.checkHPF()
return nil
}
func (s *Server) retrySocketListen(port uint) (net.Listener, error) {
log.Printf("[DEBUG] server(%d): retrySocketListen(%d)", s.port, port)
config := newReuseAddrListenConfig()
retry := 5
for {
controlListener, err := config.Listen(context.Background(), "tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err == nil || retry == 0 {
if err == nil {
log.Printf("[DEBUG] server(%d): retrySocketListen(%d) returns success", s.port, port)
} else {
log.Printf("[DEBUG] server(%d): retrySocketListen(%d) returns error %v", s.port, port, err)
}
return controlListener, err
}
retry--
log.Printf("[DEBUG] server(%d): Error listening on port %d, %d retries remaining: %v", s.port, port, retry, err)
time.Sleep(500 * time.Millisecond)
}
}
func (s *Server) handleControlListener(controlListener *net.TCPListener, controlPort uint, rec hpsdr.Receiver, stopReceiver chan struct{}) {
var controlConn net.Conn
var err error
var run, globalRun bool
globalRun = true
go func() {
<-stopReceiver
log.Printf("[DEBUG] server(%d): handleControlListener() on port %d received stopReceiver", s.port, controlPort)
globalRun = false
run = false
if controlConn != nil {
log.Printf("[DEBUG] server(%d): Closing connection on control port %d, %s", s.port, controlPort, controlConn.LocalAddr().String())
err := controlConn.Close()
if err != nil {
log.Printf("[ERROR] server(%d): Error closing connection on control port %d, %s: %v", s.port, controlPort, err, controlConn.LocalAddr().String())
}
}
log.Printf("[DEBUG] server(%d): Calling controlListener.Close() for port %d", s.port, controlPort)
err := controlListener.Close()
if err != nil {
log.Printf("[DEBUG] server(%d): Error closing listener for control port %d, %s: %v", s.port, controlPort, controlListener.Addr().String(), err)
}
log.Printf("[DEBUG] server(%d): Finished controlListener.Close() for port %d", s.port, controlPort)
}()
log.Printf("[DEBUG] server(%d): Control goroutine for port %d starting", s.port, controlPort)
for globalRun {
log.Printf("[DEBUG] Calling Accept on control port %d", controlPort)
controlConn, err = controlListener.Accept()
if err != nil {
log.Printf("[ERROR] server(%d): Error accepting connection on control port %d: %v", s.port, controlPort, err)
break
}
log.Printf("[DEBUG] server(%d): Opened connection on control port %d", s.port, controlPort)
go func(conn net.Conn) {
for run = true; run; {
buf := make([]byte, 128)
cnt, err := conn.Read(buf)
if err != nil {
log.Printf("[DEBUG] server(%d): Error reading on control socket %d: %v", s.port, controlPort, err)
run = false
break
}
if cnt <= 0 {
log.Printf("[DEBUG] server(%d): Zero-length read on control socket %d", s.port, controlPort)
run = false
break
}
cmd := strings.Trim(string(buf[:cnt]), "\x00\r\n ")
log.Printf("[DEBUG] server(%d): Received command '%s'", s.port, cmd)
tok := strings.Split(cmd, ":")
log.Printf("[DEBUG] len(tok)=%d", len(tok))
if len(tok) != 2 {
log.Printf("[DEBUG] server(%d): Ignoring invalid command '%s'", s.port, cmd)
continue
}
switch tok[0] {
case "samp_rate":
sr, err := strconv.ParseUint(tok[1], 10, 64)
if err != nil {
log.Printf("[DEBUG] server(%d): Ignoring bad sample rate value in control command '%s': %v", s.port, cmd, err)
continue
}
s.radio.SetSampleRate(uint(sr))
case "center_freq":
f, err := strconv.ParseUint(tok[1], 10, 64)
if err != nil {
log.Printf("[DEBUG] server(%d): Ignoring bad center frequency value in control command '%s': %v", s.port, cmd, err)
continue
}
rec.SetFrequency(uint(f))
s.checkHPF()
case "rf_gain":
val := strings.ToLower(tok[1])
var lnaGain uint
if val == "auto" || val == "none" {
lnaGain = 20
} else {
gain, err := strconv.ParseUint(val, 10, 64)
if err != nil {
log.Printf("[DEBUG] server(%d): Ignoring bad gain value in control command '%s': %v", s.port, cmd, err)
}
lnaGain = uint(gain)
}
s.radio.SetLNAGain(lnaGain)
default:
log.Printf("[DEBUG] server(%d): Ignoring unsupported control command: %s", s.port, cmd)
}
}
}(controlConn)
}
log.Printf("[INFO] server(%d): Control goroutine for port %d exiting", s.port, controlPort)
}
func (s *Server) handleIQListener(iqListener *net.TCPListener, iqPort uint, distributor *Distributor, stopReceiver chan struct{}) {
var iqConn net.Conn
var err error
globalRun := true
go func() {
<-stopReceiver
log.Printf("[DEBUG] server(%d): handleIQListener() on port %d received stopReceiver", s.port, iqPort)
globalRun = false
if iqConn != nil {
err = iqConn.Close()
if err != nil {
log.Printf("[DEBUG] server(%d): Error closing IQ socket on port %d: %v", s.port, iqPort, err)
}
}
log.Printf("[DEBUG] server(%d): Calling iqListener.Close() for port %d", s.port, iqPort)
err = iqListener.Close()
if err != nil {
log.Printf("[DEBUG] server(%d): Error closing IQ listener on port %d: %v", s.port, iqPort, err)
}
log.Printf("[DEBUG] server(%d): Finished iqListener.Close() for port %d", s.port, iqPort)
}()
for globalRun {
log.Printf("[DEBUG] server(%d): Calling Accept on IQ port %d", s.port, iqPort)
iqConn, err = iqListener.Accept()
if err != nil {
log.Printf("[ERROR] server(%d): Error accepting connection on IQ port %d: %v", s.port, iqPort, err)
break
}
log.Printf("[DEBUG] server(%d): Opened connection on IQ port %d", s.port, iqPort)
go func(conn net.Conn) {
bufConn := bufio.NewWriterSize(conn, 8192)
sampleChan := distributor.Listen()
defer func() {
log.Printf("[DEBUG] server(%d): Calling distributor.Close() for IQ port %d", s.port, iqPort)
distributor.Close(sampleChan)
}()
var samples []hpsdr.ReceiveSample
for run := true; run; {
samples, run = <-sampleChan
if run {
buf := make([]byte, len(samples)*8)
for i, sample := range samples {
binary.LittleEndian.PutUint32(buf[i*8:], math.Float32bits(sample.QFloat()))
binary.LittleEndian.PutUint32(buf[i*8+4:], math.Float32bits(sample.IFloat()))
}
_, err := bufConn.Write(buf)
if err != nil {
log.Printf("[DEBUG] server(%d): Error writing to IQ port %d: %v", s.port, iqPort, err)
return
}
}
}
}(iqConn)
}
log.Printf("[DEBUG] server(%d): IQ goroutine for port %d exiting", s.port, iqPort)
}
func (s *Server) sendTransmitSamples() {
ts := make([]hpsdr.TransmitSample, s.radio.TransmitSamplesPerMessage())
for {
// Send empty transmit samples to pass config changes and keep watchdog timer happy
s.radio.SendSamples(ts)
time.Sleep(time.Second / time.Duration(48000/s.radio.TransmitSamplesPerMessage()))
}
}
func (s *Server) receiverCount() int {
s.receiverMutex.RLock()
defer s.receiverMutex.RUnlock()
return len(s.receivers)
}
func (s *Server) closeReceiver(controlPort uint) {
log.Printf("[DEBUG] server(%d): closeReceiver(%d)", s.port, controlPort)
s.receiverMutex.Lock()
defer s.receiverMutex.Unlock()
// tell goroutines to exit
close(s.stopReceivers[controlPort])
delete(s.stopReceivers, controlPort)
rec := s.receivers[controlPort]
if rec == nil {
log.Printf("[INFO] server(%d): Receiver on control port %d already closed", s.port, controlPort)
return
}
err := rec.Close()
if err != nil {
log.Printf("[INFO] server(%d): Error closing receiver on control port %d", s.port, controlPort)
}
delete(s.receivers, controlPort)
}
func (s *Server) checkHPF() {
if s.radio.Device().Device == hpsdr.DeviceHermesLite2 {
s.receiverMutex.RLock()
defer s.receiverMutex.RUnlock()
minFrequency := uint(math.MaxUint32)
for _, r := range s.receivers {
f := r.GetFrequency()
if f < minFrequency {
minFrequency = f
}
}
log.Printf("[DEBUG] server(%d): minFrequency: %d", s.port, minFrequency)
if minFrequency >= 3000000 {
// Turn on high pass filter
s.radio.SetOCOut(0b1000000)
} else {
s.radio.SetOCOut(0)
}
}
}
func newReuseAddrListenConfig() net.ListenConfig {
return net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
return c.Control(func(descriptor uintptr) {
syscall.SetsockoptInt(int(descriptor), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
})
},
}
}
// Distributor distributes received samples to one or more IQ goroutines
type Distributor struct {
sync.RWMutex
serverPort uint
listeners map[chan []hpsdr.ReceiveSample]bool
}
// NewDistributor constructs a distributor
func NewDistributor(p uint) *Distributor {
return &Distributor{
serverPort: p,
listeners: map[chan []hpsdr.ReceiveSample]bool{},
}
}
// Listen returms a channel for a listening goroutine to receive samples
func (d *Distributor) Listen() chan []hpsdr.ReceiveSample {
c := make(chan []hpsdr.ReceiveSample, *sampleRateArg) // one second buffer
d.Lock()
d.listeners[c] = true
d.Unlock()
log.Printf("[DEBUG] server(%d): Added listener %v", d.serverPort, c)
return c
}
// Close closes a channel when a goroutine is done
func (d *Distributor) Close(c chan []hpsdr.ReceiveSample) {
log.Printf("[DEBUG] server(%d): Removing listener %v", d.serverPort, c)
d.Lock()
delete(d.listeners, c)
d.Unlock()
close(c)
log.Printf("[DEBUG] server(%d): Removed listener %v", d.serverPort, c)
}
// Distribute receives samples from a radio and distributes them to listening goroutines
func (d *Distributor) Distribute(samples []hpsdr.ReceiveSample) {
// log.Printf("[DEBUG] Got %d samples", len(samples))
d.RLock()
for l := range d.listeners {
select {
case l <- samples:
// fmt.Println("sent message")
default:
// Drop unsent samples on the floor
}
}
d.RUnlock()
}