Skip to content

Commit 3e1a147

Browse files
blazraSTmoucha19-st
authored andcommitted
Type and API rectification
Makes `compost_header_load` and `store` functions public. Changes some type signatures to more common types. Functionally not much change.
1 parent a4f96c6 commit 3e1a147

8 files changed

Lines changed: 101 additions & 67 deletions

File tree

compost_rpc/compost_rpc.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,15 +1681,15 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
16811681
/**
16821682
* {notif.__doc__}
16831683
*/
1684-
int16_t {notif.name}_store(uint8_t *tx_buf, size_t tx_buf_size{params});
1684+
int16_t {notif.name}_store(uint8_t *tx_buf{params});
16851685
"""
16861686
protocol_source += f"""/**
16871687
* Serialization function for {notif.name} notification
16881688
*/
1689-
int16_t {notif.name}_store(uint8_t *tx_buf, size_t tx_buf_size{params})
1689+
int16_t {notif.name}_store(uint8_t *tx_buf{params})
16901690
{{
16911691
struct CompostMsg tx = {{
1692-
.txn = 0,
1692+
.header = {{ .txn = 0 }},
16931693
.payload_buf = tx_buf + PAYLOAD_OFFSET
16941694
}};
16951695
uint8_t *dest = tx.payload_buf;
@@ -1698,9 +1698,11 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
16981698
for param_name, param_type in notif.get_param_items():
16991699
protocol_source.add(self._store_call(param_type.annotation, param_name))
17001700
protocol_source.indent_dec()
1701-
protocol_source += f""" tx.rpc_id = {notif.name.upper()};
1702-
tx.len = compost_bytes_to_words(dest - tx.payload_buf);
1703-
return compost_header_set(tx_buf, tx_buf_size, tx);
1701+
protocol_source += f""" tx.header.rpc_id = {notif.name.upper()};
1702+
size_t payload_len = dest - tx.payload_buf;
1703+
tx.header.wlen = compost_bytes_to_words(payload_len);
1704+
compost_header_store(tx_buf, tx.header);
1705+
return 4 + payload_len;
17041706
}}
17051707
17061708
"""
@@ -1750,10 +1752,10 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
17501752
invoke_fn.add('uint8_t *dest = tx->payload_buf;')
17511753
invoke_fn.add(self._store_call(sig.return_annotation, "ret"))
17521754
if sig.return_annotation is Signature.empty:
1753-
invoke_fn.add("tx->len = 0;")
1755+
invoke_fn.add("tx->header.wlen = 0;")
17541756
else:
1755-
invoke_fn.add("tx->len = compost_bytes_to_words(dest - tx->payload_buf);")
1756-
invoke_fn.add(f"tx->rpc_id = {rpc.name.upper()};")
1757+
invoke_fn.add("tx->header.wlen = compost_bytes_to_words(dest - tx->payload_buf);")
1758+
invoke_fn.add(f"tx->header.rpc_id = {rpc.name.upper()};")
17571759
invoke_fn += "}\n"
17581760

17591761
protocol_header += invoke_prototype
@@ -1763,13 +1765,13 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
17631765
rpc_id_fns = """
17641766
void compost_unknown_msg(struct CompostMsg *tx)
17651767
{
1766-
tx->rpc_id = UNKNOWN_MSG;
1767-
tx->len = 0;
1768+
tx->header.rpc_id = UNKNOWN_MSG;
1769+
tx->header.wlen = 0;
17681770
}
17691771
17701772
void compost_invoke_switch(struct CompostMsg *tx, const struct CompostMsg rx)
17711773
{
1772-
switch (rx.rpc_id) {
1774+
switch (rx.header.rpc_id) {
17731775
"""
17741776
for rpc in self._protocol._rpcs.values():
17751777
if rpc.is_notification and not endpoint.is_call_inbound(rpc):

compost_rpc/lib/c/header_template.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@
3737
*/
3838
#define COMPOST_PAYLOAD_LEN(msg) (4 * (((uint8_t *)msg)[0]))
3939
40+
struct CompostHeader {
41+
uint8_t wlen; ///< Word length of the payload (payload length in bytes is wlen * 4)
42+
uint8_t txn; ///< Transaction identifier
43+
uint16_t rpc_id; ///< RPC identifier
44+
bool resp; ///< Response flag
45+
};
46+
4047
struct CompostMsg {
41-
uint16_t len;
42-
uint16_t rpc_id;
43-
const uint16_t payload_buf_size;
44-
uint8_t txn;
45-
bool resp;
48+
struct CompostHeader header;
49+
const size_t payload_buf_size;
4650
uint8_t *payload_buf;
4751
};
4852
@@ -258,7 +262,23 @@
258262
* @return Size of valid data in bytes in the transmit buffer, or zero if there
259263
* is no message to send, or negative value if there was an error
260264
*/
261-
int16_t compost_msg_process(uint8_t *, const uint16_t, uint8_t *const, const uint16_t);
265+
int compost_msg_process(uint8_t *tx_buf, const size_t tx_buf_size, uint8_t *const rx_buf, const size_t rx_buf_size);
266+
267+
/**
268+
* @brief Loads the message header from the buffer.
269+
* @param buf Pointer to the buffer containing the header
270+
* @param header Pointer to the CompostHeader struct where to store the header data
271+
* @return Zero on success, or negative value if there was an error (e.g. invalid header)
272+
*/
273+
int compost_header_load(uint8_t *buf, struct CompostHeader *header);
274+
275+
/**
276+
* @brief Stores the message header to the buffer.
277+
* @param buf Pointer to the buffer where to store the header
278+
* @param header Pointer to the CompostHeader struct containing the header data
279+
* @return Zero on success, or negative value if there was an error (e.g. insufficient buffer size)
280+
*/
281+
int compost_header_store(uint8_t *buf, const struct CompostHeader header);
262282
263283
/**
264284
* @brief Initializes an allocator.

compost_rpc/lib/c/source_template.py

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SourcePart:
3838
} while (0)
3939
#endif
4040
41-
#define LEN_OFFSET 0
41+
#define WLEN_OFFSET 0
4242
#define TXN_OFFSET 1
4343
#define RPC_ID_HI_AND_RESP_OFFSET 2
4444
#define RPC_ID_LO_OFFSET 3
@@ -401,66 +401,78 @@ class SourcePart:
401401
/* P U B L I C A P I F U N C T I O N S */
402402
/******************************************************************************/
403403
404-
int16_t compost_header_set(uint8_t *tx_buf, uint16_t tx_buf_size, const struct CompostMsg tx)
404+
int compost_header_load(uint8_t *buf, struct CompostHeader *header)
405405
{
406-
if (tx_buf == NULL ) {
406+
if (buf == NULL || header == NULL) {
407407
return COMPOST_EINVAL;
408408
}
409-
if (tx_buf_size < 4) {
410-
return COMPOST_EMSGSIZE;
411-
}
412-
COMPOST_ASSERT(tx.rpc_id <= 0x0FFF);
413-
tx_buf[LEN_OFFSET] = tx.len;
414-
tx_buf[TXN_OFFSET] = tx.txn;
415-
tx_buf[RPC_ID_HI_AND_RESP_OFFSET] = tx.rpc_id >> 8;
416-
if (tx.resp) {
417-
tx_buf[RPC_ID_HI_AND_RESP_OFFSET] |= RESP_MASK;
409+
410+
uint8_t rpc_id_hi = buf[RPC_ID_HI_AND_RESP_OFFSET] & RPC_ID_HI_MASK;
411+
uint8_t flags = buf[RPC_ID_HI_AND_RESP_OFFSET] & FLAGS_MASK;
412+
413+
if (flags & ~RESP_MASK) {
414+
return COMPOST_EFLAGS;
418415
}
419-
tx_buf[RPC_ID_LO_OFFSET] = tx.rpc_id;
420-
if (tx.len > 255) {
421-
return COMPOST_EMSGSIZE;
416+
417+
header->wlen = buf[WLEN_OFFSET];
418+
header->txn = buf[TXN_OFFSET];
419+
header->resp = flags & RESP_MASK;
420+
header->rpc_id = ((uint16_t)rpc_id_hi << 8) | buf[RPC_ID_LO_OFFSET];
421+
return 0;
422+
}
423+
424+
int compost_header_store(uint8_t *buf, const struct CompostHeader header)
425+
{
426+
if (buf == NULL) {
427+
return COMPOST_EINVAL;
422428
}
423-
int16_t msg_size = (tx.len * 4) + 4;
424-
if (msg_size > tx_buf_size) {
425-
return COMPOST_EMSGSIZE;
429+
COMPOST_ASSERT(header.rpc_id <= 0x0FFF);
430+
buf[WLEN_OFFSET] = header.wlen;
431+
buf[TXN_OFFSET] = header.txn;
432+
buf[RPC_ID_HI_AND_RESP_OFFSET] = header.rpc_id >> 8;
433+
if (header.resp) {
434+
buf[RPC_ID_HI_AND_RESP_OFFSET] |= RESP_MASK;
426435
}
427-
return msg_size;
436+
buf[RPC_ID_LO_OFFSET] = header.rpc_id;
437+
return 0;
428438
}
429439
430-
int16_t compost_msg_process(uint8_t *tx_buf, const uint16_t tx_buf_size, uint8_t *const rx_buf,
431-
const uint16_t rx_buf_size)
440+
int compost_msg_process(uint8_t *tx_buf, const size_t tx_buf_size, uint8_t *const rx_buf,
441+
const size_t rx_buf_size)
432442
{
433443
if ((rx_buf_size < 4) || (tx_buf_size < 4) || tx_buf == NULL || rx_buf == NULL) {
434444
return COMPOST_EINVAL;
435445
}
436446
437-
uint8_t rpc_id_hi = rx_buf[RPC_ID_HI_AND_RESP_OFFSET] & RPC_ID_HI_MASK;
438-
uint8_t flags = rx_buf[RPC_ID_HI_AND_RESP_OFFSET] & FLAGS_MASK;
439-
440-
if (flags & ~RESP_MASK) {
441-
return COMPOST_EFLAGS;
447+
struct CompostHeader rx_header;
448+
int ret = compost_header_load(rx_buf, &rx_header);
449+
if (ret) {
450+
return ret;
442451
}
443452
444-
const struct CompostMsg rx = {.len = rx_buf[LEN_OFFSET],
445-
.txn = rx_buf[TXN_OFFSET],
446-
.resp = (flags & RESP_MASK) != 0,
447-
.rpc_id = ((uint16_t)rpc_id_hi << 8) | rx_buf[RPC_ID_LO_OFFSET],
453+
const struct CompostMsg rx = { .header = rx_header,
448454
.payload_buf = rx_buf + PAYLOAD_OFFSET,
449455
.payload_buf_size = rx_buf_size - PAYLOAD_OFFSET};
450456
451-
struct CompostMsg tx = {.txn = rx.txn,
452-
.resp = true,
457+
struct CompostMsg tx = { .header.txn = rx.header.txn,
458+
.header.resp = true,
459+
.header.wlen = 0,
460+
.header.rpc_id = 0,
453461
.payload_buf_size = tx_buf_size - PAYLOAD_OFFSET,
454462
.payload_buf = tx_buf + PAYLOAD_OFFSET};
455463
456-
if (rx.txn != 0 && rx.resp == true) {
464+
if (rx.header.txn != 0 && rx.header.resp == true) {
457465
return COMPOST_ETXN; // We don't support sending RPC requests, so we can't get a response
458466
}
459467
460468
compost_invoke_switch(&tx, rx);
461469
462-
if (tx.txn || tx.len) {
463-
return compost_header_set(tx_buf, tx_buf_size, tx);
470+
if (tx.header.txn || tx.header.wlen) {
471+
ret = compost_header_store(tx_buf, tx.header);
472+
if (ret) {
473+
return ret;
474+
}
475+
return 4 + (tx.header.wlen * 4); // Total message length is header (4 bytes) + payload
464476
} else {
465477
return 0; // Nothing to send
466478
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[
2-
{ "name": "LEN", "bits": 8, "type": 2 },
2+
{ "name": "WLEN", "bits": 8, "type": 2 },
33
{ "name": "TXN", "bits": 8, "type": 3 },
44
{ "name": "Reserved", "bits": 3, "type": 5, "rotate": -50 },
55
{ "name": "RESP", "bits": 1, "type": 4, "rotate": -90 },
66
{ "name": "RPC_ID", "bits": 12, "type": 7 },
7-
{ "name": "PAYLOAD", "bits": 32, "type": 1, "attr": "Variable length payload. Length is 4 * LEN bytes." }
7+
{ "name": "PAYLOAD", "bits": 32, "type": 1, "attr": "Variable length payload. Length is 4 * WLEN bytes." }
88
]

docs/_static/image/getting_started/msg_structure.svg

Lines changed: 3 additions & 3 deletions
Loading

docs/_static/image/getting_started/msg_structure_white.svg

Lines changed: 3 additions & 3 deletions
Loading

test/mock/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ int main(int argc, char *argv[])
120120
case 0x0e00:
121121
time(&current_epoch);
122122
struct MockDate date = epoch_to_date_handler(current_epoch, &alloc);
123-
tx_len = notify_date_store(tx_buf, sizeof(tx_buf), date);
123+
tx_len = notify_date_store(tx_buf, date);
124124
break;
125125
case 0x0e02:
126-
tx_len = notify_heartbeat_store(tx_buf, sizeof(tx_buf));
126+
tx_len = notify_heartbeat_store(tx_buf);
127127
break;
128128
case 0x0e03:
129-
tx_len = notify_bitwise_complement_store(tx_buf, sizeof(tx_buf), 0xAAAAAAAAAAAAAAAA, 0x5555555555555555);
129+
tx_len = notify_bitwise_complement_store(tx_buf, 0xAAAAAAAAAAAAAAAA, 0x5555555555555555);
130130
break;
131131
default:
132132
notif_requested = false;

test/protocol_impl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void notify_motor_control_handler(struct MockMotorControl control)
241241
compost_slice_set(report.voltage, i, i + 11);
242242
compost_slice_set(report.current, i, i + 41);
243243
}
244-
int16_t tx_len = notify_motor_report_store(tx_buf_notif, sizeof(tx_buf_notif), report);
244+
int16_t tx_len = notify_motor_report_store(tx_buf_notif, report);
245245
if (tx_len > 0) {
246246
fwrite(tx_buf_notif, 1, tx_len, stdout);
247247
fflush(stdout);
@@ -262,7 +262,7 @@ void notify_motor_report_handler(struct MockMotorReport report)
262262
control.state = MOTOR_STATE_STOP;
263263
control.direction = MOTOR_DIRECTION_DOWN;
264264
control.pwm_duty = 1200;
265-
int16_t tx_len = notify_motor_control_store(tx_buf_notif, sizeof(tx_buf_notif), control);
265+
int16_t tx_len = notify_motor_control_store(tx_buf_notif, control);
266266
if (tx_len > 0) {
267267
fwrite(tx_buf_notif, 1, tx_len, stdout);
268268
fflush(stdout);
@@ -290,7 +290,7 @@ void notify_bitfields_handler(struct BitfieldStruct config)
290290
config.ststart = ~config.ststart;
291291
config.temp = VOLTAGES_MV_37_50;
292292
config.tnom = ~config.tnom;
293-
uint16_t tx_len = notify_bitfields_store(tx_buf_notif, sizeof(tx_buf_notif), config);
293+
uint16_t tx_len = notify_bitfields_store(tx_buf_notif, config);
294294
fwrite(tx_buf_notif, 1, tx_len, stdout);
295295
fflush(stdout);
296296
}

0 commit comments

Comments
 (0)