blob: 8e27aa0e40ac4e0a61455de723cf5d2f2b3a103a [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
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000025#include <string.h>
26#include <ctype.h>
27#include <fcntl.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000028#include <sys/stat.h>
29#include <errno.h>
30#include <inttypes.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000031#ifdef _WIN32
32#include <conio.h>
33#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000034#include <termios.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000035#include <unistd.h>
36#include <sys/types.h>
37#include <sys/ioctl.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000038#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "flash.h"
40#include "programmer.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000041
Patrick Georgie48654c2010-01-06 22:14:39 +000042fdtype sp_fd;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000043
44void __attribute__((noreturn)) sp_die(char *msg)
45{
46 perror(msg);
47 exit(1);
48}
49
Stefan Taunerda5b17c2013-04-01 00:45:32 +000050#ifdef _WIN32
51struct baudentry {
52 DWORD flag;
53 unsigned int baud;
54};
55#define BAUDENTRY(baud) { CBR_##baud, baud },
56#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000057struct baudentry {
58 int flag;
59 unsigned int baud;
60};
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000061#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000062#endif
63
64/* I'd like if the C preprocessor could have directives in macros.
65 * See TERMIOS(3) and http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx and
66 * 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 +000067static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000068 BAUDENTRY(9600) /* unconditional default */
69#if defined(B19200) || defined(CBR_19200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000070 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000071#endif
72#if defined(B38400) || defined(CBR_38400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000073 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000074#endif
75#if defined(B57600) || defined(CBR_57600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000076 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000077#endif
78#if defined(B115200) || defined(CBR_115200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000079 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000080#endif
81#if defined(B230400) || defined(CBR_230400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000082 BAUDENTRY(230400)
83#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000084#if defined(B460800) || defined(CBR_460800)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000085 BAUDENTRY(460800)
86#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000087#if defined(B500000) || defined(CBR_500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000088 BAUDENTRY(500000)
89#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000090#if defined(B576000) || defined(CBR_576000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000091 BAUDENTRY(576000)
92#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000093#if defined(B921600) || defined(CBR_921600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000094 BAUDENTRY(921600)
95#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000096#if defined(B1000000) || defined(CBR_1000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000097 BAUDENTRY(1000000)
98#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000099#if defined(B1152000) || defined(CBR_1152000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000100 BAUDENTRY(1152000)
101#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000102#if defined(B1500000) || defined(CBR_1500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000103 BAUDENTRY(1500000)
104#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000105#if defined(B2000000) || defined(CBR_2000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000106 BAUDENTRY(2000000)
107#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000108#if defined(B2500000) || defined(CBR_2500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000109 BAUDENTRY(2500000)
110#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000111#if defined(B3000000) || defined(CBR_3000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000112 BAUDENTRY(3000000)
113#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000114#if defined(B3500000) || defined(CBR_3500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000115 BAUDENTRY(3500000)
116#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000117#if defined(B4000000) || defined(CBR_4000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000118 BAUDENTRY(4000000)
119#endif
120 {0, 0} /* Terminator */
121};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000122
123const struct baudentry *round_baud(unsigned int baud)
124{
125 int i;
126 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
127 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
128 if (sp_baudtable[i].baud == baud)
129 return &sp_baudtable[i];
130
131 if (sp_baudtable[i].baud < baud) {
132 msg_pinfo("Warning: given baudrate %d rounded down to %d.\n",
133 baud, sp_baudtable[i].baud);
134 return &sp_baudtable[i];
135 }
136 }
137 return &sp_baudtable[0];
138}
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000139
Stefan Taunerbf88be92013-04-01 00:45:08 +0000140/* Uses msg_perr to print the last system error.
141 * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
142 * strerror() or FormatMessage() and ending with a linebreak. */
143static void msg_perr_strerror(const char *msg)
144{
145 msg_perr("Error: %s", msg);
146#ifdef _WIN32
147 char *lpMsgBuf;
148 DWORD nErr = GetLastError();
149 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
150 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
151 msg_perr(lpMsgBuf);
152 /* At least some formatted messages contain a line break at the end. Make sure to always print one */
153 if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
154 msg_perr("\n");
155 LocalFree(lpMsgBuf);
156#else
157 msg_perr("%s\n", strerror(errno));
158#endif
159}
160
Patrick Georgie48654c2010-01-06 22:14:39 +0000161fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000162{
Patrick Georgie48654c2010-01-06 22:14:39 +0000163#ifdef _WIN32
164 HANDLE fd;
Uwe Hermann43959702010-03-13 17:28:29 +0000165 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000166 if ((strlen(dev) > 3) &&
167 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000168 (tolower((unsigned char)dev[1]) == 'o') &&
169 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000170 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000171 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000172 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000173 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000174 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000175 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000176 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000177 }
Uwe Hermann43959702010-03-13 17:28:29 +0000178 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
179 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000180 if (dev2 != dev)
181 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000182 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000183 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000184 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000185 }
186 DCB dcb;
187 if (!GetCommState(fd, &dcb)) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000188 msg_perr_strerror("Could not fetch serial port configuration: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000189 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000190 }
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000191 const struct baudentry *entry = round_baud(baud);
192 dcb.BaudRate = entry->baud;
Uwe Hermann43959702010-03-13 17:28:29 +0000193 dcb.ByteSize = 8;
194 dcb.Parity = NOPARITY;
195 dcb.StopBits = ONESTOPBIT;
Patrick Georgie48654c2010-01-06 22:14:39 +0000196 if (!SetCommState(fd, &dcb)) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000197 msg_perr_strerror("Could not change serial port configuration: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000198 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000199 }
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000200 if (!GetCommState(fd, &dcb)) {
201 msg_perr_strerror("Could not fetch serial port configuration: ");
202 return SER_INV_FD;
203 }
204 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
Patrick Georgie48654c2010-01-06 22:14:39 +0000205 return fd;
206#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000207 struct termios options;
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000208 int fd;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000209 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000210 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000211 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000212 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000213 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000214 fcntl(fd, F_SETFL, 0);
215 tcgetattr(fd, &options);
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000216 const struct baudentry *entry = round_baud(baud);
217 cfsetispeed(&options, entry->flag);
218 cfsetospeed(&options, entry->flag);
219 msg_pdbg("Setting baud rate to %d.\n", entry->baud);
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000220 options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
221 options.c_cflag |= (CS8 | CLOCAL | CREAD);
222 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
223 options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
224 options.c_oflag &= ~OPOST;
225 tcsetattr(fd, TCSANOW, &options);
226 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000227#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000228}
229
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000230void sp_set_pin(enum SP_PIN pin, int val) {
231#ifdef _WIN32
232 DWORD ctl;
233
234 if(pin == PIN_TXD) {
235 ctl = val ? SETBREAK: CLRBREAK;
236 }
237 else if(pin == PIN_DTR) {
238 ctl = val ? SETDTR: CLRDTR;
239 }
240 else {
241 ctl = val ? SETRTS: CLRRTS;
242 }
243 EscapeCommFunction(sp_fd, ctl);
244#else
245 int ctl, s;
246
247 if(pin == PIN_TXD) {
248 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
249 }
250 else {
251 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
252 ioctl(sp_fd, TIOCMGET, &ctl);
253
254 if (val) {
255 ctl |= s;
256 }
257 else {
258 ctl &= ~s;
259 }
260 ioctl(sp_fd, TIOCMSET, &ctl);
261 }
262#endif
263}
264
265int sp_get_pin(enum SP_PIN pin) {
266 int s;
267#ifdef _WIN32
268 DWORD ctl;
269
270 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
271 GetCommModemStatus(sp_fd, &ctl);
272#else
273 int ctl;
274 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
275 ioctl(sp_fd, TIOCMGET, &ctl);
276#endif
277
278 return ((ctl & s) ? 1 : 0);
279
280}
281
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000282void sp_flush_incoming(void)
283{
Patrick Georgie48654c2010-01-06 22:14:39 +0000284#ifdef _WIN32
285 PurgeComm(sp_fd, PURGE_RXCLEAR);
286#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000287 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000288 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000289#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000290 return;
291}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000292
David Hendricks8bb20212011-06-14 01:35:36 +0000293int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000294{
Patrick Georgie48654c2010-01-06 22:14:39 +0000295#ifdef _WIN32
296 CloseHandle(sp_fd);
297#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000298 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000299#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000300 return 0;
301}
302
303int serialport_write(unsigned char *buf, unsigned int writecnt)
304{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000305#ifdef _WIN32
306 DWORD tmp = 0;
307#else
308 ssize_t tmp = 0;
309#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000310 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000311
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000312 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000313#ifdef _WIN32
314 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
315#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000316 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000317#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000318 if (tmp == -1) {
319 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000320 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000321 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000322 if (!tmp) {
323 msg_pdbg2("Empty write\n");
324 empty_writes--;
325 programmer_delay(500);
326 if (empty_writes == 0) {
327 msg_perr("Serial port is unresponsive!\n");
328 return 1;
329 }
330 }
331 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000332 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000333 }
334
335 return 0;
336}
337
338int serialport_read(unsigned char *buf, unsigned int readcnt)
339{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000340#ifdef _WIN32
341 DWORD tmp = 0;
342#else
343 ssize_t tmp = 0;
344#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000345
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000346 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000347#ifdef _WIN32
348 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
349#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000350 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000351#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000352 if (tmp == -1) {
353 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000354 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000355 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000356 if (!tmp)
Sean Nelsonebb4f5f2010-01-09 23:46:39 +0000357 msg_pdbg("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000358 readcnt -= tmp;
359 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000360 }
361
362 return 0;
363}
Stefan Tauner00e16082013-04-01 00:45:38 +0000364
365/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
366 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
367 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
368int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
369{
370 int ret = 1;
371 /* disable blocked i/o and declare platform-specific variables */
372#ifdef _WIN32
373 DWORD rv;
374 COMMTIMEOUTS oldTimeout;
375 COMMTIMEOUTS newTimeout = {
376 .ReadIntervalTimeout = MAXDWORD,
377 .ReadTotalTimeoutMultiplier = 0,
378 .ReadTotalTimeoutConstant = 0,
379 .WriteTotalTimeoutMultiplier = 0,
380 .WriteTotalTimeoutConstant = 0
381 };
382 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
383 msg_perr_strerror("Could not get serial port timeout settings: ");
384 return -1;
385 }
386 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
387#else
388 ssize_t rv;
389 const int flags = fcntl(sp_fd, F_GETFL);
390 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
391#endif
392 msg_perr_strerror("Could not set serial port timeout settings %s");
393 return -1;
394 }
395
396 int i;
397 int rd_bytes = 0;
398 for (i = 0; i < timeout; i++) {
399 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
400#ifdef _WIN32
401 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
402 msg_pspew("read %lu bytes\n", rv);
403#else
404 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
405 msg_pspew("read %zd bytes\n", rv);
406#endif
407 if ((rv == -1) && (errno != EAGAIN)) {
408 msg_perr_strerror("Serial port read error: ");
409 ret = -1;
410 break;
411 }
412 if (rv > 0)
413 rd_bytes += rv;
414 if (rd_bytes == readcnt) {
415 ret = 0;
416 break;
417 }
418 internal_delay(1000); /* 1ms units */
419 }
420 if (really_read != NULL)
421 *really_read = rd_bytes;
422
423 /* restore original blocking behavior */
424#ifdef _WIN32
425 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
426#else
427 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
428#endif
429 msg_perr_strerror("Could not restore serial port timeout settings: %s\n");
430 ret = -1;
431 }
432 return ret;
433}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000434
435/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
436 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
437 * If really_wrote is not NULL, this function sets its contents to the number of bytes written successfully. */
438int serialport_write_nonblock(unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
439{
440 int ret = 1;
441 /* disable blocked i/o and declare platform-specific variables */
442#ifdef _WIN32
443 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)) {
457 msg_perr_strerror("Could not set serial port timeout settings: ");
458 return -1;
459 }
460#else
461 ssize_t rv;
462 const int flags = fcntl(sp_fd, F_GETFL);
463 fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK);
464#endif
465
466 int i;
467 int wr_bytes = 0;
468 for (i = 0; i < timeout; i++) {
469 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
470#ifdef _WIN32
471 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
472 msg_pspew("wrote %lu bytes\n", rv);
473#else
474 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
475 msg_pspew("wrote %zd bytes\n", rv);
476#endif
477 if ((rv == -1) && (errno != EAGAIN)) {
478 msg_perr_strerror("Serial port write error: ");
479 ret = -1;
480 break;
481 }
482 if (rv > 0) {
483 wr_bytes += rv;
484 if (wr_bytes == writecnt) {
485 msg_pspew("write successful\n");
486 ret = 0;
487 break;
488 }
489 }
490 internal_delay(1000); /* 1ms units */
491 }
492 if (really_wrote != NULL)
493 *really_wrote = wr_bytes;
494
495 /* restore original blocking behavior */
496#ifdef _WIN32
497 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
498 msg_perr_strerror("Could not restore serial port timeout settings: ");
499#else
500 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
501#endif
502 return -1;
503 }
504 return ret;
505}