blob: 75297f72a4678ccf6c86c977fbd215061621c78c [file] [log] [blame]
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com>
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +00005 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000022#include "platform.h"
23
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000024#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000027#include <string.h>
28#include <ctype.h>
29#include <fcntl.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000030#include <sys/stat.h>
31#include <errno.h>
32#include <inttypes.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000033#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +000034#include <conio.h>
35#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000036#include <termios.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000037#include <unistd.h>
38#include <sys/types.h>
39#include <sys/ioctl.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000040#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000041#include "flash.h"
42#include "programmer.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000043
Stefan Taunere33c40e2013-04-13 00:29:30 +000044fdtype sp_fd = SER_INV_FD;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000045
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000046#if IS_WINDOWS
Stefan Taunerda5b17c2013-04-01 00:45:32 +000047struct baudentry {
48 DWORD flag;
49 unsigned int baud;
50};
51#define BAUDENTRY(baud) { CBR_##baud, baud },
52#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000053struct baudentry {
54 int flag;
55 unsigned int baud;
56};
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000057#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000058#endif
59
60/* I'd like if the C preprocessor could have directives in macros.
61 * See TERMIOS(3) and http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx and
62 * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h */
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000063static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000064 BAUDENTRY(9600) /* unconditional default */
65#if defined(B19200) || defined(CBR_19200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000066 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000067#endif
68#if defined(B38400) || defined(CBR_38400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000069 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000070#endif
71#if defined(B57600) || defined(CBR_57600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000072 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000073#endif
74#if defined(B115200) || defined(CBR_115200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000075 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000076#endif
77#if defined(B230400) || defined(CBR_230400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000078 BAUDENTRY(230400)
79#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000080#if defined(B460800) || defined(CBR_460800)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000081 BAUDENTRY(460800)
82#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000083#if defined(B500000) || defined(CBR_500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000084 BAUDENTRY(500000)
85#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000086#if defined(B576000) || defined(CBR_576000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000087 BAUDENTRY(576000)
88#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000089#if defined(B921600) || defined(CBR_921600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000090 BAUDENTRY(921600)
91#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000092#if defined(B1000000) || defined(CBR_1000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000093 BAUDENTRY(1000000)
94#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000095#if defined(B1152000) || defined(CBR_1152000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000096 BAUDENTRY(1152000)
97#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000098#if defined(B1500000) || defined(CBR_1500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000099 BAUDENTRY(1500000)
100#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000101#if defined(B2000000) || defined(CBR_2000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000102 BAUDENTRY(2000000)
103#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000104#if defined(B2500000) || defined(CBR_2500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000105 BAUDENTRY(2500000)
106#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000107#if defined(B3000000) || defined(CBR_3000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000108 BAUDENTRY(3000000)
109#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000110#if defined(B3500000) || defined(CBR_3500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000111 BAUDENTRY(3500000)
112#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000113#if defined(B4000000) || defined(CBR_4000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000114 BAUDENTRY(4000000)
115#endif
116 {0, 0} /* Terminator */
117};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000118
Stefan Tauner72587f82016-01-04 03:05:15 +0000119static const struct baudentry *round_baud(unsigned int baud)
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000120{
121 int i;
122 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
123 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
124 if (sp_baudtable[i].baud == baud)
125 return &sp_baudtable[i];
126
127 if (sp_baudtable[i].baud < baud) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000128 msg_pwarn("Warning: given baudrate %d rounded down to %d.\n",
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000129 baud, sp_baudtable[i].baud);
130 return &sp_baudtable[i];
131 }
132 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000133 msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud);
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000134 return &sp_baudtable[0];
135}
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000136
Stefan Taunerbf88be92013-04-01 00:45:08 +0000137/* Uses msg_perr to print the last system error.
138 * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
139 * strerror() or FormatMessage() and ending with a linebreak. */
140static void msg_perr_strerror(const char *msg)
141{
142 msg_perr("Error: %s", msg);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000143#if IS_WINDOWS
Stefan Taunerbf88be92013-04-01 00:45:08 +0000144 char *lpMsgBuf;
145 DWORD nErr = GetLastError();
146 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
147 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
148 msg_perr(lpMsgBuf);
149 /* At least some formatted messages contain a line break at the end. Make sure to always print one */
150 if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
151 msg_perr("\n");
152 LocalFree(lpMsgBuf);
153#else
154 msg_perr("%s\n", strerror(errno));
155#endif
156}
157
Stefan Tauner72587f82016-01-04 03:05:15 +0000158int serialport_config(fdtype fd, int baud)
Stefan Tauner184c52c2013-08-23 21:51:32 +0000159{
160 if (fd == SER_INV_FD) {
161 msg_perr("%s: File descriptor is invalid.\n", __func__);
162 return 1;
163 }
164
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000165#if IS_WINDOWS
Stefan Tauner184c52c2013-08-23 21:51:32 +0000166 DCB dcb;
167 if (!GetCommState(fd, &dcb)) {
168 msg_perr_strerror("Could not fetch original serial port configuration: ");
169 return 1;
170 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000171 if (baud >= 0) {
172 const struct baudentry *entry = round_baud(baud);
173 dcb.BaudRate = entry->flag;
174 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000175 dcb.ByteSize = 8;
176 dcb.Parity = NOPARITY;
177 dcb.StopBits = ONESTOPBIT;
178 if (!SetCommState(fd, &dcb)) {
179 msg_perr_strerror("Could not change serial port configuration: ");
180 return 1;
181 }
182 if (!GetCommState(fd, &dcb)) {
183 msg_perr_strerror("Could not fetch new serial port configuration: ");
184 return 1;
185 }
186 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
187#else
188 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000189 if (tcgetattr(fd, &observed) != 0) {
190 msg_perr_strerror("Could not fetch original serial port configuration: ");
191 return 1;
192 }
193 wanted = observed;
Stefan Tauner72587f82016-01-04 03:05:15 +0000194 if (baud >= 0) {
195 const struct baudentry *entry = round_baud(baud);
196 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
197 msg_perr_strerror("Could not set serial baud rate: ");
198 return 1;
199 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000200 }
201 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
202 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
203 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
204 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
205 wanted.c_oflag &= ~OPOST;
206 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
207 msg_perr_strerror("Could not change serial port configuration: ");
208 return 1;
209 }
210 if (tcgetattr(fd, &observed) != 0) {
211 msg_perr_strerror("Could not fetch new serial port configuration: ");
212 return 1;
213 }
214 if (observed.c_cflag != wanted.c_cflag ||
215 observed.c_lflag != wanted.c_lflag ||
216 observed.c_iflag != wanted.c_iflag ||
Stefan Tauner631bb022016-01-04 03:05:06 +0000217 observed.c_oflag != wanted.c_oflag) {
218 msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
219 msg_pdbg(" observed wanted\n"
220 "c_cflag: 0x%08lX 0x%08lX\n"
221 "c_lflag: 0x%08lX 0x%08lX\n"
222 "c_iflag: 0x%08lX 0x%08lX\n"
223 "c_oflag: 0x%08lX 0x%08lX\n",
224 (long)observed.c_cflag, (long)wanted.c_cflag,
225 (long)observed.c_lflag, (long)wanted.c_lflag,
226 (long)observed.c_iflag, (long)wanted.c_iflag,
227 (long)observed.c_oflag, (long)wanted.c_oflag
228 );
Stefan Tauner184c52c2013-08-23 21:51:32 +0000229 }
Stefan Tauner631bb022016-01-04 03:05:06 +0000230 if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
231 cfgetospeed(&observed) != cfgetospeed(&wanted)) {
232 msg_pwarn("Could not set baud rates exactly.\n");
233 msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
234 (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
235 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000236#endif
237 return 0;
238}
239
Stefan Tauner72587f82016-01-04 03:05:15 +0000240fdtype sp_openserport(char *dev, int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000241{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000242 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000243#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000244 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000245 if ((strlen(dev) > 3) &&
246 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000247 (tolower((unsigned char)dev[1]) == 'o') &&
248 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000249 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000250 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000251 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000252 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000253 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000254 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000255 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000256 }
Uwe Hermann43959702010-03-13 17:28:29 +0000257 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
258 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000259 if (dev2 != dev)
260 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000261 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000262 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000263 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000264 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000265 if (serialport_config(fd, baud) != 0) {
266 CloseHandle(fd);
267 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000268 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000269 return fd;
270#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000271 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000272 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000273 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000274 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000275 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000276
277 /* Ensure that we use blocking I/O */
278 const int flags = fcntl(fd, F_GETFL);
279 if (flags == -1) {
280 msg_perr_strerror("Could not get serial port mode: ");
281 return SER_INV_FD;
282 }
283 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
284 msg_perr_strerror("Could not set serial port mode to blocking: ");
285 return SER_INV_FD;
286 }
287
Stefan Tauner184c52c2013-08-23 21:51:32 +0000288 if (serialport_config(fd, baud) != 0) {
289 close(fd);
290 return SER_INV_FD;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000291 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000292 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000293#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000294}
295
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000296void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000297#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000298 DWORD ctl;
299
300 if(pin == PIN_TXD) {
301 ctl = val ? SETBREAK: CLRBREAK;
302 }
303 else if(pin == PIN_DTR) {
304 ctl = val ? SETDTR: CLRDTR;
305 }
306 else {
307 ctl = val ? SETRTS: CLRRTS;
308 }
309 EscapeCommFunction(sp_fd, ctl);
310#else
311 int ctl, s;
312
313 if(pin == PIN_TXD) {
314 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
315 }
316 else {
317 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
318 ioctl(sp_fd, TIOCMGET, &ctl);
319
320 if (val) {
321 ctl |= s;
322 }
323 else {
324 ctl &= ~s;
325 }
326 ioctl(sp_fd, TIOCMSET, &ctl);
327 }
328#endif
329}
330
331int sp_get_pin(enum SP_PIN pin) {
332 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000333#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000334 DWORD ctl;
335
336 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
337 GetCommModemStatus(sp_fd, &ctl);
338#else
339 int ctl;
340 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
341 ioctl(sp_fd, TIOCMGET, &ctl);
342#endif
343
344 return ((ctl & s) ? 1 : 0);
345
346}
347
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000348void sp_flush_incoming(void)
349{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000350#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000351 PurgeComm(sp_fd, PURGE_RXCLEAR);
352#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000353 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000354 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000355#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000356 return;
357}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000358
David Hendricks8bb20212011-06-14 01:35:36 +0000359int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000360{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000361#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000362 CloseHandle(sp_fd);
363#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000364 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000365#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000366 return 0;
367}
368
Mark Marshallf20b7be2014-05-09 21:16:21 +0000369int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000370{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000371#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000372 DWORD tmp = 0;
373#else
374 ssize_t tmp = 0;
375#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000376 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000377
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000378 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000379#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000380 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
381#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000382 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000383#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000384 if (tmp == -1) {
385 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000386 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000387 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000388 if (!tmp) {
389 msg_pdbg2("Empty write\n");
390 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000391 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000392 if (empty_writes == 0) {
393 msg_perr("Serial port is unresponsive!\n");
394 return 1;
395 }
396 }
397 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000398 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000399 }
400
401 return 0;
402}
403
404int serialport_read(unsigned char *buf, unsigned int readcnt)
405{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000406#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000407 DWORD tmp = 0;
408#else
409 ssize_t tmp = 0;
410#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000411
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000412 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000413#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000414 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
415#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000416 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000417#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000418 if (tmp == -1) {
419 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000420 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000421 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000422 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000423 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000424 readcnt -= tmp;
425 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000426 }
427
428 return 0;
429}
Stefan Tauner00e16082013-04-01 00:45:38 +0000430
431/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
432 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
433 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
434int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
435{
436 int ret = 1;
437 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000438#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000439 DWORD rv;
440 COMMTIMEOUTS oldTimeout;
441 COMMTIMEOUTS newTimeout = {
442 .ReadIntervalTimeout = MAXDWORD,
443 .ReadTotalTimeoutMultiplier = 0,
444 .ReadTotalTimeoutConstant = 0,
445 .WriteTotalTimeoutMultiplier = 0,
446 .WriteTotalTimeoutConstant = 0
447 };
448 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
449 msg_perr_strerror("Could not get serial port timeout settings: ");
450 return -1;
451 }
452 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000453 msg_perr_strerror("Could not set serial port timeout settings: ");
454 return -1;
455 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000456#else
457 ssize_t rv;
458 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000459 if (flags == -1) {
460 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000461 return -1;
462 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000463 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
464 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
465 return -1;
466 }
467#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000468
469 int i;
470 int rd_bytes = 0;
471 for (i = 0; i < timeout; i++) {
472 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000473#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000474 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
475 msg_pspew("read %lu bytes\n", rv);
476#else
477 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
478 msg_pspew("read %zd bytes\n", rv);
479#endif
480 if ((rv == -1) && (errno != EAGAIN)) {
481 msg_perr_strerror("Serial port read error: ");
482 ret = -1;
483 break;
484 }
485 if (rv > 0)
486 rd_bytes += rv;
487 if (rd_bytes == readcnt) {
488 ret = 0;
489 break;
490 }
491 internal_delay(1000); /* 1ms units */
492 }
493 if (really_read != NULL)
494 *really_read = rd_bytes;
495
496 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000497#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000498 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000499 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000500 ret = -1;
501 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000502#else
503 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
504 msg_perr_strerror("Could not restore serial port mode to blocking: ");
505 ret = -1;
506 }
507#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000508 return ret;
509}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000510
511/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
512 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
513 * If really_wrote is not NULL, this function sets its contents to the number of bytes written successfully. */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000514int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000515{
516 int ret = 1;
517 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000518#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000519 DWORD rv;
520 COMMTIMEOUTS oldTimeout;
521 COMMTIMEOUTS newTimeout = {
522 .ReadIntervalTimeout = MAXDWORD,
523 .ReadTotalTimeoutMultiplier = 0,
524 .ReadTotalTimeoutConstant = 0,
525 .WriteTotalTimeoutMultiplier = 0,
526 .WriteTotalTimeoutConstant = 0
527 };
528 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
529 msg_perr_strerror("Could not get serial port timeout settings: ");
530 return -1;
531 }
532 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
533 msg_perr_strerror("Could not set serial port timeout settings: ");
534 return -1;
535 }
536#else
537 ssize_t rv;
538 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000539 if (flags == -1) {
540 msg_perr_strerror("Could not get serial port mode: ");
541 return -1;
542 }
543 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
544 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
545 return -1;
546 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000547#endif
548
549 int i;
550 int wr_bytes = 0;
551 for (i = 0; i < timeout; i++) {
552 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000553#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000554 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
555 msg_pspew("wrote %lu bytes\n", rv);
556#else
557 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
558 msg_pspew("wrote %zd bytes\n", rv);
559#endif
560 if ((rv == -1) && (errno != EAGAIN)) {
561 msg_perr_strerror("Serial port write error: ");
562 ret = -1;
563 break;
564 }
565 if (rv > 0) {
566 wr_bytes += rv;
567 if (wr_bytes == writecnt) {
568 msg_pspew("write successful\n");
569 ret = 0;
570 break;
571 }
572 }
573 internal_delay(1000); /* 1ms units */
574 }
575 if (really_wrote != NULL)
576 *really_wrote = wr_bytes;
577
578 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000579#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000580 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
581 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000582 return -1;
583 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000584#else
585 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
586 msg_perr_strerror("Could not restore serial port blocking behavior: ");
587 return -1;
588 }
589#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000590 return ret;
591}