blob: 49484f5dbd9a360bdacd805ade1974356391deaf [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 Taunerf966cc42013-04-01 00:45:57 +0000188 msg_perr_strerror("Could not fetch original serial port configuration: ");
189 goto out_close;
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 Taunerf966cc42013-04-01 00:45:57 +0000198 goto out_close;
Patrick Georgie48654c2010-01-06 22:14:39 +0000199 }
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000200 if (!GetCommState(fd, &dcb)) {
Stefan Taunerf966cc42013-04-01 00:45:57 +0000201 msg_perr_strerror("Could not fetch new serial port configuration: ");
202 goto out_close;
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000203 }
204 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
Patrick Georgie48654c2010-01-06 22:14:39 +0000205 return fd;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000206out_close:
207 CloseHandle(sp_fd);
208 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000209#else
Stefan Taunerf966cc42013-04-01 00:45:57 +0000210 struct termios wanted, observed;
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000211 int fd;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000212 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000213 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000214 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000215 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000216 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000217 fcntl(fd, F_SETFL, 0);
Stefan Taunerf966cc42013-04-01 00:45:57 +0000218 if (tcgetattr(fd, &observed) != 0) {
219 msg_perr_strerror("Could not fetch original serial port configuration: ");
220 goto out_close;
221 }
222 wanted = observed;
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000223 const struct baudentry *entry = round_baud(baud);
Stefan Taunerf966cc42013-04-01 00:45:57 +0000224 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
225 msg_perr_strerror("Could not set serial baud rate: ");
226 goto out_close;
227 }
228 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
229 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
230 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
231 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
232 wanted.c_oflag &= ~OPOST;
233 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
234 msg_perr_strerror("Could not change serial port configuration: ");
235 goto out_close;
236 }
237 if (tcgetattr(fd, &observed) != 0) {
238 msg_perr_strerror("Could not fetch new serial port configuration: ");
239 goto out_close;
240 }
241 if (observed.c_cflag != wanted.c_cflag ||
242 observed.c_lflag != wanted.c_lflag ||
243 observed.c_iflag != wanted.c_iflag ||
244 observed.c_oflag != wanted.c_oflag ||
245 cfgetispeed(&observed) != cfgetispeed(&wanted)) {
246 msg_perr("%s: Some requested options did not stick.\n", __func__);
247 goto out_close;
248 }
249 msg_pdbg("Baud rate is %d.\n", entry->baud);
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000250 return fd;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000251
252out_close:
253 close(sp_fd);
254 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000255#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000256}
257
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000258void sp_set_pin(enum SP_PIN pin, int val) {
259#ifdef _WIN32
260 DWORD ctl;
261
262 if(pin == PIN_TXD) {
263 ctl = val ? SETBREAK: CLRBREAK;
264 }
265 else if(pin == PIN_DTR) {
266 ctl = val ? SETDTR: CLRDTR;
267 }
268 else {
269 ctl = val ? SETRTS: CLRRTS;
270 }
271 EscapeCommFunction(sp_fd, ctl);
272#else
273 int ctl, s;
274
275 if(pin == PIN_TXD) {
276 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
277 }
278 else {
279 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
280 ioctl(sp_fd, TIOCMGET, &ctl);
281
282 if (val) {
283 ctl |= s;
284 }
285 else {
286 ctl &= ~s;
287 }
288 ioctl(sp_fd, TIOCMSET, &ctl);
289 }
290#endif
291}
292
293int sp_get_pin(enum SP_PIN pin) {
294 int s;
295#ifdef _WIN32
296 DWORD ctl;
297
298 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
299 GetCommModemStatus(sp_fd, &ctl);
300#else
301 int ctl;
302 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
303 ioctl(sp_fd, TIOCMGET, &ctl);
304#endif
305
306 return ((ctl & s) ? 1 : 0);
307
308}
309
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000310void sp_flush_incoming(void)
311{
Patrick Georgie48654c2010-01-06 22:14:39 +0000312#ifdef _WIN32
313 PurgeComm(sp_fd, PURGE_RXCLEAR);
314#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000315 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000316 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000317#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000318 return;
319}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000320
David Hendricks8bb20212011-06-14 01:35:36 +0000321int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000322{
Patrick Georgie48654c2010-01-06 22:14:39 +0000323#ifdef _WIN32
324 CloseHandle(sp_fd);
325#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000326 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000327#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000328 return 0;
329}
330
331int serialport_write(unsigned char *buf, unsigned int writecnt)
332{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000333#ifdef _WIN32
334 DWORD tmp = 0;
335#else
336 ssize_t tmp = 0;
337#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000338 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000339
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000340 while (writecnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000341#ifdef _WIN32
342 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
343#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000344 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000345#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000346 if (tmp == -1) {
347 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000348 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000349 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000350 if (!tmp) {
351 msg_pdbg2("Empty write\n");
352 empty_writes--;
353 programmer_delay(500);
354 if (empty_writes == 0) {
355 msg_perr("Serial port is unresponsive!\n");
356 return 1;
357 }
358 }
359 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000360 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000361 }
362
363 return 0;
364}
365
366int serialport_read(unsigned char *buf, unsigned int readcnt)
367{
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000368#ifdef _WIN32
369 DWORD tmp = 0;
370#else
371 ssize_t tmp = 0;
372#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000373
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000374 while (readcnt > 0) {
Patrick Georgie48654c2010-01-06 22:14:39 +0000375#ifdef _WIN32
376 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
377#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000378 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000379#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000380 if (tmp == -1) {
381 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000382 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000383 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000384 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000385 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000386 readcnt -= tmp;
387 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000388 }
389
390 return 0;
391}
Stefan Tauner00e16082013-04-01 00:45:38 +0000392
393/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
394 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
395 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
396int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
397{
398 int ret = 1;
399 /* disable blocked i/o and declare platform-specific variables */
400#ifdef _WIN32
401 DWORD rv;
402 COMMTIMEOUTS oldTimeout;
403 COMMTIMEOUTS newTimeout = {
404 .ReadIntervalTimeout = MAXDWORD,
405 .ReadTotalTimeoutMultiplier = 0,
406 .ReadTotalTimeoutConstant = 0,
407 .WriteTotalTimeoutMultiplier = 0,
408 .WriteTotalTimeoutConstant = 0
409 };
410 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
411 msg_perr_strerror("Could not get serial port timeout settings: ");
412 return -1;
413 }
414 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
415#else
416 ssize_t rv;
417 const int flags = fcntl(sp_fd, F_GETFL);
418 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
419#endif
420 msg_perr_strerror("Could not set serial port timeout settings %s");
421 return -1;
422 }
423
424 int i;
425 int rd_bytes = 0;
426 for (i = 0; i < timeout; i++) {
427 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
428#ifdef _WIN32
429 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
430 msg_pspew("read %lu bytes\n", rv);
431#else
432 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
433 msg_pspew("read %zd bytes\n", rv);
434#endif
435 if ((rv == -1) && (errno != EAGAIN)) {
436 msg_perr_strerror("Serial port read error: ");
437 ret = -1;
438 break;
439 }
440 if (rv > 0)
441 rd_bytes += rv;
442 if (rd_bytes == readcnt) {
443 ret = 0;
444 break;
445 }
446 internal_delay(1000); /* 1ms units */
447 }
448 if (really_read != NULL)
449 *really_read = rd_bytes;
450
451 /* restore original blocking behavior */
452#ifdef _WIN32
453 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
454#else
455 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
456#endif
457 msg_perr_strerror("Could not restore serial port timeout settings: %s\n");
458 ret = -1;
459 }
460 return ret;
461}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000462
463/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
464 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
465 * If really_wrote is not NULL, this function sets its contents to the number of bytes written successfully. */
466int serialport_write_nonblock(unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
467{
468 int ret = 1;
469 /* disable blocked i/o and declare platform-specific variables */
470#ifdef _WIN32
471 DWORD rv;
472 COMMTIMEOUTS oldTimeout;
473 COMMTIMEOUTS newTimeout = {
474 .ReadIntervalTimeout = MAXDWORD,
475 .ReadTotalTimeoutMultiplier = 0,
476 .ReadTotalTimeoutConstant = 0,
477 .WriteTotalTimeoutMultiplier = 0,
478 .WriteTotalTimeoutConstant = 0
479 };
480 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
481 msg_perr_strerror("Could not get serial port timeout settings: ");
482 return -1;
483 }
484 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
485 msg_perr_strerror("Could not set serial port timeout settings: ");
486 return -1;
487 }
488#else
489 ssize_t rv;
490 const int flags = fcntl(sp_fd, F_GETFL);
491 fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK);
492#endif
493
494 int i;
495 int wr_bytes = 0;
496 for (i = 0; i < timeout; i++) {
497 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
498#ifdef _WIN32
499 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
500 msg_pspew("wrote %lu bytes\n", rv);
501#else
502 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
503 msg_pspew("wrote %zd bytes\n", rv);
504#endif
505 if ((rv == -1) && (errno != EAGAIN)) {
506 msg_perr_strerror("Serial port write error: ");
507 ret = -1;
508 break;
509 }
510 if (rv > 0) {
511 wr_bytes += rv;
512 if (wr_bytes == writecnt) {
513 msg_pspew("write successful\n");
514 ret = 0;
515 break;
516 }
517 }
518 internal_delay(1000); /* 1ms units */
519 }
520 if (really_wrote != NULL)
521 *really_wrote = wr_bytes;
522
523 /* restore original blocking behavior */
524#ifdef _WIN32
525 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
526 msg_perr_strerror("Could not restore serial port timeout settings: ");
527#else
528 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
529#endif
530 return -1;
531 }
532 return ret;
533}