|
| 1 | +# UDS -> C Code |
| 2 | + |
| 3 | +Users can generate C code based on their configured UDS services. The code format depends on user-defined templates. |
| 4 | + |
| 5 | + |
| 6 | +## UDS Code Generation |
| 7 | + |
| 8 | +First, users need to enable UDS code generation in the UDS Tester. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +Then users can add Template Configurations. Each template configuration contains two fields: |
| 13 | +* **Template Path**: Users can select a template file from the local file system. |
| 14 | +* **Generate Path**: Users can specify the path where the generated code will be saved. |
| 15 | + |
| 16 | +Multiple template configurations are supported. Users can add or remove template configurations as needed, and each template supports runtime preview functionality. |
| 17 | + |
| 18 | +## Special Properties |
| 19 | + |
| 20 | +When enabling UDS code generation, each service supports adding user-defined special properties for `code generation`. |
| 21 | + |
| 22 | + |
| 23 | +Properties are defined in Key-Value format. |
| 24 | + |
| 25 | +**Requirements:** |
| 26 | +* Keys cannot be duplicated |
| 27 | +* Values must be strings |
| 28 | + |
| 29 | + |
| 30 | +## Template |
| 31 | + |
| 32 | +The UDS code generation system is based on [**Handlebars.js**](https://handlebarsjs.com) templating engine, enhanced with extensive custom helper methods to provide powerful code generation capabilities. |
| 33 | + |
| 34 | +### Template System Overview |
| 35 | + |
| 36 | +Templates use Handlebars syntax with double curly braces <span v-pre>`{{ }}`</span> for expressions and <span v-pre>`{{# }}{{/ }}`</span> for block helpers. The system provides access to UDS service data, configuration properties, and a rich set of helper functions. |
| 37 | + |
| 38 | +### Available Data Context |
| 39 | + |
| 40 | +When generating code, templates have access to: |
| 41 | +- **tester:[TesterInfo](https://app.whyengineer.com/scriptApi/interfaces/TesterInfo.html)**: All configured UDS services and their properties |
| 42 | +- **project:ProjectInfo**: Project information, type is: |
| 43 | +```ts |
| 44 | +export interface ProjectInfo { |
| 45 | + name: string |
| 46 | + path: string |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### Custom Helper Methods |
| 51 | + |
| 52 | +#### **Mathematical Operations** |
| 53 | +- <span v-pre>`{{add a b}}`</span> - Addition |
| 54 | +- <span v-pre>`{{subtract a b}}`</span> - Subtraction |
| 55 | +- <span v-pre>`{{multiply a b}}`</span> - Multiplication |
| 56 | +- <span v-pre>`{{divide a b}}`</span> - Division |
| 57 | +- <span v-pre>`{{abs num}}`</span> - Absolute value |
| 58 | +- <span v-pre>`{{ceil num}}`</span> - Round up |
| 59 | +- <span v-pre>`{{floor num}}`</span> - Round down |
| 60 | +- <span v-pre>`{{modulo a b}}`</span> - Remainder |
| 61 | +- <span v-pre>`{{avg array}}`</span> - Average of array |
| 62 | +- <span v-pre>`{{sum array}}`</span> - Sum of array |
| 63 | + |
| 64 | +#### **String Manipulation** |
| 65 | +- <span v-pre>`{{camelcase str}}`</span> - Convert to camelCase |
| 66 | +- <span v-pre>`{{capitalize str}}`</span> - Capitalize first letter |
| 67 | +- <span v-pre>`{{uppercase str}}`</span> - Convert to UPPERCASE |
| 68 | +- <span v-pre>`{{lowercase str}}`</span> - Convert to lowercase |
| 69 | +- <span v-pre>`{{dashcase str}}`</span> - Convert to dash-case |
| 70 | +- <span v-pre>`{{snakecase str}}`</span> - Convert to snake_case |
| 71 | +- <span v-pre>`{{dotcase str}}`</span> - Convert to dot.case |
| 72 | +- <span v-pre>`{{append str suffix}}`</span> - Append suffix |
| 73 | +- <span v-pre>`{{prepend str prefix}}`</span> - Prepend prefix |
| 74 | +- <span v-pre>`{{trim str}}`</span> - Remove whitespace |
| 75 | +- <span v-pre>`{{replace str old new}}`</span> - Replace text |
| 76 | + |
| 77 | +#### **Array Operations** |
| 78 | +- <span v-pre>`{{first array n}}`</span> - Get first n items |
| 79 | +- <span v-pre>`{{last array n}}`</span> - Get last n items |
| 80 | +- <span v-pre>`{{after array n}}`</span> - Get items after index n |
| 81 | +- <span v-pre>`{{before array n}}`</span> - Get items before index n |
| 82 | +- <span v-pre>`{{join array separator}}`</span> - Join array elements |
| 83 | +- <span v-pre>`{{arrayify value}}`</span> - Convert to array |
| 84 | +- <span v-pre>`{{#forEach array}}...{{/forEach}}`</span> - Iterate array |
| 85 | +- <span v-pre>`{{#eachIndex array}}...{{/eachIndex}}`</span> - Iterate with index |
| 86 | + |
| 87 | +#### **Comparison & Logic** |
| 88 | +- <span v-pre>`{{eq a b}}`</span> - Equal to |
| 89 | +- <span v-pre>`{{ne a b}}`</span> - Not equal to |
| 90 | +- <span v-pre>`{{gt a b}}`</span> - Greater than |
| 91 | +- <span v-pre>`{{lt a b}}`</span> - Less than |
| 92 | +- <span v-pre>`{{gte a b}}`</span> - Greater than or equal |
| 93 | +- <span v-pre>`{{lte a b}}`</span> - Less than or equal |
| 94 | +- <span v-pre>`{{isString val}}`</span> - Check if string |
| 95 | +- <span v-pre>`{{isNumber val}}`</span> - Check if number |
| 96 | +- <span v-pre>`{{isArray val}}`</span> - Check if array |
| 97 | +- <span v-pre>`{{isDefined val}}`</span> - Check if defined |
| 98 | +- <span v-pre>`{{isUndefined val}}`</span> - Check if undefined |
| 99 | + |
| 100 | +#### **Utility Functions** |
| 101 | +- <span v-pre>`{{setVar name value}}`</span> - Set variable |
| 102 | +- <span v-pre>`{{jsonParse str}}`</span> - Parse JSON string |
| 103 | +- <span v-pre>`{{jsonStringify obj}}`</span> - Convert to JSON |
| 104 | +- <span v-pre>`{{times n}}...{{/times}}`</span> - Repeat n times |
| 105 | +- <span v-pre>`{{range start end}}...{{/range}}`</span> - Loop from start to end |
| 106 | +- <span v-pre>`{{logFile message}}`</span> - Debug output |
| 107 | +- <span v-pre>`{{error message}}`</span> - Throw error |
| 108 | + |
| 109 | +### Template Example |
| 110 | + |
| 111 | +```handlebars |
| 112 | +/* Generated UDS Code */ |
| 113 | +#include <stdint.h> |
| 114 | +
|
| 115 | +{{#forEach services}} |
| 116 | +// Service: {{name}} |
| 117 | +{{#if (isDefined description)}} |
| 118 | +/* {{description}} */ |
| 119 | +{{/if}} |
| 120 | +
|
| 121 | +#define {{uppercase (snakecase name)}}_ID 0x{{serviceId}} |
| 122 | +
|
| 123 | +{{#if specialProperties}} |
| 124 | +/* Special Properties */ |
| 125 | +{{#each specialProperties}} |
| 126 | +#define {{uppercase (snakecase @key)}} "{{this}}" |
| 127 | +{{/each}} |
| 128 | +{{/if}} |
| 129 | +
|
| 130 | +{{/forEach}} |
| 131 | +``` |
| 132 | + |
| 133 | +### More Complex Example |
| 134 | + |
| 135 | +See the [uds_generate_code](../../../resources/examples/uds_generate_code/readme) example. |
0 commit comments