10#include "rte/oal/memory/rte_mem_util.h"
11#include "rte/oal/log/rte_log.h"
40 if (status != RTE_STATUS_OK)
42 RTE_EXAMPLE_LOG(
"[GW %s] TrainHandler: relay A channel setup failed (%s)\n",
48 if (status != RTE_STATUS_OK)
50 RTE_EXAMPLE_LOG(
"[GW %s] TrainHandler: relay B channel setup failed (%s)\n",
60 if (status != RTE_STATUS_OK)
62 RTE_EXAMPLE_LOG(
"[GW %s] TrainHandler: sim channel setup failed (%s)\n",
76 const uint8_t *bytes,
size_t len)
82 if ((status == RTE_STATUS_OK) || (status == RTE_STATUS_NOT_CONNECTED))
98 if (status == RTE_STATUS_TIMEOUT)
103 peer_name(peer), rte_status_to_string(status));
105 "ENVELOPE", rte_status_to_string(status), NULL);
113 if ((status != RTE_STATUS_OK) && (status != RTE_STATUS_TIMEOUT) && (status != RTE_STATUS_NOT_CONNECTED))
115 RTE_EXAMPLE_LOG(
"[GW %s] TrainHandler: forward to TrainSim failed (%s)\n",
117 rte_log_write_event(RTE_LOG_LEVEL_ERROR,
site_config_name(ctx->
site), 0U,
"GW",
"Train",
"ENVELOPE",
118 rte_status_to_string(status), NULL);
141 bool forwarded_this_cycle =
false;
146 rte_mem_set(bytes, 0,
sizeof(bytes));
147 while (rte_channel_service_read_ex(&
g_train.
sim_channel, bytes,
sizeof(bytes), 0U, &actual_len) ==
157 rte_mem_set(bytes, 0,
sizeof(bytes));
162 forwarded_this_cycle =
true;
163 rte_mem_set(bytes, 0,
sizeof(bytes));
175 if (!forwarded_this_cycle)
190 uint32_t a_count = 0U;
191 uint32_t b_count = 0U;
193 rte_mem_set(bytes, 0,
sizeof(bytes));
195 bytes,
sizeof(bytes), 0U, &actual_len)) == RTE_STATUS_OK)
202 rte_mem_set(bytes, 0,
sizeof(bytes));
204 if ((status != RTE_STATUS_OK) && (status != RTE_STATUS_TIMEOUT))
211 else if (a_count > 0U)
223 rte_mem_set(bytes, 0,
sizeof(bytes));
225 bytes,
sizeof(bytes), 0U, &actual_len)) == RTE_STATUS_OK)
232 rte_mem_set(bytes, 0,
sizeof(bytes));
234 if ((status != RTE_STATUS_OK) && (status != RTE_STATUS_TIMEOUT))
241 else if (b_count > 0U)
258 char *out_which,
size_t which_size)
267 if (((down_since != 0U) && ((now_ms - down_since) >= threshold_ms)) ||
268 ((send_down_since != 0U) && ((now_ms - send_down_since) >= threshold_ms)))
270 (void)snprintf(out_which, which_size,
"Train relay link to %s",
peer_name(peer));
#define RTE_EXAMPLE_LOG(...)
#define RBC_ENVELOPE_WIRE_SIZE
#define RBC_TRAIN_ENVELOPE_WIRE_SIZE
void gateway_c_dedup_init(gateway_c_dedup_t *dedup)
Initialize a first-arrival deduplication tracker.
bool gateway_c_dedup_check_and_record(gateway_c_dedup_t *dedup, const uint8_t *bytes, size_t size)
Check if bytes was already forwarded within the expiry window. If not seen, records bytes with curren...
Shared skeleton for the three Role C gateway service binaries (rte_gateway_c_train / _il / _ctc - ADR...
void gateway_c_selfstatus_publish(uint8_t src_id, const char *payload)
Republish a foreign unit's status to the C broker as a PUBLISH(STATUS) with source id src_id (rbc_uti...
ADR-038: a role-C gateway service's own periodic status - a self log line plus a PUBLISH(STATUS) rbc_...
void gateway_c_train_handler_shutdown(void)
static void send_to_relay(gateway_c_context_t *ctx, gateway_c_peer_t peer, const uint8_t *bytes, size_t len)
static const char * peer_name(gateway_c_peer_t peer)
static gateway_c_train_handler_state_t g_train
bool gateway_c_train_handler_check_link_down(rte_timestamp_ms_t threshold_ms, rte_timestamp_ms_t now_ms, char *out_which, size_t which_size)
rte_status_t gateway_c_train_handler_init(gateway_c_context_t *ctx)
void gateway_c_train_handler_execute(gateway_c_context_t *ctx, uint32_t cycle)
static void send_to_sim(gateway_c_context_t *ctx, const uint8_t *bytes, size_t len)
TrainHandler - Role C Gateway's TCP/IP server for the ONE multiplexed Train sim connection and the ON...
ADR-038: the wire frame for the A/B<->C util side channel (RTE_EXAMPLE_RELAY_KIND_UTIL) and the C-sid...
#define RBC_UTIL_PAYLOAD_MAX
static bool rbc_envelope_is_sim_status(const uint8_t in[RBC_ENVELOPE_WIRE_SIZE], char *out_payload, uint16_t out_size)
true if in is a sim-status frame; when it is and out_payload is non-NULL, copies the ASCII payload th...
static void rbc_envelope_write_keepalive(uint8_t out[RBC_ENVELOPE_WIRE_SIZE])
Fills out with a keepalive frame (RBC_MSG_RELAY_KEEPALIVE in byte 0, every other byte zero) - see tha...
const char * site_config_name(rte_example_site_t site)
One gateway_c_context_t instance's state.
unsigned int a_stale_cycles
uint32_t a_seen_this_cycle
rte_channel_service_t relay_channel[GATEWAY_C_PEER_COUNT]
const char * last_known_tag
rte_channel_service_t sim_channel
rte_timestamp_ms_t relay_down_since_ms[GATEWAY_C_PEER_COUNT]
uint8_t relay_consecutive_timeout
rte_timestamp_ms_t send_down_since_ms[GATEWAY_C_PEER_COUNT]