Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
sapi_channel.c
Go to the documentation of this file.
1
10
12
14
16
18
20
21
24 const sapi_channel_config_t *config)
25{
26 sapi_status_t lifecycle_status;
27
28 if ((storage == NULL) || (config == NULL))
29 {
31 }
32 if ((config->send == NULL) || (config->recv == NULL))
33 {
35 }
36 /* REQ-LIFECYCLE-001 (ADR-026): a channel is a setup-only resource - refuse once the
37 * application's setup phase has been locked. */
38 lifecycle_status = sapi_lifecycle_check_setup_allowed();
39 if (lifecycle_status != SAPI_STATUS_OK)
40 {
41 return lifecycle_status;
42 }
43
44 storage->config = *config;
45 storage->health.send_count = 0U;
46 storage->health.send_error_count = 0U;
47 storage->health.receive_count = 0U;
48 storage->health.receive_error_count = 0U;
49 storage->health.is_healthy = true;
51 storage->initialized = true;
52
53 return SAPI_STATUS_OK;
54}
55
57 const void *data,
58 size_t data_size)
59{
60 sapi_status_t status;
61
62 if ((handle == NULL) || (data == NULL) || (data_size == 0U))
63 {
65 }
66 if (!handle->initialized)
67 {
69 }
70
71 status = handle->config.send(handle->config.channel_handle, data, data_size);
72 if (status == SAPI_STATUS_OK)
73 {
74 handle->health.send_count++;
75 }
76 else
77 {
78 handle->health.send_error_count++;
79 handle->health.last_error = status;
80 }
81 return status;
82}
83
85 void *data,
86 size_t data_size,
87 uint32_t timeout_ms)
88{
89 sapi_status_t status;
90
91 if ((handle == NULL) || (data == NULL) || (data_size == 0U))
92 {
94 }
95 if (!handle->initialized)
96 {
98 }
99
100 status = handle->config.recv(handle->config.channel_handle, data, data_size, timeout_ms);
101 if (status == SAPI_STATUS_OK)
102 {
103 handle->health.receive_count++;
104 }
105 else
106 {
107 handle->health.receive_error_count++;
108 handle->health.last_error = status;
109 }
110 return status;
111}
112
114 sapi_channel_health_t *out_health)
115{
116 if ((handle == NULL) || (out_health == NULL))
117 {
119 }
120 *out_health = handle->health;
121 return SAPI_STATUS_OK;
122}
123
124const char *sapi_channel_get_name(const sapi_channel_t *handle)
125{
126 if (handle == NULL)
127 {
128 return NULL;
129 }
130 return handle->config.name;
131}
132
134 bool is_healthy)
135{
136 if (handle == NULL)
137 {
139 }
140 handle->health.is_healthy = is_healthy;
141 return SAPI_STATUS_OK;
142}
143
145{
146 /* No dynamic memory to free - handle is caller-allocated. */
147 if (handle == NULL)
148 {
149 return SAPI_STATUS_OK;
150 }
151 handle->initialized = false;
152 return SAPI_STATUS_OK;
153}
sapi_status_t sapi_lifecycle_check_setup_allowed(void)
Convenience check for a setup-only constructor: call this as one of the first checks in any function ...
sapi_status_t
Common result/status codes.
Definition sapi_status.h:27
@ SAPI_STATUS_INVALID_PARAM
Definition sapi_status.h:29
@ SAPI_STATUS_NOT_INITIALIZED
Definition sapi_status.h:30
@ SAPI_STATUS_OK
Definition sapi_status.h:28
A single redundant "channel": an opaque transport handle plus send/receive callbacks,...
Process-wide application setup-phase lock (ADR-026).
Configuration for sapi_channel_init().
sapi_channel_send_fn send
sapi_channel_recv_fn recv
Health statistics for one vital channel.
sapi_status_t last_error
Storage for one vital channel instance (opaque to caller).
sapi_channel_config_t config
sapi_channel_health_t health