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