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
process_c.c
1
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <stdint.h>
16#include <unistd.h>
17#include <time.h>
18
19typedef struct {
20 uint32_t train_id;
21 uint32_t position;
22 uint8_t signal_state;
23 uint8_t emergency_stop;
24 uint16_t speed_limit;
25 uint32_t timestamp_ms;
26 uint32_t checksum;
27 uint32_t sequence_number;
29
30typedef struct {
31 train_command_t cmd_a;
32 train_command_t cmd_b;
33 train_command_t cmd_a_out;
34 train_command_t cmd_b_out;
35 volatile uint32_t a_ready;
36 volatile uint32_t b_ready;
37 volatile uint32_t a_compared;
38 volatile uint32_t b_compared;
39 volatile uint8_t outputs_match;
40 uint32_t mismatch_count;
41 uint32_t sequence;
42 uint32_t timestamp_ms;
44
45__attribute__((aligned(64)))
46extern shared_state_t shared_mem;
47
48typedef struct {
49 uint32_t total_cycles;
50 uint32_t matches;
51 uint32_t mismatches;
52 uint32_t output_errors;
54
55static service_stats_t stats = {0};
56
57int main(int argc, char *argv[])
58{
59 (void)argc;
60 (void)argv;
61
62 uint32_t cycle_count = 0;
63 uint32_t last_sequence = 0;
64
65 printf("\n");
66 printf("╔════════════════════════════════════════════════════════╗\n");
67 printf("║ Process C - Non-Vital Service Channel ║\n");
68 printf("║ Displays voted results, monitors A/B agreement ║\n");
69 printf("╚════════════════════════════════════════════════════════╝\n");
70 printf("\n");
71
72 while (1) {
73 cycle_count++;
74
75 /* Check if A and B have voted */
76 if (shared_mem.sequence != last_sequence) {
77 stats.total_cycles++;
78 last_sequence = shared_mem.sequence;
79
80 /* Display voted command */
81 printf("[WEST:C] Cycle %u:\n", cycle_count);
82
83 if (shared_mem.outputs_match) {
84 stats.matches++;
85 printf(" A & B AGREE ✓\n");
86 } else {
87 stats.output_errors++;
88 printf(" A & B DISAGREE ✗ (SHOULD NOT HAPPEN!)\n");
89 }
90
91 printf(" Joint Output: train=%u, sig=%u, speed=%u km/h\n",
92 shared_mem.cmd_a_out.train_id,
93 shared_mem.cmd_a_out.signal_state,
94 shared_mem.cmd_a_out.speed_limit);
95
96 printf(" A input: sig=%u, speed=%u | "
97 "B input: sig=%u, speed=%u\n",
98 shared_mem.cmd_a.signal_state,
99 shared_mem.cmd_a.speed_limit,
100 shared_mem.cmd_b.signal_state,
101 shared_mem.cmd_b.speed_limit);
102
103 /* Detect mismatches (comparison disagreement) */
104 if ((shared_mem.cmd_a.signal_state != shared_mem.cmd_b.signal_state ||
105 shared_mem.cmd_a.speed_limit != shared_mem.cmd_b.speed_limit) &&
106 (shared_mem.cmd_a_out.signal_state == 0 &&
107 shared_mem.cmd_a_out.speed_limit == 0)) {
108
109 stats.mismatches++;
110 printf(" ⚠️ MISMATCH DETECTED (contained via safe state)\n");
111 }
112
113 printf("\n");
114 }
115
116 /* Periodic statistics */
117 if ((cycle_count % 20) == 0) {
118 printf("┌────── Statistics ──────┐\n");
119 printf("│ Total cycles: %u\n", stats.total_cycles);
120 printf("│ Agreements: %u (%.0f%%)\n", stats.matches,
121 stats.total_cycles > 0 ?
122 (100.0 * stats.matches / stats.total_cycles) : 0.0);
123 printf("│ Mismatches detected: %u\n", stats.mismatches);
124 printf("│ Output errors: %u\n", stats.output_errors);
125 printf("└────────────────────────┘\n");
126 printf("\n");
127 }
128
129 usleep(50000); /* Non-deterministic service */
130 }
131
132 return EXIT_SUCCESS;
133}