connectivity/com.nokia.tcf/native/TCFNative/TCFClient/TCFCppApi.cpp
changeset 60 9d2210c8eed2
equal deleted inserted replaced
59:c892c53c664e 60:9d2210c8eed2
       
     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 the License "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 #include "stdafx.h"
       
    19 #include "TCFCppApi.h"
       
    20 #include "ServerClient.h"
       
    21 #include "ClientManager.h"
       
    22 #include "TCConstants.h"
       
    23 #include <vector>
       
    24 
       
    25 extern CClientManager* gManager;
       
    26 #ifdef _DEBUG
       
    27 extern BOOL gDoLogging;
       
    28 extern char TCDebugMsg[100];
       
    29 #define TCDEBUGOPEN() if (gDoLogging) { gManager->m_DebugLog->WaitForAccess(); }
       
    30 #define TCDEBUGLOGS(s) if (gDoLogging) { sprintf(TCDebugMsg,"%s", s); gManager->m_DebugLog->log(TCDebugMsg); }
       
    31 #define TCDEBUGLOGA1(s, a1) if (gDoLogging) { sprintf(TCDebugMsg, s, a1); gManager->m_DebugLog->log(TCDebugMsg); }
       
    32 #define TCDEBUGLOGA2(s, a1, a2) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2); gManager->m_DebugLog->log(TCDebugMsg); }
       
    33 #define TCDEBUGLOGA3(s, a1, a2, a3) if (gDoLogging) { sprintf(TCDebugMsg, s, a1, a2, a3); gManager->m_DebugLog->log(TCDebugMsg); }
       
    34 #define TCDEBUGCLOSE() if (gDoLogging) { gManager->m_DebugLog->ReleaseAccess(); }
       
    35 #else
       
    36 #define TCDEBUGOPEN()
       
    37 #define TCDEBUGLOGS(s)
       
    38 #define TCDEBUGLOGA1(s, a1)
       
    39 #define TCDEBUGLOGA2(s, a1, a2)
       
    40 #define TCDEBUGLOGA3(s, a1, a2, a3)
       
    41 #define TCDEBUGCLOSE()
       
    42 #endif
       
    43 
       
    44 // for internal testing with 2.x USB TRK define this
       
    45 //#define FOR_2X_USB
       
    46 
       
    47 // client Ids connected for this C++ process
       
    48 static std::vector<long> *pcppClientIds = NULL;
       
    49 
       
    50 static long CheckClient(long id);
       
    51 static long CheckConnection(pTCFCppConnectData inConnection);
       
    52 static long CheckRealSerialSettings(pTCFCppConnectData inConnection);
       
    53 static long CheckMessageOptions(pTCFCppMessageOptions inMessageOptions);
       
    54 static long CheckMessageIds(pTCFCppMessageIds inMessageIds);
       
    55 static long CheckMessage(pTCFCppMessage inMessage);
       
    56 static void ConvertRealSerialSettingsToServer(pTCFCppConnectData inConnection, pRealSerialConnectData pData); 
       
    57 static void ConvertRealSerialSettingsToHost(pTCFCppConnectData inConnection, pRealSerialConnectData pData); 
       
    58 
       
    59 TCF_EXP long TCF_CALL TCFConnect(pTCFCppConnectData inConnection, pTCFCppMessageOptions inMessageOptions, pTCFCppMessageIds inMessageIds, long* outClientId)
       
    60 {
       
    61 	long ret = TCAPI_ERR_NONE;
       
    62 	ServerCommandData cmdrsp;
       
    63 	pServerCommandData pCmdrsp = &cmdrsp;
       
    64 
       
    65 	TCDEBUGOPEN();
       
    66 	TCDEBUGLOGS("TCFConnect\n");
       
    67 //	if (!gManager->IsServerRunning())
       
    68 //		return TCAPI_ERR_COMM_SERVER_RESPONSE_TIMEOUT;
       
    69 
       
    70 	gManager->m_Server->WaitforServerPipeAccess();
       
    71 
       
    72 	ret = CheckConnection(inConnection);
       
    73 #ifdef FOR_2X_USB
       
    74 	inMessageOptions->unWrapFormat = eTCFCppNone;
       
    75 #endif
       
    76 	if (ret == TCAPI_ERR_NONE)
       
    77 	{
       
    78 		ret = CheckMessageOptions(inMessageOptions);
       
    79 	}
       
    80 
       
    81 	if (ret == TCAPI_ERR_NONE)
       
    82 	{
       
    83 		ret = CheckMessageIds(inMessageIds);
       
    84 	}
       
    85 
       
    86 	if (ret == TCAPI_ERR_NONE)
       
    87 	{
       
    88 		if (strcmp(inConnection->connectType, "tcp") == 0)
       
    89 		{
       
    90 			char* pAddress = inConnection->tcpSettings.ipAddress;
       
    91 			char* pPort = inConnection->tcpSettings.ipPort;
       
    92 
       
    93 			pCmdrsp->command = eCmdConnect;
       
    94 			strncpy(pCmdrsp->connectSettings.connectType, inConnection->connectType, MAX_CONNECTION_TYPE);
       
    95 			strncpy(pCmdrsp->connectSettings.tcpSettings.ipAddress, pAddress, MAX_IPADDRESS_SIZE);
       
    96 			strncpy(pCmdrsp->connectSettings.tcpSettings.ipPort, pPort, MAX_PORT_SIZE);
       
    97 
       
    98 			pCmdrsp->connectSettings.retryInterval = inConnection->retryInterval;
       
    99 			pCmdrsp->connectSettings.retryTimeout = inConnection->retryTimeout;
       
   100 			strncpy(pCmdrsp->connectSettings.decodeFormat, inConnection->decodeFormat, MAX_DECODE_FORMAT);
       
   101 			pCmdrsp->clientOptions.unWrapFormat = inMessageOptions->unWrapFormat;
       
   102 			pCmdrsp->clientOptions.ostVersion = inMessageOptions->ostVersion;
       
   103 		}
       
   104 		else if (strcmp(inConnection->connectType, "virtualserial") == 0)
       
   105 		{
       
   106 			char* pComPort = inConnection->virtualSerialSettings.comPort;
       
   107 
       
   108 			pCmdrsp->command = eCmdConnect;
       
   109 			strncpy(pCmdrsp->connectSettings.connectType, inConnection->connectType, MAX_CONNECTION_TYPE);
       
   110 			strncpy(pCmdrsp->connectSettings.virtualSerialSettings.comPort, pComPort, MAX_COMPORT_SIZE);
       
   111 
       
   112 			pCmdrsp->connectSettings.retryInterval = inConnection->retryInterval;
       
   113 			pCmdrsp->connectSettings.retryTimeout = inConnection->retryTimeout;
       
   114 			strncpy(pCmdrsp->connectSettings.decodeFormat, inConnection->decodeFormat, MAX_DECODE_FORMAT);
       
   115 			pCmdrsp->clientOptions.unWrapFormat = inMessageOptions->unWrapFormat;
       
   116 			pCmdrsp->clientOptions.ostVersion = inMessageOptions->ostVersion;
       
   117 		}
       
   118 		else if (strcmp(inConnection->connectType, "serial") == 0)
       
   119 		{
       
   120 			char* pComPort = inConnection->realSerialSettings.comPort;
       
   121 
       
   122 			pCmdrsp->command = eCmdConnect;
       
   123 			strncpy(pCmdrsp->connectSettings.connectType, inConnection->connectType, MAX_CONNECTION_TYPE);
       
   124 			strncpy(pCmdrsp->connectSettings.realSerialSettings.comPort, pComPort, MAX_COMPORT_SIZE);
       
   125 
       
   126 			ConvertRealSerialSettingsToServer(inConnection, &pCmdrsp->connectSettings.realSerialSettings);
       
   127 
       
   128 			pCmdrsp->connectSettings.retryInterval = inConnection->retryInterval;
       
   129 			pCmdrsp->connectSettings.retryTimeout = inConnection->retryTimeout;
       
   130 			strncpy(pCmdrsp->connectSettings.decodeFormat, inConnection->decodeFormat, MAX_DECODE_FORMAT);
       
   131 			pCmdrsp->clientOptions.unWrapFormat = inMessageOptions->unWrapFormat;
       
   132 			pCmdrsp->clientOptions.ostVersion = inMessageOptions->ostVersion;
       
   133 		}
       
   134 		else
       
   135 		{
       
   136 			// Add other connections here
       
   137 		}
       
   138 		// send connect command
       
   139 		long id = 0;
       
   140 
       
   141 		if (ret == TCAPI_ERR_NONE)
       
   142 		{
       
   143 			gManager->m_Server->SendCommand(pCmdrsp);
       
   144 			gManager->m_Server->GetResponse(pCmdrsp);
       
   145 			if (pCmdrsp->response == eRspError)
       
   146 			{
       
   147 				ret = pCmdrsp->error;
       
   148 				TCDEBUGLOGA1("TCFConnect eCmdConnect: ret = %d\n", ret);
       
   149 			}
       
   150 			else
       
   151 			{
       
   152 				id = pCmdrsp->clientId;
       
   153 				TCDEBUGLOGA1("TCFConnect eCmdConnect: id = %d\n", id);
       
   154 			}
       
   155 		}
       
   156 		
       
   157 		// send message Ids to capture
       
   158 		if (ret == TCAPI_ERR_NONE)
       
   159 		{
       
   160 			pCmdrsp->command = eCmdSetMessageIds;
       
   161 			pCmdrsp->clientId = id;
       
   162 			pCmdrsp->number = inMessageIds->numberIds;
       
   163 			for (int i = 0; i < inMessageIds->numberIds; i++)
       
   164 			{
       
   165 				pCmdrsp->messageIds[i] = inMessageIds->messageIds[i];
       
   166 			}
       
   167 
       
   168 			gManager->m_Server->SendCommand(pCmdrsp);
       
   169 			gManager->m_Server->GetResponse(pCmdrsp);
       
   170 			if (pCmdrsp->response == eRspError)
       
   171 			{
       
   172 				ret = pCmdrsp->error;
       
   173 				TCDEBUGLOGA1("TCFConnect eCmdSetMessageIds: ret = %d\n", ret);
       
   174 			}
       
   175 			else
       
   176 			{
       
   177 				TCDEBUGLOGA1("TCFConnect eCmdSetMessageIds: ret = %d\n", ret);
       
   178 			}
       
   179 		}
       
   180 
       
   181 		// create input stream overflow temp file
       
   182 		// create input stream
       
   183 		if (ret == TCAPI_ERR_NONE)
       
   184 		{
       
   185 //			eTCPCppStreamOverflowOption overflowOption = inMessageOptions->streamOverflowOption;
       
   186 //			char* pFileName = inMessageOptions->overflowFile;
       
   187 			long streamSize = inMessageOptions->inputStreamSize;
       
   188 			CInputStream* stream = new CInputStream(NULL, streamSize, eTCPCppStreamOverflowOff, id);
       
   189 			stream->CreateStream();
       
   190 			gManager->AddInputStream(stream);
       
   191 
       
   192 			pCmdrsp->command = eCmdOpenStream;
       
   193 			pCmdrsp->clientId = id;
       
   194 			// TODO: implement message file in the future?
       
   195 			pCmdrsp->destinationOptions.destination = eDestinationInputStream;
       
   196 			pCmdrsp->destinationOptions.streamSize = streamSize;
       
   197 			pCmdrsp->destinationOptions.overFlowToFile = FALSE;//(overflowOption == eTCPCppStreamOverflowOn);
       
   198 //			if (pFileName != NULL)
       
   199 //			{
       
   200 //				strncpy(pCmdrsp->destinationOptions.destinationFile, pFileName, MAX_INPUTSTREAMPATH);
       
   201 //			}
       
   202 //			else
       
   203 			{
       
   204 				pCmdrsp->destinationOptions.destinationFile[0] = NULL;
       
   205 			}
       
   206 			gManager->m_Server->SendCommand(pCmdrsp);
       
   207 			gManager->m_Server->GetResponse(pCmdrsp);
       
   208 
       
   209 			if (pCmdrsp->response == eRspError)
       
   210 			{
       
   211 				ret = pCmdrsp->error;
       
   212 				TCDEBUGLOGA1("TCFConnect eCmdOpenStream: ret = %d\n", ret);
       
   213 			}
       
   214 			else
       
   215 			{
       
   216 				TCDEBUGLOGA1("TCFConnect eCmdOpenStream: ret = %d\n", ret);
       
   217 			}
       
   218 		}
       
   219 
       
   220 		// create error monitor
       
   221 		if (ret == TCAPI_ERR_NONE)
       
   222 		{
       
   223 			CErrorMonitor* monitor = new CErrorMonitor(id);
       
   224 			if (monitor != NULL)
       
   225 			{
       
   226 				monitor->CreateData();
       
   227 				gManager->AddErrorMonitor(monitor);
       
   228 			}
       
   229 		}
       
   230 
       
   231 		// start client capture
       
   232 		if (ret == TCAPI_ERR_NONE)
       
   233 		{
       
   234 			pCmdrsp->command = eCmdStart;
       
   235 			pCmdrsp->clientId = id;
       
   236 			gManager->m_Server->SendCommand(pCmdrsp);
       
   237 			gManager->m_Server->GetResponse(pCmdrsp);
       
   238 
       
   239 			if (pCmdrsp->response == eRspError)
       
   240 			{
       
   241 				ret = pCmdrsp->error;
       
   242 				TCDEBUGLOGA1("TCFConnect eCmdStart: ret = %d\n", ret);
       
   243 			}
       
   244 			else
       
   245 			{
       
   246 				TCDEBUGLOGA1("TCFConnect eCmdStart: ret = %d\n", ret);
       
   247 			}
       
   248 		}
       
   249 
       
   250 		if (ret == TCAPI_ERR_NONE)
       
   251 		{
       
   252 			TCDEBUGLOGA1("TCFConnect pcppClientIds: %x\n", pcppClientIds); 
       
   253 			if (pcppClientIds == NULL)
       
   254 			{
       
   255 				pcppClientIds = new std::vector<long>;
       
   256 				pcppClientIds->empty();
       
   257 			}
       
   258 			pcppClientIds->push_back(id);
       
   259 			*outClientId = id;
       
   260 			TCDEBUGLOGA1("TCFConnect pcppClientIds: size = %d\n", pcppClientIds->size());
       
   261 		}
       
   262 	}
       
   263 
       
   264 	gManager->m_Server->ReleaseServerPipeAccess();
       
   265 	TCDEBUGLOGA1("TCFConnect return ret=%d\n", ret);
       
   266 	TCDEBUGCLOSE();
       
   267 	return ret;
       
   268 }
       
   269 
       
   270 
       
   271 TCF_EXP long TCF_CALL TCFDisconnect(long inClientId)
       
   272 {
       
   273 	long ret = TCAPI_ERR_NONE;
       
   274 	ServerCommandData cmdrsp;
       
   275 	pServerCommandData pCmdrsp = &cmdrsp;
       
   276 
       
   277 	TCDEBUGOPEN();
       
   278 	TCDEBUGLOGA1("TCFDisconnect id=%d\n", inClientId);
       
   279 
       
   280 	gManager->m_Server->WaitforServerPipeAccess();
       
   281 	gManager->WaitForErrorMonitorListAccess();
       
   282 	gManager->WaitForStreamListAccess();
       
   283 
       
   284 	// check client ID
       
   285 	ret = CheckClient(inClientId);
       
   286 	TCDEBUGLOGA1("TCFDisconnect CheckClient: ret = %d\n", ret);
       
   287 	
       
   288 	// stop client
       
   289 	if (ret == TCAPI_ERR_NONE)
       
   290 	{
       
   291 		pCmdrsp->clientId = inClientId;
       
   292 		pCmdrsp->command = eCmdStop;
       
   293 		gManager->m_Server->SendCommand(pCmdrsp);
       
   294 		gManager->m_Server->GetResponse(pCmdrsp);
       
   295 	}
       
   296 
       
   297 	// close error monitor
       
   298 	TCDEBUGLOGS("TCFDisconnect clear error monitor\n");
       
   299 	CErrorMonitor *monitor = gManager->FindErrorMonitor(inClientId);
       
   300 	if (monitor != NULL)
       
   301 	{
       
   302 		gManager->RemoveErrorMonitor(monitor);
       
   303 		delete monitor;
       
   304 	}
       
   305 
       
   306 	// close input stream
       
   307 	TCDEBUGLOGS("TCFDisconnect remove input stream\n");
       
   308 	CInputStream* inputStream = gManager->FindInputStream(inClientId);
       
   309 	if (inputStream != NULL)
       
   310 	{
       
   311 		gManager->RemoveInputStream(inputStream);
       
   312 		delete inputStream;
       
   313 	}
       
   314 
       
   315 	// send disconnect
       
   316 	TCDEBUGLOGS("TCFDisconnect send disconnect\n");
       
   317 	if (ret == TCAPI_ERR_NONE)
       
   318 	{
       
   319 		pCmdrsp->command = eCmdDisconnect;
       
   320 		pCmdrsp->clientId = inClientId;
       
   321 
       
   322 		gManager->m_Server->SendCommand(pCmdrsp);
       
   323 		gManager->m_Server->GetResponse(pCmdrsp);
       
   324 		if (pCmdrsp->response == eRspError)
       
   325 		{
       
   326 			ret = pCmdrsp->error;
       
   327 		}
       
   328 	}
       
   329 	
       
   330 	TCDEBUGLOGS("TCFDisconnect clear erase id\n");
       
   331 	if (pcppClientIds != NULL && ret == TCAPI_ERR_NONE)
       
   332 	{
       
   333 		std::vector<long>::iterator idIter;
       
   334 		for (idIter = pcppClientIds->begin(); idIter != pcppClientIds->end(); idIter++)
       
   335 		{
       
   336 			if (*idIter == inClientId)
       
   337 			{
       
   338 				pcppClientIds->erase(idIter);
       
   339 				break;
       
   340 			}
       
   341 		}
       
   342 		if (pcppClientIds->size() == 0)
       
   343 		{
       
   344 			pcppClientIds->empty();
       
   345 			delete pcppClientIds;
       
   346 			pcppClientIds = NULL;
       
   347 		}
       
   348 	}
       
   349 
       
   350 	gManager->ReleaseStreamListAccess();
       
   351 	gManager->ReleaseErrorMonitorListAccess();
       
   352 
       
   353 	TCDEBUGLOGS("TCFDisconnect stop server\n");
       
   354 	gManager->m_Server->ReleaseServerPipeAccess();
       
   355 
       
   356 	TCDEBUGLOGA1("TCFDisconnect return ret=%d\n", ret);
       
   357 	TCDEBUGCLOSE();
       
   358 	return ret;
       
   359 }
       
   360 
       
   361 TCF_EXP long TCF_CALL TCFGetVersions(long inClientId, long& outNumberVersions, char** outVersions)
       
   362 {
       
   363 	long ret = TCAPI_ERR_FEATURE_NOT_IMPLEMENTED;
       
   364 	outNumberVersions = 0;
       
   365 
       
   366 	// check client ID
       
   367 
       
   368 	// get # versions from server
       
   369 
       
   370 	// get version[i] from server
       
   371 
       
   372 
       
   373 	return ret;
       
   374 }
       
   375 TCF_EXP long TCF_CALL TCFGetConnections(long& outNumberConnections, pTCFCppConnectData* outConnections)
       
   376 {
       
   377 	long ret = TCAPI_ERR_FEATURE_NOT_IMPLEMENTED;
       
   378 
       
   379 	outNumberConnections = 0;
       
   380 
       
   381 	return ret;
       
   382 }
       
   383 TCF_EXP long TCF_CALL TCFSendMessage(long inClientId, pTCFCppMessage inMessage, long inLength, BYTE* inData)
       
   384 {
       
   385 	long ret = TCAPI_ERR_NONE;
       
   386 	ServerCommandData cmdrsp; pServerCommandData pCmdrsp = &cmdrsp;
       
   387 
       
   388 #ifdef FOR_2X_USB
       
   389 	inMessage->useMyId = FALSE;
       
   390 #endif
       
   391 
       
   392 	TCDEBUGOPEN();
       
   393 	TCDEBUGLOGA1("TCFSendMessage id=%d\n", inClientId);
       
   394 
       
   395 	// check client ID
       
   396 	ret = CheckClient(inClientId);
       
   397 	TCDEBUGLOGA1("TCFSendMessage CheckClient: ret=%d\n", ret);
       
   398 
       
   399 	gManager->m_Server->WaitforServerPipeAccess();
       
   400 	// send message to server
       
   401 	if (ret == TCAPI_ERR_NONE)
       
   402 	{
       
   403 		ret = CheckMessage(inMessage);
       
   404 		if (ret == TCAPI_ERR_NONE)
       
   405 		{
       
   406 			pCmdrsp->command = eCmdSendMessage;
       
   407 			pCmdrsp->clientId = inClientId;
       
   408 			pCmdrsp->encodeOption = (inMessage->encodeFormat == eTCFCppEncodeNone) ? ENCODE_NO_FORMAT : ENCODE_FORMAT;
       
   409 			pCmdrsp->useMyId = inMessage->useMyId;
       
   410 			pCmdrsp->protocolVersion = inMessage->ostVersion;
       
   411 			pCmdrsp->myId = inMessage->myId;
       
   412 
       
   413 			gManager->m_Server->SendCommand(pCmdrsp, inLength, inData);
       
   414 			gManager->m_Server->GetResponse(pCmdrsp);
       
   415 
       
   416 			if (pCmdrsp->response == eRspError)
       
   417 			{
       
   418 				ret = pCmdrsp->error;
       
   419 				TCDEBUGLOGA1("TCFSendMessage eCmdSendMessage: ret=%d\n", ret);
       
   420 			}
       
   421 		}
       
   422 	}
       
   423 
       
   424 	gManager->m_Server->ReleaseServerPipeAccess();
       
   425 
       
   426 	TCDEBUGLOGA1("TCFSendMessage return ret=%d\n", ret);
       
   427 	TCDEBUGCLOSE();
       
   428 	return ret;
       
   429 }
       
   430 TCF_EXP long TCF_CALL TCFStart(long inClientId)
       
   431 {
       
   432 	long ret = TCAPI_ERR_NONE;
       
   433 
       
   434 	// check client Id
       
   435 	ret = CheckClient(inClientId);
       
   436 	if (ret == TCAPI_ERR_NONE)
       
   437 	{
       
   438 		gManager->m_Server->WaitforServerPipeAccess();
       
   439 
       
   440 		ServerCommandData cmdrsp; pServerCommandData pCmdrsp = &cmdrsp;
       
   441 
       
   442 		pCmdrsp->command = eCmdStart;
       
   443 		pCmdrsp->clientId = inClientId;
       
   444 
       
   445 		gManager->m_Server->SendCommand(pCmdrsp);
       
   446 		gManager->m_Server->GetResponse(pCmdrsp);
       
   447 
       
   448 		if (pCmdrsp->response == eRspError)
       
   449 		{
       
   450 			ret = pCmdrsp->error;
       
   451 		}
       
   452 		gManager->m_Server->ReleaseServerPipeAccess();
       
   453 
       
   454 	}
       
   455 
       
   456 	return ret;
       
   457 }
       
   458 
       
   459 TCF_EXP long TCF_CALL TCFStop(long inClientId)
       
   460 {
       
   461 	long ret = TCAPI_ERR_NONE;
       
   462 
       
   463 	// check client Id
       
   464 	ret = CheckClient(inClientId);
       
   465 	if (ret == TCAPI_ERR_NONE)
       
   466 	{
       
   467 		gManager->m_Server->WaitforServerPipeAccess();
       
   468 
       
   469 		ServerCommandData cmdrsp; pServerCommandData pCmdrsp = &cmdrsp;
       
   470 
       
   471 		pCmdrsp->command = eCmdStop;
       
   472 		pCmdrsp->clientId = inClientId;
       
   473 
       
   474 		gManager->m_Server->SendCommand(pCmdrsp);
       
   475 		gManager->m_Server->GetResponse(pCmdrsp);
       
   476 
       
   477 		if (pCmdrsp->response == eRspError)
       
   478 		{
       
   479 			ret = pCmdrsp->error;
       
   480 		}
       
   481 		gManager->m_Server->ReleaseServerPipeAccess();
       
   482 
       
   483 	}
       
   484 
       
   485 	return ret;
       
   486 }
       
   487 
       
   488 TCF_EXP long TCF_CALL TCFSetMessageIds(long inClientId, pTCFCppMessageIds inMessageIds)
       
   489 {
       
   490 	long ret = TCAPI_ERR_NONE;
       
   491 	ServerCommandData cmdrsp; pServerCommandData pCmdrsp = &cmdrsp;
       
   492 
       
   493 	// check client Id
       
   494 	ret = CheckClient(inClientId);
       
   495 
       
   496 	// check message ids
       
   497 	if (ret == TCAPI_ERR_NONE)
       
   498 	{
       
   499 		ret = CheckMessageIds(inMessageIds);
       
   500 	}
       
   501 
       
   502 	gManager->m_Server->WaitforServerPipeAccess();
       
   503 	// check client status
       
   504 	if (ret == TCAPI_ERR_NONE)
       
   505 	{
       
   506 		pCmdrsp->command = eCmdGetClientStatus;
       
   507 		pCmdrsp->clientId = inClientId;
       
   508 
       
   509 		gManager->m_Server->SendCommand(pCmdrsp);
       
   510 		gManager->m_Server->GetResponse(pCmdrsp);
       
   511 
       
   512 		if (pCmdrsp->response == eRspError)
       
   513 		{
       
   514 			ret = pCmdrsp->error;
       
   515 		}
       
   516 		else
       
   517 		{
       
   518 			if (pCmdrsp->clientStatus == eStarted)
       
   519 			{
       
   520 				ret = TCAPI_ERR_ROUTING_IN_PROGRESS;
       
   521 			}
       
   522 			else if (pCmdrsp->clientStatus == eUnknownClient)
       
   523 			{
       
   524 				ret = TCAPI_ERR_MEDIA_NOT_OPEN;
       
   525 			}
       
   526 		}
       
   527 	}
       
   528 
       
   529 	// set message ids
       
   530 	if (ret == TCAPI_ERR_NONE)
       
   531 	{
       
   532 		pCmdrsp->command = eCmdSetMessageIds;
       
   533 		pCmdrsp->clientId = inClientId;
       
   534 		pCmdrsp->number = inMessageIds->numberIds;
       
   535 		for (int i = 0; i < inMessageIds->numberIds; i++)
       
   536 		{
       
   537 			pCmdrsp->messageIds[i] = inMessageIds->messageIds[i];
       
   538 		}
       
   539 		gManager->m_Server->SendCommand(pCmdrsp);
       
   540 		gManager->m_Server->GetResponse(pCmdrsp);
       
   541 
       
   542 		if (pCmdrsp->response == eRspError)
       
   543 		{
       
   544 			ret = pCmdrsp->error;
       
   545 		}
       
   546 
       
   547 	}
       
   548 	gManager->m_Server->ReleaseServerPipeAccess();
       
   549 
       
   550 	return ret;
       
   551 }
       
   552 
       
   553 TCF_EXP long TCF_CALL TCFPollInputStream(long inClientId, long& outLength)
       
   554 {
       
   555 	long ret = TCAPI_ERR_NONE;
       
   556 	outLength = 0;
       
   557 
       
   558 	TCDEBUGOPEN();
       
   559 	TCDEBUGLOGA1("TCFPollInputStream id=%d\n", inClientId);
       
   560 
       
   561 	gManager->WaitForStreamListAccess();
       
   562 
       
   563 	// check client ID
       
   564 	ret = CheckClient(inClientId);
       
   565 	if (ret == TCAPI_ERR_NONE)
       
   566 	{
       
   567 		// get client's input stream
       
   568 		CInputStream* inputStream = gManager->FindInputStream(inClientId);
       
   569 
       
   570 		if (inputStream != NULL)
       
   571 		{
       
   572 			outLength = inputStream->GetNextMessageSize();
       
   573 		}
       
   574 		else
       
   575 		{
       
   576 			ret = TCAPI_ERR_INPUTSTREAM_CLOSED;
       
   577 		}
       
   578 	}
       
   579 
       
   580 	gManager->ReleaseStreamListAccess();
       
   581 
       
   582 	TCDEBUGLOGA2("TCFPollInputStream return ret=%d outLength=%d\n", ret, outLength);
       
   583 	TCDEBUGCLOSE();
       
   584 	return ret;
       
   585 }
       
   586 TCF_EXP long TCF_CALL TCFReadInputStream(long inClientId, pTCFCppMessage outMessage, long& inLength, BYTE* outData)
       
   587 {
       
   588 	long ret = TCAPI_ERR_NONE;
       
   589 
       
   590 	if (outData == NULL || inLength <= 0)
       
   591 		return ret;
       
   592 
       
   593 	gManager->WaitForStreamListAccess();
       
   594 
       
   595 	// check client ID
       
   596 	ret = CheckClient(inClientId);
       
   597 	if (ret == TCAPI_ERR_NONE)
       
   598 	{
       
   599 		// get client's input stream
       
   600 		CInputStream* inputStream = gManager->FindInputStream(inClientId);
       
   601 
       
   602 		if (inputStream != NULL)
       
   603 		{
       
   604 			DWORD length = inputStream->GetNextMessageSize();
       
   605 			if (length > inLength) length = inLength;
       
   606 			if (length > 0)
       
   607 			{
       
   608 				inLength = length;
       
   609 				inputStream->GetNextMessage(inLength, outData);
       
   610 
       
   611 			}
       
   612 			else
       
   613 			{
       
   614 				inLength = 0;
       
   615 			}
       
   616 		}
       
   617 		else
       
   618 		{
       
   619 			ret = TCAPI_ERR_INPUTSTREAM_CLOSED;
       
   620 		}
       
   621 	}
       
   622 
       
   623 	gManager->ReleaseStreamListAccess();
       
   624 
       
   625 	return ret;
       
   626 }
       
   627 TCF_EXP BOOL TCF_CALL TCFPollError(long inClientId, int* outErrorCode, BOOL* outHasOSErrorCode, long* outOSErrorCode)
       
   628 {
       
   629 	BOOL foundError = FALSE;
       
   630 	gManager->WaitForErrorMonitorListAccess();
       
   631 
       
   632 	if (gManager->ErrorMonitorListSize() > 0)
       
   633 	{
       
   634 		CErrorMonitor* errorMonitor = gManager->FindErrorMonitor(inClientId);
       
   635 		if (errorMonitor != NULL)
       
   636 		{
       
   637 			LONG tcfError = TCAPI_ERR_NONE;
       
   638 			BOOL osErrorUsed = FALSE;
       
   639 			DWORD osError = 0;
       
   640 			BOOL found = errorMonitor->GetError(&tcfError, &osErrorUsed, &osError);
       
   641 			if (found)
       
   642 			{
       
   643 				foundError = true;
       
   644 				*outErrorCode = (int)tcfError;
       
   645 				*outHasOSErrorCode = osErrorUsed;
       
   646 				*outOSErrorCode = osError;
       
   647 			}
       
   648 		}
       
   649 	}
       
   650 	gManager->ReleaseErrorMonitorListAccess();
       
   651 
       
   652 	return foundError;
       
   653 }
       
   654 
       
   655 long CheckClient(long id)
       
   656 {
       
   657 	long ret = TCAPI_ERR_NONE;
       
   658 	BOOL found = FALSE;
       
   659 
       
   660 	if (id <= 0)
       
   661 	{
       
   662 		ret = TCAPI_ERR_INVALID_HANDLE;
       
   663 	}
       
   664 	else if (gManager->IsServerRunning() == FALSE)
       
   665 	{
       
   666 		ret = TCAPI_ERR_MEDIA_NOT_OPEN;
       
   667 	}
       
   668 	else
       
   669 	{
       
   670 		if (pcppClientIds == NULL)
       
   671 		{
       
   672 			ret = TCAPI_ERR_MEDIA_NOT_OPEN;
       
   673 		}
       
   674 		else
       
   675 		{
       
   676 			std::vector<long>::iterator idIter;
       
   677 			for (idIter = pcppClientIds->begin(); idIter != pcppClientIds->end(); idIter++)
       
   678 			{
       
   679 				if (*idIter == id)
       
   680 				{
       
   681 					found = TRUE;
       
   682 					break;
       
   683 				}
       
   684 			}
       
   685 			if (!found)
       
   686 			{
       
   687 				ret = TCAPI_ERR_INVALID_HANDLE;
       
   688 			}
       
   689 		}
       
   690 	}
       
   691 
       
   692 	return ret;
       
   693 }
       
   694 long CheckConnection(pTCFCppConnectData inConnection)
       
   695 {
       
   696 	long ret = TCAPI_ERR_NONE;
       
   697 
       
   698 	if (inConnection == NULL)
       
   699 	{
       
   700 		ret = TCAPI_ERR_MISSING_CONNECTION_SPEC;
       
   701 	} 
       
   702 	else
       
   703 	{
       
   704 		long retryI = inConnection->retryInterval;
       
   705 		long retryT = inConnection->retryTimeout;
       
   706 		if (retryI == 0 || retryT == 0 || retryI > retryT)
       
   707 			ret = TCAPI_ERR_INVALID_RETRY_PERIODS;
       
   708 	}
       
   709 	if (ret == TCAPI_ERR_NONE)
       
   710 	{
       
   711 		if (strcmp(inConnection->connectType, "tcp") == 0)
       
   712 		{
       
   713 			if (inConnection->tcpSettings.ipAddress == NULL)
       
   714 			{
       
   715 				ret = TCAPI_ERR_MISSING_MEDIA_DATA;
       
   716 			}
       
   717 			else if (inConnection->tcpSettings.ipPort == NULL)
       
   718 			{
       
   719 				ret = TCAPI_ERR_MISSING_MEDIA_DATA;
       
   720 			}
       
   721 		}
       
   722 		else if (strcmp(inConnection->connectType, "virtualserial") == 0)
       
   723 		{
       
   724 			if (inConnection->virtualSerialSettings.comPort == NULL)
       
   725 			{
       
   726 				ret = TCAPI_ERR_MISSING_MEDIA_DATA;
       
   727 			}
       
   728 		}
       
   729 		else if (strcmp(inConnection->connectType, "serial") == 0)
       
   730 		{
       
   731 			ret = CheckRealSerialSettings(inConnection);
       
   732 		}
       
   733 		else
       
   734 		{
       
   735 		}
       
   736 	}
       
   737 
       
   738 	return ret;
       
   739 }
       
   740 
       
   741 long CheckRealSerialSettings(pTCFCppConnectData inConnection)
       
   742 {
       
   743 	long ret = TCAPI_ERR_NONE;
       
   744 
       
   745 	if (inConnection->realSerialSettings.comPort == NULL)
       
   746 	{
       
   747 		ret = TCAPI_ERR_MISSING_MEDIA_DATA;
       
   748 	}
       
   749 	else if (inConnection->realSerialSettings.dataBits < 4 || inConnection->realSerialSettings.dataBits > 8)
       
   750 	{
       
   751 		ret = TCAPI_ERR_COMM_INVALID_DATABITS;
       
   752 	}
       
   753 	else if (inConnection->realSerialSettings.baudRate < 110UL || inConnection->realSerialSettings.baudRate > 256000UL)
       
   754 	{
       
   755 		ret = TCAPI_ERR_MISSING_MEDIA_DATA;
       
   756 	}
       
   757 	else
       
   758 	{
       
   759 		switch(inConnection->realSerialSettings.stopBits)
       
   760 		{
       
   761 		case eTCFCppStopBits1:
       
   762 		case eTCFCppStopBits15:
       
   763 		case eTCFCppStopBits2:
       
   764 			break;
       
   765 		default:
       
   766 			ret = TCAPI_ERR_COMM_INVALID_STOPBITS;
       
   767 			break;
       
   768 		}
       
   769 
       
   770 	}
       
   771 	if (ret == TCAPI_ERR_NONE)
       
   772 	{
       
   773 		switch(inConnection->realSerialSettings.flowControl)
       
   774 		{
       
   775 		case eTCFCppFlowControlNone:
       
   776 		case eTCFCppFlowControlHW:
       
   777 		case eTCFCppFlowControlSW:
       
   778 			break;
       
   779 		default:
       
   780 			ret = TCAPI_ERR_COMM_INVALID_FLOWCONTROL;
       
   781 			break;
       
   782 		}
       
   783 	}
       
   784 	if (ret == TCAPI_ERR_NONE)
       
   785 	{
       
   786 		switch(inConnection->realSerialSettings.parity)
       
   787 		{
       
   788 		case eTCFCppParityNone:
       
   789 		case eTCFCppParityOdd:
       
   790 		case eTCFCppParityEven:
       
   791 			break;
       
   792 		default:
       
   793 			ret = TCAPI_ERR_COMM_INVALID_PARITY;
       
   794 			break;
       
   795  		}
       
   796 	}
       
   797 	
       
   798 	return ret;
       
   799 }
       
   800 long CheckMessageOptions(pTCFCppMessageOptions inMessageOptions)
       
   801 {
       
   802 	long ret = TCAPI_ERR_NONE;
       
   803 
       
   804 	if (inMessageOptions == NULL)
       
   805 	{
       
   806 		ret = TCAPI_ERR_MISSING_MESSAGE_OPTIONS;
       
   807 	}
       
   808 	else
       
   809 	{
       
   810 		if (ret == TCAPI_ERR_NONE)
       
   811 		{
       
   812 			if (inMessageOptions->unWrapFormat != eTCFCppNone && 
       
   813 				inMessageOptions->unWrapFormat != eTCFCppDeleteHeader)
       
   814 			{
       
   815 				ret = TCAPI_ERR_INVALID_MESSAGE_UNWRAP_OPTION;
       
   816 			}
       
   817 		}
       
   818 		if (ret == TCAPI_ERR_NONE)
       
   819 		{
       
   820 			if (inMessageOptions->inputStreamSize <= 0) 
       
   821 			{
       
   822 				ret = TCAPI_ERR_INVALID_STREAM_BUFFER_SIZE;
       
   823 			}
       
   824 		}
       
   825 	}
       
   826 	return ret;
       
   827 }
       
   828 long CheckMessageIds(pTCFCppMessageIds inMessageIds)
       
   829 {
       
   830 	long ret = TCAPI_ERR_NONE;
       
   831 
       
   832 	if (inMessageIds == NULL)
       
   833 	{
       
   834 		ret = TCAPI_ERR_NO_MESSAGESIDS_REGISTERED;
       
   835 	}
       
   836 	else if (inMessageIds->numberIds <= 0 || inMessageIds->messageIds == NULL)
       
   837 	{
       
   838 		ret = TCAPI_ERR_NO_MESSAGESIDS_REGISTERED;
       
   839 	}
       
   840 	else if (inMessageIds->numberIds > 256)
       
   841 	{
       
   842 		ret = TCAPI_ERR_MESSAGEID_MAXIMUM;
       
   843 	}
       
   844 
       
   845 	return ret;
       
   846 }
       
   847 long CheckMessage(pTCFCppMessage inMessage)
       
   848 {
       
   849 	long ret = TCAPI_ERR_NONE;
       
   850 
       
   851 	if (inMessage == NULL)
       
   852 		return TCAPI_ERR_MISSING_MESSAGE;
       
   853 
       
   854 	if (inMessage->useMyId)
       
   855 	{
       
   856 		if ((inMessage->encodeFormat != eTCFCppEncodeNone) && (inMessage->encodeFormat != eTCFCppEncode))
       
   857 		{
       
   858 			ret = TCAPI_ERR_INVALID_ENCODE_FORMAT;
       
   859 		}
       
   860 	}
       
   861 
       
   862 	return ret;
       
   863 }
       
   864 
       
   865 void ConvertRealSerialSettingsToServer(pTCFCppConnectData inConnection, pRealSerialConnectData pData)
       
   866 {
       
   867 	pData->baudRate = inConnection->realSerialSettings.baudRate;
       
   868 	pData->dataBits = inConnection->realSerialSettings.dataBits;
       
   869 	switch(inConnection->realSerialSettings.flowControl)
       
   870 	{
       
   871 	default:
       
   872 	case eTCFCppFlowControlNone:
       
   873 		pData->flowControl = eFlowControlNone;
       
   874 		break;
       
   875 	case eTCFCppFlowControlHW:
       
   876 		pData->flowControl = eFlowControlHW;
       
   877 		break;
       
   878 	case eTCFCppFlowControlSW:
       
   879 		pData->flowControl = eFlowControlSW;
       
   880 		break;
       
   881 	}
       
   882 	switch(inConnection->realSerialSettings.parity)
       
   883 	{
       
   884 	default:
       
   885 	case eTCFCppParityNone:
       
   886 		pData->parity = eParityNone;
       
   887 		break;
       
   888 	case eTCFCppParityOdd:
       
   889 		pData->parity = eParityOdd;
       
   890 		break;
       
   891 	case eTCFCppParityEven:
       
   892 		pData->parity = eParityEven;
       
   893 		break;
       
   894 	}
       
   895 	switch(inConnection->realSerialSettings.stopBits)
       
   896 	{
       
   897 	default:
       
   898 	case eTCFCppStopBits1:
       
   899 		pData->stopBits = eStopBits1;
       
   900 		break;
       
   901 	case eTCFCppStopBits15:
       
   902 		pData->stopBits = eStopBits15;
       
   903 		break;
       
   904 	case eTCFCppStopBits2:
       
   905 		pData->stopBits = eStopBits2;
       
   906 		break;
       
   907 	}
       
   908 }
       
   909 
       
   910 void ConvertRealSerialSettingsToHost(pTCFCppConnectData inConnection, pRealSerialConnectData pData)
       
   911 {
       
   912 	inConnection->realSerialSettings.baudRate = pData->baudRate;
       
   913 	inConnection->realSerialSettings.dataBits = pData->dataBits;
       
   914 	switch(pData->flowControl)
       
   915 	{
       
   916 	default:
       
   917 	case eFlowControlNone:
       
   918 		inConnection->realSerialSettings.flowControl = eTCFCppFlowControlNone;
       
   919 		break;
       
   920 	case eFlowControlHW:
       
   921 		inConnection->realSerialSettings.flowControl = eTCFCppFlowControlHW;
       
   922 		break;
       
   923 	case eFlowControlSW:
       
   924 		inConnection->realSerialSettings.flowControl = eTCFCppFlowControlSW;
       
   925 		break;
       
   926 	}
       
   927 
       
   928 	switch(pData->parity)
       
   929 	{
       
   930 	default:
       
   931 	case eParityNone:
       
   932 		inConnection->realSerialSettings.parity = eTCFCppParityNone;
       
   933 		break;
       
   934 	case eParityEven:
       
   935 		inConnection->realSerialSettings.parity = eTCFCppParityEven;
       
   936 		break;
       
   937 	case eParityOdd:
       
   938 		inConnection->realSerialSettings.parity = eTCFCppParityOdd;
       
   939 		break;
       
   940 	}
       
   941 
       
   942 	switch(pData->stopBits)
       
   943 	{
       
   944 	default:
       
   945 	case eStopBits1:
       
   946 		inConnection->realSerialSettings.stopBits = eTCFCppStopBits1;
       
   947 		break;
       
   948 	case eStopBits15:
       
   949 		inConnection->realSerialSettings.stopBits = eTCFCppStopBits15;
       
   950 		break;
       
   951 	case eStopBits2:
       
   952 		inConnection->realSerialSettings.stopBits = eTCFCppStopBits2;
       
   953 		break;
       
   954 	}
       
   955 }