RBC_Test_CommServer 0.1.0 build fe9835b
Non-vital customer-network access point in front of the RBC's role-C gateways
Loading...
Searching...
No Matches
cs_config.c
Go to the documentation of this file.
1
5#include "cs_config.h"
6
7#include <stdio.h>
8#include <string.h>
9
10#include "safecomm/ale/ale.h"
11#include "safecomm/config/config.h"
12
14static void cfg_u32(const safecomm_config_t *c, const char *key, uint32_t lo, uint32_t hi, uint32_t *out)
15{
16 uint32_t v = 0U;
17
18 if (safecomm_config_get_u32(c, key, &v) == SAFECOMM_STATUS_OK)
19 {
20 if ((v >= lo) && (v <= hi))
21 {
22 *out = v;
23 }
24 else
25 {
26 (void)fprintf(stderr, "commserver: %s=%u out of [%u,%u], keeping %u\n",
27 key, v, lo, hi, *out);
28 }
29 }
30}
31
32static void cfg_host(const safecomm_config_t *c, const char *key, char *out, size_t cap)
33{
34 const char *s = safecomm_config_get_str(c, key);
35
36 if ((s != NULL) && (strlen(s) < cap))
37 {
38 (void)strcpy(out, s);
39 }
40}
41
42int load_cfg(const char *path, cs_cfg_t *cfg)
43{
44 safecomm_config_t raw;
45 uint32_t u;
46
47 (void)memset(cfg, 0, sizeof(*cfg));
48 (void)strcpy(cfg->west_host, "127.0.0.1");
49 (void)strcpy(cfg->east_host, "127.0.0.1");
50 (void)strcpy(cfg->listen_host, "0.0.0.0");
51 cfg->west_port = 15010U;
52 cfg->east_port = 15110U;
53 cfg->listen_port = 16010U;
54 cfg->message_size = 96U;
55 cfg->connect_timeout_ms = 4000U;
56 cfg->idle_ms = 5U;
57
58 if (safecomm_config_load(path, &raw) != SAFECOMM_STATUS_OK)
59 {
60 (void)fprintf(stderr, "commserver: cannot load config '%s'\n", path);
61 return -1;
62 }
63
64 cfg_host(&raw, "rbc.west_host", cfg->west_host, sizeof(cfg->west_host));
65 cfg_host(&raw, "rbc.east_host", cfg->east_host, sizeof(cfg->east_host));
66 cfg_host(&raw, "train.listen_host", cfg->listen_host, sizeof(cfg->listen_host));
67
68 u = cfg->west_port; cfg_u32(&raw, "rbc.west_port", 1U, 65535U, &u); cfg->west_port = (uint16_t)u;
69 u = cfg->east_port; cfg_u32(&raw, "rbc.east_port", 1U, 65535U, &u); cfg->east_port = (uint16_t)u;
70 u = cfg->listen_port; cfg_u32(&raw, "train.listen_port", 1U, 65535U, &u); cfg->listen_port = (uint16_t)u;
71 u = cfg->message_size; cfg_u32(&raw, "rbc.message_size", 1U, CS_MSG_MAX, &u); cfg->message_size = (uint16_t)u;
72 cfg_u32(&raw, "rbc.connect_timeout_ms", 100U, 60000U, &cfg->connect_timeout_ms);
73 cfg_u32(&raw, "poll.idle_ms", 0U, 1000U, &cfg->idle_ms);
74
75 /* safecomm::ale is linked and its config path is honoured; the stub's
76 * single-route relay does not need the peer table yet. */
77 {
78 safecomm_ale_t ale;
79 if (safecomm_ale_from_config(&raw, &ale) == SAFECOMM_STATUS_OK)
80 {
81 (void)fprintf(stderr, "commserver: ALE peer map loaded (%u peers)\n", ale.peer_count);
82 }
83 }
84 return 0;
85}
int load_cfg(const char *path, cs_cfg_t *cfg)
Loads path into cfg, defaults first then overridden by whatever the file actually sets (out-of-range ...
Definition cs_config.c:42
CommServer's own config parsing (cs_cfg_t + load_cfg()), extracted from main.c (2026-09-24) purely fo...