From d3fba38214ffc204fb02afdc74657b95c2722bfe Mon Sep 17 00:00:00 2001 From: Private Island Networks Date: Wed, 22 Apr 2026 12:00:41 -0400 Subject: mle: fix up various issues with formatting and messages so messages appear in the ML tab --- main.py | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 56db667..f29cfca 100755 --- a/main.py +++ b/main.py @@ -33,7 +33,6 @@ import platform from socket import socket, AF_INET, SOCK_DGRAM from time import sleep import json -# from websockets.asyncio.server import serve from constants import * @@ -46,7 +45,7 @@ LOG_PATH = PROJECT_ROOT + '/logs' ws_key = web.AppKey("ws_key", dict[str, web.WebSocketResponse]) -async def _parse_pkt(app, pkt): +async def _parse_ctl_pkt(app, pkt): """ Parse a Wireshark received packet""" # Drop unwanted packets @@ -84,6 +83,7 @@ async def _parse_pkt(app, pkt): port = PORT_UNDEFINED message["format"] = MSG_FORMAT_CONTROLLER + message["num_cmds"] = 1 message["time"] = pkt.frame_info.get_field("frame.time_relative").__str__()[0:6] message['port'] = port message['type'] = msg_type @@ -93,6 +93,40 @@ async def _parse_pkt(app, pkt): return message +async def _parse_mle_pkt(app, pkt): + """ Parse a Wireshark received packet""" + + # Drop unwanted packets + if not (pkt.ip.src == app['ipaddr_betsy']): + return None + + try: + data = pkt.data.data.__str__() + except: + if DEBUG: + print('non MLE msg received') + return None + + message = dict() + + try: + msg_type = MSG_TYPE_MAP[int(data[0:2],16)] + msg_token = '0x' + data[2:4] + msg_data = '0x' + data[4::] + except Exception as e: + print("mapping error:", str(e)) + pass + + # TODO: add logic to determine the source of the MLE packet + + message["format"] = MSG_FORMAT_MLE + message["num_cmds"] = 1 + message["time"] = pkt.frame_info.get_field("frame.time_relative").__str__()[0:6] + message['type'] = msg_type + message['token'] = msg_token + message['data'] = msg_data + + return message async def home(request): data = request.match_info.get('data', "") @@ -153,9 +187,9 @@ async def controller(request): int(data.get('token'),16).to_bytes(1), int(data.get('address'),16).to_bytes(2), int(data.get('data'),16).to_bytes(4)) - send_pkt = await _parse_pkt(request.app, q_cont.get(block=True)) + send_pkt = await _parse_ctl_pkt(request.app, q_cont.get(block=True)) if send_pkt['type'] == "READ": - rcv_pkt = await _parse_pkt(request.app, q_cont.get(block=True)) + rcv_pkt = await _parse_ctl_pkt(request.app, q_cont.get(block=True)) except Exception as e: print(str(e)) result = STATUS.ERROR_SERVER @@ -258,7 +292,7 @@ async def get_icon(request): raise aiohttp.web.HTTPNotFound() -# WebSocket handle +# WebSocket for MLE async def wslogger(request): ws = web.WebSocketResponse() await ws.prepare(request) # onopen is called in the browser @@ -282,8 +316,8 @@ async def wslogger(request): num_pkts = num_pkts + 1 if DEBUG: - print('get from Q:', num_pkts) - message = await _parse_pkt(request.app, pkt) + print('get from MLE Q:', num_pkts) + message = await _parse_mle_pkt(request.app, pkt) if message: await ws.send_json(message) -- cgit v1.2.3-8-gadcc