testconns/statdesktop/desktop/testsource/dlltestermt/src/dlltest_worker.cpp
changeset 4 b8d1455fddc0
equal deleted inserted replaced
2:73b88125830c 4:b8d1455fddc0
       
     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 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "stdafx.h"
       
    22 #include <statcommon.h>
       
    23 #include "dlltest_worker.h"
       
    24 #include <stattask.h>
       
    25 #include <statexp.h>
       
    26 
       
    27 //////////////////////////////////////////////////////////////////////////
       
    28 // Write the contents of a screenshot out to file
       
    29 bool WriteBitmap(char *file, TBitmapFileHeader *fileheader, TBitmapInfoHeader * bmpHeader, char *bmpBits, unsigned long lSize)
       
    30 {
       
    31 	bool success = false;
       
    32 
       
    33 	// write the whole lot out to file
       
    34 	HANDLE infile;
       
    35 	if (INVALID_HANDLE_VALUE != (infile = CreateFile(file,
       
    36 										   GENERIC_WRITE,
       
    37 										   0, 
       
    38 										   NULL, 
       
    39 										   CREATE_ALWAYS,
       
    40 										   FILE_ATTRIBUTE_NORMAL,
       
    41 										   0)))
       
    42 	{
       
    43 		DWORD dwBytesWritten = 0;
       
    44 
       
    45 		// write the file info
       
    46 		if (WriteFile(infile, (LPVOID *)fileheader, sizeof(TBitmapFileHeader), &dwBytesWritten, NULL) &&
       
    47 			dwBytesWritten == sizeof(TBitmapFileHeader))
       
    48 		{
       
    49 			// write the bitmap info
       
    50 			if (WriteFile(infile, (LPVOID *)bmpHeader, sizeof(TBitmapInfoHeader), &dwBytesWritten, NULL) &&
       
    51 				dwBytesWritten == sizeof(TBitmapInfoHeader))
       
    52 			{
       
    53 				// write the bitmap data
       
    54 				if (WriteFile(infile, (LPVOID *)bmpBits, lSize, &dwBytesWritten, NULL) &&
       
    55 					dwBytesWritten == lSize)
       
    56 				{
       
    57 					success = true;
       
    58 				}
       
    59 			}
       
    60 		}
       
    61 
       
    62 		CloseHandle(infile);
       
    63 	}
       
    64 
       
    65 	return success;
       
    66 }
       
    67 
       
    68 
       
    69 //////////////////////////////////////////////////////////////////////////
       
    70 // This thread function handles the processing of a script by a single
       
    71 // connection
       
    72 DWORD WINAPI
       
    73 STATDeviceThreadProcedure(LPVOID lpParameter)
       
    74 {
       
    75 	DWORD valid = true;
       
    76 
       
    77 	STATTASKINFO *pWorker = (STATTASKINFO *)lpParameter;
       
    78 	if (pWorker)
       
    79 	{
       
    80 		int h = ConnectMT(pWorker->connectType, pWorker->szAddress);
       
    81 		if (h)
       
    82 		{
       
    83 			printf("Version %s\r\n", Version());
       
    84 			valid = SetCommandLogging(h, pWorker->pLogfile, NULL);
       
    85 			valid = SetCommandDelay(h, pWorker->delay);
       
    86 
       
    87 			if (valid)
       
    88 				if (pWorker->verify)
       
    89 					valid = SetImageVerification(h, pWorker->pRefdir, pWorker->removeImages?true:false, pWorker->fudge);
       
    90 
       
    91 			if (valid)
       
    92 			{
       
    93 				for (int i=0;i<pWorker->iterations;i++)
       
    94 				{
       
    95 					//valid = SendRawCommand(h, pWorker->pCommand);
       
    96 					valid = SendCommandFile(h, pWorker->pCommand);
       
    97 					if (!valid)
       
    98 					{
       
    99 						printf("[%d] bad command(%s)\r\n", h, GetError(h));
       
   100 						break;
       
   101 					}
       
   102 
       
   103 					/*struct TBitmapFileHeader *pFile = NULL;
       
   104 					struct TBitmapInfoHeader *pInfo = NULL;
       
   105 					char *pData = NULL;
       
   106 					unsigned long lSize = 0;
       
   107 
       
   108 					valid = GetSnapshot(h, &pFile, &pInfo, &pData, &lSize);
       
   109 					if (valid)
       
   110 						valid = WriteBitmap(pWorker->pSnapshot, pFile, pInfo, pData, lSize);
       
   111 					if (!valid)
       
   112 					{
       
   113 						printf("[%d] bad command(%s)\r\n", h, GetError(h));
       
   114 						break;
       
   115 					}*/
       
   116 				}
       
   117 			}
       
   118 
       
   119 			Disconnect(h);
       
   120 		}
       
   121 		else
       
   122 			printf("Bad connection (%s)\r\n", GetError());
       
   123 	}
       
   124 	else
       
   125 		printf("Bad parameter\r\n");
       
   126 
       
   127 	return (DWORD)valid;
       
   128 }
       
   129 
       
   130 
       
   131 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   132 // Constructor
       
   133 dlltest_worker::dlltest_worker()
       
   134 : iterations(1), delay(100), lConectionCount(0), verify(0), removeImages(0), fudge(0)
       
   135 {
       
   136 	memset(szLogfile, 0, sizeof(szLogfile));
       
   137 	memset(szCommand, 0, sizeof(szCommand));
       
   138 	memset(szSnapshot, 0, sizeof(szSnapshot));
       
   139 	memset(szRefdir, 0, sizeof(szRefdir));
       
   140 }
       
   141 
       
   142 
       
   143 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   144 // Destructor
       
   145 dlltest_worker::~dlltest_worker()
       
   146 {
       
   147 	Finish();
       
   148 }
       
   149 
       
   150 
       
   151 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   152 // Get the settings and start the worker threads
       
   153 bool dlltest_worker::Prepare()
       
   154 {
       
   155 	CStatIniFile statIniFile;
       
   156 	statIniFile.SetIniFileName(STAT_INI_NAME);
       
   157 	if(statIniFile.SectionExists(ST_TEST_KEY) )
       
   158 	{
       
   159 		CString setting;
       
   160 		TCHAR szBuffer[2048];
       
   161 		long lCount = 0;
       
   162 		*(szBuffer) = (TCHAR)0;
       
   163 
       
   164 		setting.Empty();
       
   165 		setting=statIniFile.GetKeyValue(ST_ITERATIONS,ST_TEST_KEY);
       
   166 		if(!setting.IsEmpty())
       
   167 		{
       
   168 			if (_ttoi(setting)<1)
       
   169 				setting=_T("1");
       
   170 
       
   171 			iterations=_ttoi(setting);
       
   172 		}
       
   173 
       
   174 		setting=statIniFile.GetKeyValue(ST_DELAY,ST_TEST_KEY);
       
   175 		if(!setting.IsEmpty())
       
   176 		{
       
   177 			int iPos = _ttoi(setting);
       
   178 			if (iPos < 100)
       
   179 				setting= _T("100");
       
   180 
       
   181 			if (iPos > 30000)
       
   182 				setting= _T("30000");
       
   183 
       
   184 			delay=_ttoi(setting);
       
   185 		}
       
   186 
       
   187 	// logging settings
       
   188 		strcpy(szLogfile,(LPCSTR)statIniFile.GetKeyValue(ST_LOGFILE,ST_TEST_KEY));
       
   189 
       
   190 	// raw command
       
   191 		strcpy(szCommand,(LPCSTR)statIniFile.GetKeyValue(ST_CMDFILE,ST_TEST_KEY));
       
   192 
       
   193 	// snapshot settings
       
   194 		strcpy(szSnapshot,(LPCSTR)statIniFile.GetKeyValue(ST_SNAPSHOT,ST_TEST_KEY));
       
   195 
       
   196 	// image verification settings
       
   197 		verify=0;
       
   198 		setting=statIniFile.GetKeyValue(ST_CHKVERIF,ST_TEST_KEY);
       
   199 		if(!setting.IsEmpty())
       
   200 		{
       
   201 			verify=_ttol(setting);
       
   202 
       
   203 		}
       
   204 		removeImages=0;
       
   205 		setting=statIniFile.GetKeyValue(ST_VERIFYREMOVEIMAGES,ST_TEST_KEY);
       
   206 		if(!setting.IsEmpty())
       
   207 		{
       
   208 			removeImages=_ttol(setting);
       
   209 
       
   210 		}
       
   211 		strcpy(szRefdir,(LPCSTR)	statIniFile.GetKeyValue(ST_REFDIR,ST_TEST_KEY));
       
   212 		setting.Empty();
       
   213 		setting=statIniFile.GetKeyValue(ST_FUDGE,ST_TEST_KEY);
       
   214 		if(!setting.IsEmpty())
       
   215 			fudge = _ttoi(setting);
       
   216 
       
   217 	// connection settings
       
   218 
       
   219 		lConectionCount = 0;
       
   220 		setting.Empty();
       
   221 		setting=statIniFile.GetKeyValue(ST_CONNECTIONIDX,ST_TEST_KEY);
       
   222 		if(!setting.IsEmpty())
       
   223 		{
       
   224 			lConectionCount = _ttol(setting);
       
   225 
       
   226 		}
       
   227 		if(statIniFile.SectionExists(ST_CONNECTION_LIST) )
       
   228 		{
       
   229 			for( int i=0;i<lConectionCount;++i)
       
   230 			{
       
   231 				TCHAR buf[6];
       
   232 				_ltot(i+1, buf, 10);
       
   233 				setting.Empty();
       
   234 				setting=statIniFile.GetKeyValue(buf,ST_CONNECTION_LIST);
       
   235 				if(!setting.IsEmpty())
       
   236 					strcpy(szConections[i],(LPCSTR)(setting));
       
   237 			}
       
   238 		}
       
   239 	}
       
   240 	// turn on high-level logging
       
   241 	SetConnectionLogging(szLogfile);
       
   242 
       
   243 	// start the threads
       
   244 	int i;
       
   245 	for (i=0;i<lConectionCount;i++)
       
   246 	{
       
   247 		GetTaskInfo(i);
       
   248 		pDeviceTask[i] = new STATTask(STATDeviceThreadProcedure, &TaskInfo[i]);
       
   249 		if (pDeviceTask[i])
       
   250 		{
       
   251 			if (!pDeviceTask[i]->Start())
       
   252 			{
       
   253 				delete pDeviceTask[i];
       
   254 				pDeviceTask[i] = NULL;
       
   255 			}
       
   256 		}
       
   257 	}
       
   258 
       
   259 	// wait a bit to allow the threads to start
       
   260 	Sleep(500);
       
   261 
       
   262 	return true;
       
   263 }
       
   264 
       
   265 
       
   266 /////////////////////////////////////////////////////////////////////////
       
   267 // Check to see if all our jobs have finished
       
   268 int dlltest_worker::WorkPending()
       
   269 {
       
   270 	int i;
       
   271 	int count = 0;
       
   272 	for (i=0;i<lConectionCount;i++)
       
   273 	{
       
   274 		if (pDeviceTask[i] && pDeviceTask[i]->StillActive())
       
   275 		{
       
   276 			count++;
       
   277 		}
       
   278 	}
       
   279 
       
   280 	return count;
       
   281 }
       
   282 
       
   283 /////////////////////////////////////////////////////////////////////////
       
   284 // Release any resources
       
   285 void dlltest_worker::Finish()
       
   286 {
       
   287 	// kill the threads
       
   288 	int i;
       
   289 	for (i=0;i<lConectionCount;i++)
       
   290 	{
       
   291 		if (pDeviceTask[i])
       
   292 		{
       
   293 			pDeviceTask[i]->Kill();
       
   294 			delete pDeviceTask[i];
       
   295 			pDeviceTask[i] = NULL;
       
   296 		}
       
   297 	}
       
   298 
       
   299 	// turn off high-level logging
       
   300 	CloseConnectionLogging();
       
   301 }
       
   302 
       
   303 
       
   304 /////////////////////////////////////////////////////////////////////////
       
   305 // PRIVATE METHODS
       
   306 /////////////////////////////////////////////////////////////////////////
       
   307 
       
   308 /////////////////////////////////////////////////////////////////////////
       
   309 // Set task information for a single thread
       
   310 void dlltest_worker::GetTaskInfo(int index)
       
   311 {
       
   312 	STATCONNECTTYPE type;
       
   313 	char *addr;
       
   314 	ParseConnection(szConections[index], &type, &addr);
       
   315 
       
   316 	TaskInfo[index].connectType = type;
       
   317 	TaskInfo[index].delay = delay;
       
   318 	TaskInfo[index].fudge = fudge;
       
   319 	TaskInfo[index].iterations = iterations;
       
   320 	TaskInfo[index].removeImages = removeImages;
       
   321 	TaskInfo[index].pCommand = szCommand;
       
   322 	TaskInfo[index].pLogfile = szLogfile;
       
   323 	TaskInfo[index].pRefdir = szRefdir;
       
   324 	TaskInfo[index].pSnapshot = szSnapshot;
       
   325 	TaskInfo[index].verify = verify;
       
   326 	strcpy(TaskInfo[index].szAddress, addr);
       
   327 }
       
   328 
       
   329 
       
   330 //////////////////////////////////////////////////////////////////////////////////////
       
   331 // Split a connection registry entry into its respective parts
       
   332 void dlltest_worker::ParseConnection(char *pConnection, STATCONNECTTYPE *pType, char **ppAddress)
       
   333 {
       
   334 	(*pType) = SymbianInvalid;
       
   335 	(*ppAddress) = NULL;
       
   336 
       
   337 	static char szConnection[256];
       
   338 	memset(szConnection, 0, sizeof(szConnection));
       
   339 	strcpy(szConnection, pConnection);
       
   340 
       
   341 	char *p = strchr(szConnection, ':');
       
   342 	if (p)
       
   343 	{
       
   344 		(*p) = (char)0;
       
   345 		(*ppAddress) = p + 1;
       
   346 
       
   347 		if (stricmp(szConnection, "SymbianSocket") == 0)
       
   348 		{
       
   349 			(*pType) = SymbianSocket;
       
   350 		}
       
   351 		else if (stricmp(szConnection, "SymbianSerial") == 0)
       
   352 		{
       
   353 			(*pType) = SymbianSerial;
       
   354 		}
       
   355 		else if (stricmp(szConnection, "SymbianInfrared") == 0)
       
   356 		{
       
   357 			(*pType) = SymbianInfrared;
       
   358 		}
       
   359 		else if (stricmp(szConnection, "SymbianBluetooth") == 0)
       
   360 		{
       
   361 			(*pType) = SymbianBluetooth;
       
   362 		}
       
   363 		else if (stricmp(szConnection, "SymbianUSB") == 0)
       
   364 		{
       
   365 			(*pType) = SymbianUSB;
       
   366 		}
       
   367 
       
   368 		(*p) = ':';
       
   369 	}
       
   370 }
       
   371 
       
   372