hti/PC_Tools/HTIGateway/ServicePlugins/HtiFtp/HtiFtp.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 "HtiFtpH.h"
       
    17 
       
    18 #include "HtiPlugin.h"
       
    19 #include <string.h>
       
    20 
       
    21 #include "HtiSoapHandlerInterface.h"
       
    22 
       
    23 const unsigned char CMD_FTP_STOR	    = 0x03;
       
    24 const unsigned char CMD_FTP_RETR	    = 0x05;
       
    25 const unsigned char CMD_FTP_LIST	    = 0x07;
       
    26 const unsigned char CMD_FTP_LISTDIR	    = 0x11;
       
    27 const unsigned char CMD_FTP_LISTSIZE    = 0x13;
       
    28 const unsigned char CMD_FTP_LISTDRIVES  = 0x15;
       
    29 const unsigned char CMD_FTP_MKD		    = 0x09;
       
    30 const unsigned char CMD_FTP_RMD	    	= 0x0B;
       
    31 const unsigned char CMD_FTP_DELE	    = 0x0D;
       
    32 const unsigned char CMD_FTP_CANCEL	    = 0x0E;
       
    33 const unsigned char CMD_FTP_FILESIZE    = 0x0F;
       
    34 
       
    35 const unsigned char CMD_FTP_SETFORCE	= 0x20;
       
    36 const unsigned char CMD_FTP_CHECKSUM	= 0x30;
       
    37 const unsigned char CMD_FTP_CHECKSUM_U	= 0x31;
       
    38 const unsigned char CMD_FTP_FORMAT      = 0x40;
       
    39 
       
    40 const unsigned char CMD_FTP_OK		= 0xF0;
       
    41 const unsigned char CONTROL_PRIORITY = 2;
       
    42 
       
    43 const int MAX_HTI_MSG_SIZE = 8192;
       
    44 
       
    45 char fileMimeType[] = "application/octet-stream"; //??
       
    46 
       
    47 const static int g_ftpHtiTimeoutControl = 10000;
       
    48 const static int g_ftpHtiTimeoutData = 60*60000;
       
    49 
       
    50 int sendHtiMessage(struct soap *soap,
       
    51 				   const char cmd,
       
    52 				   const wchar_t* arg )
       
    53 {
       
    54 	int resultCode = SOAP_OK;
       
    55 	
       
    56 	int textLen = 0;
       
    57 	int bodyLen = 0;
       
    58 	if ( arg )
       
    59 	{
       
    60 		textLen = (int)wcslen( arg );
       
    61 		if ( textLen > 0xFF )
       
    62 		{
       
    63 			soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
    64 			resultCode = SOAP_FAULT;
       
    65 			return resultCode;
       
    66 		}
       
    67 		bodyLen = 2*textLen + 2;
       
    68 	}
       
    69 	else
       
    70 	{
       
    71         bodyLen = 1;
       
    72 	}
       
    73 
       
    74 	BYTE* msgBody = new BYTE[ bodyLen ];
       
    75 	msgBody[0] = cmd;
       
    76 
       
    77 	if ( arg )
       
    78 	{
       
    79 		msgBody[1] = textLen;
       
    80 		memcpy( msgBody + 2, arg, 2*textLen );
       
    81 	}
       
    82 
       
    83 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
    84 
       
    85 	handler->SendHtiMessage(HTI_UID, msgBody, bodyLen, CONTROL_PRIORITY );
       
    86 
       
    87 	delete[] msgBody;
       
    88 	return resultCode;
       
    89 }
       
    90 
       
    91 int receiveFtpOk( struct soap *soap, DWORD waitTimeout )
       
    92 {
       
    93 	int resultCode = SOAP_OK;
       
    94 	
       
    95 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
    96 
       
    97 	if ( handler->WaitForHtiMessage( waitTimeout ) )
       
    98 	{
       
    99 		if ( !handler->IsReceivedHtiError() )
       
   100 		{
       
   101 			DWORD bodySize = handler->ReceivedHtiMessageBodySize();
       
   102 			BYTE* body = (BYTE*)(handler->ReceivedHtiMessageBody());
       
   103 
       
   104 			if ( ! ( bodySize == 1 && body[0] == CMD_FTP_OK ) )
       
   105 			{
       
   106 				soap->error = soap_receiver_fault(soap,
       
   107 												"Received invalid hti message", NULL);
       
   108 				resultCode = SOAP_FAULT;
       
   109 			}
       
   110 		}
       
   111 		else
       
   112 		{
       
   113 			handler->SendSoapFaultFromReceivedHtiError();
       
   114 			resultCode = SOAP_FAULT;
       
   115 		}
       
   116 	}
       
   117 	else
       
   118 	{
       
   119 		soap->error = soap_receiver_fault(soap,
       
   120 										  "Failed receive hti message", NULL);
       
   121 		resultCode = SOAP_FAULT;
       
   122 	}
       
   123 	
       
   124 	return resultCode;
       
   125 }
       
   126 
       
   127 int sendHtiMessageAndReceiveOK(struct soap *soap,
       
   128 							   const char cmd,
       
   129 							   const wchar_t* arg )
       
   130 {
       
   131 	if ( arg == NULL )
       
   132 	{
       
   133 		soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
   134 		return SOAP_FAULT;
       
   135 	}
       
   136 	int resultCode = sendHtiMessage( soap, cmd, arg );
       
   137 	if ( resultCode != SOAP_OK )
       
   138 	{
       
   139 		return resultCode;
       
   140 	}
       
   141 	
       
   142 	return receiveFtpOk(soap, g_ftpHtiTimeoutControl);
       
   143 }
       
   144 
       
   145 int sendHtiMessageAndReceiveStringArray(struct soap *soap,
       
   146 										const char cmd,
       
   147 										const wchar_t* arg,
       
   148 										struct ArrayOfStrings &array )
       
   149 {
       
   150 	if ( arg == NULL )
       
   151 	{
       
   152 		soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
   153 		return SOAP_FAULT;
       
   154 	}
       
   155 
       
   156 	int resultCode = SOAP_OK;
       
   157 	resultCode = sendHtiMessage( soap, cmd, arg );
       
   158 	if ( resultCode != SOAP_OK )
       
   159 	{
       
   160 		return resultCode;
       
   161 	}
       
   162 	
       
   163 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   164 
       
   165 	if (handler->WaitForHtiMessage(g_ftpHtiTimeoutData) )
       
   166 	{
       
   167 		if ( !handler->IsReceivedHtiError() )
       
   168 		{
       
   169             DWORD bodySize = handler->ReceivedHtiMessageBodySize();
       
   170 			BYTE* body = (BYTE*)(handler->ReceivedHtiMessageBody());
       
   171 			//check that body size is valid
       
   172 			if ( bodySize > 0 )
       
   173 			{
       
   174 				//calculate number of elements
       
   175 				DWORD i = 0;
       
   176 				int numOfElements = 0;
       
   177 				while ( i < bodySize )
       
   178 				{
       
   179                     ++numOfElements;
       
   180 					i += 2*body[i] + 1;
       
   181 				}
       
   182 
       
   183 				//printf("strings: %d\n", numOfElements);
       
   184 
       
   185 				if ( i != bodySize )
       
   186 				{
       
   187 					soap->error = soap_receiver_fault(soap,
       
   188 													"Invalid hti message", NULL);
       
   189 					resultCode = SOAP_FAULT;
       
   190 				}
       
   191 				else
       
   192 				{
       
   193 					array.__size = numOfElements;
       
   194 					array.__ptr = (wchar_t**)soap_malloc(soap, sizeof(wchar_t*)*array.__size );
       
   195 					int j;
       
   196 					for( j=0, i=0; j<array.__size; ++j, i += 2*body[i] + 1)
       
   197 					{
       
   198 						//printf("%d size=%d ", j, body[i]);
       
   199 						array.__ptr[j] = (wchar_t*)soap_malloc(soap, 2*body[i] + 2 );
       
   200 						memcpy(array.__ptr[j],
       
   201 								body + i + 1,
       
   202 								2*body[i] );
       
   203 						(array.__ptr[j])[body[i]] = 0; //null char
       
   204 						int strLen = (int)wcslen( array.__ptr[j] );
       
   205 
       
   206 						//wprintf(L">%S<\n", array.__ptr[j] );
       
   207 						//printf("\n");
       
   208 					}
       
   209 				}
       
   210 			}
       
   211 			else
       
   212 			{
       
   213 				array.__size = 0;
       
   214 				array.__ptr = 0;
       
   215 			}
       
   216 		}
       
   217 		else
       
   218 		{
       
   219 			handler->SendSoapFaultFromReceivedHtiError();
       
   220 			resultCode = SOAP_FAULT;
       
   221 		}
       
   222 	}
       
   223 	else
       
   224 	{
       
   225 		soap->error = soap_receiver_fault(soap,
       
   226 										  "Failed receive hti message", NULL);
       
   227 		resultCode = SOAP_FAULT;
       
   228 	}
       
   229 	
       
   230 	return resultCode;
       
   231 }
       
   232 
       
   233 int sendHtiMessageAndReceiveHtiFileInfoArray(struct soap *soap,
       
   234 										const char cmd,
       
   235 										const wchar_t* arg,
       
   236 										struct ArrayOfHtiFileInfos &array )
       
   237 {
       
   238 	if ( arg == NULL )
       
   239 	{
       
   240 		soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
   241 		return SOAP_FAULT;
       
   242 	}
       
   243 
       
   244 	int resultCode = SOAP_OK;
       
   245 	resultCode = sendHtiMessage( soap, cmd, arg );
       
   246 	if ( resultCode != SOAP_OK )
       
   247 	{
       
   248 		return resultCode;
       
   249 	}
       
   250 	
       
   251 	HtiSoapHandlerInterface* handler =
       
   252         static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   253 
       
   254 	if (handler->WaitForHtiMessage(g_ftpHtiTimeoutData) )
       
   255 	{
       
   256 		if ( !handler->IsReceivedHtiError() )
       
   257 		{
       
   258             DWORD bodySize = handler->ReceivedHtiMessageBodySize();
       
   259 			BYTE* body = (BYTE*)(handler->ReceivedHtiMessageBody());
       
   260 			//check that body size is valid
       
   261 			if ( bodySize > 0 )
       
   262 			{
       
   263 				//calculate number of elements
       
   264 				DWORD i = 0;
       
   265 				int numOfElements = 0;
       
   266 				while ( i < bodySize )
       
   267 				{
       
   268                     ++numOfElements;
       
   269 					i += 2*body[i] + 1 + 4;
       
   270 				}
       
   271 
       
   272                 if ( i != bodySize )
       
   273 				{
       
   274 					soap->error = soap_receiver_fault(soap,
       
   275 											"Invalid hti message", NULL);
       
   276 					resultCode = SOAP_FAULT;
       
   277 				}
       
   278 				else
       
   279 				{
       
   280 					array.__size = numOfElements;
       
   281 					array.__ptr = (ns1__HtiFileInfo*) soap_malloc(
       
   282                             soap, sizeof(ns1__HtiFileInfo)*array.__size );
       
   283 					int j;
       
   284 					for( j=0, i=0; j<array.__size; ++j, i += 2*body[i] + 1 + 4)
       
   285 					{
       
   286 						array.__ptr[j].fileName =
       
   287                             (wchar_t*)soap_malloc(soap, 2*body[i] + 2 );
       
   288 						memcpy(array.__ptr[j].fileName, body + i + 1, 2*body[i]);
       
   289                         (array.__ptr[j].fileName)[body[i]] = 0; //null char
       
   290                         // the file size bytes
       
   291                         array.__ptr[j].fileSize = *(int*)(body + i + 2*body[i] + 1);
       
   292                     }
       
   293                 }
       
   294             }
       
   295 			else
       
   296 			{
       
   297 				array.__size = 0;
       
   298 				array.__ptr = 0;
       
   299 			}
       
   300 		}
       
   301 		else
       
   302 		{
       
   303 			handler->SendSoapFaultFromReceivedHtiError();
       
   304 			resultCode = SOAP_FAULT;
       
   305 		}
       
   306 	}
       
   307 	else
       
   308 	{
       
   309 		soap->error = soap_receiver_fault(soap,
       
   310 										  "Failed receive hti message", NULL);
       
   311 		resultCode = SOAP_FAULT;
       
   312 	}
       
   313 
       
   314     return resultCode;
       
   315 }
       
   316 
       
   317 int sendHtiMessageAndReceiveHtiDriveInfoArray(struct soap *soap,
       
   318                                         const char cmd,
       
   319                                         struct ArrayOfHtiDriveInfos &array )
       
   320 {
       
   321 	int resultCode = SOAP_OK;
       
   322 	resultCode = sendHtiMessage( soap, cmd, NULL );
       
   323 	if ( resultCode != SOAP_OK )
       
   324 	{
       
   325 		return resultCode;
       
   326 	}
       
   327 	
       
   328 	HtiSoapHandlerInterface* handler =
       
   329         static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   330 
       
   331 	if (handler->WaitForHtiMessage(g_ftpHtiTimeoutData) )
       
   332 	{
       
   333 		if (!handler->IsReceivedHtiError())
       
   334 		{
       
   335 			array.__size = 0;
       
   336 			array.__ptr = 0;
       
   337             DWORD bodySize = handler->ReceivedHtiMessageBodySize();
       
   338 			BYTE* body = (BYTE*)(handler->ReceivedHtiMessageBody());
       
   339             int offset = 0;
       
   340 			//first byte is the number of elements
       
   341 			int numOfElements = body[offset];
       
   342             offset++;
       
   343 
       
   344             array.__size = numOfElements;
       
   345 			array.__ptr = (ns1__HtiDriveInfo*) soap_malloc(
       
   346                     soap, sizeof(ns1__HtiDriveInfo)*array.__size );
       
   347 
       
   348 			int i;
       
   349 			for(i=0; i<array.__size; ++i)
       
   350 			{
       
   351                 int rootPathLength = body[offset];
       
   352                 offset++;
       
   353 				array.__ptr[i].rootPath =
       
   354                     (wchar_t*)soap_malloc(soap, 2*rootPathLength + 2);
       
   355 				memcpy(array.__ptr[i].rootPath, body + offset, 2*rootPathLength);
       
   356                 (array.__ptr[i].rootPath)[rootPathLength] = 0; //null char
       
   357                 offset += 2*rootPathLength;
       
   358                 array.__ptr[i].mediaType = (ns1__driveMediaType)body[offset];
       
   359                 offset++;
       
   360                 array.__ptr[i].uniqueID = *(int*)(body + offset);
       
   361                 offset += sizeof(int);
       
   362                 array.__ptr[i].driveSize = *(ULONG64*)(body + offset);
       
   363                 offset += sizeof(ULONG64);
       
   364                 array.__ptr[i].freeSpace = *(ULONG64*)(body + offset);
       
   365                 offset += sizeof(ULONG64);
       
   366                 int nameLength = body[offset];
       
   367                 offset++;
       
   368                 array.__ptr[i].driveName = 
       
   369                     (wchar_t*)soap_malloc(soap, 2*nameLength + 2);
       
   370                 memcpy(array.__ptr[i].driveName, body + offset, 2*nameLength);
       
   371                 (array.__ptr[i].driveName)[nameLength] = 0; //null char
       
   372                 offset += 2*nameLength;
       
   373             }
       
   374 		}
       
   375 		else
       
   376 		{
       
   377 			handler->SendSoapFaultFromReceivedHtiError();
       
   378 			resultCode = SOAP_FAULT;
       
   379 		}
       
   380 	}
       
   381 	else
       
   382 	{
       
   383 		soap->error = soap_receiver_fault(soap,
       
   384 										  "Failed receive hti message", NULL);
       
   385 		resultCode = SOAP_FAULT;
       
   386 	}
       
   387 
       
   388     return resultCode;
       
   389 }
       
   390 
       
   391 int sendBinary(struct soap *soap, char* data, const DWORD dataSize )
       
   392 {
       
   393 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   394 
       
   395 	DWORD offset = 0;
       
   396 
       
   397     // dataSize == 0 means that uploading empty file - we need to send one
       
   398     // empty data message, so needs to go to following while once
       
   399 	while( offset < dataSize || ( offset == 0 && dataSize == 0 ) )
       
   400 	{
       
   401 		handler->SendHtiMessage( HTI_UID,
       
   402 					data + offset,
       
   403 					offset + MAX_HTI_MSG_SIZE > dataSize ? dataSize - offset : MAX_HTI_MSG_SIZE
       
   404 					);
       
   405 		offset += MAX_HTI_MSG_SIZE;
       
   406 		//check for error messages
       
   407 		if (handler->WaitForHtiMessage(0) ) 
       
   408 		{
       
   409 			if ( handler->IsReceivedHtiError() )
       
   410 			{
       
   411 				handler->SendSoapFaultFromReceivedHtiError();
       
   412 			}
       
   413 			else
       
   414 			{
       
   415 				soap->error = soap_receiver_fault(soap,
       
   416 												"Not expected hti message", NULL);
       
   417 			}
       
   418 			return SOAP_FAULT;
       
   419 		}
       
   420 	}
       
   421 
       
   422 	return SOAP_OK;
       
   423 }
       
   424 
       
   425 int sendSTOR(struct soap *soap,
       
   426              const wchar_t* arg,
       
   427 			 const DWORD fileSize )
       
   428 {
       
   429 	int resultCode = SOAP_OK;
       
   430 	
       
   431 	int textLen = (int)wcslen( arg );
       
   432 
       
   433 	if ( !arg || textLen > 0xFF )
       
   434 	{
       
   435 		soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
   436 		resultCode = SOAP_FAULT;
       
   437 		return resultCode;
       
   438 	}
       
   439 
       
   440 	int	bodyLen = 2*textLen + 6;
       
   441 
       
   442 	BYTE* msgBody = new BYTE[ bodyLen ];
       
   443 	msgBody[0] = CMD_FTP_STOR;
       
   444 	*((DWORD*)(msgBody + 1)) = fileSize;
       
   445 	msgBody[5] = textLen;
       
   446 	memcpy( msgBody + 6, arg, 2*textLen );
       
   447 
       
   448 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   449 
       
   450 	handler->SendHtiMessage(HTI_UID, msgBody, bodyLen, CONTROL_PRIORITY );
       
   451 
       
   452 	delete[] msgBody;
       
   453 	return resultCode;
       
   454 }
       
   455 
       
   456 int ns1__putFile(struct soap* soap,
       
   457 				 struct ns1__HtiSoapAttachment *file,
       
   458 				 wchar_t *targetPath,
       
   459 				 struct ns1__putFileResponse *out)
       
   460 {
       
   461 	int resultCode;
       
   462 	//send attachment as several hti ftp data messages
       
   463 	if ( file == NULL || targetPath == NULL )
       
   464 	{
       
   465 		soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
   466 		return SOAP_FAULT;
       
   467 	}
       
   468 	else if ( file->href != NULL )
       
   469 	{
       
   470 		for (soap_multipart::iterator attachment = (*soap).dime.begin();
       
   471 				attachment != (*soap).dime.end(); ++attachment) 
       
   472 		{
       
   473 			if ( (*attachment).id && strcmp((*attachment).id, file->href)==0 )
       
   474 			{
       
   475 				resultCode = sendSTOR(soap, targetPath, (*attachment).size );
       
   476 				if ( resultCode != SOAP_OK )
       
   477 				{
       
   478 					return resultCode;
       
   479 				}
       
   480 				resultCode = receiveFtpOk(soap, g_ftpHtiTimeoutControl);
       
   481 				if ( resultCode != SOAP_OK )
       
   482 				{
       
   483 					return resultCode;
       
   484 				}
       
   485 				resultCode = sendBinary( soap, (*attachment).ptr, (*attachment).size );
       
   486 				if ( resultCode != SOAP_OK )
       
   487 				{
       
   488 					return resultCode;
       
   489 				}
       
   490 				return receiveFtpOk(soap, g_ftpHtiTimeoutData); 
       
   491 			}
       
   492 		} 
       
   493 	}
       
   494 	else
       
   495 	{
       
   496 		soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
   497 		return SOAP_FAULT;
       
   498 	}
       
   499 
       
   500 	soap->error = soap_sender_fault(soap, "Invalid attachment", NULL);
       
   501 	resultCode = SOAP_FAULT;
       
   502 
       
   503 	return resultCode;
       
   504 }
       
   505 
       
   506 int ns1__getFile(struct soap* soap,
       
   507 				 wchar_t *filePath,
       
   508 				 struct ns1__getFileResponse &r)
       
   509 {
       
   510 	int resultCode = SOAP_OK;
       
   511 
       
   512 	if ( filePath == NULL )
       
   513 	{
       
   514 		soap->error = soap_sender_fault(soap, "Invalid arguments", NULL);
       
   515 		return SOAP_FAULT;
       
   516 	}
       
   517 
       
   518 	resultCode = sendHtiMessage( soap, CMD_FTP_RETR, filePath );
       
   519 
       
   520 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   521 
       
   522     //receive FILESIZE message
       
   523 	DWORD fileSize = 0;
       
   524 	if (handler->WaitForHtiMessage(g_ftpHtiTimeoutControl) )
       
   525 	{
       
   526 		if ( !handler->IsReceivedHtiError() )
       
   527 		{
       
   528 			DWORD bodySize = handler->ReceivedHtiMessageBodySize();
       
   529 			BYTE* body = (BYTE*)(handler->ReceivedHtiMessageBody());
       
   530 
       
   531 			if ( bodySize == 5 && body[0] == CMD_FTP_FILESIZE )
       
   532 			{
       
   533 				fileSize = *((DWORD*)(body + 1));
       
   534 			}
       
   535 			else
       
   536 			{
       
   537 				soap->error = soap_receiver_fault(soap,
       
   538 												"Received invalid hti message", NULL);
       
   539 				return SOAP_FAULT;
       
   540 			}
       
   541 		}
       
   542 		else
       
   543 		{
       
   544 			handler->SendSoapFaultFromReceivedHtiError();
       
   545 			return SOAP_FAULT;
       
   546 		}
       
   547 	}
       
   548 	else
       
   549 	{
       
   550 		soap->error = soap_receiver_fault(soap,
       
   551 										  "Failed receive hti message", NULL);
       
   552 		return SOAP_FAULT;
       
   553 	}
       
   554 	
       
   555 	//receive data messages
       
   556 	DWORD receivedFile = 0;
       
   557 	char* body = (char*)soap_malloc(soap, fileSize );
       
   558 
       
   559 	while ( receivedFile < fileSize )
       
   560 	{
       
   561 		if (handler->WaitForHtiMessage(g_ftpHtiTimeoutData) )
       
   562 		{
       
   563 			if ( !handler->IsReceivedHtiError() )
       
   564 			{
       
   565 				//extract response
       
   566 				DWORD bodySize = handler->ReceivedHtiMessageBodySize();
       
   567 				memcpy( body + receivedFile,
       
   568 					   (char*)(handler->ReceivedHtiMessageBody()),
       
   569 					   bodySize );
       
   570 				receivedFile += bodySize;
       
   571 			}
       
   572 			else
       
   573 			{
       
   574 				handler->SendSoapFaultFromReceivedHtiError();
       
   575 				return SOAP_FAULT;
       
   576 			}
       
   577 		}
       
   578 		else
       
   579 		{
       
   580 			soap->error = soap_receiver_fault(soap,
       
   581 											"Failed receive hti message", NULL);
       
   582 			return SOAP_FAULT;
       
   583 		}
       
   584 	}
       
   585 
       
   586 	//create attachment
       
   587 	int pLen = (int)wcslen(filePath)+1;
       
   588 	r._return.href = (char*)soap_malloc(soap, pLen );
       
   589 	WideCharToMultiByte(CP_ACP, 0, filePath, -1, r._return.href, pLen, NULL, NULL);
       
   590 
       
   591 	r._return.mimeType = (char*)soap_malloc(soap, strlen(fileMimeType)+1 );
       
   592 	strcpy( r._return.mimeType, fileMimeType );
       
   593 
       
   594 	//pass result as DIME attachment
       
   595 	soap_set_dime(soap);
       
   596 	resultCode = soap_set_dime_attachment(soap,
       
   597 								body,
       
   598 								fileSize,
       
   599 								r._return.mimeType,
       
   600 								r._return.href,
       
   601 								0, NULL);
       
   602 	if (  resultCode != SOAP_OK )
       
   603 	{
       
   604 		//Util::Debug("soap_set_dime_attachment failed");
       
   605 		soap_clr_dime(soap);
       
   606 		soap->error = soap_receiver_fault(soap,
       
   607 								"Failed create DIME attachment", NULL);
       
   608 	}
       
   609 	
       
   610 	return resultCode;
       
   611 }
       
   612 
       
   613 int ns1__cancelFileTransfer(struct soap* soap,
       
   614 							void *_,
       
   615 							struct ns1__cancelFileTransferResponse *out)
       
   616 {
       
   617 	int resultCode = sendHtiMessage( soap, CMD_FTP_CANCEL, NULL );
       
   618 	if ( resultCode != SOAP_OK )
       
   619 	{
       
   620 		return resultCode;
       
   621 	}
       
   622 	
       
   623 	return receiveFtpOk(soap, g_ftpHtiTimeoutControl);
       
   624 }
       
   625 
       
   626 int ns1__listFiles(struct soap* soap,
       
   627 				   wchar_t *targetDir,
       
   628 				   struct ArrayOfStrings &dirs)
       
   629 {
       
   630 	return sendHtiMessageAndReceiveStringArray( soap, CMD_FTP_LIST, targetDir, dirs );
       
   631 }
       
   632 
       
   633 int ns1__listFilesSizes(struct soap* soap,
       
   634                         wchar_t *targetDir,
       
   635                         struct ArrayOfHtiFileInfos &fileInfos)
       
   636 {
       
   637     return sendHtiMessageAndReceiveHtiFileInfoArray( soap, CMD_FTP_LISTSIZE,
       
   638                                                      targetDir, fileInfos );
       
   639 }
       
   640 
       
   641 int ns1__listDirs(struct soap* soap,
       
   642 				  wchar_t *targetDir,
       
   643 				  struct ArrayOfStrings &files)
       
   644 {
       
   645 	return sendHtiMessageAndReceiveStringArray( soap, CMD_FTP_LISTDIR, targetDir, files );
       
   646 }
       
   647 
       
   648 int ns1__createDir(struct soap* soap,
       
   649 				   wchar_t *targetDir,
       
   650 				   struct ns1__createDirResponse *out)
       
   651 {
       
   652 	return sendHtiMessageAndReceiveOK( soap, CMD_FTP_MKD, targetDir );
       
   653 }
       
   654 
       
   655 int ns1__deleteDir(struct soap* soap,
       
   656 				   wchar_t *targetDir,
       
   657 				   struct ns1__deleteDirResponse *out)
       
   658 {
       
   659 	return sendHtiMessageAndReceiveOK( soap, CMD_FTP_RMD, targetDir );
       
   660 }
       
   661 
       
   662 int ns1__deleteFile(struct soap* soap,
       
   663 					wchar_t *targetFile,
       
   664 					struct ns1__deleteFileResponse *out)
       
   665 {
       
   666 	return sendHtiMessageAndReceiveOK( soap, CMD_FTP_DELE, targetFile );
       
   667 }
       
   668 
       
   669 
       
   670 int send_receive_htimessage(struct soap* soap, BYTE **msgBody, int &msgBodyLen, int timeout)
       
   671 {
       
   672 	// Send the message to symbian side
       
   673 	HtiSoapHandlerInterface* handler = static_cast<HtiSoapHandlerInterface*>(soap->user);
       
   674 	handler->SendHtiMessage(HTI_UID, *msgBody, msgBodyLen, CONTROL_PRIORITY);
       
   675 
       
   676 	// Clean these up for received HTI message
       
   677 	delete *msgBody;
       
   678 	*msgBody = NULL;
       
   679 	msgBodyLen = 0;
       
   680 
       
   681 	// ...and wait for OK or error msg
       
   682 	if (handler->WaitForHtiMessage(timeout))
       
   683 	{
       
   684 		if ( !handler->IsReceivedHtiError() )
       
   685 		{
       
   686 			// Get received message
       
   687 			*msgBody = (BYTE*) handler->ReceivedHtiMessageBody();
       
   688 			msgBodyLen = handler->ReceivedHtiMessageBodySize();
       
   689 			return ERROR_SUCCESS;
       
   690 		}
       
   691 		else
       
   692 		{
       
   693 			handler->SendSoapFaultFromReceivedHtiError();
       
   694 			return ERROR_GEN_FAILURE;
       
   695 		}
       
   696 	}
       
   697 	// ...or timeout
       
   698 	else
       
   699 	{
       
   700 		soap->error = soap_receiver_fault(soap, "HtiGateway", "No response from symbian side");
       
   701 		return WAIT_TIMEOUT;
       
   702 	}
       
   703 }
       
   704 
       
   705 int ns1__setForcedOperations(struct soap* soap,
       
   706 							 bool state, 
       
   707                              struct ns1__setForcedOperationsResponse *out)
       
   708 {
       
   709 	int msgBodyLen = 1+1;
       
   710 	BYTE *msgBody = new BYTE[msgBodyLen];
       
   711 	msgBody[0] = CMD_FTP_SETFORCE;
       
   712 	msgBody[1] = (BYTE) state;
       
   713 
       
   714 	if( send_receive_htimessage(soap, &msgBody, msgBodyLen, g_ftpHtiTimeoutControl) 
       
   715         != ERROR_SUCCESS )
       
   716 		return SOAP_FAULT;
       
   717 
       
   718 	if(msgBody[0] != CMD_FTP_OK)
       
   719 	{
       
   720 		soap->error = soap_receiver_fault(soap, "HtiError", "Operation failed. Response code not ok.");
       
   721 		return SOAP_FAULT;
       
   722 	}
       
   723 
       
   724 	return SOAP_OK;
       
   725 }
       
   726 
       
   727 int ns1__fileChecksum(struct soap* soap, 
       
   728 					  unsigned char algorithmId, 
       
   729 					  wchar_t *targetFile, 
       
   730                       struct ArrayOfBytes &checksumByteArray)
       
   731 {
       
   732 	int targetFileLen = targetFile ? (int) wcslen(targetFile) : 0;
       
   733 	if(targetFileLen == 0)
       
   734 	{
       
   735 		soap->error = soap_receiver_fault(soap, "HtiGateway", "targetFile parameter missing");
       
   736 		return SOAP_FAULT;
       
   737 	}
       
   738 
       
   739 	int msgBodyLen = 1+1+1+targetFileLen*2;
       
   740 	BYTE *msgBody = new BYTE[msgBodyLen];
       
   741 	msgBody[0] = CMD_FTP_CHECKSUM_U;
       
   742 	msgBody[1] = algorithmId;
       
   743 	msgBody[2] = targetFileLen;
       
   744 	memcpy(msgBody+3, targetFile, targetFileLen*2);
       
   745 
       
   746 	if( send_receive_htimessage(soap, &msgBody, msgBodyLen, g_ftpHtiTimeoutControl) 
       
   747         != ERROR_SUCCESS )
       
   748 		return SOAP_FAULT;
       
   749 
       
   750 	checksumByteArray.__size = msgBodyLen;
       
   751 	checksumByteArray.__ptr = (BYTE*)soap_malloc(soap, sizeof(BYTE)*checksumByteArray.__size );
       
   752 
       
   753 	memcpy(checksumByteArray.__ptr, msgBody, msgBodyLen);
       
   754 
       
   755 	return SOAP_OK;
       
   756 }
       
   757 
       
   758 int ns1__format(struct soap* soap,
       
   759 				unsigned char drive, 
       
   760                 unsigned char formatMode,
       
   761                 struct ns1__formatResponse *out)
       
   762 {
       
   763 	int msgBodyLen = 3;
       
   764 	BYTE *msgBody = new BYTE[msgBodyLen];
       
   765 	msgBody[0] = CMD_FTP_FORMAT;
       
   766 	msgBody[1] = (BYTE) drive;
       
   767     msgBody[2] = (BYTE) formatMode;
       
   768 
       
   769     // might take long with large memory cards
       
   770 	if( send_receive_htimessage(soap, &msgBody, msgBodyLen, g_ftpHtiTimeoutData) 
       
   771         != ERROR_SUCCESS )
       
   772 		return SOAP_FAULT;
       
   773 
       
   774 	if(msgBody[0] != CMD_FTP_OK)
       
   775 	{
       
   776 		soap->error = soap_receiver_fault(soap, "HtiError", "Operation failed. Response code not ok.");
       
   777 		return SOAP_FAULT;
       
   778 	}
       
   779 
       
   780 	return SOAP_OK;
       
   781 }
       
   782 
       
   783 int ns1__listDrives(struct soap* soap,
       
   784                     void *_,
       
   785                     struct ArrayOfHtiDriveInfos &driveInfos)
       
   786 {
       
   787     return sendHtiMessageAndReceiveHtiDriveInfoArray( soap, CMD_FTP_LISTDRIVES,
       
   788                                                      driveInfos );
       
   789 }
       
   790