testexecmgmt/ucc/Source/Uccs.v2/DeviceControlChannel/CUCCSCommandControl.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 * System Includes
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <stdio.h>
       
    22 #include <stdlib.h>
       
    23 #include <windows.h>
       
    24 #include <assert.h>
       
    25 
       
    26 /**********************************************************************************************
       
    27  *
       
    28  * Local Includes
       
    29  *
       
    30  *********************************************************************************************/
       
    31 #include "CUCCSCommandControl.h"
       
    32 #include "../Core/UCCS_ErrorCodes.h"
       
    33 
       
    34 /**********************************************************************************************
       
    35  *
       
    36  * Defines
       
    37  *
       
    38  *********************************************************************************************/
       
    39 
       
    40 /**********************************************************************************************
       
    41  *
       
    42  * Constructor
       
    43  *
       
    44  *********************************************************************************************/
       
    45 CUCCSCommandControl::CUCCSCommandControl( MUccsControl *aCallBackService, IOutput *aOutput )
       
    46 {
       
    47 	assert( aCallBackService != NULL );
       
    48 	assert( aOutput != NULL );
       
    49 
       
    50 	iOutput = aOutput;
       
    51 	iCallBackService = aCallBackService;
       
    52 	iStopFlag = 0;
       
    53 }
       
    54 
       
    55 /**********************************************************************************************
       
    56  *
       
    57  * Destructor
       
    58  *
       
    59  *********************************************************************************************/
       
    60 CUCCSCommandControl::~CUCCSCommandControl()
       
    61 {
       
    62 }
       
    63 
       
    64 /**********************************************************************************************
       
    65  *
       
    66  * Start() -- the main routine called to run the serial listener.
       
    67  *
       
    68  *********************************************************************************************/
       
    69 TCommandControlError CUCCSCommandControl::Start( TRemoteControlTransport aTransport, char *aPortname, int *aErrorCode, int *aScriptError )
       
    70 {
       
    71 	int dataLength, script_result;
       
    72 	TCommandControlError return_value = EAC_SUCCESS;
       
    73 	int engine_result = 0;
       
    74 	char commandBuffer[KMAXDATALENGTH];
       
    75 	char *cmdbptr;
       
    76 	TPCommand commandID;
       
    77 	TCPError protErr;
       
    78 
       
    79 	// Pointers to all structures to receive data for an ID.
       
    80 	TStartUsecaseRequest *startusecase_data;
       
    81 	TSignalRequest *signal_data;
       
    82 	TRendezvousRequest *rendezvous_data;
       
    83 	TWaitRequest *wait_data;
       
    84 	TEndUsecaseRequest *endusecase_data;
       
    85 	TGetVariableNameRequest *variablename_data;
       
    86 	TRunCommandRequest *runcommand_data;
       
    87 
       
    88 	// Pointers to all structures used in a reply to a request
       
    89 	TStartUsecaseReply startusecase_rep;
       
    90 	TSignalReply signal_rep;
       
    91 	TRendezvousReply rendezvous_rep;
       
    92 	TWaitReply wait_rep;
       
    93 	TEndUsecaseReply endusecase_rep;
       
    94 	TGetVariableNameReply variablename_rep;
       
    95 	TRunCommandReply runcommand_rep;
       
    96 	cmdbptr = &(commandBuffer[0]);
       
    97 
       
    98 	//Check params
       
    99 	assert ( aPortname != NULL );
       
   100 	assert ( aErrorCode!= NULL );
       
   101 	assert ( aScriptError != NULL );
       
   102 	*aScriptError = 0;
       
   103 	iTransport = aTransport;
       
   104 
       
   105 	// Initialise the transport
       
   106 	protErr = iProtocol.initialise( aTransport, aPortname, iOutput );
       
   107 	if (protErr != TCP_SUCCESS)
       
   108 	{
       
   109 		*aErrorCode = protErr;
       
   110 		return EAC_ERRINITTRANSPORT;
       
   111 	}
       
   112 
       
   113 	// receive bytes -- forever really
       
   114 	while( 1 ) {
       
   115 
       
   116 		// check the end flag
       
   117 		if( iStopFlag != 0 ) {
       
   118 			break;
       
   119 		}
       
   120 
       
   121 		// receive the command id and data via iProtocol
       
   122 		dataLength = KMAXDATALENGTH;
       
   123 		protErr = iProtocol.receiveMessage (&commandID, &dataLength, cmdbptr);
       
   124 
       
   125 		// if there is an error in the protocol we cannot recover. 
       
   126 		if (protErr != TCP_SUCCESS)
       
   127 		{
       
   128 			return_value =  EAC_RECEIVEBYTESERROR;
       
   129 			*aErrorCode = protErr;
       
   130 			break;
       
   131 		}
       
   132 
       
   133 		// msg for serial clients
       
   134 		LocalUpdateMessageIn(commandID, cmdbptr);
       
   135 
       
   136 		// now handle the command
       
   137 		switch( commandID ) {
       
   138 
       
   139 			case CMD_REQ_STARTUSECASEID:
       
   140 				startusecase_data = (TStartUsecaseRequest*)cmdbptr;
       
   141 				engine_result = iCallBackService->StartUsecase( startusecase_data->iUsecaseID );
       
   142 				startusecase_rep.iResult = engine_result;
       
   143 				protErr = iProtocol.sendReply(CMD_REP_STARTUSECASEID, sizeof(startusecase_rep), &startusecase_rep);
       
   144 				break;
       
   145 
       
   146 			case CMD_REQ_SIGNALID:
       
   147 				signal_data = (TSignalRequest*)cmdbptr;
       
   148 				engine_result = iCallBackService->Signal( signal_data->iUsecaseID );
       
   149 				signal_rep.iResult = engine_result;
       
   150 				protErr = iProtocol.sendReply(CMD_REP_SIGNALID, sizeof(signal_rep), &signal_rep);
       
   151 				break;
       
   152 			
       
   153 			case CMD_REQ_RENDEZVOUSID:
       
   154 				rendezvous_data = (TRendezvousRequest*)cmdbptr;
       
   155 				engine_result = iCallBackService->Rendezvous( rendezvous_data->iUsecaseID );
       
   156 				rendezvous_rep.iResult = engine_result;
       
   157 				protErr = iProtocol.sendReply(CMD_REP_RENDEZVOUSID, sizeof(rendezvous_rep), &rendezvous_rep);
       
   158 				break;
       
   159 				
       
   160 			case CMD_REQ_WAITID:
       
   161 				wait_data = (TWaitRequest*)cmdbptr;
       
   162 				engine_result = iCallBackService->Wait( wait_data->iUsecaseID );
       
   163 				wait_rep.iResult = engine_result;
       
   164 				protErr = iProtocol.sendReply(CMD_REP_WAITID, sizeof(wait_rep), &wait_rep);
       
   165 				break;	
       
   166 
       
   167 			case CMD_REQ_ENDUSECASEID:
       
   168 				endusecase_data = (TEndUsecaseRequest*)cmdbptr;
       
   169 				engine_result = iCallBackService->EndUsecase( endusecase_data->iUsecaseID, endusecase_data->iResult, &script_result);
       
   170 				endusecase_rep.iCommandResult = engine_result;
       
   171 				endusecase_rep.iScriptResult = script_result;
       
   172 				protErr = iProtocol.sendReply(CMD_REP_ENDUSECASEID, sizeof(endusecase_rep), &endusecase_rep);
       
   173 				iStopFlag = 1;
       
   174 				break;
       
   175 
       
   176 			case CMD_REQ_GETVARIABLENAMEID:
       
   177 				variablename_data = (TGetVariableNameRequest*)cmdbptr;
       
   178 				engine_result = iCallBackService->GetEnvVariable(variablename_data->iVariableName, variablename_rep.iVariableValue, MAXVARNAMELEN );
       
   179 				variablename_rep.iResult = engine_result;
       
   180 				protErr = iProtocol.sendReply(CMD_REP_GETVARIABLENAMEID, sizeof(variablename_rep), &variablename_rep);
       
   181 				break;
       
   182 
       
   183 			case CMD_QUITID:
       
   184 				protErr = TCP_FAILED;
       
   185 				return_value = EAC_QUIT;
       
   186 				break;	
       
   187 
       
   188 			case CMD_REQ_RUNCOMMAND:
       
   189 				runcommand_data = (TRunCommandRequest*)cmdbptr;
       
   190 				engine_result = iCallBackService->RunCommand( runcommand_data->iCommandLine );
       
   191 				runcommand_rep.iResult = engine_result;
       
   192 				protErr = iProtocol.sendReply(CMD_REP_RUNCOMMAND, sizeof(runcommand_rep), &runcommand_rep);
       
   193 				break;
       
   194 
       
   195 			default:
       
   196 				iOutput->Error( UCCS_UNKNOWNCONTROLCOMMAND, NULL );
       
   197 				protErr = TCP_SUCCESS;
       
   198 				return_value = EAC_SUCCESS;
       
   199 		}
       
   200 
       
   201 		// print the message
       
   202 		LocalUpdateMessageOut( engine_result );
       
   203 
       
   204 		// if there was an error in the transport then exit
       
   205 		if( protErr != TCP_SUCCESS  ) {
       
   206 			if( return_value != EAC_QUIT ) {
       
   207 				return_value = EAC_PROCESSINGFAILED;
       
   208 			}
       
   209 			break;
       
   210 		}
       
   211 	}
       
   212 
       
   213 	// done
       
   214 	iProtocol.disconnect();
       
   215 	iStopFlag = 0;
       
   216 	*aErrorCode = protErr;
       
   217 	return return_value;
       
   218 	
       
   219 }		
       
   220 
       
   221 
       
   222 /**********************************************************************************************
       
   223  *
       
   224  * Helper: Print messages when we are in serial mode
       
   225  *
       
   226  *********************************************************************************************/
       
   227 void CUCCSCommandControl::LocalUpdateMessageIn(int aCommandId, char *aMsg)
       
   228 {
       
   229 	static char *cmdIdStrings[] = 
       
   230 	{
       
   231 		"StartUseCase (request)",		// CMD_REQ_STARTUSECASEID,
       
   232 		"Signal  (request)",			// CMD_REQ_SIGNALID,
       
   233 		"Rendezvous  (request)",		// CMD_REQ_RENDEZVOUSID,
       
   234 		"WaitForSignal (request)",		// CMD_REQ_WAITID,
       
   235 		"EndUseCase (request)",			// CMD_REQ_ENDUSECASEID,
       
   236 		"GetVariableName (request)",	// CMD_REQ_GETVARIABLENAMEID,
       
   237 		"Run (request)",				// CMD_REQ_RUNCOMMAND
       
   238 		"StartUseCase (response)",		// CMD_REP_STARTUSECASEID,
       
   239 		"Signal (response)"				// CMD_REP_SIGNALID,
       
   240 		"Rendezvous (response)",		// CMD_REP_RENDEZVOUSID,
       
   241 		"WaitForSignal  (response)",	// CMD_REP_WAITID,
       
   242 		"EndUseCase (response)",		// CMD_REP_ENDUSECASEID,
       
   243 		"GetVariableName (response)",	// CMD_REP_GETVARIABLENAMEID,
       
   244 		"Run (response)",				// CMD_REP_RUNCOMMAND,
       
   245 		"Quit",							// CMD_QUITID,
       
   246 		"Unknown",						// CMD_UNKNOWN
       
   247 	};
       
   248 
       
   249 	// don't display for console;  - consolidate o/p
       
   250 	if (iTransport != RCI_CONSOLE)
       
   251 	{
       
   252 		// display msg - belongs within protocol implementation?
       
   253 		fprintf(stdout, "\n%s\n", aMsg);
       
   254 
       
   255 		if (aCommandId > CMD_UNKNOWN)
       
   256 			fprintf(stdout, "Error! unsupported command identifier %d\n", aCommandId);
       
   257 		else
       
   258 			fprintf(stdout, " command; \"%s\" (%d)\n", cmdIdStrings[aCommandId], aCommandId);
       
   259 	}
       
   260 }
       
   261 
       
   262 void CUCCSCommandControl::LocalUpdateMessageOut(int aResult)
       
   263 {
       
   264 	// don't display for console;  - consolidate o/p
       
   265 	if( iTransport != RCI_CONSOLE)
       
   266 		fprintf(stdout, " result; \"%s\" (%d)\n", GetUccsErrorStringI(aResult), aResult);
       
   267 }