Skip to content

Commit 20c9edc

Browse files
authored
added migration guide (#61)
1 parent c9c29ab commit 20c9edc

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

MIGRATION_v1.x_to_v2.0.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Migration Guide: Upgrading from LibUDPard v1.x to v2.0
2+
3+
This migration guide provides step-by-step instructions to help you update your application code from LibUDPard version 1.x to version 2.0. The guide highlights the key changes in the API and offers recommendations on how to adapt your code accordingly.
4+
5+
## Introduction
6+
7+
LibUDPard version 2.0 introduces several significant changes to improve memory management and payload handling. This guide will help you understand these changes and update your application code to be compatible with the new version.
8+
9+
These changes do not affect wire compatibility.
10+
11+
## Version Changes
12+
13+
- **LibUDPard Version**:
14+
- **Old**: `UDPARD_VERSION_MAJOR 1`, `UDPARD_VERSION_MINOR 2`
15+
- **New**: `UDPARD_VERSION_MAJOR 2`, `UDPARD_VERSION_MINOR 0`
16+
- **Cyphal Specification Version**: Remains the same (`1.0`).
17+
18+
## Key API Changes
19+
20+
### UdpardTx Structure Changes
21+
22+
- **Memory Resource Field**: The `UdpardTx` structure's `memory` field type has changed from `UdpardMemoryResource` to `UdpardTxMemoryResources`.
23+
24+
```c
25+
// In v1.x
26+
struct UdpardTx {
27+
// ...
28+
struct UdpardMemoryResource memory;
29+
// ...
30+
};
31+
32+
// In v2.0
33+
struct UdpardTx {
34+
// ...
35+
struct UdpardTxMemoryResources memory;
36+
// ...
37+
};
38+
```
39+
40+
### Memory Management Adjustments
41+
42+
- **Separate Memory Resources**: `UdpardTxMemoryResources` now allows separate memory resources for fragment handles and payload storage.
43+
44+
```c
45+
struct UdpardTxMemoryResources {
46+
struct UdpardMemoryResource fragment; // For UdpardTxItem allocations
47+
struct UdpardMemoryResource payload; // For datagram payload allocations
48+
};
49+
```
50+
51+
- **Memory Allocation Changes**: The number of memory allocations per datagram has increased from one to two:
52+
- **v1.x**: One allocation per datagram (including `UdpardTxItem` and payload).
53+
- **v2.0**: Two allocations per datagram—one for `UdpardTxItem` and one for the payload.
54+
55+
### UdpardTxItem Structure Updates
56+
57+
- **Mutable datagram_payload Field**: The `datagram_payload` field in `UdpardTxItem` is now mutable, allowing ownership transfer of the payload.
58+
59+
- **New priority Field**: A new `priority` field has been added to `UdpardTxItem` to retain the original transfer priority level.
60+
61+
```c
62+
struct UdpardTxItem {
63+
// ...
64+
enum UdpardPriority priority; // New field in v2.0
65+
struct UdpardMutablePayload datagram_payload; // Now mutable
66+
// ...
67+
};
68+
```
69+
70+
### Function Signature Modifications
71+
72+
- **udpardTxInit**: The `memory` parameter type has changed to `UdpardTxMemoryResources`.
73+
74+
```c
75+
// In v1.x
76+
int_fast8_t udpardTxInit(
77+
struct UdpardTx* self,
78+
const UdpardNodeID* local_node_id,
79+
size_t queue_capacity,
80+
struct UdpardMemoryResource memory
81+
);
82+
83+
// In v2.0
84+
int_fast8_t udpardTxInit(
85+
struct UdpardTx* self,
86+
const UdpardNodeID* local_node_id,
87+
size_t queue_capacity,
88+
struct UdpardTxMemoryResources memory
89+
);
90+
```
91+
92+
- **udpardTxFree**: The `memory` parameter type has changed to `UdpardTxMemoryResources`.
93+
94+
```c
95+
// In v1.x
96+
void udpardTxFree(
97+
const struct UdpardMemoryResource memory,
98+
struct UdpardTxItem* item
99+
);
100+
101+
// In v2.0
102+
void udpardTxFree(
103+
const struct UdpardTxMemoryResources memory,
104+
struct UdpardTxItem* item
105+
);
106+
```
107+
108+
- **udpardTxPeek**: The return type has changed from `const struct UdpardTxItem*` to `struct UdpardTxItem*` to allow modification of the `datagram_payload` field.
109+
110+
```c
111+
// In v1.x
112+
const struct UdpardTxItem* udpardTxPeek(const struct UdpardTx* self);
113+
114+
// In v2.0
115+
struct UdpardTxItem* udpardTxPeek(const struct UdpardTx* self);
116+
```
117+
118+
## Migration Steps
119+
120+
Follow these steps to update your application code to be compatible with LibUDPard v2.0.
121+
122+
### 1. Update UdpardTx Initialization
123+
124+
- **Adjust the `udpardTxInit` Call**: Update the `memory` parameter to use `UdpardTxMemoryResources`.
125+
126+
```c
127+
// Before (v1.x)
128+
struct UdpardMemoryResource tx_memory = { /*...*/ };
129+
udpardTxInit(&tx_instance, &local_node_id, queue_capacity, tx_memory);
130+
131+
// After (v2.0)
132+
struct UdpardTxMemoryResources tx_memory = {
133+
.fragment = { /*...*/ },
134+
.payload = { /*...*/ }
135+
};
136+
udpardTxInit(&tx_instance, &local_node_id, queue_capacity, tx_memory);
137+
```
138+
139+
- **Define Separate Memory Resources**: Initialize separate memory resources for fragments and payloads.
140+
141+
### 2. Adjust Memory Resources
142+
143+
- **Update Memory Allocation Logic**: Ensure that your memory allocator handles two separate allocations per datagram—one for `UdpardTxItem` and one for the payload.
144+
145+
```c
146+
// Example allocator adjustments
147+
void* allocate_fragment(void* user_reference, size_t size) { /*...*/ }
148+
void* allocate_payload(void* user_reference, size_t size) { /*...*/ }
149+
```
150+
151+
### 3. Modify UdpardTxItem Usage
152+
153+
- **Handle Mutable Payloads**: Since `datagram_payload` is now mutable, you can transfer ownership of the payload to another component (e.g., transmission media) by nullifying the `size` and `data` fields after copying.
154+
155+
```c
156+
struct UdpardTxItem* tx_item = udpardTxPeek(&tx_instance);
157+
if (tx_item) {
158+
// Transfer ownership of the payload
159+
transmit_payload(tx_item->datagram_payload.data, tx_item->datagram_payload.size);
160+
tx_item->datagram_payload.data = NULL;
161+
tx_item->datagram_payload.size = 0;
162+
163+
// Pop and free the item after transmission
164+
udpardTxPop(&tx_instance, tx_item);
165+
udpardTxFree(tx_instance.memory, tx_item);
166+
}
167+
```
168+
169+
- **Utilize the New priority Field**: Access the `priority` field in `UdpardTxItem` if needed for your application logic.
170+
171+
```c
172+
enum UdpardPriority tx_priority = tx_item->priority;
173+
```
174+
175+
### 4. Revise Function Calls
176+
177+
- **Update `udpardTxFree` Calls**: Pass the updated `memory` parameter type.
178+
179+
```c
180+
// Before (v1.x)
181+
udpardTxFree(tx_memory, tx_item);
182+
183+
// After (v2.0)
184+
udpardTxFree(tx_instance.memory, tx_item);
185+
```
186+
187+
- **Modify `udpardTxPeek` Usage**: Since `udpardTxPeek` now returns a mutable pointer, update your code to handle the mutable `UdpardTxItem`.
188+
189+
```c
190+
// Before (v1.x)
191+
const struct UdpardTxItem* tx_item = udpardTxPeek(&tx_instance);
192+
193+
// After (v2.0)
194+
struct UdpardTxItem* tx_item = udpardTxPeek(&tx_instance);
195+
```
196+
197+
- **Ensure Correct Deallocation**: When freeing payloads, use the appropriate memory resource from `UdpardTxMemoryResources`.

0 commit comments

Comments
 (0)