From 0b5fe4ea2d4f9980e33fff497f825de0cd7a88cb Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Thu, 21 May 2026 19:07:31 +0200 Subject: [PATCH 1/3] Add round robin and Stack Packet to DataTemplate. Now DataPackets and OrderPackets work without ethernet --- .../Packet_generation/DataTemplate.hpp | 29 ++++++++++++++----- .../Packet_generation/OrderTemplate.hpp | 6 ++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp b/Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp index 81dae9d1..865bf7f7 100644 --- a/Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp +++ b/Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp @@ -3,6 +3,10 @@ /*Data packets for {{board}} -AUTOGENERATED CODE, DO NOT MODIFY-*/ + +#ifndef ARRAY_LEN +#define ARRAY_LEN(Arr) (sizeof(Arr)/sizeof(Arr[0])) +#endif class DataPackets{ public: {% for enum in enums -%} @@ -13,25 +17,35 @@ class DataPackets{ {%- endfor %} }; {% endfor %} - +#ifdef STLIB_ETH {% for packet in packets -%} static void {{packet.name}}_init({% for variable in packet.variables %}{{variable.type}} &{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}) { - {{packet.name}}_packet = new HeapPacket(static_cast({{packet.id}}){% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %}); + {{packet.name}}_packet = new StackPacket(static_cast({{packet.id}}){% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %}); } {% endfor -%} +#endif public: +#ifdef STLIB_ETH {%for packet in packets -%} - inline static HeapPacket *{{packet.name}}_packet{nullptr}; + inline static Packet *{{packet.name}}_packet{nullptr}; {% endfor %} + + {% for group in sending_packets -%} + inline static uint32_t group_{{loop.index0}}_idx = 0; + inline static Packet **group_{{loop.index0}}[] = { {% for packet in group.packets %}&{{packet.name}}_packet{% if not loop.last %}, {% endif %}{% endfor %} }; + {% endfor %} + {% for socket in DatagramSockets -%} inline static {{socket.type}} *{{socket.name}}{nullptr}; {% endfor %} +#endif static void start() { +#ifdef STLIB_ETH {% for packet in packets -%} if ({{packet.name}}_packet == nullptr) { PANIC("Packet {{packet.name}} not initialized"); @@ -43,12 +57,13 @@ class DataPackets{ {% endfor %} {%- for group in sending_packets %} - Scheduler::register_task({% if group.period_type == "ms" %}{{ (group.period*1000)|round|int }}{% else %}{{ group.period|round|int }}{% endif %}, +[](){ - {% for packet in group.packets -%} - DataPackets::{{packet.socket}}->send_packet(*DataPackets::{{packet.name}}_packet); - {% endfor -%} + Scheduler::register_task({% if group.period_type == "ms" %}{{ (group.period*1000)|round|int }}{% else %}{{ group.period|round|int }}{% endif %} / ARRAY_LEN(DataPackets::group_{{loop.index0}}), +[](){ + Packet *packet = *DataPackets::group_{{loop.index0}}[DataPackets::group_{{loop.index0}}_idx]; + DataPackets::{{group.packets[0].socket}}->send_packet(*packet); + DataPackets::group_{{loop.index0}}_idx = (DataPackets::group_{{loop.index0}}_idx + 1) % ARRAY_LEN(DataPackets::group_{{loop.index0}}); }); {%- endfor %} +#endif } diff --git a/Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp b/Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp index 6911008e..122b087a 100644 --- a/Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp +++ b/Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp @@ -21,7 +21,7 @@ class OrderPackets{ {% endfor %} OrderPackets() = default; - +#ifdef STLIB_ETH {% for packet in packets -%} inline static HeapOrder *{{packet.name}}_order{nullptr}; {% endfor %} @@ -39,9 +39,10 @@ class OrderPackets{ {% for socket in ServerSockets -%} inline static {{socket.type}} *{{socket.name}}{nullptr}; {% endfor %} - +#endif static void start() { +#ifdef STLIB_ETH {% for packet in packets -%} if ({{packet.name}}_order == nullptr) { PANIC("Order {{packet.name}} not initialized"); @@ -54,6 +55,7 @@ class OrderPackets{ {% for socket in Sockets -%} {{socket.name}} = new Socket("{{socket.board_ip}}",{{socket.local_port}},"{{socket.remote_ip}}",{{socket.remote_port}}); {% endfor %} +#endif } private: From c5c6e80faeb8b081bc0a8298630d7dfd79db2d1d Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Mon, 8 Jun 2026 11:16:40 +0200 Subject: [PATCH 2/3] Remote ports can now be labeled as backend. This will make it so that you use the general_info.json ports. Also better exceptions for the code :p --- CMakeLists.txt | 2 +- Core/Inc/Code_generation/JSON_ADE | 2 +- .../Packet_generation/Packet_descriptions.py | 38 ++++++++++++++----- .../Packet_generation/Packet_generation.py | 3 +- deps/ST-LIB | 2 +- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00c73cbc..ecfbb8d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ include(CTest) enable_testing() set(EXECUTABLE ${PROJECT_NAME}.elf) -set(BOARD_NAME "TEST" CACHE STRING "Board key from Core/Inc/Code_generation/JSON_ADE/boards.json") +set(BOARD_NAME "VCU" CACHE STRING "Board key from Core/Inc/Code_generation/JSON_ADE/boards.json" FORCE) if(BOARD_NAME STREQUAL "") message(FATAL_ERROR "BOARD_NAME cannot be empty") endif() diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index d0f7d80d..3045bdda 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit d0f7d80d726bc9e6d5f4e1d13f3a120d63fedec4 +Subproject commit 3045bdda611d4e03a364eba6ecd4ba6f0af8c42c diff --git a/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py b/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py index f7e24836..20d2cd8e 100644 --- a/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py +++ b/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py @@ -10,6 +10,7 @@ def __init__(self, name: str, board: dict, JSONpath: str): # Load backend IP from general_info.json backend_ip = "0.0.0.0" + backend_ports = {} try: with open(JSONpath + "/general_info.json") as f: general_info = json.load(f) @@ -18,14 +19,17 @@ def __init__(self, name: str, board: dict, JSONpath: str): and "backend" in general_info["addresses"] ): backend_ip = general_info["addresses"]["backend"] + if "ports" in general_info: + backend_ports = general_info["ports"] except Exception as e: - print(f"Warning: Could not load backend IP from general_info.json: {e}") + msg = f"Warning: Could not load backend IP and ports from general_info.json: {e}" + raise Exception(msg) # Sockets: try: with open(JSONpath + "/boards/" + name + "/sockets.json") as s: socks = json.load(s) - self.sockets = self.SocketsDescription(socks, self.ip, backend_ip) + self.sockets = self.SocketsDescription(socks, self.ip, backend_ip, backend_ports) except Exception as e: raise Exception(f"Error in file {JSONpath}/boards/{name}/sockets.json: {e}") # Packets: @@ -95,13 +99,14 @@ def fix_sendind_packets(sending_packets: list): return fixed_packets class SocketsDescription: - def __init__(self, sockets: list, board_ip: str, backend_ip: str): + def __init__(self, sockets: list, board_ip: str, backend_ip: str, backend_ports: dict): self.allSockets = [] self.ServerSockets = [] self.Sockets = [] self.DatagramSockets = [] self.board_ip = board_ip self.backend_ip = backend_ip + self.backend_ports = backend_ports for sock in sockets: name = sock["name"].replace(" ", "_").replace("-", "_") sock_type = sock["type"] @@ -120,6 +125,11 @@ def __init__(self, sockets: list, board_ip: str, backend_ip: str): remote_ip = sock["remote_ip"] if remote_ip == "backend": remote_ip = self.backend_ip + + remote_port = sock["remote_port"] + if remote_port == "backend": + remote_port = self.backend_ports.get("TCP_SERVER", remote_port) + self.Sockets.append( { "name": name, @@ -127,22 +137,32 @@ def __init__(self, sockets: list, board_ip: str, backend_ip: str): "board_ip": self.board_ip, "local_port": sock["local_port"], "remote_ip": remote_ip, - "remote_port": sock["remote_port"], + "remote_port": remote_port, } ) elif sock_type == "DatagramSocket": remote_ip = sock["remote_ip"] if remote_ip == "backend": remote_ip = self.backend_ip - self.DatagramSockets.append( - { + + port = sock["port"] + if port == "backend": + port = self.backend_ports.get("UDP", port) + + entry = { "name": name, "type": sock_type, "board_ip": self.board_ip, - "port": sock["port"], + "port": port, "remote_ip": remote_ip, - } - ) + } + if "remote_port" in sock: + remote_port = sock["remote_port"] + if remote_port == "backend": + remote_port = self.backend_ports.get("UDP", remote_port) + entry["remote_port"] = remote_port + + self.DatagramSockets.append(entry) class PacketDescription: diff --git a/Core/Inc/Code_generation/Packet_generation/Packet_generation.py b/Core/Inc/Code_generation/Packet_generation/Packet_generation.py index 68a38a56..08f25099 100644 --- a/Core/Inc/Code_generation/Packet_generation/Packet_generation.py +++ b/Core/Inc/Code_generation/Packet_generation/Packet_generation.py @@ -19,8 +19,7 @@ def Generate_PacketDescription(JSONpath: str, board: str): board_instance = BoardDescription(board, b, JSONpath) globals()[board] = board_instance else: - print(f"Board {board} not found, exiting...") - sys.exit() + raise Exception(f"Board {board} not found, exiting...") return boards_name diff --git a/deps/ST-LIB b/deps/ST-LIB index 621214e5..fe4f403e 160000 --- a/deps/ST-LIB +++ b/deps/ST-LIB @@ -1 +1 @@ -Subproject commit 621214e51ef210a5516c2a1816bf7ceff8595856 +Subproject commit fe4f403e3c9b05f936a7837722be8a8e8ee41195 From 330e8db309d3d7bb660ff08cf9912ebdd8e7710c Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Mon, 8 Jun 2026 11:18:25 +0200 Subject: [PATCH 3/3] Format fixes --- .../Packet_generation/Packet_descriptions.py | 24 +++++++++++-------- .../Packet_generation/Packet_generation.py | 1 - 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py b/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py index 20d2cd8e..bae3d242 100644 --- a/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py +++ b/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py @@ -29,7 +29,9 @@ def __init__(self, name: str, board: dict, JSONpath: str): try: with open(JSONpath + "/boards/" + name + "/sockets.json") as s: socks = json.load(s) - self.sockets = self.SocketsDescription(socks, self.ip, backend_ip, backend_ports) + self.sockets = self.SocketsDescription( + socks, self.ip, backend_ip, backend_ports + ) except Exception as e: raise Exception(f"Error in file {JSONpath}/boards/{name}/sockets.json: {e}") # Packets: @@ -99,7 +101,9 @@ def fix_sendind_packets(sending_packets: list): return fixed_packets class SocketsDescription: - def __init__(self, sockets: list, board_ip: str, backend_ip: str, backend_ports: dict): + def __init__( + self, sockets: list, board_ip: str, backend_ip: str, backend_ports: dict + ): self.allSockets = [] self.ServerSockets = [] self.Sockets = [] @@ -125,7 +129,7 @@ def __init__(self, sockets: list, board_ip: str, backend_ip: str, backend_ports: remote_ip = sock["remote_ip"] if remote_ip == "backend": remote_ip = self.backend_ip - + remote_port = sock["remote_port"] if remote_port == "backend": remote_port = self.backend_ports.get("TCP_SERVER", remote_port) @@ -144,24 +148,24 @@ def __init__(self, sockets: list, board_ip: str, backend_ip: str, backend_ports: remote_ip = sock["remote_ip"] if remote_ip == "backend": remote_ip = self.backend_ip - + port = sock["port"] if port == "backend": port = self.backend_ports.get("UDP", port) entry = { - "name": name, - "type": sock_type, - "board_ip": self.board_ip, - "port": port, - "remote_ip": remote_ip, + "name": name, + "type": sock_type, + "board_ip": self.board_ip, + "port": port, + "remote_ip": remote_ip, } if "remote_port" in sock: remote_port = sock["remote_port"] if remote_port == "backend": remote_port = self.backend_ports.get("UDP", remote_port) entry["remote_port"] = remote_port - + self.DatagramSockets.append(entry) diff --git a/Core/Inc/Code_generation/Packet_generation/Packet_generation.py b/Core/Inc/Code_generation/Packet_generation/Packet_generation.py index 08f25099..12a59988 100644 --- a/Core/Inc/Code_generation/Packet_generation/Packet_generation.py +++ b/Core/Inc/Code_generation/Packet_generation/Packet_generation.py @@ -2,7 +2,6 @@ import json import os import jinja2 -import sys templates_path = "Core/Inc/Code_generation/Packet_generation"