SafeAPI RBC 2oo2 Generic Application
IL/CTC route and train logic for the safeAPIRBC2oo2 reference RBC
Loading...
Searching...
No Matches
ab_ga_ma_logic_adaptation.c
Go to the documentation of this file.
1
7
8#include "safeapi/oal/log/sapi_log.h"
9
10#include "common_config.h" /* SAFEAPI_EXAMPLE_LOG - this module's own setup-failure log, see
11 * ab_ga_ma_adaptation_init()'s own doc, ga_interface.h */
12#include "ab_siteGA.h" /* ab_site_ga_route_layout_t - GA's own reduced site header, implemented by
13 * safeAPIRBC2oo2SA (sa::site::ga), same direct-include posture as every
14 * other GA module (ga_interface.h's own doc) */
15#include "../main/ab_ga_dispatch.h" /* ab_ga_dispatch_init() - GA's single GP-notification
16 * subscriber, see that file's own doc */
17#include "../il/ab_ga_il.h" /* ab_ga_il_site_has_route()/_route_exists() - used by
18 * this file's own validate_route_assignment() below */
19
21
23
25
27const char *g_ga_role_tag = "";
28
30static bool train_holds_preceding_route(const ab_gp_db_t *db, const ab_site_ga_route_layout_t *layout, uint8_t train_id);
31static bool validate_route_assignment(void *context, uint16_t route_id, uint8_t train_id);
32
33
35sapi_status_t ab_ga_ma_adaptation_init(ab_gp_train_session_t *sessions, ab_gp_db_t *db, sapi_example_site_t site,
36 const char *role_tag)
37{
38 sapi_status_t status;
39
40 g_ga_role_tag = role_tag;
41
42 status = ab_gp_ga_register_init(validate_route_assignment, (void *)db);
43 if (status == SAPI_STATUS_OK)
44 {
45 /* GA's single dispatch module (ab_ga_dispatch.c) - the only
46 * thing that registers a route-context/train-context/IL-envelope/
47 * IL-CTC-output-producer subscription with GP now, fanning out to
48 * il/ctc internally. Replaces the old
49 * ab_ga_il_init(role_tag)/ab_ga_ctc_init(role_tag) pair
50 * (interface-simplification pass). */
51 status = ab_ga_dispatch_init(sessions, db);
52 }
53 if (status != SAPI_STATUS_OK)
54 {
55 SAFEAPI_EXAMPLE_LOG("[%s] GA registration -> %s\n", role_tag, sapi_status_to_string(status));
56 sapi_log_write_event(SAPI_LOG_LEVEL_ERROR, site_config_name(site), 0U, role_tag, "-", "GA",
57 "GA registration failed", NULL);
58 }
59 return status;
60}
61
62
63/****Local functions ****/
64
68static bool train_holds_preceding_route(const ab_gp_db_t *db, const ab_site_ga_route_layout_t *layout, uint8_t train_id)
69{
70 ab_gp_route_context_t prev_ctx;
71 size_t i;
72
73 /* 1. Direct predecessor by prev_route_id */
74 if (layout->prev_route_id != 0U)
75 {
76 if (ab_gp_db_get_route_context(db, layout->prev_route_id, &prev_ctx))
77 {
78 if (prev_ctx.used_in_ma && (prev_ctx.train_id == train_id))
79 {
80 return true;
81 }
82 }
83 }
84
85 /* 2. Any route ending at candidate route's start signal held by this train */
86 for (i = 0U; i < ab_site_ga_track_layout_count; i++)
87 {
88 if (ab_site_ga_track_layout[i].end_signal == layout->start_signal)
89 {
90 if (ab_gp_db_get_route_context(db, ab_site_ga_track_layout[i].id, &prev_ctx))
91 {
92 if (prev_ctx.used_in_ma && (prev_ctx.train_id == train_id))
93 {
94 return true;
95 }
96 }
97 }
98 }
99
100 return false;
101}
102
114static bool validate_route_assignment(void *context, uint16_t route_id, uint8_t train_id)
115{
116 const ab_gp_db_t *db = (const ab_gp_db_t *)context;
117 const ab_site_ga_route_layout_t *layout = NULL;
118 uint32_t last_nid_lrbg = 0U;
119 bool balise_found = false;
120 uint8_t b;
121
122 if (!ab_ga_il_site_has_route(route_id, &layout))
123 {
124 return false; /* not a route this site's own layout knows about */
125 }
126 if (!ab_gp_db_route_exists(db, route_id))
127 {
128 return false; /* route not set/locked by Interlocking in DB */
129 }
130 /* If train has a last position inside the route then allow to send MA */
131 if (ab_gp_train_get_last_position(db, train_id, &last_nid_lrbg) && (last_nid_lrbg != 0U))
132 {
133 if (layout->balise_count == 0U)
134 {
135 return true;
136 }
137 for (b = 0U; b < layout->balise_count; b++)
138 {
139 if (layout->balise_nid_lrbgs[b] == last_nid_lrbg)
140 {
141 sapi_log_write_event(SAPI_LOG_LEVEL_INFO, g_ga_role_tag,
142 0U, "GA", "GP", "ROUTE_VALIDATION",
143 "train's last position matches route balise", NULL);
144 balise_found = true;
145 break;
146 }
147 }
148 if (!balise_found)
149 {
150 if (train_holds_preceding_route(db, layout, train_id))
151 {
152 sapi_log_write_event(SAPI_LOG_LEVEL_INFO, g_ga_role_tag,
153 0U, "GA", "GP", "ROUTE_VALIDATION",
154 "route is extension of route held by train", NULL);
155 return true;
156 }
157
158 sapi_log_write_event(SAPI_LOG_LEVEL_WARNING, g_ga_role_tag,
159 0U, "GA", "GP", "ROUTE_VALIDATION",
160 "train's last position does NOT match any route balise and not an extension", NULL);
161 return false; /* reported balise does not match this route */
162 }
163 }
164
165 return true;
166}
sapi_status_t ab_ga_dispatch_init(ab_gp_train_session_t *sessions, ab_gp_db_t *db)
Registers every GA<->GP subscription this process needs, in one place: route-context notify,...
GA's single GP-notification subscriber AND registration point for everything GP calls into GA with - ...
char * g_ga_role_tag
bool ab_ga_il_site_has_route(uint16_t route_id, const ab_site_ga_route_layout_t **out_layout)
Pure site-layout lookup - does route_id exist in this site's own static track layout at all (ab_site_...
Definition ab_ga_il.c:71
A/B's own route handling against GP's own runtime table (via AB/GP/ga_interface/ga_interface....
static bool validate_route_assignment(void *context, uint16_t route_id, uint8_t train_id)
static bool train_holds_preceding_route(const ab_gp_db_t *db, const ab_site_ga_route_layout_t *layout, uint8_t train_id)
Checks if the given train already holds a preceding route connecting to layout.
sapi_status_t ab_ga_ma_adaptation_init(ab_gp_train_session_t *sessions, ab_gp_db_t *db, sapi_example_site_t site, const char *role_tag)
GA's own single entry point, IMPLEMENTED here - ab_ga_ma_adaptation_init() is DECLARED in AB/GP/ga_in...
GA's own site data: route composition, built from GP's raw physical elements (railML interlocking <ro...
const ab_site_ga_route_layout_t ab_site_ga_track_layout[]
One route's static layout entry (railML interlocking <route>) - GA's own full definition (not a reduc...
Definition ab_siteGA.h:69
uint32_t balise_nid_lrbgs[SAFEAPI_EXAMPLE_MAX_BALISES_PER_ROUTE]
Definition ab_siteGA.h:86