| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. |
| 5 | * libCEC(R) is an original work, containing original code. |
| 6 | * |
| 7 | * libCEC(R) is a trademark of Pulse-Eight Limited. |
| 8 | * |
| 9 | * This program is dual-licensed; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | * |
| 23 | * |
| 24 | * Alternatively, you can license this library under a commercial license, |
| 25 | * please contact Pulse-Eight Licensing for more information. |
| 26 | * |
| 27 | * For more information contact: |
| 28 | * Pulse-Eight Licensing <license@pulse-eight.com> |
| 29 | * http://www.pulse-eight.com/ |
| 30 | * http://www.pulse-eight.net/ |
| 31 | */ |
| 32 | |
| 33 | #include "../serialport.h" |
| 34 | #include "../baudrate.h" |
| 35 | #include "../timeutils.h" |
| 36 | |
| 37 | using namespace std; |
| 38 | using namespace CEC; |
| 39 | |
| 40 | void FormatWindowsError(int iErrorCode, string &strMessage) |
| 41 | { |
| 42 | if (iErrorCode != ERROR_SUCCESS) |
| 43 | { |
| 44 | char strAddMessage[1024]; |
| 45 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, iErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), strAddMessage, 1024, NULL); |
| 46 | strMessage.append(": "); |
| 47 | strMessage.append(strAddMessage); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | CSerialPort::CSerialPort(void) : |
| 52 | m_handle(INVALID_HANDLE_VALUE), |
| 53 | m_bIsOpen(false), |
| 54 | m_iBaudrate(0), |
| 55 | m_iDatabits(0), |
| 56 | m_iStopbits(0), |
| 57 | m_iParity(0) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | CSerialPort::~CSerialPort(void) |
| 62 | { |
| 63 | Close(); |
| 64 | } |
| 65 | |
| 66 | bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity) |
| 67 | { |
| 68 | CLockObject lock(&m_mutex); |
| 69 | m_handle = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
| 70 | if (m_handle == INVALID_HANDLE_VALUE) |
| 71 | { |
| 72 | m_error = "Unable to open COM port"; |
| 73 | FormatWindowsError(GetLastError(), m_error); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | COMMCONFIG commConfig = {0}; |
| 78 | DWORD dwSize = sizeof(commConfig); |
| 79 | commConfig.dwSize = dwSize; |
| 80 | if (GetDefaultCommConfig(name.c_str(), &commConfig,&dwSize)) |
| 81 | { |
| 82 | if (!SetCommConfig(m_handle, &commConfig,dwSize)) |
| 83 | { |
| 84 | m_error = "unable to set default config"; |
| 85 | FormatWindowsError(GetLastError(), m_error); |
| 86 | } |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | m_error = "unable to get default config"; |
| 91 | FormatWindowsError(GetLastError(), m_error); |
| 92 | } |
| 93 | |
| 94 | if (!SetupComm(m_handle, 64, 64)) |
| 95 | { |
| 96 | m_error = "unable to set up the com port"; |
| 97 | FormatWindowsError(GetLastError(), m_error); |
| 98 | } |
| 99 | |
| 100 | m_iDatabits = databits; |
| 101 | m_iStopbits = stopbits; |
| 102 | m_iParity = parity; |
| 103 | if (!SetBaudRate(baudrate)) |
| 104 | { |
| 105 | m_error = "unable to set baud rate"; |
| 106 | FormatWindowsError(GetLastError(), m_error); |
| 107 | Close(); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (!SetTimeouts(false)) |
| 112 | { |
| 113 | m_error = "unable to set timeouts"; |
| 114 | FormatWindowsError(GetLastError(), m_error); |
| 115 | Close(); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | m_bIsOpen = true; |
| 120 | return m_bIsOpen; |
| 121 | } |
| 122 | |
| 123 | bool CSerialPort::SetTimeouts(bool bBlocking) |
| 124 | { |
| 125 | if (m_handle == INVALID_HANDLE_VALUE) |
| 126 | return false; |
| 127 | |
| 128 | COMMTIMEOUTS cto; |
| 129 | if (!GetCommTimeouts(m_handle, &cto)) |
| 130 | { |
| 131 | m_error = "GetCommTimeouts failed"; |
| 132 | FormatWindowsError(GetLastError(), m_error); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (bBlocking) |
| 137 | { |
| 138 | cto.ReadIntervalTimeout = 0; |
| 139 | cto.ReadTotalTimeoutConstant = 0; |
| 140 | cto.ReadTotalTimeoutMultiplier = 0; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | cto.ReadIntervalTimeout = MAXDWORD; |
| 145 | cto.ReadTotalTimeoutConstant = 0; |
| 146 | cto.ReadTotalTimeoutMultiplier = 0; |
| 147 | } |
| 148 | |
| 149 | if (!SetCommTimeouts(m_handle, &cto)) |
| 150 | { |
| 151 | m_error = "SetCommTimeouts failed"; |
| 152 | FormatWindowsError(GetLastError(), m_error); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | void CSerialPort::Close(void) |
| 160 | { |
| 161 | CLockObject lock(&m_mutex); |
| 162 | if (m_bIsOpen) |
| 163 | { |
| 164 | CloseHandle(m_handle); |
| 165 | m_bIsOpen = false; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | int8_t CSerialPort::Write(const cec_adapter_message &data) |
| 170 | { |
| 171 | CLockObject lock(&m_mutex); |
| 172 | DWORD iBytesWritten = 0; |
| 173 | if (!m_bIsOpen) |
| 174 | return -1; |
| 175 | |
| 176 | if (!WriteFile(m_handle, data.packet.data, data.size(), &iBytesWritten, NULL)) |
| 177 | { |
| 178 | m_error = "Error while writing to COM port"; |
| 179 | FormatWindowsError(GetLastError(), m_error); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | return (int8_t)iBytesWritten; |
| 184 | } |
| 185 | |
| 186 | int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = 0 */) |
| 187 | { |
| 188 | CLockObject lock(&m_mutex); |
| 189 | int32_t iReturn(-1); |
| 190 | DWORD iBytesRead = 0; |
| 191 | if (m_handle == 0) |
| 192 | { |
| 193 | m_error = "Error while reading from COM port: invalid handle"; |
| 194 | return iReturn; |
| 195 | } |
| 196 | |
| 197 | if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0) |
| 198 | { |
| 199 | m_error = "unable to read from device"; |
| 200 | FormatWindowsError(GetLastError(), m_error); |
| 201 | iReturn = -1; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | iReturn = (int32_t) iBytesRead; |
| 206 | } |
| 207 | |
| 208 | return iReturn; |
| 209 | } |
| 210 | |
| 211 | bool CSerialPort::SetBaudRate(uint32_t baudrate) |
| 212 | { |
| 213 | int32_t rate = IntToBaudrate(baudrate); |
| 214 | if (rate < 0) |
| 215 | m_iBaudrate = baudrate > 0 ? baudrate : 0; |
| 216 | else |
| 217 | m_iBaudrate = rate; |
| 218 | |
| 219 | DCB dcb; |
| 220 | memset(&dcb,0,sizeof(dcb)); |
| 221 | dcb.DCBlength = sizeof(dcb); |
| 222 | dcb.BaudRate = IntToBaudrate(m_iBaudrate); |
| 223 | dcb.fBinary = true; |
| 224 | dcb.fDtrControl = DTR_CONTROL_DISABLE; |
| 225 | dcb.fRtsControl = RTS_CONTROL_DISABLE; |
| 226 | dcb.fOutxCtsFlow = false; |
| 227 | dcb.fOutxDsrFlow = false; |
| 228 | dcb.fOutX = false; |
| 229 | dcb.fInX = false; |
| 230 | dcb.fAbortOnError = true; |
| 231 | |
| 232 | if (m_iParity == PAR_NONE) |
| 233 | dcb.Parity = NOPARITY; |
| 234 | else if (m_iParity == PAR_EVEN) |
| 235 | dcb.Parity = EVENPARITY; |
| 236 | else |
| 237 | dcb.Parity = ODDPARITY; |
| 238 | |
| 239 | if (m_iStopbits == 2) |
| 240 | dcb.StopBits = TWOSTOPBITS; |
| 241 | else |
| 242 | dcb.StopBits = ONESTOPBIT; |
| 243 | |
| 244 | dcb.ByteSize = m_iDatabits; |
| 245 | |
| 246 | if(!SetCommState(m_handle,&dcb)) |
| 247 | { |
| 248 | m_error = "SetCommState failed"; |
| 249 | FormatWindowsError(GetLastError(), m_error); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | bool CSerialPort::IsOpen() |
| 257 | { |
| 258 | CLockObject lock(&m_mutex); |
| 259 | return m_bIsOpen; |
| 260 | } |