Code generation method and device for vehicle-mounted CAN network, equipment and medium

By using a declarative configuration file in JSON format and preset JSON Schema validation, combined with a template engine to generate target C code, the problem of low efficiency and error-proneness in generating code for vehicle CAN networks is solved, achieving efficient and reliable code generation and integration.

CN122018890APending Publication Date: 2026-05-12HANGZHOU MAITANG TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
HANGZHOU MAITANG TECH CO LTD
Filing Date
2026-02-13
Publication Date
2026-05-12

AI Technical Summary

Technical Problem

Existing technologies are inefficient, error-prone, unable to fully cover functions, lack flexibility, cannot support custom functions, cannot adapt to multi-bus scenarios, and are difficult to integrate.

Method used

It adopts a declarative configuration file in JSON format, uses a preset JSON Schema file for validation, generates target C code through parsing and template engine, and populates the initial C code file to realize the preset processing operations of general gateway firmware.

Benefits of technology

It improves the efficiency and reliability of vehicle CAN network code generation, realizes automated code generation and integration, and enhances software reusability and deployment flexibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122018890A_ABST
    Figure CN122018890A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method and device for a vehicle-mounted CAN network, equipment and a medium, and relates to the technical field of embedded communication, and the method comprises the steps: determining a declarative configuration file which is in a JSON format and is used for defining the configuration information of a CAN gateway in a target vehicle, and verifying the declarative configuration file through a JSON Schema file, and obtaining a verification result; when the verification result shows that the verification is passed, analyzing the declarative configuration file, and generating a target code by using the obtained target data and a preset template engine; and filling the target code into the initial C code file to obtain a target C code file suitable for the vehicle-mounted CAN network, so that after the general gateway firmware of the CAN gateway receives the CAN message, the target C code file is utilized to drive the general gateway firmware to trigger a preset processing operation for the CAN message. According to the invention, an efficient and reliable code generation scheme for the vehicle-mounted CAN network can be designed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of embedded communication technology, and in particular to a code generation method, apparatus, device, and medium for vehicle-mounted CAN networks. Background Technology

[0002] In modern automotive electronic and electrical architecture, the CAN (Controller Area Network) bus is widely used to connect various electronic control units (ECUs). With the development of vehicle intelligence, the number of ECUs has surged, forming multi-domain CAN sub-networks. The CAN gateway, as a core component, is responsible for message routing and interaction between sub-networks, and its functional logic is defined by the communication matrix.

[0003] Traditional CAN gateway software development relies on manual interpretation of the communication matrix and manual coding to implement functions such as message sending and receiving, signal packing and unpacking, and routing. This process is tedious and error-prone, and repeated modifications and tests are required when the communication matrix changes frequently, resulting in extremely low efficiency. Existing improved solutions use commercial tools combined with DBC (Database CAN) files to generate partial code, but still have significant drawbacks: incomplete functional coverage, only generating signal packing and unpacking functions, while core functions such as gateway routing logic and message filtering still require manual coding; insufficient flexibility, as the fixed DBC format cannot support custom functions such as IDS (Intrusion Detection System) parameter configuration and loop code verification, and configuration information is scattered and difficult to manage; limited by the format and tied to the tool ecosystem, it cannot adapt to multi-bus scenarios such as Ethernet, the generated code is prone to incompatibility with the existing project architecture, integration is difficult, and the separation of configuration and code further increases the development and maintenance burden.

[0004] Therefore, designing an efficient and reliable code generation scheme for vehicle-mounted CAN networks is a technical problem that urgently needs to be solved. Summary of the Invention

