blob: a64a51d3ebd81c5c40febf9f0156eabdf7644b28 [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
Urja Rannikkodc445842016-01-04 05:08:40 +000046/* There is no way defined by POSIX to use arbitrary baud rates. It only defines some macros that can be used to
47 * specify respective baud rates and many implementations extend this list with further macros, cf. TERMIOS(3)
48 * and http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h
49 * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal
50 * with numerical user input.
51 *
52 * On Linux there is a non-standard way to use arbitrary baud rates that flashrom does not support (yet), cf.
53 * http://www.downtowndougbrown.com/2013/11/linux-custom-serial-baud-rates/
54 *
55 * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for
56 * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see
57 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
58 */
59#if !IS_WINDOWS
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000060struct baudentry {
61 int flag;
62 unsigned int baud;
63};
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000064#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000065
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000066static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000067 BAUDENTRY(9600) /* unconditional default */
Urja Rannikkodc445842016-01-04 05:08:40 +000068#ifdef B19200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000069 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000070#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000071#ifdef B38400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000072 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000073#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000074#ifdef B57600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000075 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000076#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000077#ifdef B115200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000078 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000079#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000080#ifdef B230400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000081 BAUDENTRY(230400)
82#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000083#ifdef B460800
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000084 BAUDENTRY(460800)
85#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000086#ifdef B500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000087 BAUDENTRY(500000)
88#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000089#ifdef B576000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000090 BAUDENTRY(576000)
91#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000092#ifdef B921600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000093 BAUDENTRY(921600)
94#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000095#ifdef B1000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000096 BAUDENTRY(1000000)
97#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000098#ifdef B1152000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000099 BAUDENTRY(1152000)
100#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000101#ifdef B1500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000102 BAUDENTRY(1500000)
103#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000104#ifdef B2000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000105 BAUDENTRY(2000000)
106#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000107#ifdef B2500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000108 BAUDENTRY(2500000)
109#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000110#ifdef B3000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000111 BAUDENTRY(3000000)
112#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000113#ifdef B3500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000114 BAUDENTRY(3500000)
115#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000116#ifdef B4000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000117 BAUDENTRY(4000000)
118#endif
119 {0, 0} /* Terminator */
120};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000121
Stefan Tauner72587f82016-01-04 03:05:15 +0000122static const struct baudentry *round_baud(unsigned int baud)
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000123{
124 int i;
125 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
126 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
127 if (sp_baudtable[i].baud == baud)
128 return &sp_baudtable[i];
129
130 if (sp_baudtable[i].baud < baud) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000131 msg_pwarn("Warning: given baudrate %d rounded down to %d.\n",
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000132 baud, sp_baudtable[i].baud);
133 return &sp_baudtable[i];
134 }
135 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000136 msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud);
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000137 return &sp_baudtable[0];
138}
Urja Rannikkodc445842016-01-04 05:08:40 +0000139#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000140
Stefan Taunerbf88be92013-04-01 00:45:08 +0000141/* Uses msg_perr to print the last system error.
142 * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
143 * strerror() or FormatMessage() and ending with a linebreak. */
144static void msg_perr_strerror(const char *msg)
145{
146 msg_perr("Error: %s", msg);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000147#if IS_WINDOWS
Stefan Taunerbf88be92013-04-01 00:45:08 +0000148 char *lpMsgBuf;
149 DWORD nErr = GetLastError();
150 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
151 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
152 msg_perr(lpMsgBuf);
153 /* At least some formatted messages contain a line break at the end. Make sure to always print one */
154 if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
155 msg_perr("\n");
156 LocalFree(lpMsgBuf);
157#else
158 msg_perr("%s\n", strerror(errno));
159#endif
160}
161
Stefan Tauner72587f82016-01-04 03:05:15 +0000162int serialport_config(fdtype fd, int baud)
Stefan Tauner184c52c2013-08-23 21:51:32 +0000163{
164 if (fd == SER_INV_FD) {
165 msg_perr("%s: File descriptor is invalid.\n", __func__);
166 return 1;
167 }
168
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000169#if IS_WINDOWS
Stefan Tauner184c52c2013-08-23 21:51:32 +0000170 DCB dcb;
171 if (!GetCommState(fd, &dcb)) {
172 msg_perr_strerror("Could not fetch original serial port configuration: ");
173 return 1;
174 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000175 if (baud >= 0) {
Urja Rannikkodc445842016-01-04 05:08:40 +0000176 dcb.BaudRate = baud;
Stefan Tauner72587f82016-01-04 03:05:15 +0000177 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000178 dcb.ByteSize = 8;
179 dcb.Parity = NOPARITY;
180 dcb.StopBits = ONESTOPBIT;
181 if (!SetCommState(fd, &dcb)) {
182 msg_perr_strerror("Could not change serial port configuration: ");
183 return 1;
184 }
185 if (!GetCommState(fd, &dcb)) {
186 msg_perr_strerror("Could not fetch new serial port configuration: ");
187 return 1;
188 }
189 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
190#else
191 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000192 if (tcgetattr(fd, &observed) != 0) {
193 msg_perr_strerror("Could not fetch original serial port configuration: ");
194 return 1;
195 }
196 wanted = observed;
Stefan Tauner72587f82016-01-04 03:05:15 +0000197 if (baud >= 0) {
198 const struct baudentry *entry = round_baud(baud);
199 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
200 msg_perr_strerror("Could not set serial baud rate: ");
201 return 1;
202 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000203 }
204 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
205 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
206 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
207 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
208 wanted.c_oflag &= ~OPOST;
209 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
210 msg_perr_strerror("Could not change serial port configuration: ");
211 return 1;
212 }
213 if (tcgetattr(fd, &observed) != 0) {
214 msg_perr_strerror("Could not fetch new serial port configuration: ");
215 return 1;
216 }
217 if (observed.c_cflag != wanted.c_cflag ||
218 observed.c_lflag != wanted.c_lflag ||
219 observed.c_iflag != wanted.c_iflag ||
Stefan Tauner631bb022016-01-04 03:05:06 +0000220 observed.c_oflag != wanted.c_oflag) {
221 msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
222 msg_pdbg(" observed wanted\n"
223 "c_cflag: 0x%08lX 0x%08lX\n"
224 "c_lflag: 0x%08lX 0x%08lX\n"
225 "c_iflag: 0x%08lX 0x%08lX\n"
226 "c_oflag: 0x%08lX 0x%08lX\n",
227 (long)observed.c_cflag, (long)wanted.c_cflag,
228 (long)observed.c_lflag, (long)wanted.c_lflag,
229 (long)observed.c_iflag, (long)wanted.c_iflag,
230 (long)observed.c_oflag, (long)wanted.c_oflag
231 );
Stefan Tauner184c52c2013-08-23 21:51:32 +0000232 }
Stefan Tauner631bb022016-01-04 03:05:06 +0000233 if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
234 cfgetospeed(&observed) != cfgetospeed(&wanted)) {
235 msg_pwarn("Could not set baud rates exactly.\n");
236 msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
237 (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
238 }
Urja Rannikkodc445842016-01-04 05:08:40 +0000239 // FIXME: display actual baud rate - at least if none was specified by the user.
Stefan Tauner184c52c2013-08-23 21:51:32 +0000240#endif
241 return 0;
242}
243
Stefan Tauner72587f82016-01-04 03:05:15 +0000244fdtype sp_openserport(char *dev, int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000245{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000246 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000247#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000248 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000249 if ((strlen(dev) > 3) &&
250 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000251 (tolower((unsigned char)dev[1]) == 'o') &&
252 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000253 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000254 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000255 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000256 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000257 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000258 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000259 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000260 }
Uwe Hermann43959702010-03-13 17:28:29 +0000261 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
262 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000263 if (dev2 != dev)
264 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000265 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000266 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000267 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000268 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000269 if (serialport_config(fd, baud) != 0) {
270 CloseHandle(fd);
271 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000272 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000273 return fd;
274#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000275 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000276 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000277 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000278 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000279 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000280
281 /* Ensure that we use blocking I/O */
282 const int flags = fcntl(fd, F_GETFL);
283 if (flags == -1) {
284 msg_perr_strerror("Could not get serial port mode: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000285 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000286 }
287 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
288 msg_perr_strerror("Could not set serial port mode to blocking: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000289 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000290 }
291
Stefan Tauner184c52c2013-08-23 21:51:32 +0000292 if (serialport_config(fd, baud) != 0) {
Stefan Taunera3712812016-01-16 18:50:27 +0000293 goto err;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000294 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000295 return fd;
Stefan Taunera3712812016-01-16 18:50:27 +0000296err:
297 close(fd);
298 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000299#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000300}
301
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000302void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000303#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000304 DWORD ctl;
305
306 if(pin == PIN_TXD) {
307 ctl = val ? SETBREAK: CLRBREAK;
308 }
309 else if(pin == PIN_DTR) {
310 ctl = val ? SETDTR: CLRDTR;
311 }
312 else {
313 ctl = val ? SETRTS: CLRRTS;
314 }
315 EscapeCommFunction(sp_fd, ctl);
316#else
317 int ctl, s;
318
319 if(pin == PIN_TXD) {
320 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
321 }
322 else {
323 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
324 ioctl(sp_fd, TIOCMGET, &ctl);
325
326 if (val) {
327 ctl |= s;
328 }
329 else {
330 ctl &= ~s;
331 }
332 ioctl(sp_fd, TIOCMSET, &ctl);
333 }
334#endif
335}
336
337int sp_get_pin(enum SP_PIN pin) {
338 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000339#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000340 DWORD ctl;
341
342 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
343 GetCommModemStatus(sp_fd, &ctl);
344#else
345 int ctl;
346 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
347 ioctl(sp_fd, TIOCMGET, &ctl);
348#endif
349
350 return ((ctl & s) ? 1 : 0);
351
352}
353
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000354void sp_flush_incoming(void)
355{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000356#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000357 PurgeComm(sp_fd, PURGE_RXCLEAR);
358#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000359 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000360 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000361#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000362 return;
363}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000364
David Hendricks8bb20212011-06-14 01:35:36 +0000365int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000366{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000367#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000368 CloseHandle(sp_fd);
369#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000370 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000371#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000372 return 0;
373}
374
Mark Marshallf20b7be2014-05-09 21:16:21 +0000375int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000376{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000377#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000378 DWORD tmp = 0;
379#else
380 ssize_t tmp = 0;
381#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000382 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000383
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000384 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000385#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000386 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
387#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000388 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000389#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000390 if (tmp == -1) {
391 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000392 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000393 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000394 if (!tmp) {
395 msg_pdbg2("Empty write\n");
396 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000397 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000398 if (empty_writes == 0) {
399 msg_perr("Serial port is unresponsive!\n");
400 return 1;
401 }
402 }
403 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000404 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000405 }
406
407 return 0;
408}
409
410int serialport_read(unsigned char *buf, unsigned int readcnt)
411{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000412#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000413 DWORD tmp = 0;
414#else
415 ssize_t tmp = 0;
416#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000417
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000418 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000419#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000420 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
421#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000422 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000423#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000424 if (tmp == -1) {
425 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000426 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000427 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000428 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000429 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000430 readcnt -= tmp;
431 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000432 }
433
434 return 0;
435}
Stefan Tauner00e16082013-04-01 00:45:38 +0000436
437/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
438 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
439 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
440int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
441{
442 int ret = 1;
443 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000444#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000445 DWORD rv;
446 COMMTIMEOUTS oldTimeout;
447 COMMTIMEOUTS newTimeout = {
448 .ReadIntervalTimeout = MAXDWORD,
449 .ReadTotalTimeoutMultiplier = 0,
450 .ReadTotalTimeoutConstant = 0,
451 .WriteTotalTimeoutMultiplier = 0,
452 .WriteTotalTimeoutConstant = 0
453 };
454 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
455 msg_perr_strerror("Could not get serial port timeout settings: ");
456 return -1;
457 }
458 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000459 msg_perr_strerror("Could not set serial port timeout settings: ");
460 return -1;
461 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000462#else
463 ssize_t rv;
464 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000465 if (flags == -1) {
466 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000467 return -1;
468 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000469 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
470 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
471 return -1;
472 }
473#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000474
475 int i;
476 int rd_bytes = 0;
477 for (i = 0; i < timeout; i++) {
478 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000479#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000480 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
481 msg_pspew("read %lu bytes\n", rv);
482#else
483 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
484 msg_pspew("read %zd bytes\n", rv);
485#endif
486 if ((rv == -1) && (errno != EAGAIN)) {
487 msg_perr_strerror("Serial port read error: ");
488 ret = -1;
489 break;
490 }
491 if (rv > 0)
492 rd_bytes += rv;
493 if (rd_bytes == readcnt) {
494 ret = 0;
495 break;
496 }
497 internal_delay(1000); /* 1ms units */
498 }
499 if (really_read != NULL)
500 *really_read = rd_bytes;
501
502 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000503#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000504 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000505 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000506 ret = -1;
507 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000508#else
509 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
510 msg_perr_strerror("Could not restore serial port mode to blocking: ");
511 ret = -1;
512 }
513#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000514 return ret;
515}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000516
517/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
518 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
519 * 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 +0000520int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000521{
522 int ret = 1;
523 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000524#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000525 DWORD rv;
526 COMMTIMEOUTS oldTimeout;
527 COMMTIMEOUTS newTimeout = {
528 .ReadIntervalTimeout = MAXDWORD,
529 .ReadTotalTimeoutMultiplier = 0,
530 .ReadTotalTimeoutConstant = 0,
531 .WriteTotalTimeoutMultiplier = 0,
532 .WriteTotalTimeoutConstant = 0
533 };
534 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
535 msg_perr_strerror("Could not get serial port timeout settings: ");
536 return -1;
537 }
538 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
539 msg_perr_strerror("Could not set serial port timeout settings: ");
540 return -1;
541 }
542#else
543 ssize_t rv;
544 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000545 if (flags == -1) {
546 msg_perr_strerror("Could not get serial port mode: ");
547 return -1;
548 }
549 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
550 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
551 return -1;
552 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000553#endif
554
555 int i;
556 int wr_bytes = 0;
557 for (i = 0; i < timeout; i++) {
558 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000559#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000560 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
561 msg_pspew("wrote %lu bytes\n", rv);
562#else
563 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
564 msg_pspew("wrote %zd bytes\n", rv);
565#endif
566 if ((rv == -1) && (errno != EAGAIN)) {
567 msg_perr_strerror("Serial port write error: ");
568 ret = -1;
569 break;
570 }
571 if (rv > 0) {
572 wr_bytes += rv;
573 if (wr_bytes == writecnt) {
574 msg_pspew("write successful\n");
575 ret = 0;
576 break;
577 }
578 }
579 internal_delay(1000); /* 1ms units */
580 }
581 if (really_wrote != NULL)
582 *really_wrote = wr_bytes;
583
584 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000585#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000586 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
587 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000588 return -1;
589 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000590#else
591 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
592 msg_perr_strerror("Could not restore serial port blocking behavior: ");
593 return -1;
594 }
595#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000596 return ret;
597}