Skip to content

Commit 730f0fd

Browse files
committed
Oper/Cli: Add operational data for accesspoint status
1 parent 300c4e6 commit 730f0fd

File tree

2 files changed

+90
-38
lines changed

2 files changed

+90
-38
lines changed

src/statd/python/cli_pretty/cli_pretty.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class PadWifiScan:
8787
encryption = 30
8888
signal = 9
8989

90+
class PadWifiStations:
91+
mac = 20
92+
signal = 9
93+
9094

9195
class PadLldp:
9296
interface = 16
@@ -651,13 +655,30 @@ def pr_proto_loopack(self, pipe=''):
651655
row = self._pr_proto_common("loopback", False, pipe);
652656
print(row)
653657

658+
def pr_wifi_stations(self):
659+
hdr = "\nCONNECTED STATIONS"
660+
print(Decore.invert(hdr))
661+
hdr = (f"{'MAC':<{PadWifiStations.mac}}"
662+
f"{'SIGNAL':<{PadWifiStations.signal}}"
663+
)
664+
print(Decore.invert(hdr))
665+
666+
stations=self.wifi.get("connected-stations", {})
667+
for station in stations:
668+
status=rssi_to_status(station["rssi"])
669+
row = f"{station['mac']:<{PadWifiStations.mac}}"
670+
row += f"{status:<{PadWifiStations.signal}}"
671+
print(row)
672+
654673
def pr_wifi_ssids(self):
674+
hdr = "\nSCAN RESULTS"
675+
print(Decore.invert(hdr))
655676
hdr = (f"{'SSID':<{PadWifiScan.ssid}}"
656677
f"{'ENCRYPTION':<{PadWifiScan.encryption}}"
657678
f"{'SIGNAL':<{PadWifiScan.signal}}"
658679
)
659-
660680
print(Decore.invert(hdr))
681+
661682
results=self.wifi.get("scan-results", {})
662683
for result in results:
663684
encstr = ",".join(result["encryption"])
@@ -676,14 +697,16 @@ def pr_proto_wifi(self, pipe=''):
676697
rssi = None
677698
status_str=""
678699
if self.wifi:
679-
rssi=self.wifi.get("active-rssi")
680-
ssid=self.wifi.get("active-ssid")
681-
else:
682-
signal=rssi_to_status(rssi)
683-
684-
if ssid is not None:
685-
status_str = f"ssid: {ssid}, signal: {signal}"
686-
700+
if self.wifi.get("mode", "") == "client":
701+
ssid=self.wifi.get("active-ssid")
702+
if ssid is not None:
703+
rssi=self.wifi.get("active-rssi")
704+
signal=rssi_to_status(rssi)
705+
706+
status_str = f"ssid: {ssid}, signal: {signal}"
707+
elif self.wifi.get("mode", "") == "accesspoint":
708+
stations=self.wifi.get("connected-stations", {})
709+
status_str = f"Connected stations: {len(stations)}"
687710
row = f"{'':<{Pad.iface}}"
688711
row += f"{'wifi':<{Pad.proto}}"
689712
row += f"{'':<{Pad.state}}{status_str}"
@@ -944,15 +967,6 @@ def pr_iface(self):
944967
else:
945968
print(f"{'ipv6 addresses':<{20}}:")
946969

947-
if self.wifi:
948-
ssid=self.wifi.get('active-ssid')
949-
rssi=self.wifi.get('active-rssi')
950-
if ssid is not None:
951-
print(f"{'SSID':<{20}}: {ssid}")
952-
print(f"{'Signal':<{20}}: {rssi}")
953-
print("")
954-
self.pr_wifi_ssids()
955-
956970
if self.gre:
957971
print(f"{'local address':<{20}}: {self.gre['local']}")
958972
print(f"{'remote address':<{20}}: {self.gre['remote']}")
@@ -973,6 +987,18 @@ def pr_iface(self):
973987
for key, val in frame.items():
974988
key = remove_yang_prefix(key)
975989
print(f"eth-{key:<{25}}: {val}")
990+
if self.wifi:
991+
ssid=self.wifi.get('active-ssid', "")
992+
rssi=self.wifi.get('active-rssi', "")
993+
mode=self.wifi.get('mode')
994+
if mode == "client":
995+
print(f"{'SSID':<{20}}: {ssid}")
996+
print(f"{'Signal':<{20}}: {rssi}")
997+
print("")
998+
self.pr_wifi_ssids()
999+
if mode == "accesspoint":
1000+
self.pr_wifi_stations()
1001+
9761002

