ANSI C UA Server SDK  1.5.1.313
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
main_win.c
/*****************************************************************************
* *
* Copyright (c) 2006-2015 Unified Automation GmbH. All rights reserved. *
* *
* Software License Agreement ("SLA") Version 2.5 *
* *
* Unless explicitly acquired and licensed from Licensor under another *
* license, the contents of this file are subject to the Software License *
* Agreement ("SLA") Version 2.5, or subsequent versions as allowed by the *
* SLA, and You may not copy or use this file in either source code or *
* executable form, except in compliance with the terms and conditions of *
* the SLA. *
* *
* All software distributed under the SLA is provided strictly on an "AS *
* IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, *
* AND LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT *
* LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR *
* PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the SLA for specific *
* language governing rights and limitations under the SLA. *
* *
* The complete license agreement can be found here: *
* http://unifiedautomation.com/License/SLA/2.5/ *
* *
* Project: Unified Automation ANSI C based OPC UA Server SDK *
* *
*****************************************************************************/
/*============================================================================
* Includes
*===========================================================================*/
/* direct platform access */
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#if defined (_MSC_VER) && !defined (_WIN32_WCE)
#include <crtdbg.h>
#endif
#ifdef _WIN32_WCE
#include <winsock2.h>
#endif
/* windows getopt replacement */
#include "getopt.h"
#include <opcua_proxystub.h>
#include <opcua_trace.h>
#include "democlient.h"
/*============================================================================
* Includes End
*===========================================================================*/
/*============================================================================
* Global Variables
*===========================================================================*/
static UaBase_Settings g_settings;
/*============================================================================
* Global Variables End
*===========================================================================*/
int GetFQHostname( char *szHostname, int len )
{
struct hostent *pEnt = 0;
int ret = gethostname( szHostname, len );
if ( ret != 0 ) return ret;
pEnt = gethostbyname( szHostname );
if ( pEnt == 0 ) return -1;
UaBase_strlcpy( szHostname, pEnt->h_name, len );
szHostname[len - 1] = 0;
return 0;
}
void PrintUsage(const char *szAppName)
{
printf( "Usage: %s [-h] [-v] [-c <config file>] [-u <URL>] [-d]\n", szAppName );
printf( " -h: Shows this help\n" );
printf( " -v: Shows detailed version information\n" );
printf( " -c: Sets the configuration file to use (default=client_settings.ini)\n" );
printf( " -u: Sets the URL to use (default is to use the URLs from the configuration file)\n" );
printf( " -d: For each 'd' argument, the trace level is increased by one (e.g. '-ddd' for INFO)\n" );
}
int main( int argc, char *argv[] )
{
int ret = EXIT_SUCCESS;
int opt;
char szConfigurationFile[UABASE_PATH_MAX] = "client_settings.ini";
OpcUa_StatusCode uStatus;
OpcUa_UInt32 uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_ERROR;
char szHostname[256] = "";
char szUrl[256] = "";
#if defined (_MSC_VER) && !defined (_WIN32_WCE)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
/* _CrtSetBreakAlloc(2432); */
#endif
/* parse commandline arguments */
while ( ( opt = getopt( argc, argv, "hvc:u:d" ) ) != -1 )
{
switch ( opt )
{
case 'h':
PrintUsage( argv[0] );
exit( EXIT_SUCCESS );
break;
case 'v':
PrintVersionInfo();
exit( EXIT_SUCCESS );
break;
case 'c':
UaBase_strlcpy( szConfigurationFile, optarg, sizeof(szConfigurationFile) );
break;
case 'u':
UaBase_strlcpy( szUrl, optarg, sizeof(szUrl) );
break;
case 'd':
switch (uTraceLevel)
{
case OPCUA_TRACE_OUTPUT_LEVEL_ERROR: uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_WARNING; break;
case OPCUA_TRACE_OUTPUT_LEVEL_WARNING: uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_SYSTEM; break;
case OPCUA_TRACE_OUTPUT_LEVEL_SYSTEM: uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_INFO; break;
case OPCUA_TRACE_OUTPUT_LEVEL_INFO: uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_DEBUG; break;
case OPCUA_TRACE_OUTPUT_LEVEL_DEBUG: uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_CONTENT; break;
case OPCUA_TRACE_OUTPUT_LEVEL_CONTENT: uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_ALL; break;
default: break;
}
break;
default: /* '?' */
PrintUsage( argv[0] );
exit( EXIT_FAILURE );
}
}
/* Setup OPC UA */
ret = InitializeOpcUaStack(uTraceLevel);
if ( ret != 0 ) return ret;
uStatus = UaBase_Settings_Initialize(&g_settings, szConfigurationFile);
if (OpcUa_IsNotGood(uStatus))
{
printf( "Could not open configuration file '%s'\nPress Enter to close Window!\n", szConfigurationFile);
getchar();
CleanupOpcUaStack();
exit( EXIT_FAILURE );
}
GetFQHostname(szHostname, sizeof(szHostname));
#if OPCUA_SUPPORT_PKI && UABASE_USE_FILESYSTEM
SetupPKIInfrastructure(&g_settings, szHostname);
#endif /* OPCUA_SUPPORT_PKI && UABASE_USE_FILESYSTEM */
ret = ClientMain(&g_settings, szHostname, szUrl);
UaBase_Settings_Clear(&g_settings);
CleanupOpcUaStack();
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