-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
165 lines (137 loc) · 4.39 KB
/
flags.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
// RTLAMR - An rtl-sdr receiver for smart meters operating in the 900MHz ISM band.
// Copyright (C) 2015 Douglas Hall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/gob"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/bemasher/rtlamr/csv"
)
var logFilename = flag.String("logfile", "/dev/stdout", "log statement dump file")
var logFile *os.File
var sampleFilename = flag.String("samplefile", os.DevNull, "raw signal dump file")
var sampleFile *os.File
var msgType = flag.String("msgtype", "scm", "message type to receive: scm, idm or r900")
var fastMag = flag.Bool("fastmag", false, "use faster alpha max + beta min magnitude approximation")
var symbolLength = flag.Int("symbollength", 73, "symbol length in samples, see -help for valid lengths")
var timeLimit = flag.Duration("duration", 0, "time to run for, 0 for infinite, ex. 1h5m10s")
var meterID UintMap
var meterType UintMap
var encoder Encoder
var format = flag.String("format", "plain", "format to write log messages in: plain, csv, json, xml or gob")
var gobUnsafe = flag.Bool("gobunsafe", false, "allow gob output to stdout")
var quiet = flag.Bool("quiet", false, "suppress printing state information at startup")
var single = flag.Bool("single", false, "one shot execution")
func RegisterFlags() {
meterID = make(UintMap)
meterType = make(UintMap)
flag.Var(meterID, "filterid", "display only messages matching an id in a comma-separated list of ids.")
flag.Var(meterType, "filtertype", "display only messages matching a type in a comma-separated list of types.")
rtlamrFlags := map[string]bool{
"logfile": true,
"samplefile": true,
"msgtype": true,
"symbollength": true,
"duration": true,
"filterid": true,
"filtertype": true,
"format": true,
"gobunsafe": true,
"quiet": true,
"single": true,
"cpuprofile": true,
"fastmag": true,
}
printDefaults := func(validFlags map[string]bool, inclusion bool) {
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if validFlags[f.Name] != inclusion {
return
}
format := " -%s=%s: %s\n"
fmt.Fprintf(os.Stderr, format, f.Name, f.DefValue, f.Usage)
})
}
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
printDefaults(rtlamrFlags, true)
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "rtltcp specific:")
printDefaults(rtlamrFlags, false)
}
}
func HandleFlags() {
var err error
if *logFilename == "/dev/stdout" {
logFile = os.Stdout
} else {
logFile, err = os.Create(*logFilename)
if err != nil {
log.Fatal("Error creating log file:", err)
}
}
log.SetOutput(logFile)
sampleFile, err = os.Create(*sampleFilename)
if err != nil {
log.Fatal("Error creating sample file:", err)
}
*format = strings.ToLower(*format)
switch *format {
case "plain":
break
case "csv":
encoder = csv.NewEncoder(logFile)
case "json":
encoder = json.NewEncoder(logFile)
case "xml":
encoder = xml.NewEncoder(logFile)
case "gob":
encoder = gob.NewEncoder(logFile)
if !*gobUnsafe && *logFilename == "/dev/stdout" {
fmt.Println("Gob encoded messages are not stdout safe, specify non-stdout -logfile or use -gobunsafe.")
os.Exit(1)
}
}
}
// JSON, XML and GOB all implement this interface so we can simplify log
// output formatting.
type Encoder interface {
Encode(interface{}) error
}
type UintMap map[uint]bool
func (m UintMap) String() (s string) {
var values []string
for k := range m {
values = append(values, strconv.FormatUint(uint64(k), 10))
}
return strings.Join(values, ",")
}
func (m UintMap) Set(value string) error {
values := strings.Split(value, ",")
for _, v := range values {
n, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return err
}
m[uint(n)] = true
}
return nil
}