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