[0005] In view of this, the purpose of this invention is to provide a code generation method, apparatus, device, and medium for vehicle-mounted CAN networks, which can improve the efficiency and reliability of code generation for vehicle-mounted CAN networks. The specific solution is as follows: Firstly, this application provides a code generation method for in-vehicle CAN networks, including: A declarative configuration file in JSON format is determined, which is used to define the configuration information of the CAN gateway in the target vehicle. The declarative configuration file is then validated using a preset JSON Schema file to obtain the validation result. If the verification result shows that the verification is successful, the declarative configuration file is parsed, and the target code is generated using the parsed target data and the preset template engine. The target code is filled into the initial C code file to obtain a target C code file suitable for the vehicle CAN network. This target C code file is used to drive the general gateway firmware of the CAN gateway to trigger a preset processing operation for the CAN message after the general gateway firmware receives the CAN message. The general gateway firmware is a gateway application configured with general processing logic for CAN messages.

[0006] Optionally, the declarative configuration file includes the definition information of the target vehicle's CAN bus and the CAN messages transmitted on the CAN bus, the configuration information of the unified diagnostic service, the parameter information of the vehicle intrusion detection system, and the cyclic code verification information of the CAN messages.

[0007] Optionally, the step of validating the declarative configuration file using a preset JSON Schema file to obtain the validation result includes: The verification results are obtained by calling the preset validator and using the preset JSON Schema file to perform corresponding verifications on the data structure, field names, data types, and numerical ranges of the declarative configuration file.

[0008] Optionally, parsing the declarative configuration file and generating target code using the parsed target data and a preset template engine includes: The declarative configuration file is parsed recursively using a preset code generation engine to obtain the target data; The preset code generation engine generates target code based on the target data and placeholders in the preset template engine.

[0009] Optionally, the step of generating target code using the parsed target data and a preset template engine includes: The parsed target data is converted into a nested structure, the keys in the target data are converted into member names of the members in the nested structure, and the values ​​in the target data are converted into the initial values ​​of the members. The global structure variable is assigned a value based on the target data using a static initialization method, resulting in the assigned global structure variable. The target code is generated based on the preset template engine, the nested structure, the initial value, and the global structure variable after assignment.

[0010] Optionally, filling the target code into the initial C code file to obtain a target C code file suitable for the vehicle CAN network includes: Generate a header file containing structures, enumerations, and global structure variables, and generate a source file for instantiating and initializing the global structure variables; The header file and the source file are identified as the initial C code file, and the target code is filled into the header file and the source file of the initial C code file respectively to obtain the target C code file suitable for vehicle CAN network.

[0011] Optionally, after the general gateway firmware of the CAN gateway receives the CAN message, the step of using the target C code file to drive the general gateway firmware to trigger a preset processing operation for the CAN message includes: After the general gateway firmware of the CAN gateway receives the CAN message, it calls the interrupt service routine for the general gateway firmware. Based on the identity information and bus information carried in the CAN message, the general gateway firmware determines the message configuration information corresponding to the CAN message from the global structure variables of the target C code file. The general gateway firmware determines the processing rules for the CAN message based on the message configuration information, and uses the processing rules to process the CAN message accordingly.

[0012] Secondly, this application provides a code generation device for vehicle-mounted CAN networks, comprising: The file verification module is used to determine the JSON format declarative configuration file that defines the configuration information of the CAN gateway in the target vehicle, and to verify the declarative configuration file using a preset JSON Schema file to obtain the verification result. The code generation module is used to parse the declarative configuration file when the verification result shows that the verification is successful, and to generate target code using the parsed target data and a preset template engine. The message processing module is used to fill the target code into the initial C code file to obtain a target C code file suitable for the vehicle CAN network, so that after the general gateway firmware of the CAN gateway receives the CAN message, it can use the target C code file to drive the general gateway firmware to trigger a preset processing operation for the CAN message; the general gateway firmware is a gateway application configured with general processing logic for CAN messages.

[0013] Thirdly, this application provides an electronic device, comprising: Memory, used to store computer programs; A processor is used to execute the computer program to implement the aforementioned code generation method for vehicle-mounted CAN networks.

[0014] Fourthly, this application provides a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the aforementioned code generation method for vehicle-mounted CAN networks.

