Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/Code_generation/JSON_ADE
29 changes: 22 additions & 7 deletions Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand All @@ -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<uint16_t>({{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<uint16_t>({{packet.id}}){% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %});
}

{% endfor -%}
#endif
Comment on lines +20 to +28

@victor-Lopez25 victor-Lopez25 Jun 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making this in a single #ifdef block you can inside each function put an #ifdef block to do this:

#ifdef STLIB_ETH
  {{packet.name}}_packet = new StackPacket(static_cast<uint16_t>({{packet.id}}){% if packet.variables %}, {% for variable in packet.variables %}&{{variable.name}}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %});
#else
  {% for variable in packet.variables %}(void){{variable.name}};{% endfor %}
#endif

This way the program using this api won't have to change if it is not using ethernet


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");
Expand All @@ -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
}


Expand Down
6 changes: 4 additions & 2 deletions Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OrderPackets{
{% endfor %}

OrderPackets() = default;

#ifdef STLIB_ETH
{% for packet in packets -%}
inline static HeapOrder *{{packet.name}}_order{nullptr};
{% endfor %}
Expand All @@ -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");
Expand All @@ -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:
Expand Down
50 changes: 37 additions & 13 deletions Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -18,14 +19,19 @@ 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:
Expand Down Expand Up @@ -95,13 +101,16 @@ 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"]
Expand All @@ -120,29 +129,44 @@ 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,
"type": sock_type,
"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(
{
"name": name,
"type": sock_type,
"board_ip": self.board_ip,
"port": sock["port"],
"remote_ip": remote_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,
}
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import os
import jinja2
import sys

templates_path = "Core/Inc/Code_generation/Packet_generation"

Expand All @@ -19,8 +18,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

Expand Down
2 changes: 1 addition & 1 deletion deps/ST-LIB
Loading