forked from bemasher/rtldavis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (144 loc) · 4.28 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
/*
rtldavis, an rtl-sdr receiver for Davis Instruments weather stations.
Copyright (C) 2015 Douglas Hall
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"time"
"github.com/bemasher/rtldavis/protocol"
"github.com/jpoirier/gortlsdr"
)
var (
id *int
verbose *bool
verboseLogger *log.Logger
)
func init() {
log.SetFlags(log.Lmicroseconds)
rand.Seed(time.Now().UnixNano())
id = flag.Int("id", 0, "id of the station to listen for")
verbose = flag.Bool("v", false, "log extra information to /dev/stderr")
flag.Parse()
verboseLogger = log.New(ioutil.Discard, "", log.Lshortfile|log.Lmicroseconds)
if *verbose {
verboseLogger.SetOutput(os.Stderr)
}
}
func main() {
p := protocol.NewParser(14, *id)
p.Cfg.Log()
fs := p.Cfg.SampleRate
dev, err := rtlsdr.Open(0)
if err != nil {
log.Fatal(err)
}
hop := p.RandHop()
verboseLogger.Println(hop)
if err := dev.SetCenterFreq(hop.ChannelFreq); err != nil {
log.Fatal(err)
}
if err := dev.SetSampleRate(fs); err != nil {
log.Fatal(err)
}
if err := dev.SetTunerGainMode(false); err != nil {
log.Fatal(err)
}
if err := dev.ResetBuffer(); err != nil {
log.Fatal(err)
}
in, out := io.Pipe()
go dev.ReadAsync(func(buf []byte) {
out.Write(buf)
}, nil, 1, p.Cfg.BlockSize2)
// Handle frequency hops concurrently since the callback will stall if we
// stop reading to hop.
nextHop := make(chan protocol.Hop, 1)
go func() {
for hop := range nextHop {
verboseLogger.Printf("Hop: %s\n", hop)
if err := dev.SetCenterFreq(hop.ChannelFreq + hop.FreqError); err != nil {
log.Fatal(err)
}
}
}()
defer func() {
in.Close()
out.Close()
dev.CancelAsync()
dev.Close()
os.Exit(0)
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
block := make([]byte, p.Cfg.BlockSize2)
// Set the dwellTimer for one full rotation of the pattern + 1. Some channels
// may have enough frequency error that they won't receive until we've
// seen at least one message and set the frequency correction.
dwellTimer := time.After(52 * p.DwellTime)
// We set missCount to 3 so that we immediately pick another random
// channel and wait on that channel instead of hopping like we missed one.
missCount := 3
for {
select {
case <-sig:
return
case <-dwellTimer:
// If the dwellTimer has expired one of two things has happened:
// 1: We've missed a message.
// 2: We've waited for sync and nothing has happened for a
// full cycle of the pattern.
// Reset the timer and incrmeent the missed packet counter.
dwellTimer = time.After(p.DwellTime)
missCount++
if missCount >= 3 {
// We've missed three packets in a row, hop to a random
// channel and wait for a full hopping cycle.
nextHop <- p.RandHop()
dwellTimer = time.After(52 * p.DwellTime)
} else {
// We've missed fewer than three packets in a row, hop to the
// next channel in the pattern.
nextHop <- p.NextHop()
}
default:
in.Read(block)
recvPacket := false
for _, msg := range p.Parse(p.Demodulate(block)) {
if int(msg.ID) != *id {
continue
}
recvPacket = true
log.Printf("%02X\n", msg.Data)
}
if recvPacket {
// Reset the missed packet counter.
missCount = 0
// Set the dwell timer to 1.5 * dwell time. If this timer
// expires before we've received a packet then the missed
// packet hopping logic will reset the timer to exactly the
// dwell time and we then expect packets to arrive half-way
// through the timer.
dwellTimer = time.After(p.DwellTime + p.DwellTime>>1)
// Hop to the next channel.
nextHop <- p.NextHop()
}
}
}
}