-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHPSDRProgrammer_cmd.go
More file actions
345 lines (303 loc) · 9.5 KB
/
HPSDRProgrammer_cmd.go
File metadata and controls
345 lines (303 loc) · 9.5 KB
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
// Program to program HPSDR boards from the command line
// new protocol version
// by David R. Larsen KV0S, Copyright 2014-11-24
//
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"runtime"
"strings"
"time"
"github.com/kv0s/newopenhpsdr"
)
const version string = "0.2.8"
const protocol string = ">1.7"
const update string = "2016-9-17"
// global current board
var crtbd newopenhpsdr.Hpsdrboard
// function to point users to the command list
func usage() {
log.Printf(" For a list of commands use -help \n\n")
}
// Function to print the program name info
func program() {
log.Printf("HPSDRProgrammer_cmd version:(%s)\n", version)
log.Printf(" By Dave KV0S, 2014-11-24, GPL2 \n\n")
log.Printf(" Protocol: %s \n", protocol)
log.Printf(" Last Updated: %s \n\n", update)
}
// Convenience function to print board data
func Listboard(str newopenhpsdr.Hpsdrboard) {
if str.Macaddress != "0:0:0:0:0:0" {
log.Printf("\n")
log.Printf(" Board Type: %s\n", str.Board)
log.Printf(" HPSDR Board: (%s)\n", str.Macaddress)
log.Printf(" Board Address: %s\n", str.Baddress)
log.Printf(" Protocol: %s\n", str.Protocol)
log.Printf(" Firmware: %s\n", str.Firmware)
log.Printf(" Receivers: %d\n", str.Receivers)
log.Printf(" Freq. Input: %s\n", str.Freqinput)
log.Printf(" IQ data format: %s\n", str.Iqdata)
log.Printf(" Status: %s\n", str.Status)
}
}
// Convenience function to print interface data
func Listinterface(itr newopenhpsdr.Intface) {
log.Printf(" Computer: (%v)\n", itr.MAC)
log.Printf(" OS: %s (%s) %d CPU(s)\n", runtime.GOOS, runtime.GOARCH, runtime.NumCPU())
if runtime.GOARCH != "arm" {
u, err := user.Current()
if err != nil {
panic(err.Error())
}
log.Printf(" Username: %s (%s) %s\n", u.Name, u.Username, u.HomeDir)
}
log.Printf(" IPV4: %v\n", itr.Ipv4)
//log.Printf(" Mask: %d\n", itr.Mask)
//log.Printf(" Network: %v\n", itr.Network)
log.Printf(" IPV6: %v\n", itr.Ipv6)
}
func Listflags(fg flagsettings) {
log.Printf(" Saved Settings: \n")
log.Printf(" Interface: %v\n", fg.Intface)
log.Printf(" Index: %v\n", fg.Index)
log.Printf(" Filename: %v\n", fg.Filename)
log.Printf(" Selected MAC: (%v)\n", fg.SelectMAC)
log.Printf(" SetRBF: %v\n", fg.SetRBF)
log.Printf(" Debug: %v\n", fg.Debug)
log.Printf(" Ddelay: %d\n", fg.Ddelay)
log.Printf(" Edelay: %d\n", fg.Edelay)
}
func Listflagstemp(fgt flagtemp) {
log.Printf(" Temp settings: \n")
log.Printf(" Settings: %v\n", fgt.Settings)
log.Printf(" SetIP: %v\n", fgt.SetIP)
log.Printf(" Save: %v\n", fgt.Save)
log.Printf(" Load: %v\n", fgt.Load)
}
func Initflags(fg *flagsettings) {
fg.Intface = "none"
fg.Filename = "none"
fg.SelectMAC = "none"
fg.SetRBF = "none"
fg.Debug = "none"
fg.Ddelay = 2
fg.Edelay = 60
}
type flagsettings struct {
Filename string
Intface string
Index int
SelectMAC string
SetRBF string
Debug string
Ddelay int
Edelay int
}
type flagtemp struct {
SetIP string
Settings string
Save string
Load string
}
func Initflagstemp(fgt *flagtemp) {
fgt.SetIP = "none"
fgt.Settings = "none"
fgt.Save = "none"
fgt.Load = "none"
}
func Parseflagstruct(fg *flagsettings, fgt *flagtemp, id int, stmac string, stip string, strbf string, db string, ss string, sv string, ld string, dd int, ed int) {
Initflags(fg)
Initflagstemp(fgt)
if (ld == "default") || (ld == "Default") {
fg.Filename = "HPSDRProgrammer_cmd.json"
} else if ld != "none" {
fg.Filename = ld
}
if ld != "none" {
dta, _ := ioutil.ReadFile(fg.Filename)
err := json.Unmarshal(dta, &fg)
if err != nil {
log.Println("error:", err)
}
}
//if ifn != "none" {
// fg.Intface = ifn
//}
if id != 0 {
fg.Index = id
}
if stmac != "none" {
fg.SelectMAC = stmac
}
if strbf != "none" {
fg.SetRBF = strbf
}
if db != "none" {
fg.Debug = db
}
if ed != 20 {
fg.Edelay = ed
}
if dd != 2 {
fg.Ddelay = dd
}
if ed != 2 {
fg.Edelay = ed
}
if stip != "none" {
fgt.SetIP = stip
}
if ss != "none" {
fgt.Settings = ss
}
if sv == "default" {
fgt.Save = sv
fg.Filename = "HPSDRProgrammer_cmd.json"
} else if sv != "none" {
fg.Filename = sv
fgt.Save = sv
} else {
fgt.Save = sv
}
if ld == "default" {
fgt.Load = ld
fg.Filename = "HPSDRProgrammer_cmd.json"
} else if ld != "none" {
fg.Filename = ld
fgt.Load = ld
} else {
fgt.Load = ld
}
if fgt.Save != "none" {
f, err := os.Create(fg.Filename)
if err != nil {
panic(err)
}
b, err := json.MarshalIndent(fg, "", "\t")
if err != nil {
log.Println("error:", err)
}
fmt.Fprintf(f, "%s\n", b)
}
if ss != "none" {
Listflags(*fg)
Listflagstemp(*fgt)
}
}
func main() {
var fg flagsettings
var fgt flagtemp
//var erstat newopenhpsdr.Erasestatus
// Create the command line flags
//ifn := flag.String("interface", "none", "Select one interface number")
id := flag.Int("index", 0, "Select one interface by number")
stmac := flag.String("selectMAC", "none", "Select Board by MAC address")
stip := flag.String("setIP", "none", "Set IP address, unused number from your subnet or 0.0.0.0 for DHCP")
strbf := flag.String("setRBF", "none", "Select the RBF file to write to the board")
dd := flag.Int("ddelay", 8, "Discovery delay before a rediscovery")
ed := flag.Int("edelay", 60, "Discovery delay before a rediscovery")
db := flag.String("debug", "none", "Turn debugging and output type, (none, dec, hex)")
ss := flag.String("settings", "none", "Show the settings values (show)")
sv := flag.String("save", "none", "Save these current flags for future use in default or a named file")
ld := flag.String("load", "none", "Load a saved command file from default or a named file")
//cadr := flag.Bool("checkaddress", true, "check if new address is in subdomain and not restricted space")
//cbad := flag.Bool("checkboard", true, "check if new RBF file name has the same name as the board type")
flag.Parse()
if flag.NFlag() < 1 {
program()
usage()
}
Parseflagstruct(&fg, &fgt, *id, *stmac, *stip, *strbf, *db, *ss, *sv, *ld, *dd, *ed)
intf := newopenhpsdr.Interfaces()
for i := range intf {
if flag.NFlag() < 1 {
// if no flags list the interfaces in short form
log.Printf(" %d - %s (%s)\n", intf[i].Index, intf[i].Intname, intf[i].MAC)
} else if (flag.NFlag() == 1) && (fg.Index == 0) {
if fg.Debug == "none" {
// if one flag and it is debug = none, list the interface in short form
log.Printf(" %d - %s (%s)\n", intf[i].Index, intf[i].Intname, intf[i].MAC)
} else {
// if one flag and it is debug = dec or hex, list the interface in long form
log.Printf(" %d - %s (%s %s %s\n", intf[i].Index, intf[i].Intname, intf[i].MAC, intf[i].Ipv4, intf[i].Ipv6)
}
}
// if ifn flag matches the current interface
if fg.Index == intf[i].Index {
if len(intf[i].Ipv4) != 0 {
//list the sending computer information
Listinterface(intf[i])
var adr string
var bcadr string
adr = intf[i].Ipv4 + ":0"
bcadr = intf[i].Ipv4Bcast + ":1024"
// perform a discovery
str, err := newopenhpsdr.Discover(adr, bcadr, fg.Debug)
if err != nil {
log.Println("Error ", err)
}
//loop throught the list of discovered HPSDR boards
for i := 0; i < len(str); i++ {
Listboard(str[i])
if fg.SelectMAC == str[i].Macaddress {
log.Printf(" Selected MAC: (%s) %s\n", fg.SelectMAC, str[i].Board)
crtbd = str[i]
if (fgt.SetIP != str[i].Baddress) && (fgt.SetIP != "none") {
//If the IPV4 changes
if strings.Contains(*stip, "255.255.255.255") {
log.Printf(" Changing IP address from %s to DHCP address\n\n", str[i].Baddress)
} else {
log.Printf(" Changing IP address from %s to %s\n\n", str[i].Baddress, *stip)
}
_, err := newopenhpsdr.Setip(adr, bcadr, str[i], *stip, fg.Debug)
if err != nil {
log.Printf("Error %v", err)
panic(err)
}
// perform a rediscovery
time.Sleep(time.Duration(fg.Ddelay) * time.Second)
str, err = newopenhpsdr.Discover(adr, bcadr, fg.Debug)
if err != nil {
log.Println("Error ", err)
}
Listboard(str[i])
} else if *strbf != "none" {
if (fg.SelectMAC != "none") && (fg.SelectMAC == str[i].Macaddress) {
if strings.Contains(strings.ToLower(*strbf), strings.ToLower(str[i].Board)) {
// erase the board flash memory
//erstat, err := newopenhpsdr.Erase(str[i], fg.SetRBF, fg.Debug)
//err := newopenhpsdr.Erase(crtbd, fg.Debug)
err := newopenhpsdr.Erase(adr, str[i], fg.Debug)
if err != nil {
panic(err)
} else {
//log.Printf(" %v %v\n", erstat.Seconds, erstat.State)
// send the RBF to the flash memory
//time.Sleep(8 * time.Second)
//newopenhpsdr.Program(str[i], fg.SetRBF, fg.Debug)
err := newopenhpsdr.Program(adr, str[i], *strbf, fg.Debug)
if err != nil {
panic(err)
}
}
} else {
log.Printf("\n Input Check: RBF name \"%s\" and selectedMAC board name \"%s\" (%s) do not match!\n", *strbf, str[i].Board, str[i].Macaddress)
log.Printf(" Please correct to program the board.\n")
}
} else {
log.Printf(" Interface not active! \n")
}
}
}
}
}
}
}
}