blob: e4e911d0a082ce7722b963bc826ddbfa9f5d9d4c [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;
Stefan Reinauer0df84462014-05-27 22:10:15 +0000184 if (fcntl(fd, F_SETFL, 0) != 0) {
185 msg_perr_strerror("Could not clear serial port mode: ");
186 return 1;
187 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000188 if (tcgetattr(fd, &observed) != 0) {
189 msg_perr_strerror("Could not fetch original serial port configuration: ");
190 return 1;
191 }
192 wanted = observed;
193 const struct baudentry *entry = round_baud(baud);
194 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
195 msg_perr_strerror("Could not set serial baud rate: ");
196 return 1;
197 }
198 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
199 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
200 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
201 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
202 wanted.c_oflag &= ~OPOST;
203 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
204 msg_perr_strerror("Could not change serial port configuration: ");
205 return 1;
206 }
207 if (tcgetattr(fd, &observed) != 0) {
208 msg_perr_strerror("Could not fetch new serial port configuration: ");
209 return 1;
210 }
211 if (observed.c_cflag != wanted.c_cflag ||
212 observed.c_lflag != wanted.c_lflag ||
213 observed.c_iflag != wanted.c_iflag ||
214 observed.c_oflag != wanted.c_oflag ||
215 cfgetispeed(&observed) != cfgetispeed(&wanted)) {
216 msg_perr("%s: Some requested options did not stick.\n", __func__);
217 return 1;
218 }
219 msg_pdbg("Baud rate is %d now.\n", entry->baud);
220#endif
221 return 0;
222}
223
Patrick Georgie48654c2010-01-06 22:14:39 +0000224fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000225{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000226 fdtype fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000227#ifdef _WIN32
Uwe Hermann43959702010-03-13 17:28:29 +0000228 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000229 if ((strlen(dev) > 3) &&
230 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000231 (tolower((unsigned char)dev[1]) == 'o') &&
232 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000233 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000234 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000235 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000236 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000237 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000238 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000239 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000240 }
Uwe Hermann43959702010-03-13 17:28:29 +0000241 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
242 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000243 if (dev2 != dev)
244 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000245 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000246 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000247 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000248 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000249 if (serialport_config(fd, baud) != 0) {
250 CloseHandle(fd);
251 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000252 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000253 return fd;
254#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000255 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000256 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000257 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000258 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000259 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000260 if (serialport_config(fd, baud) != 0) {
261 close(fd);
262 return SER_INV_FD;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000263 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000264 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000265#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000266}
267
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000268void sp_set_pin(enum SP_PIN pin, int val) {
269#ifdef _WIN32
270 DWORD ctl;
271
272 if(pin == PIN_TXD) {
273 ctl = val ? SETBREAK: CLRBREAK;
274 }
275 else if(pin == PIN_DTR) {
276 ctl = val ? SETDTR: CLRDTR;
277 }
278 else {
279 ctl = val ? SETRTS: CLRRTS;
280 }
281 EscapeCommFunction(sp_fd, ctl);
282#else
283 int ctl, s;
284
285 if(pin == PIN_TXD) {
286 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
287 }
288 else {
289 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
290 ioctl(sp_fd, TIOCMGET, &ctl);
291
292 if (val) {
293 ctl |= s;
294 }
295 else {
296 ctl &= ~s;
297 }
298 ioctl(sp_fd, TIOCMSET, &ctl);
299 }
300#endif
301}
302
303int sp_get_pin(enum SP_PIN pin) {
304 int s;
305#ifdef _WIN32
306 DWORD ctl;
307
308 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
309 GetCommModemStatus(sp_fd, &ctl);
310#else
311 int ctl;
312 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
313 ioctl(sp_fd, TIOCMGET, &ctl);
314#endif
315
316 return ((ctl & s) ? 1 : 0);
317
318}
319
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000320void sp_flush_incoming(void)
321{
Patrick Georgie48654c2010-01-06 22:14:39 +0000322#ifdef _WIN32
323 PurgeComm(sp_fd, PURGE_RXCLEAR);
324#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000325 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000326 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000327#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000328 return;
329}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000330
David Hendricks8bb20212011-06-14 01:35:36 +0000331int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000332{
Patrick Georgie48654c2010-01-06 22:14:39 +0000333#ifdef _WIN32
334 CloseHandle(sp_fd);
335#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000336 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000337#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000338 return 0;
339}
340
Mark Marshallf20b7be2014-05-09 21:16:21 +0000341int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000342{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000343#ifdef _WIN32
344 DWORD tmp = 0;
345#else
346 ssize_t tmp = 0;
347#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000348 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000349
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000350 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000351#ifdef _WIN32
352 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
353#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000354 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000355#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000356 if (tmp == -1) {
357 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000358 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000359 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000360 if (!tmp) {
361 msg_pdbg2("Empty write\n");
362 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000363 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000364 if (empty_writes == 0) {
365 msg_perr("Serial port is unresponsive!\n");
366 return 1;
367 }
368 }
369 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000370 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000371 }
372
373 return 0;
374}
375
376int serialport_read(unsigned char *buf, unsigned int readcnt)
377{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000378#ifdef _WIN32
379 DWORD tmp = 0;
380#else
381 ssize_t tmp = 0;
382#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000383
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000384 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000385#ifdef _WIN32
386 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
387#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000388 tmp = read(sp_fd, buf, readcnt);
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 read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000392 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000393 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000394 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000395 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000396 readcnt -= tmp;
397 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000398 }
399
400 return 0;
401}
Stefan Tauner00e16082013-04-01 00:45:38 +0000402
403/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
404 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
405 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
406int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
407{
408 int ret = 1;
409 /* disable blocked i/o and declare platform-specific variables */
410#ifdef _WIN32
411 DWORD rv;
412 COMMTIMEOUTS oldTimeout;
413 COMMTIMEOUTS newTimeout = {
414 .ReadIntervalTimeout = MAXDWORD,
415 .ReadTotalTimeoutMultiplier = 0,
416 .ReadTotalTimeoutConstant = 0,
417 .WriteTotalTimeoutMultiplier = 0,
418 .WriteTotalTimeoutConstant = 0
419 };
420 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
421 msg_perr_strerror("Could not get serial port timeout settings: ");
422 return -1;
423 }
424 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000425 msg_perr_strerror("Could not set serial port timeout settings: ");
426 return -1;
427 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000428#else
429 ssize_t rv;
430 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000431 if (flags == -1) {
432 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000433 return -1;
434 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000435 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
436 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
437 return -1;
438 }
439#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000440
441 int i;
442 int rd_bytes = 0;
443 for (i = 0; i < timeout; i++) {
444 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
445#ifdef _WIN32
446 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
447 msg_pspew("read %lu bytes\n", rv);
448#else
449 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
450 msg_pspew("read %zd bytes\n", rv);
451#endif
452 if ((rv == -1) && (errno != EAGAIN)) {
453 msg_perr_strerror("Serial port read error: ");
454 ret = -1;
455 break;
456 }
457 if (rv > 0)
458 rd_bytes += rv;
459 if (rd_bytes == readcnt) {
460 ret = 0;
461 break;
462 }
463 internal_delay(1000); /* 1ms units */
464 }
465 if (really_read != NULL)
466 *really_read = rd_bytes;
467
468 /* restore original blocking behavior */
469#ifdef _WIN32
470 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000471 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000472 ret = -1;
473 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000474#else
475 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
476 msg_perr_strerror("Could not restore serial port mode to blocking: ");
477 ret = -1;
478 }
479#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000480 return ret;
481}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000482
483/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
484 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
485 * 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 +0000486int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000487{
488 int ret = 1;
489 /* disable blocked i/o and declare platform-specific variables */
490#ifdef _WIN32
491 DWORD rv;
492 COMMTIMEOUTS oldTimeout;
493 COMMTIMEOUTS newTimeout = {
494 .ReadIntervalTimeout = MAXDWORD,
495 .ReadTotalTimeoutMultiplier = 0,
496 .ReadTotalTimeoutConstant = 0,
497 .WriteTotalTimeoutMultiplier = 0,
498 .WriteTotalTimeoutConstant = 0
499 };
500 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
501 msg_perr_strerror("Could not get serial port timeout settings: ");
502 return -1;
503 }
504 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
505 msg_perr_strerror("Could not set serial port timeout settings: ");
506 return -1;
507 }
508#else
509 ssize_t rv;
510 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000511 if (flags == -1) {
512 msg_perr_strerror("Could not get serial port mode: ");
513 return -1;
514 }
515 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
516 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
517 return -1;
518 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000519#endif
520
521 int i;
522 int wr_bytes = 0;
523 for (i = 0; i < timeout; i++) {
524 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
525#ifdef _WIN32
526 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
527 msg_pspew("wrote %lu bytes\n", rv);
528#else
529 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
530 msg_pspew("wrote %zd bytes\n", rv);
531#endif
532 if ((rv == -1) && (errno != EAGAIN)) {
533 msg_perr_strerror("Serial port write error: ");
534 ret = -1;
535 break;
536 }
537 if (rv > 0) {
538 wr_bytes += rv;
539 if (wr_bytes == writecnt) {
540 msg_pspew("write successful\n");
541 ret = 0;
542 break;
543 }
544 }
545 internal_delay(1000); /* 1ms units */
546 }
547 if (really_wrote != NULL)
548 *really_wrote = wr_bytes;
549
550 /* restore original blocking behavior */
551#ifdef _WIN32
552 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
553 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000554 return -1;
555 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000556#else
557 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
558 msg_perr_strerror("Could not restore serial port blocking behavior: ");
559 return -1;
560 }
561#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000562 return ret;
563}