Skip to content

Commit

Permalink
various networking fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NicK4rT committed Dec 19, 2024
1 parent 97e235b commit 624145a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 49 deletions.
10 changes: 9 additions & 1 deletion software/networking/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def add_peer(self, peer_mac, peer_config=None, channel=None, ifidx=None):
self._peers[peer_mac].update({'ifidx': ifidx})
if peer_config is not None:
self._peers[peer_mac].update(peer_config)
self._peers[peer_mac].update({'rssi': None, 'timestamp': None})
self._peers[peer_mac].update({'rssi': None, 'time': None, 'last_ping': 0})
self.master.dprint(f"Peer {peer_mac} added with channel {channel}, ifidx {ifidx} and name {self.peer_name(peer_mac)}")
except OSError as e:
self.master.eprint(f"Error adding peer {peer_mac}: {e}")
Expand Down Expand Up @@ -277,6 +277,14 @@ def ping(self, mac, channel=None, ifidx=None): #Ping
else:
send_channel = self.master.sta.channel()
self.send_command(0x01, 0x10, mac, [send_channel, self.ifidx, self.master.config], channel, ifidx) # sends channel, ifidx and name
if isinstance(mac, list):
for key in mac:
self._peers[key].update({'last_ping': time.ticks_ms()})
elif mac == b'\xff\xff\xff\xff\xff\xff':
for key in self._peers:
self._peers[key].update({'last_ping': time.ticks_ms()})
else:
self._peers[mac].update({'last_ping': time.ticks_ms()})

def boop(self, mac, channel=None, ifidx=None): #"RSSI/Status/Config-Boop"
self.master.dprint("aen.boop")
Expand Down
86 changes: 52 additions & 34 deletions software/networking/ssp_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def peers(self):

def wpeers(self):
self.networking.iprint(f"time.ticks_ms(): {time.ticks_ms()}")
networking_peer_info = f"networking_peers_info_start{self.networking.aen.peers()}networking_peers_info_end"
original_dict = self.networking.aen.peers()
#decoded_dict = {ubinascii.hexlify(key, ':').decode(): value for key, value in original_dict.items()}
networking_peer_info = f"networking_peers_info_start{original_dict}networking_peers_info_end"
print(networking_peer_info)

