Skip to content

Commit

Permalink
Upgrade FORTE to Open62541 1.4 release (eclipse-4diac#260)
Browse files Browse the repository at this point in the history
* Add changes to implementation for open62541 1.4 support

* Refactor opcua server configuration for open62541 1.4

* Refactor custom hostname handling for open62541 1.4

* Fix memory leak with UA Logger

* Switch to unordered map in Abstract OPC UA Handler

Co-authored-by: cochicde <cabralcochi@gmail.com>

* Refactor handling of custom hostname

---------

Co-authored-by: cochicde <cabralcochi@gmail.com>
  • Loading branch information
m-meingast and cochicde authored Oct 30, 2024
1 parent 85edca3 commit f11a174
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/com/opc_ua/opcua_client_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool CUA_ClientInformation::configureClient() {

if(configureClientFromFile(*configPointer)) {
configPointer->stateCallback = CUA_RemoteCallbackFunctions::clientStateChangeCallback;
configPointer->logger = COPC_UA_HandlerAbstract::getLogger();
configPointer->logging = &COPC_UA_HandlerAbstract::getLogger();
configPointer->timeout = scmClientTimeoutInMilli;
} else {
UA_Client_delete(mClient);
Expand Down Expand Up @@ -242,7 +242,7 @@ UA_StatusCode CUA_ClientInformation::executeCallMethod(CActionInfo& paActionInfo
}

UA_RemoteCallHandle *remoteCallHandle = new UA_RemoteCallHandle(paActionInfo, *this);
retVal = UA_Client_sendAsyncRequest(mClient, &request, &UA_TYPES[UA_TYPES_CALLREQUEST], CUA_RemoteCallbackFunctions::callMethodAsyncCallback,
retVal = __UA_Client_AsyncService(mClient, &request, &UA_TYPES[UA_TYPES_CALLREQUEST], CUA_RemoteCallbackFunctions::callMethodAsyncCallback,
&UA_TYPES[UA_TYPES_CALLRESPONSE], remoteCallHandle, nullptr);

if(UA_STATUSCODE_GOOD != retVal) {
Expand Down
34 changes: 21 additions & 13 deletions src/com/opc_ua/opcua_handler_abstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@
#include <forte_printer.h>
#include <devlog.h>
#include <parameterParser.h>
#include <unordered_map>

//we tried to use the logLevelNames and logCategoryNames as extern from open62541 but it failed when using with shared libray
const char *const LogLevelNames[6] = {
"trace",
"debug",
"info",
"warning",
"error",
"fatal" };
const char *const LogCategoryNames[7] = {
const std::unordered_map<size_t, const char*> LogLevelNames {
{100, "trace"},
{200, "debug"},
{300, "info"},
{400, "warning"},
{500, "error"},
{600, "fatal"},
};

const char *const LogCategoryNames[10] = {
"network",
"channel",
"session",
"server",
"client",
"userland" };
"userland",
"securitypolicy",
"eventloop",
"pubsub",
"discovery"
};

const UA_Logger COPC_UA_HandlerAbstract::UA_Forte_logger = {UA_Log_Forte, nullptr, UA_Log_Forte_clear};
UA_Logger COPC_UA_HandlerAbstract::UA_Forte_logger = {UA_Log_Forte, nullptr, UA_Log_Forte_clear};

COPC_UA_HandlerAbstract::COPC_UA_HandlerAbstract(CDeviceExecution& paDeviceExecution) :
CExternalEventHandler(paDeviceExecution) {
Expand All @@ -52,7 +60,7 @@ int COPC_UA_HandlerAbstract::getPriority() const {
return 0;
}

UA_Logger COPC_UA_HandlerAbstract::getLogger() {
UA_Logger &COPC_UA_HandlerAbstract::getLogger() {
return UA_Forte_logger;
}

Expand All @@ -61,7 +69,7 @@ void COPC_UA_HandlerAbstract::UA_Log_Forte( //We omit SONAR only for the paramet
) {

char tmpStr[mMaxLogLength];
forte_snprintf(tmpStr, mMaxLogLength, "[OPC UA LOGGER] %s/%s\t", LogLevelNames[paLevel], LogCategoryNames[paCategory]);
forte_snprintf(tmpStr, mMaxLogLength, "[OPC UA LOGGER] %s/%s\t", LogLevelNames.at(paLevel), LogCategoryNames[paCategory]);
char *start = &tmpStr[strlen(tmpStr)];

forte_vsnprintf(start, mMaxLogLength, paMsg, paArgs);
Expand Down Expand Up @@ -91,7 +99,7 @@ void COPC_UA_HandlerAbstract::UA_Log_Forte( //We omit SONAR only for the paramet
}

void COPC_UA_HandlerAbstract::UA_Log_Forte_clear( //We omit SONAR only for the parameters
void* //NOSONAR
UA_Logger* //NOSONAR
) {
//do nothing
}
6 changes: 3 additions & 3 deletions src/com/opc_ua/opcua_handler_abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ class COPC_UA_HandlerAbstract : public CExternalEventHandler {
* Get the logger used by the the OPC UA stack
* @return Logger used by the OPC UA stack
*/
static UA_Logger getLogger();
static UA_Logger &getLogger();

private:

static void UA_Log_Forte_clear(void *paLogContext);
static const UA_Logger UA_Forte_logger;
static UA_Logger UA_Forte_logger;

static void UA_Log_Forte(void*, UA_LogLevel paLevel, UA_LogCategory paCategory, const char *paMsg, va_list paArgs);
static void UA_Log_Forte_clear(UA_Logger *paLogger);

/**
* Maximum size of the logging buffer
Expand Down
36 changes: 25 additions & 11 deletions src/com/opc_ua/opcua_local_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ void COPC_UA_Local_Handler::run() {
mUaServer = UA_Server_new();
if(mUaServer) {
UA_ServerConfig *uaServerConfig = UA_Server_getConfig(mUaServer);
UA_ServerConfig_setMinimal(uaServerConfig, gOpcuaServerPort, nullptr);
/* The original logger is needed to avoid memory leak on shutdown.
* It is reassigned before the server shutdown so that freeing the memory
* works properly.
*/
UA_Logger uaLogger = *uaServerConfig->logging;
*uaServerConfig->logging = COPC_UA_HandlerAbstract::getLogger();

UA_ServerStrings serverStrings;
generateServerStrings(gOpcuaServerPort, serverStrings);
Expand Down Expand Up @@ -117,6 +122,8 @@ void COPC_UA_Local_Handler::run() {
} else {
DEVLOG_ERROR("[OPC UA LOCAL]: Couldn't initialize Nodesets\n", gOpcuaServerPort);
}
/* Reassign original logger to avoid memory leak. */
*uaServerConfig->logging = uaLogger;
UA_Server_delete(mUaServer);
mUaServer = nullptr;
}
Expand Down Expand Up @@ -144,33 +151,40 @@ void COPC_UA_Local_Handler::generateServerStrings(TForteUInt16 paUAServerPort, U
#endif //FORTE_COM_OPC_UA_MULTICAST

paServerStrings.mAppURI = "org.eclipse.4diac."s;
paServerStrings.mHostname = std::string("opc.tcp://");
#ifdef FORTE_COM_OPC_UA_CUSTOM_HOSTNAME
paServerStrings.mHostname = std::string(FORTE_COM_OPC_UA_CUSTOM_HOSTNAME);
paServerStrings.mHostname.append(FORTE_COM_OPC_UA_CUSTOM_HOSTNAME);
paServerStrings.mHostname.append("-"s);
paServerStrings.mHostname.append(helperBuffer);
paServerStrings.mAppURI.append(paServerStrings.mHostname);
#else
paServerStrings.mAppURI.append(helperBuffer);
#endif

paServerStrings.mHostname.append(":");
paServerStrings.mHostname.append(std::to_string(paUAServerPort));
}

void COPC_UA_Local_Handler::configureUAServer(UA_ServerStrings &paServerStrings, UA_ServerConfig &paUaServerConfig) const {

paUaServerConfig.logger = COPC_UA_HandlerAbstract::getLogger();

#ifdef FORTE_COM_OPC_UA_MULTICAST
paUaServerConfig.applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
// hostname will be added by mdns library
UA_String_clear(&paUaServerConfig.mdnsConfig.mdnsServerName);
paUaServerConfig.mdnsConfig.mdnsServerName = UA_String_fromChars(paServerStrings.mMdnsServerName.c_str());
#endif //FORTE_COM_OPC_UA_MULTICAST

UA_String_clear(&paUaServerConfig.customHostname);
#ifdef FORTE_COM_OPC_UA_CUSTOM_HOSTNAME
UA_String customHost = UA_String_fromChars(paServerStrings.mHostname.c_str());
UA_String_copy(&customHost, &paUaServerConfig.customHostname);
#endif
UA_Array_delete(paUaServerConfig.serverUrls, paUaServerConfig.serverUrlsSize, &UA_TYPES[UA_TYPES_STRING]);
paUaServerConfig.serverUrls = nullptr;
paUaServerConfig.serverUrlsSize = 0;

UA_String serverUrls[1];
size_t serverUrlsSize = 0;
serverUrls[serverUrlsSize] = UA_STRING(&paServerStrings.mHostname[0]);
serverUrlsSize++;
UA_StatusCode retVal = UA_Array_copy(serverUrls, serverUrlsSize, (void**)&paUaServerConfig.serverUrls, &UA_TYPES[UA_TYPES_STRING]);
if(retVal != UA_STATUSCODE_GOOD) {
return;
}
paUaServerConfig.serverUrlsSize = serverUrlsSize;

// delete pre-initialized values
UA_LocalizedText_clear(&paUaServerConfig.applicationDescription.applicationName);
Expand Down
1 change: 1 addition & 0 deletions src/com/opc_ua/opcua_local_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class COPC_UA_Local_Handler : public COPC_UA_HandlerAbstract, public CThread {
* @param paServerStrings Place to store the generated strings
*/
void generateServerStrings(TForteUInt16 paUAServerPort, UA_ServerStrings &paServerStrings) const;

/**
* Creates the configuration for the OPC UA Server.
* @param paServerStrings Strings needed to configure the server
Expand Down

0 comments on commit f11a174

Please sign in to comment.