[0015] In this application, a declarative configuration file in JSON format is determined and used to define the configuration information of the CAN gateway in the target vehicle. The declarative configuration file is validated using a preset JSON Schema file to obtain a validation result. When the validation result shows that the validation is successful, the declarative configuration file is parsed, and target code is generated using the parsed target data and a preset template engine. The target code is filled into an initial C code file to obtain a target C code file suitable for the vehicle CAN network. This allows the target C code file to drive the general gateway firmware of the CAN gateway to trigger preset processing operations for the CAN message after the general gateway firmware receives the CAN message. The general gateway firmware is a gateway application configured with general processing logic for CAN messages. As can be seen from the above, in this application, a declarative configuration file in JSON format is first determined to define the configuration information of the target vehicle's CAN gateway. Then, the declarative configuration file is validated using a preset JSON Schema file, and the validation result is obtained. If the validation result shows that the validation passed, the declarative configuration file is parsed, and target code is generated using the parsed target data and a preset template engine. Subsequently, this target code is filled into an initial C code file to obtain a target C code file suitable for the vehicle CAN network. This allows the general gateway firmware of the CAN gateway to, upon receiving a CAN message, rely on this target C code file to drive itself to trigger preset processing operations for that CAN message. In this way, this application can improve the efficiency and reliability of code generation for vehicle CAN networks. Attached Figure Description

[0016] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0017] Figure 1 This application discloses a flowchart of a code generation method for vehicle-mounted CAN networks. Figure 2This is a system framework diagram of a code generation method for vehicle-mounted CAN networks disclosed in this application; Figure 3 This application discloses a specific code generation method for vehicle-mounted CAN networks. Figure 4 This is an example diagram of a data structure mapping disclosed in this application; Figure 5 This is an example diagram of a data-driven logic disclosed in this application; Figure 6 This is a schematic diagram of a code generation device for vehicle-mounted CAN networks disclosed in this application. Figure 7 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation

[0018] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0019] Currently, traditional CAN gateway software development relies on manual interpretation of the communication matrix and manual coding to implement functions such as message sending and receiving, signal packing and unpacking, and routing judgment. This process is cumbersome and error-prone, and repeated modifications and tests are required when the communication matrix changes frequently, resulting in extremely low efficiency. Existing improvement solutions use commercial tools combined with DBC files to generate partial code, but still have significant drawbacks: incomplete functional coverage, only generating signal packing and unpacking functions, while core functions such as gateway routing logic and message filtering still require manual coding; insufficient flexibility, as the fixed DBC format cannot support custom functions such as IDS parameter configuration and cycle code verification, and configuration information is scattered and difficult to manage; limited by the format and tied to the tool ecosystem, it cannot adapt to multi-bus scenarios such as Ethernet, and the generated code is prone to incompatibility with existing project architectures, making integration difficult. Furthermore, the separation of configuration and code further increases the development and maintenance burden. To address these issues, this application provides a code generation method, apparatus, device, and medium for vehicle-mounted CAN networks, which can improve the efficiency and reliability of code generation for vehicle-mounted CAN networks.

[0020] See Figure 1 As shown, this embodiment of the invention discloses a code generation method for vehicle-mounted CAN networks, including: Step S11: Determine a declarative configuration file in JSON format used to define the configuration information of the CAN gateway in the target vehicle, and verify the declarative configuration file using a preset JSON Schema file to obtain the verification result.

[0021] In this embodiment, a declarative configuration file in JSON format must first be determined. This file comprehensively defines the configuration information of the CAN gateway in the target vehicle. The declarative configuration file includes the definition information of the CAN bus of the target vehicle and the CAN messages transmitted on the CAN bus, the configuration information of the unified diagnostic service, the parameter information of the vehicle intrusion detection system, and the cyclic code check information of the CAN messages.

