-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wifi_types.go
642 lines (548 loc) · 14.6 KB
/
wifi_types.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
package hitron
import (
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"time"
)
// WiFiAccessControl -
type WiFiAccessControl struct {
Error
BlockType string
RulesList []WiFiAccessControlRule
}
// WiFiAccessControlRule -
type WiFiAccessControlRule struct {
Hostname string
MACAddr net.HardwareAddr
ID int
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *WiFiAccessControlRule) UnmarshalJSON(b []byte) error {
raw := struct {
Hostname string `json:"hostName"`
MACAddr string `json:"macAddr"`
ID int
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiAccessControlRule %q: %w", string(b), err)
}
s.ID = raw.ID
s.Hostname = raw.Hostname
s.MACAddr, _ = net.ParseMAC(raw.MACAddr)
return nil
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *WiFiAccessControl) UnmarshalJSON(b []byte) error {
raw := struct {
Error
BlockType string
RulesList []WiFiAccessControlRule `json:"Rules_List"`
RuleNumberOfEntries int
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiAccessControl %q: %w", string(b), err)
}
s.Error = raw.Error
s.BlockType = raw.BlockType
s.RulesList = raw.RulesList
return nil
}
// WiFiAccessControlStatus -
type WiFiAccessControlStatus struct {
Error
BlockType string
}
// WiFiGuestSSID -
type WiFiGuestSSID struct {
Error
SSID string
SSID5G string
Password string
MaxUsers int
Enable bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *WiFiGuestSSID) UnmarshalJSON(b []byte) error {
raw := struct {
Error
SSIDName string
SSIDName5G string
Enable string
Pswd string
AdminGuestAccProvider string
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiGuestSSID %q: %w", string(b), err)
}
s.Error = raw.Error
s.SSID = raw.SSIDName
s.SSID5G = raw.SSIDName5G
s.Password = raw.Pswd
s.Enable = raw.Enable == on
s.MaxUsers, _ = strconv.Atoi(raw.AdminGuestAccProvider)
return nil
}
// WiFiRadios -
type WiFiRadios struct {
Error
Radios []WiFiRadio `json:"Raidos_List"` // Yup, there's a typo in the Cable Modem
}
// WiFiRadio -
type WiFiRadio struct {
Error
Vendor string // "1" (??)
Band string // 2.4G/5G
ChanBandwidth string // 20MHz,20/40MHz,40MHz,80MHz
RadioURI string // Path to the radio: /1/Device/WiFi/Radios/<n>
Channel int // only applicable if AutoChannel == false
CurrentChannel int //
Mode WiFiMode //
Enable bool //
EnableDCS bool // Dynamic Channel Selection
EnableDFS bool // Dynamic Frequency Selection - use frequencies reserved for radars
EnableWPS bool // WiFi Protected Setup
IGMPSnoop bool //
AutoChannel bool //
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *WiFiRadio) UnmarshalJSON(b []byte) error {
raw := struct {
Error
Vendor string
Band string
WlsOnOff string
WlsDcsOnOff string
WlsMode string
NBandwidth string `json:"n_bandwidth"`
WlsChannel interface{}
AutoChannel string
WlsDfsOnOff string
WlsCurrentChannel string
WlswpsOnOff string
IgmpSnoop string
RadioURI string `json:"Radio_URI"`
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiRadio %q: %w", string(b), err)
}
s.Error = raw.Error
s.Vendor = raw.Vendor
s.Band = raw.Band
s.ChanBandwidth = raw.NBandwidth
s.RadioURI = raw.RadioURI
s.Enable = raw.WlsOnOff == on
s.EnableDCS = raw.WlsDcsOnOff == on
s.EnableDFS = raw.WlsDfsOnOff == on
s.AutoChannel = raw.AutoChannel == on
s.EnableWPS = raw.WlswpsOnOff == on
s.IGMPSnoop = raw.IgmpSnoop == on
s.CurrentChannel, _ = strconv.Atoi(raw.WlsCurrentChannel)
switch c := raw.WlsChannel.(type) {
case int:
s.Channel = c
case string:
s.Channel, _ = strconv.Atoi(c)
}
// map according to the CODA's UI
wirelessMode := map[string]WiFiMode{
"0": WiFiModeB,
"1": WiFiModeG,
"2": WiFiModeN,
"3": WiFiModeB | WiFiModeG | WiFiModeN,
"4": WiFiModeG | WiFiModeN,
"5": WiFiModeB | WiFiModeG,
"6": WiFiModeAC,
"7": WiFiModeA,
"8": WiFiModeA | WiFiModeN,
"9": WiFiModeA | WiFiModeN | WiFiModeAC,
}
s.Mode = wirelessMode[raw.WlsMode]
return nil
}
// WiFiMode enumerates the available WiFi modes (802.11a/b/g/n/ac)
type WiFiMode uint8
// Useful WiFi constants for indicating protocol support. For multi-protocol
// support, join with binary ORs (|).
const (
WiFiModeA WiFiMode = 1 << iota
WiFiModeB
WiFiModeG
WiFiModeN
WiFiModeAC
)
func (m WiFiMode) String() string {
suffixes := []string{}
if m&WiFiModeA != 0 {
suffixes = append(suffixes, "a")
}
if m&WiFiModeB != 0 {
suffixes = append(suffixes, "b")
}
if m&WiFiModeG != 0 {
suffixes = append(suffixes, "g")
}
if m&WiFiModeN != 0 {
suffixes = append(suffixes, "n")
}
if m&WiFiModeAC != 0 {
suffixes = append(suffixes, "ac")
}
suffix := strings.Join(suffixes, "/")
return "802.11" + suffix
}
// WiFiRadiosAdvanced -
type WiFiRadiosAdvanced struct {
Error
Radios []WiFiRadioAdvanced `json:"Advanced_List"`
}
// WiFiRadioAdvanced -
type WiFiRadioAdvanced struct {
Error
RxStream string
SSID string
BGMode string
NOperatingMode string
NGuardInterval string
TxStream string
WiFiRadio
NMCS int
NCoexistence bool
NRDG bool
Namsdu bool
Nautoba bool
Nbadecline bool
BandSteering bool
ShowMSO bool
}
// UnmarshalJSON - implements json.Unmarshaler
//
//nolint:funlen
func (s *WiFiRadioAdvanced) UnmarshalJSON(b []byte) error {
raw := struct {
Error
Vendor string
Band string
WlsOnOff string
WlsDcsOnOff string
WlsMode string
NBandwidth string `json:"n_bandwidth"`
WlsChannel interface{}
AutoChannel string
WlsDfsOnOff string
WlsCurrentChannel string
WlswpsOnOff string
IgmpSnoop string
RadioURI string `json:"Radio_URI"`
BGMode string `json:"bgMode"`
SSIDName string `json:"ssidName"`
NCoexistence string `json:"n_coexistence"`
BandSteering string `json:"bandsteering"`
NOperatingMode string `json:"n_OperatingMode"`
NGuardInterval string `json:"n_GuardInterval"`
NMCS string `json:"n_mcs"`
NRDG string `json:"n_rdg"`
Namsdu string `json:"n_amsdu"`
Nautoba string `json:"n_autoba"`
Nbadecline string `json:"n_badecline"`
TxStream string `json:"tx_stream"`
RxStream string `json:"rx_stream"`
ShowMSO string `json:"showMSO"`
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiRadioAdvanced %q: %w", string(b), err)
}
s.Error = raw.Error
s.SSID = raw.SSIDName
s.NOperatingMode = raw.NOperatingMode
s.NGuardInterval = raw.NGuardInterval
s.TxStream = raw.TxStream
s.RxStream = raw.RxStream
s.BandSteering = raw.BandSteering == on
s.ShowMSO = raw.ShowMSO == "true"
s.NCoexistence = raw.NCoexistence == enabled
s.NRDG = raw.NRDG == enabled
s.Namsdu = raw.Namsdu == enabled
s.Nautoba = raw.Nautoba == enabled
s.Nbadecline = raw.Nbadecline == enabled
s.NMCS, _ = strconv.Atoi(raw.NMCS)
s.Vendor = raw.Vendor
s.Band = raw.Band
s.ChanBandwidth = raw.NBandwidth
s.RadioURI = raw.RadioURI
s.Enable = raw.WlsOnOff == on
s.EnableDCS = raw.WlsDcsOnOff == on
s.EnableDFS = raw.WlsDfsOnOff == on
s.AutoChannel = raw.AutoChannel == on
s.EnableWPS = raw.WlswpsOnOff == on
s.IGMPSnoop = raw.IgmpSnoop == on
s.CurrentChannel, _ = strconv.Atoi(raw.WlsCurrentChannel)
switch c := raw.WlsChannel.(type) {
case int:
s.Channel = c
case string:
s.Channel, _ = strconv.Atoi(c)
}
// map according to the CODA's UI
wirelessMode := map[string]WiFiMode{
"0": WiFiModeB,
"1": WiFiModeG,
"2": WiFiModeN,
"3": WiFiModeB | WiFiModeG | WiFiModeN,
"4": WiFiModeG | WiFiModeN,
"5": WiFiModeB | WiFiModeG,
"6": WiFiModeAC,
"7": WiFiModeA,
"8": WiFiModeA | WiFiModeN,
"9": WiFiModeA | WiFiModeN | WiFiModeAC,
}
s.Mode = wirelessMode[raw.WlsMode]
return nil
}
// WiFiRadiosSurvey -
type WiFiRadiosSurvey struct {
Error
APs []WiFiAP `json:"APs_List"`
}
// WiFiAP - information about a WiFi Access Point/Network
type WiFiAP struct {
Band string
SSID string
NT string
WMode string // W-MODE
Security string
ExtCh string
BSSID net.HardwareAddr
Channel int
Signal int // percentage
WPS bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *WiFiAP) UnmarshalJSON(b []byte) error {
raw := struct {
Band string
SSIDName string `json:"ssidName"`
WlsChannel string `json:"wlsChannel"`
BSSID string `json:"bssid"`
Signal string `json:"signal"`
WMode string `json:"wmode"`
Security string `json:"security"`
WPS string `json:"wps"`
ExtCh string `json:"extch"`
NT string `json:"nt"`
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiAP %q: %w", string(b), err)
}
s.SSID = raw.SSIDName
s.Band = raw.Band
s.WMode = raw.WMode
s.ExtCh = raw.ExtCh
s.NT = raw.NT
s.WPS = raw.WPS == yes
s.BSSID, _ = net.ParseMAC(strings.TrimSpace(raw.BSSID))
s.Channel, _ = strconv.Atoi(raw.WlsChannel)
return nil
}
// WiFiSSIDs -
type WiFiSSIDs struct {
Error
SSIDs []SSID `json:"SSIDs_List"`
Guests []GuestSSID `json:"Guests_List"`
}
// SSID -
type SSID struct {
DefaultKey string
Name string `json:"ssidName"`
Band string
IfName string
AuthMode string
SecurityMode string
EncryptType string
Passphrase string
URI string
BSSID net.HardwareAddr
ID int
Radio int
Enable bool
EnableWPS bool
Visible bool
EnableWMM bool
EnableWLS bool
BandSteering bool
Primary bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *SSID) UnmarshalJSON(b []byte) error {
raw := struct {
ID string `json:"id"`
SSIDName string `json:"ssidName"`
Band string `json:"band"`
Enable string `json:"enable"`
WlswpsOnOff string `json:"wlswpsOnOff"`
IfName string `json:"ifName"`
BSSID string `json:"bssid"`
Radio string `json:"radio"`
Visible string `json:"visible"`
WMM string `json:"wmm"`
AuthMode string `json:"authMode"`
SecuMode string `json:"SecuMode"`
EncryptType string `json:"encryptType"`
Passphrase string `json:"passPhrase"`
WlsEnable string `json:"wlsEnable"`
URI string `json:"SSID_URI"`
DefaultKey string `json:"defaultKey"`
Bandsteer string `json:"bandsteer"`
Primary string `json:"primary"`
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal SSID %q: %w", string(b), err)
}
s.Name = raw.SSIDName
s.Band = raw.Band
s.IfName = raw.IfName
s.AuthMode = raw.AuthMode
s.SecurityMode = raw.SecuMode
s.EncryptType = raw.EncryptType
s.Passphrase = raw.Passphrase
s.URI = raw.URI
s.DefaultKey = raw.DefaultKey
s.Radio, _ = strconv.Atoi(raw.Radio)
s.Enable = raw.Enable == on
s.EnableWPS = raw.WlswpsOnOff == on
s.EnableWLS = raw.WlsEnable == on
s.Visible = raw.Visible == on
s.EnableWMM = raw.WMM == on
s.BandSteering = raw.Bandsteer == on
s.Primary = raw.Primary == yes
s.BSSID, _ = net.ParseMAC(strings.TrimSpace(raw.BSSID))
s.ID, _ = strconv.Atoi(raw.ID)
return nil
}
// GuestSSID -
type GuestSSID struct {
IfName string
Relate string
Enable bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *GuestSSID) UnmarshalJSON(b []byte) error {
raw := struct {
Enable string
IfName string
Relate string
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal GuestSSID %q: %w", string(b), err)
}
s.Enable = raw.Enable == on
s.IfName = raw.IfName
s.Relate = raw.Relate
return nil
}
// WiFiWPS -
type WiFiWPS struct {
Error
Method string
ClientPin string
Status string
TimeElapsed time.Duration
Enable bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *WiFiWPS) UnmarshalJSON(b []byte) error {
raw := struct {
Error
WlswpsOnOff string
WlsWpsMethod string
WlsWpsClientPin string
WlsWpsStatus string
WlsWpsTimeElapsed string
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiWPS %q: %w", string(b), err)
}
s.Error = raw.Error
s.Method = raw.WlsWpsMethod
s.ClientPin = raw.WlsWpsClientPin
s.Status = raw.WlsWpsStatus
s.Enable = raw.WlswpsOnOff == on
t, _ := strconv.Atoi(raw.WlsWpsTimeElapsed)
s.TimeElapsed = time.Duration(t) * time.Second
return nil
}
// WiFiClient -
type WiFiClient struct {
Error
Clients []WiFiClientEntry `json:"Client_List"`
}
// WiFiClientEntry -
type WiFiClientEntry struct {
Band string
SSID string
Hostname string
PhyMode string
MACAddr net.HardwareAddr
Index int
AID int
RSSI int // Received Signal Strength Indicator. Estimated measure of power level that a client is receiving from AP.
DataRate int64
Channel int
Bandwidth int64
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *WiFiClientEntry) UnmarshalJSON(b []byte) error {
raw := struct {
DataRate string `json:"br"`
Band string `json:"band"`
SSID string `json:"ssid"`
Hostname string `json:"hostname"`
MACAddr string `json:"mac"`
RSSI string `json:"rssi"`
PhyMode string `json:"pm"`
Channel string `json:"ch"`
Bandwidth string `json:"bw"`
AID json.RawMessage `json:"aid"`
Index int `json:"index"`
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal WiFiClientEntry %q: %w", string(b), err)
}
if len(raw.AID) > 0 {
// 'aid' may be a string or an integer - unwrap first if it's a string
if raw.AID[0] == '"' {
raw.AID = raw.AID[1 : len(raw.AID)-1]
}
_ = json.Unmarshal(raw.AID, &s.AID)
}
s.Index = raw.Index
s.Band = raw.Band
s.SSID = raw.SSID
s.Hostname = raw.Hostname
s.MACAddr, _ = net.ParseMAC(raw.MACAddr)
s.RSSI, _ = strconv.Atoi(raw.RSSI)
s.PhyMode = raw.PhyMode
s.Channel, _ = strconv.Atoi(raw.Channel)
if strings.HasSuffix(raw.Bandwidth, "MHz") {
s.Bandwidth = atoi64(raw.Bandwidth[0 : len(raw.Bandwidth)-3])
s.Bandwidth *= 1_000_000
}
if strings.HasSuffix(raw.DataRate, "M") {
s.DataRate = atoi64(raw.DataRate[0 : len(raw.DataRate)-1])
// track as bits per second (value was in mebibits/sec)
s.DataRate *= mib
}
return nil
}