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