Commit | Line | Data |
---|---|---|
abbca718 LOK |
1 | /* |
2 | * boblight | |
3 | * Copyright (C) Bob 2009 | |
4 | * | |
5 | * boblight is free software: you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * boblight is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
13 | * See the GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
abbca718 | 19 | #include <stdio.h> |
abbca718 | 20 | #include <fcntl.h> |
abbca718 LOK |
21 | #include "../serialport.h" |
22 | #include "../baudrate.h" | |
b9187cc6 | 23 | #include "../timeutils.h" |
abbca718 LOK |
24 | |
25 | using namespace std; | |
b9187cc6 | 26 | using namespace CEC; |
abbca718 LOK |
27 | |
28 | CSerialPort::CSerialPort() | |
29 | { | |
b9187cc6 | 30 | m_fd = -1; |
abbca718 LOK |
31 | } |
32 | ||
33 | CSerialPort::~CSerialPort() | |
34 | { | |
35 | Close(); | |
36 | } | |
37 | ||
7eb13cca | 38 | int32_t CSerialPort::Write(uint8_t* data, uint32_t len) |
abbca718 LOK |
39 | { |
40 | fd_set port; | |
b1f50952 LOK |
41 | |
42 | CLockObject lock(&m_mutex); | |
abbca718 LOK |
43 | if (m_fd == -1) |
44 | { | |
45 | m_error = "port closed"; | |
46 | return -1; | |
47 | } | |
48 | ||
7eb13cca | 49 | int32_t byteswritten = 0; |
abbca718 | 50 | |
7eb13cca | 51 | while (byteswritten < (int32_t) len) |
abbca718 LOK |
52 | { |
53 | FD_ZERO(&port); | |
54 | FD_SET(m_fd, &port); | |
55 | int returnv = select(m_fd + 1, NULL, &port, NULL, NULL); | |
56 | if (returnv == -1) | |
57 | { | |
b9187cc6 | 58 | m_error = strerror(errno); |
abbca718 LOK |
59 | return -1; |
60 | } | |
61 | ||
62 | returnv = write(m_fd, data + byteswritten, len - byteswritten); | |
63 | if (returnv == -1) | |
64 | { | |
b9187cc6 | 65 | m_error = strerror(errno); |
abbca718 LOK |
66 | return -1; |
67 | } | |
68 | byteswritten += returnv; | |
69 | } | |
70 | ||
71 | //print what's written to stdout for debugging | |
72 | // if (m_tostdout) | |
73 | // { | |
74 | // printf("%s write:", m_name.c_str()); | |
75 | // for (int i = 0; i < byteswritten; i++) | |
76 | // printf(" %02x", (unsigned int)data[i]); | |
77 | ||
78 | // printf("\n"); | |
79 | // } | |
80 | ||
81 | return byteswritten; | |
82 | } | |
83 | ||
7eb13cca | 84 | int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/) |
abbca718 LOK |
85 | { |
86 | fd_set port; | |
87 | struct timeval timeout, *tv; | |
7eb13cca LOK |
88 | int64_t now(0), target(0); |
89 | int32_t bytesread = 0; | |
abbca718 | 90 | |
b1f50952 | 91 | CLockObject lock(&m_mutex); |
abbca718 LOK |
92 | if (m_fd == -1) |
93 | { | |
94 | m_error = "port closed"; | |
95 | return -1; | |
96 | } | |
97 | ||
7eb13cca | 98 | if (iTimeoutMs > 0) |
abbca718 LOK |
99 | { |
100 | now = GetTimeMs(); | |
101 | target = now + (int64_t) iTimeoutMs; | |
102 | } | |
103 | ||
7eb13cca | 104 | while (bytesread < (int32_t) len && (iTimeoutMs == 0 || target > now)) |
abbca718 | 105 | { |
7eb13cca | 106 | if (iTimeoutMs == 0) |
abbca718 LOK |
107 | { |
108 | tv = NULL; | |
109 | } | |
110 | else | |
111 | { | |
112 | timeout.tv_sec = ((long int)target - (long int)now) / (long int)1000.; | |
113 | timeout.tv_usec = ((long int)target - (long int)now) % (long int)1000.; | |
114 | tv = &timeout; | |
115 | } | |
116 | ||
117 | FD_ZERO(&port); | |
118 | FD_SET(m_fd, &port); | |
7eb13cca | 119 | int32_t returnv = select(m_fd + 1, &port, NULL, NULL, tv); |
abbca718 LOK |
120 | |
121 | if (returnv == -1) | |
122 | { | |
b9187cc6 | 123 | m_error = strerror(errno); |
abbca718 LOK |
124 | return -1; |
125 | } | |
126 | else if (returnv == 0) | |
127 | { | |
128 | break; //nothing to read | |
129 | } | |
130 | ||
131 | returnv = read(m_fd, data + bytesread, len - bytesread); | |
132 | if (returnv == -1) | |
133 | { | |
b9187cc6 | 134 | m_error = strerror(errno); |
abbca718 LOK |
135 | return -1; |
136 | } | |
137 | ||
138 | bytesread += returnv; | |
139 | ||
140 | if (iTimeoutMs > 0) | |
141 | now = GetTimeMs(); | |
142 | } | |
143 | ||
144 | //print what's read to stdout for debugging | |
145 | // if (m_tostdout && bytesread > 0) | |
146 | // { | |
147 | // printf("%s read:", m_name.c_str()); | |
148 | // for (int i = 0; i < bytesread; i++) | |
149 | // printf(" %02x", (unsigned int)data[i]); | |
150 | // | |
151 | // printf("\n"); | |
152 | // } | |
153 | ||
154 | return bytesread; | |
155 | } | |
156 | ||
157 | //setting all this stuff up is a pain in the ass | |
7eb13cca | 158 | bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits /* = 8 */, uint8_t stopbits /* = 1 */, uint8_t parity /* = PAR_NONE */) |
abbca718 LOK |
159 | { |
160 | m_name = name; | |
b9187cc6 | 161 | m_error = strerror(errno); |
b1f50952 LOK |
162 | CLockObject lock(&m_mutex); |
163 | ||
abbca718 LOK |
164 | if (databits < 5 || databits > 8) |
165 | { | |
166 | m_error = "Databits has to be between 5 and 8"; | |
167 | return false; | |
168 | } | |
169 | ||
170 | if (stopbits != 1 && stopbits != 2) | |
171 | { | |
172 | m_error = "Stopbits has to be 1 or 2"; | |
173 | return false; | |
174 | } | |
175 | ||
176 | if (parity != PAR_NONE && parity != PAR_EVEN && parity != PAR_ODD) | |
177 | { | |
178 | m_error = "Parity has to be none, even or odd"; | |
179 | return false; | |
180 | } | |
181 | ||
182 | m_fd = open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); | |
183 | ||
184 | if (m_fd == -1) | |
185 | { | |
b9187cc6 | 186 | m_error = strerror(errno); |
abbca718 LOK |
187 | return false; |
188 | } | |
189 | ||
190 | fcntl(m_fd, F_SETFL, 0); | |
191 | ||
192 | if (!SetBaudRate(baudrate)) | |
193 | { | |
194 | return false; | |
195 | } | |
196 | ||
197 | m_options.c_cflag |= (CLOCAL | CREAD); | |
198 | m_options.c_cflag &= ~HUPCL; | |
199 | ||
200 | m_options.c_cflag &= ~CSIZE; | |
201 | if (databits == 5) m_options.c_cflag |= CS5; | |
202 | if (databits == 6) m_options.c_cflag |= CS6; | |
203 | if (databits == 7) m_options.c_cflag |= CS7; | |
204 | if (databits == 8) m_options.c_cflag |= CS8; | |
205 | ||
206 | m_options.c_cflag &= ~PARENB; | |
207 | if (parity == PAR_EVEN || parity == PAR_ODD) | |
208 | m_options.c_cflag |= PARENB; | |
209 | if (parity == PAR_ODD) | |
210 | m_options.c_cflag |= PARODD; | |
211 | ||
212 | #ifdef CRTSCTS | |
213 | m_options.c_cflag &= ~CRTSCTS; | |
214 | #elif defined(CNEW_RTSCTS) | |
215 | m_options.c_cflag &= ~CNEW_RTSCTS; | |
216 | #endif | |
217 | ||
218 | if (stopbits == 1) m_options.c_cflag &= ~CSTOPB; | |
219 | else m_options.c_cflag |= CSTOPB; | |
220 | ||
221 | //I guessed a little here | |
222 | m_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | XCASE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | TOSTOP); | |
223 | ||
224 | if (parity == PAR_NONE) | |
225 | { | |
226 | m_options.c_iflag &= ~INPCK; | |
227 | } | |
228 | else | |
229 | { | |
230 | m_options.c_iflag |= INPCK | ISTRIP; | |
231 | } | |
232 | ||
233 | m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL); | |
234 | m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL); | |
235 | ||
236 | if (tcsetattr(m_fd, TCSANOW, &m_options) != 0) | |
237 | { | |
b9187cc6 | 238 | m_error = strerror(errno); |
abbca718 LOK |
239 | return false; |
240 | } | |
241 | ||
242 | //non-blocking port | |
243 | fcntl(m_fd, F_SETFL, FNDELAY); | |
244 | ||
245 | return true; | |
246 | } | |
247 | ||
248 | void CSerialPort::Close() | |
249 | { | |
b1f50952 | 250 | CLockObject lock(&m_mutex); |
abbca718 LOK |
251 | if (m_fd != -1) |
252 | { | |
253 | close(m_fd); | |
254 | m_fd = -1; | |
255 | m_name = ""; | |
256 | m_error = ""; | |
257 | } | |
258 | } | |
259 | ||
7eb13cca | 260 | bool CSerialPort::SetBaudRate(uint32_t baudrate) |
abbca718 | 261 | { |
b9187cc6 | 262 | int rate = IntToBaudrate(baudrate); |
abbca718 LOK |
263 | if (rate == -1) |
264 | { | |
265 | char buff[255]; | |
266 | sprintf(buff, "%i is not a valid baudrate", baudrate); | |
267 | m_error = buff; | |
268 | return false; | |
269 | } | |
270 | ||
271 | //get the current port attributes | |
272 | if (tcgetattr(m_fd, &m_options) != 0) | |
273 | { | |
b9187cc6 | 274 | m_error = strerror(errno); |
abbca718 LOK |
275 | return false; |
276 | } | |
277 | ||
278 | if (cfsetispeed(&m_options, rate) != 0) | |
279 | { | |
b9187cc6 | 280 | m_error = strerror(errno); |
abbca718 LOK |
281 | return false; |
282 | } | |
283 | ||
284 | if (cfsetospeed(&m_options, rate) != 0) | |
285 | { | |
b9187cc6 | 286 | m_error = strerror(errno); |
abbca718 LOK |
287 | return false; |
288 | } | |
289 | ||
290 | return true; | |
291 | } | |
b9187cc6 | 292 | |
b1f50952 | 293 | bool CSerialPort::IsOpen() |
b9187cc6 | 294 | { |
b1f50952 | 295 | CLockObject lock(&m_mutex); |
b9187cc6 LOK |
296 | return m_fd != -1; |
297 | } |