testexecmgmt/ucc/Source/Uccs.v2/Core/UCCS_Main.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 * Filename: UCCS_Main.cpp
       
    16 * System Includes
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 #include <stdio.h>
       
    23 #include <string.h>
       
    24 #include <assert.h>
       
    25 #include <windows.h>
       
    26 #include <direct.h>
       
    27 
       
    28 
       
    29 /********************************************************************************
       
    30  *
       
    31  * Local Includes
       
    32  *
       
    33  ********************************************************************************/
       
    34 #include "strncpynt.h"
       
    35 #include "UCCS_CBatchEngine.h"
       
    36 #include "HTMLOutput.h"
       
    37 #include "CRetrieveCommandFromFile.h"
       
    38 #include "CRetrieveCommandFromConsole.h"
       
    39 #include "../DeviceControlChannel/CUCCSCommandControl.h"
       
    40 #include "../DeviceControlChannel/CApplicationControlNull.h"
       
    41 #include "../DeviceControlChannel/CommandControlErrors.h"
       
    42 
       
    43 
       
    44 /********************************************************************************
       
    45  *
       
    46  * Definitions
       
    47  *
       
    48  ********************************************************************************/
       
    49 #define MAXCONTROLMODULEARGS 256
       
    50 #define MAXPATHLENGTH		 1024
       
    51 #define WAIT_TIME_RECEIVE_BYTES_ERROR 100
       
    52 
       
    53 
       
    54 /********************************************************************************
       
    55  *
       
    56  * Prototypes
       
    57  *
       
    58  ********************************************************************************/
       
    59 int ParseParameters( int argc, char *argv[] );
       
    60 int ProcessParameters();
       
    61 void PrintUsage();
       
    62 
       
    63 
       
    64 /********************************************************************************
       
    65  *
       
    66  * Types
       
    67  *
       
    68  ********************************************************************************/
       
    69 typedef enum {
       
    70 	OUTPUT_MODULE_HTML
       
    71 } TOutputModule;
       
    72 
       
    73 typedef enum {
       
    74 	INPUT_MODULE_FILE,
       
    75 } TInputModule;
       
    76 
       
    77 typedef enum {
       
    78 	CONTROL_MODULE_SERIAL,
       
    79 	CONTROL_MODULE_CONSOLE,
       
    80 	CONTROL_MODULE_TCP,
       
    81 	CONTROL_MODULE_AUTO
       
    82 } TControlModule;
       
    83 
       
    84 typedef struct {
       
    85 	TRemoteControlTransport iRemoteControlTransport;
       
    86 	TOutputModule	iOutputModule;
       
    87 	TInputModule	iInputModule;
       
    88 	TControlModule	iControlModule;
       
    89 	char			iControlModuleArgs[MAXCONTROLMODULEARGS];
       
    90 	int				iUseAlternateScriptDirectory;
       
    91 	char			iAlternateScriptDirectory[MAXPATHLENGTH];
       
    92 } TControllerArgs;
       
    93 
       
    94 typedef enum {
       
    95 	OPT_HELP,
       
    96 
       
    97 	// Control moduless
       
    98 	OPT_SERIALCONTROL,
       
    99 	OPT_CONSOLECONTROL,
       
   100 	OPT_TCPCONTROL,
       
   101 	OPT_AUTOCONTROL,
       
   102 
       
   103 	// Input modules
       
   104 	OPT_FILEINPUT,
       
   105 
       
   106 	// Output modules
       
   107 	OPT_HTMLOUTPUT,
       
   108 
       
   109 	// Options
       
   110 	OPT_SCRIPTDIR
       
   111 } TCommandOptions;
       
   112 
       
   113 
       
   114 /********************************************************************************
       
   115  *
       
   116  * File-scope variables
       
   117  *
       
   118  ********************************************************************************/
       
   119 static TControllerArgs gControllerArgs;
       
   120 static char *gOptionStrings[] = {	"--help", 
       
   121 									"--serialcontrol", 
       
   122 									"--consolecontrol", 
       
   123 									"--tcpcontrol",
       
   124 									"--autocontrol", 
       
   125 									"--fileinput",  
       
   126 									"--htmloutput", 
       
   127 									"--scriptdir", 
       
   128 									NULL };
       
   129 
       
   130 // Module Classes
       
   131 static CRetrieveCommandFromFile gRetrieveCommandFromFile;
       
   132 static CHTMLOutput gHtmlOutput;
       
   133 static CUCCSCommandControl *gApplicationControlPC;
       
   134 static CApplicationControlNull *gApplicationControlNull;
       
   135 static CBatchEngine *gBatchEngine;
       
   136 
       
   137 // Module Interfaces
       
   138 static IOutput *gOutput;
       
   139 static IRetrieveCommand *gInput;
       
   140 static IRemoteControl *gRemoteControl;
       
   141 
       
   142 
       
   143 /********************************************************************************
       
   144  *
       
   145  * Main() program entry point
       
   146  *
       
   147  ********************************************************************************/
       
   148 int main(int argc, char* argv[])
       
   149 {
       
   150 	int err, errcode, rv = 0, script_error;
       
   151 	TCommandControlError channel_execution_error;
       
   152 
       
   153 	// parse the parameters
       
   154 	err = ParseParameters( argc, argv );
       
   155 	if( err != 0 ) {
       
   156 		PrintUsage();
       
   157 		return -1;
       
   158 	}
       
   159 
       
   160 	// process the parameters 
       
   161 	err = ProcessParameters();
       
   162 	if( err != 0 ) {
       
   163 		return -1;
       
   164 	}
       
   165 
       
   166 	// print a welcome banner
       
   167 	fprintf( stdout, "------------------------------------------------------------------\n" );
       
   168 	fprintf( stdout, "\n" );
       
   169 	fprintf( stdout, " Network Emulator - Use Case Controller\n" );
       
   170 	fprintf( stdout, "\n" );
       
   171 	fprintf( stdout, " eg. runcmd svcname=\"TestService\"  methodid=\"1\"\n");
       
   172 	fprintf( stdout, " eg. runcmd svcname=\"TestService\"  methodid=\"10\" call=\"TestCallOne\" param1=\"one\" param2=\"two\"\n" );
       
   173 	fprintf( stdout, " eg. runcmd svcname=\"SyncService\"  methodid=\"1\"\n");
       
   174 	fprintf( stdout, " eg. runcmd svcname=\"SyncService\"  methodid=\"10\" call=\"StartSyncTestCase\"\n" );
       
   175 	fprintf( stdout, " eg. runcmd svcname=\"ExeService\"   methodid=\"1\"\n");
       
   176 	fprintf( stdout, " eg. runcmd svcname=\"ExeService\"   methodid=\"2\"\n");
       
   177 	fprintf( stdout, " eg. runcmd svcname=\"ExeService\"   methodid=\"10\" call=\"Start\"       cmd=\"cmd\" args=\"\"\n" );
       
   178 	fprintf( stdout, " eg. runcmd svcname=\"ExeService\"   methodid=\"10\" call=\"Start\"       cmd=\"cmd\" args=\"\" isDefault=\"true\"\n" );
       
   179 	fprintf( stdout, " eg. runcmd svcname=\"ExeService\"   methodid=\"10\" call=\"Stop\"        handle=\"0\"\n" );
       
   180 	fprintf( stdout, " eg. runcmd svcname=\"IperfService\" methodid=\"10\" call=\"StartServer\" isDefault=\"true\"\n" );
       
   181 	fprintf( stdout, " eg. runcmd svcname=\"PcapService\"  methodid=\"10\" call=\"StartCapture\" isDefault=\"true\" outputFileName=\"test10\"\n" );
       
   182 	fprintf( stdout, " eg. runcmd svcname=\"PcapService\"  methodid=\"10\" call=\"StopCapture\"  isDefault=\"true\"\n" );
       
   183 	
       
   184 	fprintf( stdout, "------------------------------------------------------------------\n\n" );
       
   185 
       
   186 	// start the remote control module listening for commands
       
   187 	while( 1 ) {
       
   188 		channel_execution_error = gRemoteControl->Start( gControllerArgs.iRemoteControlTransport, gControllerArgs.iControlModuleArgs, &errcode, &script_error );
       
   189 		if( channel_execution_error == EAC_RECEIVEBYTESERROR ) {
       
   190 			Sleep( WAIT_TIME_RECEIVE_BYTES_ERROR );
       
   191 		}
       
   192 
       
   193 		if( (script_error != 0) && (gRemoteControl == gApplicationControlNull) ) {
       
   194 			fprintf( stderr, "WARNING: Script error occured, last error was '%s' (%d)\n", GetUccsErrorString((TUCCSErrors)script_error), script_error );
       
   195 		}
       
   196 	}
       
   197 
       
   198 	// if err is not quit then print an error
       
   199 	if( channel_execution_error != EAC_QUIT ) {
       
   200 		fprintf( stdout, "WARNING: Controller completed with errors: '%s' (%d, %d)\n", GetCommandControlErrorString(channel_execution_error), channel_execution_error, errcode );
       
   201 		rv = -2;
       
   202 	}
       
   203 
       
   204 
       
   205 	// now cleanup all the objects (just delete)
       
   206 	gOutput = NULL;
       
   207 	gInput = NULL;
       
   208 	gRemoteControl = NULL;
       
   209 	delete gBatchEngine;
       
   210 	gBatchEngine = NULL;
       
   211 	if( gApplicationControlPC != NULL ) {
       
   212 		delete gApplicationControlPC;
       
   213 		gApplicationControlPC = NULL;
       
   214 	}
       
   215 	if( gApplicationControlNull != NULL ) {
       
   216 		delete gApplicationControlNull;
       
   217 		gApplicationControlNull = NULL;
       
   218 	}
       
   219 
       
   220 	// done
       
   221 	return rv;
       
   222 }
       
   223 
       
   224 
       
   225 /********************************************************************************
       
   226  *
       
   227  * Parse parameters
       
   228  *
       
   229  ********************************************************************************/
       
   230 int ParseParameters( int argc, char *argv[] )
       
   231 {
       
   232 	int match, i, j;
       
   233 
       
   234 	// set the arguments to default values
       
   235 	gControllerArgs.iControlModule = CONTROL_MODULE_CONSOLE;
       
   236 	gControllerArgs.iControlModuleArgs[0] = 0;
       
   237 	gControllerArgs.iInputModule = INPUT_MODULE_FILE;
       
   238 	gControllerArgs.iOutputModule = OUTPUT_MODULE_HTML;
       
   239 	gControllerArgs.iUseAlternateScriptDirectory = 0;
       
   240 	gControllerArgs.iAlternateScriptDirectory[0] = 0;
       
   241 	gControllerArgs.iRemoteControlTransport = RCI_CONSOLE;
       
   242 
       
   243 	// Parse the parameters to get the configuration information
       
   244 	for( i = 1; i < argc; i++ ) {
       
   245 
       
   246 		// search for a matching option string
       
   247 		for( j = 0; gOptionStrings[j] != NULL; j++ ) {
       
   248 			match = strcmp( argv[i], gOptionStrings[j] );
       
   249 			if( match == 0 )
       
   250 				break;
       
   251 		}
       
   252 
       
   253 		// if no option found then continue with the next token
       
   254 		if( match != 0 ) {
       
   255 			fprintf( stderr, "WARNING: unknown option '%s' (ignoring).\n", argv[i] );
       
   256 			continue;
       
   257 		}
       
   258 
       
   259 		// otherwise parse each option
       
   260 		switch( j ) {
       
   261 
       
   262 			// just print the usage
       
   263 		case OPT_HELP:
       
   264 			return -1;
       
   265 
       
   266 			// use an alternate script directory
       
   267 		case OPT_SCRIPTDIR:
       
   268 			if( i == (argc-1) ) {
       
   269 				fprintf( stderr, "ERROR: --scriptdir requires a directory name to be specified.\n" );
       
   270 				return -1;
       
   271 			}
       
   272 			gControllerArgs.iUseAlternateScriptDirectory = 1;
       
   273 			STRNCPY_NULL_TERMINATE( gControllerArgs.iAlternateScriptDirectory, argv[++i], MAXPATHLENGTH );
       
   274 			break;
       
   275 
       
   276 			// use the serial control
       
   277 		case OPT_SERIALCONTROL:
       
   278 			if( i == (argc-1) ) {
       
   279 				fprintf( stderr, "ERROR: --serialcontrol requires a portname to be specified (e.g. --serialcontrol COM2).\n" );
       
   280 				return -1;
       
   281 			}
       
   282 			gControllerArgs.iRemoteControlTransport = RCI_SERIAL;
       
   283 			gControllerArgs.iControlModule = CONTROL_MODULE_SERIAL;
       
   284 			STRNCPY_NULL_TERMINATE( gControllerArgs.iControlModuleArgs, argv[++i], MAXCONTROLMODULEARGS );
       
   285 			break;
       
   286 
       
   287 			// use the control control
       
   288 		case OPT_CONSOLECONTROL:
       
   289 			gControllerArgs.iRemoteControlTransport = RCI_CONSOLE;
       
   290 			gControllerArgs.iControlModule = CONTROL_MODULE_CONSOLE;
       
   291 			break;
       
   292 
       
   293 			// use the wintunnel control
       
   294 		case OPT_TCPCONTROL:
       
   295 			if( i == (argc-1) ) {
       
   296 				fprintf( stderr, "ERROR: --tcpcontrol requires a portname to be specified (e.g. --tcpcontrol 110).\n" );
       
   297 				return -1;
       
   298 			}
       
   299 			gControllerArgs.iRemoteControlTransport = RCI_TCP;
       
   300 			gControllerArgs.iControlModule = CONTROL_MODULE_TCP;
       
   301 			STRNCPY_NULL_TERMINATE( gControllerArgs.iControlModuleArgs, argv[++i], MAXCONTROLMODULEARGS );
       
   302 			break;
       
   303 
       
   304 			// use the auto control
       
   305 		case OPT_AUTOCONTROL:
       
   306 			if( i == (argc-1) ) {
       
   307 				fprintf( stderr, "ERROR: --autocontrol requires a config string (i.e. <sid>[:<rc>[:<int>]]).\n" );
       
   308 				return -1;
       
   309 			}
       
   310 			gControllerArgs.iRemoteControlTransport = RCI_NONE;
       
   311 			gControllerArgs.iControlModule = CONTROL_MODULE_AUTO;
       
   312 			STRNCPY_NULL_TERMINATE( gControllerArgs.iControlModuleArgs, argv[++i], MAXCONTROLMODULEARGS );
       
   313 			break;
       
   314 
       
   315 			// input from a script file
       
   316 		case OPT_FILEINPUT:
       
   317 			gControllerArgs.iInputModule = INPUT_MODULE_FILE;
       
   318 			break;
       
   319 
       
   320 			// output to html
       
   321 		case OPT_HTMLOUTPUT:
       
   322 			gControllerArgs.iOutputModule = OUTPUT_MODULE_HTML;
       
   323 			break;
       
   324 		}
       
   325 	}
       
   326 
       
   327 	// done - success
       
   328 	return 0;
       
   329 }
       
   330 
       
   331 
       
   332 /********************************************************************************
       
   333  *
       
   334  * Process parameters
       
   335  *
       
   336  ********************************************************************************/
       
   337 int ProcessParameters()
       
   338 {
       
   339 	int err;
       
   340 
       
   341 	// use alternate directory
       
   342 	if( gControllerArgs.iUseAlternateScriptDirectory != 0 ) {
       
   343 		err = _chdir( gControllerArgs.iAlternateScriptDirectory );
       
   344 		if( err != 0 ) {
       
   345 			fprintf( stderr, "ERROR: failed to set directory '%s' - %s.\n", gControllerArgs.iAlternateScriptDirectory, strerror(errno) );
       
   346 			return -1;
       
   347 		}
       
   348 	}
       
   349 
       
   350 	// set the output module pointer
       
   351 	gOutput = &gHtmlOutput;
       
   352 
       
   353 	// set the script command input module 
       
   354 	gInput = &gRetrieveCommandFromFile;
       
   355 
       
   356 	// create the batchengine
       
   357 	gBatchEngine = new CBatchEngine( gInput, gOutput );
       
   358 	assert( gBatchEngine != NULL );
       
   359 
       
   360 	// create the remote channel
       
   361 	if( gControllerArgs.iControlModule == CONTROL_MODULE_SERIAL ) {
       
   362 		gApplicationControlPC = new CUCCSCommandControl( gBatchEngine, gOutput );
       
   363 		assert( gApplicationControlPC != NULL );
       
   364 		gRemoteControl = gApplicationControlPC;
       
   365 	}
       
   366 	if( gControllerArgs.iControlModule == CONTROL_MODULE_CONSOLE ) {
       
   367 		gApplicationControlPC = new CUCCSCommandControl( gBatchEngine, gOutput );
       
   368 		assert( gApplicationControlPC != NULL );
       
   369 		gRemoteControl = gApplicationControlPC;
       
   370 	}
       
   371 	if( gControllerArgs.iControlModule == CONTROL_MODULE_TCP ) {
       
   372 		gApplicationControlPC = new CUCCSCommandControl( gBatchEngine, gOutput );
       
   373 		assert( gApplicationControlPC != NULL );
       
   374 		gRemoteControl = gApplicationControlPC;
       
   375 	}
       
   376 	if( gControllerArgs.iControlModule == CONTROL_MODULE_AUTO ) {
       
   377 		gApplicationControlNull = new CApplicationControlNull( gBatchEngine, gOutput );
       
   378 		assert( gApplicationControlNull != NULL );
       
   379 		gRemoteControl = gApplicationControlNull;
       
   380 	}
       
   381 
       
   382 	// done - success
       
   383 	return 0;
       
   384 }
       
   385 
       
   386 
       
   387 /********************************************************************************
       
   388  *
       
   389  * PrintUsage
       
   390  *
       
   391  ********************************************************************************/
       
   392 void PrintUsage()
       
   393 {
       
   394 	fprintf( stderr, "\n" );
       
   395 	fprintf( stderr, "usage: uccs [options]\n" );
       
   396 	fprintf( stderr, "\n" );
       
   397 
       
   398 	fprintf( stderr, "CONTROL COMMAND OPTIONS:\n" );
       
   399 	fprintf( stderr, "\t--serialcontrol <comport>\n" );
       
   400 	fprintf( stderr, "\t\tControl commands are sent over a serial cable.\n" );
       
   401 	fprintf( stderr, "\t--consolecontrol\n" );
       
   402 	fprintf( stderr, "\t\tControl commands are entered into the console.\n" );
       
   403 	fprintf( stderr, "\t--tcpcontrol <port number>\n" );
       
   404 	fprintf( stderr, "\t\tControl commands are sent over tcp/ip using WinTunnel.\n" );
       
   405 	fprintf( stderr, "\t--autocontrol <use-case-id>[:<rendezvous-count>[:<interactive_flag>]]\n" );
       
   406 	fprintf( stderr, "\t\tControl commands are automatically entered.\n" );
       
   407 	fprintf( stderr, "\n" );
       
   408 
       
   409 	fprintf( stderr, "GENERAL OPTIONS:\n" );
       
   410 	fprintf( stderr, "\t--scriptdir <pathname>\n" );
       
   411 	fprintf( stderr, "\t\tAlternate directory to look for script files and put output files.\n" );
       
   412 	fprintf( stderr, "\n" );
       
   413 }