14#include "rte/app/appmanager/rte_appmanager.h"
15#include "rte/core/rte_core_version.h"
16#include "rte/oal/log/rte_log.h"
17#include "rte/oal/memory/rte_mem_util.h"
18#include "rte/posix_osadapter/rte_posix_osadapter_channel_service.h"
19#include "rte/utils/safestate/rte_safestate.h"
29#include "rte_rbc2oo2_version.h"
39static rte_status_t
resolve_channel_c(
const char *name, rte_netlink_config_t *config,
void *context)
43 if ((name == NULL) || (config == NULL) || (ctx == NULL))
45 return RTE_STATUS_INVALID_PARAM;
55 config->role = RTE_NETLINK_ROLE_CONNECT;
56 config->host =
"127.0.0.1";
61 rte_mem_set(config, 0,
sizeof(*config));
62 config->role = RTE_NETLINK_ROLE_LISTEN;
79 if (strcmp(name,
"c-sim-train") == 0)
85 if (strcmp(name,
"c-relay-train-a") == 0)
91 if (strcmp(name,
"c-relay-train-b") == 0)
98 if (strcmp(name,
"c-sim-il") == 0)
101 return RTE_STATUS_OK;
103 if (strcmp(name,
"c-relay-il-a") == 0)
106 return RTE_STATUS_OK;
108 if (strcmp(name,
"c-relay-il-b") == 0)
111 return RTE_STATUS_OK;
114 if (strcmp(name,
"c-sim-ctc") == 0)
117 return RTE_STATUS_OK;
119 if (strcmp(name,
"c-relay-ctc-a") == 0)
122 return RTE_STATUS_OK;
124 if (strcmp(name,
"c-relay-ctc-b") == 0)
127 return RTE_STATUS_OK;
133 if ((strcmp(name,
"c-relay-util-a") == 0) || (strcmp(name,
"c-relay-util-b") == 0)
134 || (strcmp(name,
"c-broker-train") == 0) || (strcmp(name,
"c-broker-il") == 0)
135 || (strcmp(name,
"c-broker-ctc") == 0))
138 if (strcmp(name,
"c-relay-util-a") == 0)
142 else if (strcmp(name,
"c-relay-util-b") == 0)
146 else if (strcmp(name,
"c-broker-train") == 0)
150 else if (strcmp(name,
"c-broker-il") == 0)
165 config->role = RTE_NETLINK_ROLE_CONNECT;
166 config->host =
"127.0.0.1";
168 return RTE_STATUS_OK;
171 return RTE_STATUS_INVALID_PARAM;
185 rte_timer_config_t timer_cfg;
189 "gateway service starting", NULL);
195 const char *env_cfg = getenv(
"RTE_CONFIG_FILE");
196 char default_cfg_path[128];
205 (void)snprintf(default_cfg_path,
sizeof(default_cfg_path),
"etc/config/c_%s.json", site_str);
208 (void)snprintf(default_cfg_path,
sizeof(default_cfg_path),
"/etc/sapi/c_%s.json", site_str);
214 status = rte_posix_osadapter_channel_service_register_resolver(
resolve_channel_c, ctx);
215 if (status != RTE_STATUS_OK)
223 if (status != RTE_STATUS_OK)
233 rte_mem_set(&timer_cfg, 0,
sizeof(timer_cfg));
234 timer_cfg.mode = RTE_TIMER_MODE_PERIODIC;
237 timer_cfg.user_ctx = ctx;
239 if (status != RTE_STATUS_OK)
244 "cycle timer_create failed", NULL);
248 if (status != RTE_STATUS_OK)
253 "cycle timer_start failed", NULL);
259 return RTE_STATUS_OK;
270 RTE_EXAMPLE_LOG(
"[GW-%s %s] %s down >= %ums: rebooting Gateway service\n",
273 "CHANNEL_REBOOT", which, NULL);
282 rte_timestamp_ms_t now_ms = 0U;
284 (void)rte_timer_now(&now_ms);
318 return RTE_STATUS_OK;
327 "gateway service shutting down", NULL);
337 return RTE_STATUS_OK;
353 static const rte_appmanager_operations_t ops = {
357 .post_execute = NULL,
362 rte_appmanager_config_t config;
369 rte_mem_set(&config, 0,
sizeof(config));
372 config.max_iterations = 0U;
380 rte_appmanager_config_t config;
381 const char *framework_actual_version;
386 framework_actual_version = rte_core_get_version();
387 (void)fprintf(stdout,
"%s version %s (framework %s, expected %s)\n", argv[0], RTE_RBC2OO2_VERSION,
388 framework_actual_version, RTE_FRAMEWORK_EXPECTED_VERSION);
389 if (strcmp(framework_actual_version, RTE_FRAMEWORK_EXPECTED_VERSION) != 0)
391 (void)fprintf(stderr,
392 "WARNING: linked safeAPIFreamwork version (%s) does not match what this binary was "
393 "built against (%s) - a stale library archive or wrong framework dist/ may be linked in\n",
394 framework_actual_version, RTE_FRAMEWORK_EXPECTED_VERSION);
399 (void)fprintf(stderr,
"usage: %s {WEST|EAST}\n", argv[0]);
404 (void)fprintf(stderr,
"unknown site '%s' - expected WEST or EAST\n", argv[1]);
409 if (status != RTE_STATUS_OK)
415 return rte_appmanager_run(&config);
420 uint64_t h = 14695981039346656037ULL;
422 for (i = 0U; i < size; i++)
424 h ^= (uint64_t)bytes[i];
425 h *= 1099511628211ULL;
434 rte_mem_set(dedup, 0,
sizeof(*dedup));
441 rte_timestamp_ms_t now_ms = 0U;
442 rte_timestamp_ms_t expiry_ms;
445 if ((dedup == NULL) || (bytes == NULL) || (size == 0U))
450 (void)rte_timer_now(&now_ms);
static void cycle_tick_callback(rte_timer_handle_t handle, void *user_ctx)
rte_status_t app_main_common_init(int argc, char **argv)
Runs this process's one-time runtime setup, common to every role: unbuffered stdout,...
Process-wide runtime setup shared by every one of this example's binaries (main.c for A/B,...
#define RTE_EXAMPLE_LOG(...)
#define RTE_EXAMPLE_CHANNEL_DOWN_REBOOT_MS
#define RBC_ENVELOPE_WIRE_SIZE
#define RBC_TRAIN_ENVELOPE_WIRE_SIZE
static rte_status_t gateway_c_shutdown(void *context)
static rte_status_t gateway_c_execute(void *context)
int gateway_c_main(int argc, char **argv, const gateway_c_service_def_t *svc)
Full process entry for a Role C gateway service: prints the version banner, parses argv[1] as {WEST|E...
static const char * gateway_c_get_name(void)
static const gateway_c_service_def_t * g_svc
void gateway_c_dedup_init(gateway_c_dedup_t *dedup)
Initialize a first-arrival deduplication tracker.
static uint64_t gateway_c_hash_bytes(const uint8_t *bytes, size_t size)
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...
static void gateway_c_check_channel_down_reboot(gateway_c_context_t *ctx, rte_timestamp_ms_t now_ms)
static rte_appmanager_config_t gateway_c_common_make_config(rte_example_site_t site, const gateway_c_service_def_t *svc)
static gateway_c_context_t g_c_ctx
static const char * gateway_c_get_version(void)
static rte_status_t gateway_c_init(void *context)
static rte_status_t resolve_channel_c(const char *name, rte_netlink_config_t *config, void *context)
Shared skeleton for the three Role C gateway service binaries (rte_gateway_c_train / _il / _ctc - ADR...
rte_status_t gateway_c_config_find_channel(const gateway_c_config_t *cfg, const char *channel_name, rte_netlink_config_t *out_netlink_cfg)
rte_status_t gateway_c_config_init_defaults(rte_example_site_t site, gateway_c_config_t *out_cfg)
rte_status_t gateway_c_config_load_file(const char *filepath, gateway_c_config_t *out_cfg)
Configuration data structures and parser for Role C Gateway (JSON-based).
void gateway_c_selfstatus_shutdown(void)
void gateway_c_selfstatus_tick(const gateway_c_context_t *ctx, uint32_t cycle, const char *log_tag, uint8_t src_id)
Call once per cycle. Every RTE_EXAMPLE_STATUS_PERIOD_MS (and on cycle 1) logs [unit] [-] [STATUS] [se...
void gateway_c_selfstatus_init(const char *broker_channel_name)
Setup the broker CONNECT link. broker_channel_name is one of "c-broker-train"/"-il"/"-ctc"....
ADR-038: a role-C gateway service's own periodic status - a self log line plus a PUBLISH(STATUS) rbc_...
Shared wire-format constants and gateway_c_context_t.
#define GATEWAY_C_DEDUP_SAF_EXPIRY_MS
#define GATEWAY_C_DEDUP_WINDOW_SIZE
void gateway_c_sleep_ms(unsigned int ms)
#define GATEWAY_C_DEDUP_EXPIRY_MS
Declares gateway_c_sleep_ms() - see gateway_c_types.h's own doc.
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_FRAME_SIZE
uint16_t site_config_c_port_for_broker(rte_example_site_t site, rte_example_relay_kind_t gw_kind)
ADR-038: the status/broker service's LISTEN port for one local gateway service to CONNECT and publish...
uint16_t site_config_c_port_for_ctc(rte_example_site_t site)
const char * site_config_name(rte_example_site_t site)
uint16_t site_config_c_port_for_il(rte_example_site_t site)
bool site_config_parse(const char *arg, rte_example_site_t *out_site)
Parses a site name argument ("WEST"/"EAST", case-insensitive).
uint16_t site_config_c_port_for_b(rte_example_site_t site, rte_example_relay_kind_t kind)
uint16_t site_config_c_port_for_train(rte_example_site_t site)
C's single listen port for the Train sim, for this site (ADR-036: one multiplexed connection carries ...
uint16_t site_config_c_port_for_a(rte_example_site_t site, rte_example_relay_kind_t kind)
C's listen port for A's dedicated kind relay link, for this site (rte_example_relay_kind_t's own doc)...
@ RTE_EXAMPLE_RELAY_KIND_UTIL
@ RTE_EXAMPLE_RELAY_KIND_CTC
@ RTE_EXAMPLE_RELAY_KIND_IL
@ RTE_EXAMPLE_RELAY_KIND_TRAIN
gateway_c_config_timers_t timers
uint32_t reconnect_attempt_timeout_ms
uint32_t report_period_ms
One gateway_c_context_t instance's state.
volatile bool stop_requested
gateway_c_config_t config
rte_timer_storage_t cycle_timer_storage
rte_timer_handle_t cycle_timer_handle
volatile uint32_t cycle_ticks
rte_timestamp_ms_t timestamp_ms
gateway_c_dedup_entry_t entries[GATEWAY_C_DEDUP_WINDOW_SIZE]
One gateway service's identity and its single handler's entry points - a pure-literal table each main...
bool(*) handler_check_link_down(rte_timestamp_ms_t threshold_ms, rte_timestamp_ms_t now_ms, char *out_which, size_t which_size)
void(*) handler_execute(gateway_c_context_t *ctx, uint32_t cycle)
void(*) pre_execute_hook(gateway_c_context_t *ctx)
rte_status_t(*) handler_init(gateway_c_context_t *ctx)
const char * broker_channel
void(*) idle_hook(gateway_c_context_t *ctx)
void(*) handler_shutdown(void)