Coverage Report

Created: 2025-11-18 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/S2OPC/src/ClientServer/services/bgenc/channel_mgr.c
Line
Count
Source
1
/*
2
 * Licensed to Systerel under one or more contributor license
3
 * agreements. See the NOTICE file distributed with this work
4
 * for additional information regarding copyright ownership.
5
 * Systerel licenses this file to you under the Apache
6
 * License, Version 2.0 (the "License"); you may not use this
7
 * file except in compliance with the License. You may obtain
8
 * a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
/******************************************************************************
21
22
 File Name            : channel_mgr.c
23
24
 Date                 : 29/05/2024 08:51:39
25
26
 C Translator Version : tradc Java V1.2 (06/02/2022)
27
28
******************************************************************************/
29
30
/*------------------------
31
   Exported Declarations
32
  ------------------------*/
33
#include "channel_mgr.h"
34
35
/*----------------------------
36
   CONCRETE_VARIABLES Clause
37
  ----------------------------*/
38
t_bool channel_mgr__all_channel_closing;
39
t_bool channel_mgr__all_client_only_channel_closing;
40
41
/*------------------------
42
   INITIALISATION Clause
43
  ------------------------*/
44
0
void channel_mgr__INITIALISATION(void) {
45
0
   channel_mgr__all_channel_closing = false;
46
0
   channel_mgr__all_client_only_channel_closing = false;
47
0
}
48
49
/*--------------------
50
   OPERATIONS Clause
51
  --------------------*/
