diff -r 000000000000 -r c9bc50fca66e usbmgmt/usbmgrtest/t_acm_wins/src/t_acm_wins.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/usbmgmt/usbmgrtest/t_acm_wins/src/t_acm_wins.cpp Tue Feb 02 02:02:59 2010 +0200 @@ -0,0 +1,920 @@ +/* +* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* This program executes all the tests in the ACM CSY Test Specification v 0.1 +* on the host, and is to be used in conjunction with the device-side application (t_acm). +* +*/ + +// Includes //////////////////////////////////////////////////////////////////// + +/* Symbian OS includes */ +#include +#include +#include + +#define WIN32_LEAN_AND_MEAN + +/* Windows i/o for serial device */ +#include +#include + +// Defines and enumerations ///////////////////////////////////////////////////// + +#define _printf console->Printf +#define _getch console->Getch +#define LEAVE(_x) VerboseLeaveL(_x, __LINE__, TPtrC8((const TUint8*)__FILE__), _L8(#_x)) +#define LEAVEIFERROR(_x) VerboseLeaveIfErrorL(_x, __LINE__, TPtrC8((const TUint8*)__FILE__), _L8(#_x)) +#define CHECK(_x) if (! (_x)) VerboseLeaveL(KErrGeneral, __LINE__, TPtrC8((const TUint8*)__FILE__), _L8(#_x)) + +#define MAX_BUFFER_SIZE 1024 + +// Flow control constants +enum FlowControl_Modes {KFLOW_NONE, KFLOW_RTSCTS, KFLOW_DTRDSR}; + +// Global Variables ///////////////////////////////////////////////////////////// + +HANDLE hSerial; // An MS Windows handle for the serial port +DCB OldDcb; // for storing the original serial device context +DCB newDcb; + +// For holding our new serial port parameteres +struct SerialParameters + { + char *comport; // which port + unsigned long pktsize, pktcount, maxwait; // packet config + unsigned long baud, bits, stop; // serial parameters + unsigned char parity; + enum FlowControl_Modes flowcontrol; // flow control handshaking +} SerialParams; + +LOCAL_D CConsoleBase* console; // A Symbian command console + +_LIT(KPortName, "COM3"); // Windows serial port name + +RTimer timer; // A timer for use by several of the tests + +// Some buffers allocated on the heap for functions with large +// stack usage so we don't get a __chkstk error. +char writeBufbig[MAX_BUFFER_SIZE]; +char readBufbig[MAX_BUFFER_SIZE]; + +// Functions ////////////////////////////////////////////////////////////////// + +void VerboseLeaveL(TInt aError, TInt aLineNum, const TDesC8& aFileName, const TDesC8& aCode) +/** + * For bombing out usefully. + */ + { + TInt filenameOffset = aFileName.LocateReverse('\\') + 1; + if (filenameOffset < 0) filenameOffset = 1; + TPtrC8 shortFileName = aFileName.Mid(filenameOffset); + TBuf<64> fName, code; + fName.Copy(shortFileName.Left(64)); + code.Copy(aCode.Left(64)); + _printf(_L("ERROR (%d) on line %d of file %S\n"), aError, aLineNum, &fName); + _printf(_L("Code: %S\n\n"), &code); + _printf(_L("[ press any key ]")); + _getch(); + User::Leave(aError); + } + +void VerboseLeaveIfErrorL(TInt aError, TInt aLineNum, const TDesC8& aFileName, const TDesC8& aCode) +/** + * For bombing out usefully if there's an error. + */ + { + if (aError) + VerboseLeaveL(aError, aLineNum, aFileName, aCode); + } + +void SetWindowsSerialParams() +/** + * Set up the new serial parameters with which to run the tests. + */ +{ + // parameter 1 is port name + // read in COM port name (this parameter must be specified!) + SerialParams.comport = new char[10]; + strcpy(SerialParams.comport, "COM3"); + + // default packet size + SerialParams.pktsize = 1024; + + // default no of packets + SerialParams.pktcount = 1; + + // default wait for data return + SerialParams.maxwait = 30; + + // default flow control + SerialParams.flowcontrol = KFLOW_RTSCTS; + + // default data rate + // baud rate 0 (port's default) standard rates 300 -> 115200 + SerialParams.baud = 0; + + // default bits per char + SerialParams.bits = 8; + + // default parity + SerialParams.parity = 'N'; + + // default 9 is stop bits + SerialParams.stop = 1; +} + +static int InitializePort() +/** + * Store the old state of the serial port and set it up for our tests. + */ +{ + DCB dcb; + COMMTIMEOUTS CommsTimeouts; + + // Setup comms device receive & transmit buffers + if(SetupComm(hSerial, 16384, 16384) == 0) + { + LEAVE(-1); + } + + // Purge all characters from the output and input buffers + // and terminate any pending read or write operations + if(PurgeComm(hSerial, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR) == 0) + { + LEAVE(-1); + } + + dcb.DCBlength = sizeof(DCB); + if(GetCommState(hSerial, &dcb) == 0) + { + // not a serial device + LEAVE(-1); + } + + memcpy(&OldDcb, &dcb, sizeof(DCB)); + + // General initialization for both transmitted and received bytes + // using port's default baud rate etc..., so read it + SerialParams.baud = dcb.BaudRate; // Get baud rate + SerialParams.bits = dcb.ByteSize; // Get number of bits + if(dcb.StopBits == 2) // Get number of stop bits to be used + SerialParams.stop = 2; + else + SerialParams.stop = 1; + + // Get parity scheme + if((dcb.fParity == FALSE) || (dcb.Parity == 0)) + { + SerialParams.parity = 'N'; + } + else + { + switch(dcb.Parity) + { + case 1: SerialParams.parity = 'O'; break; + case 2: SerialParams.parity = 'E'; break; + case 3: SerialParams.parity = 'M'; break; + case 4: SerialParams.parity = 'S'; break; + default: SerialParams.parity = 'N'; break; // shouldn't happen + } + } + dcb.fBinary = TRUE; // Set binary mode + + // Setup hardware flow control + if(SerialParams.flowcontrol == KFLOW_RTSCTS) + { + dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; // Specify the type of RTS flow control + dcb.fOutxCtsFlow = 1; // Specify whether the CTS signal is monitored + + dcb.fDtrControl = DTR_CONTROL_ENABLE; // Specify the type of DTR flow control + dcb.fOutxDsrFlow = 0; // Specify whether the DSR signal is monitored + } + else if(SerialParams.flowcontrol == KFLOW_DTRDSR) + { + dcb.fRtsControl = RTS_CONTROL_ENABLE; // Specify the type of RTS flow control + dcb.fOutxCtsFlow = 0; // Specify whether the CTS signal is monitored + + dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;// Specify the type of DTR flow control + dcb.fOutxDsrFlow = 1; // Specify whether the DSR signal is monitored + } + else // KFLOW_NONE + { + dcb.fRtsControl = RTS_CONTROL_ENABLE; // Specify the type of RTS flow control + dcb.fOutxCtsFlow = 0; // Specify whether the CTS signal is monitored + + dcb.fDtrControl = DTR_CONTROL_ENABLE; // Specify the type of DTR flow control + dcb.fOutxDsrFlow = 0; // Specify whether the DSR signal is monitored + } + + dcb.fInX = dcb.fOutX = 0; + dcb.fDsrSensitivity = 0; + dcb.fErrorChar = 0; + dcb.fNull = 0; + dcb.fAbortOnError = 1; + + // Configure the communications device according + // to the specifications in the device-control block + // The function reinitializes all hardware and control settings, + // but does not empty output or input queues + if(SetCommState(hSerial, &dcb) == 0) + { + LEAVE(-1); + } + + // Set timeout parameters for comms device + /* A value of MAXDWORD, combined with zero values for both the + * ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, + * specifies that the read operation is to return immediately with + * the characters that have already been received, even if no characters + * have been received. + */ + + CommsTimeouts.ReadIntervalTimeout = 0xFFFFFFFF; + CommsTimeouts.ReadTotalTimeoutMultiplier = 0; + CommsTimeouts.ReadTotalTimeoutConstant = 1000; + + // CBR_9600 is approximately 1byte/ms. For our purposes be generous + // & allow double the expected time per character + if(SerialParams.maxwait > 1) + { + CommsTimeouts.WriteTotalTimeoutMultiplier = 0; + CommsTimeouts.WriteTotalTimeoutConstant = (SerialParams.maxwait - 1) * 1000; + } + else + { + CommsTimeouts.WriteTotalTimeoutMultiplier = 2*CBR_9600/dcb.BaudRate; + CommsTimeouts.WriteTotalTimeoutConstant = 1000; + } + + if(0 == SetCommTimeouts(hSerial, &CommsTimeouts)) + { + LEAVE(-1); + } + + if(0 == SetCommMask(hSerial, EV_RXCHAR | EV_ERR /* | EV_TXEMPTY */)) + { + LEAVE(-1); + } + + return 0; +} + +void OpenComPort() +/** + * Create a Windows com port for our serial device. + */ +{ + hSerial = INVALID_HANDLE_VALUE; + wchar_t *fullname = new wchar_t[50]; + + // Open comms device + wcscpy(fullname, L"\\\\.\\com3"); + hSerial = CreateFile(fullname, + GENERIC_READ | GENERIC_WRITE, + 0, // exclusive access + NULL, // no security attrs + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL ) ; + + // return if handle for comms device is invalid + if (hSerial == INVALID_HANDLE_VALUE) + { + delete[] fullname; + LEAVE(GetLastError()); + } + else + { + if(InitializePort()) + { + // Close comms device and event handles for overlapped read and write + CloseHandle(hSerial); + hSerial = INVALID_HANDLE_VALUE; + } + } + delete[] fullname; +} + +void FillBuffer(char *buffer, int size) +/** + * Fills the buffer with incrementing data + */ + { + for (TInt i=0; i