testtoolsconn/stat/desktop/source/lib/src/statserialbluetooth.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "stdafx.h"
       
    22 #include "statserialbluetooth.h"
       
    23 #include "assert.h"
       
    24 
       
    25 #define SP_COMMANDSIZE	4
       
    26 #define SP_LENGTHSIZE	4
       
    27 #define SP_BUFFERSIZE	STAT_BUFFERSIZE
       
    28 
       
    29 ////////////////////////////////////////////////////////////////////////////////////////
       
    30 // Constructor
       
    31 CSTATSerialBluetooth::CSTATSerialBluetooth()
       
    32 : hComPort((HANDLE)0), 	pTemp(NULL)
       
    33 {
       
    34 }
       
    35 
       
    36 
       
    37 ////////////////////////////////////////////////////////////////////////////////////////
       
    38 // Destructor
       
    39 CSTATSerialBluetooth::~CSTATSerialBluetooth()
       
    40 {
       
    41 	Disconnect();
       
    42 	Release();
       
    43 }
       
    44 
       
    45 
       
    46 ////////////////////////////////////////////////////////////////////////////////////////
       
    47 // Initialisation stuff
       
    48 int CSTATSerialBluetooth::Initialise(void)
       
    49 {
       
    50 	return ITS_OK;
       
    51 }
       
    52 
       
    53 
       
    54 ////////////////////////////////////////////////////////////////////////////////////////
       
    55 // Open a connection
       
    56 int CSTATSerialBluetooth::Connect(const char *pAddress)
       
    57 {
       
    58 	// already connected
       
    59 	if (hComPort)
       
    60 		return ITS_OK;
       
    61 
       
    62 	if (OpenComPort(pAddress))
       
    63 	{
       
    64 		SetError("Connected to port %s Speed %d Bytes %d Parity %d Stop %d",
       
    65 			pAddress, STATBT_BAUDRATE, STATBT_BYTESIZE, STATBT_PARITY, STATBT_STOPBITS);
       
    66 		return ITS_OK;
       
    67 	}
       
    68 
       
    69 	return E_CONNECTIONFAILED;
       
    70 }
       
    71 
       
    72 
       
    73 ////////////////////////////////////////////////////////////////////////////////////////
       
    74 // Send a command and associated data
       
    75 ////////////////////////////////////////////////////////////////////////////////////////
       
    76 int CSTATSerialBluetooth::Send(const char cIdentifier, const char *pCommand, const unsigned long ulLength)
       
    77 {
       
    78 	DWORD dwBytes = 0;
       
    79 	int ret = E_WRITEFAILED;
       
    80 
       
    81 	// prepare command
       
    82 	STATCOMMAND STATSendCommand;
       
    83 	memset(&STATSendCommand, 0, sizeof(STATCOMMAND));
       
    84 	STATSendCommand.cIdentifier = cIdentifier;
       
    85 	STATSendCommand.ulLength = ulLength;
       
    86 
       
    87 	if (WriteFile(hComPort, (LPVOID)&STATSendCommand, sizeof(STATCOMMAND), &dwBytes, NULL))
       
    88 	{
       
    89 		if (dwBytes == sizeof(STATCOMMAND))
       
    90 		{
       
    91 			// only send data if it will fit into our buffer
       
    92 		//	if (pCommand && STATSendCommand.ulLength && STATSendCommand.ulLength <= STAT_BUFFERSIZE)
       
    93 		//	{
       
    94 				if (WriteFile(hComPort, (LPVOID)pCommand, STATSendCommand.ulLength, &dwBytes, NULL))
       
    95 				{
       
    96 					if (dwBytes == STATSendCommand.ulLength)
       
    97 					{
       
    98 						SetError("Sent successfully %ld bytes", STATSendCommand.ulLength);
       
    99 						ret = ITS_OK;
       
   100 					}
       
   101 					else
       
   102 					{
       
   103 						SetError("Incorrect number of bytes written - expected %ld sent %ld", STATSendCommand.ulLength, dwBytes);
       
   104 						ret = E_BADNUMBERBYTES;
       
   105 					}
       
   106 				}
       
   107 				else
       
   108 					SetError("Write data to port failed");
       
   109 		///	}
       
   110 		//	else
       
   111 		//	{
       
   112 		//		SetError("Sent successfully");
       
   113 		//		ret = ITS_OK;
       
   114 		//	}
       
   115 		}
       
   116 		else
       
   117 		{
       
   118 			SetError("Incorrect number of bytes written - expected %ld sent %ld", sizeof(STATCOMMAND), dwBytes);
       
   119 			ret = E_BADNUMBERBYTES;
       
   120 		}
       
   121 	}
       
   122 	else
       
   123 		SetError("Write command to port failed");
       
   124 
       
   125 	return ret;
       
   126 }
       
   127 
       
   128 
       
   129 ////////////////////////////////////////////////////////////////////////////////////////
       
   130 // Receive a command and associated data
       
   131 //
       
   132 // Tries to read a STATCOMMAND structure from the port.  If successful, the length
       
   133 // of any data following will be specified in the structure.  If extra data, try to
       
   134 // read this.  If all successful, set the pointers to point to it
       
   135 //
       
   136 ////////////////////////////////////////////////////////////////////////////////////////
       
   137 int CSTATSerialBluetooth::ReceiveBytes( char *buff, unsigned long size )
       
   138 {
       
   139 	int ret;
       
   140 	int err;
       
   141 	unsigned long bytes_read = 0;
       
   142 	unsigned long bytes_read_this_iteration = 0;
       
   143 
       
   144 	// make sure size > 0 otherwise the following semantics fail
       
   145 	assert( size > 0 );
       
   146 
       
   147 	// loop until either all bytes have been received or an error occurs
       
   148 	for( bytes_read = 0; bytes_read < size; bytes_read += bytes_read_this_iteration ) 
       
   149 	{ 
       
   150 		ret = ReadFile( hComPort, &(buff[bytes_read]), (size - bytes_read), &bytes_read_this_iteration, NULL );		
       
   151 		
       
   152 		if( bytes_read_this_iteration == 0 ) 
       
   153 		{
       
   154 			return NO_DATA_AT_PORT;
       
   155 		}
       
   156 
       
   157 		if( ret == 0 ) 
       
   158 		{
       
   159 			err = GetLastError();
       
   160 			SetError( "Error while receiving command - %ld", err );
       
   161 			return err;
       
   162 		}
       
   163 	}
       
   164 
       
   165 	// everything is OK
       
   166 	return 0;
       
   167 }
       
   168 
       
   169 ////////////////////////////////////////////////////////////////////////////////////////
       
   170 int CSTATSerialBluetooth::Receive( char *cIdentifier, char **ppData, unsigned long *pLength )
       
   171 {
       
   172 	int ret;
       
   173 	unsigned long command;
       
   174 	unsigned long data_length;
       
   175 
       
   176 	// initialise parameters
       
   177 	*cIdentifier = 0;
       
   178 	*ppData = NULL;
       
   179 	*pLength = 0;
       
   180 
       
   181 	// get command
       
   182 	ret = ReceiveBytes( (char*)&command, SP_COMMANDSIZE );
       
   183 	if( ret != 0 ) {
       
   184 		return ret;
       
   185 	}
       
   186 		
       
   187 	// get length
       
   188 	ret = ReceiveBytes( (char*)&data_length, SP_LENGTHSIZE );
       
   189 	if( ret != 0 ) {
       
   190 		return ret;
       
   191 	}
       
   192 
       
   193 	// put these values into the return slots
       
   194 	*cIdentifier = (char)command;
       
   195 	*pLength = data_length;
       
   196 
       
   197 	// if the length is zero then there is no more to do
       
   198 	if( data_length == 0 ) {
       
   199 		return ITS_OK;
       
   200 	}
       
   201 
       
   202 	// if the length is greater than the buffer size then this is a header packet and we are not
       
   203 	// supposed to actually read any data from it.
       
   204 /*	if( data_length > SP_BUFFERSIZE ) {
       
   205 		SetError( "Received command ID: %c successfully length %d", (*cIdentifier), (*pLength) );
       
   206 		return ITS_OK;
       
   207 	}
       
   208 */
       
   209 
       
   210 	SetError( "Received command ID: %c successfully length %d", (*cIdentifier), (*pLength) );
       
   211 	
       
   212 	// Otherwise we are about to read some data. We first have to setup the memory management
       
   213 	// which is done in a pretty ugly way IMAO.
       
   214 	if( pTemp ) 
       
   215 	{
       
   216 		delete [] pTemp;
       
   217 		pTemp = NULL;
       
   218 	}
       
   219 	
       
   220 	pTemp = new char [data_length];
       
   221 	
       
   222 	if( pTemp == NULL ) 
       
   223 	{
       
   224 		SetError( "Unable to allocate %ld bytes of memory to hold data", data_length );
       
   225 		return E_OUTOFMEM;
       
   226 	}
       
   227 
       
   228 	// now read the data
       
   229 	ret = ReceiveBytes( pTemp, data_length );
       
   230 	
       
   231 	if( ret != 0 ) 
       
   232 	{
       
   233 		return ret;
       
   234 	}
       
   235 
       
   236 	*ppData = pTemp;
       
   237 
       
   238 	return ITS_OK;
       
   239 }
       
   240 
       
   241 
       
   242 ////////////////////////////////////////////////////////////////////////////////////////
       
   243 // Disconnect from the port
       
   244 int CSTATSerialBluetooth::Disconnect(void)
       
   245 {
       
   246 	// release previous resources
       
   247 	if (pTemp)
       
   248 	{
       
   249 		delete [] pTemp;
       
   250 		pTemp = NULL;
       
   251 	}
       
   252 
       
   253 	// disconnect
       
   254 	if (hComPort)
       
   255 	{
       
   256 		CloseHandle(hComPort);
       
   257 		hComPort = (HANDLE)0;
       
   258 	}
       
   259 
       
   260 	SetError("Disconnected successfully");
       
   261 	return ITS_OK;
       
   262 }
       
   263 
       
   264 
       
   265 ////////////////////////////////////////////////////////////////////////////////////////
       
   266 // Release resources
       
   267 int CSTATSerialBluetooth::Release(void)
       
   268 {
       
   269 	Disconnect();
       
   270 	return ITS_OK;
       
   271 }
       
   272 
       
   273 
       
   274 ////////////////////////////////////////////////////////////////////////////////////////
       
   275 //	PRIVATE FUNCTIONS
       
   276 ////////////////////////////////////////////////////////////////////////////////////////
       
   277 
       
   278 ////////////////////////////////////////////////////////////////////////////////////////
       
   279 // 
       
   280 bool CSTATSerialBluetooth::OpenComPort(const char *pAddress)
       
   281 {
       
   282 	TCHAR szAddress[256] = {0};
       
   283 
       
   284 #ifdef UNICODE
       
   285 	szAddress[0] = (TCHAR)0;
       
   286 
       
   287     // Convert to UNICODE.
       
   288     MultiByteToWideChar(CP_ACP,					// conversion type
       
   289 						 0,							// flags
       
   290 						 pAddress,					// source
       
   291 						 -1,						// length
       
   292 						 szAddress,					// dest
       
   293 						 256);						// length
       
   294 #else
       
   295 	strcpy(szAddress, pAddress);
       
   296 #endif
       
   297 
       
   298 	if (INVALID_HANDLE_VALUE == (hComPort = CreateFile(szAddress,
       
   299 					 GENERIC_READ | GENERIC_WRITE,
       
   300 					 0,
       
   301 					 NULL,
       
   302 					 OPEN_EXISTING,
       
   303 					 FILE_FLAG_WRITE_THROUGH,
       
   304 					 NULL)))
       
   305 	{
       
   306 		SetError("Port [%s] could not be opened", pAddress);
       
   307 		return false;
       
   308 	}
       
   309 
       
   310 	COMMTIMEOUTS CommTimeOuts;
       
   311 	CommTimeOuts.WriteTotalTimeoutMultiplier	= STATBT_WRITETOTALTIMEOUTMULTIPLIER; 
       
   312 	CommTimeOuts.WriteTotalTimeoutConstant		= STATBT_WRITETOTALTIMEOUTCONSTANT;
       
   313 	CommTimeOuts.ReadIntervalTimeout			= STATBT_READINTERVALTIMEOUT;          
       
   314     CommTimeOuts.ReadTotalTimeoutMultiplier		= STATBT_READTOTALTIMEOUTMULTIPLIER;   
       
   315     CommTimeOuts.ReadTotalTimeoutConstant		= STATBT_READTOTALTIMEOUTCONSTANT;
       
   316 	
       
   317 	if (!SetCommTimeouts(hComPort, &CommTimeOuts))
       
   318 	{
       
   319 		SetError("Comm port timeouts could not be set");
       
   320 		CloseHandle(hComPort);
       
   321 		return false;
       
   322 	} 
       
   323 
       
   324 	// Configure the COM port 
       
   325 	DCB dcb;
       
   326 	GetCommState(hComPort, &dcb);
       
   327 	dcb.DCBlength			= sizeof(dcb); 
       
   328 	dcb.BaudRate			= STATBT_BAUDRATE;
       
   329 	dcb.fBinary				= TRUE;
       
   330 	dcb.fParity				= STATBT_PARITY;
       
   331 	dcb.fOutxCtsFlow		= TRUE;
       
   332 	dcb.fOutxDsrFlow		= FALSE;
       
   333 	dcb.fDtrControl			= DTR_CONTROL_ENABLE;	
       
   334 	dcb.fDsrSensitivity		= FALSE;
       
   335 	dcb.fTXContinueOnXoff	= TRUE;
       
   336 	dcb.fOutX				= FALSE;
       
   337 	dcb.fInX				= FALSE;
       
   338 	dcb.fErrorChar			= FALSE;
       
   339 	dcb.fNull				= FALSE;
       
   340 	dcb.fRtsControl			= RTS_CONTROL_HANDSHAKE;
       
   341 	dcb.fAbortOnError		= TRUE;
       
   342 	dcb.XonLim				= 4096;
       
   343 	dcb.XoffLim				= 1024;
       
   344 	dcb.ByteSize			= STATBT_BYTESIZE;
       
   345 	dcb.Parity				= STATBT_PARITY;
       
   346 	dcb.StopBits			= STATBT_STOPBITS;
       
   347 	dcb.XonChar				= 17;
       
   348 	dcb.XoffChar			= 19;
       
   349 
       
   350 //	dcb.fRtsControl		= RTS_CONTROL_ENABLE;
       
   351 //	dcb.BaudRate		= STAT_BAUDRATE;
       
   352 //	dcb.fOutxCtsFlow	= FALSE;
       
   353 //	dcb.XonLim			= STAT_XONLIMIT;
       
   354 //	dcb.XoffLim			= STAT_XOFFLIMIT;
       
   355 	
       
   356 	if (!SetCommState(hComPort, &dcb))
       
   357 	{
       
   358 		SetError("Comm port state could not be set");
       
   359 		CloseHandle(hComPort);
       
   360 		return false;
       
   361 	} 
       
   362 
       
   363 	return true;
       
   364 }