26 uint8_t emergency_stop;
28 uint32_t timestamp_ms;
30 uint32_t sequence_number;
36 uint32_t process_a_seq;
37 uint32_t process_b_seq;
38 volatile uint8_t ready_a;
39 volatile uint8_t ready_b;
40 volatile uint8_t process_a_healthy;
41 volatile uint8_t process_b_healthy;
42 uint32_t timestamp_ms;
45__attribute__((aligned(64)))
51 uint32_t voter_sequence;
52 char log_message[256];
55__attribute__((aligned(64)))
56extern voted_result_t voted_result;
68 if (cmd_a == NULL || cmd_b == NULL || result_out == NULL) {
73 if (cmd_a->train_id != cmd_b->train_id ||
74 cmd_a->position != cmd_b->position ||
75 cmd_a->signal_state != cmd_b->signal_state ||
76 cmd_a->speed_limit != cmd_b->speed_limit) {
79 snprintf(message_out, message_len,
80 "[VOTER] MISMATCH: A(train=%u,sig=%u,spd=%u) vs "
81 "B(train=%u,sig=%u,spd=%u) → SAFE_STATE",
82 cmd_a->train_id, cmd_a->signal_state, cmd_a->speed_limit,
83 cmd_b->train_id, cmd_b->signal_state, cmd_b->speed_limit);
87 result_out->signal_state = 0;
88 result_out->speed_limit = 0;
89 result_out->emergency_stop = 1;
95 snprintf(message_out, message_len,
96 "[VOTER] MATCH: train=%u, pos=%u, sig=%u, speed=%u",
97 cmd_a->train_id, cmd_a->position,
98 cmd_a->signal_state, cmd_a->speed_limit);
100 *result_out = *cmd_a;
108int main(
int argc,
char *argv[])
113 uint32_t voter_seq = 0;
114 uint32_t total_votes = 0;
115 uint32_t total_matches = 0;
116 uint32_t total_mismatches = 0;
117 uint32_t total_timeouts = 0;
120 printf(
"╔════════════════════════════════════════════════════════════╗\n");
121 printf(
"║ Voter Process - 2oo2 Voting Logic (Deterministic) ║\n");
122 printf(
"║ Reads from Processes A & B, writes to Process C ║\n");
123 printf(
"╚════════════════════════════════════════════════════════════╝\n");
130 if (!shared_mem.ready_a || !shared_mem.ready_b) {
136 if (shared_mem.process_a_seq != shared_mem.process_b_seq) {
137 fprintf(stderr,
"[VOTER] Sequence mismatch: A_seq=%u vs B_seq=%u\n",
138 shared_mem.process_a_seq, shared_mem.process_b_seq);
145 char vote_message[256];
146 int vote_result = perform_2oo2_vote(
156 switch (vote_result) {
169 voted_result.voted_command = voted_cmd;
170 voted_result.vote_result = (uint8_t)vote_result;
171 voted_result.voter_sequence = voter_seq;
172 strncpy(voted_result.log_message, vote_message,
sizeof(voted_result.log_message) - 1);
173 voted_result.log_message[
sizeof(voted_result.log_message) - 1] =
'\0';
176 if ((total_votes % 10) == 0) {
178 "[VOTER] Cycle %u: votes=%u, matches=%u, mismatches=%u, timeouts=%u\n",
179 voter_seq, total_votes, total_matches, total_mismatches, total_timeouts);
183 shared_mem.ready_a = 0;
184 shared_mem.ready_b = 0;