9#include "safeapi/oal/memory/sapi_mem_util.h"
10#include "safeapi/oal/log/sapi_log.h"
11#include "common/common_config.h"
12#include "common/rbc_util_frame.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";
22static void broadcast_one(gateway_c_peer_t peer,
const uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE])
24 sapi_status_t status = sapi_channel_service_send(&g_il.relay_channel[peer], bytes,
25 RBC_ENVELOPE_WIRE_SIZE, 0U);
27 if ((status != SAPI_STATUS_OK) && (status != SAPI_STATUS_TIMEOUT))
29 sapi_log_write_event(SAPI_LOG_LEVEL_WARNING,
"-", 0U,
"GW", peer_name(peer),
"LOG_LEVEL",
30 sapi_status_to_string(status), NULL);
36 broadcast_one(GATEWAY_C_PEER_A, bytes);
37 broadcast_one(GATEWAY_C_PEER_B, bytes);
40sapi_status_t gateway_c_il_handler_init(gateway_c_context_t *ctx)
44 g_il.last_known_tag =
"SUPPRESSED";
45 g_il.relay_consecutive_timeout = 0U;
46 g_il.relay_down_since_ms[GATEWAY_C_PEER_A] = 0U;
47 g_il.relay_down_since_ms[GATEWAY_C_PEER_B] = 0U;
48 g_il.a_seen_this_cycle = 0U;
49 g_il.a_stale_cycles = 0U;
51 status = sapi_channel_service_setup(&g_il.relay_channel[GATEWAY_C_PEER_A],
"c-relay-il-a");
52 if (status != SAPI_STATUS_OK)
54 SAFEAPI_EXAMPLE_LOG(
"[GW %s] ILHandler: relay A channel setup failed (%s)\n",
55 site_config_name(ctx->site), sapi_status_to_string(status));
58 status = sapi_channel_service_setup(&g_il.relay_channel[GATEWAY_C_PEER_B],
"c-relay-il-b");
59 if (status != SAPI_STATUS_OK)
61 SAFEAPI_EXAMPLE_LOG(
"[GW %s] ILHandler: relay B channel setup failed (%s)\n",
62 site_config_name(ctx->site), sapi_status_to_string(status));
65 status = sapi_channel_service_setup(&g_il.sim_channel,
"c-sim-il");
66 if (status != SAPI_STATUS_OK)
68 SAFEAPI_EXAMPLE_LOG(
"[GW %s] ILHandler: sim channel setup failed (%s)\n",
69 site_config_name(ctx->site), sapi_status_to_string(status));
72 return SAPI_STATUS_OK;
75static void send_to_relay(gateway_c_context_t *ctx, gateway_c_peer_t peer,
76 const uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE])
78 sapi_status_t status = sapi_channel_service_send(&g_il.relay_channel[peer],
79 bytes, RBC_ENVELOPE_WIRE_SIZE, 0U);
80 if ((status != SAPI_STATUS_OK) && (status != SAPI_STATUS_TIMEOUT))
82 SAFEAPI_EXAMPLE_LOG(
"[GW %s] ILHandler: forward to %s failed (%s)\n", site_config_name(ctx->site),
83 peer_name(peer), sapi_status_to_string(status));
84 sapi_log_write_event(SAPI_LOG_LEVEL_ERROR, site_config_name(ctx->site), 0U,
"GW", peer_name(peer),
85 "ENVELOPE", sapi_status_to_string(status), NULL);
89static void send_to_sim(gateway_c_context_t *ctx,
const uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE])
91 sapi_status_t status = sapi_channel_service_send(&g_il.sim_channel,
92 bytes, RBC_ENVELOPE_WIRE_SIZE, 0U);
93 if ((status != SAPI_STATUS_OK) && (status != SAPI_STATUS_TIMEOUT))
95 SAFEAPI_EXAMPLE_LOG(
"[GW %s] ILHandler: forward to ILSim failed (%s)\n",
96 site_config_name(ctx->site), sapi_status_to_string(status));
97 sapi_log_write_event(SAPI_LOG_LEVEL_ERROR, site_config_name(ctx->site), 0U,
"GW",
"IL",
"ENVELOPE",
98 sapi_status_to_string(status), NULL);
102void gateway_c_il_handler_execute(gateway_c_context_t *ctx, uint32_t cycle)
104 uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE];
105 char sim_status[RBC_UTIL_PAYLOAD_MAX + 1];
106 sapi_status_t status;
107 bool forwarded_this_cycle =
false;
111 while (sapi_channel_service_read(&g_il.sim_channel, bytes,
sizeof(bytes), 0U) == SAPI_STATUS_OK)
117 if (rbc_envelope_is_sim_status(bytes, sim_status, (uint16_t)
sizeof(sim_status)))
119 gateway_c_selfstatus_publish((uint8_t)RBC_UTIL_SRC_SIM_IL, sim_status);
122 send_to_relay(ctx, GATEWAY_C_PEER_A, bytes);
123 send_to_relay(ctx, GATEWAY_C_PEER_B, bytes);
124 forwarded_this_cycle =
true;
132 if (!forwarded_this_cycle)
134 uint8_t keepalive[RBC_ENVELOPE_WIRE_SIZE];
136 rbc_envelope_write_keepalive(keepalive);
137 send_to_relay(ctx, GATEWAY_C_PEER_A, keepalive);
138 send_to_relay(ctx, GATEWAY_C_PEER_B, keepalive);
144 uint32_t a_count = 0U;
147 while ((status = sapi_channel_service_read(&g_il.relay_channel[GATEWAY_C_PEER_A],
148 bytes,
sizeof(bytes), 0U)) == SAPI_STATUS_OK)
150 send_to_sim(ctx, bytes);
153 if ((status != SAPI_STATUS_OK) && (status != SAPI_STATUS_TIMEOUT))
155 if (g_il.relay_down_since_ms[GATEWAY_C_PEER_A] == 0U)
157 (void)sapi_timer_now(&g_il.relay_down_since_ms[GATEWAY_C_PEER_A]);
160 else if (a_count > 0U)
162 g_il.relay_down_since_ms[GATEWAY_C_PEER_A] = 0U;
163 g_il.relay_consecutive_timeout = 0U;
164 g_il.a_stale_cycles = 0U;
168 g_il.a_stale_cycles++;
170 g_il.a_seen_this_cycle = a_count;
172 use_b_fallback = (g_il.a_stale_cycles >= (
unsigned int)SAFEAPI_EXAMPLE_C_A_STALE_CYCLE_LIMIT);
174 uint32_t b_count = 0U;
176 while ((status = sapi_channel_service_read(&g_il.relay_channel[GATEWAY_C_PEER_B],
177 bytes,
sizeof(bytes), 0U)) == SAPI_STATUS_OK)
181 send_to_sim(ctx, bytes);
185 if ((status != SAPI_STATUS_OK) && (status != SAPI_STATUS_TIMEOUT))
187 if (g_il.relay_down_since_ms[GATEWAY_C_PEER_B] == 0U)
189 (void)sapi_timer_now(&g_il.relay_down_since_ms[GATEWAY_C_PEER_B]);
192 else if (b_count > 0U)
194 g_il.relay_down_since_ms[GATEWAY_C_PEER_B] = 0U;
198 g_il.last_known_tag = (b_count > 0U) ?
"ACTIVE" :
"SUPPRESSED";
202 g_il.last_known_tag = (a_count > 0U) ?
"ACTIVE" :
"SUPPRESSED";
208void gateway_c_il_handler_shutdown(
void)
210 (void)sapi_channel_service_close(&g_il.sim_channel);
211 (void)sapi_channel_service_close(&g_il.relay_channel[GATEWAY_C_PEER_A]);
212 (void)sapi_channel_service_close(&g_il.relay_channel[GATEWAY_C_PEER_B]);
215bool gateway_c_il_handler_check_link_down(sapi_timestamp_ms_t threshold_ms, sapi_timestamp_ms_t now_ms,
216 char *out_which,
size_t which_size)
218 gateway_c_peer_t peer;
220 for (peer = GATEWAY_C_PEER_A; peer <= GATEWAY_C_PEER_B; peer = (gateway_c_peer_t)((int)peer + 1))
222 sapi_timestamp_ms_t down_since = g_il.relay_down_since_ms[peer];
224 if ((down_since != 0U) && ((now_ms - down_since) >= threshold_ms))
226 (void)snprintf(out_which, which_size,
"IL relay link to %s", peer_name(peer));
void gateway_c_il_handler_broadcast_to_ab(const uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE])
Sends one RBC_ENVELOPE_WIRE_SIZE frame to BOTH A and B on the IL relay link. Used by gateway_c_logctl...
ILHandler - Role C Gateway's TCP/IP server for the ONE multiplexed IL sim connection and the ONE dedi...