/src/S2OPC/src/Common/configuration/sopc_common_constants.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "sopc_common_constants.h" |
21 | | |
22 | | #include "sopc_assert.h" |
23 | | #include "sopc_atomic.h" |
24 | | |
25 | | bool SOPC_Internal_Common_Constants_RuntimeCheck(void) |
26 | 0 | { |
27 | 0 | bool res = (sizeof(uintptr_t) == sizeof(void*)); |
28 | 0 | SOPC_ASSERT(res && "uintptr_t has not same size as void* which is expected for other language bindings"); |
29 | 0 | return res; |
30 | 0 | } |
31 | | |
32 | | static SOPC_Common_EncodingConstants globalEncodingConfig = { |
33 | | .buffer_size = SOPC_DEFAULT_TCP_UA_MAX_BUFFER_SIZE, |
34 | | .receive_max_msg_size = SOPC_DEFAULT_RECEIVE_MAX_MESSAGE_LENGTH, |
35 | | .receive_max_nb_chunks = SOPC_DEFAULT_RECEIVE_MAX_NB_CHUNKS, |
36 | | .send_max_msg_size = SOPC_DEFAULT_SEND_MAX_MESSAGE_LENGTH, |
37 | | .send_max_nb_chunks = SOPC_DEFAULT_SEND_MAX_NB_CHUNKS, |
38 | | |
39 | | .max_string_length = SOPC_DEFAULT_MAX_STRING_LENGTH, |
40 | | .max_array_length = SOPC_DEFAULT_MAX_ARRAY_LENGTH, |
41 | | .max_nested_diag_info = SOPC_DEFAULT_MAX_DIAG_INFO_NESTED_LEVEL, |
42 | | .max_nested_struct = SOPC_DEFAULT_MAX_STRUCT_NESTED_LEVEL}; |
43 | | |
44 | | int32_t globalEncodingConfigSet = false; |
45 | | |
46 | | SOPC_Common_EncodingConstants SOPC_Common_GetDefaultEncodingConstants(void) |
47 | 0 | { |
48 | 0 | return (SOPC_Common_EncodingConstants){.buffer_size = SOPC_DEFAULT_TCP_UA_MAX_BUFFER_SIZE, |
49 | 0 | .receive_max_msg_size = SOPC_DEFAULT_RECEIVE_MAX_MESSAGE_LENGTH, |
50 | 0 | .receive_max_nb_chunks = SOPC_DEFAULT_RECEIVE_MAX_NB_CHUNKS, |
51 | 0 | .send_max_msg_size = SOPC_DEFAULT_SEND_MAX_MESSAGE_LENGTH, |
52 | 0 | .send_max_nb_chunks = SOPC_DEFAULT_SEND_MAX_NB_CHUNKS, |
53 | |
|
54 | 0 | .max_string_length = SOPC_DEFAULT_MAX_STRING_LENGTH, |
55 | 0 | .max_array_length = SOPC_DEFAULT_MAX_ARRAY_LENGTH, |
56 | 0 | .max_nested_diag_info = SOPC_DEFAULT_MAX_DIAG_INFO_NESTED_LEVEL, |
57 | 0 | .max_nested_struct = SOPC_DEFAULT_MAX_STRUCT_NESTED_LEVEL}; |
58 | 0 | } |
59 | | |
60 | | const SOPC_Common_EncodingConstants* SOPC_Internal_Common_GetEncodingConstants(void) |
61 | 74.3M | { |
62 | 74.3M | return &globalEncodingConfig; |
63 | 74.3M | } |
64 | | |
65 | | bool SOPC_Common_SetEncodingConstants(SOPC_Common_EncodingConstants config) |
66 | 0 | { |
67 | | // Check same constraints that the one on default values |
68 | |
|
69 | 0 | if ((config.receive_max_msg_size != 0 && config.receive_max_msg_size < config.buffer_size) || |
70 | 0 | (config.send_max_msg_size != 0 && config.send_max_msg_size < config.buffer_size)) |
71 | 0 | { |
72 | 0 | return false; |
73 | 0 | } |
74 | | |
75 | 0 | if ((0 == config.receive_max_msg_size && 0 == config.receive_max_nb_chunks) || |
76 | 0 | (0 == config.send_max_msg_size && 0 == config.send_max_nb_chunks)) |
77 | 0 | { |
78 | 0 | return false; |
79 | 0 | } |
80 | | |
81 | 0 | if (!SOPC_Atomic_Int_Get(&globalEncodingConfigSet)) |
82 | 0 | { |
83 | 0 | SOPC_Atomic_Int_Set(&globalEncodingConfigSet, true); |
84 | | |
85 | | // Enforce definition of max message size |
86 | 0 | if (0 == config.send_max_msg_size) |
87 | 0 | { |
88 | 0 | SOPC_ASSERT(0 != config.send_max_nb_chunks); |
89 | 0 | config.send_max_msg_size = config.send_max_nb_chunks * config.buffer_size; |
90 | 0 | } |
91 | 0 | if (0 == config.receive_max_msg_size) |
92 | 0 | { |
93 | 0 | SOPC_ASSERT(0 != config.receive_max_msg_size); |
94 | 0 | config.receive_max_msg_size = config.receive_max_nb_chunks * config.buffer_size; |
95 | 0 | } |
96 | | |
97 | 0 | globalEncodingConfig = config; |
98 | 0 | return true; |
99 | 0 | } |
100 | 0 | else |
101 | 0 | { |
102 | 0 | return false; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | bool SOPC_Common_EncodingConstantsGetInitialized(void) |
107 | 0 | { |
108 | 0 | return SOPC_Atomic_Int_Get(&globalEncodingConfigSet); |
109 | 0 | } |