|
| 1 | +# Migration Guide: Updating from Libcanard v3.x to v4.0 |
| 2 | + |
| 3 | +This guide is intended to help developers migrate their applications from Libcanard version 3.x to version 4.0. It outlines the key changes between the two versions and provides step-by-step instructions to update your code accordingly. |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +Libcanard is a compact implementation of the Cyphal/CAN protocol designed for high-integrity real-time embedded systems. Version 4 introduces several changes that may impact your existing codebase. This guide will help you understand these changes and update your application accordingly. |
| 8 | + |
| 9 | +These changes do not affect wire compatibility. |
| 10 | + |
| 11 | +## Version Changes |
| 12 | + |
| 13 | +- **Libcanard Version**: |
| 14 | + - **Old**: `CANARD_VERSION_MAJOR 3`, `CANARD_VERSION_MINOR 3` |
| 15 | + - **New**: `CANARD_VERSION_MAJOR 4`, `CANARD_VERSION_MINOR 0` |
| 16 | +- **Cyphal Specification Version**: Remains the same (`1.0`). |
| 17 | + |
| 18 | +## API Changes |
| 19 | + |
| 20 | +### New Functions |
| 21 | + |
| 22 | +- **`canardTxFree`**: |
| 23 | + - **Description**: A helper function to free memory allocated for transmission queue items, including the frame payload buffer. |
| 24 | + - **Prototype**: |
| 25 | + ```c |
| 26 | + void canardTxFree( |
| 27 | + struct CanardTxQueue* const que, |
| 28 | + const struct CanardInstance* const ins, |
| 29 | + struct CanardTxQueueItem* const item); |
| 30 | + ``` |
| 31 | + - **Usage**: After popping a transmission queue item using `canardTxPop`, use `canardTxFree` to deallocate its memory. |
| 32 | + |
| 33 | +### Modified Functions |
| 34 | + |
| 35 | +Several functions have updated prototypes and usage patterns: |
| 36 | + |
| 37 | +1. **`canardInit`**: |
| 38 | + - **Old Prototype**: |
| 39 | + ```c |
| 40 | + CanardInstance canardInit( |
| 41 | + const CanardMemoryAllocate memory_allocate, |
| 42 | + const CanardMemoryFree memory_free); |
| 43 | + ``` |
| 44 | + - **New Prototype**: |
| 45 | + ```c |
| 46 | + struct CanardInstance canardInit( |
| 47 | + const struct CanardMemoryResource memory); |
| 48 | + ``` |
| 49 | + - **Changes**: |
| 50 | + - Replaces `CanardMemoryAllocate` and `CanardMemoryFree` function pointers with a `CanardMemoryResource` struct. |
| 51 | + |
| 52 | +2. **`canardTxInit`**: |
| 53 | + - **Old Prototype**: |
| 54 | + ```c |
| 55 | + CanardTxQueue canardTxInit( |
| 56 | + const size_t capacity, |
| 57 | + const size_t mtu_bytes); |
| 58 | + ``` |
| 59 | + - **New Prototype**: |
| 60 | + ```c |
| 61 | + struct CanardTxQueue canardTxInit( |
| 62 | + const size_t capacity, |
| 63 | + const size_t mtu_bytes, |
| 64 | + const struct CanardMemoryResource memory); |
| 65 | + ``` |
| 66 | + - **Changes**: |
| 67 | + - Adds a `CanardMemoryResource` parameter for memory allocation of payload data. |
| 68 | + |
| 69 | +3. **`canardTxPush`**: |
| 70 | + - **Old Prototype**: |
| 71 | + ```c |
| 72 | + int32_t canardTxPush( |
| 73 | + CanardTxQueue* const que, |
| 74 | + CanardInstance* const ins, |
| 75 | + const CanardMicrosecond tx_deadline_usec, |
| 76 | + const CanardTransferMetadata* const metadata, |
| 77 | + const size_t payload_size, |
| 78 | + const void* const payload); |
| 79 | + ``` |
| 80 | + - **New Prototype**: |
| 81 | + ```c |
| 82 | + int32_t canardTxPush( |
| 83 | + struct CanardTxQueue* const que, |
| 84 | + const struct CanardInstance* const ins, |
| 85 | + const CanardMicrosecond tx_deadline_usec, |
| 86 | + const struct CanardTransferMetadata* const metadata, |
| 87 | + const struct CanardPayload payload); |
| 88 | + ``` |
| 89 | + - **Changes**: |
| 90 | + - Replaces `payload_size` and `payload` with a single `CanardPayload` struct. |
| 91 | + |
| 92 | +4. **`canardTxPeek`** and **`canardTxPop`**: |
| 93 | + - The functions now return and accept pointers to mutable `struct CanardTxQueueItem` instead of const pointers. |
| 94 | + |
| 95 | +### Removed Functions |
| 96 | + |
| 97 | +- No functions have been explicitly removed, but some have modified prototypes. |
| 98 | + |
| 99 | +## Data Structure Changes |
| 100 | + |
| 101 | +### Type Definitions |
| 102 | + |
| 103 | +- **Enumerations**: |
| 104 | + - Changed from `typedef enum` to `enum` without `typedef`. |
| 105 | + - **Old**: |
| 106 | + ```c |
| 107 | + typedef enum { ... } CanardPriority; |
| 108 | + ``` |
| 109 | + - **New**: |
| 110 | + ```c |
| 111 | + enum CanardPriority { ... }; |
| 112 | + ``` |
| 113 | + |
| 114 | +- **Structures**: |
| 115 | + - Changed from forward declarations and `typedef struct` to direct `struct` definitions. |
| 116 | + - **Old**: |
| 117 | + ```c |
| 118 | + typedef struct CanardInstance CanardInstance; |
| 119 | + ``` |
| 120 | + - **New**: |
| 121 | + ```c |
| 122 | + struct CanardInstance { ... }; |
| 123 | + ``` |
| 124 | + |
| 125 | +### Struct Modifications |
| 126 | + |
| 127 | +- **`CanardFrame`**: |
| 128 | + - **Old**: |
| 129 | + ```c |
| 130 | + typedef struct { |
| 131 | + uint32_t extended_can_id; |
| 132 | + size_t payload_size; |
| 133 | + const void* payload; |
| 134 | + } CanardFrame; |
| 135 | + ``` |
| 136 | + - **New**: |
| 137 | + ```c |
| 138 | + struct CanardFrame { |
| 139 | + uint32_t extended_can_id; |
| 140 | + struct CanardPayload payload; |
| 141 | + }; |
| 142 | + ``` |
| 143 | + - **Changes**: |
| 144 | + - Payload now uses the `CanardPayload` struct. |
| 145 | + |
| 146 | +- **New Structs Introduced**: |
| 147 | + - **`CanardPayload`** and **`CanardMutablePayload`**: Encapsulate payload data and size. |
| 148 | + - **`CanardMutableFrame`**: Similar to `CanardFrame` but uses `CanardMutablePayload`. |
| 149 | + |
| 150 | +- **`CanardInstance`**: |
| 151 | + - Now contains a `CanardMemoryResource` for memory management instead of separate function pointers. |
| 152 | + |
| 153 | +- **`CanardTxQueue`**: |
| 154 | + - Includes a `CanardMemoryResource` for payload data allocation. |
| 155 | + |
| 156 | +- **`CanardMemoryResource`** and **`CanardMemoryDeleter`**: |
| 157 | + - New structs to encapsulate memory allocation and deallocation functions along with user references. |
| 158 | + |
| 159 | +## Memory Management Changes |
| 160 | + |
| 161 | +- **Memory Resource Structs**: |
| 162 | + - Memory allocation and deallocation are now handled via `CanardMemoryResource` and `CanardMemoryDeleter` structs. |
| 163 | + - Functions now receive these structs instead of direct function pointers. |
| 164 | + |
| 165 | +- **Allocation Functions**: |
| 166 | + - **Allocation**: |
| 167 | + ```c |
| 168 | + typedef void* (*CanardMemoryAllocate)( |
| 169 | + void* const user_reference, |
| 170 | + const size_t size); |
| 171 | + ``` |
| 172 | + - **Deallocation**: |
| 173 | + ```c |
| 174 | + typedef void (*CanardMemoryDeallocate)( |
| 175 | + void* const user_reference, |
| 176 | + const size_t size, |
| 177 | + void* const pointer); |
| 178 | + ``` |
| 179 | + |
| 180 | +## Migration Steps |
| 181 | + |
| 182 | +1. **Update Type Definitions**: |
| 183 | + - Replace all `typedef`-based enum and struct types with direct `struct` and `enum` declarations. |
| 184 | + - For example, change `CanardInstance` to `struct CanardInstance`. |
| 185 | + |
| 186 | +2. **Adjust Memory Management Code**: |
| 187 | + - Replace separate memory allocation and deallocation function pointers with `CanardMemoryResource` and `CanardMemoryDeleter` structs. |
| 188 | + - Update function calls and definitions accordingly. |
| 189 | + |
| 190 | +3. **Modify Function Calls**: |
| 191 | + - Update all function calls to match the new prototypes. |
| 192 | + - **`canardInit`**: |
| 193 | + - Before: |
| 194 | + ```c |
| 195 | + canardInit(memory_allocate, memory_free); |
| 196 | + ``` |
| 197 | + - After: |
| 198 | + ```c |
| 199 | + struct CanardMemoryResource memory = { |
| 200 | + .user_reference = ..., |
| 201 | + .allocate = memory_allocate, |
| 202 | + .deallocate = memory_free |
| 203 | + }; |
| 204 | + canardInit(memory); |
| 205 | + ``` |
| 206 | + - **`canardTxInit`**: |
| 207 | + - Before: |
| 208 | + ```c |
| 209 | + canardTxInit(capacity, mtu_bytes); |
| 210 | + ``` |
| 211 | + - After: |
| 212 | + ```c |
| 213 | + canardTxInit(capacity, mtu_bytes, memory); |
| 214 | + ``` |
| 215 | + - **`canardTxPush`**: |
| 216 | + - Before: |
| 217 | + ```c |
| 218 | + canardTxPush(que, ins, tx_deadline_usec, metadata, payload_size, payload); |
| 219 | + ``` |
| 220 | + - After: |
| 221 | + ```c |
| 222 | + struct CanardPayload payload_struct = { |
| 223 | + .size = payload_size, |
| 224 | + .data = payload |
| 225 | + }; |
| 226 | + canardTxPush(que, ins, tx_deadline_usec, metadata, payload_struct); |
| 227 | + ``` |
| 228 | + |
| 229 | +4. **Handle New Functions**: |
| 230 | + - Use `canardTxFree` to deallocate transmission queue items after popping them. |
| 231 | + - Example: |
| 232 | + ```c |
| 233 | + struct CanardTxQueueItem* item = canardTxPeek(&tx_queue); |
| 234 | + while (item != NULL) { |
| 235 | + // Transmit the frame... |
| 236 | + canardTxPop(&tx_queue, item); |
| 237 | + canardTxFree(&tx_queue, &canard_instance, item); |
| 238 | + item = canardTxPeek(&tx_queue); |
| 239 | + } |
| 240 | + ``` |
| 241 | + |
| 242 | +5. **Update Struct Field Access**: |
| 243 | + - Adjust your code to access struct fields directly, considering the changes in struct definitions. |
| 244 | + - For example, access `payload.size` instead of `payload_size`. |
| 245 | + |
| 246 | +6. **Adjust Memory Allocation Logic**: |
| 247 | + - Ensure that your memory allocation and deallocation functions conform to the new prototypes. |
| 248 | + - Pay attention to the additional `size` parameter in the deallocation function. |
| 249 | + |
| 250 | +7. **Test Thoroughly**: |
| 251 | + - After making the changes, thoroughly test your application to ensure that it functions correctly with the new library version. |
| 252 | + - Pay special attention to memory management and potential leaks. |
0 commit comments