blob: 257578ad347a2d3af961e1f725ccf6369e08bd74 [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: ");
285 return SER_INV_FD;
286 }
287 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
288 msg_perr_strerror("Could not set serial port mode to blocking: ");
289 return SER_INV_FD;
290 }
291
Stefan Tauner184c52c2013-08-23 21:51:32 +0000292 if (serialport_config(fd, baud) != 0) {
293 close(fd);
294 return SER_INV_FD;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000295 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000296 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000297#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000298}
299
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000300void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000301#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000302 DWORD ctl;
303
304 if(pin == PIN_TXD) {
305 ctl = val ? SETBREAK: CLRBREAK;
306 }
307 else if(pin == PIN_DTR) {
308 ctl = val ? SETDTR: CLRDTR;
309 }
310 else {
311 ctl = val ? SETRTS: CLRRTS;
312 }
313 EscapeCommFunction(sp_fd, ctl);
314#else
315 int ctl, s;
316
317 if(pin == PIN_TXD) {
318 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
319 }
320 else {
321 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
322 ioctl(sp_fd, TIOCMGET, &ctl);
323
324 if (val) {
325 ctl |= s;
326 }
327 else {
328 ctl &= ~s;
329 }
330 ioctl(sp_fd, TIOCMSET, &ctl);
331 }
332#endif
333}
334
335int sp_get_pin(enum SP_PIN pin) {
336 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000337#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000338 DWORD ctl;
339
340 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
341 GetCommModemStatus(sp_fd, &ctl);
342#else
343 int ctl;
344 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
345 ioctl(sp_fd, TIOCMGET, &ctl);
346#endif
347
348 return ((ctl & s) ? 1 : 0);
349
350}
351
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000352void sp_flush_incoming(void)
353{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000354#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000355 PurgeComm(sp_fd, PURGE_RXCLEAR);
356#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000357 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000358 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000359#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000360 return;
361}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000362
David Hendricks8bb20212011-06-14 01:35:36 +0000363int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000364{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000365#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000366 CloseHandle(sp_fd);
367#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000368 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000369#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000370 return 0;
371}
372
Mark Marshallf20b7be2014-05-09 21:16:21 +0000373int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000374{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000375#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000376 DWORD tmp = 0;
377#else
378 ssize_t tmp = 0;
379#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000380 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000381
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000382 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000383#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000384 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
385#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000386 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000387#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000388 if (tmp == -1) {
389 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000390 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000391 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000392 if (!tmp) {
393 msg_pdbg2("Empty write\n");
394 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000395 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000396 if (empty_writes == 0) {
397 msg_perr("Serial port is unresponsive!\n");
398 return 1;
399 }
400 }
401 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000402 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000403 }
404
405 return 0;
406}
407
408int serialport_read(unsigned char *buf, unsigned int readcnt)
409{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000410#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000411 DWORD tmp = 0;
412#else
413 ssize_t tmp = 0;
414#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000415
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000416 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000417#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000418 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
419#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000420 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000421#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000422 if (tmp == -1) {
423 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000424 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000425 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000426 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000427 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000428 readcnt -= tmp;
429 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000430 }
431
432 return 0;
433}
Stefan Tauner00e16082013-04-01 00:45:38 +0000434
435/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
436 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
437 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
438int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
439{
440 int ret = 1;
441 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000442#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000443 DWORD rv;
444 COMMTIMEOUTS oldTimeout;
445 COMMTIMEOUTS newTimeout = {
446 .ReadIntervalTimeout = MAXDWORD,
447 .ReadTotalTimeoutMultiplier = 0,
448 .ReadTotalTimeoutConstant = 0,
449 .WriteTotalTimeoutMultiplier = 0,
450 .WriteTotalTimeoutConstant = 0
451 };
452 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
453 msg_perr_strerror("Could not get serial port timeout settings: ");
454 return -1;
455 }
456 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000457 msg_perr_strerror("Could not set serial port timeout settings: ");
458 return -1;
459 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000460#else
461 ssize_t rv;
462 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000463 if (flags == -1) {
464 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000465 return -1;
466 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000467 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
468 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
469 return -1;
470 }
471#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000472
473 int i;
474 int rd_bytes = 0;
475 for (i = 0; i < timeout; i++) {
476 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000477#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000478 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
479 msg_pspew("read %lu bytes\n", rv);
480#else
481 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
482 msg_pspew("read %zd bytes\n", rv);
483#endif
484 if ((rv == -1) && (errno != EAGAIN)) {
485 msg_perr_strerror("Serial port read error: ");
486 ret = -1;
487 break;
488 }
489 if (rv > 0)
490 rd_bytes += rv;
491 if (rd_bytes == readcnt) {
492 ret = 0;
493 break;
494 }
495 internal_delay(1000); /* 1ms units */
496 }
497 if (really_read != NULL)
498 *really_read = rd_bytes;
499
500 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000501#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000502 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000503 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000504 ret = -1;
505 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000506#else
507 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
508 msg_perr_strerror("Could not restore serial port mode to blocking: ");
509 ret = -1;
510 }
511#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000512 return ret;
513}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000514
515/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
516 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
517 * 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 +0000518int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000519{
520 int ret = 1;
521 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000522#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000523 DWORD rv;
524 COMMTIMEOUTS oldTimeout;
525 COMMTIMEOUTS newTimeout = {
526 .ReadIntervalTimeout = MAXDWORD,
527 .ReadTotalTimeoutMultiplier = 0,
528 .ReadTotalTimeoutConstant = 0,
529 .WriteTotalTimeoutMultiplier = 0,
530 .WriteTotalTimeoutConstant = 0
531 };
532 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
533 msg_perr_strerror("Could not get serial port timeout settings: ");
534 return -1;
535 }
536 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
537 msg_perr_strerror("Could not set serial port timeout settings: ");
538 return -1;
539 }
540#else
541 ssize_t rv;
542 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000543 if (flags == -1) {
544 msg_perr_strerror("Could not get serial port mode: ");
545 return -1;
546 }
547 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
548 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
549 return -1;
550 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000551#endif
552
553 int i;
554 int wr_bytes = 0;
555 for (i = 0; i < timeout; i++) {
556 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000557#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000558 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
559 msg_pspew("wrote %lu bytes\n", rv);
560#else
561 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
562 msg_pspew("wrote %zd bytes\n", rv);
563#endif
564 if ((rv == -1) && (errno != EAGAIN)) {
565 msg_perr_strerror("Serial port write error: ");
566 ret = -1;
567 break;
568 }
569 if (rv > 0) {
570 wr_bytes += rv;
571 if (wr_bytes == writecnt) {
572 msg_pspew("write successful\n");
573 ret = 0;
574 break;
575 }
576 }
577 internal_delay(1000); /* 1ms units */
578 }
579 if (really_wrote != NULL)
580 *really_wrote = wr_bytes;
581
582 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000583#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000584 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
585 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000586 return -1;
587 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000588#else
589 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
590 msg_perr_strerror("Could not restore serial port blocking behavior: ");
591 return -1;
592 }
593#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000594 return ret;
595}