Commit | Line | Data |
---|---|---|
abbca718 LOK |
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" | |
b9187cc6 | 35 | #include "../timeutils.h" |
abbca718 LOK |
36 | |
37 | using namespace std; | |
b9187cc6 | 38 | using namespace CEC; |
abbca718 LOK |
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 | ||
8bca69de | 66 | bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity) |
abbca718 | 67 | { |
ff50391c | 68 | CStdString strComPath = "\\\\.\\" + name; |
8bca69de | 69 | CLockObject lock(&m_mutex); |
ff50391c | 70 | m_handle = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
abbca718 LOK |
71 | if (m_handle == INVALID_HANDLE_VALUE) |
72 | { | |
73 | m_error = "Unable to open COM port"; | |
74 | FormatWindowsError(GetLastError(), m_error); | |
75 | return false; | |
76 | } | |
77 | ||
78 | COMMCONFIG commConfig = {0}; | |
79 | DWORD dwSize = sizeof(commConfig); | |
80 | commConfig.dwSize = dwSize; | |
ff50391c | 81 | if (GetDefaultCommConfig(strComPath.c_str(), &commConfig,&dwSize)) |
abbca718 LOK |
82 | { |
83 | if (!SetCommConfig(m_handle, &commConfig,dwSize)) | |
84 | { | |
85 | m_error = "unable to set default config"; | |
86 | FormatWindowsError(GetLastError(), m_error); | |
87 | } | |
88 | } | |
89 | else | |
90 | { | |
91 | m_error = "unable to get default config"; | |
92 | FormatWindowsError(GetLastError(), m_error); | |
93 | } | |
94 | ||
95 | if (!SetupComm(m_handle, 64, 64)) | |
96 | { | |
97 | m_error = "unable to set up the com port"; | |
98 | FormatWindowsError(GetLastError(), m_error); | |
99 | } | |
100 | ||
101 | m_iDatabits = databits; | |
102 | m_iStopbits = stopbits; | |
103 | m_iParity = parity; | |
104 | if (!SetBaudRate(baudrate)) | |
105 | { | |
106 | m_error = "unable to set baud rate"; | |
107 | FormatWindowsError(GetLastError(), m_error); | |
108 | Close(); | |
109 | return false; | |
110 | } | |
111 | ||
112 | if (!SetTimeouts(false)) | |
113 | { | |
114 | m_error = "unable to set timeouts"; | |
115 | FormatWindowsError(GetLastError(), m_error); | |
116 | Close(); | |
117 | return false; | |
118 | } | |
119 | ||
120 | m_bIsOpen = true; | |
121 | return m_bIsOpen; | |
122 | } | |
123 | ||
124 | bool CSerialPort::SetTimeouts(bool bBlocking) | |
125 | { | |
126 | if (m_handle == INVALID_HANDLE_VALUE) | |
127 | return false; | |
128 | ||
129 | COMMTIMEOUTS cto; | |
130 | if (!GetCommTimeouts(m_handle, &cto)) | |
131 | { | |
132 | m_error = "GetCommTimeouts failed"; | |
133 | FormatWindowsError(GetLastError(), m_error); | |
134 | return false; | |
135 | } | |
136 | ||
137 | if (bBlocking) | |
138 | { | |
139 | cto.ReadIntervalTimeout = 0; | |
140 | cto.ReadTotalTimeoutConstant = 0; | |
141 | cto.ReadTotalTimeoutMultiplier = 0; | |
142 | } | |
143 | else | |
144 | { | |
145 | cto.ReadIntervalTimeout = MAXDWORD; | |
146 | cto.ReadTotalTimeoutConstant = 0; | |
147 | cto.ReadTotalTimeoutMultiplier = 0; | |
148 | } | |
149 | ||
150 | if (!SetCommTimeouts(m_handle, &cto)) | |
151 | { | |
152 | m_error = "SetCommTimeouts failed"; | |
153 | FormatWindowsError(GetLastError(), m_error); | |
154 | return false; | |
155 | } | |
156 | ||
157 | return true; | |
158 | } | |
159 | ||
160 | void CSerialPort::Close(void) | |
161 | { | |
8bca69de | 162 | CLockObject lock(&m_mutex); |
abbca718 LOK |
163 | if (m_bIsOpen) |
164 | { | |
165 | CloseHandle(m_handle); | |
166 | m_bIsOpen = false; | |
167 | } | |
168 | } | |
169 | ||
28352a04 | 170 | int8_t CSerialPort::Write(CCECAdapterMessage *data) |
abbca718 | 171 | { |
8bca69de | 172 | CLockObject lock(&m_mutex); |
abbca718 LOK |
173 | DWORD iBytesWritten = 0; |
174 | if (!m_bIsOpen) | |
175 | return -1; | |
176 | ||
56701628 | 177 | if (!WriteFile(m_handle, data->packet.data, data->size(), &iBytesWritten, NULL)) |
abbca718 LOK |
178 | { |
179 | m_error = "Error while writing to COM port"; | |
180 | FormatWindowsError(GetLastError(), m_error); | |
181 | return -1; | |
182 | } | |
183 | ||
25701fa6 | 184 | return (int8_t)iBytesWritten; |
abbca718 LOK |
185 | } |
186 | ||
8bca69de | 187 | int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = 0 */) |
abbca718 | 188 | { |
8bca69de LOK |
189 | CLockObject lock(&m_mutex); |
190 | int32_t iReturn(-1); | |
abbca718 LOK |
191 | DWORD iBytesRead = 0; |
192 | if (m_handle == 0) | |
193 | { | |
194 | m_error = "Error while reading from COM port: invalid handle"; | |
8bca69de | 195 | return iReturn; |
abbca718 LOK |
196 | } |
197 | ||
198 | if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0) | |
199 | { | |
200 | m_error = "unable to read from device"; | |
201 | FormatWindowsError(GetLastError(), m_error); | |
8bca69de LOK |
202 | iReturn = -1; |
203 | } | |
204 | else | |
205 | { | |
206 | iReturn = (int32_t) iBytesRead; | |
abbca718 LOK |
207 | } |
208 | ||
8bca69de | 209 | return iReturn; |
abbca718 LOK |
210 | } |
211 | ||
8bca69de | 212 | bool CSerialPort::SetBaudRate(uint32_t baudrate) |
abbca718 | 213 | { |
8bca69de LOK |
214 | int32_t rate = IntToBaudrate(baudrate); |
215 | if (rate < 0) | |
216 | m_iBaudrate = baudrate > 0 ? baudrate : 0; | |
217 | else | |
218 | m_iBaudrate = rate; | |
abbca718 LOK |
219 | |
220 | DCB dcb; | |
221 | memset(&dcb,0,sizeof(dcb)); | |
222 | dcb.DCBlength = sizeof(dcb); | |
b9187cc6 | 223 | dcb.BaudRate = IntToBaudrate(m_iBaudrate); |
abbca718 LOK |
224 | dcb.fBinary = true; |
225 | dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
226 | dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
227 | dcb.fOutxCtsFlow = false; | |
228 | dcb.fOutxDsrFlow = false; | |
229 | dcb.fOutX = false; | |
230 | dcb.fInX = false; | |
231 | dcb.fAbortOnError = true; | |
232 | ||
233 | if (m_iParity == PAR_NONE) | |
234 | dcb.Parity = NOPARITY; | |
235 | else if (m_iParity == PAR_EVEN) | |
236 | dcb.Parity = EVENPARITY; | |
237 | else | |
238 | dcb.Parity = ODDPARITY; | |
239 | ||
240 | if (m_iStopbits == 2) | |
241 | dcb.StopBits = TWOSTOPBITS; | |
242 | else | |
243 | dcb.StopBits = ONESTOPBIT; | |
244 | ||
245 | dcb.ByteSize = m_iDatabits; | |
246 | ||
247 | if(!SetCommState(m_handle,&dcb)) | |
248 | { | |
249 | m_error = "SetCommState failed"; | |
250 | FormatWindowsError(GetLastError(), m_error); | |
251 | return false; | |
252 | } | |
253 | ||
254 | return true; | |
255 | } | |
b9187cc6 | 256 | |
8bca69de | 257 | bool CSerialPort::IsOpen() |
b9187cc6 | 258 | { |
8bca69de | 259 | CLockObject lock(&m_mutex); |
b9187cc6 LOK |
260 | return m_bIsOpen; |
261 | } |