33#include "rte/status/rte_status.h"
34#include "rte/types/rte_types.h"
35#include "rte/buffer/rte_buffer.h"
36#include "rte/string/rte_string.h"
37#include "rte/log/rte_log.h"
38#include "rte/timer/rte_timer.h"
39#include "rte/safestate/rte_safestate.h"
45#define APP_NAME "railway-message-processor"
46#define APP_VERSION "1.0.0"
47#define MESSAGE_BUFFER_SIZE 256U
48#define LOG_LINE_MAX 128U
60 uint32_t message_count;
61 uint32_t heartbeat_count;
98 char line[LOG_LINE_MAX];
101 (void)fprintf(stderr,
"Failed to initialize logging: %d\n", (
int)status);
105 (void)snprintf(line,
sizeof(line),
"Initializing %s v%s", APP_NAME, APP_VERSION);
107 g_app.state = STATE_RUNNING;
119 char line[LOG_LINE_MAX];
126 (void)snprintf(line,
sizeof(line),
"Processing message [%zu bytes]", len);
140 (void)memcpy(&position, &msg[1],
sizeof(position));
141 (void)snprintf(line,
sizeof(line),
"Train position update: %u", position);
144 (void)snprintf(line,
sizeof(line),
"Invalid POSITION_UPDATE message (len=%zu)", len);
155 (void)snprintf(line,
sizeof(line),
"Unknown message type: 0x%02x", msg_type);
160 g_app.message_count++;
175 g_app.heartbeat_count++;
178 if ((g_app.heartbeat_count % 5U) == 0U) {
179 char line[LOG_LINE_MAX];
180 (void)snprintf(line,
sizeof(line),
181 "Heartbeat #%u - Messages processed: %u, Errors: %u",
182 g_app.heartbeat_count,
195 static const uint8_t sequence[] = {0x01, 0x02, 0x01, 0x03};
196 static size_t seq_idx = 0;
198 uint8_t message[MESSAGE_BUFFER_SIZE];
201 message[0] = sequence[seq_idx % 4U];
204 if (message[0] == 0x02U) {
205 uint32_t position = 1000U + (g_app.message_count * 50U);
206 (void)memcpy(&message[1], &position,
sizeof(position));
221 if ((sig == SIGINT) || (sig == SIGTERM)) {
231 static const char *
const state_names[] = {
232 "INIT",
"RUNNING",
"ERROR",
"SHUTDOWN"
235 if ((uint32_t)new_state >= 4U) {
239 if (g_app.state == new_state) {
244 char line[LOG_LINE_MAX];
245 (void)snprintf(line,
sizeof(line),
"State transition: %s -> %s",
246 state_names[g_app.state], state_names[new_state]);
250 g_app.state = new_state;
258int main(
int argc,
char *argv[])
273 (void)fprintf(stderr,
"Initialization failed\n");
285 while ((g_app.running != 0) && (loop_count < 50U)) {
291 char line[LOG_LINE_MAX];
293 (void)snprintf(line,
sizeof(line),
"Message processing failed: %d", (
int)status);
298 if ((loop_count % 10U) == 0U) {
303 (void)usleep(100000);
312 (void)printf(
"===================================================================\n");
313 (void)printf(
"%s Summary\n", APP_NAME);
314 (void)printf(
"===================================================================\n");
315 (void)printf(
"Messages processed: %u\n", g_app.message_count);
316 (void)printf(
"Heartbeats sent: %u\n", g_app.heartbeat_count);
317 (void)printf(
"Errors encountered: %u\n", g_app.error_count);
318 (void)printf(
"Final state: SHUTDOWN\n");
319 (void)printf(
"===================================================================\n");
rte_log_level_t
Log message severity, passed to rte_log_write().
rte_status_t rte_log_init(void)
Initializes the logging OSAdapter. Safe to call once at startup.
void rte_log_write(rte_log_level_t level, const char *tag, const char *message)
Emits one log message. Non-blocking; never fails the caller's control flow even if the message is dro...
#define RTE_ASSERT(cond)
Checked assertion. Always active in every build configuration, including production (ADR-004 section ...
rte_status_t
Common result/status codes.
@ RTE_STATUS_INVALID_PARAM
struct rte_timer_impl_s * rte_timer_handle_t
static rte_status_t transition_to_state(app_state_t new_state)
Safe state transition.
static void heartbeat_callback(rte_timer_handle_t handle, void *user_context)
Timer callback - generates periodic heartbeats.
static rte_status_t app_init(void)
Initialize the application context.
static rte_status_t process_message(const uint8_t *msg, size_t len)
Process a single message (simulated).
static void log_line(rte_log_level_t level, const char *line)
Logs one already-formatted line at the given level.
static void signal_handler(int sig)
Signal handler for graceful shutdown.
static rte_status_t simulate_train_message(void)
Simulate receiving a message from train.