RTE RBC 2oo2 Reference Application
0.1.0 build 0006685
SIL 4 2oo2 Dual-Site Fault-Tolerant ERTMS Radio Block Centre Reference Architecture
Toggle main menu visibility
Loading...
Searching...
No Matches
gateway_c_selfstatus.c
Go to the documentation of this file.
1
5
#include "
gateway_c_selfstatus.h
"
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
14
#include "
common/common_config.h
"
15
#include "
common/rbc_util_frame.h
"
16
#include "
common/site_config.h
"
17
18
static
rte_channel_service_t
s_broker
;
19
static
bool
s_have_broker
;
20
static
rte_timestamp_ms_t
s_last_ms
;
21
static
uint8_t
s_seq
;
22
23
void
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
42
void
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
++;
70
rbc_util_frame_encode
(frame,
RBC_UTIL_MSG_PUBLISH
, src_id, (uint16_t)
RBC_UTIL_TOPIC_STATUS
,
71
s_seq
, payload);
72
(void)rte_channel_service_send(&
s_broker
, frame,
RBC_UTIL_FRAME_SIZE
, 0U);
73
}
74
}
75
76
void
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
89
void
gateway_c_selfstatus_shutdown
(
void
)
90
{
91
if
(
s_have_broker
)
92
{
93
(void)rte_channel_service_close(&
s_broker
);
94
s_have_broker
=
false
;
95
}
96
}
s_seq
static uint8_t s_seq
Definition
ab_gp_jru_forward.c:24
common_config.h
RTE_EXAMPLE_STATUS_PERIOD_MS
#define RTE_EXAMPLE_STATUS_PERIOD_MS
Definition
common_config.h:278
gateway_c_selfstatus_shutdown
void gateway_c_selfstatus_shutdown(void)
Definition
gateway_c_selfstatus.c:89
gateway_c_selfstatus_tick
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...
Definition
gateway_c_selfstatus.c:42
gateway_c_selfstatus_publish
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...
Definition
gateway_c_selfstatus.c:76
s_broker
static rte_channel_service_t s_broker
Definition
gateway_c_selfstatus.c:18
gateway_c_selfstatus_init
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"....
Definition
gateway_c_selfstatus.c:23
s_have_broker
static bool s_have_broker
Definition
gateway_c_selfstatus.c:19
s_last_ms
static rte_timestamp_ms_t s_last_ms
Definition
gateway_c_selfstatus.c:20
gateway_c_selfstatus.h
ADR-038: a role-C gateway service's own periodic status - a self log line plus a PUBLISH(STATUS) rbc_...
rbc_util_frame.h
ADR-038: the wire frame for the A/B<->C util side channel (RTE_EXAMPLE_RELAY_KIND_UTIL) and the C-sid...
rbc_util_frame_encode
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.
Definition
rbc_util_frame.h:110
RBC_UTIL_FRAME_SIZE
#define RBC_UTIL_FRAME_SIZE
Definition
rbc_util_frame.h:39
RBC_UTIL_TOPIC_STATUS
#define RBC_UTIL_TOPIC_STATUS
Topic ids (byte 2..3).
Definition
rbc_util_frame.h:55
RBC_UTIL_MSG_PUBLISH
@ RBC_UTIL_MSG_PUBLISH
Definition
rbc_util_frame.h:50
site_config_name
const char * site_config_name(rte_example_site_t site)
Definition
site_config.c:52
site_config.h
West/East site identity and the per-site config derived from it (ports, container host lookup) - see ...
gateway_c_context_t
One gateway_c_context_t instance's state.
Definition
gateway_c_types.h:58
gateway_c_context_t::site
rte_example_site_t site
Definition
gateway_c_types.h:59
src
application
C
gateway_c_selfstatus.c
Generated by
1.18.0