#include <uaclient_config.h>
#include <stdlib.h>
#include <opcua_datetime.h>
#include <opcua_trace.h>
#include <opcua_string.h>
#include <uaclient_module.h>
#include <uaclient_session.h>
#include <uabase_utilities.h>
#define UACLIENT_APPLICATION_NAME "UaSdkC - Client - Lesson03"
#define UACLIENT_APPLICATION_URI "urn:UnifiedAutomation:UaSdkC:Client:Lesson03"
#define UACLIENT_PRODUCT_URI "urn:UnifiedAutomation:UaSdkC:Client:Lesson03"
#define SERVER_ENDPOINT_URL "opc.tcp://localhost:48020"
typedef enum _SampleStateMachine
{
State_Idle,
State_Connect,
State_Connected,
State_Browse,
State_BrowseDone,
State_Write,
State_WriteDone,
State_Disconnect,
State_Disconnected,
State_Error
} SampleStateMachine;
typedef struct _SampleClientContext
{
SampleStateMachine State;
} SampleClientContext;
OpcUa_ProxyStubConfiguration *a_pProxyStubConfiguration)
{
OpcUa_InitializeStatus(OpcUa_Module_Client, "InitializeOpcUaStack");
printf("UA Client: Initializing Stack...\n");
a_pProxyStubConfiguration->bProxyStub_Trace_Enabled = OpcUa_True;
a_pProxyStubConfiguration->uProxyStub_Trace_Level = OPCUA_TRACE_OUTPUT_LEVEL_WARNING;
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
OpcUa_FinishErrorHandling;
}
{
OpcUa_InitializeStatus(OpcUa_Module_Client, "CleanupOpcUaStack");
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
OpcUa_FinishErrorHandling;
}
{
SampleClientContext *pClientContext = a_pSession->
pUserData;
const char *pStatus = "INVALID";
switch (a_status)
{
pStatus = "Disconnected";
if (pClientContext->State == State_Connect)
{
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Client: failed to connect to server!\n");
}
if (pClientContext->State == State_Disconnect)
{
pClientContext->State = State_Disconnected;
}
else
{
pClientContext->State = State_Error;
}
break;
pStatus = "Connected";
if (pClientContext->State == State_Connect)
{
pClientContext->State = State_Connected;
}
else
{
pClientContext->State = State_Error;
}
break;
pStatus = "Connecting";
break;
pStatus = "ConnectionWarningWatchdogTimeout";
break;
pStatus = "ConnectionErrorClientReconnect";
break;
pStatus = "SessionAutomaticallyRecreated";
break;
default: break;
}
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "\n--> Sample_ConnectionStatusChanged_CB: %s\n\n", pStatus);
}
OpcUa_Boolean a_overridable)
{
SampleClientContext *pClientContext = a_pSession->
pUserData;
const char *pServiceType = "INVALID";
switch (a_serviceType)
{
default: break;
}
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "Sample_ConnectError_CB:\n");
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " ServiceType: %s\n", pServiceType);
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " Error: 0x%08x\n", a_error);
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " Overridable: %s\n", a_overridable == OpcUa_False ? "false" : "true");
pClientContext->State = State_Error;
return OpcUa_False;
}
OpcUa_Int32 a_NoOfResults,
OpcUa_Int32 a_NoOfDiagnosticInfos,
OpcUa_Void *a_pUserData)
{
SampleClientContext *pClientContext = a_pSession->
pUserData;
OpcUa_ReferenceParameter(a_pSession);
OpcUa_ReferenceParameter(a_NoOfDiagnosticInfos);
OpcUa_ReferenceParameter(a_pDiagnosticInfos);
OpcUa_ReferenceParameter(a_pUserData);
if (OpcUa_IsGood(uStatus) && a_NoOfResults == 1)
{
}
if (OpcUa_IsGood(uStatus))
{
OpcUa_Int32 i;
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "Sample_Browse_CB:\n");
for (i = 0; i < a_pResults[0].NoOfReferences; i++)
{
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " [%i]:\n", i);
switch (a_pResults[0].References[i].NodeId.NodeId.IdentifierType)
{
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " NodeId: ns=%hu;i=%u\n",
a_pResults[0].References[i].NodeId.NodeId.NamespaceIndex,
break;
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " NodeId: ns=%hu;s=%s\n",
a_pResults[0].References[i].NodeId.NodeId.NamespaceIndex,
OpcUa_String_GetRawString(&a_pResults[0].References[i].NodeId.NodeId.Identifier.String));
break;
default:
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " NodeId: Not supported\n");
break;
}
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " DisplayName: %s\n",
OpcUa_String_GetRawString(&a_pResults[0].References[i].DisplayName.Text));
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " BrowseName: %hu|%s\n",
a_pResults[0].References[i].BrowseName.NamespaceIndex,
OpcUa_String_GetRawString(&a_pResults[0].References[i].BrowseName.Name));
}
if (a_pResults[0].ContinuationPoint.Length > 0)
{
}
else
{
if (pClientContext->State == State_Browse)
pClientContext->State = State_BrowseDone;
else
pClientContext->State = State_Error;
}
}
else
{
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "Sample_Browse_CB failed (0x%08x)\n", uStatus);
pClientContext->State = State_Error;
}
}
{
if (a_pContinuationPoint == OpcUa_Null)
{
OpcUa_BrowseDescription_Initialize(&nodeToBrowse);
OpcUa_ViewDescription_Initialize(&viewDescription);
nodeToBrowse.
ResultMask = OpcUa_BrowseResultMask_All;
OpcUa_Null,
&viewDescription,
5,
1,
&nodeToBrowse,
Sample_Browse_CB,
a_pContinuationPoint);
}
else
{
OpcUa_Null,
OpcUa_False,
1,
a_pContinuationPoint,
Sample_Browse_CB,
a_pContinuationPoint);
}
}
OpcUa_Int32 a_NoOfResults,
OpcUa_Int32 a_NoOfDiagnosticInfos,
OpcUa_Void *a_pUserData)
{
SampleClientContext *pClientContext = a_pSession->
pUserData;
OpcUa_ReferenceParameter(a_NoOfDiagnosticInfos);
OpcUa_ReferenceParameter(a_pDiagnosticInfos);
OpcUa_ReferenceParameter(a_pUserData);
{
OpcUa_Int32 i;
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "Sample_Write_CB:\n");
for (i = 0; i < a_NoOfResults; i++)
{
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " [%i]:\n", i);
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, " Status: 0x%08x\n", a_pResults[i]);
}
if (pClientContext->State == State_Write)
pClientContext->State = State_WriteDone;
else
pClientContext->State = State_Error;
}
else
{
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING,
"Sample_Write_CB failed (0x%08x)\n", a_pResponseHeader->
ServiceResult);
pClientContext->State = State_Error;
}
}
{
OpcUa_Int32 numNodesToWrite = 1;
OpcUa_InitializeStatus(OpcUa_Module_Client, "Sample_Write");
OpcUa_WriteValue_Initialize(&nodesToWrite[0]);
OpcUa_String_StrnCpy(&nodesToWrite[0].NodeId.Identifier.String, OpcUa_String_FromCString("Demo.Static.Scalar.Int32"), OPCUA_STRING_LENDONTCARE);
OpcUa_Null,
numNodesToWrite,
nodesToWrite,
Sample_Write_CB,
OpcUa_Null);
OpcUa_GotoErrorIfBad(uStatus);
OpcUa_WriteValue_Clear(&nodesToWrite[0]);
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
OpcUa_WriteValue_Clear(&nodesToWrite[0]);
OpcUa_FinishErrorHandling;
}
{
OpcUa_Boolean bClientInitialized = OpcUa_False;
OpcUa_Boolean bComplete = OpcUa_False;
SampleClientContext clientContext;
OpcUa_InitializeStatus(OpcUa_Module_Client, "ClientMain");
OpcUa_DeclareErrorTraceModule(OpcUa_Module_Client);
clientContext.State = State_Idle;
OpcUa_GotoErrorIfBad(uStatus);
bClientInitialized = OpcUa_True;
pClientConfiguration->
PkiConfig.strPkiType = (
char*)OPCUA_PKI_TYPE_NONE;
OpcUa_MemSet(&sessionCallback, 0, sizeof(sessionCallback));
sessionCallback.pfConnectionStatusChanged_CB = Sample_ConnectionStatusChanged_CB;
sessionCallback.pfConnectError_CB = Sample_ConnectError_CB;
OpcUa_GotoErrorIfBad(uStatus);
OpcUa_GotoErrorIfBad(uStatus);
{
switch (clientContext.State)
{
case State_Idle:
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Client: Connecting to %s ...\n", SERVER_ENDPOINT_URL);
OpcUa_GotoErrorIfBad(uStatus);
clientContext.State = State_Connect;
break;
case State_Connected:
uStatus = Sample_Browse(pSession, OpcUa_Null);
OpcUa_GotoErrorIfBad(uStatus);
clientContext.State = State_Browse;
break;
case State_BrowseDone:
uStatus = Sample_Write(pSession);
OpcUa_GotoErrorIfBad(uStatus);
clientContext.State = State_Write;
break;
case State_WriteDone:
OpcUa_GotoErrorIfBad(uStatus);
clientContext.State = State_Disconnect;
break;
case State_Disconnected:
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "Sample successfully completed. Terminating now.\n");
bComplete = OpcUa_True;
break;
case State_Error:
if (OpcUa_IsBad(uStatus) && uStatus != OpcUa_BadInvalidState) {OpcUa_GotoError;}
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "An error occured. Terminating now.\n");
bComplete = OpcUa_True;
break;
default:
break;
}
if (OpcUa_IsBad(uStatus))
{
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UaBase_DoCom failed (0x%08x)\n", uStatus);
bComplete = OpcUa_True;
}
}
OpcUa_CertificateStoreConfiguration_Initialize(&pClientConfiguration->
PkiConfig);
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Client: Main stopped\n");
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
if (pSession)
if (bClientInitialized != OpcUa_False)
{
}
OpcUa_Trace(OPCUA_TRACE_LEVEL_WARNING, "UA Client: Main stopped due ERROR! (0x%08x)\n", uStatus);
OpcUa_FinishErrorHandling;
}
int main(void)
{
int ret = EXIT_SUCCESS;
OpcUa_Handle hProxyStubPlatformLayer = OpcUa_Null;
OpcUa_ProxyStubConfiguration proxyStubConfiguration;
uStatus = InitializeOpcUaStack(&hProxyStubPlatformLayer, &proxyStubConfiguration);
if ( OpcUa_IsNotGood(uStatus) )
{
return EXIT_FAILURE;
}
uStatus = ClientMain();
if ( OpcUa_IsNotGood(uStatus) )
{
ret = EXIT_FAILURE;
}
uStatus = CleanupOpcUaStack(&hProxyStubPlatformLayer);
if ( OpcUa_IsNotGood(uStatus) )
{
ret = EXIT_FAILURE;
}
return ret;
}