Explorar el Código

CTT: Add DiscoveryUrl in ApplicationDescription of Server Endpoint

Signed-off-by: Jayanth Velusamy <jayanth.v@kalycito.com>
Jayanth Velusamy hace 4 años
padre
commit
17fd93b2a0
Se han modificado 1 ficheros con 35 adiciones y 0 borrados
  1. 35 0
      plugins/ua_config_default.c

+ 35 - 0
plugins/ua_config_default.c

@@ -227,6 +227,29 @@ addDefaultNetworkLayers(UA_ServerConfig *conf, UA_UInt16 portNumber,
     return UA_ServerConfig_addNetworkLayerTCP(conf, portNumber, sendBufferSize, recvBufferSize);
 }
 
+static UA_StatusCode
+addDiscoveryUrl(UA_ServerConfig *config, UA_UInt16 portNumber) {
+       config->applicationDescription.discoveryUrlsSize = 1;
+    UA_String *discurl = (UA_String *) UA_Array_new(1, &UA_TYPES[UA_TYPES_STRING]);
+    char discoveryUrlBuffer[220];
+    if (config->customHostname.length) {
+        UA_snprintf(discoveryUrlBuffer, 220, "opc.tcp://%.*s:%d/",
+                                                 (int)config->customHostname.length,
+                                                 config->customHostname.data,
+                                                 portNumber);
+    } else {
+    char hostnameBuffer[200];
+       if(UA_gethostname(hostnameBuffer, 200) == 0) {
+               UA_snprintf(discoveryUrlBuffer, 220, "opc.tcp://%s:%d/", hostnameBuffer, portNumber);
+       } else {
+            UA_LOG_ERROR(&config->logger, UA_LOGCATEGORY_NETWORK, "Could not get the hostname");
+        }
+    }
+    discurl[0] = UA_String_fromChars(discoveryUrlBuffer);
+    config->applicationDescription.discoveryUrls = discurl;
+    return UA_STATUSCODE_GOOD;
+}
+
 UA_EXPORT UA_StatusCode
 UA_ServerConfig_addNetworkLayerTCP(UA_ServerConfig *conf, UA_UInt16 portNumber,
                                    UA_UInt32 sendBufferSize, UA_UInt32 recvBufferSize) {
@@ -369,6 +392,12 @@ UA_ServerConfig_setMinimalCustomBuffer(UA_ServerConfig *config, UA_UInt16 portNu
         UA_ServerConfig_clean(config);
         return retval;
     }
+    
+    retval = addDiscoveryUrl(config, portNumber);
+    if (retval != UA_STATUSCODE_GOOD) {
+        UA_ServerConfig_clean(config);
+        return retval;
+    }
 
     /* Allocate the SecurityPolicies */
     retval = UA_ServerConfig_addSecurityPolicyNone(config, certificate);
@@ -574,6 +603,12 @@ UA_ServerConfig_setDefaultWithSecurityPolicies(UA_ServerConfig *conf,
         return retval;
     }
 
+    retval = addDiscoveryUrl(conf, portNumber);
+    if (retval != UA_STATUSCODE_GOOD) {
+        UA_ServerConfig_clean(conf);
+        return retval;
+    }
+
     retval = UA_ServerConfig_addAllSecurityPolicies(conf, certificate, privateKey);
     if(retval != UA_STATUSCODE_GOOD) {
         UA_ServerConfig_clean(conf);