RTE RBC 2oo2 Reference Application 0.1.0 build 0006685
SIL 4 2oo2 Dual-Site Fault-Tolerant ERTMS Radio Block Centre Reference Architecture
Loading...
Searching...
No Matches
gateway_c_selfstatus.c
Go to the documentation of this file.
1
6
7#include <stdio.h>
8#include <string.h>
9
10#include "rte/oal/log/rte_log.h"
11#include "rte/oal/timer/rte_timer.h"
12#include "rte/redundancy/channel_service/rte_channel_service.h"
13
16#include "common/site_config.h"
17
18static rte_channel_service_t s_broker;
19static bool s_have_broker;
20static rte_timestamp_ms_t s_last_ms;
21static uint8_t s_seq;
22
23void gateway_c_selfstatus_init(const char *broker_channel_name)
24{
25 rte_status_t status;
26
27 if ((broker_channel_name == NULL) || (broker_channel_name[0] == '\0'))
28 {
29 return;
30 }
31 status = rte_channel_service_setup(&s_broker, broker_channel_name);
32 if (status != RTE_STATUS_OK)
33 {
34 rte_log_write_event(RTE_LOG_LEVEL_WARNING, "-", 0U, "GW", "-", "STATUS",
35 "broker link setup failed", broker_channel_name);
36 return;
37 }
38 s_have_broker = true;
39 s_last_ms = 0U;
40}
41
42void gateway_c_selfstatus_tick(const gateway_c_context_t *ctx, uint32_t cycle, const char *log_tag,
43 uint8_t src_id)
44{
45 char payload[96];
46 uint8_t frame[RBC_UTIL_FRAME_SIZE];
47 rte_timestamp_ms_t now = 0U;
48 const char *site;
49
50 if (ctx == NULL)
51 {
52 return;
53 }
54 (void)rte_timer_now(&now);
55 if ((cycle != 1U)
56 && ((now - s_last_ms) < (rte_timestamp_ms_t)RTE_EXAMPLE_STATUS_PERIOD_MS))
57 {
58 return;
59 }
60 s_last_ms = now;
61 site = site_config_name(ctx->site);
62
63 (void)snprintf(payload, sizeof(payload), "unit=GW-%s/%s up=1", log_tag, site);
64
65 rte_log_write_event(RTE_LOG_LEVEL_INFO, site, cycle, log_tag, "-", "STATUS", "self", payload);
66
67 if (s_have_broker)
68 {
69 s_seq++;
71 s_seq, payload);
72 (void)rte_channel_service_send(&s_broker, frame, RBC_UTIL_FRAME_SIZE, 0U);
73 }
74}
75
76void gateway_c_selfstatus_publish(uint8_t src_id, const char *payload)
77{
78 uint8_t frame[RBC_UTIL_FRAME_SIZE];
79
80 if (!s_have_broker || (payload == NULL))
81 {
82 return;
83 }
84 s_seq++;
85 rbc_util_frame_encode(frame, RBC_UTIL_MSG_PUBLISH, src_id, (uint16_t)RBC_UTIL_TOPIC_STATUS, s_seq, payload);
86 (void)rte_channel_service_send(&s_broker, frame, RBC_UTIL_FRAME_SIZE, 0U);
87}
88
90{
91 if (s_have_broker)
92 {
93 (void)rte_channel_service_close(&s_broker);
94 s_have_broker = false;
95 }
96}
static uint8_t s_seq
#define RTE_EXAMPLE_STATUS_PERIOD_MS
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_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...
static rte_channel_service_t s_broker
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"....
static bool s_have_broker
static rte_timestamp_ms_t s_last_ms
ADR-038: a role-C gateway service's own periodic status - a self log line plus a PUBLISH(STATUS) rbc_...
ADR-038: the wire frame for the A/B<->C util side channel (RTE_EXAMPLE_RELAY_KIND_UTIL) and the C-sid...
static void rbc_util_frame_encode(uint8_t out[RBC_UTIL_FRAME_SIZE], rbc_util_msg_type_t msg_type, uint8_t source_id, uint16_t topic_id, uint8_t seq, const char *payload)
Encodes a util frame into a fixed RBC_UTIL_FRAME_SIZE buffer.
#define RBC_UTIL_FRAME_SIZE
#define RBC_UTIL_TOPIC_STATUS
Topic ids (byte 2..3).
@ RBC_UTIL_MSG_PUBLISH
const char * site_config_name(rte_example_site_t site)
Definition site_config.c:52
West/East site identity and the per-site config derived from it (ports, container host lookup) - see ...
One gateway_c_context_t instance's state.
rte_example_site_t site