f2d28c5b7340256f652a59931451cf2f2c9c8c0c
[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 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 bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking)
52 {
53 if (socket == INVALID_HANDLE_VALUE)
54 return false;
55
56 COMMTIMEOUTS cto;
57 if (!GetCommTimeouts(socket, &cto))
58 {
59 *iError = GetLastError();
60 return false;
61 }
62
63 if (bBlocking)
64 {
65 cto.ReadIntervalTimeout = 0;
66 cto.ReadTotalTimeoutConstant = 0;
67 cto.ReadTotalTimeoutMultiplier = 0;
68 }
69 else
70 {
71 cto.ReadIntervalTimeout = MAXDWORD;
72 cto.ReadTotalTimeoutConstant = 0;
73 cto.ReadTotalTimeoutMultiplier = 0;
74 }
75
76 if (!SetCommTimeouts(socket, &cto))
77 {
78 *iError = GetLastError();
79 return false;
80 }
81
82 return true;
83 }
84
85 void CSerialSocket::Close(void)
86 {
87 if (IsOpen())
88 SerialSocketClose(m_socket);
89 }
90
91 void CSerialSocket::Shutdown(void)
92 {
93 if (IsOpen())
94 SerialSocketClose(m_socket);
95 }
96
97 ssize_t CSerialSocket::Write(void* data, size_t len)
98 {
99 return IsOpen() ? SerialSocketWrite(m_socket, &m_iError, data, len) : -1;
100 }
101
102 ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
103 {
104 return IsOpen() ? SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1;
105 }
106
107 bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
108 {
109 iTimeoutMs = 0;
110 if (IsOpen())
111 return false;
112
113 CStdString strComPath = "\\\\.\\" + m_strName;
114 CLockObject lock(m_mutex);
115 m_socket = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
116 if (m_socket == INVALID_HANDLE_VALUE)
117 {
118 m_strError = "Unable to open COM port";
119 FormatWindowsError(GetLastError(), m_strError);
120 return false;
121 }
122
123 COMMCONFIG commConfig = {0};
124 DWORD dwSize = sizeof(commConfig);
125 commConfig.dwSize = dwSize;
126 if (GetDefaultCommConfig(strComPath.c_str(), &commConfig,&dwSize))
127 {
128 if (!SetCommConfig(m_socket, &commConfig,dwSize))
129 {
130 m_strError = "unable to set default config";
131 FormatWindowsError(GetLastError(), m_strError);
132 }
133 }
134 else
135 {
136 m_strError = "unable to get default config";
137 FormatWindowsError(GetLastError(), m_strError);
138 }
139
140 if (!SetupComm(m_socket, 64, 64))
141 {
142 m_strError = "unable to set up the com port";
143 FormatWindowsError(GetLastError(), m_strError);
144 }
145
146 if (!SetBaudRate(m_iBaudrate))
147 {
148 m_strError = "unable to set baud rate";
149 FormatWindowsError(GetLastError(), m_strError);
150 Close();
151 return false;
152 }
153
154 if (!SetTimeouts(m_socket, &m_iError, false))
155 {
156 m_strError = "unable to set timeouts";
157 FormatWindowsError(GetLastError(), m_strError);
158 Close();
159 return false;
160 }
161
162 m_bIsOpen = true;
163 return m_bIsOpen;
164 }
165
166 bool CSerialSocket::SetBaudRate(uint32_t baudrate)
167 {
168 int32_t rate = IntToBaudrate(baudrate);
169 if (rate < 0)
170 m_iBaudrate = baudrate > 0 ? baudrate : 0;
171 else
172 m_iBaudrate = rate;
173
174 DCB dcb;
175 memset(&dcb,0,sizeof(dcb));
176 dcb.DCBlength = sizeof(dcb);
177 dcb.BaudRate = IntToBaudrate(m_iBaudrate);
178 dcb.fBinary = true;
179 dcb.fDtrControl = DTR_CONTROL_DISABLE;
180 dcb.fRtsControl = RTS_CONTROL_DISABLE;
181 dcb.fOutxCtsFlow = false;
182 dcb.fOutxDsrFlow = false;
183 dcb.fOutX = false;
184 dcb.fInX = false;
185 dcb.fAbortOnError = true;
186
187 if (m_iParity == SERIAL_PARITY_NONE)
188 dcb.Parity = NOPARITY;
189 else if (m_iParity == SERIAL_PARITY_EVEN)
190 dcb.Parity = EVENPARITY;
191 else
192 dcb.Parity = ODDPARITY;
193
194 if (m_iStopbits == SERIAL_STOP_BITS_TWO)
195 dcb.StopBits = TWOSTOPBITS;
196 else
197 dcb.StopBits = ONESTOPBIT;
198
199 dcb.ByteSize = (BYTE)m_iDatabits;
200
201 if(!SetCommState(m_socket,&dcb))
202 {
203 m_strError = "SetCommState failed";
204 FormatWindowsError(GetLastError(), m_strError);
205 return false;
206 }
207
208 return true;
209 }