From 62eb9eb0815fbc3adba7739083ae5dc40e760301 Mon Sep 17 00:00:00 2001 From: Gwendolyn Date: Fri, 1 Dec 2023 19:13:12 +0100 Subject: [PATCH] includes for generated c types --- src/c3nav/mesh/baseformats.py | 5 ++++- src/c3nav/mesh/dataformats.py | 2 +- src/c3nav/mesh/management/commands/generate_c_types.py | 10 ++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/c3nav/mesh/baseformats.py b/src/c3nav/mesh/baseformats.py index 78780063..6ed88de4 100644 --- a/src/c3nav/mesh/baseformats.py +++ b/src/c3nav/mesh/baseformats.py @@ -355,10 +355,13 @@ class StructType: _union_options = {} union_type_field = None existing_c_struct = None + c_includes = set() # noinspection PyMethodOverriding - def __init_subclass__(cls, /, union_type_field=None, existing_c_struct=None, **kwargs): + def __init_subclass__(cls, /, union_type_field=None, existing_c_struct=None, c_includes=None, **kwargs): cls.union_type_field = union_type_field + if c_includes is not None: + cls.c_includes |= set(c_includes) if cls.existing_c_struct is not None: # TODO: can we make it possible? does it even make sense? raise TypeError('subclassing an external c struct is not possible') diff --git a/src/c3nav/mesh/dataformats.py b/src/c3nav/mesh/dataformats.py index f2127d9e..8e236e08 100644 --- a/src/c3nav/mesh/dataformats.py +++ b/src/c3nav/mesh/dataformats.py @@ -158,7 +158,7 @@ class RawFTMEntry(StructType): @dataclass -class FirmwareAppDescription(StructType, existing_c_struct="esp_app_desc_t"): +class FirmwareAppDescription(StructType, existing_c_struct="esp_app_desc_t", c_includes=['']): magic_word: int = field(metadata={"format": SimpleConstFormat('I', 0xAB_CD_54_32)}, repr=False) secure_version: int = field(metadata={"format": SimpleFormat('I')}) reserv1: list[int] = field(metadata={"format": SimpleFormat('2I')}, repr=False) diff --git a/src/c3nav/mesh/management/commands/generate_c_types.py b/src/c3nav/mesh/management/commands/generate_c_types.py index 9d36b694..0653ade4 100644 --- a/src/c3nav/mesh/management/commands/generate_c_types.py +++ b/src/c3nav/mesh/management/commands/generate_c_types.py @@ -2,7 +2,7 @@ from dataclasses import fields from django.core.management.base import BaseCommand -from c3nav.mesh.baseformats import normalize_name +from c3nav.mesh.baseformats import normalize_name, StructType from c3nav.mesh.messages import MeshMessage from c3nav.mesh.utils import indent_c @@ -18,13 +18,19 @@ class Command(BaseCommand): struct_max_sizes = [] done_definitions = set() + for include in StructType.c_includes: + print(f'#include {include}') + ignore_names = set(field_.name for field_ in fields(MeshMessage)) for msg_type, msg_class in MeshMessage.get_types().items(): if msg_class.c_struct_name: if msg_class.c_struct_name in done_struct_names: continue done_struct_names.add(msg_class.c_struct_name) - msg_class = MeshMessage.c_structs[msg_class.c_struct_name] + if MeshMessage.c_structs[msg_class.c_struct_name] != msg_class: + # the purpose of MeshMessage.c_structs is unclear, currently this never triggers + # todo get rid of the whole c_structs thing if it doesn't turn out to be useful for anything + raise ValueError('what happened?') base_name = (msg_class.c_struct_name or normalize_name( getattr(msg_type, 'name', msg_class.__name__)