Skip to content

Commit da227ca

Browse files
committed
Pain
1 parent 17c769e commit da227ca

20 files changed

+934
-34
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# jpkg-template-package
2-
A template package for jpkg
1+
# Beryllium OS netutils
2+
A set of networking utillities for Beryllium OS.
33
<br />
44
Build with <code>make package</code>

files/Manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"JPKG_minimum_version": 2,
3-
"package_name": "package",
4-
"version": [0, 0, 0],
3+
"package_name": "netutils",
4+
"version": [1, 0, 0],
55
"dependencies": [],
66
"conflicts": [],
77
"install": "installer.py",
88
"remove": "uninstaller.py",
9-
"update": "updater.py",
9+
"update": "installer.py",
1010
"strap": "strap.py",
1111
"restart_required": false
1212
}

files/headless.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
vr("passwd", None)
2+
vr("inc", 1)
3+
if vr("args")[0] == "--passphrase":
4+
if vr("args")[1].startswith('"'):
5+
while True:
6+
if vr("args")[vr("inc")].endswith('"'):
7+
break
8+
elif vr("inc") < vr("argl"):
9+
vrp("inc")
10+
else:
11+
be.based.error(1)
12+
vrm("inc")
13+
if vr("inc") is not -1:
14+
vr("passwd", vr("args")[1] + " ")
15+
for pv[get_pid()]["i"] in range(2, vr("inc") + 1):
16+
vrp("passwd", vr("args")[vr("i")] + " ")
17+
vr("passwd", vr("passwd")[1:-2])
18+
else:
19+
vr("passwd", vr("args")[1])
20+
else:
21+
vr("inc", None)
22+
if vr("inc") is not None:
23+
vrp("inc")
24+
vr("args", vr("args")[vr("inc") :])
25+
vrm("argl", vr("inc"))
26+
27+
if vr("argl") > 2 and vr("args")[0] == "station" and vr("args")[1] == vr("device_n"):
28+
if vr("argl") > 3 and vr("args")[2] == "connect":
29+
vr("networks", be.devices["network"][0].scan())
30+
if vr("args")[3] in vr("networks"):
31+
vr("res", False)
32+
if vr("networks")[vr("args")[3]][0] != "OPEN":
33+
vr("tpd", cptoml.fetch(vr("args")[3], subtable="IWD"))
34+
if vr("passwd") is not None:
35+
be.devices["network"][0].disconnect()
36+
be.devices["network"][0].disconnect_ap()
37+
dmtex('IWD: Connecting to: "{}"'.format(vr("args")[3]))
38+
vr(
39+
"res",
40+
be.devices["network"][0].connect(vr("args")[3], vr("passwd")),
41+
)
42+
elif vr("tpd") is not None:
43+
be.devices["network"][0].disconnect()
44+
be.devices["network"][0].disconnect_ap()
45+
dmtex(
46+
'IWD: Connecting to: "{}" with stored password.'.format(
47+
vr("args")[3]
48+
)
49+
)
50+
vr(
51+
"res",
52+
be.devices["network"][0].connect(vr("args")[3], vr("tpd")),
53+
)
54+
else:
55+
term.write("Error: No password specified")
56+
else:
57+
be.devices["network"][0].disconnect()
58+
be.devices["network"][0].disconnect_ap()
59+
dmtex('IWD: Connecting to: "{}"'.format(vr("args")[3]))
60+
vr("res", be.devices["network"][0].connect(vr("args")[3]))
61+
exec(vr("wifi_connect_msg"))
62+
if (
63+
vr("res")
64+
and (
65+
vr("args")[3] not in cptoml.keys("IWD")
66+
or cptoml.fetch(vr("args")[3], subtable="IWD") != vr("passwd")
67+
)
68+
) and vr("passwd") is not None:
69+
# Store this network
70+
cptoml.put(vr("args")[3], vr("passwd"), subtable="IWD")
71+
be.api.setvar("return", str(int(not vr("res"))))
72+
else:
73+
term.write("Network not found")
74+
be.api.setvar("return", "1")
75+
elif vr("args")[2] == "ap_mode" and vr("argl") > 3:
76+
if hasattr(be.devices["network"][0], "connect_ap"):
77+
vr(
78+
"res",
79+
be.devices["network"][0].connect_ap(vr("args")[3], vr("passwd")),
80+
)
81+
exec(vr("wifi_ap_msg"))
82+
be.api.setvar("return", str(int(not vr("res"))))
83+
else:
84+
dmtex("IWD: This interface does not support AP.")
85+
elif vr("args")[2] == "auto":
86+
if not be.devices["network"][0].connected:
87+
# We don't need to run on an already connected interface
88+
vr("stored_networks", cptoml.keys("IWD"))
89+
if len(vr("stored_networks")):
90+
vr("scanned_networks", be.devices["network"][0].scan())
91+
vr("best", None) # The best network to connect to
92+
vr("best_alt", None) # An alternative, just in case.
93+
vr("best_index", None) # Rating
94+
vr("best_alt_index", None) # Rating for alt
95+
for pv[get_pid()]["i"] in vr("scanned_networks"):
96+
if vr("i") in vr("stored_networks"):
97+
if vr("best") is None: # We have no alternative
98+
vr("best", vr("i"))
99+
vr("best_index", vr("stored_networks").index(vr("i")))
100+
else: # We already have a network we can use
101+
vr(
102+
"cind", vr("stored_networks").index(vr("i"))
103+
) # To test if it's better
104+
if vr("best_index") > vr("cind"):
105+
# It's a better network
106+
vr("best_alt", vr("best"))
107+
vr("best_alt_index", vr("best_index"))
108+
vr("best", vr("i"))
109+
vr("best_index", vr("cind"))
110+
elif vr("best_alt") is None or vr("best_alt_index") > vr(
111+
"cind"
112+
):
113+
vr("best_alt", vr("i"))
114+
vr("best_alt_index", vr("cind"))
115+
if vr("best") is not None: # We can connect
116+
exec(vr("wifi_best"))
117+
if not vr("res"):
118+
if vr("best_alt") is not None:
119+
vr("best", vr("best_alt"))
120+
exec(vr("wifi_best"))
121+
else:
122+
dmtex(f"IWD: No available alternative networks. ABORT.")
123+
vr("best", None)
124+
if (
125+
vr("best") is None
126+
): # We have to create a hotspot based on toml settings.
127+
vr("apssid", cptoml.fetch("SSID", subtable="IWD-AP"))
128+
vr("appasswd", cptoml.fetch("PASSWD", subtable="IWD-AP"))
129+
if vr("apssid") is not None:
130+
vr(
131+
"res",
132+
be.devices["network"][0].connect_ap(
133+
vr("apssid"), vr("appasswd")
134+
),
135+
)
136+
exec(vr("wifi_ap_msg"))
137+
elif vr("args")[2] == "disconnect":
138+
be.devices["network"][0].disconnect()
139+
be.devices["network"][0].disconnect_ap()
140+
be.api.setvar("return", "0")
141+
else:
142+
be.based.error(1)
143+
else:
144+
be.based.error(1)

files/installer.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
print("""
2-
This script is run during the first install of the package.
3-
4-
You should copy the files you want in the system from here.
5-
You should NOT bother with the manifest or the uninstall files from here.
6-
""")
1+
for pv[get_pid()]["f"] in [
2+
"adafruit_connection_manager.mpy",
3+
"adafruit_ntp.mpy",
4+
"adafruit_requests.mpy",
5+
"telnet_console.mpy",
6+
]:
7+
be.based.run("cp " + vr("f") + " /lib/" + vr("f"))
8+
9+
be.based.run("mkdir /bin/iwctl")
10+
for pv[get_pid()]["f"] in [
11+
"headless.py",
12+
"interactive.py",
13+
"main.py",
14+
]:
15+
be.based.run("cp " + vr("f") + " /bin/iwctl/" + vr("f"))
16+
17+
for pv[get_pid()]["f"] in [
18+
"ping.lja",
19+
"ping.py",
20+
"iwctl.lja",
21+
"telnet.lja",
22+
"telnet.py",
23+
"timesync.lja",
24+
"timesync.py",
25+
]:
26+
be.based.run("cp " + vr("f") + " /bin/" + vr("f"))
27+
28+
be.api.setvar("return", "0")

0 commit comments

Comments
 (0)