blob: 562a95af18f89f2f549c5c8bdb2b5320235021cd [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
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
Stefan Tauner184c52c2013-08-23 21:51:32 +0000161int serialport_config(fdtype fd, unsigned int baud)
162{
163 if (fd == SER_INV_FD) {
164 msg_perr("%s: File descriptor is invalid.\n", __func__);
165 return 1;
166 }
167
168#ifdef _WIN32
169 DCB dcb;
170 if (!GetCommState(fd, &dcb)) {
171 msg_perr_strerror("Could not fetch original serial port configuration: ");
172 return 1;
173 }
174 const struct baudentry *entry = round_baud(baud);
175 dcb.BaudRate = entry->flag;
176 dcb.ByteSize = 8;
177 dcb.Parity = NOPARITY;
178 dcb.StopBits = ONESTOPBIT;
179 if (!SetCommState(fd, &dcb)) {
180 msg_perr_strerror("Could not change serial port configuration: ");
181 return 1;
182 }
183 if (!GetCommState(fd, &dcb)) {
184 msg_perr_strerror("Could not fetch new serial port configuration: ");
185 return 1;
186 }
187 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
188#else
189 struct termios wanted, observed;
190 fcntl(fd, F_SETFL, 0);
191 if (tcgetattr(fd, &observed) != 0) {
192 msg_perr_strerror("Could not fetch original serial port configuration: ");
193 return 1;
194 }
195 wanted = observed;
196 const struct baudentry *entry = round_baud(baud);
197 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
198 msg_perr_strerror("Could not set serial baud rate: ");
199 return 1;
200 }
201 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
202 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
203 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
204 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
205 wanted.c_oflag &= ~OPOST;
206 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
207 msg_perr_strerror("Could not change serial port configuration: ");
208 return 1;
209 }
210 if (tcgetattr(fd, &observed) != 0) {
211 msg_perr_strerror("Could not fetch new serial port configuration: ");
212 return 1;
213 }
214 if (observed.c_cflag != wanted.c_cflag ||
215 observed.c_lflag != wanted.c_lflag ||
216 observed.c_iflag != wanted.c_iflag ||
217 observed.c_oflag != wanted.c_oflag ||
218 cfgetispeed(&observed) != cfgetispeed(&wanted)) {
219 msg_perr("%s: Some requested options did not stick.\n", __func__);
220 return 1;
221 }
222 msg_pdbg("Baud rate is %d now.\n", entry->baud);
223#endif
224 return 0;
225}
226
Patrick Georgie48654c2010-01-06 22:14:39 +0000227fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000228{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000229 fdtype fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000230#ifdef _WIN32
Uwe Hermann43959702010-03-13 17:28:29 +0000231 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000232 if ((strlen(dev) > 3) &&
233 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000234 (tolower((unsigned char)dev[1]) == 'o') &&
235 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000236 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000237 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000238 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000239 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000240 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000241 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000242 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000243 }
Uwe Hermann43959702010-03-13 17:28:29 +0000244 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
245 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000246 if (dev2 != dev)
247 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000248 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000249 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000250 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000251 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000252 if (serialport_config(fd, baud) != 0) {
253 CloseHandle(fd);
254 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000255 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000256 return fd;
257#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000258 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000259 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000260 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000261 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000262 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000263 if (serialport_config(fd, baud) != 0) {
264 close(fd);
265 return SER_INV_FD;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000266 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000267 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000268#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000269}
270
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000271void sp_set_pin(enum SP_PIN pin, int val) {
272#ifdef _WIN32
273 DWORD ctl;
274
275 if(pin == PIN_TXD) {
276 ctl = val ? SETBREAK: CLRBREAK;
277 }
278 else if(pin == PIN_DTR) {
279 ctl = val ? SETDTR: CLRDTR;
280 }
281 else {
282 ctl = val ? SETRTS: CLRRTS;
283 }
284 EscapeCommFunction(sp_fd, ctl);
285#else
286 int ctl, s;
287
288 if(pin == PIN_TXD) {
289 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
290 }
291 else {
292 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
293 ioctl(sp_fd, TIOCMGET, &ctl);
294
295 if (val) {
296 ctl |= s;
297 }
298 else {
299 ctl &= ~s;
300 }
301 ioctl(sp_fd, TIOCMSET, &ctl);
302 }
303#endif
304}
305
306int sp_get_pin(enum SP_PIN pin) {
307 int s;
308#ifdef _WIN32
309 DWORD ctl;
310
311 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
312 GetCommModemStatus(sp_fd, &ctl);
313#else
314 int ctl;
315 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
316 ioctl(sp_fd, TIOCMGET, &ctl);
317#endif
318
319 return ((ctl & s) ? 1 : 0);
320
321}
322
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000323void sp_flush_incoming(void)
324{
Patrick Georgie48654c2010-01-06 22:14:39 +0000325#ifdef _WIN32
326 PurgeComm(sp_fd, PURGE_RXCLEAR);
327#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000328 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000329 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000330#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000331 return;
332}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000333
David Hendricks8bb20212011-06-14 01:35:36 +0000334int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000335{
Patrick Georgie48654c2010-01-06 22:14:39 +0000336#ifdef _WIN32
337 CloseHandle(sp_fd);
338#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000339 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000340#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000341 return 0;
342}
343
344int serialport_write(unsigned char *buf, unsigned int writecnt)
345{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000346#ifdef _WIN32
347 DWORD tmp = 0;
348#else
349 ssize_t tmp = 0;
350#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000351 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000352
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000353 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000354#ifdef _WIN32
355 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
356#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000357 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000358#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000359 if (tmp == -1) {
360 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000361 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000362 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000363 if (!tmp) {
364 msg_pdbg2("Empty write\n");
365 empty_writes--;
366 programmer_delay(500);
367 if (empty_writes == 0) {
368 msg_perr("Serial port is unresponsive!\n");
369 return 1;
370 }
371 }
372 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000373 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000374 }
375
376 return 0;
377}
378
379int serialport_read(unsigned char *buf, unsigned int readcnt)
380{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000381#ifdef _WIN32
382 DWORD tmp = 0;
383#else
384 ssize_t tmp = 0;
385#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000386
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000387 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000388#ifdef _WIN32
389 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
390#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000391 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000392#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000393 if (tmp == -1) {
394 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000395 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000396 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000397 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000398 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000399 readcnt -= tmp;
400 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000401 }
402
403 return 0;
404}
Stefan Tauner00e16082013-04-01 00:45:38 +0000405
406/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
407 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
408 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
409int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
410{
411 int ret = 1;
412 /* disable blocked i/o and declare platform-specific variables */
413#ifdef _WIN32
414 DWORD rv;
415 COMMTIMEOUTS oldTimeout;
416 COMMTIMEOUTS newTimeout = {
417 .ReadIntervalTimeout = MAXDWORD,
418 .ReadTotalTimeoutMultiplier = 0,
419 .ReadTotalTimeoutConstant = 0,
420 .WriteTotalTimeoutMultiplier = 0,
421 .WriteTotalTimeoutConstant = 0
422 };
423 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
424 msg_perr_strerror("Could not get serial port timeout settings: ");
425 return -1;
426 }
427 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
428#else
429 ssize_t rv;
430 const int flags = fcntl(sp_fd, F_GETFL);
431 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
432#endif
433 msg_perr_strerror("Could not set serial port timeout settings %s");
434 return -1;
435 }
436
437 int i;
438 int rd_bytes = 0;
439 for (i = 0; i < timeout; i++) {
440 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
441#ifdef _WIN32
442 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
443 msg_pspew("read %lu bytes\n", rv);
444#else
445 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
446 msg_pspew("read %zd bytes\n", rv);
447#endif
448 if ((rv == -1) && (errno != EAGAIN)) {
449 msg_perr_strerror("Serial port read error: ");
450 ret = -1;
451 break;
452 }
453 if (rv > 0)
454 rd_bytes += rv;
455 if (rd_bytes == readcnt) {
456 ret = 0;
457 break;
458 }
459 internal_delay(1000); /* 1ms units */
460 }
461 if (really_read != NULL)
462 *really_read = rd_bytes;
463
464 /* restore original blocking behavior */
465#ifdef _WIN32
466 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
467#else
468 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
469#endif
470 msg_perr_strerror("Could not restore serial port timeout settings: %s\n");
471 ret = -1;
472 }
473 return ret;
474}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000475
476/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
477 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
478 * If really_wrote is not NULL, this function sets its contents to the number of bytes written successfully. */
479int serialport_write_nonblock(unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
480{
481 int ret = 1;
482 /* disable blocked i/o and declare platform-specific variables */
483#ifdef _WIN32
484 DWORD rv;
485 COMMTIMEOUTS oldTimeout;
486 COMMTIMEOUTS newTimeout = {
487 .ReadIntervalTimeout = MAXDWORD,
488 .ReadTotalTimeoutMultiplier = 0,
489 .ReadTotalTimeoutConstant = 0,
490 .WriteTotalTimeoutMultiplier = 0,
491 .WriteTotalTimeoutConstant = 0
492 };
493 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
494 msg_perr_strerror("Could not get serial port timeout settings: ");
495 return -1;
496 }
497 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
498 msg_perr_strerror("Could not set serial port timeout settings: ");
499 return -1;
500 }
501#else
502 ssize_t rv;
503 const int flags = fcntl(sp_fd, F_GETFL);
504 fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK);
505#endif
506
507 int i;
508 int wr_bytes = 0;
509 for (i = 0; i < timeout; i++) {
510 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
511#ifdef _WIN32
512 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
513 msg_pspew("wrote %lu bytes\n", rv);
514#else
515 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
516 msg_pspew("wrote %zd bytes\n", rv);
517#endif
518 if ((rv == -1) && (errno != EAGAIN)) {
519 msg_perr_strerror("Serial port write error: ");
520 ret = -1;
521 break;
522 }
523 if (rv > 0) {
524 wr_bytes += rv;
525 if (wr_bytes == writecnt) {
526 msg_pspew("write successful\n");
527 ret = 0;
528 break;
529 }
530 }
531 internal_delay(1000); /* 1ms units */
532 }
533 if (really_wrote != NULL)
534 *really_wrote = wr_bytes;
535
536 /* restore original blocking behavior */
537#ifdef _WIN32
538 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
539 msg_perr_strerror("Could not restore serial port timeout settings: ");
540#else
541 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
542#endif
543 return -1;
544 }
545 return ret;
546}