Skip to content

Commit ea8aa7a

Browse files
committed
Add macros to eliminate magic constants from Compost transport implementations in C
1 parent 51744d4 commit ea8aa7a

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

compost_rpc/lib/c/header_template.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222
#define COMPOST_EFLAGS -91 ///< Reserved flags set
2323
#define COMPOST_ETXN -92 ///< Unexcepted transaction value
2424
25+
/// Length of the message header in bytes
26+
#define COMPOST_HEADER_LEN 4
27+
28+
/**
29+
* @brief Calculates the length of the entire message in bytes from the first byte of the header
30+
* @param msg Pointer to the message
31+
*/
32+
#define COMPOST_MSG_LEN(msg) (4 + (4 * (((uint8_t *)msg)[0])))
33+
34+
/**
35+
* @brief Calculates the length of the payload in bytes from the first byte of the header
36+
* @param msg Pointer to the message
37+
*/
38+
#define COMPOST_PAYLOAD_LEN(msg) (4 * (((uint8_t *)msg)[0]))
39+
2540
struct CompostMsg {
2641
uint16_t len;
2742
uint16_t rpc_id;

examples/pc_to_pc/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ int main(int argc, char *argv[])
8787
for (;;) {
8888

8989
// Read the header (first 4 bytes)
90-
if (read_bytes(connection, rx_buf, HEADER_SIZE) < 1)
90+
if (read_bytes(connection, rx_buf, COMPOST_HEADER_LEN) < 1)
9191
break;
9292

9393
// Calculate the size of the payload
9494
// (first byte of the header indicates the number of 32-bit words of the payload)
95-
size_t rx_payload_size = 4 * rx_buf[0];
95+
size_t rx_payload_size = COMPOST_PAYLOAD_LEN(rx_buf);
9696

9797
// Read the payload
98-
if (read_bytes(connection, rx_buf + HEADER_SIZE, rx_payload_size) < 1)
98+
if (read_bytes(connection, rx_buf + COMPOST_HEADER_LEN, rx_payload_size) < 1)
9999
break;
100100

101101
// Process the request and prepare the response
102102
int16_t tx_msg_size =
103-
compost_msg_process(tx_buf, sizeof(tx_buf), rx_buf, HEADER_SIZE + rx_payload_size);
103+
compost_msg_process(tx_buf, sizeof(tx_buf), rx_buf, COMPOST_MSG_LEN(rx_buf));
104104

105105
// If the message size is negative, it indicates an error
106106
if (tx_msg_size > 0) {

test/mock/main.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,18 @@ int main(int argc, char *argv[])
9292
compost_set_assert_func(compost_assert);
9393

9494
for (;;) {
95-
if (fread(rx_buf, 1, 4, stdin) != 4) {
95+
if (fread(rx_buf, 1, COMPOST_HEADER_LEN, stdin) != 4) {
9696
break;
9797
}
98-
uint16_t data_len8 = rx_buf[0] * 4;
99-
if (data_len8 > 0) {
100-
if (fread(rx_buf + 4, 1, data_len8, stdin) != data_len8) {
98+
size_t payload_len = COMPOST_PAYLOAD_LEN(rx_buf);
99+
if (payload_len > 0) {
100+
if (fread(rx_buf + COMPOST_HEADER_LEN, 1, payload_len, stdin) != payload_len) {
101101
break;
102102
}
103103
}
104-
log_msg(" mock <- ", rx_buf, 4 + data_len8);
105-
int16_t tx_len = compost_msg_process(tx_buf, sizeof(tx_buf), rx_buf, 4 + data_len8);
104+
size_t msg_len = COMPOST_MSG_LEN(rx_buf);
105+
log_msg(" mock <- ", rx_buf, msg_len);
106+
int16_t tx_len = compost_msg_process(tx_buf, sizeof(tx_buf), rx_buf, msg_len);
106107
log_msg(" mock -> ", tx_buf, tx_len);
107108

108109
if (tx_len) {

0 commit comments

Comments
 (0)