| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
Comm.h |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
Declaration of cComm class |
| 7 |
|
| 8 |
PUBLIC CLASSES AND METHODS: |
| 9 |
cComm |
| 10 |
This class wraps low level port communications |
| 11 |
|
| 12 |
Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 13 |
|
| 14 |
Redistribution and use in source and binary forms, with or without |
| 15 |
modification, are permitted provided that the following conditions are met: |
| 16 |
* Redistributions of source code must retain the above copyright |
| 17 |
notice, this list of conditions and the following disclaimer. |
| 18 |
* Redistributions in binary form must reproduce the above copyright |
| 19 |
notice, this list of conditions and the following disclaimer in the |
| 20 |
documentation and/or other materials provided with the distribution. |
| 21 |
* Neither the name of Code Aurora Forum nor |
| 22 |
the names of its contributors may be used to endorse or promote |
| 23 |
products derived from this software without specific prior written |
| 24 |
permission. |
| 25 |
|
| 26 |
|
| 27 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 28 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 30 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 31 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 32 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 33 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 34 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 35 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 36 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 37 |
POSSIBILITY OF SUCH DAMAGE. |
| 38 |
===========================================================================*/ |
| 39 |
|
| 40 |
//--------------------------------------------------------------------------- |
| 41 |
// Include Files |
| 42 |
//--------------------------------------------------------------------------- |
| 43 |
#include "Event.h" |
| 44 |
|
| 45 |
//--------------------------------------------------------------------------- |
| 46 |
// Pragmas |
| 47 |
//--------------------------------------------------------------------------- |
| 48 |
#pragma once |
| 49 |
|
| 50 |
/*=========================================================================*/ |
| 51 |
// Class cIOCallback |
| 52 |
/*=========================================================================*/ |
| 53 |
class cIOCallback |
| 54 |
{ |
| 55 |
public: |
| 56 |
// (Inline) Constructor |
| 57 |
cIOCallback() { }; |
| 58 |
|
| 59 |
// (Inline) Destructor |
| 60 |
virtual ~cIOCallback() { }; |
| 61 |
|
| 62 |
// The I/O has been completed, process the results |
| 63 |
virtual void IOComplete( |
| 64 |
DWORD status, |
| 65 |
DWORD bytesTransferred ) = 0; |
| 66 |
}; |
| 67 |
|
| 68 |
/*=========================================================================*/ |
| 69 |
// Class cComm |
| 70 |
/*=========================================================================*/ |
| 71 |
class cComm |
| 72 |
{ |
| 73 |
public: |
| 74 |
// Constructor |
| 75 |
cComm(); |
| 76 |
|
| 77 |
// Destructor |
| 78 |
virtual ~cComm(); |
| 79 |
|
| 80 |
// Is this object valid? |
| 81 |
virtual bool IsValid(); |
| 82 |
|
| 83 |
// Connect to the specified port |
| 84 |
virtual bool Connect( LPCSTR pPort ); |
| 85 |
|
| 86 |
// Run an IOCTL on the open file handle |
| 87 |
int RunIOCTL( |
| 88 |
UINT ioctlReq, |
| 89 |
void * pData ); |
| 90 |
|
| 91 |
// Disconnect from the current port |
| 92 |
virtual bool Disconnect(); |
| 93 |
|
| 94 |
// Configure the port with the passed in parameters |
| 95 |
bool ConfigureSettings( termios * pSettings ); |
| 96 |
|
| 97 |
// Return the current port settings |
| 98 |
bool GetSettings( termios * pSettings ); |
| 99 |
|
| 100 |
// Cancel any in-progress I/O |
| 101 |
bool CancelIO(); |
| 102 |
|
| 103 |
// Cancel any in-progress receive operation |
| 104 |
bool CancelRx(); |
| 105 |
|
| 106 |
// Cancel any in-progress transmit operation |
| 107 |
bool CancelTx(); |
| 108 |
|
| 109 |
// Receive data |
| 110 |
virtual bool RxData( |
| 111 |
BYTE * pBuf, |
| 112 |
ULONG bufSz, |
| 113 |
cIOCallback * pCallback ); |
| 114 |
|
| 115 |
// Transmit data |
| 116 |
virtual bool TxData( |
| 117 |
const BYTE * pBuf, |
| 118 |
ULONG bufSz ); |
| 119 |
|
| 120 |
// (Inline) Return current port name |
| 121 |
virtual std::string GetPortName() const |
| 122 |
{ |
| 123 |
return mPortName; |
| 124 |
}; |
| 125 |
|
| 126 |
// Are we currently connected to a port? |
| 127 |
bool IsConnected() |
| 128 |
{ |
| 129 |
return (mPort != INVALID_HANDLE_VALUE); |
| 130 |
}; |
| 131 |
|
| 132 |
protected: |
| 133 |
/* Name of current port */ |
| 134 |
std::string mPortName; |
| 135 |
|
| 136 |
/* Handle to COM port */ |
| 137 |
int mPort; |
| 138 |
|
| 139 |
/* Read callbacks */ |
| 140 |
cIOCallback * mpRxCallback; |
| 141 |
|
| 142 |
// Cancel the write request? |
| 143 |
bool mbCancelWrite; |
| 144 |
|
| 145 |
/* Buffer */ |
| 146 |
BYTE * mpBuffer; |
| 147 |
|
| 148 |
/* Buffer size */ |
| 149 |
ULONG mBuffSz; |
| 150 |
|
| 151 |
/* Pipe for comunication with thread */ |
| 152 |
int mCommandPipe[2]; |
| 153 |
|
| 154 |
/* Thread ID of Rx Thread. */ |
| 155 |
pthread_t mRxThreadID; |
| 156 |
|
| 157 |
// Rx thread is allowed complete access |
| 158 |
friend void * RxThread( void * pData ); |
| 159 |
}; |