Safe API Framework 0.1.0 build 384e6ae
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
main.c File Reference

Linux POSIX example application demonstrating RteFramework. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "rte/status/rte_status.h"
#include "rte/types/rte_types.h"
#include "rte/buffer/rte_buffer.h"
#include "rte/string/rte_string.h"
#include "rte/log/rte_log.h"
#include "rte/timer/rte_timer.h"
#include "rte/safestate/rte_safestate.h"
Include dependency graph for main.c:

Go to the source code of this file.

Data Structures

struct  app_context_t

Macros

#define APP_NAME   "railway-message-processor"
#define APP_VERSION   "1.0.0"
#define MESSAGE_BUFFER_SIZE   256U
#define LOG_LINE_MAX   128U

Enumerations

enum  app_state_t { STATE_INIT , STATE_RUNNING , STATE_ERROR , STATE_SHUTDOWN }

Functions

static void log_line (rte_log_level_t level, const char *line)
 Logs one already-formatted line at the given level.
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 heartbeat_callback (rte_timer_handle_t handle, void *user_context)
 Timer callback - generates periodic heartbeats.
static rte_status_t simulate_train_message (void)
 Simulate receiving a message from train.
static void signal_handler (int sig)
 Signal handler for graceful shutdown.
static rte_status_t transition_to_state (app_state_t new_state)
 Safe state transition.
int main (int argc, char *argv[])

Variables

static app_context_t g_app

Detailed Description

Linux POSIX example application demonstrating RteFramework.

This example shows a simple railway message processor running on Linux with POSIX OAL. It demonstrates:

  • Timer management (periodic message processing)
  • Task scheduling and IPC
  • Safe state transitions
  • Structured logging
  • Error handling with status codes

Use case: Simple train communication handler

  • Main task processes incoming messages
  • Timer callback generates heartbeats
  • All operations logged with safe state management

No timer/log OSAdapter is registered in this minimal example (a real integration wires one per ADR-005 - see safeAPIRBC2oo2's src/posix_osadapter/ for a full POSIX OSAdapter), so rte_log_write() is a documented no-op here (REQ-OAL-LOG-001: a missing OSAdapter never blocks or fails the caller) and the timer callback is invoked directly rather than through a real rte_timer_create()/_start() - see the main loop below.

Definition in file main.c.

Macro Definition Documentation

◆ APP_NAME

#define APP_NAME   "railway-message-processor"

Definition at line 45 of file main.c.

◆ APP_VERSION

#define APP_VERSION   "1.0.0"

Definition at line 46 of file main.c.

◆ MESSAGE_BUFFER_SIZE

#define MESSAGE_BUFFER_SIZE   256U

Definition at line 47 of file main.c.

◆ LOG_LINE_MAX

#define LOG_LINE_MAX   128U

Definition at line 48 of file main.c.

Enumeration Type Documentation

◆ app_state_t

enum app_state_t

Definition at line 51 of file main.c.

Function Documentation

◆ log_line()

void log_line ( rte_log_level_t level,
const char * line )
static

Logs one already-formatted line at the given level.

rte_log_write() takes a fixed message string, deliberately no varargs (MISRA C:2012 Rule 17.1 - see its own header) - callers below build the line with snprintf() into a fixed-size buffer first, at the call site, rather than through a variadic wrapper here, to keep that same no-varargs discipline visible in this example rather than reintroducing it one layer down.

Definition at line 88 of file main.c.

◆ app_init()

rte_status_t app_init ( void )
static

Initialize the application context.

Definition at line 96 of file main.c.

◆ process_message()

rte_status_t process_message ( const uint8_t * msg,
size_t len )
static

Process a single message (simulated).

Parameters
msgMessage data
lenMessage length

Definition at line 117 of file main.c.

◆ heartbeat_callback()

void heartbeat_callback ( rte_timer_handle_t handle,
void * user_context )
static

Timer callback - generates periodic heartbeats.

Parameters
handleTimer that expired (unused - invoked directly in this minimal example, not through a real timer OSAdapter)
user_contextApplication context

Definition at line 170 of file main.c.

◆ simulate_train_message()

rte_status_t simulate_train_message ( void )
static

Simulate receiving a message from train.

Definition at line 192 of file main.c.

◆ signal_handler()

void signal_handler ( int sig)
static

Signal handler for graceful shutdown.

Definition at line 219 of file main.c.

◆ transition_to_state()

rte_status_t transition_to_state ( app_state_t new_state)
static

Safe state transition.

Definition at line 229 of file main.c.

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 258 of file main.c.

Variable Documentation

◆ g_app

app_context_t g_app
static
Initial value:
= {
.state = STATE_INIT,
.message_count = 0,
.heartbeat_count = 0,
.error_count = 0,
.running = 1
}

Definition at line 66 of file main.c.