hti/PC_Tools/HTIGateway/ServicePlugins/HtiStif/HtiStif.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 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 */
       
    16 #include "HtiStifH.h"
       
    17 #include "HtiPlugin.h"
       
    18 #include "HtiSoapHandlerInterface.h"
       
    19 #include "../../htigateway/inc/DllModule.h"
       
    20 
       
    21 
       
    22 #include <iosfwd>
       
    23 #include <sstream>
       
    24 
       
    25 
       
    26 //**********************************************************************************
       
    27 // CONSTANTS
       
    28 //
       
    29 //**********************************************************************************
       
    30 
       
    31 // commands
       
    32 const BYTE CMD_STIF_OPEN				= 0x01;
       
    33 const BYTE CMD_STIF_CLOSE				= 0x02;
       
    34 const BYTE CMD_STIF_LOAD_MODULE			= 0x03;
       
    35 const BYTE CMD_STIF_UNLOAD_MODULE		= 0x04;
       
    36 const BYTE CMD_STIF_LIST_CASES			= 0x05;
       
    37 const BYTE CMD_STIF_START_CASE			= 0x06;
       
    38 const BYTE CMD_STIF_CANCEL_CASE			= 0x07;
       
    39 const BYTE CMD_STIF_PAUSE_CASE			= 0x08;
       
    40 const BYTE CMD_STIF_RESUME_CASE			= 0x09;
       
    41 const BYTE CMD_STIF_ADD_CASE_FILE		= 0x0A;
       
    42 const BYTE CMD_STIF_REMOVE_CASE_FILE	= 0x0B;
       
    43 const BYTE CMD_STIF_CASE_MESSAGE		= 0x0C;
       
    44 const BYTE CMD_STIF_SET_DEVICEID		= 0x0D;
       
    45 const BYTE CMD_STIF_TEST_COMPLETED		= 0x0E;
       
    46 const BYTE CMD_STIF_ATS_MESSAGE			= 0x0F;
       
    47 const BYTE CMD_STIF_SET_ATTRIBUTE		= 0x10;
       
    48 
       
    49 // timeouts
       
    50 const int TIMEOUT_10_SECONDS = 10000;
       
    51 
       
    52 // soap-action for this plugin
       
    53 const char soapAction[] = "HtiStif";
       
    54 
       
    55 // error codes for handle_received_message()
       
    56 #define HTI_RECV_ASYNC_CALL_SERVICED      1
       
    57 #define HTI_RECV_OK                       0
       
    58 #define HTI_RECV_GEN_ERROR                -1
       
    59 #define HTI_RECV_ERROR_MESSAGE            -2
       
    60 #define HTI_RECV_TIMEOUT                  -3
       
    61 #define HTI_RECV_ASYNC_CALL_SERVICE_ERROR -4
       
    62 #define HTI_RECV_ROUGUE_MESSAGE           -5
       
    63 #define HTI_RECV_UNKOWN_MESSAGE           -6
       
    64 
       
    65 //**********************************************************************************
       
    66 // FORWARD DECLARATIONS
       
    67 //
       
    68 //**********************************************************************************
       
    69 
       
    70 class HtiStifMsg;
       
    71 class HtiStifClients;
       
    72 class HtiStifSenderDll;
       
    73 //class HtiStifResults
       
    74 
       
    75 //int GetParameters(char* in, int inLen, char** parameters, int &size);
       
    76 int handle_received_message(HtiSoapHandlerInterface* handler, BOOL isSoapCallActive);
       
    77 void SetSoapFaultFromReceivedHtiError(struct soap *soap, BYTE *receivedMsgBody, int receivedMsgBodyLen);
       
    78 
       
    79 
       
    80 //**********************************************************************************
       
    81 // GLOBAL VARIABLES
       
    82 //
       
    83 //**********************************************************************************
       
    84 //HtiStifResults g_testCaseResults;
       
    85 HtiStifSenderDll *g_HtiStifSenderDll;
       
    86 HtiStifClients *g_HtiStifClients;
       
    87 
       
    88 //**********************************************************************************
       
    89 // HELPER FUNCTIONS/CLASSES
       
    90 //
       
    91 //**********************************************************************************
       
    92 
       
    93 //**********************************************************************************
       
    94 // HtiStifMsg
       
    95 //**********************************************************************************
       
    96 class HtiStifMsg
       
    97 {
       
    98 public:
       
    99 	HtiStifMsg(BYTE command);
       
   100 
       
   101 	BYTE* GetMsgBody() { return msgBody; };
       
   102 	int GetMsgLen() { return msgBodyLen; };
       
   103 
       
   104 	void AddString(char* string);
       
   105 	void AddIntAsString(int value);
       
   106 	void AddByte(BYTE byte);
       
   107 	void AddParameterSeparator();
       
   108 
       
   109 	int CheckResponseCommandCode(struct soap *soap, BYTE commandCode);
       
   110 
       
   111 	int SendReceiveMsg(struct soap *soap, int timeout);
       
   112 
       
   113 private:
       
   114 	void IncBodySize(int size);
       
   115 	BYTE *msgBody; // NOTE: deleted when sent or by gateway when received
       
   116 	int msgBodyLen;
       
   117 };
       
   118 
       
   119 HtiStifMsg::HtiStifMsg(BYTE command)
       
   120 {
       
   121 	msgBody = new BYTE[1];
       
   122 	msgBody[0] = command;
       
   123 	msgBodyLen = 1;
       
   124 }
       
   125 
       
   126 void HtiStifMsg::IncBodySize(int size)
       
   127 {
       
   128 	BYTE *newMsgBody = new BYTE[msgBodyLen+size];
       
   129 	memcpy(newMsgBody, msgBody, msgBodyLen);
       
   130 	delete msgBody;
       
   131 	msgBody = newMsgBody;
       
   132 	msgBodyLen += size;
       
   133 }
       
   134 
       
   135 void HtiStifMsg::AddString(char* string)
       
   136 {
       
   137 	int stringLen = (int) strlen(string);
       
   138 	int writePos = msgBodyLen;
       
   139 	IncBodySize(stringLen);
       
   140 	memcpy(msgBody+writePos, string, stringLen);
       
   141 }
       
   142 
       
   143 void HtiStifMsg::AddIntAsString(int value)
       
   144 {
       
   145 	char buf[50];
       
   146 	_itoa(value, buf, 10);
       
   147 	AddString(buf);
       
   148 }
       
   149 
       
   150 void HtiStifMsg::AddByte(BYTE byte)
       
   151 {
       
   152 	int writePos = msgBodyLen;
       
   153 	IncBodySize(1);
       
   154 	msgBody[writePos] = byte;
       
   155 }
       
   156 
       
   157 void HtiStifMsg::AddParameterSeparator()
       
   158 {
       
   159 	AddByte('|');
       
   160 }
       
   161 
       
   162 int HtiStifMsg::SendReceiveMsg(struct soap* soap, int timeout)
       
   163 {
       
   164 	//dump(msgBody, msgBodyLen);
       
   165 
       
   166 	BYTE commandCode = msgBody[0];
       
   167 
       
   168 	// Send the message to symbian side
       
   169 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   170 	handler->SendHtiMessage(HTI_STIF_UID, msgBody, msgBodyLen);
       
   171 
       
   172 	// Clean these up for received HTI message
       
   173 	delete msgBody;
       
   174 	msgBody = NULL;
       
   175 	msgBodyLen = 0;
       
   176 
       
   177 	while(1)
       
   178 	{
       
   179 		if (handler->WaitForHtiMessage(timeout))
       
   180 		{
       
   181 			int err = handle_received_message(handler, TRUE);
       
   182 
       
   183 			// NOTE: this will be destroyed by gateway
       
   184 			msgBody = (BYTE*) handler->ReceivedHtiMessageBody();
       
   185 			msgBodyLen = handler->ReceivedHtiMessageBodySize();
       
   186 			
       
   187 			// Received a HTI initiated msg
       
   188 			// Start waiting again
       
   189 			if( (err == HTI_RECV_ASYNC_CALL_SERVICED) || 
       
   190 				(err == HTI_RECV_ASYNC_CALL_SERVICE_ERROR) )
       
   191 			{
       
   192                 continue;
       
   193 			}
       
   194 
       
   195 			if( err == HTI_RECV_ERROR_MESSAGE )
       
   196 				SetSoapFaultFromReceivedHtiError(soap, msgBody, msgBodyLen);
       
   197 
       
   198 			return err;
       
   199 		}
       
   200 		else
       
   201 		{
       
   202 			// timeout
       
   203 			soap->error = soap_receiver_fault(soap, "HtiGateway", "No response from symbian side");
       
   204 			return HTI_RECV_TIMEOUT;
       
   205 		}
       
   206 	}
       
   207 }
       
   208 
       
   209 int HtiStifMsg::CheckResponseCommandCode(struct soap *soap, BYTE commandCode)
       
   210 {
       
   211 	int result = SOAP_OK;
       
   212 	
       
   213 	if( msgBody[0] != commandCode )
       
   214 	{
       
   215 		char msg[] = "incorrect response CommandCode (expected 0x%x, got 0x%x)";
       
   216 		int msgLen = _scprintf(msg, commandCode, msgBody[0])+1; // +1 for nul char
       
   217 		char *buf = new char[msgLen];
       
   218 		sprintf(buf, msg, commandCode, msgBody[0]);
       
   219 		soap->error = soap_receiver_fault(soap, "HtiError", buf);
       
   220 		delete buf;
       
   221 		result = SOAP_FAULT;
       
   222 	}
       
   223 
       
   224 	return result;
       
   225 }
       
   226 
       
   227 //**********************************************************************************
       
   228 // HtiStifClients
       
   229 //**********************************************************************************
       
   230 class HtiStifClients
       
   231 {
       
   232 public:
       
   233 	HtiStifClients() {m_size = 0; m_clients = NULL;};
       
   234 	~HtiStifClients();
       
   235 	int Add(char* host);
       
   236 	int Remove(char* host);
       
   237 	int NrOfClients() {return m_size;};
       
   238 	const char* Get(int index) {return m_clients[index];};
       
   239 private:
       
   240 	int m_size;
       
   241 	char **m_clients;
       
   242 };
       
   243 
       
   244 HtiStifClients::~HtiStifClients()
       
   245 {
       
   246 	for(int i=0; i<m_size; i++)
       
   247 		delete m_clients[i];
       
   248 	free(m_clients);
       
   249 	m_size = 0;
       
   250 }
       
   251 
       
   252 int HtiStifClients::Add(char* host)
       
   253 {
       
   254 	// check if host already exist
       
   255 	for(int i=0; i<m_size; i++)
       
   256 	{
       
   257 		if(!strcmp(m_clients[i], host))
       
   258 			return -1;
       
   259 	}
       
   260 
       
   261 	// alloc space for new pointer array
       
   262 	char **new_clients = (char**) malloc(sizeof(char*)*(m_size+1));
       
   263 	
       
   264 	// copy data
       
   265 	for(int i=0; i<m_size; i++)
       
   266 		new_clients[i] = m_clients[i];
       
   267 
       
   268 	// free previous pointer array & set new one
       
   269 	free(m_clients);
       
   270 	m_clients = new_clients;
       
   271 
       
   272 	// alloc space for host
       
   273 	char* buf = new char[strlen(host)+1]; // +1 for nul
       
   274 	strcpy(buf, host);
       
   275 	new_clients[m_size] = buf;
       
   276 
       
   277 	// update size
       
   278 	m_size += 1;
       
   279 
       
   280 	return 0;
       
   281 }
       
   282 
       
   283 int HtiStifClients::Remove(char* host)
       
   284 {
       
   285 	int err = -1;
       
   286 
       
   287 	// alloc a smaller pointer array
       
   288 	char **new_clients = (char**) malloc(sizeof(char*)*(m_size-1));
       
   289 	
       
   290 	// copy data except removed
       
   291 	int j=0;
       
   292 	for(int i=0; i<m_size; i++)
       
   293 	{
       
   294 		if(!strcmp(m_clients[i],host))
       
   295 		{
       
   296 			delete m_clients[i]; // delete removed host
       
   297 			err = 0;
       
   298 			continue;
       
   299 		}
       
   300 
       
   301 		new_clients[j] = m_clients[i];
       
   302 		j++;
       
   303 	}
       
   304 
       
   305 	// did not find host?
       
   306 	if(err != 0)
       
   307 	{
       
   308 		free(new_clients);
       
   309 		return err;
       
   310 	}
       
   311 
       
   312 	// free previous pointer array & set new one
       
   313 	free(m_clients);
       
   314 	m_clients = new_clients;
       
   315 
       
   316 	// update size
       
   317 	m_size -= 1;
       
   318 
       
   319 	return err;
       
   320 }
       
   321 
       
   322 
       
   323 
       
   324 //**********************************************************************************
       
   325 // HtiStifSenderDll
       
   326 //**********************************************************************************
       
   327 class HtiStifSenderDll : public CDLLModule
       
   328 {
       
   329 public:
       
   330 
       
   331 	DECLARE_DLL_FUNCTION(int, __cdecl,
       
   332 						soap_send_ns1__stifMessage, (soap *, const char *, const char *, char *, void *))
       
   333 	DECLARE_DLL_FUNCTION(int, __cdecl,
       
   334 						soap_send_ns1__stifResult, (soap *, const char *, const char *, ns1__HtiStifResult, void *))
       
   335 
       
   336     BEGIN_DLL_INIT()
       
   337 		INIT_DLL_FUNCTION(int, __cdecl,
       
   338 			 soap_send_ns1__stifMessage, (soap *, const char *, const char *, char *, void *), "soap_send_ns1__stifMessage")
       
   339 		INIT_DLL_FUNCTION(int, __cdecl,
       
   340 			 soap_send_ns1__stifResult, (soap *, const char *, const char *, ns1__HtiStifResult, void *), "soap_send_ns1__stifResult")
       
   341 	END_DLL_INIT()
       
   342 };
       
   343 
       
   344 void SetSoapFaultFromReceivedHtiError(struct soap *soap, BYTE *receivedMsgBody, int receivedMsgBodyLen)
       
   345 {
       
   346 	if( receivedMsgBodyLen == 10 )
       
   347 	{
       
   348 		// This is a standard error message
       
   349 		// (eg. not authenticated)
       
   350 		HtiSoapHandlerInterface* handler =
       
   351 			static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   352 		handler->SendSoapFaultFromReceivedHtiError();
       
   353 		return;
       
   354 	}
       
   355 
       
   356 	// Get error codes
       
   357 	int frameworkErrorCode = *((BYTE*)(receivedMsgBody + 1));
       
   358 	int serviceErrorCode = *((DWORD*)(receivedMsgBody + 2));
       
   359 
       
   360 	// Get error description
       
   361 	// NOTE: first byte is skipped because it contains the command code
       
   362 	int serviceErrorDescLen = receivedMsgBodyLen - 11;
       
   363 	char* serviceErrorDesc = new char[receivedMsgBodyLen - 11 + 1];
       
   364 	memcpy(serviceErrorDesc, receivedMsgBody+11, serviceErrorDescLen);
       
   365 	serviceErrorDesc[serviceErrorDescLen] = 0x0;
       
   366 
       
   367 	// Fill the xml struct
       
   368 	std::stringstream s;
       
   369 	s<<"<htiError xmlns=\'urn:hti/fault\'><frameworkErrorCode>";
       
   370 	s<<frameworkErrorCode;
       
   371 	s<<"</frameworkErrorCode><serviceErrorCode>";
       
   372 	s<<serviceErrorCode;
       
   373 	s<<"</serviceErrorCode><serviceErrorDescription>";
       
   374 	s<<serviceErrorDesc;
       
   375     s<<"</serviceErrorDescription>";
       
   376 	s<<"</htiError>";
       
   377 	
       
   378 	soap->error = soap_receiver_fault(soap, "HtiError", s.str().c_str() );
       
   379 
       
   380 	delete serviceErrorDesc;
       
   381 }
       
   382 
       
   383 //**********************************************************************************
       
   384 // handle_received_message
       
   385 //**********************************************************************************
       
   386 int handle_received_message(HtiSoapHandlerInterface* handler, BOOL isSoapCallActive)
       
   387 {
       
   388 	if(handler->IsReceivedHtiError())
       
   389 		return HTI_RECV_ERROR_MESSAGE;
       
   390 
       
   391 	// Get message
       
   392 	BYTE *msgBody = (BYTE*) handler->ReceivedHtiMessageBody();
       
   393 	int msgBodyLen = handler->ReceivedHtiMessageBodySize();
       
   394 
       
   395 	switch(msgBody[0])
       
   396 	{
       
   397 	// Initiated by SOAP
       
   398 	case CMD_STIF_OPEN:
       
   399 	case CMD_STIF_CLOSE:		
       
   400 	case CMD_STIF_LOAD_MODULE:
       
   401 	case CMD_STIF_UNLOAD_MODULE:
       
   402 	case CMD_STIF_LIST_CASES:
       
   403 	case CMD_STIF_START_CASE:
       
   404 	case CMD_STIF_CANCEL_CASE:
       
   405 	case CMD_STIF_PAUSE_CASE:
       
   406 	case CMD_STIF_RESUME_CASE:
       
   407 	case CMD_STIF_ADD_CASE_FILE:
       
   408 	case CMD_STIF_REMOVE_CASE_FILE:
       
   409 	case CMD_STIF_CASE_MESSAGE:
       
   410 	case CMD_STIF_SET_DEVICEID:
       
   411 	case CMD_STIF_SET_ATTRIBUTE:
       
   412 		if(isSoapCallActive)
       
   413 			return HTI_RECV_OK;
       
   414 		else
       
   415 		{
       
   416 			printf("HtiStif: Received a known but unexpected message (command code 0x%x)\n", msgBody[0]);
       
   417 			return HTI_RECV_ROUGUE_MESSAGE;
       
   418 		}
       
   419         break;
       
   420 
       
   421 	// Initiated by HTI
       
   422 	case CMD_STIF_TEST_COMPLETED:
       
   423 		{
       
   424 			// Make copy of parameters
       
   425 			char *params = new char[msgBodyLen];
       
   426 			memcpy(params, msgBody+1, msgBodyLen-1);
       
   427 			params[msgBodyLen-1] = 0x0; // add nul to make strtok easier
       
   428 			
       
   429 			// Get data from params
       
   430 			char *testCaseId = strtok(params, "|");
       
   431 			char *testResult = strtok(NULL, "|");
       
   432 			char *testExecutionResult = strtok(NULL, "|");
       
   433 			char *resultDescription = strtok(NULL, "|");
       
   434 
       
   435 			// Check if all is there
       
   436 			if( !testCaseId || !testResult || !testExecutionResult)
       
   437 			{
       
   438 				printf("HtiStif: Received incomplete testcase result\n" );
       
   439 				delete params;
       
   440 				return HTI_RECV_ASYNC_CALL_SERVICE_ERROR;
       
   441 			}
       
   442 			
       
   443 			// Get soap env
       
   444 			struct soap soap;
       
   445 			soap_init(&soap);
       
   446 			soap.namespaces = namespaces;
       
   447 
       
   448 			// Fill HtiStifResult
       
   449 			struct ns1__HtiStifResult result;
       
   450 			result.caseId = atoi(testCaseId);
       
   451 			result.caseResult = atoi(testResult);
       
   452 			result.executionResult = atoi(testExecutionResult);
       
   453 			if(resultDescription)
       
   454 			{
       
   455 				result.resultDescription = (char*) soap_malloc( &soap, strlen(resultDescription)+1 );
       
   456 				strcpy(result.resultDescription, resultDescription);
       
   457 			}
       
   458 			else
       
   459 				result.resultDescription = NULL;
       
   460 			
       
   461 			delete params; // not needed anymore
       
   462 			
       
   463 			// Send the HtiStifResult to registered clients
       
   464 			for(int i=0; i<g_HtiStifClients->NrOfClients(); i++)
       
   465 			{
       
   466 				if(g_HtiStifSenderDll->soap_send_ns1__stifResult(&soap, g_HtiStifClients->Get(i), soapAction, result, NULL) != SOAP_OK)
       
   467 					printf("HtiStif: error sending stifResult to %s\n", g_HtiStifClients->Get(i) );
       
   468 			}
       
   469 			soap_destroy(&soap);
       
   470 			soap_end(&soap);
       
   471 			soap_done(&soap);
       
   472 
       
   473 			return HTI_RECV_ASYNC_CALL_SERVICED;
       
   474 		}
       
   475 		break;
       
   476 
       
   477 	case CMD_STIF_ATS_MESSAGE:
       
   478 		{
       
   479 			// Get soap env
       
   480 			struct soap soap;
       
   481 			soap_init(&soap);
       
   482 			soap.namespaces = namespaces;
       
   483 
       
   484 			// Get message
       
   485 			// NOTE: messages are in string format so they don't have nul-characters
       
   486 			// This might change in the future and then xsd__string will no do for this task.
       
   487 			char* msg = (char*) soap_malloc(&soap, msgBodyLen);
       
   488 			memcpy(msg, msgBody+1, msgBodyLen-1);
       
   489 			msg[msgBodyLen-1] = 0x0; // add nul
       
   490 
       
   491 			// Send the HtiStifResult to registered clients
       
   492 			for(int i=0; i<g_HtiStifClients->NrOfClients(); i++)
       
   493 			{
       
   494 				if(g_HtiStifSenderDll->soap_send_ns1__stifMessage(&soap, g_HtiStifClients->Get(i), soapAction, msg, NULL) != SOAP_OK)
       
   495 					printf("HtiStif: error sending stifMessage to %s\n", g_HtiStifClients->Get(i) );
       
   496 			}
       
   497 			soap_destroy(&soap);
       
   498 			soap_end(&soap);
       
   499 			soap_done(&soap);
       
   500 			
       
   501 			return HTI_RECV_ASYNC_CALL_SERVICED;
       
   502 		}
       
   503 		break;
       
   504 
       
   505 
       
   506 	default:
       
   507 		printf("HtiStif: Received an unkown message (command code 0x%x, sync call active %d)\n", msgBody[0], isSoapCallActive);
       
   508 		return HTI_RECV_UNKOWN_MESSAGE;
       
   509 		break;
       
   510 	}
       
   511 
       
   512 	// Should never come here
       
   513 	return HTI_RECV_GEN_ERROR;
       
   514 }
       
   515 
       
   516 
       
   517 
       
   518 //**********************************************************************************
       
   519 // SOAP FUNCTIONS
       
   520 //
       
   521 //**********************************************************************************
       
   522 
       
   523 //**********************************************************************************
       
   524 // ns1__openStif()
       
   525 //**********************************************************************************
       
   526 int ns1__openStif(struct soap* soap, char *iniFile, struct ns1__openStifResponse *out)
       
   527 {
       
   528 	// Assemble HTI message
       
   529 	HtiStifMsg msg(CMD_STIF_OPEN);
       
   530 	if(iniFile != NULL)
       
   531 	{
       
   532 		if(strlen(iniFile) != 0)
       
   533 			msg.AddString(iniFile);
       
   534 	}
       
   535 
       
   536 	// Send msg and receive response
       
   537 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   538 		return SOAP_FAULT;
       
   539 
       
   540 	// Check response command code
       
   541 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_OPEN) )
       
   542 		return SOAP_FAULT;
       
   543 
       
   544 	return SOAP_OK;
       
   545 }
       
   546 
       
   547 //**********************************************************************************
       
   548 // ns1__closeStif()
       
   549 //**********************************************************************************
       
   550 int ns1__closeStif(struct soap* soap, void *_, struct ns1__closeStifResponse *out)
       
   551 {
       
   552 	// Assemble HTI message
       
   553 	HtiStifMsg msg(CMD_STIF_CLOSE);
       
   554 
       
   555 	// Send msg and receive response
       
   556 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   557 		return SOAP_FAULT;
       
   558 
       
   559 	// Check response command code
       
   560 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_CLOSE) )
       
   561 		return SOAP_FAULT;
       
   562 
       
   563 	return SOAP_OK;
       
   564 }
       
   565 
       
   566 
       
   567 //**********************************************************************************
       
   568 // ns1__loadStifTestModule()
       
   569 //**********************************************************************************
       
   570 int ns1__loadStifTestModule(struct soap* soap, char *moduleName, char *iniFile, char *&moduleNameLoaded)
       
   571 {
       
   572 	// Parameter check
       
   573 	if(check_mandatory_string_parameter(soap, moduleName, "moduleName"))
       
   574 		return SOAP_FAULT;
       
   575 
       
   576 	// Assemble HTI message
       
   577 	HtiStifMsg msg(CMD_STIF_LOAD_MODULE);
       
   578 	msg.AddString(moduleName);
       
   579 	if(iniFile != NULL)
       
   580 	{
       
   581 		if(strlen(iniFile) != 0)
       
   582 		{
       
   583 			msg.AddParameterSeparator();
       
   584 			msg.AddString(iniFile);
       
   585 		}
       
   586 	}
       
   587 
       
   588 	// Send msg and receive response
       
   589 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   590 		return SOAP_FAULT;
       
   591 
       
   592 	// Check CommandCode
       
   593 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_LOAD_MODULE) )
       
   594 		return SOAP_FAULT;
       
   595 
       
   596 	int moduleNameLoadedLen = msg.GetMsgLen()-1; // remaining msgBody
       
   597 	moduleNameLoaded = (char*) soap_malloc(soap, moduleNameLoadedLen+1); // +1 for nul char
       
   598 	memcpy(moduleNameLoaded, msg.GetMsgBody()+1, moduleNameLoadedLen);
       
   599 	moduleNameLoaded[moduleNameLoadedLen] = 0x0; // add nul char
       
   600 	
       
   601 	return SOAP_OK;
       
   602 }
       
   603 
       
   604 //**********************************************************************************
       
   605 // ns1__unloadStifTestModule()
       
   606 //**********************************************************************************
       
   607 int ns1__unloadStifTestModule(struct soap* soap, char *moduleName, char *&moduleNameUnloaded)
       
   608 {
       
   609 	// Parameter check
       
   610 	if(check_mandatory_string_parameter(soap, moduleName, "moduleName"))
       
   611 		return SOAP_FAULT;
       
   612 
       
   613 		// Assemble HTI message
       
   614 	HtiStifMsg msg(CMD_STIF_UNLOAD_MODULE);
       
   615 	msg.AddString(moduleName);
       
   616 
       
   617 	// Send msg and receive response
       
   618 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   619 		return SOAP_FAULT;
       
   620 
       
   621 	// Check CommandCode
       
   622 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_UNLOAD_MODULE) )
       
   623 		return SOAP_FAULT;
       
   624 
       
   625 	int moduleNameUnloadedLen = msg.GetMsgLen()-1; // remaining msgBody
       
   626 	moduleNameUnloaded = (char*) soap_malloc(soap, moduleNameUnloadedLen+1); // +1 for nul char
       
   627 	memcpy(moduleNameUnloaded, msg.GetMsgBody()+1, moduleNameUnloadedLen);
       
   628 	moduleNameUnloaded[moduleNameUnloadedLen] = 0x0; // add nul char
       
   629 
       
   630 	return SOAP_OK;
       
   631 }
       
   632 
       
   633 //**********************************************************************************
       
   634 // ns1__listStifTestCases()
       
   635 //**********************************************************************************
       
   636 int ns1__listStifTestCases(struct soap* soap, char *moduleName, struct ArrayOfTestCases *testCases)
       
   637 {
       
   638 	// Assemble HTI message
       
   639 	HtiStifMsg msg(CMD_STIF_LIST_CASES);
       
   640 	if(moduleName != NULL)
       
   641 	{
       
   642 		if(strlen(moduleName) != 0)
       
   643 			msg.AddString(moduleName);
       
   644 	}
       
   645 
       
   646 	// Send msg and receive response
       
   647 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   648 		return SOAP_FAULT;
       
   649 
       
   650 	// Check CommandCode
       
   651 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_LIST_CASES) )
       
   652 		return SOAP_FAULT;
       
   653 
       
   654 	// No testcases found
       
   655 	if(msg.GetMsgLen() <= 1)
       
   656 		return SOAP_OK;
       
   657 
       
   658 	// make a copy of the list
       
   659 	char* testCaseTitleList = new char[msg.GetMsgLen()];
       
   660 	memcpy(testCaseTitleList, msg.GetMsgBody()+1, msg.GetMsgLen());
       
   661 	testCaseTitleList[msg.GetMsgLen()-1] = 0x0; //add nul to make life easier
       
   662 
       
   663 	// Get number of testcases
       
   664 	char* testcase = strtok( testCaseTitleList, "|" );
       
   665 
       
   666 	// Only one testcase ?
       
   667 	if(testcase == NULL)
       
   668 	{
       
   669 		testCases->__size = 1;
       
   670 		testCases->__ptr = (char**) soap_malloc(soap, sizeof(char**));
       
   671 		testCases->__ptr[0] = (char*) soap_malloc(soap, msg.GetMsgLen());
       
   672 		memcpy(testCases->__ptr[0], testCaseTitleList, msg.GetMsgLen()); // (remember we added nul)	
       
   673 		delete testCaseTitleList;
       
   674 		return SOAP_OK;
       
   675 	}
       
   676 
       
   677 	// ..no! we have at least two
       
   678 	while(testcase != NULL)
       
   679 	{
       
   680 		testCases->__size++;
       
   681 		testcase = strtok( NULL, "|" ); // continue with testCaseTitleList
       
   682 	}
       
   683 	
       
   684 	// Now that we know the number of testcases title we can alloc soap space for 
       
   685 	// pointers and copy testcase titles to soap space.
       
   686 	testCases->__ptr = (char**) soap_malloc(soap, sizeof(char**)*testCases->__size);
       
   687 
       
   688 	// reset testCaseTitleList from previous strtok
       
   689 	memcpy(testCaseTitleList, msg.GetMsgBody()+1, msg.GetMsgLen());
       
   690 	testCaseTitleList[msg.GetMsgLen()-1] = 0x0; //add nul to make life easier
       
   691 
       
   692 	int i = 0;
       
   693 	testcase = strtok( testCaseTitleList, "|" );
       
   694 	while(testcase != NULL)
       
   695 	{
       
   696 		int testCaseLen = (int) strlen(testcase);
       
   697 		testCases->__ptr[i] = (char*) soap_malloc(soap, testCaseLen+1);
       
   698 		memcpy(testCases->__ptr[i], testcase, testCaseLen);
       
   699 		testCases->__ptr[i][testCaseLen] = 0x0; // add nul char
       
   700 		i++;
       
   701 		testcase = strtok( NULL, "|" );
       
   702 	}
       
   703 	delete testCaseTitleList;
       
   704 
       
   705 	return SOAP_OK;
       
   706 }
       
   707 
       
   708 //**********************************************************************************
       
   709 // ns1__startStifTestCase()
       
   710 //**********************************************************************************
       
   711 int ns1__startStifTestCase(struct soap* soap, char *moduleName, int testCaseIndex, int &testCaseId)
       
   712 {
       
   713 	// Assemble HTI message
       
   714 	HtiStifMsg msg(CMD_STIF_START_CASE);
       
   715 
       
   716 	if(moduleName != NULL)
       
   717 	{
       
   718 		if(strlen(moduleName) != 0)
       
   719 		{
       
   720 			msg.AddString(moduleName);
       
   721 			msg.AddParameterSeparator();
       
   722 		}
       
   723 	}
       
   724     msg.AddIntAsString(testCaseIndex);
       
   725 
       
   726 	// Send msg and receive response
       
   727 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   728 		return SOAP_FAULT;
       
   729 
       
   730 	// Check response command code
       
   731 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_START_CASE) )
       
   732 		return SOAP_FAULT;
       
   733 
       
   734 	// Get testCaseId
       
   735 	char* tmp = new char[msg.GetMsgLen()];
       
   736 	memcpy(tmp, msg.GetMsgBody()+1, msg.GetMsgLen()-1);
       
   737 	tmp[msg.GetMsgLen()-1] = 0x0; // add nul
       
   738 	testCaseId = atoi(tmp);
       
   739 
       
   740 	return SOAP_OK;
       
   741 }
       
   742 
       
   743 //**********************************************************************************
       
   744 // ns1__cancelStifTestCase()
       
   745 //**********************************************************************************
       
   746 int ns1__cancelStifTestCase(struct soap* soap, int testCaseId, struct ns1__cancelStifTestCaseResponse *out)
       
   747 {
       
   748 	// Assemble HTI message
       
   749 	HtiStifMsg msg(CMD_STIF_CANCEL_CASE);
       
   750 	msg.AddIntAsString(testCaseId);
       
   751 
       
   752 	// Send msg and receive response
       
   753 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   754 		return SOAP_FAULT;
       
   755 
       
   756 	// Check response command code
       
   757 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_CANCEL_CASE) )
       
   758 		return SOAP_FAULT;
       
   759 
       
   760 	return SOAP_OK;
       
   761 }
       
   762 
       
   763 //**********************************************************************************
       
   764 // ns1__pauseStifTestCase()
       
   765 //**********************************************************************************
       
   766 int ns1__pauseStifTestCase(struct soap* soap, int testCaseId, struct ns1__pauseStifTestCaseResponse *out)
       
   767 {
       
   768 	// Assemble HTI message
       
   769 	HtiStifMsg msg(CMD_STIF_PAUSE_CASE);
       
   770 	msg.AddIntAsString(testCaseId);
       
   771 
       
   772 	// Send msg and receive response
       
   773 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   774 		return SOAP_FAULT;
       
   775 
       
   776 	// Check response command code
       
   777 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_PAUSE_CASE) )
       
   778 		return SOAP_FAULT;
       
   779 
       
   780 	return SOAP_OK;
       
   781 }
       
   782 
       
   783 //**********************************************************************************
       
   784 // ns1__resumeStifTestCase()
       
   785 //**********************************************************************************
       
   786 int ns1__resumeStifTestCase(struct soap* soap, int testCaseId, struct ns1__resumeStifTestCaseResponse *out)
       
   787 {
       
   788 	// Assemble HTI message
       
   789 	HtiStifMsg msg(CMD_STIF_RESUME_CASE);
       
   790 	msg.AddIntAsString(testCaseId);
       
   791 
       
   792 	// Send msg and receive response
       
   793 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   794 		return SOAP_FAULT;
       
   795 
       
   796 	// Check response command code
       
   797 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_RESUME_CASE) )
       
   798 		return SOAP_FAULT;
       
   799 
       
   800 	return SOAP_OK;
       
   801 }
       
   802 
       
   803 //**********************************************************************************
       
   804 // ns1__addStifTestCaseFile()
       
   805 //**********************************************************************************
       
   806 int ns1__addStifTestCaseFile(struct soap* soap, char *moduleName, char *testCaseFile, struct ns1__addStifTestCaseFileResponse *out)
       
   807 {
       
   808 	// Parameter check
       
   809 	if( check_mandatory_string_parameter(soap, moduleName, "moduleName") ||
       
   810 		check_mandatory_string_parameter(soap, testCaseFile, "testCaseFile") )
       
   811 		return SOAP_FAULT;
       
   812 
       
   813 	// Assemble HTI message
       
   814 	HtiStifMsg msg(CMD_STIF_ADD_CASE_FILE);
       
   815 	msg.AddString(moduleName);
       
   816 	msg.AddParameterSeparator();
       
   817 	msg.AddString(testCaseFile);
       
   818 
       
   819 	// Send msg and receive response
       
   820 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   821 		return SOAP_FAULT;
       
   822 
       
   823 	// Check response command code
       
   824 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_ADD_CASE_FILE) )
       
   825 		return SOAP_FAULT;
       
   826 
       
   827 	return SOAP_OK;
       
   828 }
       
   829 
       
   830 //**********************************************************************************
       
   831 // ns1__removeStifTestCaseFile()
       
   832 //**********************************************************************************
       
   833 int ns1__removeStifTestCaseFile(struct soap* soap, char *moduleName, char *testCaseFile, struct ns1__removeStifTestCaseFileResponse *out)
       
   834 {
       
   835 	// Parameter check
       
   836 	if( check_mandatory_string_parameter(soap, moduleName, "moduleName") ||
       
   837 		check_mandatory_string_parameter(soap, testCaseFile, "testCaseFile") )
       
   838 		return SOAP_FAULT;
       
   839 
       
   840 	// Assemble HTI message
       
   841 	HtiStifMsg msg(CMD_STIF_REMOVE_CASE_FILE);
       
   842 	msg.AddString(moduleName);
       
   843 	msg.AddParameterSeparator();
       
   844 	msg.AddString(testCaseFile);
       
   845 
       
   846 	// Send msg and receive response
       
   847 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   848 		return SOAP_FAULT;
       
   849 
       
   850 	// Check response command code
       
   851 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_REMOVE_CASE_FILE) )
       
   852 		return SOAP_FAULT;
       
   853 
       
   854 	return SOAP_OK;
       
   855 }
       
   856 
       
   857 //**********************************************************************************
       
   858 // ns1__setDeviceId()
       
   859 //**********************************************************************************
       
   860 int ns1__setDeviceId(struct soap* soap, int deviceId, struct ns1__setDeviceIdResponse *out)
       
   861 {
       
   862 	// Assemble HTI message
       
   863 	HtiStifMsg msg(CMD_STIF_SET_DEVICEID);
       
   864 	msg.AddIntAsString(deviceId);
       
   865 
       
   866 	// Send msg and receive response
       
   867 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   868 		return SOAP_FAULT;
       
   869 
       
   870 	// Check response command code
       
   871 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_SET_DEVICEID) )
       
   872 		return SOAP_FAULT;
       
   873 
       
   874 	return SOAP_OK;
       
   875 }
       
   876 
       
   877 //**********************************************************************************
       
   878 // ns1__queryStifTestCaseResult()
       
   879 //**********************************************************************************
       
   880 int ns1__queryStifTestCaseResult(struct soap* soap, int testCaseId, queryStifTestCaseResultResponse &r)
       
   881 {
       
   882 	soap->error = soap_receiver_fault(soap, "HtiGateway", "queryStifTestCaseResult: not supported");
       
   883 	return SOAP_FAULT;
       
   884 }
       
   885 
       
   886 //**********************************************************************************
       
   887 // ns1__runStifTestCase()
       
   888 //**********************************************************************************
       
   889 int ns1__runStifTestCase(struct soap* soap, char *moduleName, int testCaseIndex, struct runStifTestCaseResponse &r)
       
   890 {
       
   891 	soap->error = soap_receiver_fault(soap, "HtiGateway", "runStifTestCase: not supported");
       
   892 	return SOAP_FAULT;
       
   893 }
       
   894 
       
   895 //**********************************************************************************
       
   896 // ns1__register()
       
   897 //**********************************************************************************
       
   898 int ns1__register(struct soap* soap, char *host, struct ns1__registerResponse *out)
       
   899 {
       
   900 	if(check_mandatory_string_parameter(soap, host, "host"))
       
   901 		return SOAP_FAULT;
       
   902 
       
   903 	if(g_HtiStifClients->Add(host) != 0)
       
   904 	{
       
   905 		soap->error = soap_receiver_fault(soap, "HtiGateway", "register: host already registered");
       
   906 		return SOAP_FAULT;
       
   907 	}
       
   908 
       
   909 	return SOAP_OK;
       
   910 }
       
   911 
       
   912 //**********************************************************************************
       
   913 // ns1__deregister()
       
   914 //**********************************************************************************
       
   915 int ns1__deregister(struct soap* soap, char *host, struct ns1__deregisterResponse *out)
       
   916 {
       
   917 	if(check_mandatory_string_parameter(soap, host, "host"))
       
   918 		return SOAP_FAULT;
       
   919 
       
   920 	if(g_HtiStifClients->Remove(host) != 0)
       
   921 	{
       
   922 		soap->error = soap_receiver_fault(soap, "HtiGateway", "deregister: did not find host");
       
   923         return SOAP_FAULT;
       
   924 	}
       
   925 
       
   926 	return SOAP_OK;
       
   927 }
       
   928 
       
   929 //**********************************************************************************
       
   930 // ns1__stifMessage()
       
   931 //**********************************************************************************
       
   932 int ns1__stifMessage(struct soap* soap, char *message, struct ns1__stifMessageResponse *out)
       
   933 {
       
   934 	// Parameter check
       
   935 	if( check_mandatory_string_parameter(soap, message, "message") )
       
   936 		return SOAP_FAULT;
       
   937 
       
   938 	// Assemble HTI message
       
   939 	HtiStifMsg msg(CMD_STIF_CASE_MESSAGE);
       
   940 	msg.AddString(message);
       
   941 
       
   942 	// Send msg and receive response
       
   943 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   944 		return SOAP_FAULT;
       
   945 
       
   946 	// Check response command code
       
   947 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_CASE_MESSAGE) )
       
   948 		return SOAP_FAULT;
       
   949 
       
   950 	return SOAP_OK;
       
   951 }
       
   952 
       
   953 //**********************************************************************************
       
   954 // ns1__setAttribute()
       
   955 //**********************************************************************************
       
   956 int ns1__setAttribute(struct soap* soap, 
       
   957 					  char *attribute, 
       
   958 					  char *value, 
       
   959                       struct ns1__SetAttributeResponse *out)
       
   960 {
       
   961 	// Parameter check
       
   962 	if( check_mandatory_string_parameter(soap, attribute, "attribute") ||
       
   963 		check_mandatory_string_parameter(soap, value, "value") )
       
   964 		return SOAP_FAULT;
       
   965 
       
   966 	// Assemble HTI message
       
   967 	HtiStifMsg msg(CMD_STIF_SET_ATTRIBUTE);
       
   968 	msg.AddString(attribute);
       
   969 	msg.AddParameterSeparator();
       
   970 	msg.AddString(value);
       
   971 
       
   972 	// Send msg and receive response
       
   973 	if( msg.SendReceiveMsg(soap, TIMEOUT_10_SECONDS) != ERROR_SUCCESS )
       
   974 		return SOAP_FAULT;
       
   975 
       
   976 	// Check response command code
       
   977 	if( msg.CheckResponseCommandCode(soap, CMD_STIF_SET_ATTRIBUTE) )
       
   978 		return SOAP_FAULT;
       
   979 
       
   980 	return SOAP_OK;
       
   981 }
       
   982 
       
   983 
       
   984 //**********************************************************************************
       
   985 // EXPORTED FUNCTIONS
       
   986 //
       
   987 //**********************************************************************************
       
   988 
       
   989 //**********************************************************************************
       
   990 // DllMain
       
   991 //**********************************************************************************
       
   992 BOOL WINAPI DllMain(
       
   993     HINSTANCE hinstDLL,  // handle to DLL module
       
   994     DWORD fdwReason,     // reason for calling function
       
   995     LPVOID lpReserved )  // reserved
       
   996 {
       
   997     switch( fdwReason ) 
       
   998     { 
       
   999         case DLL_PROCESS_ATTACH:
       
  1000 
       
  1001 			g_HtiStifClients = new HtiStifClients;
       
  1002 			
       
  1003 			// Load HtiStifSender
       
  1004 			g_HtiStifSenderDll = new HtiStifSenderDll;
       
  1005 			if( g_HtiStifSenderDll->Init("serviceplugins/HtiStifSender.dll") != TRUE)
       
  1006 			{
       
  1007 				printf("HtiStif: could not load HtiStifSender.dll");
       
  1008 				return FALSE;
       
  1009 			}
       
  1010 			
       
  1011 			// Check exported functions
       
  1012 			if( !g_HtiStifSenderDll->soap_send_ns1__stifMessage )
       
  1013 			{
       
  1014 				printf("HtiStif: HtiStifSender.dll - exported function soap_send_ns1__stifMessage() not found");
       
  1015 				return FALSE;
       
  1016 			}
       
  1017 
       
  1018 			if( !g_HtiStifSenderDll->soap_send_ns1__stifResult )
       
  1019 			{
       
  1020 				printf("HtiStif: HtiStifSender.dll - exported function soap_send_ns1__stifResult() not found");
       
  1021 				return FALSE;
       
  1022 			}
       
  1023 	        break;
       
  1024 
       
  1025         case DLL_PROCESS_DETACH:
       
  1026 
       
  1027 			// Delete HtiStifSender
       
  1028  			delete g_HtiStifSenderDll;
       
  1029 
       
  1030 			delete g_HtiStifClients;
       
  1031 
       
  1032             break;
       
  1033     }
       
  1034     return TRUE;
       
  1035 }
       
  1036 
       
  1037 //**********************************************************************************
       
  1038 // hti_serve
       
  1039 //**********************************************************************************
       
  1040 int hti_serve(HtiSoapHandlerInterface* handler)
       
  1041 {
       
  1042 	return handle_received_message(handler, FALSE);
       
  1043 }
       
  1044 
       
  1045 
       
  1046 
       
  1047