UA ANSI C Server Professional  1.4.2.297
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
main_win.c
/******************************************************************************
**
** Copyright (C) 2005-2014 Unified Automation GmbH. All Rights Reserved.
** Web: http://www.unifiedautomation.com
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** Project: OPC Ansi C OPC Server
**
** Description: Server main file with server loop.
**
******************************************************************************/
/*============================================================================
* 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>
#else
/* windows getopt replacement */
#include "getopt.h"
#endif
#include <opcua_proxystub.h>
#include <opcua_trace.h>
#include "demoserver.h"
/*============================================================================
* Includes End
*===========================================================================*/
/*============================================================================
* Global Variables
*===========================================================================*/
static UaServer_Settings g_settings;
static char g_szHostname[256] = "";
/*============================================================================
* 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;
strlcpy( szHostname, pEnt->h_name, len );
szHostname[len - 1] = 0;
return 0;
}
void PrintUsage(const char *szAppName)
{
printf( "Usage: %s [-h] [-v] [-n <hostname>] [-c <config file>] [-d]\n", szAppName );
printf( " -h: Shows this help\n" );
printf( " -v: Shows detailed version information\n" );
printf( " -n: Sets the hostname to bind on\n" );
printf( " -c: Sets the configuration file to use (default=settings.ini)\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;
char szConfigurationFile[UASERVER_PATH_MAX] = "settings.ini";
OpcUa_StatusCode uStatus;
OpcUa_UInt32 uTraceLevel = OPCUA_TRACE_OUTPUT_LEVEL_ERROR;
#if defined(_WIN32_WCE)
/* WindowsCE doesn't have multi byte string OS functions, so we need to use wchar_t here */
wchar_t wszConfigurationFile[UASERVER_PATH_MAX] = {0};
DWORD retTmp = GetModuleFileName(NULL, wszConfigurationFile, sizeof(wszConfigurationFile) / sizeof(wchar_t));
if (retTmp > 0)
{
wchar_t *szTmp = wcsrchr(wszConfigurationFile, '\\');
if (szTmp)
{
*szTmp = L'\0';
wcsncat(wszConfigurationFile, L"\\settings.ini", UASERVER_PATH_MAX - wcslen(wszConfigurationFile) - 1);
wcstombs(szConfigurationFile, wszConfigurationFile, UASERVER_PATH_MAX - 1);
szConfigurationFile[UASERVER_PATH_MAX - 1] = '\0';
}
else
{
printf("Could not find '\\' in module file path!\n");
}
}
else
{
printf("GetModuleFileName failed!\n");
}
#else
int opt;
# if defined (_MSC_VER)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
/* _CrtSetBreakAlloc(2432); */
# endif
/* parse commandline arguments */
while ( ( opt = getopt( argc, argv, "hvn:c:d" ) ) != -1 )
{
switch ( opt )
{
case 'h':
PrintUsage( argv[0] );
exit( EXIT_SUCCESS );
break;
case 'v':
PrintVersionInfo();
exit( EXIT_SUCCESS );
break;
case 'n':
strlcpy( g_szHostname, optarg, sizeof(g_szHostname) );
break;
case 'c':
strlcpy( szConfigurationFile, optarg, sizeof(szConfigurationFile) );
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 );
}
}
#endif
/* Setup OPC UA */
ret = InitializeOpcUaStack(uTraceLevel);
if ( ret != 0 ) return ret;
/* check if hostname was given */
if ( g_szHostname[0] == 0 )
{
/* no hostname was given so read out the fully qualified hostname. */
GetFQHostname( g_szHostname, sizeof( g_szHostname ) );
}
uStatus = UaServer_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 );
}
#if OPCUA_SUPPORT_PKI
SetupPKIInfrastructure(&g_settings, g_szHostname);
#endif /* OPCUA_SUPPORT_PKI */
ret = ServerMain(&g_settings, g_szHostname);
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