UDS Generate Code Example
This example demonstrates how to use EcuBus-Pro's UDS code generation system with Handlebars templates.The uds.hbs
template generates C code for DCM (Diagnostic Communication Manager) configuration based on your UDS service definitions.
Template File Analysis: uds.hbs
Template Structure Overview
The template generates C code with two main configuration arrays:
Dcm[]
- Main service configuration arrayDcmRoutineControlConfig[]
- Routine control specific configuration
Detailed Code Analysis
1. License Header
Static license header that will be included in all generated files.
2. Include Statement
Standard C include for DCM functionality.
3. Service Count Calculation
Line-by-line explanation:
{{logFile 'Calculate active service type except JobFunction'~}}
- Debug output to log file{{setVar 'serviceNum' 0~}}
- Initialize counter variable to 0{{#each tester.allServiceList~}}
- Iterate through all services in the tester{{#if (not (eq @key 'Job'))}}
- Check if current service key is NOT 'Job'{{setVar 'serviceNum' (add @root.serviceNum 1)~}}
- Increment counter for non-Job services{{/if~}}
- End if statement{{/each~}}
- End each loop#define SUPPORTED_SERVICE_NUM {{@root.serviceNum}}
- Generate C macro with total count
4. Main DCM Configuration Array
Explanation:
DcmConfig_t Dcm[]={
- Start C array declaration{{#each tester.allServiceList}}
- Loop through all UDS services
4.1 Session Control Service (0x10)
Explanation:
{{#if (eq @key '0x10')}}
- Check if current service ID equals 0x10 (Session Control)- Generates DCM configuration for Session Control service
- Enables multiple session types using bitwise OR
- Sets security level to NULL (no security required)
- Assigns callback function
4.2 Read Data by Identifier Service (0x22)
Explanation:
{{#if (eq @key '0x22')}}
- Check for service ID 0x22 (Read Data by Identifier)- Similar structure to Session Control but with different callback function
4.3 Routine Control Service (0x31)
Explanation:
{{#if (eq @key '0x31')}}
- Check for service ID 0x31 (Routine Control)- Configures Routine Control service with appropriate callback
5. Routine Control Specific Configuration
Line-by-line explanation:
DcmRoutineControlConfig_t DcmRoutineControlConfig[]={
- Start routine control config array{{#each (get tester.allServiceList '0x31')}}
- Loop through all 0x31 (Routine Control) services/* {{name}} */
- Generate comment with service name{
- Start configuration structure.routineControlType={{get (first (filter params 'routineControlType' property='name')) 'phyValue' }},
- Extract routine control type from parameters.routineIdentifier=[{{get (first (filter params 'routineIdentifier' property='name')) 'value.data'}}],
- Extract routine identifier.session={{#if generateConfigs}}{{get generateConfigs 'session'}}{{else}}DCM_DEFAULT_SESSION_EN{{/if}},
- Set session type (custom or default).security={{#if generateConfigs}}{{get generateConfigs 'security'}}{{else}}DCM_SECURITY_LEVEL_NULL{{/if}},
- Set security level (custom or default)},
- End configuration structure{{/each}}
- End loop};
- Close array
Helper Methods Used
Data Access Helpers
{{get object property}}
- Access object property safely{{first array}}
- Get first element of array{{filter array value property='name'}}
- Filter array by property value
Logic Helpers
{{#if condition}}...{{else}}...{{/if}}
- Conditional logic{{eq a b}}
- Equality comparison{{not condition}}
- Logical NOT
Utility Helpers
{{setVar name value}}
- Set template variable{{add a b}}
- Mathematical addition{{logFile message}}
- Debug logging
Iteration Helpers
{{#each array}}...{{/each}}
- Loop through array elements{{@key}}
- Current key in iteration{{@root}}
- Access root context
Usage Instructions
- Configure UDS Services: Set up your UDS services in EcuBus-Pro tester
- Add Template: Select the
uds.hbs
template file - Set Generation Path: Specify output directory for generated C code
- Generate Code: Run the code generation process
- Integration: Include generated files in your DCM implementation
Generated Output Example
The template will generate C code similar to:
c
/**
* Open Source License
*
* EcuBus-Pro is licensed under a modified version of the Apache License 2.0, with the following additional conditions:
*
* 1. EcuBus-Pro may be utilized commercially, including as a diagnostic tool or as a development platform for enterprises. Should the conditions below be met, a commercial license must be obtained from the producer:
*
* a. Device support and licensing:
* - The EcuBus-Pro source code includes support for a set of standard usb devices.
* - If you want to add support for your proprietary devices without open-sourcing the implementation, you must obtain a commercial license from EcuBus-Pro.
*
* b. LOGO and copyright information: In the process of using EcuBus-Pro's frontend, you may not remove or modify the LOGO or copyright information in the EcuBus-Pro console or applications. This restriction is inapplicable to uses of EcuBus-Pro that do not involve its frontend.
* - Frontend Definition: For the purposes of this license, the "frontend" of EcuBus-Pro includes all components located in the `src/renderer/` directory when running EcuBus-Pro from the raw source code, or the "renderer" components when running EcuBus-Pro with Electron.
*
* 2. As a contributor, you should agree that:
*
* a. The producer can adjust the open-source agreement to be more strict or relaxed as deemed necessary.
* b. Your contributed code may be used for commercial purposes, including but not limited to its diagnostic business operations.
*
*
* Apart from the specific conditions mentioned above, all other rights and restrictions follow the Apache License 2.0. Detailed information about the Apache License 2.0 can be found at http://www.apache.org/licenses/LICENSE-2.0.
*
* The interactive design and hardware interface of this product are protected by patents.
* EcuBus-Pro and its logo are registered trademarks of Whyengineer, Inc.
*
* © 2025 Whyengineer, Inc.
*
* For commercial licensing inquiries, please contact: frankie.zengfu@gmail.com
*/
#include "Dcm.h"
#define SUPPORTED_SERVICE_NUM 9
DcmConfig_t Dcm[]={
/* Diagnostic Session Control (0x10) */
{
DCM_DIAGNOSTIC_SESSION_CONTROL,
DCM_DEFAULT_SESSION_EN | DCM_PROGRAMMING_SESSION_EN | DCM_EXTENDED_DIAGNOSTIC_SESSION_EN,
DCM_SECURITY_LEVEL_NULL,
Dcm_SessionControlCallback,
},
/* RoutineControl (0x31) */
{
DCM_ROUNTINE_CONTROL,
DCM_DEFAULT_SESSION_EN | DCM_PROGRAMMING_SESSION_EN | DCM_EXTENDED_DIAGNOSTIC_SESSION_EN,
DCM_SECURITY_LEVEL_NULL,
Dcm_RoutineControlCallback,
},
};
DcmRoutineControlConfig_t DcmRoutineControlConfig[]={
/* RoutineControl490 */
{
.routineControlType=1,
.routineIdentifier=[2,2],
.session=DCM_EXTENDED_DIAGNOSTIC_SESSION_EN,
.security=DCM_SECURITY_LEVEL_1 | DCM_SECURITY_LEVEL_2,
},
/* RoutineControl491 */
{
.routineControlType=2,
.routineIdentifier=[255,0],
.session=DCM_DEFAULT_SESSION_EN,
.security=DCM_SECURITY_LEVEL_NULL,
},
};