UA Ansi C Server Professional  1.3.2.233
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SDK Tutorial - How to integrate the OPC UA Server SDK into your application

There are two main development tasks to implement an OPC UA Server using the SDK.

One is to integrate the SDK and the UA Server into a new or an existing application. The second task is to integrate the data sources into the SDK. This tutorial gives an introduction on how to integrate the ANSI C UA SDK into a new application or an existing application.
The provided sample code implements the start up and the shut down code in the main function of the console application. For integration in existing applications the start up code for the SDK can be integrated into the start up sequence of the application and the shutdown code of the SDK into the shut down sequence of the application.

Please read the Introduction section before working through this tutorial.

Contents:

Global variables needed for the integration

There are two variables needed during the whole runtime of the OPC UA Server. The one variable is the handle of the UA Stack platform layer. The other one is the UaServer object containing the configuration and management data for the OPC UA Server instance. The variables can be global variables or part of an application management structure available during the runtime of the Server.

// The UA Stack platform layer instance
OpcUa_Handle g_hProxyStubPlatformLayer = OpcUa_Null;
// The UA Server instance
UaServer g_UaServer;

Start up code for UA Stack and UA Server

The first step in the start up code is the initialization of the UA Stack. This requires filling the ProxyStubConfiguration structure with the configuration setting for the UA Stack. The helper function OpcUa_ProxyStubConfiguration_InitializeDefault is setting default values configured for the stack. The trace and serializer settings can be changed. In a second step the stack is initialized and the platform layer is assigned to the stack.

See Also
OpcUa_ProxyStubConfiguration_InitializeDefault for a description of the structure
OpcUa_StatusCode uStatus = OpcUa_Good;
OpcUa_ProxyStubConfiguration proxyStubConfiguration;
// Initialize ProxyStubConfiguration with default values
// Default values can be changed here
proxyStubConfiguration.bProxyStub_Trace_Enabled = OpcUa_True;
// Initialize UA Stack and assign platform layer to UA Stack
uStatus = UaServer_Module_InitializeUaStack(g_hProxyStubPlatformLayer, &proxyStubConfiguration);
OpcUa_GotoErrorIfBad(uStatus);

The second step is to initialize the UaServer object. With the first call UaServer_Initialize the structure and contained structures will be initialized. This includes the server instance specific settings like EndpointUrl, ApplicationUri and ApplicationName and product specific settings like the ProductUri.

See Also
UaServer_Initialize for a description of the parameters. With the following calls the providers are initialized.
// Initialize Server
&g_UaServer,
"opc.tcp://localhost:4841",
"MyNodeName::4841/UaCSDKSampleServer",
"http://www.unified-automation.com/UaCSDKSampleServer",
"ANSI C SDK UA Sample Server");
OpcUa_GotoErrorIfBad(uStatus);
// Initialize provider list and add mandatory server provider
uStatus = UaServer_ProviderList_Create(&g_UaServer);
OpcUa_ReturnErrorIfBad(uStatus);
// Add sample provider to provider list
memset(&provider, 0, sizeof(provider));
#ifdef UAPROVIDER_STATIC
provider.pfInit = UaProvider_Sample_Initialize; // static intialization function
#else
provider.sProviderLibrary = "sampleprovider"; // basename of shared library which contains the provider
#endif
UaServer_ProviderList_AddProvider(&g_UaServer, &provider);
// initialize the providers
uStatus = UaServer_Providers_Initialize(&g_UaServer);
OpcUa_ReturnErrorIfBad(uStatus);

The third step is to start the Ua Server. After this call, clients are able to connect to the server.

// StartUp Server
uStatus = UaServer_StartUp(&g_UaServer);
OpcUa_GotoErrorIfBad(uStatus);

Shut down code for UA Server and UA Stack

The shut down sequence clears the UaServer object first which also closes the Endpoints. Clients are not longer able to connect to the server after this call. In the second call the UA stack is cleaned up.

// Clean Up Server
UaServer_Clear(&g_UaServer);
// Clean Up UA Stack
UaServer_Module_ClearUaStack(g_hProxyStubPlatformLayer);