Commit | Line | Data |
---|---|---|
abbca718 | 1 | /* |
f7febb0e LOK |
2 | * This file is part of the libCEC(R) library. |
3 | * | |
9b53a148 | 4 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
f7febb0e 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 | |
abbca718 | 12 | * (at your option) any later version. |
f7febb0e LOK |
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/ | |
abbca718 LOK |
31 | */ |
32 | ||
ba65909d | 33 | #include "../os.h" |
abbca718 | 34 | #include <stdio.h> |
abbca718 | 35 | #include <fcntl.h> |
ba65909d LOK |
36 | #include "../sockets/serialport.h" |
37 | #include "../util/baudrate.h" | |
99666519 | 38 | #include "../posix/os-socket.h" |
abbca718 | 39 | |
5458fd77 | 40 | #if defined(__APPLE__) || defined(__FreeBSD__) |
6df2c52f | 41 | #ifndef XCASE |
42 | #define XCASE 0 | |
43 | #endif | |
44 | #ifndef OLCUC | |
45 | #define OLCUC 0 | |
46 | #endif | |
47 | #ifndef IUCLC | |
48 | #define IUCLC 0 | |
49 | #endif | |
f9e70e9f LOK |
50 | #else |
51 | #include <lockdev.h> | |
6df2c52f | 52 | #endif |
f9e70e9f | 53 | |
abbca718 | 54 | using namespace std; |
f00ff009 | 55 | using namespace PLATFORM; |
abbca718 | 56 | |
99666519 | 57 | void CSerialSocket::Close(void) |
abbca718 | 58 | { |
48c1be3c | 59 | if (IsOpen()) |
f9e70e9f | 60 | { |
48c1be3c | 61 | SocketClose(m_socket); |
f9e70e9f LOK |
62 | #if !defined(__APPLE__) && !defined(__FreeBSD__) |
63 | dev_unlock(m_strName.c_str(), m_lockPid); | |
64 | #endif | |
65 | } | |
99666519 LOK |
66 | } |
67 | ||
68 | void CSerialSocket::Shutdown(void) | |
69 | { | |
48c1be3c | 70 | if (IsOpen()) |
f9e70e9f | 71 | { |
48c1be3c | 72 | SocketClose(m_socket); |
f9e70e9f LOK |
73 | #if !defined(__APPLE__) && !defined(__FreeBSD__) |
74 | dev_unlock(m_strName.c_str(), m_lockPid); | |
75 | #endif | |
76 | } | |
99666519 LOK |
77 | } |
78 | ||
79 | ssize_t CSerialSocket::Write(void* data, size_t len) | |
80 | { | |
48c1be3c | 81 | return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1; |
99666519 LOK |
82 | } |
83 | ||
84 | ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */) | |
85 | { | |
48c1be3c | 86 | return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1; |
abbca718 LOK |
87 | } |
88 | ||
abbca718 | 89 | //setting all this stuff up is a pain in the ass |
99666519 | 90 | bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */) |
abbca718 | 91 | { |
99666519 | 92 | iTimeoutMs = 0; |
48c1be3c | 93 | if (IsOpen()) |
f9e70e9f LOK |
94 | { |
95 | m_iError = EINVAL; | |
48c1be3c | 96 | return false; |
f9e70e9f | 97 | } |
b1f50952 | 98 | |
99666519 LOK |
99 | if (m_iDatabits != SERIAL_DATA_BITS_FIVE && m_iDatabits != SERIAL_DATA_BITS_SIX && |
100 | m_iDatabits != SERIAL_DATA_BITS_SEVEN && m_iDatabits != SERIAL_DATA_BITS_EIGHT) | |
abbca718 | 101 | { |
ba65909d | 102 | m_strError = "Databits has to be between 5 and 8"; |
f9e70e9f | 103 | m_iError = EINVAL; |
abbca718 LOK |
104 | return false; |
105 | } | |
106 | ||
99666519 | 107 | if (m_iStopbits != SERIAL_STOP_BITS_ONE && m_iStopbits != SERIAL_STOP_BITS_TWO) |
abbca718 | 108 | { |
ba65909d | 109 | m_strError = "Stopbits has to be 1 or 2"; |
f9e70e9f | 110 | m_iError = EINVAL; |
abbca718 LOK |
111 | return false; |
112 | } | |
113 | ||
99666519 | 114 | if (m_iParity != SERIAL_PARITY_NONE && m_iParity != SERIAL_PARITY_EVEN && m_iParity != SERIAL_PARITY_ODD) |
abbca718 | 115 | { |
ba65909d | 116 | m_strError = "Parity has to be none, even or odd"; |
f9e70e9f LOK |
117 | m_iError = EINVAL; |
118 | return false; | |
119 | } | |
120 | ||
121 | #if !defined(__APPLE__) && !defined(__FreeBSD__) | |
122 | m_lockPid = dev_lock(m_strName.c_str()); | |
123 | if (m_lockPid != 0) | |
124 | { | |
125 | m_strError = "Couldn't lock the serial port"; | |
126 | m_iError = EBUSY; | |
abbca718 LOK |
127 | return false; |
128 | } | |
f9e70e9f | 129 | #endif |
abbca718 | 130 | |
99666519 | 131 | m_socket = open(m_strName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); |
abbca718 | 132 | |
99666519 | 133 | if (m_socket == INVALID_SERIAL_SOCKET_VALUE) |
abbca718 | 134 | { |
ba65909d | 135 | m_strError = strerror(errno); |
f9e70e9f LOK |
136 | #if !defined(__APPLE__) && !defined(__FreeBSD__) |
137 | m_lockPid = dev_unlock(m_strName.c_str(), m_lockPid); | |
138 | #endif | |
abbca718 LOK |
139 | return false; |
140 | } | |
141 | ||
ba65909d | 142 | SocketSetBlocking(m_socket, false); |
abbca718 | 143 | |
99666519 | 144 | if (!SetBaudRate(m_iBaudrate)) |
abbca718 | 145 | return false; |
abbca718 LOK |
146 | |
147 | m_options.c_cflag |= (CLOCAL | CREAD); | |
148 | m_options.c_cflag &= ~HUPCL; | |
149 | ||
150 | m_options.c_cflag &= ~CSIZE; | |
99666519 LOK |
151 | if (m_iDatabits == SERIAL_DATA_BITS_FIVE) m_options.c_cflag |= CS5; |
152 | if (m_iDatabits == SERIAL_DATA_BITS_SIX) m_options.c_cflag |= CS6; | |
153 | if (m_iDatabits == SERIAL_DATA_BITS_SEVEN) m_options.c_cflag |= CS7; | |
154 | if (m_iDatabits == SERIAL_DATA_BITS_EIGHT) m_options.c_cflag |= CS8; | |
abbca718 LOK |
155 | |
156 | m_options.c_cflag &= ~PARENB; | |
99666519 | 157 | if (m_iParity == SERIAL_PARITY_EVEN || m_iParity == SERIAL_PARITY_ODD) |
abbca718 | 158 | m_options.c_cflag |= PARENB; |
99666519 | 159 | if (m_iParity == SERIAL_PARITY_ODD) |
abbca718 LOK |
160 | m_options.c_cflag |= PARODD; |
161 | ||
162 | #ifdef CRTSCTS | |
163 | m_options.c_cflag &= ~CRTSCTS; | |
164 | #elif defined(CNEW_RTSCTS) | |
165 | m_options.c_cflag &= ~CNEW_RTSCTS; | |
166 | #endif | |
167 | ||
99666519 | 168 | if (m_iStopbits == SERIAL_STOP_BITS_ONE) m_options.c_cflag &= ~CSTOPB; |
abbca718 LOK |
169 | else m_options.c_cflag |= CSTOPB; |
170 | ||
171 | //I guessed a little here | |
172 | m_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | XCASE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | TOSTOP); | |
173 | ||
99666519 | 174 | if (m_iParity == SERIAL_PARITY_NONE) |
abbca718 | 175 | m_options.c_iflag &= ~INPCK; |
abbca718 | 176 | else |
abbca718 | 177 | m_options.c_iflag |= INPCK | ISTRIP; |
abbca718 LOK |
178 | |
179 | m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL); | |
180 | m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL); | |
181 | ||
ba65909d | 182 | if (tcsetattr(m_socket, TCSANOW, &m_options) != 0) |
abbca718 | 183 | { |
ba65909d | 184 | m_strError = strerror(errno); |
f9e70e9f LOK |
185 | #if !defined(__APPLE__) && !defined(__FreeBSD__) |
186 | m_lockPid = dev_unlock(m_strName.c_str(), m_lockPid); | |
187 | #endif | |
abbca718 LOK |
188 | return false; |
189 | } | |
190 | ||
ba65909d | 191 | SocketSetBlocking(m_socket, true); |
99666519 | 192 | m_bIsOpen = true; |
abbca718 LOK |
193 | |
194 | return true; | |
195 | } | |
196 | ||
99666519 | 197 | bool CSerialSocket::SetBaudRate(uint32_t baudrate) |
abbca718 | 198 | { |
b9187cc6 | 199 | int rate = IntToBaudrate(baudrate); |
abbca718 LOK |
200 | if (rate == -1) |
201 | { | |
202 | char buff[255]; | |
203 | sprintf(buff, "%i is not a valid baudrate", baudrate); | |
ba65909d | 204 | m_strError = buff; |
abbca718 LOK |
205 | return false; |
206 | } | |
99666519 | 207 | |
abbca718 | 208 | //get the current port attributes |
ba65909d | 209 | if (tcgetattr(m_socket, &m_options) != 0) |
abbca718 | 210 | { |
ba65909d | 211 | m_strError = strerror(errno); |
abbca718 LOK |
212 | return false; |
213 | } | |
214 | ||
215 | if (cfsetispeed(&m_options, rate) != 0) | |
216 | { | |
ba65909d | 217 | m_strError = strerror(errno); |
abbca718 LOK |
218 | return false; |
219 | } | |
99666519 | 220 | |
abbca718 LOK |
221 | if (cfsetospeed(&m_options, rate) != 0) |
222 | { | |
ba65909d | 223 | m_strError = strerror(errno); |
abbca718 LOK |
224 | return false; |
225 | } | |
226 | ||
227 | return true; | |
228 | } |