Browse Source

Fix setup of namespace 1 URI

Jojakim Stahl 5 years ago
parent
commit
6debf8cd84
3 changed files with 22 additions and 2 deletions
  1. 15 2
      src/server/ua_server.c
  2. 1 0
      src/server/ua_server_internal.h
  3. 6 0
      src/server/ua_server_ns0.c

+ 15 - 2
src/server/ua_server.c

@@ -34,7 +34,16 @@
 /* Namespace Handling */
 /**********************/
 
+void setupNs1Uri(UA_Server *server) {
+    if (!server->namespaces[1].data) {
+        UA_String_copy(&server->config.applicationDescription.applicationUri, &server->namespaces[1]);
+    }
+}
+
 UA_UInt16 addNamespace(UA_Server *server, const UA_String name) {
+    /* ensure that the uri for ns1 is set up from the app description */
+    setupNs1Uri(server);
+
     /* Check if the namespace already exists in the server's namespace array */
     for(UA_UInt16 i = 0; i < server->namespacesSize; ++i) {
         if(UA_String_equal(&name, &server->namespaces[i]))
@@ -78,6 +87,9 @@ UA_Server_getConfig(UA_Server *server)
 UA_StatusCode
 UA_Server_getNamespaceByName(UA_Server *server, const UA_String namespaceUri,
                              size_t* foundIndex) {
+    /* ensure that the uri for ns1 is set up from the app description */
+    setupNs1Uri(server);
+
     for(size_t idx = 0; idx < server->namespacesSize; idx++) {
         if(!UA_String_equal(&server->namespaces[idx], &namespaceUri))
             continue;
@@ -214,14 +226,15 @@ UA_Server_new() {
     server->adminSession.sessionId.identifier.guid.data1 = 1;
     server->adminSession.validTill = UA_INT64_MAX;
 
-    /* Create Namespaces 0 and 1 */
+    /* Create Namespaces 0 and 1
+     * Ns1 will be filled later with the uri from the app description */
     server->namespaces = (UA_String *)UA_Array_new(2, &UA_TYPES[UA_TYPES_STRING]);
     if(!server->namespaces) {
         UA_Server_delete(server);
         return NULL;
     }
     server->namespaces[0] = UA_STRING_ALLOC("http://opcfoundation.org/UA/");
-    UA_String_copy(&server->config.applicationDescription.applicationUri, &server->namespaces[1]);
+    server->namespaces[1] = UA_STRING_NULL;
     server->namespacesSize = 2;
 
     /* Initialized SecureChannel and Session managers */

+ 1 - 0
src/server/ua_server_internal.h

@@ -135,6 +135,7 @@ UA_StatusCode UA_Server_editNode(UA_Server *server, UA_Session *session,
 extern const UA_NodeId subtypeId;
 extern const UA_NodeId hierarchicalReferences;
 
+void setupNs1Uri(UA_Server *server);
 UA_UInt16 addNamespace(UA_Server *server, const UA_String name);
 
 UA_Boolean

+ 6 - 0
src/server/ua_server_ns0.c

@@ -400,6 +400,9 @@ readNamespaces(UA_Server *server, const UA_NodeId *sessionId, void *sessionConte
                const UA_NodeId *nodeid, void *nodeContext, UA_Boolean includeSourceTimeStamp,
                const UA_NumericRange *range,
                UA_DataValue *value) {
+    /* ensure that the uri for ns1 is set up from the app description */
+    setupNs1Uri(server);
+
     if(range) {
         value->hasStatus = true;
         value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
@@ -442,6 +445,9 @@ writeNamespaces(UA_Server *server, const UA_NodeId *sessionId, void *sessionCont
     if(newNamespacesSize <= server->namespacesSize)
         return UA_STATUSCODE_BADTYPEMISMATCH;
 
+    /* ensure that the uri for ns1 is set up from the app description */
+    setupNs1Uri(server);
+    
     /* Test if the existing namespaces are unchanged */
     for(size_t i = 0; i < server->namespacesSize; ++i) {
         if(!UA_String_equal(&server->namespaces[i], &newNamespaces[i]))