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
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <stdint.h>
22#include <unistd.h>
23#include <time.h>
24
25/* ============================================================================
26 * Shared Memory (Same as Processes A & B)
27 * ========================================================================== */
28
29typedef struct {
30 uint32_t train_id;
31 uint32_t position;
32 uint8_t signal_state;
33 uint8_t emergency_stop;
34 uint16_t speed_limit;
35 uint32_t timestamp_ms;
36 uint32_t checksum;
37 uint32_t sequence_number;
39
40typedef struct {
41 train_command_t cmd_a;
42 train_command_t cmd_b;
43 uint32_t process_a_seq;
44 uint32_t process_b_seq;
45 volatile uint8_t ready_a;
46 volatile uint8_t ready_b;
47 volatile uint8_t process_a_healthy;
48 volatile uint8_t process_b_healthy;
49 uint32_t timestamp_ms;
51
52__attribute__((aligned(64)))
53extern shared_state_t shared_mem;
54
55/* ============================================================================
56 * Voted Result (from Voter process)
57 * ========================================================================== */
58
59typedef struct {
60 train_command_t voted_command;
61 uint8_t vote_result; /* 0=match, 1=mismatch, 2=timeout */
62 uint32_t voter_sequence;
63 char log_message[256];
64} voted_result_t;
65
66__attribute__((aligned(64)))
67static voted_result_t voted_result = {0};
68
69/* ============================================================================
70 * Statistics
71 * ========================================================================== */
72
73typedef struct {
74 uint32_t total_results;
75 uint32_t matches;
76 uint32_t mismatches;
77 uint32_t timeouts;
78 uint32_t process_a_errors;
79 uint32_t process_b_errors;
81
82static service_stats_t stats = {0};
83
84/* ============================================================================
85 * Main Loop: Display & Logging
86 * ========================================================================== */
87
88int main(int argc, char *argv[])
89{
90 (void)argc;
91 (void)argv;
92
93 uint32_t cycle_count = 0;
94 uint32_t last_voter_seq = 0;
95
96 printf("\n");
97 printf("╔════════════════════════════════════════════════════════════╗\n");
98 printf("║ WEST Site - Process C (Non-Vital Service Channel) ║\n");
99 printf("║ Displays voting results and diagnostics ║\n");
100 printf("╚════════════════════════════════════════════════════════════╝\n");
101 printf("\n");
102
103 while (1) {
104 cycle_count++;
105
106 /* Check process health */
107 uint8_t process_a_healthy = shared_mem.process_a_healthy;
108 uint8_t process_b_healthy = shared_mem.process_b_healthy;
109
110 if (!process_a_healthy) {
111 stats.process_a_errors++;
112 printf("[WEST:C] ⚠️ Process A is NOT RESPONDING\n");
113 }
114
115 if (!process_b_healthy) {
116 stats.process_b_errors++;
117 printf("[WEST:C] ⚠️ Process B is NOT RESPONDING\n");
118 }
119
120 /* Display voting results (when available from Voter) */
121 if (voted_result.voter_sequence != last_voter_seq) {
122 stats.total_results++;
123 last_voter_seq = voted_result.voter_sequence;
124
125 /* Update statistics based on vote result */
126 switch (voted_result.vote_result) {
127 case 0:
128 stats.matches++;
129 break;
130 case 1:
131 stats.mismatches++;
132 break;
133 case 2:
134 stats.timeouts++;
135 break;
136 }
137
138 /* Display the result */
139 printf("[WEST:C] Cycle %u: ", cycle_count);
140 printf("%s\n", voted_result.log_message);
141 printf(" → Signal=%u, Speed=%u km/h, EmergencyStop=%u\n",
142 voted_result.voted_command.signal_state,
143 voted_result.voted_command.speed_limit,
144 voted_result.voted_command.emergency_stop);
145 }
146
147 /* Periodically print statistics */
148 if ((cycle_count % 20) == 0) {
149 printf("\n");
150 printf("┌────── WEST:C Statistics ──────┐\n");
151 printf("│ Total results: %u\n", stats.total_results);
152 printf("│ Matches: %u (%.0f%%)\n", stats.matches,
153 stats.total_results > 0 ?
154 (100.0 * stats.matches / stats.total_results) : 0.0);
155 printf("│ Mismatches: %u\n", stats.mismatches);
156 printf("│ Timeouts: %u\n", stats.timeouts);
157 printf("│ Process A err: %u\n", stats.process_a_errors);
158 printf("│ Process B err: %u\n", stats.process_b_errors);
159 printf("│ A healthy: %s, B healthy: %s\n",
160 process_a_healthy ? "YES" : "NO ",
161 process_b_healthy ? "YES" : "NO ");
162 printf("└───────────────────────────────┘\n");
163 printf("\n");
164 }
165
166 /* Service can be slow (non-deterministic) */
167 usleep(50000); /* 50ms - not deterministic like A & B */
168 }
169
170 return EXIT_SUCCESS;
171}