def irq(self, func):
Expand All @@ -58,10 +60,13 @@ def send_command(self, msg_subkey, mac, payload=None, channel=None, ifidx=None,
else:
payload = [payload, "sudo"]
if (msg_key := "cmd") and msg_subkey in msg_subcodes[msg_key]:
self.networking.iprint(f"Sending {msg_subkey} ({bytes([msg_subcodes[msg_key][msg_subkey]])}) command to {mac} ({self.master.networking.aen.peer_name(mac)})")
if isinstance(mac, list):
self.networking.iprint(f"Sending {msg_subkey} ({bytes([msg_subcodes[msg_key][msg_subkey]])}) command to {mac}")
else:
self.networking.iprint(f"Sending {msg_subkey} ({bytes([msg_subcodes[msg_key][msg_subkey]])}) command to {mac} ({self.networking.aen.peer_name(mac)})")
self.networking.aen.send_command(msg_codes[msg_key], msg_subcodes[msg_key][msg_subkey], mac, payload, channel, ifidx)
else:
self.master.iprint(f"Command {msg_subkey} not found")
self.networking.iprint(f"Command {msg_subkey} not found")
gc.collect()

def ping(self, mac, channel=None, ifidx=None):
Expand All @@ -74,7 +79,7 @@ def echo(self, mac, message, channel=None, ifidx=None):

def boop(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.boop")
self.networking.aen.boop(mac, channel, ifidx, sudo)
self.networking.aen.boop(mac, channel, ifidx)

def send(self, mac, message, channel=None, ifidx=None):
self.networking.dprint("net.cmd.message")
Expand All @@ -85,91 +90,99 @@ def broadcast(self, message, channel=None, ifidx=None):
mac = b'\xff\xff\xff\xff\xff\xff'
self.send(mac, message, channel, ifidx)

def send_data(self, mac, message, channel=None,ifidx=None): # message is a dict, key is the sensor type and the value is the sensor value
def send_data(self, mac, message, channel=None, ifidx=None): # message is a dict, key is the sensor type and the value is the sensor value
self.networking.dprint("net.cmd.message")
self.networking.aen.send_data(mac, message, channel, ifidx)

def reboot(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.reboot")
self.networking.aen.send_command("Reboot", mac, None, channel, ifidx, sudo)
self.send_command("Reboot", mac, None, channel, ifidx, sudo)

def send_configure(self, mac, configuration, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.send_configure")
self.send_command("Send-Configure", mac, configuration, channel, ifidx, sudo)

def receive_configure(self, mac, configuration, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.send_configure")
self.send_command("Receive-Configure", mac, configuration, channel, ifidx, sudo)

def firmware_update(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.firmware_update")
self.networking.aen.send_command("Firmware-Update", mac, None, channel, ifidx, sudo)
self.send_command("Firmware-Update", mac, None, channel, ifidx, sudo)

def file_update(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.file_update")
self.networking.aen.send_command("File-Update", mac, None, channel, ifidx, sudo)
self.send_command("File-Update", mac, None, channel, ifidx, sudo)

def file_download(self, mac, link, file_list=None, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.file_download")
self.networking.aen.send_command("File-Download", mac, [link, file_list], channel, ifidx, sudo)
self.send_command("File-Download", mac, [link, file_list], channel, ifidx, sudo)

def web_repl(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.web_repl")
self.networking.ap.set_ap(ap_name := self.networking.config["name"], password := networking_keys["default_ap_key"])
self.networking.aen.send_command("Web-Repl", mac, [ap_name, password], channel, ifidx, sudo)
self.send_command("Web-Repl", mac, [ap_name, password], channel, ifidx, sudo)
# await success message and if success False disable AP or try again

def file_run(self, mac, filename, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.file_run")
self.networking.aen.send_command("File-Run", mac, filename, channel, ifidx, sudo)
self.send_command("File-Run", mac, filename, channel, ifidx, sudo)

def admin_set(self, mac, new_bool, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.admin_set")
self.networking.aen.send_command("Admin-Set", mac, new_bool, channel, ifidx, sudo)
self.send_command("Admin-Set", mac, new_bool, channel, ifidx, sudo)

def whitelist_add(self, mac, mac_list=None, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.whitelist_add")
if mac_list is not None:
mac_list = [self.networking.sta.mac_decoded, self.networking.ap.mac_decoded]
self.networking.aen.send_command("Whitelist-Add", mac, mac_list, channel, ifidx, sudo)
self.send_command("Whitelist-Add", mac, mac_list, channel, ifidx, sudo)

def config_change(self, mac, new_config, hardcode=False, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.config_change")
self.networking.aen.send_command("Config-Change", mac, [new_config, hardcode], channel, ifidx, sudo)
self.send_command("Config-Change", mac, [new_config, hardcode], channel, ifidx, sudo)

def name_change(self, mac, new_name, hardcode=False, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.name_change")
self.networking.aen.send_command("Name-Change", mac, [new_name, hardcode], channel, ifidx, sudo)
self.send_command("Name-Change", mac, [new_name, hardcode], channel, ifidx, sudo)

def pair(self, mac, key=networking_keys["handshake_key1"], channel=None, ifidx=None):
self.networking.dprint("net.cmd.pair")
self.networking.aen.send_command("Pair", mac, key, channel, ifidx)
self.send_command("Pair", mac, key, channel, ifidx)

def pair_enable(self, mac, pair_bool, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.pair")
self.networking.aen.send_command("Set-Pair", mac, pair_bool, channel, ifidx, sudo)
self.send_command("Set-Pair", mac, pair_bool, channel, ifidx, sudo)

def directory_get(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.directory_get")
self.networking.aen.send_command("Directory-Get", mac, None, channel, ifidx, sudo)
self.send_command("Directory-Get", mac, None, channel, ifidx, sudo)

# resend cmd

def wifi_connect(self, mac, name, password, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.wifi_connect")
self.networking.aen.send_command("Wifi-Connect", mac, [name, password], channel, ifidx, sudo)
self.send_command("Wifi-Connect", mac, [name, password], channel, ifidx, sudo)

def wifi_disconnect(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.wifi_disconnect")
self.networking.aen.send_command("Wifi-Disconnect", mac, None, channel, ifidx, sudo)
self.send_command("Wifi-Disconnect", mac, None, channel, ifidx, sudo)

def ap_enable(self, mac, name, password, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.ap_enable")
self.networking.aen.send_command("AP-Enable", mac, [name, password], channel, ifidx, sudo)
self.send_command("AP-Enable", mac, [name, password], channel, ifidx, sudo)

def ap_disable(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.ap_disable")
self.networking.aen.send_command("AP-Disable", mac, None, channel, ifidx, sudo)
self.send_command("AP-Disable", mac, None, channel, ifidx, sudo)

def pause(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.pause")
self.networking.aen.send_command("Pause", mac, None, channel, ifidx, sudo)
self.send_command("Pause", mac, None, channel, ifidx, sudo)

def resume(self, mac, channel=None, ifidx=None, sudo=False):
self.networking.dprint("net.cmd.resume")
self.networking.aen.send_command("Resume", mac, None, channel, ifidx, sudo)
self.send_command("Resume", mac, None, channel, ifidx, sudo)


class Orders:
Expand Down Expand Up @@ -198,16 +211,24 @@ def custom_cmd_handler(self, data):
self.master.networking.dprint("net.order.custom_cmd_handler")
sender_mac, subtype, send_timestamp, receive_timestamp, payload, msg_key = data
if (msg_subkey := "Reboot") and subtype == msg_subcodes[msg_key][msg_subkey]: # Reboot
self.master.iprint(
f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
self.master.iprint(f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
if __check_authorisation(sender_mac, payload):
__send_confirmation("Confirm", sender_mac, f"{msg_subkey} ({subtype})", payload)
machine.reset()
else:
__send_confirmation("Fail", sender_mac, f"{msg_subkey} ({subtype})", payload, "Not authorised")
elif (msg_subkey := "Send-Configure") and subtype == msg_subcodes[msg_key][msg_subkey]: # Send-Configure
self.master.iprint(f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
if __check_authorisation(sender_mac, payload):
send_configuration = payload[0]
#setup the function to send the sensor data to the specified mac(s)
elif (msg_subkey := "Receive-Configure") and subtype == msg_subcodes[msg_key][msg_subkey]: # Receive-Configure
self.master.iprint(f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
if __check_authorisation(sender_mac, payload):
receive_configuration = payload[0]
# setup the function to calculate output based on the received sensor data (and own sensor data?)
elif (msg_subkey := "Firmware-Update") and subtype == msg_subcodes[msg_key][msg_subkey]: # Firmware-Update
self.master.iprint(
f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
self.master.iprint(f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
if __check_authorisation(sender_mac, payload):
try:
# Insert update logic here
Expand All @@ -219,8 +240,7 @@ def custom_cmd_handler(self, data):
else:
__send_confirmation("Fail", sender_mac, f"{msg_subkey} ({subtype})", payload, "Not authorised")
elif (msg_subkey := "File-Update") and subtype == msg_subcodes[msg_key][msg_subkey]: # File-Update
self.master.iprint(
f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
self.master.iprint(f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
if __check_authorisation(sender_mac, payload):
try:
# Insert update logic here
Expand All @@ -232,8 +252,7 @@ def custom_cmd_handler(self, data):
else:
__send_confirmation("Fail", sender_mac, f"{msg_subkey} ({subtype})", payload, "Not authorised")
elif (msg_subkey := "File-Download") and subtype == msg_subcodes[msg_key][msg_subkey]: # File-Download
self.master.iprint(
f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
self.master.iprint(f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
# should return a list with a link and the list of files to download
if __check_authorisation(sender_mac, payload):
try:
Expand All @@ -252,8 +271,7 @@ def custom_cmd_handler(self, data):
else:
__send_confirmation("Fail", sender_mac, f"{msg_subkey} ({subtype})", payload, "Not authorised")
elif (msg_subkey := "Web-Repl") and subtype == msg_subcodes[msg_key][msg_subkey]: # Web-Repl
self.master.iprint(
f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
self.master.iprint(f"{msg_subkey} ({subtype}) command received from {sender_mac} ({self.master.networking.aen.peer_name(sender_mac)})")
# should be a list with name and password
if __check_authorisation(sender_mac, payload):
try:
Expand Down
25 changes: 11 additions & 14 deletions software/networking/ssp_networking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
dbgmsg = False
errmsg = True
configuration = config["configuration"]
if configuration == "AM1":
infmsg = True

#Network
networking = SSP_Networking(infmsg, dbgmsg, errmsg)
Expand Down Expand Up @@ -105,14 +103,14 @@ def receive():
print(f"\033[31mTest rssifailed: {e}\033[0m")

try:
networking.commands.ping(peer_mac)
networking.ping(peer_mac)
print("\033[34mWaiting for pong\033[0m")
time.sleep(0.02)
print("\033[32mTest ping passed\033[0m")
except Exception as e:
print(f"\033[31mTest ping failed: {e}\033[0m")
try:
networking.commands.echo(peer_mac, message)
networking.echo(peer_mac, message)
print("\033[34mWaiting for echo\033[0m")
time.sleep(0.02)
print("\033[32mTest echo passed\033[0m")
Expand All @@ -125,50 +123,49 @@ def receive():
# print(f"\033[31mTest broadcast failed: {e}\033[0m")

try:
networking.commands.echo(peer_mac, message_str)
networking.echo(peer_mac, message_str)
print("\033[34mWaiting for echo\033[0m")
time.sleep(2)

print("\033[32mTest long string passed\033[0m")
except Exception as e:
print(f"\033[31mTest long string failed: {e}\033[0m")
try:
networking.commands.echo(peer_mac, message_int)
networking.echo(peer_mac, message_int)
print("\033[34mWaiting for echo\033[0m")
time.sleep(2)
print("\033[32mTest long int passed\033[0m")
except Exception as e:
print(f"\033[31mTest long int failed: {e}\033[0m")
try:
networking.commands.echo(peer_mac, message_float)
networking.echo(peer_mac, message_float)
print("\033[34mWaiting for echo\033[0m")
time.sleep(2)
print("\033[32mTest long float passed\033[0m")
except Exception as e:
print(f"\033[31mTest long float failed: {e}\033[0m")
try:
networking.commands.echo(peer_mac, message_dict)
networking.echo(peer_mac, message_dict)
print("\033[34mWaiting for echo\033[0m")
time.sleep(2)
print("\033[32mTest long dict passed\033[0m")
except Exception as e:
print(f"\033[31mTest long dict failed: {e}\033[0m")
try:
networking.commands.echo(peer_mac, message_list)
networking.echo(peer_mac, message_list)
print("\033[34mWaiting for echo\033[0m")
time.sleep(2)
print("\033[32mTest long list passed\033[0m")
except Exception as e:
print(f"\033[31mTest long list failed: {e}\033[0m")
try:
networking.commands.echo(peer_mac, message_bytes)
networking.echo(peer_mac, message_bytes)
print("\033[34mWaiting for echo\033[0m")
time.sleep(3)
print("\033[32mTest long bytes passed\033[0m")
except Exception as e:
print(f"\033[31mTest long bytes failed: {e}\033[0m")
try:
networking.commands.echo(peer_mac, message_bytearray)
networking.echo(peer_mac, message_bytearray)
print("\033[34mWaiting for echo\033[0m")
time.sleep(3)
print("\033[32mTest long bytearray passed\033[0m")
Expand Down Expand Up @@ -198,7 +195,7 @@ def boop(pin):
global lastPressed
if(time.ticks_ms()-lastPressed>1000):
lastPressed = time.ticks_ms()
networking.commands.send(peer_mac, message)
networking.send(peer_mac, message)
# print(f"Sent {random_bytes} to {peer_mac}")

switch_select = Pin(9, Pin.IN, Pin.PULL_UP)
Expand All @@ -211,7 +208,7 @@ def boop(pin):

while True:
print(f"{int(time.ticks_ms()-start_time)/1000}: {gc.mem_free()}")
#networking.commands.ping(peer_mac)
#networking.ping(peer_mac)
#print(f"Sent ping to {peer_mac}")
time.sleep(0.1)
if networking.check_messages():
Expand Down
2 changes: 2 additions & 0 deletions software/release/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"Directory-Get": 0x14,
"Echo": 0x15,
"Resend": 0x16,
"Send-Configure": 0x17,
"Receive-Configure": 0x18,
"WiFi-Connect": 0x21,
"WiFi-Disconnect": 0x22,
"AP-Enable": 0x23,
Expand Down

0 comments on commit 624145a

Please sign in to comment.