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
redundancy_multi_site.h
Go to the documentation of this file.
1
17
18#ifndef REDUNDANCY_MULTI_SITE_H
19#define REDUNDANCY_MULTI_SITE_H
20
21#include <stdint.h>
22#include <stdbool.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/* ============================================================================
29 * Site Identification
30 * ========================================================================== */
31
32typedef enum {
33 SITE_WEST = 0, /* Primary / Online */
34 SITE_EAST = 1, /* Backup / Standby */
35} site_id_t;
36
37typedef enum {
38 SITE_STATE_ONLINE = 0, /* Active, processing commands */
39 SITE_STATE_STANDBY = 1, /* Passive, synced but not voting */
40 SITE_STATE_ISOLATED = 2, /* No comms with other site */
41 SITE_STATE_FAULTY = 3, /* Self-detected fault, shutdown */
42} site_state_t;
43
44/* ============================================================================
45 * Safety-Related Command (Same as 2oo2 example)
46 * ========================================================================== */
47
48typedef struct {
49 uint32_t train_id;
50 uint32_t position;
51 uint8_t signal_state; /* 0=red, 1=yellow, 2=green */
52 uint8_t emergency_stop;
53 uint16_t speed_limit;
54 uint32_t timestamp_ms;
55 uint32_t checksum;
56 uint32_t sequence_number; /* For ordering & replay detection */
58
59/* ============================================================================
60 * Per-Site Channel Configuration
61 * ========================================================================== */
62
63typedef struct {
64 /* Vital channels (2oo2 voting) */
65 train_command_t channel_a; /* CPU0 computation */
66 train_command_t channel_b; /* CPU1 computation */
67
68 /* Service channel (monitoring/output) */
69 char service_output[256]; /* Printf output, logging, etc */
70
71 /* Voting result */
72 uint8_t voting_result; /* 0=match, 1=mismatch, 2=timeout */
73 train_command_t voted_command; /* Result of 2oo2 voting */
75
76/* ============================================================================
77 * Per-Site Statistics & Health
78 * ========================================================================== */
79
80typedef struct {
81 uint32_t total_votes;
82 uint32_t total_matches;
83 uint32_t total_mismatches;
84 uint32_t channel_a_errors;
85 uint32_t channel_b_errors;
86 uint32_t voting_timeouts;
87 uint32_t sequence_number; /* Last processed command sequence */
89
90typedef struct {
91 site_id_t site_id;
92 site_state_t state;
93 site_channels_t channels;
94 site_stats_t stats;
95 uint32_t heartbeat_timestamp_ms;
96 uint32_t heartbeat_miss_count;
98
99/* ============================================================================
100 * Multi-Site Voter Configuration
101 * ========================================================================== */
102
103typedef struct {
104 /* Timing */
105 uint32_t site_heartbeat_interval_ms; /* How often to check peer */
106 uint32_t heartbeat_timeout_ms; /* If no response, site is down */
107 uint32_t channel_timeout_ms; /* Voting deadline per site */
108
109 /* Failover thresholds */
110 uint32_t max_heartbeat_misses; /* Trigger failover after N misses */
111 uint32_t max_voting_errors; /* Shutdown site after N errors */
112
113 /* Hardware info */
114 const char *west_site_name; /* E.g., "ERTMS-RBC-West (Madrid)" */
115 const char *east_site_name; /* E.g., "ERTMS-RBC-East (Barcelona)" */
117
118/* ============================================================================
119 * Multi-Site Voter State
120 * ========================================================================== */
121
122typedef struct {
123 multi_site_config_t config;
124 site_context_t west;
125 site_context_t east;
126 site_id_t active_site; /* Which site is online */
128
129/* ============================================================================
130 * Public API: Multi-Site Initialization
131 * ========================================================================== */
132
140 const multi_site_config_t *config);
141
157 site_id_t site,
158 const train_command_t *cmd_a,
159 const train_command_t *cmd_b);
160
173
184 site_id_t new_active);
185
193 train_command_t *cmd_out);
194
203 char *output_buf,
204 uint32_t buf_size);
205
211
218
219#ifdef __cplusplus
220}
221#endif
222
223#endif /* REDUNDANCY_MULTI_SITE_H */
int multi_site_get_service_output(multi_site_voter_t *voter, char *output_buf, uint32_t buf_size)
Get service output from active site (for printf, logging).
void multi_site_print_stats(const multi_site_voter_t *voter)
Print multi-site statistics for diagnostics.
int multi_site_voter_shutdown(multi_site_voter_t *voter)
Shutdown multi-site voter.
int multi_site_voter_init(multi_site_voter_t *voter, const multi_site_config_t *config)
Initialize multi-site voter with West online, East standby.
int multi_site_get_active_command(multi_site_voter_t *voter, train_command_t *cmd_out)
Get voted command from currently active site.
int multi_site_vote_cycle(multi_site_voter_t *voter, site_id_t site, const train_command_t *cmd_a, const train_command_t *cmd_b)
Process voting cycle on one site.
site_id_t multi_site_heartbeat(multi_site_voter_t *voter)
Check heartbeat between sites and trigger failover if needed.
int multi_site_failover_request(multi_site_voter_t *voter, site_id_t new_active)
Trigger manual failover (for maintenance).