[0022] To ensure the standardization and correctness of the configuration file, a preset validator is invoked, and a preset JSONSchema file is used to validate the data structure, field names, data types, and value ranges of the declarative configuration file, obtaining the validation results. This validation method allows potential problems in the configuration data to be identified early in the code generation process, avoiding the risk of code generation failure or incorrect code generation due to configuration errors, thereby improving the robustness of the entire code generation process.

[0023] Step S12: When the verification result shows that the verification is successful, the declarative configuration file is parsed, and the target data obtained from the parsing and the preset template engine are used to generate target code.

[0024] In this embodiment, after the verification result confirms that the configuration file conforms to the preset specifications, the code generation process is triggered. First, the declarative configuration file is parsed recursively using a preset code generation engine to obtain the target data. This process ensures that the complex nesting relationships and data associations in the original declarative configuration are accurately captured and preserved.

[0025] Furthermore, the target code is generated based on the target data and placeholders in the preset code generation engine using the preset code generation engine. Specifically, the parsed target data is converted into a nested structure, the keys in the target data are converted into member names of the members in the nested structure, and the values ​​in the target data are converted into the initial values ​​of the members. Global structure variables are assigned values ​​based on the target data using static initialization, resulting in the assigned global structure variables. Target code is generated based on the preset template engine, the nested structure, the initial values, and the assigned global structure variables. By filling the parsed target data into the corresponding placeholders, C code files that meet the specific requirements of the target vehicle and can be directly integrated into the in-vehicle software project are automatically generated, thereby significantly improving development efficiency and consistency.

[0026] Step S13: Fill the target code into the initial C code file to obtain a target C code file suitable for the vehicle CAN network, so that after the general gateway firmware of the CAN gateway receives the CAN message, the target C code file can be used to drive the general gateway firmware to trigger a preset processing operation for the CAN message; the general gateway firmware is a gateway application configured with general processing logic for CAN messages.

[0027] In this embodiment, after generating the target code through the template engine, it needs to be integrated into a C language source code file that can be directly compiled and used. First, a header file containing structures, enumerations, and global structure variables is generated, along with a source file for instantiating and initializing the global structure variables. The header file and the source file are then used as the initial C code file, and the target code is filled into both the header file and the source file within the initial C code file, resulting in a target C code file suitable for automotive CAN networks. This file, as a set of portable software modules, can be integrated with the underlying firmware of a general gateway.

[0028] It should be noted that the purpose of the target C code file is to provide specific, vehicle-configuration-related data processing logic for the general gateway firmware of the CAN gateway. Specifically, after the general gateway firmware receives a CAN message, it calls the interrupt service routine for the general gateway firmware; based on the identity information and bus information carried in the CAN message, the general gateway firmware determines the message configuration information corresponding to the CAN message from the global structure variables of the target C code file; based on the message configuration information, the general gateway firmware determines the processing rules for the CAN message, and uses the processing rules to process the CAN message accordingly. This entire process decouples the general processing logic from the specific vehicle configuration. By changing different target C code files, the same set of general gateway firmware can be adapted to different vehicle models or network configurations, greatly enhancing software reusability and deployment flexibility.

[0029] As can be seen from the above, in this application, a declarative configuration file in JSON format is first determined to define the configuration information of the target vehicle's CAN gateway. Then, the declarative configuration file is validated using a preset JSON Schema file, and the validation result is obtained. If the validation result shows that the validation passed, the declarative configuration file is parsed, and target code is generated using the parsed target data and a preset template engine. Subsequently, this target code is filled into an initial C code file to obtain a target C code file suitable for the vehicle CAN network. This allows the general gateway firmware of the CAN gateway to, upon receiving a CAN message, rely on this target C code file to drive itself to trigger preset processing operations for that CAN message. In this way, this application can improve the efficiency and reliability of code generation for vehicle CAN networks.

[0030] The following is combined Figure 2 The schematic diagram shown illustrates the components in the system architecture of this application embodiment.

