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