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