[0031] The system in this application mainly includes: a declarative JSON (i.e., JavaScript Object Notation, a lightweight data exchange format) configuration file, a JSON Schema (a specification for describing and validating JSON data structures and formats) definition file, a code generation system, C code files, a general gateway firmware, and an embedded compiler.

[0032] A declarative JSON configuration file is a JSON text file maintained by the developer and following a specific format, such as cangw.json. It is the sole input and sole source of truth for the entire system, used to declaratively define all gateway configurations, including but not limited to the CAN communication matrix, UDS (Universal Diagnostics Services) diagnostic routes, and IDS security policies.

[0033] A JSON Schema definition file is a JSON Schema file used to define and validate the declarative JSON configuration file format; an example is cangw_schema.json. It ensures the structural correctness and data validity of the configuration file.

[0034] The code generation system is the core processing system of this invention. It receives configuration files and outputs C code. Internally, it includes: a configuration validator, responsible for validating the legality of the input JSON configuration file according to the JSON Schema; and a code generation engine, which, after successful configuration validation, parses the JSON data and calls the template engine to generate code. In this embodiment, this is a Python script, such as json2c.py; the Jinja2 template is a predefined C code template file, which may contain... .h.jinja and The .c.jinja C code template file contains the skeleton of C code and placeholders for filling in data.

[0035] The C code files are the C header files and source files ultimately generated by the code generation system. For example, the C header file could be cangw.h, and the source file could be cangw.c. These files define a series of C structures and instantiate and initialize a global configuration data structure, such as g_cangw_schema_.

[0036] Generic gateway firmware is a pre-written, logically generic gateway application. It does not contain any hard-coded routing rules or message information; all its behavior is driven by reading and interpreting generated configuration data structures at runtime.

[0037] An embedded compiler is used to compile and link the generated C code file together with other source code of the generic gateway firmware, ultimately generating CAN gateway firmware that can run on the target hardware.

[0038] The following is combined Figure 3 The schematic diagram shown illustrates the solution flow of the embodiments of this application in detail.

[0039] First, define or modify the declarative JSON configuration file. Developers define or modify the CAN gateway configuration in a JSON file according to product requirements. The declarative JSON configuration file is highly structured and mainly includes: can_matrix, uds, cangw_ids, and cycle_code_model.

[0040] Specifically, `can_matrix` defines the baud rate, name, and messages on each CAN bus. For each message, it defines its CAN ID, ID type (standard / extended), transmitting node, period, data length, etc., and its routing target (`proxy_can_bus`), i.e., the other buses to which the message needs to be forwarded and the forwarding period. The message also defines a list of signals, each describing its name, start bit, length, byte order, data type, scaling factor, and offset.

[0041] The UDS defines the relevant configurations for the UDS diagnostic service. This includes the gateway's own diagnostic ID and a list of uds_matrixes used to define the routing rules from which diagnostic requests originate in the ECU (e.g., the OBD (On-Board Diagnostics) port) and proceed to the target ECU. Additionally, parameters such as security_access levels, algorithms, and key lengths can be configured.

[0042] `cangw_ids` defines the parameters of the vehicle intrusion detection system (IDS). For example, to target CAN flooding attacks, fuzzing attacks, replay attacks, etc., a threshold number of times (tt, threshold times) can be configured to trigger alarms within a specific time window (tw, time window).

[0043] The `cycle_code_model` defines the verification rules for the message cycle counter (i.e., the rolling counter), used to check the liveness and authenticity of the message and prevent replay attacks. The signal location and tolerance value of the cycle counter can be configured for a specific message ID on a specific bus.

[0044] Secondly, the configuration file is validated using JSON Schema. Before generating code, the code generation system first calls the configuration validator to rigorously validate the JSON configuration file provided by the developer using a predefined JSON Schema file. The validation includes: whether the data structure is complete, whether the field names are correct, whether the data types match (e.g., CAN ID should be a number, and is_enable should be a boolean value); and whether the values ​​are within a valid range (e.g., CAN bus number should be between 0 and 9).

