#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>
#define UACLIENT_APPLICATION_NAME "UaSdkC - Client - Lesson01"
#define UACLIENT_APPLICATION_URI "urn:UnifiedAutomation:UaSdkC:Client:Lesson01"
#define UACLIENT_PRODUCT_URI "urn:UnifiedAutomation:UaSdkC:Client:Lesson01"
#define SERVER_ENDPOINT_URL "opc.tcp://localhost:48020"
typedef enum _SampleStateMachine
{
State_Idle,
State_Connect,
State_Connected,
State_Read,
State_ReadDone,
State_Disconnect,
State_Disconnected,
State_Error
} SampleStateMachine;
typedef struct _SampleClientContext
{
SampleStateMachine State;
} SampleClientContext;
OpcUa_StatusCode InitializeOpcUaStack(OpcUa_Handle *a_phProxyStubPlatformLayer,
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_ERROR;
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
OpcUa_FinishErrorHandling;
}
OpcUa_StatusCode CleanupOpcUaStack(OpcUa_Handle *a_phProxyStubPlatformLayer)
{
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)
{
printf("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;
}
printf("\n--> Sample_ConnectionStatusChanged_CB: %s\n\n", pStatus);
}
OpcUa_StatusCode a_error,
OpcUa_Boolean a_clientSideError)
{
SampleClientContext *pClientContext = a_pSession->
pUserData;
const char *pServiceType = "INVALID";
switch (a_serviceType)
{
default: break;
}
printf("Sample_ConnectError_CB:\n");
printf(" ServiceType: %s\n", pServiceType);
printf(" Error: 0x%08x\n", a_error);
printf(" ClientSideError: %s\n", a_clientSideError == OpcUa_False ? "false" : "true");
pClientContext->State = State_Error;
return OpcUa_False;
}
static void Variant_ToString(const OpcUa_Variant *pValue, char *text, int len)
{
if (pValue->ArrayType == OpcUa_VariantArrayType_Scalar)
{
switch (pValue->Datatype)
{
case OpcUaType_Null:
OpcUa_SnPrintfA(text, len, "Null");
break;
case OpcUaType_Int32:
OpcUa_SnPrintfA(text, len, "%i", pValue->Value.Int32);
break;
case OpcUaType_UInt32:
OpcUa_SnPrintfA(text, len, "%u", pValue->Value.UInt32);
break;
case OpcUaType_Float:
OpcUa_SnPrintfA(text, len, "%f", pValue->Value.Float);
break;
case OpcUaType_Double:
OpcUa_SnPrintfA(text, len, "%f", pValue->Value.Double);
break;
case OpcUaType_DateTime:
OpcUa_DateTime_GetStringFromDateTime(pValue->Value.DateTime, text, len);
break;
default:
OpcUa_SnPrintfA(text, len, "Unsupported type");
break;
}
}
else
{
OpcUa_SnPrintfA(text, len, "Unsupported type");
}
}
OpcUa_Int32 a_NoOfResults,
OpcUa_DataValue *a_pResults,
OpcUa_Int32 a_NoOfDiagnosticInfos,
OpcUa_DiagnosticInfo *a_pDiagnosticInfos,
OpcUa_Void *a_pUserData)
{
SampleClientContext *pClientContext = a_pSession->
pUserData;
OpcUa_ReferenceParameter(a_NoOfDiagnosticInfos);
OpcUa_ReferenceParameter(a_pDiagnosticInfos);
OpcUa_ReferenceParameter(a_pUserData);
{
OpcUa_Int32 i;
printf("Sample_Read_CB:\n");
for (i = 0; i < a_NoOfResults; i++)
{
char szSourceTimestamp[64] = {0};
char szServerTimestamp[64] = {0};
char szValue[64] = {0};
OpcUa_DateTime_GetStringFromDateTime(a_pResults[i].SourceTimestamp, szSourceTimestamp, sizeof(szSourceTimestamp));
OpcUa_DateTime_GetStringFromDateTime(a_pResults[i].ServerTimestamp, szServerTimestamp, sizeof(szServerTimestamp));
Variant_ToString(&a_pResults[i].Value, szValue, sizeof(szValue));
printf(" [%i]:\n", i);
printf(" Status: 0x%08x\n", a_pResults[i].StatusCode);
printf(" SourceTimestamp: %s\n", szSourceTimestamp);
printf(" ServerTimestamp: %s\n", szServerTimestamp);
printf(" Value: %s\n", szValue);
}
if (pClientContext->State == State_Read)
pClientContext->State = State_ReadDone;
else
pClientContext->State = State_Error;
}
else
{
printf(
"Sample_Read_CB failed (0x%08x)\n", a_pResponseHeader->
ServiceResult);
pClientContext->State = State_Error;
}
}
{
OpcUa_Int32 numNodesToRead = 2;
OpcUa_ReadValueId_Initialize(&nodesToRead[0]);
nodesToRead[0].
NodeId.NamespaceIndex = 0;
nodesToRead[0].
NodeId.IdentifierType = OpcUa_IdentifierType_Numeric;
nodesToRead[0].
NodeId.Identifier.Numeric = OpcUaId_Server_ServerStatus_State;
OpcUa_ReadValueId_Initialize(&nodesToRead[1]);
nodesToRead[1].
NodeId.NamespaceIndex = 0;
nodesToRead[1].
NodeId.IdentifierType = OpcUa_IdentifierType_Numeric;
nodesToRead[1].
NodeId.Identifier.Numeric = OpcUaId_Server_ServerStatus_CurrentTime;
OpcUa_Null,
0,
numNodesToRead,
nodesToRead,
Sample_Read_CB,
OpcUa_Null);
}
OpcUa_StatusCode ClientMain()
{
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);
while (!bComplete)
{
switch (clientContext.State)
{
case State_Idle:
printf("UA Client: Connecting to %s ...\n", SERVER_ENDPOINT_URL);
OpcUa_GotoErrorIfBad(uStatus);
clientContext.State = State_Connect;
break;
case State_Connected:
uStatus = Sample_Read(pSession);
OpcUa_GotoErrorIfBad(uStatus);
clientContext.State = State_Read;
break;
case State_ReadDone:
OpcUa_GotoErrorIfBad(uStatus);
clientContext.State = State_Disconnect;
break;
case State_Disconnected:
printf("Sample successfully completed. Terminating now.\n");
bComplete = OpcUa_True;
break;
case State_Error:
printf("An error occured. Terminating now.\n");
bComplete = OpcUa_True;
break;
default:
break;
}
if (OpcUa_IsBad(uStatus))
{
printf("UaClient_DoCom failed (0x%08x)\n", uStatus);
bComplete = OpcUa_True;
}
}
OpcUa_CertificateStoreConfiguration_Initialize(&pClientConfiguration->
PkiConfig);
printf("UA Client: Main stopped.\nPress Enter to close!\n");
getchar();
OpcUa_ReturnStatusCode;
OpcUa_BeginErrorHandling;
if (bClientInitialized != OpcUa_False)
{
}
printf("UA Client: Main stopped due ERROR! (0x%08x)\nPress Enter to close!\n", uStatus);
getchar();
OpcUa_FinishErrorHandling;
}
int main()
{
int ret = EXIT_SUCCESS;
OpcUa_StatusCode uStatus = OpcUa_Good;
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;
}
#ifdef _WIN32_WCE
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nShowCmd)
{
OpcUa_ReferenceParameter(hInstance);
OpcUa_ReferenceParameter(hPrevInstance);
OpcUa_ReferenceParameter(lpCmdLine);
OpcUa_ReferenceParameter(nShowCmd);
return main(0, OpcUa_Null);
}
#endif