9771003
def pr_mdb(self, bridge):
9781004
for group in self.br_mdb.get("multicast-filter", {}):

src/statd/python/yanger/ietf_interfaces/wifi.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,54 @@
33
import re
44

55
def wifi(ifname):
6-
data=HOST.run(tuple(f"wpa_cli -i {ifname} status".split()), default="")
6+
iw_data=HOST.run(tuple(f"iw dev {ifname} info".split()), default="")
77
wifi_data={}
88

9-
if data != "":
10-
for line in data.splitlines():
11-
k,v = line.split("=")
12-
if k == "ssid":
13-
wifi_data["active-ssid"] = v
14-
15-
data=HOST.run(tuple(f"wpa_cli -i {ifname} signal_poll".split()), default="FAIL")
16-
17-
# signal_poll return FAIL not connected
18-
if data.strip() != "FAIL":
19-
for line in data.splitlines():
20-
k,v = line.strip().split("=")
21-
if k == "RSSI":
22-
wifi_data["active-rssi"]=int(v)
23-
data=HOST.run(tuple(f"wpa_cli -i {ifname} scan_result".split()), default="FAIL")
24-
25-
if data != "FAIL":
26-
wifi_data["scan-results"] = parse_wpa_scan_result(data)
27-
9+
if iw_data != "":
10+
for line in iw_data.splitlines():
11+
line=line.strip() # Fix crazy output from iw.
12+
l=line.split(" ")
13+
if l[0] == "type":
14+
if l[1] == "AP":
15+
wifi_data["mode"] = "accesspoint"
16+
else:
17+
wifi_data["mode"] = "client"
18+
19+
if wifi_data["mode"] == "client":
20+
client_data=HOST.run(tuple(f"wpa_cli -i {ifname} status".split()), default="")
21+
if client_data != "":
22+
for line in client_data.splitlines():
23+
k,v = line.split("=")
24+
if k == "ssid":
25+
wifi_data["active-ssid"] = v
26+
27+
data=HOST.run(tuple(f"wpa_cli -i {ifname} signal_poll".split()), default="FAIL")
28+
29+
# signal_poll return FAIL not connected
30+
if data.strip() != "FAIL":
31+
for line in data.splitlines():
32+
k,v = line.strip().split("=")
33+
if k == "RSSI":
34+
wifi_data["active-rssi"]=int(v)
35+
data=HOST.run(tuple(f"wpa_cli -i {ifname} scan_result".split()), default="FAIL")
36+
if data != "FAIL":
37+
wifi_data["scan-results"] = parse_wpa_scan_result(data)
38+
elif wifi_data["mode"] == "accesspoint":
39+
ap_data=HOST.run(tuple(f"hostapd_cli -i {ifname} list_sta".split()), default="")
40+
if ap_data != "":
41+
stations=[]
42+
for mac in ap_data.splitlines():
43+
station = {}
44+
status=HOST.run(tuple(f"hostapd_cli -i {ifname} sta {mac}".split()), default="")
45+
if status != "":
46+
for line in status.splitlines()[1:]:
47+
k,v = line.split("=")
48+
if k == "signal":
49+
station["rssi"] = int(v)
50+
station["mac"] = mac
51+
stations.append(station)
52+
53+
wifi_data["connected-stations"] = stations
2854
return wifi_data
2955

3056

0 commit comments

Comments
 (0)