[0045] Next, the validation result is checked. If validation fails, the process terminates and a detailed error message is reported to the developer, indicating the location and reason for the non-compliance in the configuration file. If validation passes, the process proceeds to the next step. This step ensures the quality of the input data and forms the basis for generating correct code subsequently.

[0046] Then, the JSON data is parsed and the template is rendered. After successful validation, the code generation engine recursively parses the data in the JSON configuration file. Simultaneously, it loads predefined Jinja2 templates. The engine matches and fills in the parsed data (such as message names, CANIDs, signal lists, etc.) with placeholders in the template, ultimately rendering and generating the text content of the C language header and source files.

[0047] Specifically, regarding header file generation, the jschema2c.h.jinja template is responsible for generating the corresponding C struct and enum definitions based on the JSON structure. For example, the can_matrix object in the JSON will be mapped to a TCanMatrix struct, and the messages array will be mapped to a TMessage struct array. String enums in the JSON, such as "std" and "ext" in id_type, will be mapped to C enum types. Finally, the template generates a global configuration struct variable declared with extern, such as extern TCangwSchema g_cangw_schema_.

[0048] Specifically, the jschema2c.c.jinja template is responsible for generating the instantiation and static initialization of the global configuration struct variables. It recursively iterates through the JSON data and, following C's static initialization syntax, fills each value in the JSON—such as strings, numbers, and booleans—into the corresponding member of the struct. For example, "can_id":"0x123" will be converted to .can_id_ = 0x123.

[0049] Finally, the code text content generated in memory in the previous step is written to the specified header file and source file, namely cangw.h and cangw.c, respectively.

[0050] Through the above process, any modifications developers make to the gateway logic only need to be made in the JSON configuration file. Then, by executing a generation script once, the configuration data structure in the C code will be automatically and accurately updated. The code of the general gateway firmware requires no modification; simply recompiling applies the new configuration. This "configuration as code" and "data-driven" design paradigm greatly simplifies the development and maintenance process.

[0051] To illustrate the "configuration is code" process more intuitively, Figure 4 This demonstrates the mapping between JSON configuration and C code. For example... Figure 4As shown, the code generation engine precisely converts the hierarchical data in the cangw.json file into the corresponding nested structs in the C language, and assigns a value to the global variable g_cangw_schema_ using static initialization. The keys in the JSON are converted into struct member names, and the values ​​are converted into the initial values ​​of the members.

[0052] Reference Figure 5 As shown, Figure 5 This demonstrates how a generic gateway firmware, upon receiving a CAN message frame, utilizes the generated data structure to perform "data-driven" routing decisions. For example... Figure 5 As shown, the firmware logic is generic and does not contain any specific IDs or routing rules. It acts like an interpreter, searching for the current packet by traversing the `g_cangw_schema_` data structure. Figure 5 The ID 0x123 in the configuration information comes from bus 0. Once found, it reads the route target from the configuration, i.e. Figure 5 The `proxy_can_bus_` directive is used to perform forwarding actions. If the routing rules need to be modified, such as forwarding 0x123 packets to bus 3, developers only need to add a target to the JSON file; no changes are required to the firmware code.

[0053] Accordingly, see Figure 6 As shown, this application embodiment provides a code generation device for vehicle-mounted CAN networks, including: The file verification module 11 is used to determine the JSON format of the declarative configuration file that defines the configuration information of the CAN gateway in the target vehicle, and to verify the declarative configuration file using a preset JSON Schema file to obtain the verification result. The code generation module 12 is used to parse the declarative configuration file when the verification result shows that the verification is successful, and to generate target code using the parsed target data and a preset template engine. The message processing module 13 is used to fill the target code into the initial C code file to obtain a target C code file suitable for the vehicle CAN network, so that after the general gateway firmware of the CAN gateway receives the CAN message, it can use the target C code file to drive the general gateway firmware to trigger a preset processing operation for the CAN message; the general gateway firmware is a gateway application configured with general processing logic for CAN messages.

