Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Toggle main menu visibility
Loading...
Searching...
No Matches
sapi_channel.c
Go to the documentation of this file.
1
8
#include "
safeapi/redundancy/channel_link/sapi_channel.h
"
9
#include "
safeapi/utils/lifecycle/sapi_lifecycle.h
"
10
12
14
16
18
20
21
23
sapi_status_t
sapi_channel_init
(
sapi_channel_storage_t
*storage,
24
const
sapi_channel_config_t
*config)
25
{
26
sapi_status_t
lifecycle_status;
27
28
if
((storage == NULL) || (config == NULL))
29
{
30
return
SAPI_STATUS_INVALID_PARAM
;
31
}
32
if
((config->
send
== NULL) || (config->
recv
== NULL))
33
{
34
return
SAPI_STATUS_INVALID_PARAM
;
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
;
50
storage->
health
.
last_error
=
SAPI_STATUS_OK
;
51
storage->
initialized
=
true
;
52
53
return
SAPI_STATUS_OK
;
54
}
55
56
sapi_status_t
sapi_channel_send
(
sapi_channel_t
*handle,
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
{
64
return
SAPI_STATUS_INVALID_PARAM
;
65
}
66
if
(!handle->
initialized
)
67
{
68
return
SAPI_STATUS_NOT_INITIALIZED
;
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
84
sapi_status_t
sapi_channel_receive
(
sapi_channel_t
*handle,
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
{
93
return
SAPI_STATUS_INVALID_PARAM
;
94
}
95
if
(!handle->
initialized
)
96
{
97
return
SAPI_STATUS_NOT_INITIALIZED
;
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
113
sapi_status_t
sapi_channel_get_health
(
const
sapi_channel_t
*handle,
114
sapi_channel_health_t
*out_health)
115
{
116
if
((handle == NULL) || (out_health == NULL))
117
{
118
return
SAPI_STATUS_INVALID_PARAM
;
119
}
120
*out_health = handle->
health
;
121
return
SAPI_STATUS_OK
;
122
}
123
124
const
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
133
sapi_status_t
sapi_channel_set_healthy
(
sapi_channel_t
*handle,
134
bool
is_healthy)
135
{
136
if
(handle == NULL)
137
{
138
return
SAPI_STATUS_INVALID_PARAM
;
139
}
140
handle->
health
.
is_healthy
= is_healthy;
141
return
SAPI_STATUS_OK
;
142
}
143
144
sapi_status_t
sapi_channel_destroy
(
sapi_channel_t
*handle)
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_lifecycle_check_setup_allowed
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 ...
Definition
sapi_lifecycle.c:40
sapi_status_t
sapi_status_t
Common result/status codes.
Definition
sapi_status.h:27
SAPI_STATUS_INVALID_PARAM
@ SAPI_STATUS_INVALID_PARAM
Definition
sapi_status.h:29
SAPI_STATUS_NOT_INITIALIZED
@ SAPI_STATUS_NOT_INITIALIZED
Definition
sapi_status.h:30
SAPI_STATUS_OK
@ SAPI_STATUS_OK
Definition
sapi_status.h:28
sapi_channel_get_health
sapi_status_t sapi_channel_get_health(const sapi_channel_t *handle, sapi_channel_health_t *out_health)
Queries this channel's health statistics.
Definition
sapi_channel.c:113
sapi_channel_get_name
const char * sapi_channel_get_name(const sapi_channel_t *handle)
Returns this channel's name, as given in sapi_channel_config_t::name at sapi_channel_init() time.
Definition
sapi_channel.c:124
sapi_channel_set_healthy
sapi_status_t sapi_channel_set_healthy(sapi_channel_t *handle, bool is_healthy)
Marks this channel healthy/unhealthy.
Definition
sapi_channel.c:133
sapi_channel_destroy
sapi_status_t sapi_channel_destroy(sapi_channel_t *handle)
Destroys a vital channel instance.
Definition
sapi_channel.c:144
sapi_channel_send
sapi_status_t sapi_channel_send(sapi_channel_t *handle, const void *data, size_t data_size)
Sends data on this channel via its backend send callback.
Definition
sapi_channel.c:56
sapi_channel_receive
sapi_status_t sapi_channel_receive(sapi_channel_t *handle, void *data, size_t data_size, uint32_t timeout_ms)
Receives data on this channel via its backend receive callback.
Definition
sapi_channel.c:84
sapi_channel_init
sapi_status_t sapi_channel_init(sapi_channel_storage_t *storage, const sapi_channel_config_t *config)
Initializes one vital channel over a caller-supplied transport.
Definition
sapi_channel.c:23
sapi_channel_t
sapi_channel_storage_t sapi_channel_t
Opaque handle to a vital channel instance.
Definition
sapi_channel.h:127
sapi_channel.h
A single redundant "channel": an opaque transport handle plus send/receive callbacks,...
sapi_lifecycle.h
Process-wide application setup-phase lock (ADR-026).
sapi_channel_config_t
Configuration for sapi_channel_init().
Definition
sapi_channel.h:90
sapi_channel_config_t::send
sapi_channel_send_fn send
Definition
sapi_channel.h:96
sapi_channel_config_t::channel_handle
void * channel_handle
Definition
sapi_channel.h:94
sapi_channel_config_t::name
const char * name
Definition
sapi_channel.h:105
sapi_channel_config_t::recv
sapi_channel_recv_fn recv
Definition
sapi_channel.h:98
sapi_channel_health_t
Health statistics for one vital channel.
Definition
sapi_channel.h:39
sapi_channel_health_t::send_error_count
uint32_t send_error_count
Definition
sapi_channel.h:43
sapi_channel_health_t::last_error
sapi_status_t last_error
Definition
sapi_channel.h:53
sapi_channel_health_t::receive_count
uint32_t receive_count
Definition
sapi_channel.h:45
sapi_channel_health_t::send_count
uint32_t send_count
Definition
sapi_channel.h:41
sapi_channel_health_t::is_healthy
bool is_healthy
Definition
sapi_channel.h:51
sapi_channel_health_t::receive_error_count
uint32_t receive_error_count
Definition
sapi_channel.h:47
sapi_channel_storage_t
Storage for one vital channel instance (opaque to caller).
Definition
sapi_channel.h:114
sapi_channel_storage_t::config
sapi_channel_config_t config
Definition
sapi_channel.h:115
sapi_channel_storage_t::initialized
bool initialized
Definition
sapi_channel.h:117
sapi_channel_storage_t::health
sapi_channel_health_t health
Definition
sapi_channel.h:116
src
redundancy
channel_link
sapi_channel.c
Generated by
1.18.0