RTE RBC 2oo2 Reference Application 0.1.0 build 0006685
SIL 4 2oo2 Dual-Site Fault-Tolerant ERTMS Radio Block Centre Reference Architecture
Loading...
Searching...
No Matches
rbc_util_frame.h
Go to the documentation of this file.
1#ifndef RTE_EXAMPLE_RBC_UTIL_FRAME_H
2#define RTE_EXAMPLE_RBC_UTIL_FRAME_H
3
30
31#include <stdbool.h>
32#include <stdint.h>
33#include <string.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39#define RBC_UTIL_FRAME_SIZE 256U
40#define RBC_UTIL_HEADER_SIZE 6U
41#define RBC_UTIL_PAYLOAD_MAX (RBC_UTIL_FRAME_SIZE - RBC_UTIL_HEADER_SIZE) /* 250 */
42
53
55#define RBC_UTIL_TOPIC_STATUS 1U
60#define RBC_UTIL_TOPIC_JRU_LOG 2U
65#define RBC_UTIL_TOPIC_KM114_FILE 3U
70#define RBC_UTIL_TOPIC_TAKEOVER_CONFIRM 4U
71
88
91typedef struct
92{
94 uint8_t source_id;
95 uint16_t topic_id;
96 uint8_t seq;
97 uint8_t payload_len;
100
110static inline void rbc_util_frame_encode(uint8_t out[RBC_UTIL_FRAME_SIZE], rbc_util_msg_type_t msg_type,
111 uint8_t source_id, uint16_t topic_id, uint8_t seq, const char *payload)
112{
113 size_t len = 0U;
114
115 (void)memset(out, 0, RBC_UTIL_FRAME_SIZE);
116 if (payload != NULL)
117 {
118 len = strlen(payload);
119 if (len > (size_t)RBC_UTIL_PAYLOAD_MAX)
120 {
121 len = (size_t)RBC_UTIL_PAYLOAD_MAX;
122 }
123 }
124 out[0] = (uint8_t)msg_type;
125 out[1] = source_id;
126 out[2] = (uint8_t)(topic_id & 0xFFU);
127 out[3] = (uint8_t)((topic_id >> 8) & 0xFFU);
128 out[4] = seq;
129 out[5] = (uint8_t)len;
130 if (len > 0U)
131 {
132 (void)memcpy(&out[RBC_UTIL_HEADER_SIZE], payload, len);
133 }
134}
135
141static inline bool rbc_util_frame_decode(const uint8_t in[RBC_UTIL_FRAME_SIZE], rbc_util_frame_t *out)
142{
143 uint8_t plen;
144
145 if ((in == NULL) || (out == NULL))
146 {
147 return false;
148 }
149 plen = in[5];
150 if (plen > (uint8_t)RBC_UTIL_PAYLOAD_MAX)
151 {
152 return false;
153 }
154 out->msg_type = (rbc_util_msg_type_t)in[0];
155 out->source_id = in[1];
156 out->topic_id = (uint16_t)((uint16_t)in[2] | ((uint16_t)in[3] << 8));
157 out->seq = in[4];
158 out->payload_len = plen;
159 if (plen > 0U)
160 {
161 (void)memcpy(out->payload_z, &in[RBC_UTIL_HEADER_SIZE], plen);
162 }
163 out->payload_z[plen] = '\0';
164 return true;
165}
166
167#ifdef __cplusplus
168} /* extern "C" */
169#endif
170
171#endif /* RTE_EXAMPLE_RBC_UTIL_FRAME_H */
static void rbc_util_frame_encode(uint8_t out[RBC_UTIL_FRAME_SIZE], rbc_util_msg_type_t msg_type, uint8_t source_id, uint16_t topic_id, uint8_t seq, const char *payload)
Encodes a util frame into a fixed RBC_UTIL_FRAME_SIZE buffer.
#define RBC_UTIL_FRAME_SIZE
#define RBC_UTIL_HEADER_SIZE
#define RBC_UTIL_PAYLOAD_MAX
rbc_util_source_t
Stable small source ids. West/East are disambiguated by the link the frame arrived on / the log's own...
@ RBC_UTIL_SRC_B
@ RBC_UTIL_SRC_GW_STATUS
@ RBC_UTIL_SRC_GW_CTC
@ RBC_UTIL_SRC_SIM_IL
@ RBC_UTIL_SRC_UNKNOWN
@ RBC_UTIL_SRC_A
@ RBC_UTIL_SRC_GW_IL
@ RBC_UTIL_SRC_GW_TRAIN
@ RBC_UTIL_SRC_SIM_CTC
@ RBC_UTIL_SRC_SIM_TRAIN
rbc_util_msg_type_t
Control-plane opcode - byte 0 of the frame.
@ RBC_UTIL_MSG_HELLO
@ RBC_UTIL_MSG_UNSUBSCRIBE
@ RBC_UTIL_MSG_KEEPALIVE
@ RBC_UTIL_MSG_SUBSCRIBE
@ RBC_UTIL_MSG_PUBLISH
@ RBC_UTIL_MSG_BYE
static bool rbc_util_frame_decode(const uint8_t in[RBC_UTIL_FRAME_SIZE], rbc_util_frame_t *out)
Decodes a util frame. Returns false only on a NULL arg or a payload_len past the buffer (a corrupt fr...
Decoded view of a util frame (payload is a pointer into the caller's buffer, NUL-terminated into payl...
char payload_z[RBC_UTIL_PAYLOAD_MAX+1U]
rbc_util_msg_type_t msg_type