blob: f7d8e5ed248e12fdfa217c1165ed2422c04f39df [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
Stefan Taunere33c40e2013-04-13 00:29:30 +000042fdtype sp_fd = SER_INV_FD;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000043
Stefan Taunerda5b17c2013-04-01 00:45:32 +000044#ifdef _WIN32
45struct baudentry {
46 DWORD flag;
47 unsigned int baud;
48};
49#define BAUDENTRY(baud) { CBR_##baud, baud },
50#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000051struct baudentry {
52 int flag;
53 unsigned int baud;
54};
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000055#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000056#endif
57
58/* I'd like if the C preprocessor could have directives in macros.
59 * See TERMIOS(3) and http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx and
60 * 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 +000061static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000062 BAUDENTRY(9600) /* unconditional default */
63#if defined(B19200) || defined(CBR_19200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000064 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000065#endif
66#if defined(B38400) || defined(CBR_38400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000067 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000068#endif
69#if defined(B57600) || defined(CBR_57600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000070 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000071#endif
72#if defined(B115200) || defined(CBR_115200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000073 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000074#endif
75#if defined(B230400) || defined(CBR_230400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000076 BAUDENTRY(230400)
77#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000078#if defined(B460800) || defined(CBR_460800)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000079 BAUDENTRY(460800)
80#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000081#if defined(B500000) || defined(CBR_500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000082 BAUDENTRY(500000)
83#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000084#if defined(B576000) || defined(CBR_576000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000085 BAUDENTRY(576000)
86#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000087#if defined(B921600) || defined(CBR_921600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000088 BAUDENTRY(921600)
89#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000090#if defined(B1000000) || defined(CBR_1000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000091 BAUDENTRY(1000000)
92#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000093#if defined(B1152000) || defined(CBR_1152000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000094 BAUDENTRY(1152000)
95#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000096#if defined(B1500000) || defined(CBR_1500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000097 BAUDENTRY(1500000)
98#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000099#if defined(B2000000) || defined(CBR_2000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000100 BAUDENTRY(2000000)
101#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000102#if defined(B2500000) || defined(CBR_2500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000103 BAUDENTRY(2500000)
104#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000105#if defined(B3000000) || defined(CBR_3000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000106 BAUDENTRY(3000000)
107#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000108#if defined(B3500000) || defined(CBR_3500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000109 BAUDENTRY(3500000)
110#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000111#if defined(B4000000) || defined(CBR_4000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000112 BAUDENTRY(4000000)
113#endif
114 {0, 0} /* Terminator */
115};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000116
117const struct baudentry *round_baud(unsigned int baud)
118{
119 int i;
120 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
121 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
122 if (sp_baudtable[i].baud == baud)
123 return &sp_baudtable[i];
124
125 if (sp_baudtable[i].baud < baud) {
126 msg_pinfo("Warning: given baudrate %d rounded down to %d.\n",
127 baud, sp_baudtable[i].baud);
128 return &sp_baudtable[i];
129 }
130 }
131 return &sp_baudtable[0];
132}
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000133
Stefan Taunerbf88be92013-04-01 00:45:08 +0000134/* Uses msg_perr to print the last system error.
135 * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
136 * strerror() or FormatMessage() and ending with a linebreak. */
137static void msg_perr_strerror(const char *msg)
138{
139 msg_perr("Error: %s", msg);
140#ifdef _WIN32
141 char *lpMsgBuf;
142 DWORD nErr = GetLastError();
143 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
144 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
145 msg_perr(lpMsgBuf);
146 /* At least some formatted messages contain a line break at the end. Make sure to always print one */
147 if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
148 msg_perr("\n");
149 LocalFree(lpMsgBuf);
150#else
151 msg_perr("%s\n", strerror(errno));
152#endif
153}
154
Stefan Tauner184c52c2013-08-23 21:51:32 +0000155int serialport_config(fdtype fd, unsigned int baud)
156{
157 if (fd == SER_INV_FD) {
158 msg_perr("%s: File descriptor is invalid.\n", __func__);
159 return 1;
160 }
161
162#ifdef _WIN32
163 DCB dcb;
164 if (!GetCommState(fd, &dcb)) {
165 msg_perr_strerror("Could not fetch original serial port configuration: ");
166 return 1;
167 }
168 const struct baudentry *entry = round_baud(baud);
169 dcb.BaudRate = entry->flag;
170 dcb.ByteSize = 8;
171 dcb.Parity = NOPARITY;
172 dcb.StopBits = ONESTOPBIT;
173 if (!SetCommState(fd, &dcb)) {
174 msg_perr_strerror("Could not change serial port configuration: ");
175 return 1;
176 }
177 if (!GetCommState(fd, &dcb)) {
178 msg_perr_strerror("Could not fetch new serial port configuration: ");
179 return 1;
180 }
181 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
182#else
183 struct termios wanted, observed;
184 fcntl(fd, F_SETFL, 0);
185 if (tcgetattr(fd, &observed) != 0) {
186 msg_perr_strerror("Could not fetch original serial port configuration: ");
187 return 1;
188 }
189 wanted = observed;
190 const struct baudentry *entry = round_baud(baud);
191 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
192 msg_perr_strerror("Could not set serial baud rate: ");
193 return 1;
194 }
195 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
196 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
197 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
198 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
199 wanted.c_oflag &= ~OPOST;
200 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
201 msg_perr_strerror("Could not change serial port configuration: ");
202 return 1;
203 }
204 if (tcgetattr(fd, &observed) != 0) {
205 msg_perr_strerror("Could not fetch new serial port configuration: ");
206 return 1;
207 }
208 if (observed.c_cflag != wanted.c_cflag ||
209 observed.c_lflag != wanted.c_lflag ||
210 observed.c_iflag != wanted.c_iflag ||
211 observed.c_oflag != wanted.c_oflag ||
212 cfgetispeed(&observed) != cfgetispeed(&wanted)) {
213 msg_perr("%s: Some requested options did not stick.\n", __func__);
214 return 1;
215 }
216 msg_pdbg("Baud rate is %d now.\n", entry->baud);
217#endif
218 return 0;
219}
220
Patrick Georgie48654c2010-01-06 22:14:39 +0000221fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000222{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000223 fdtype fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000224#ifdef _WIN32
Uwe Hermann43959702010-03-13 17:28:29 +0000225 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000226 if ((strlen(dev) > 3) &&
227 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000228 (tolower((unsigned char)dev[1]) == 'o') &&
229 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000230 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000231 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000232 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000233 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000234 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000235 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000236 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000237 }
Uwe Hermann43959702010-03-13 17:28:29 +0000238 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
239 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000240 if (dev2 != dev)
241 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000242 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000243 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000244 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000245 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000246 if (serialport_config(fd, baud) != 0) {
247 CloseHandle(fd);
248 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000249 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000250 return fd;
251#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000252 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000253 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000254 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000255 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000256 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000257 if (serialport_config(fd, baud) != 0) {
258 close(fd);
259 return SER_INV_FD;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000260 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000261 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000262#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000263}
264
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000265void sp_set_pin(enum SP_PIN pin, int val) {
266#ifdef _WIN32
267 DWORD ctl;
268
269 if(pin == PIN_TXD) {
270 ctl = val ? SETBREAK: CLRBREAK;
271 }
272 else if(pin == PIN_DTR) {
273 ctl = val ? SETDTR: CLRDTR;
274 }
275 else {
276 ctl = val ? SETRTS: CLRRTS;
277 }
278 EscapeCommFunction(sp_fd, ctl);
279#else
280 int ctl, s;
281
282 if(pin == PIN_TXD) {
283 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
284 }
285 else {
286 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
287 ioctl(sp_fd, TIOCMGET, &ctl);
288
289 if (val) {
290 ctl |= s;
291 }
292 else {
293 ctl &= ~s;
294 }
295 ioctl(sp_fd, TIOCMSET, &ctl);
296 }
297#endif
298}
299
300int sp_get_pin(enum SP_PIN pin) {
301 int s;
302#ifdef _WIN32
303 DWORD ctl;
304
305 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
306 GetCommModemStatus(sp_fd, &ctl);
307#else
308 int ctl;
309 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
310 ioctl(sp_fd, TIOCMGET, &ctl);
311#endif
312
313 return ((ctl & s) ? 1 : 0);
314
315}
316
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000317void sp_flush_incoming(void)
318{
Patrick Georgie48654c2010-01-06 22:14:39 +0000319#ifdef _WIN32
320 PurgeComm(sp_fd, PURGE_RXCLEAR);
321#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000322 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000323 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000324#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000325 return;
326}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000327
David Hendricks8bb20212011-06-14 01:35:36 +0000328int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000329{
Patrick Georgie48654c2010-01-06 22:14:39 +0000330#ifdef _WIN32
331 CloseHandle(sp_fd);
332#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000333 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000334#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000335 return 0;
336}
337
Mark Marshallf20b7be2014-05-09 21:16:21 +0000338int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000339{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000340#ifdef _WIN32
341 DWORD tmp = 0;
342#else
343 ssize_t tmp = 0;
344#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000345 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000346
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000347 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000348#ifdef _WIN32
349 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
350#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000351 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000352#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000353 if (tmp == -1) {
354 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000355 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000356 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000357 if (!tmp) {
358 msg_pdbg2("Empty write\n");
359 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000360 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000361 if (empty_writes == 0) {
362 msg_perr("Serial port is unresponsive!\n");
363 return 1;
364 }
365 }
366 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000367 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000368 }
369
370 return 0;
371}
372
373int serialport_read(unsigned char *buf, unsigned int readcnt)
374{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000375#ifdef _WIN32
376 DWORD tmp = 0;
377#else
378 ssize_t tmp = 0;
379#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000380
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000381 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000382#ifdef _WIN32
383 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
384#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000385 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000386#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000387 if (tmp == -1) {
388 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000389 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000390 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000391 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000392 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000393 readcnt -= tmp;
394 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000395 }
396
397 return 0;
398}
Stefan Tauner00e16082013-04-01 00:45:38 +0000399
400/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
401 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
402 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
403int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
404{
405 int ret = 1;
406 /* disable blocked i/o and declare platform-specific variables */
407#ifdef _WIN32
408 DWORD rv;
409 COMMTIMEOUTS oldTimeout;
410 COMMTIMEOUTS newTimeout = {
411 .ReadIntervalTimeout = MAXDWORD,
412 .ReadTotalTimeoutMultiplier = 0,
413 .ReadTotalTimeoutConstant = 0,
414 .WriteTotalTimeoutMultiplier = 0,
415 .WriteTotalTimeoutConstant = 0
416 };
417 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
418 msg_perr_strerror("Could not get serial port timeout settings: ");
419 return -1;
420 }
421 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
422#else
423 ssize_t rv;
424 const int flags = fcntl(sp_fd, F_GETFL);
425 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
426#endif
427 msg_perr_strerror("Could not set serial port timeout settings %s");
428 return -1;
429 }
430
431 int i;
432 int rd_bytes = 0;
433 for (i = 0; i < timeout; i++) {
434 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
435#ifdef _WIN32
436 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
437 msg_pspew("read %lu bytes\n", rv);
438#else
439 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
440 msg_pspew("read %zd bytes\n", rv);
441#endif
442 if ((rv == -1) && (errno != EAGAIN)) {
443 msg_perr_strerror("Serial port read error: ");
444 ret = -1;
445 break;
446 }
447 if (rv > 0)
448 rd_bytes += rv;
449 if (rd_bytes == readcnt) {
450 ret = 0;
451 break;
452 }
453 internal_delay(1000); /* 1ms units */
454 }
455 if (really_read != NULL)
456 *really_read = rd_bytes;
457
458 /* restore original blocking behavior */
459#ifdef _WIN32
460 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
461#else
462 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
463#endif
464 msg_perr_strerror("Could not restore serial port timeout settings: %s\n");
465 ret = -1;
466 }
467 return ret;
468}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000469
470/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
471 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
472 * 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 +0000473int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000474{
475 int ret = 1;
476 /* disable blocked i/o and declare platform-specific variables */
477#ifdef _WIN32
478 DWORD rv;
479 COMMTIMEOUTS oldTimeout;
480 COMMTIMEOUTS newTimeout = {
481 .ReadIntervalTimeout = MAXDWORD,
482 .ReadTotalTimeoutMultiplier = 0,
483 .ReadTotalTimeoutConstant = 0,
484 .WriteTotalTimeoutMultiplier = 0,
485 .WriteTotalTimeoutConstant = 0
486 };
487 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
488 msg_perr_strerror("Could not get serial port timeout settings: ");
489 return -1;
490 }
491 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
492 msg_perr_strerror("Could not set serial port timeout settings: ");
493 return -1;
494 }
495#else
496 ssize_t rv;
497 const int flags = fcntl(sp_fd, F_GETFL);
498 fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK);
499#endif
500
501 int i;
502 int wr_bytes = 0;
503 for (i = 0; i < timeout; i++) {
504 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
505#ifdef _WIN32
506 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
507 msg_pspew("wrote %lu bytes\n", rv);
508#else
509 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
510 msg_pspew("wrote %zd bytes\n", rv);
511#endif
512 if ((rv == -1) && (errno != EAGAIN)) {
513 msg_perr_strerror("Serial port write error: ");
514 ret = -1;
515 break;
516 }
517 if (rv > 0) {
518 wr_bytes += rv;
519 if (wr_bytes == writecnt) {
520 msg_pspew("write successful\n");
521 ret = 0;
522 break;
523 }
524 }
525 internal_delay(1000); /* 1ms units */
526 }
527 if (really_wrote != NULL)
528 *really_wrote = wr_bytes;
529
530 /* restore original blocking behavior */
531#ifdef _WIN32
532 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
533 msg_perr_strerror("Could not restore serial port timeout settings: ");
534#else
535 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
536#endif
537 return -1;
538 }
539 return ret;
540}