8#include "rte/oal/memory/rte_mem_util.h"
9#include "rte/oal/log/rte_log.h"
10#include "common/common_config.h"
11#include "common/rbc_util_frame.h"
12#include "gateway_c_common.h"
13#include "gateway_c_selfstatus.h"
17static const char *peer_name(gateway_c_peer_t peer)
19 return (peer == GATEWAY_C_PEER_A) ?
"A" :
"B";
22rte_status_t gateway_c_ctc_handler_init(gateway_c_context_t *ctx)
26 g_ctc.last_known_tag =
"SUPPRESSED";
27 g_ctc.relay_consecutive_timeout = 0U;
28 g_ctc.relay_down_since_ms[GATEWAY_C_PEER_A] = 0U;
29 g_ctc.relay_down_since_ms[GATEWAY_C_PEER_B] = 0U;
30 g_ctc.send_down_since_ms[GATEWAY_C_PEER_A] = 0U;
31 g_ctc.send_down_since_ms[GATEWAY_C_PEER_B] = 0U;
32 g_ctc.a_seen_this_cycle = 0U;
33 g_ctc.a_stale_cycles = 0U;
34 gateway_c_dedup_init(&g_ctc.dedup);
37 status = rte_channel_service_setup(&g_ctc.relay_channel[GATEWAY_C_PEER_A],
"c-relay-ctc-a");
38 if (status != RTE_STATUS_OK)
40 RTE_EXAMPLE_LOG(
"[GW %s] CTCHandler: relay A channel setup failed (%s)\n",
41 site_config_name(ctx->site), rte_status_to_string(status));
45 status = rte_channel_service_setup(&g_ctc.relay_channel[GATEWAY_C_PEER_B],
"c-relay-ctc-b");
46 if (status != RTE_STATUS_OK)
48 RTE_EXAMPLE_LOG(
"[GW %s] CTCHandler: relay B channel setup failed (%s)\n",
49 site_config_name(ctx->site), rte_status_to_string(status));
54 status = rte_channel_service_setup(&g_ctc.sim_channel,
"c-sim-ctc");
55 if (status != RTE_STATUS_OK)
57 RTE_EXAMPLE_LOG(
"[GW %s] CTCHandler: sim channel setup failed (%s)\n",
58 site_config_name(ctx->site), rte_status_to_string(status));
65static void send_to_relay(gateway_c_context_t *ctx, gateway_c_peer_t peer,
const uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE])
67 rte_status_t status = rte_channel_service_send(&g_ctc.relay_channel[peer],
68 bytes, RBC_ENVELOPE_WIRE_SIZE, 0U);
72 if ((status == RTE_STATUS_OK) || (status == RTE_STATUS_NOT_CONNECTED))
76 g_ctc.send_down_since_ms[peer] = 0U;
83 if (g_ctc.send_down_since_ms[peer] == 0U)
85 (void)rte_timer_now(&g_ctc.send_down_since_ms[peer]);
87 if (status == RTE_STATUS_TIMEOUT)
91 RTE_EXAMPLE_LOG(
"[GW %s] CTCHandler: keepalive to %s failed (%s)\n", site_config_name(ctx->site),
92 peer_name(peer), rte_status_to_string(status));
93 rte_log_write_event(RTE_LOG_LEVEL_ERROR, site_config_name(ctx->site), 0U,
"GW", peer_name(peer),
94 "ENVELOPE", rte_status_to_string(status), NULL);
98static void send_to_sim(gateway_c_context_t *ctx,
const uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE])
100 rte_status_t status = rte_channel_service_send(&g_ctc.sim_channel,
101 bytes, RBC_ENVELOPE_WIRE_SIZE, 0U);
103 if ((status != RTE_STATUS_OK) && (status != RTE_STATUS_TIMEOUT) && (status != RTE_STATUS_NOT_CONNECTED))
105 RTE_EXAMPLE_LOG(
"[GW %s] CTCHandler: forward to CTCSim failed (%s)\n",
106 site_config_name(ctx->site), rte_status_to_string(status));
107 rte_log_write_event(RTE_LOG_LEVEL_ERROR, site_config_name(ctx->site), 0U,
"GW",
"CTC",
"ENVELOPE",
108 rte_status_to_string(status), NULL);
112void gateway_c_ctc_handler_execute(gateway_c_context_t *ctx, uint32_t cycle)
114 uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE];
115 char sim_status[RBC_UTIL_PAYLOAD_MAX + 1];
120 while (rte_channel_service_read(&g_ctc.sim_channel, bytes,
sizeof(bytes), 0U) == RTE_STATUS_OK)
125 if (rbc_envelope_is_sim_status(bytes, sim_status, (uint16_t)
sizeof(sim_status)))
127 gateway_c_selfstatus_publish((uint8_t)RBC_UTIL_SRC_SIM_CTC, sim_status);
130 send_to_relay(ctx, GATEWAY_C_PEER_A, bytes);
131 send_to_relay(ctx, GATEWAY_C_PEER_B, bytes);
140 uint32_t a_count = 0U;
141 uint32_t b_count = 0U;
143 while ((status = rte_channel_service_read(&g_ctc.relay_channel[GATEWAY_C_PEER_A],
144 bytes,
sizeof(bytes), 0U)) == RTE_STATUS_OK)
146 if (rbc_envelope_is_keepalive(bytes))
151 if (!gateway_c_dedup_check_and_record(&g_ctc.dedup, bytes,
sizeof(bytes)))
153 send_to_sim(ctx, bytes);
157 if ((status != RTE_STATUS_OK) && (status != RTE_STATUS_TIMEOUT))
159 if (g_ctc.relay_down_since_ms[GATEWAY_C_PEER_A] == 0U)
161 (void)rte_timer_now(&g_ctc.relay_down_since_ms[GATEWAY_C_PEER_A]);
164 else if (a_count > 0U)
166 g_ctc.relay_down_since_ms[GATEWAY_C_PEER_A] = 0U;
167 g_ctc.relay_consecutive_timeout = 0U;
168 g_ctc.a_stale_cycles = 0U;
172 g_ctc.a_stale_cycles++;
174 g_ctc.a_seen_this_cycle = a_count;
176 while ((status = rte_channel_service_read(&g_ctc.relay_channel[GATEWAY_C_PEER_B],
177 bytes,
sizeof(bytes), 0U)) == RTE_STATUS_OK)
179 if (rbc_envelope_is_keepalive(bytes))
184 if (!gateway_c_dedup_check_and_record(&g_ctc.dedup, bytes,
sizeof(bytes)))
186 send_to_sim(ctx, bytes);
190 if ((status != RTE_STATUS_OK) && (status != RTE_STATUS_TIMEOUT))
192 if (g_ctc.relay_down_since_ms[GATEWAY_C_PEER_B] == 0U)
194 (void)rte_timer_now(&g_ctc.relay_down_since_ms[GATEWAY_C_PEER_B]);
197 else if (b_count > 0U)
199 g_ctc.relay_down_since_ms[GATEWAY_C_PEER_B] = 0U;
202 g_ctc.last_known_tag = ((a_count > 0U) || (b_count > 0U)) ?
"ACTIVE" :
"SUPPRESSED";
206void gateway_c_ctc_handler_shutdown(
void)
208 (void)rte_channel_service_close(&g_ctc.sim_channel);
209 (void)rte_channel_service_close(&g_ctc.relay_channel[GATEWAY_C_PEER_A]);
210 (void)rte_channel_service_close(&g_ctc.relay_channel[GATEWAY_C_PEER_B]);
213bool gateway_c_ctc_handler_check_link_down(rte_timestamp_ms_t threshold_ms, rte_timestamp_ms_t now_ms,
214 char *out_which,
size_t which_size)
216 gateway_c_peer_t peer;
218 for (peer = GATEWAY_C_PEER_A; peer <= GATEWAY_C_PEER_B; peer = (gateway_c_peer_t)((int)peer + 1))
220 rte_timestamp_ms_t down_since = g_ctc.relay_down_since_ms[peer];
221 rte_timestamp_ms_t send_down_since = g_ctc.send_down_since_ms[peer];
223 if (((down_since != 0U) && ((now_ms - down_since) >= threshold_ms)) ||
224 ((send_down_since != 0U) && ((now_ms - send_down_since) >= threshold_ms)))
226 (void)snprintf(out_which, which_size,
"CTC relay link to %s", peer_name(peer));
CTCHandler - Role C Gateway's TCP/IP server for the single CTC sim instance and its dedicated relay t...