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" | |
abbca718 | 38 | |
6df2c52f | 39 | #if defined(__APPLE__) |
40 | #ifndef XCASE | |
41 | #define XCASE 0 | |
42 | #endif | |
43 | #ifndef OLCUC | |
44 | #define OLCUC 0 | |
45 | #endif | |
46 | #ifndef IUCLC | |
47 | #define IUCLC 0 | |
48 | #endif | |
49 | #endif | |
abbca718 | 50 | using namespace std; |
f00ff009 | 51 | using namespace PLATFORM; |
abbca718 LOK |
52 | |
53 | CSerialPort::CSerialPort() | |
54 | { | |
0e51267b | 55 | m_bToStdOut = false; |
abbca718 LOK |
56 | } |
57 | ||
abbca718 | 58 | //setting all this stuff up is a pain in the ass |
7eb13cca | 59 | bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits /* = 8 */, uint8_t stopbits /* = 1 */, uint8_t parity /* = PAR_NONE */) |
abbca718 | 60 | { |
0e51267b | 61 | m_strName = name; |
f00ff009 | 62 | CLockObject lock(m_mutex); |
b1f50952 | 63 | |
abbca718 LOK |
64 | if (databits < 5 || databits > 8) |
65 | { | |
ba65909d | 66 | m_strError = "Databits has to be between 5 and 8"; |
abbca718 LOK |
67 | return false; |
68 | } | |
69 | ||
70 | if (stopbits != 1 && stopbits != 2) | |
71 | { | |
ba65909d | 72 | m_strError = "Stopbits has to be 1 or 2"; |
abbca718 LOK |
73 | return false; |
74 | } | |
75 | ||
76 | if (parity != PAR_NONE && parity != PAR_EVEN && parity != PAR_ODD) | |
77 | { | |
ba65909d | 78 | m_strError = "Parity has to be none, even or odd"; |
abbca718 LOK |
79 | return false; |
80 | } | |
81 | ||
ba65909d | 82 | m_socket = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); |
abbca718 | 83 | |
ba65909d | 84 | if (m_socket == INVALID_SOCKET) |
abbca718 | 85 | { |
ba65909d | 86 | m_strError = strerror(errno); |
abbca718 LOK |
87 | return false; |
88 | } | |
89 | ||
ba65909d | 90 | SocketSetBlocking(m_socket, false); |
abbca718 LOK |
91 | |
92 | if (!SetBaudRate(baudrate)) | |
abbca718 | 93 | return false; |
abbca718 LOK |
94 | |
95 | m_options.c_cflag |= (CLOCAL | CREAD); | |
96 | m_options.c_cflag &= ~HUPCL; | |
97 | ||
98 | m_options.c_cflag &= ~CSIZE; | |
99 | if (databits == 5) m_options.c_cflag |= CS5; | |
100 | if (databits == 6) m_options.c_cflag |= CS6; | |
101 | if (databits == 7) m_options.c_cflag |= CS7; | |
102 | if (databits == 8) m_options.c_cflag |= CS8; | |
103 | ||
104 | m_options.c_cflag &= ~PARENB; | |
105 | if (parity == PAR_EVEN || parity == PAR_ODD) | |
106 | m_options.c_cflag |= PARENB; | |
107 | if (parity == PAR_ODD) | |
108 | m_options.c_cflag |= PARODD; | |
109 | ||
110 | #ifdef CRTSCTS | |
111 | m_options.c_cflag &= ~CRTSCTS; | |
112 | #elif defined(CNEW_RTSCTS) | |
113 | m_options.c_cflag &= ~CNEW_RTSCTS; | |
114 | #endif | |
115 | ||
116 | if (stopbits == 1) m_options.c_cflag &= ~CSTOPB; | |
117 | else m_options.c_cflag |= CSTOPB; | |
118 | ||
119 | //I guessed a little here | |
120 | m_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | XCASE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | TOSTOP); | |
121 | ||
122 | if (parity == PAR_NONE) | |
123 | { | |
124 | m_options.c_iflag &= ~INPCK; | |
125 | } | |
126 | else | |
127 | { | |
128 | m_options.c_iflag |= INPCK | ISTRIP; | |
129 | } | |
130 | ||
131 | m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL); | |
132 | m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL); | |
133 | ||
ba65909d | 134 | if (tcsetattr(m_socket, TCSANOW, &m_options) != 0) |
abbca718 | 135 | { |
ba65909d | 136 | m_strError = strerror(errno); |
abbca718 LOK |
137 | return false; |
138 | } | |
139 | ||
ba65909d | 140 | SocketSetBlocking(m_socket, true); |
abbca718 LOK |
141 | |
142 | return true; | |
143 | } | |
144 | ||
7eb13cca | 145 | bool CSerialPort::SetBaudRate(uint32_t baudrate) |
abbca718 | 146 | { |
b9187cc6 | 147 | int rate = IntToBaudrate(baudrate); |
abbca718 LOK |
148 | if (rate == -1) |
149 | { | |
150 | char buff[255]; | |
151 | sprintf(buff, "%i is not a valid baudrate", baudrate); | |
ba65909d | 152 | m_strError = buff; |
abbca718 LOK |
153 | return false; |
154 | } | |
155 | ||
156 | //get the current port attributes | |
ba65909d | 157 | if (tcgetattr(m_socket, &m_options) != 0) |
abbca718 | 158 | { |
ba65909d | 159 | m_strError = strerror(errno); |
abbca718 LOK |
160 | return false; |
161 | } | |
162 | ||
163 | if (cfsetispeed(&m_options, rate) != 0) | |
164 | { | |
ba65909d | 165 | m_strError = strerror(errno); |
abbca718 LOK |
166 | return false; |
167 | } | |
168 | ||
169 | if (cfsetospeed(&m_options, rate) != 0) | |
170 | { | |
ba65909d | 171 | m_strError = strerror(errno); |
abbca718 LOK |
172 | return false; |
173 | } | |
174 | ||
175 | return true; | |
176 | } |