-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdrpower.go
217 lines (207 loc) · 4.71 KB
/
sdrpower.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
package main
import (
"context"
"fmt"
"log"
"math"
"os"
"path/filepath"
"time"
"github.com/samuel/go-rtlsdr/rtl"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
)
var total = 0
var scan = 0
var dev *rtl.Device
var id = time.Now().Unix()
var data = []opts.LineData{}
// startSdrPower : start SDR Power Monitor
func startSdrPower(ctx context.Context) {
log.Println("start sdr power")
timer := time.NewTicker(time.Second * time.Duration(syslogInterval))
defer timer.Stop()
count := 0
hz := startHz
dur := int64(0)
for {
select {
case <-timer.C:
if hz >= endHz {
hz = startHz
id = time.Now().Unix()
scan++
}
syslogCh <- fmt.Sprintf("type=Stats,total=%d,count=%d,ps=%.2f,send=%d,param=%d,scan=%d,dur=%d",
total, count, float64(count)/float64(syslogInterval), syslogCount, sdr, scan, dur)
log.Printf("type=Stats,total=%d,count=%d,ps=%.2f,send=%d,param=%d,scan=%d,dur=%d",
total, count, float64(count)/float64(syslogInterval), syslogCount, sdr, scan, dur)
syslogCount = 0
count = 0
sendMonitor()
case <-ctx.Done():
if dev != nil {
dev.Close()
dev = nil
}
log.Println("stop sdr power")
return
default:
if hz <= endHz {
if dev == nil {
if err := openRTLSdr(); err != nil {
log.Printf("failed to open RTL-SDR err=%v", err)
hz = endHz + stepHz
dur = -1
continue
}
log.Println("open RTL-SDR")
}
if err := doScan(hz); err != nil {
log.Printf("failed to scan err=%v", err)
hz = endHz + stepHz
dur = -1
continue
}
count++
total++
hz += stepHz
if hz > endHz {
dur = time.Now().Unix() - id
dev.Close()
dev = nil
log.Println("close RTL-SDR")
outChart()
if once {
log.Println("stop sdr power")
os.Exit(0)
}
}
} else {
time.Sleep(time.Millisecond * 100)
}
}
}
}
// スキャンの実施
func doScan(hz int) error {
// set center freq
if err := dev.SetCenterFreq(uint(hz)); err != nil {
return err
}
// wait 5mSec
time.Sleep(time.Millisecond * 5)
// Dummy Read
dmy := make([]byte, 1<<12)
if _, err := dev.Read(dmy); err != nil {
return err
}
// read data
buf := make([]byte, 16384)
n, err := dev.Read(buf)
if err != nil {
return err
}
p := 0
t := 0
for i := 0; i < n; i++ {
s := int(buf[i]) - 127
t += s
p += (s * s)
}
dc := float64(t) / float64(n)
e := float64(t)*2.0*dc - dc*dc*float64(n)
p -= int(math.Round(e))
dbm := float64(p)
dbm /= float64(1e6) // 1M
dbm = 10 * math.Log10(dbm)
data = append(data, opts.LineData{Value: []float64{float64(hz) / 1e6, dbm}})
syslogCh <- fmt.Sprintf("type=Power,id=%x,freq=%d,dbm=%.3f", id, hz, dbm)
return nil
}
func openRTLSdr() error {
var err error
dev, err = rtl.Open(sdr)
if err != nil {
return err
}
// no direct sample
// no offset tuning
// set auto gain
if gain == 0 {
if err := dev.SetTunerGainMode(false); err != nil {
return err
}
} else {
if err := dev.SetTunerGainMode(true); err != nil {
return err
}
if err := dev.SetTunerGain(gain); err != nil {
return err
}
}
// no PPM
// disable biasTee
if err := dev.SetBiasTee(false); err != nil {
return err
}
// reset buffer
if err := dev.ResetBuffer(); err != nil {
return err
}
// set sample rate 1MHz
if err := dev.SetSampleRate(1_000_000); err != nil {
return err
}
if err := dev.SetCenterFreq(uint(24 * 1e6)); err != nil {
return err
}
// wait 100mSec
time.Sleep(time.Millisecond * 100)
// read dumy data
buf := make([]byte, 16384)
if _, err := dev.Read(buf); err != nil {
return err
}
return nil
}
func outChart() {
if chartTitle == "" || len(data) < 1 {
data = []opts.LineData{}
return
}
theme := "white"
if dark {
theme = "dark"
}
title := chartTitle + "-" + time.Now().Format("2006/01/02/ 15:04:05")
line := charts.NewLine()
line.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{Title: title}),
charts.WithInitializationOpts(opts.Initialization{Theme: theme}),
charts.WithDataZoomOpts(opts.DataZoom{Type: "slider"}),
charts.WithTooltipOpts(opts.Tooltip{Show: true, Trigger: "axis"}),
charts.WithXAxisOpts(opts.XAxis{
AxisLabel: &opts.AxisLabel{Show: true, Formatter: "{value} MHz"},
Type: "value",
}),
charts.WithYAxisOpts(opts.YAxis{
AxisLabel: &opts.AxisLabel{Show: true, Formatter: "{value} dbm"},
Type: "value",
}),
)
line.AddSeries("power", data).SetSeriesOptions(
charts.WithLineChartOpts(opts.LineChart{
Smooth: true,
}),
)
file := filepath.Join(chartFolder, chartTitle+"-"+time.Now().Format("20060102150405")+".html")
f, err := os.Create(file)
if err != nil {
log.Printf("chart save err=%v", err)
} else {
line.Render(f)
defer f.Close()
}
data = []opts.LineData{}
}