[0054] In some specific implementations, the declarative configuration file includes the definition information of the CAN bus of the target vehicle and the CAN messages transmitted on the CAN bus, the configuration information of the unified diagnostic service, the parameter information of the vehicle intrusion detection system, and the cyclic code verification information of the CAN messages.

[0055] In some specific embodiments, the file verification module 11 specifically includes: The file verification unit is used to verify the data structure, field names, data types, and numerical ranges of the declarative configuration file by calling a preset verifier and using a preset JSON Schema file, and obtain the verification result.

[0056] In some specific embodiments, the code generation module 12 specifically includes: The file parsing unit is used to parse the declarative configuration file in a recursive manner using a preset code generation engine to obtain the target data; The first code generation unit is used to generate target code based on the target data and placeholders in the preset template engine through the preset code generation engine.

[0057] In some specific embodiments, the code generation module 12 specifically includes: The data conversion unit is used to convert the parsed target data into a nested structure, convert the keys in the target data into member names of the members in the nested structure, and convert the values ​​in the target data into the initial values ​​of the members. The variable assignment unit is used to assign values ​​to global structure variables based on the target data through static initialization, so as to obtain the assigned global structure variables. The second code generation unit is used to generate target code based on a preset template engine, the nested structure, the initial value, and the assigned global structure variable.

[0058] In some specific embodiments, the message processing module 13 specifically includes: The first file generation unit is used to generate a header file containing structures, enumerations, and global structure variables, and to generate a source file for instantiating and initializing the global structure variables. The second file generation unit is used to determine the header file and the source file as an initial C code file, and to fill the target code into the header file and the source file of the initial C code file respectively, so as to obtain a target C code file suitable for vehicle CAN network.

[0059] In some specific embodiments, the message processing module 13 specifically includes: The program calling unit is used to call the interrupt service routine for the general gateway firmware after the general gateway firmware of the CAN gateway receives the CAN message. The information determination unit is used to determine the message configuration information corresponding to the CAN message from the global structure variables of the target C code file based on the identity information and bus information carried in the CAN message through the general gateway firmware; The message processing unit is used to determine the processing rules for the CAN message based on the message configuration information through the general gateway firmware, and to process the CAN message accordingly using the processing rules.

[0060] Furthermore, embodiments of this application also disclose an electronic device, Figure 7 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application. Specifically, the electronic device 20 may include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 stores a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the code generation method for vehicle-mounted CAN networks disclosed in any of the foregoing embodiments. Furthermore, the electronic device 20 in this embodiment may specifically be an electronic computer.

[0061] In this embodiment, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

[0062] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk or optical disk, etc. The resources stored thereon can include operating system 221, computer program 222, etc., and the storage method can be temporary storage or permanent storage.

[0063] The operating system 221 is used to manage and control the various hardware devices on the electronic device 20 and the computer program 222, which may be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program capable of performing the code generation method for the vehicle-mounted CAN network disclosed in any of the foregoing embodiments, the computer program 222 may further include computer programs capable of performing other specific tasks.

[0064] Furthermore, this application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the aforementioned code generation method for in-vehicle CAN networks. Specific steps of this method can be found in the corresponding content disclosed in the foregoing embodiments, and will not be repeated here.

[0065] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.

[0066] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0067] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.

[0068] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0069] The technical solutions provided in this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.

Claims

1. A code generation method for vehicle-mounted CAN networks, characterized in that, include: A declarative configuration file in JSON format is determined, which is used to define the configuration information of the CAN gateway in the target vehicle. The declarative configuration file is then validated using a preset JSON Schema file to obtain the validation result. If the verification result shows that the verification is successful, the declarative configuration file is parsed, and the target code is generated using the parsed target data and the preset template engine. The target code is filled into the initial C code file to obtain a target C code file suitable for the vehicle CAN network. This allows the general gateway firmware of the CAN gateway to use the target C code file to drive the general gateway firmware to trigger a preset processing operation for the CAN message after receiving the CAN message. The general gateway firmware is a gateway application configured with general processing logic for CAN messages.

