/src/open62541/arch/common/eventloop_common.c
Line | Count | Source |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | | * |
5 | | * Copyright 2022 (c) Fraunhofer IOSB (Author: Julius Pfrommer) |
6 | | * Copyright 2026 (c) o6 Automation GmbH (Author: Julius Pfrommer) |
7 | | */ |
8 | | |
9 | | #include "eventloop_common.h" |
10 | | |
11 | | UA_StatusCode |
12 | | UA_KeyValueRestriction_validate(const UA_Logger *logger, const char *logprefix, |
13 | | const UA_KeyValueRestriction *restrictions, |
14 | | size_t restrictionsSize, |
15 | 0 | const UA_KeyValueMap *map) { |
16 | 0 | for(size_t i = 0; i < restrictionsSize; i++) { |
17 | 0 | const UA_KeyValueRestriction *r = &restrictions[i]; |
18 | 0 | const UA_Variant *val = UA_KeyValueMap_get(map, r->name); |
19 | | |
20 | | /* Value not present but required? */ |
21 | 0 | if(!val) { |
22 | 0 | if(r->required) { |
23 | 0 | UA_LOG_WARNING(logger, UA_LOGCATEGORY_EVENTLOOP, |
24 | 0 | "%s\t| Parameter %.*s required but not defined", |
25 | 0 | logprefix, (int)r->name.name.length, (char*)r->name.name.data); |
26 | 0 | return UA_STATUSCODE_BADINTERNALERROR; |
27 | 0 | } |
28 | 0 | continue; |
29 | 0 | } |
30 | | |
31 | | /* A NULL restriction type accepts an opaque Variant of any type. */ |
32 | 0 | if(r->type && val->type != r->type) { |
33 | 0 | UA_LOG_WARNING(logger, UA_LOGCATEGORY_EVENTLOOP, |
34 | 0 | "%s\t| Parameter %.*s has the wrong type", |
35 | 0 | logprefix, (int)r->name.name.length, (char*)r->name.name.data); |
36 | 0 | return UA_STATUSCODE_BADINTERNALERROR; |
37 | 0 | } |
38 | | |
39 | | /* Scalar / array is allowed */ |
40 | 0 | UA_Boolean scalar = UA_Variant_isScalar(val); |
41 | 0 | if(scalar && !r->scalar) { |
42 | 0 | UA_LOG_WARNING(logger, UA_LOGCATEGORY_EVENTLOOP, |
43 | 0 | "%s\t| Parameter %.*s must not be scalar", |
44 | 0 | logprefix, (int)r->name.name.length, (char*)r->name.name.data); |
45 | 0 | return UA_STATUSCODE_BADINTERNALERROR; |
46 | 0 | } |
47 | 0 | if(!scalar && !r->array) { |
48 | 0 | UA_LOG_WARNING(logger, UA_LOGCATEGORY_EVENTLOOP, |
49 | 0 | "%s\t| Parameter %.*s must not be an array", |
50 | 0 | logprefix, (int)r->name.name.length, (char*)r->name.name.data); |
51 | 0 | return UA_STATUSCODE_BADCONNECTIONREJECTED; |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | 0 | return UA_STATUSCODE_GOOD; |
56 | 0 | } |
57 | | |
58 | | UA_StatusCode |
59 | | UA_EventLoopCommon_allocStaticBuffer(UA_KeyValueMap *params, |
60 | | UA_QualifiedName name, |
61 | | UA_UInt32 defaultSize, |
62 | 0 | UA_ByteString *buf) { |
63 | 0 | UA_StatusCode res = UA_STATUSCODE_GOOD; |
64 | 0 | UA_UInt32 bufSize = defaultSize; |
65 | 0 | const UA_UInt32 *configBufSize = (const UA_UInt32 *) |
66 | 0 | UA_KeyValueMap_getScalar(params, name, &UA_TYPES[UA_TYPES_UINT32]); |
67 | 0 | if(configBufSize) |
68 | 0 | bufSize = *configBufSize; |
69 | 0 | else |
70 | | /* Write the resolved default back into the params so the |
71 | | * SecureChannel constraint logic in ua_transport_tcp.c caps the |
72 | | * channel to the actual static-buffer size. */ |
73 | 0 | res = UA_KeyValueMap_setScalar(params, name, &bufSize, |
74 | 0 | &UA_TYPES[UA_TYPES_UINT32]); |
75 | 0 | if(buf->length != bufSize) { |
76 | 0 | UA_ByteString_clear(buf); |
77 | 0 | res |= UA_ByteString_allocBuffer(buf, bufSize); |
78 | 0 | } |
79 | 0 | return res; |
80 | 0 | } |