52
void channel_mgr__l_close_secure_channel(
53
   const constants__t_channel_i channel_mgr__p_channel,
54
0
   const constants_statuscodes_bs__t_StatusCode_i channel_mgr__p_statusCode) {
55
0
   {
56
0
      t_bool channel_mgr__l_res;
57
0
      constants__t_channel_config_idx_i channel_mgr__l_channel_conf;
58
0
      t_bool channel_mgr__l_is_client_channel;
59
      
60
0
      channel_mgr_1__getall_channel_connected(channel_mgr__p_channel,
61
0
         &channel_mgr__l_res,
62
0
         &channel_mgr__l_channel_conf);
63
0
      if (channel_mgr__l_res == true) {
64
0
         channel_mgr_1__is_client_channel(channel_mgr__p_channel,
65
0
            &channel_mgr__l_is_client_channel);
66
0
         if (channel_mgr__l_is_client_channel == true) {
67
0
            channel_mgr_1__add_cli_channel_disconnecting(channel_mgr__l_channel_conf);
68
0
         }
69
0
         channel_mgr_bs__finalize_close_secure_channel(channel_mgr__p_channel,
70
0
            channel_mgr__p_statusCode);
71
0
      }
72
0
   }
73
0
}
74
75
0
void channel_mgr__l_check_all_channel_lost(void) {
76
0
   {
77
0
      t_bool channel_mgr__l_cli_con;
78
0
      t_bool channel_mgr__l_any_channel_connected_or_connecting;
79
0
      t_bool channel_mgr__l_continue;
80
0
      constants__t_channel_i channel_mgr__l_channel;
81
0
      constants__t_channel_config_idx_i channel_mgr__l_config_idx;
82
      
83
0
      if ((channel_mgr__all_channel_closing == true) ||
84
0
         (channel_mgr__all_client_only_channel_closing == true)) {
85
0
         channel_mgr__l_any_channel_connected_or_connecting = false;
86
0
         channel_mgr_it__init_iter_channel(&channel_mgr__l_continue);
87
0
         while ((channel_mgr__l_continue == true) &&
88
0
            (channel_mgr__l_any_channel_connected_or_connecting == false)) {
89
0
            channel_mgr_it__continue_iter_channel(&channel_mgr__l_continue,
90
0
               &channel_mgr__l_channel);
91
0
            channel_mgr_1__is_client_channel(channel_mgr__l_channel,
92
0
               &channel_mgr__l_cli_con);
93
0
            if ((channel_mgr__all_channel_closing == true) ||
94
0
               ((channel_mgr__all_client_only_channel_closing == true) &&
95
0
               (channel_mgr__l_cli_con == true))) {
96
0
               channel_mgr_1__is_channel_connected(channel_mgr__l_channel,
97
0
                  &channel_mgr__l_any_channel_connected_or_connecting);
98
0
            }
99
0
         }
100
0
         channel_mgr_it__init_iter_channel_config_idx(&channel_mgr__l_continue);
101
0
         while ((channel_mgr__l_continue == true) &&
102
0
            (channel_mgr__l_any_channel_connected_or_connecting == false)) {
103
0
            channel_mgr_it__continue_iter_channel_config_idx(&channel_mgr__l_continue,
104
0
               &channel_mgr__l_config_idx);
105
0
            channel_mgr_1__is_cli_channel_connecting(channel_mgr__l_config_idx,
106
0
               &channel_mgr__l_any_channel_connected_or_connecting);
107
0
         }
108
0
         if (channel_mgr__l_any_channel_connected_or_connecting == false) {
109
0
            channel_mgr_bs__last_connected_channel_lost(channel_mgr__all_client_only_channel_closing);
110
0
            channel_mgr__all_channel_closing = false;
111
0
            channel_mgr__all_client_only_channel_closing = false;
112
0
         }
113
0
      }
114
0
   }
115
0
}
116
117
void channel_mgr__l_is_new_sc_connection_allowed(
118
   const t_bool channel_mgr__is_one_sc_auto_closing,
119
0
   t_bool * const channel_mgr__l_allowed_new_sc) {
120
0
   {
121
0
      t_entier4 channel_mgr__l_card_used;
122
0
      t_entier4 channel_mgr__l_card_channel;
123
      
124
0
      *channel_mgr__l_allowed_new_sc = false;
125
0
      channel_mgr_1__get_card_channel_used(&channel_mgr__l_card_used);
126
0
      constants__get_card_t_channel(&channel_mgr__l_card_channel);
127
0
      if (channel_mgr__l_card_used != channel_mgr__l_card_channel) {
128
0
         if ((channel_mgr__l_card_used < constants__c_max_channels_connected) ||
129
0
            (channel_mgr__is_one_sc_auto_closing == true)) {
130
0
            *channel_mgr__l_allowed_new_sc = true;
131
0
         }
132
0
      }
133
0
   }
134
0
}
135
136
void channel_mgr__cli_open_secure_channel(
137
   const constants__t_channel_config_idx_i channel_mgr__config_idx,
138
   const constants__t_reverse_endpoint_config_idx_i channel_mgr__reverse_endpoint_config_idx,
139
   const t_bool channel_mgr__is_one_sc_auto_closing,
140
0
   t_bool * const channel_mgr__bres) {
141
0
   {
142
0
      t_bool channel_mgr__l_already_connecting;
143
0
      t_bool channel_mgr__l_dom;
144
0
      constants__t_channel_i channel_mgr__l_channel;
145
0
      t_bool channel_mgr__l_allowed_new_sc;
146
      
147
0
      channel_mgr_1__is_cli_channel_connecting(channel_mgr__config_idx,
148
0
         &channel_mgr__l_already_connecting);
149
0
      if (channel_mgr__l_already_connecting == true) {
150
0
         *channel_mgr__bres = true;
151
0
      }
152
0
      else {
153
0
         channel_mgr_1__getall_config_inv(channel_mgr__config_idx,
154
0
            &channel_mgr__l_dom,
155
0
            &channel_mgr__l_channel);
156
0
         if (channel_mgr__l_dom == false) {
157
0
            channel_mgr__l_is_new_sc_connection_allowed(channel_mgr__is_one_sc_auto_closing,
158
0
               &channel_mgr__l_allowed_new_sc);
159
0
            if (channel_mgr__l_allowed_new_sc == true) {
160
0
               channel_mgr_1__add_cli_channel_connecting(channel_mgr__config_idx);
161
0
               channel_mgr_bs__prepare_cli_open_secure_channel(channel_mgr__config_idx,
162
0
                  channel_mgr__reverse_endpoint_config_idx);
163
0
               *channel_mgr__bres = true;
164
0
            }
165
0
            else {
166
0
               *channel_mgr__bres = false;
167
0
            }
168
0
         }
169
0
         else {
170
0
            *channel_mgr__bres = false;
171
0
         }
172
0
      }
173
0
   }
174
0
}
175
176
void channel_mgr__srv_new_secure_channel(
177
   const constants__t_endpoint_config_idx_i channel_mgr__endpoint_config_idx,
178
   const constants__t_channel_config_idx_i channel_mgr__channel_config_idx,
179
   const constants__t_channel_i channel_mgr__channel,
180
   const t_bool channel_mgr__is_one_sc_auto_closing,
181
0
   t_bool * const channel_mgr__bres) {
182
0
   {
183
0
      t_bool channel_mgr__l_con;
184
0
      t_bool channel_mgr__l_dom;
185
0
      constants__t_channel_i channel_mgr__l_channel;
186
0
      constants__t_timeref_i channel_mgr__l_current_time;
187
0
      t_bool channel_mgr__l_allowed_new_sc;
188
      
189
0
      channel_mgr_1__is_channel_connected(channel_mgr__channel,
190
0
         &channel_mgr__l_con);
191
0
      channel_mgr_1__getall_config_inv(channel_mgr__channel_config_idx,
192
0
         &channel_mgr__l_dom,
193
0
         &channel_mgr__l_channel);
194
0
      channel_mgr__l_is_new_sc_connection_allowed(channel_mgr__is_one_sc_auto_closing,
195
0
         &channel_mgr__l_allowed_new_sc);
196
0
      if (((channel_mgr__l_con == false) &&
197
0
         (channel_mgr__l_dom == false)) &&
198
0
         (channel_mgr__l_allowed_new_sc == true)) {
199
0
         time_reference_bs__get_current_TimeReference(&channel_mgr__l_current_time);
200
0
         channel_mgr_1__add_channel_connected(channel_mgr__channel,
201
0
            channel_mgr__l_current_time);
202
0
         channel_mgr_1__set_config(channel_mgr__channel,
203
0
            channel_mgr__channel_config_idx,
204
0
            constants__c_reverse_endpoint_config_idx_indet);
205
0
         channel_mgr_1__set_endpoint(channel_mgr__channel,
206
0
            channel_mgr__endpoint_config_idx);
207
0
         channel_mgr_bs__define_SecurityPolicy(channel_mgr__channel);
208
0
         *channel_mgr__bres = true;
209
0
      }
210
0
      else {
211
0
         *channel_mgr__bres = false;
212
0
      }
213
0
   }
214
0
}
215
216
void channel_mgr__close_secure_channel(
217
   const constants__t_channel_i channel_mgr__channel,
218
0
   const constants_statuscodes_bs__t_StatusCode_i channel_mgr__statusCode) {
219
0
   channel_mgr__l_close_secure_channel(channel_mgr__channel,
220
0
      channel_mgr__statusCode);
221
0
}
222
223
void channel_mgr__close_all_channel(
224
   const t_bool channel_mgr__p_clientOnly,
225
0
   t_bool * const channel_mgr__bres) {
226
0
   {
227
0
      t_bool channel_mgr__l_continue;
228
0
      constants__t_channel_i channel_mgr__l_channel;
229
0
      constants__t_channel_config_idx_i channel_mgr__l_config_idx;
230
0
      t_bool channel_mgr__l_connected;
231
0
      t_bool channel_mgr__l_connecting;
232
0
      t_bool channel_mgr__l_cli_con;
233
0
      t_bool channel_mgr__l_any_channel_closing_or_connecting;
234
      
235
0
      channel_mgr__l_any_channel_closing_or_connecting = false;
236
0
      channel_mgr_it__init_iter_channel(&channel_mgr__l_continue);
237
0
      while (channel_mgr__l_continue == true) {
238
0
         channel_mgr_it__continue_iter_channel(&channel_mgr__l_continue,
239
0
            &channel_mgr__l_channel);
240
0
         channel_mgr_1__is_channel_connected(channel_mgr__l_channel,
241
0
            &channel_mgr__l_connected);
242
0
         channel_mgr_1__is_client_channel(channel_mgr__l_channel,
243
0
            &channel_mgr__l_cli_con);
244
0
         if ((channel_mgr__l_connected == true) &&
245
0
            ((channel_mgr__p_clientOnly == false) ||
246
0
            (channel_mgr__l_cli_con == true))) {
247
0
            channel_mgr__l_any_channel_closing_or_connecting = true;
248
0
            channel_mgr__l_close_secure_channel(channel_mgr__l_channel,
249
0
               constants_statuscodes_bs__e_sc_bad_secure_channel_closed);
250
0
         }
251
0
      }
252
0
      channel_mgr_it__init_iter_channel_config_idx(&channel_mgr__l_continue);
253
0
      while (channel_mgr__l_continue == true) {
254
0
         channel_mgr_it__continue_iter_channel_config_idx(&channel_mgr__l_continue,
255
0
            &channel_mgr__l_config_idx);
256
0
         channel_mgr_1__is_cli_channel_connecting(channel_mgr__l_config_idx,
257
0
            &channel_mgr__l_connecting);
258
0
         if (channel_mgr__l_connecting == true) {
259
0
            channel_mgr__l_any_channel_closing_or_connecting = true;
260
0
            channel_mgr_1__add_cli_channel_disconnecting(channel_mgr__l_config_idx);
261
0
         }
262
0
      }
263
0
      channel_mgr__all_channel_closing = ((channel_mgr__p_clientOnly == false) &&
264
0
         (channel_mgr__l_any_channel_closing_or_connecting == true));
265
0
      channel_mgr__all_client_only_channel_closing = ((channel_mgr__p_clientOnly == true) &&
266
0
         (channel_mgr__l_any_channel_closing_or_connecting == true));
267
0
      *channel_mgr__bres = channel_mgr__l_any_channel_closing_or_connecting;
268
0
   }
269
0
}
270
271
void channel_mgr__channel_lost(
272
0
   const constants__t_channel_i channel_mgr__channel) {
273
0
   {
274
0
      t_bool channel_mgr__l_res;
275
0
      constants__t_channel_config_idx_i channel_mgr__l_channel_conf;
276
      
277
0
      channel_mgr_1__getall_channel_connected(channel_mgr__channel,
278
0
         &channel_mgr__l_res,
279
0
         &channel_mgr__l_channel_conf);
280
0
      if (channel_mgr__l_res == true) {
281
0
         channel_mgr_1__remove_channel_connected(channel_mgr__channel);
282
0
         channel_mgr_1__remove_cli_channel_disconnecting(channel_mgr__l_channel_conf);
283
0
         channel_mgr_1__reset_config(channel_mgr__channel);
284
0
         channel_mgr_1__reset_endpoint(channel_mgr__channel);
285
0
         channel_mgr_bs__reset_SecurityPolicy(channel_mgr__channel);
286
0
         channel_mgr__l_check_all_channel_lost();
287
0
      }
288
0
   }
289
0
}
290
291
void channel_mgr__cli_set_connected_channel(
292
   const constants__t_channel_config_idx_i channel_mgr__config_idx,
293
   const constants__t_reverse_endpoint_config_idx_i channel_mgr__reverse_endpoint_config_idx,
294
   const constants__t_channel_i channel_mgr__channel,
295
0
   t_bool * const channel_mgr__bres) {
296
0
   {
297
0
      t_bool channel_mgr__l_is_channel_connecting;
298
0
      t_bool channel_mgr__l_is_channel_disconnecting;
299
0
      t_bool channel_mgr__l_is_channel_connected;
300
0
      constants__t_timeref_i channel_mgr__l_current_time;
301
      
302
0
      channel_mgr_1__is_cli_channel_connecting(channel_mgr__config_idx,
303
0
         &channel_mgr__l_is_channel_connecting);
304
0
      channel_mgr_1__is_disconnecting_channel(channel_mgr__config_idx,
305
0
         &channel_mgr__l_is_channel_disconnecting);
306
0
      channel_mgr_1__is_channel_connected(channel_mgr__channel,
307
0
         &channel_mgr__l_is_channel_connected);
308
0
      if ((channel_mgr__l_is_channel_connecting == true) &&
309
0
         (channel_mgr__l_is_channel_connected == false)) {
310
0
         channel_mgr_1__remove_cli_channel_connecting(channel_mgr__config_idx);
311
0
         time_reference_bs__get_current_TimeReference(&channel_mgr__l_current_time);
312
0
         channel_mgr_1__add_channel_connected(channel_mgr__channel,
313
0
            channel_mgr__l_current_time);
314
0
         channel_mgr_1__set_config(channel_mgr__channel,
315
0
            channel_mgr__config_idx,
316
0
            channel_mgr__reverse_endpoint_config_idx);
317
0
         channel_mgr_bs__define_SecurityPolicy(channel_mgr__channel);
318
0
         if (channel_mgr__l_is_channel_disconnecting == true) {
319
0
            channel_mgr__l_close_secure_channel(channel_mgr__channel,
320
0
               constants_statuscodes_bs__e_sc_bad_secure_channel_closed);
321
0
         }
322
0
         *channel_mgr__bres = true;
323
0
      }
324
0
      else {
325
0
         *channel_mgr__bres = false;
326
0
      }
327
0
   }
328
0
}
329
330
void channel_mgr__cli_set_connection_timeout_channel(
331
   const constants__t_channel_config_idx_i channel_mgr__config_idx,
332
0
   t_bool * const channel_mgr__bres) {
333
0
   {
334
0
      t_bool channel_mgr__l_res;
335
      
336
0
      channel_mgr_1__is_cli_channel_connecting(channel_mgr__config_idx,
337
0
         &channel_mgr__l_res);
338
0
      if (channel_mgr__l_res == true) {
339
0
         channel_mgr_1__remove_cli_channel_connecting(channel_mgr__config_idx);
340
0
         channel_mgr__l_check_all_channel_lost();
341
0
         *channel_mgr__bres = true;
342
0
      }
343
0
      else {
344
0
         *channel_mgr__bres = false;
345
0
      }
346
0
   }
347
0
}
348
349
void channel_mgr__is_auto_close_channel_active(
350
0
   t_bool * const channel_mgr__p_auto_closed_active) {
351
0
   {
352
0
      t_entier4 channel_mgr__l_card_used;
353
0
      t_entier4 channel_mgr__l_card_channel;
354
      
355
0
      channel_mgr_1__get_card_channel_used(&channel_mgr__l_card_used);
356
0
      constants__get_card_t_channel(&channel_mgr__l_card_channel);
357
0
      *channel_mgr__p_auto_closed_active = (channel_mgr__l_card_used >= constants__c_max_channels_connected);
358
0
   }
359
0
}
360
361
void channel_mgr__set_create_session_locked(
362
0
   const constants__t_channel_i channel_mgr__p_channel) {
363
0
   {
364
0
      constants__t_timeref_i channel_mgr__l_end_lock_target_timeref;
365
      
366
0
      time_reference_bs__get_target_TimeReference(constants__c_channel_lock_create_session_delay,
367
0
         &channel_mgr__l_end_lock_target_timeref);
368
0
      channel_mgr_1__set_create_session_locked_1(channel_mgr__p_channel,
369
0
         channel_mgr__l_end_lock_target_timeref);
370
0
   }
371
0
}
372
373
void channel_mgr__update_create_session_locked(
374
0
   const constants__t_channel_i channel_mgr__p_channel) {
375
0
   {
376
0
      constants__t_timeref_i channel_mgr__l_current_timeref;
377
0
      constants__t_timeref_i channel_mgr__l_target_timeref;
378
0
      t_bool channel_mgr__l_target_not_reached;
379
      
380
0
      channel_mgr_1__get_create_session_locked_1(channel_mgr__p_channel,
381
0
         &channel_mgr__l_target_timeref);
382
0
      if (channel_mgr__l_target_timeref != constants__c_timeref_indet) {
383
0
         time_reference_bs__get_current_TimeReference(&channel_mgr__l_current_timeref);
384
0
         time_reference_bs__is_less_than_TimeReference(channel_mgr__l_current_timeref,
385
0
            channel_mgr__l_target_timeref,
386
0
            &channel_mgr__l_target_not_reached);
387
0
         if (channel_mgr__l_target_not_reached == false) {
388
0
            channel_mgr_1__set_create_session_locked_1(channel_mgr__p_channel,
389
0
               constants__c_timeref_indet);
390
0
         }
391
0
      }
392
0
   }
393
0
}
394
395
void channel_mgr__is_create_session_locked(
396
   const constants__t_channel_i channel_mgr__p_channel,
397
0
   t_bool * const channel_mgr__p_session_locked) {
398
0
   {
399
0
      constants__t_timeref_i channel_mgr__l_target_timeref;
400
      
401
0
      channel_mgr_1__get_create_session_locked_1(channel_mgr__p_channel,
402
0
         &channel_mgr__l_target_timeref);
403
0
      *channel_mgr__p_session_locked = (channel_mgr__l_target_timeref != constants__c_timeref_indet);
404
0
   }
405
0
}
406