2. The code generation method for vehicle-mounted CAN networks according to claim 1, characterized in that, The declarative configuration file includes the definition information of the target vehicle's CAN bus and the CAN messages transmitted on the CAN bus, the configuration information of the unified diagnostic service, the parameter information of the vehicle intrusion detection system, and the cyclic code verification information of the CAN messages.

3. The code generation method for vehicle-mounted CAN networks according to claim 1, characterized in that, The step of validating the declarative configuration file using a preset JSON Schema file to obtain the validation result includes: The verification results are obtained by calling the preset validator and using the preset JSON Schema file to perform corresponding verifications on the data structure, field names, data types, and numerical ranges of the declarative configuration file.

4. The code generation method for vehicle-mounted CAN networks according to claim 1, characterized in that, The step of parsing the declarative configuration file and generating target code using the parsed target data and a preset template engine includes: The declarative configuration file is parsed recursively using a preset code generation engine to obtain the target data; The preset code generation engine generates target code based on the target data and placeholders in the preset template engine.

5. The code generation method for vehicle-mounted CAN networks according to claim 1, characterized in that, The process of generating target code using the parsed target data and a preset template engine includes: The parsed target data is converted into a nested structure, the keys in the target data are converted into member names of the members in the nested structure, and the values ​​in the target data are converted into the initial values ​​of the members. The global structure variable is assigned a value based on the target data using a static initialization method, resulting in the assigned global structure variable. The target code is generated based on the preset template engine, the nested structure, the initial value, and the global structure variable after assignment.

6. The code generation method for vehicle-mounted CAN networks according to claim 1, characterized in that, The step of filling the target code into the initial C code file to obtain a target C code file suitable for the vehicle CAN network includes: Generate a header file containing structures, enumerations, and global structure variables, and generate a source file for instantiating and initializing the global structure variables; The header file and the source file are identified as the initial C code file, and the target code is filled into the header file and the source file of the initial C code file respectively to obtain the target C code file suitable for vehicle CAN network.

7. The code generation method for vehicle-mounted CAN networks according to any one of claims 1 to 6, characterized in that, After the general gateway firmware of the CAN gateway receives a CAN message, the target C code file drives the general gateway firmware to trigger a preset processing operation for the CAN message, including: After the general gateway firmware of the CAN gateway receives the CAN message, it calls the interrupt service routine for the general gateway firmware. Based on the identity information and bus information carried in the CAN message, the general gateway firmware determines the message configuration information corresponding to the CAN message from the global structure variables of the target C code file. The general gateway firmware determines the processing rules for the CAN message based on the message configuration information, and uses the processing rules to process the CAN message accordingly.

8. A code generation device for vehicle-mounted CAN networks, characterized in that, include: The file verification module is used to determine the JSON format declarative configuration file that defines the configuration information of the CAN gateway in the target vehicle, and to verify the declarative configuration file using a preset JSON Schema file to obtain the verification result. The code generation module is used to parse the declarative configuration file when the verification result shows that the verification is successful, and to generate target code using the parsed target data and a preset template engine. The message processing module is used to fill the target code into the initial C code file to obtain a target C code file suitable for the vehicle CAN network, so that after the general gateway firmware of the CAN gateway receives the CAN message, it can use the target C code file to drive the general gateway firmware to trigger a preset processing operation for the CAN message. The general gateway firmware is a gateway application configured with general processing logic for CAN messages.

9. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for executing the computer program to implement the code generation method for vehicle-mounted CAN networks as described in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, Used to store computer programs; wherein, when the computer programs are executed by a processor, they implement the code generation method for vehicle-mounted CAN networks as described in any one of claims 1 to 7.