blob: 63774a5e6485b4cf9c2b9de7278424f7b5229d2a [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
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000022#include "platform.h"
23
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000024#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000027#include <string.h>
28#include <ctype.h>
29#include <fcntl.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000030#include <sys/stat.h>
31#include <errno.h>
32#include <inttypes.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000033#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +000034#include <conio.h>
35#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000036#include <termios.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000037#include <unistd.h>
38#include <sys/types.h>
39#include <sys/ioctl.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000040#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000041#include "flash.h"
42#include "programmer.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000043
Stefan Taunere33c40e2013-04-13 00:29:30 +000044fdtype sp_fd = SER_INV_FD;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000045
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000046#if IS_WINDOWS
Stefan Taunerda5b17c2013-04-01 00:45:32 +000047struct baudentry {
48 DWORD flag;
49 unsigned int baud;
50};
51#define BAUDENTRY(baud) { CBR_##baud, baud },
52#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000053struct baudentry {
54 int flag;
55 unsigned int baud;
56};
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000057#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000058#endif
59
60/* I'd like if the C preprocessor could have directives in macros.
61 * See TERMIOS(3) and http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx and
62 * 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 +000063static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000064 BAUDENTRY(9600) /* unconditional default */
65#if defined(B19200) || defined(CBR_19200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000066 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000067#endif
68#if defined(B38400) || defined(CBR_38400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000069 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000070#endif
71#if defined(B57600) || defined(CBR_57600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000072 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000073#endif
74#if defined(B115200) || defined(CBR_115200)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000075 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000076#endif
77#if defined(B230400) || defined(CBR_230400)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000078 BAUDENTRY(230400)
79#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000080#if defined(B460800) || defined(CBR_460800)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000081 BAUDENTRY(460800)
82#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000083#if defined(B500000) || defined(CBR_500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000084 BAUDENTRY(500000)
85#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000086#if defined(B576000) || defined(CBR_576000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000087 BAUDENTRY(576000)
88#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000089#if defined(B921600) || defined(CBR_921600)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000090 BAUDENTRY(921600)
91#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000092#if defined(B1000000) || defined(CBR_1000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000093 BAUDENTRY(1000000)
94#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000095#if defined(B1152000) || defined(CBR_1152000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000096 BAUDENTRY(1152000)
97#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +000098#if defined(B1500000) || defined(CBR_1500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000099 BAUDENTRY(1500000)
100#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000101#if defined(B2000000) || defined(CBR_2000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000102 BAUDENTRY(2000000)
103#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000104#if defined(B2500000) || defined(CBR_2500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000105 BAUDENTRY(2500000)
106#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000107#if defined(B3000000) || defined(CBR_3000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000108 BAUDENTRY(3000000)
109#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000110#if defined(B3500000) || defined(CBR_3500000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000111 BAUDENTRY(3500000)
112#endif
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000113#if defined(B4000000) || defined(CBR_4000000)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000114 BAUDENTRY(4000000)
115#endif
116 {0, 0} /* Terminator */
117};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000118
119const struct baudentry *round_baud(unsigned int baud)
120{
121 int i;
122 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
123 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
124 if (sp_baudtable[i].baud == baud)
125 return &sp_baudtable[i];
126
127 if (sp_baudtable[i].baud < baud) {
128 msg_pinfo("Warning: given baudrate %d rounded down to %d.\n",
129 baud, sp_baudtable[i].baud);
130 return &sp_baudtable[i];
131 }
132 }
133 return &sp_baudtable[0];
134}
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000135
Stefan Taunerbf88be92013-04-01 00:45:08 +0000136/* Uses msg_perr to print the last system error.
137 * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
138 * strerror() or FormatMessage() and ending with a linebreak. */
139static void msg_perr_strerror(const char *msg)
140{
141 msg_perr("Error: %s", msg);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000142#if IS_WINDOWS
Stefan Taunerbf88be92013-04-01 00:45:08 +0000143 char *lpMsgBuf;
144 DWORD nErr = GetLastError();
145 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
146 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
147 msg_perr(lpMsgBuf);
148 /* At least some formatted messages contain a line break at the end. Make sure to always print one */
149 if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
150 msg_perr("\n");
151 LocalFree(lpMsgBuf);
152#else
153 msg_perr("%s\n", strerror(errno));
154#endif
155}
156
Stefan Tauner184c52c2013-08-23 21:51:32 +0000157int serialport_config(fdtype fd, unsigned int baud)
158{
159 if (fd == SER_INV_FD) {
160 msg_perr("%s: File descriptor is invalid.\n", __func__);
161 return 1;
162 }
163
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000164#if IS_WINDOWS
Stefan Tauner184c52c2013-08-23 21:51:32 +0000165 DCB dcb;
166 if (!GetCommState(fd, &dcb)) {
167 msg_perr_strerror("Could not fetch original serial port configuration: ");
168 return 1;
169 }
170 const struct baudentry *entry = round_baud(baud);
171 dcb.BaudRate = entry->flag;
172 dcb.ByteSize = 8;
173 dcb.Parity = NOPARITY;
174 dcb.StopBits = ONESTOPBIT;
175 if (!SetCommState(fd, &dcb)) {
176 msg_perr_strerror("Could not change serial port configuration: ");
177 return 1;
178 }
179 if (!GetCommState(fd, &dcb)) {
180 msg_perr_strerror("Could not fetch new serial port configuration: ");
181 return 1;
182 }
183 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
184#else
185 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000186 if (tcgetattr(fd, &observed) != 0) {
187 msg_perr_strerror("Could not fetch original serial port configuration: ");
188 return 1;
189 }
190 wanted = observed;
191 const struct baudentry *entry = round_baud(baud);
192 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
193 msg_perr_strerror("Could not set serial baud rate: ");
194 return 1;
195 }
196 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
197 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
198 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
199 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
200 wanted.c_oflag &= ~OPOST;
201 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
202 msg_perr_strerror("Could not change serial port configuration: ");
203 return 1;
204 }
205 if (tcgetattr(fd, &observed) != 0) {
206 msg_perr_strerror("Could not fetch new serial port configuration: ");
207 return 1;
208 }
209 if (observed.c_cflag != wanted.c_cflag ||
210 observed.c_lflag != wanted.c_lflag ||
211 observed.c_iflag != wanted.c_iflag ||
212 observed.c_oflag != wanted.c_oflag ||
213 cfgetispeed(&observed) != cfgetispeed(&wanted)) {
214 msg_perr("%s: Some requested options did not stick.\n", __func__);
215 return 1;
216 }
217 msg_pdbg("Baud rate is %d now.\n", entry->baud);
218#endif
219 return 0;
220}
221
Patrick Georgie48654c2010-01-06 22:14:39 +0000222fdtype sp_openserport(char *dev, unsigned int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000223{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000224 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000225#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000226 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000227 if ((strlen(dev) > 3) &&
228 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000229 (tolower((unsigned char)dev[1]) == 'o') &&
230 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000231 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000232 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000233 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000234 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000235 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000236 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000237 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000238 }
Uwe Hermann43959702010-03-13 17:28:29 +0000239 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
240 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000241 if (dev2 != dev)
242 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000243 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000244 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000245 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000246 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000247 if (serialport_config(fd, baud) != 0) {
248 CloseHandle(fd);
249 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000250 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000251 return fd;
252#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000253 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000254 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000255 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000256 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000257 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000258
259 /* Ensure that we use blocking I/O */
260 const int flags = fcntl(fd, F_GETFL);
261 if (flags == -1) {
262 msg_perr_strerror("Could not get serial port mode: ");
263 return SER_INV_FD;
264 }
265 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
266 msg_perr_strerror("Could not set serial port mode to blocking: ");
267 return SER_INV_FD;
268 }
269
Stefan Tauner184c52c2013-08-23 21:51:32 +0000270 if (serialport_config(fd, baud) != 0) {
271 close(fd);
272 return SER_INV_FD;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000273 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000274 return fd;
Patrick Georgie48654c2010-01-06 22:14:39 +0000275#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000276}
277
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000278void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000279#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000280 DWORD ctl;
281
282 if(pin == PIN_TXD) {
283 ctl = val ? SETBREAK: CLRBREAK;
284 }
285 else if(pin == PIN_DTR) {
286 ctl = val ? SETDTR: CLRDTR;
287 }
288 else {
289 ctl = val ? SETRTS: CLRRTS;
290 }
291 EscapeCommFunction(sp_fd, ctl);
292#else
293 int ctl, s;
294
295 if(pin == PIN_TXD) {
296 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
297 }
298 else {
299 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
300 ioctl(sp_fd, TIOCMGET, &ctl);
301
302 if (val) {
303 ctl |= s;
304 }
305 else {
306 ctl &= ~s;
307 }
308 ioctl(sp_fd, TIOCMSET, &ctl);
309 }
310#endif
311}
312
313int sp_get_pin(enum SP_PIN pin) {
314 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000315#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000316 DWORD ctl;
317
318 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
319 GetCommModemStatus(sp_fd, &ctl);
320#else
321 int ctl;
322 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
323 ioctl(sp_fd, TIOCMGET, &ctl);
324#endif
325
326 return ((ctl & s) ? 1 : 0);
327
328}
329
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000330void sp_flush_incoming(void)
331{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000332#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000333 PurgeComm(sp_fd, PURGE_RXCLEAR);
334#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000335 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000336 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000337#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000338 return;
339}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000340
David Hendricks8bb20212011-06-14 01:35:36 +0000341int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000342{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000343#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000344 CloseHandle(sp_fd);
345#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000346 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000347#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000348 return 0;
349}
350
Mark Marshallf20b7be2014-05-09 21:16:21 +0000351int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000352{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000353#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000354 DWORD tmp = 0;
355#else
356 ssize_t tmp = 0;
357#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000358 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000359
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000360 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000361#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000362 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
363#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000364 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000365#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000366 if (tmp == -1) {
367 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000368 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000369 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000370 if (!tmp) {
371 msg_pdbg2("Empty write\n");
372 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000373 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000374 if (empty_writes == 0) {
375 msg_perr("Serial port is unresponsive!\n");
376 return 1;
377 }
378 }
379 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000380 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000381 }
382
383 return 0;
384}
385
386int serialport_read(unsigned char *buf, unsigned int readcnt)
387{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000388#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000389 DWORD tmp = 0;
390#else
391 ssize_t tmp = 0;
392#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000393
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000394 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000395#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000396 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
397#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000398 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000399#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000400 if (tmp == -1) {
401 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000402 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000403 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000404 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000405 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000406 readcnt -= tmp;
407 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000408 }
409
410 return 0;
411}
Stefan Tauner00e16082013-04-01 00:45:38 +0000412
413/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
414 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
415 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
416int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
417{
418 int ret = 1;
419 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000420#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000421 DWORD rv;
422 COMMTIMEOUTS oldTimeout;
423 COMMTIMEOUTS newTimeout = {
424 .ReadIntervalTimeout = MAXDWORD,
425 .ReadTotalTimeoutMultiplier = 0,
426 .ReadTotalTimeoutConstant = 0,
427 .WriteTotalTimeoutMultiplier = 0,
428 .WriteTotalTimeoutConstant = 0
429 };
430 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
431 msg_perr_strerror("Could not get serial port timeout settings: ");
432 return -1;
433 }
434 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000435 msg_perr_strerror("Could not set serial port timeout settings: ");
436 return -1;
437 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000438#else
439 ssize_t rv;
440 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000441 if (flags == -1) {
442 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000443 return -1;
444 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000445 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
446 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
447 return -1;
448 }
449#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000450
451 int i;
452 int rd_bytes = 0;
453 for (i = 0; i < timeout; i++) {
454 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000455#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000456 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
457 msg_pspew("read %lu bytes\n", rv);
458#else
459 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
460 msg_pspew("read %zd bytes\n", rv);
461#endif
462 if ((rv == -1) && (errno != EAGAIN)) {
463 msg_perr_strerror("Serial port read error: ");
464 ret = -1;
465 break;
466 }
467 if (rv > 0)
468 rd_bytes += rv;
469 if (rd_bytes == readcnt) {
470 ret = 0;
471 break;
472 }
473 internal_delay(1000); /* 1ms units */
474 }
475 if (really_read != NULL)
476 *really_read = rd_bytes;
477
478 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000479#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000480 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000481 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000482 ret = -1;
483 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000484#else
485 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
486 msg_perr_strerror("Could not restore serial port mode to blocking: ");
487 ret = -1;
488 }
489#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000490 return ret;
491}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000492
493/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
494 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
495 * 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 +0000496int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000497{
498 int ret = 1;
499 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000500#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000501 DWORD rv;
502 COMMTIMEOUTS oldTimeout;
503 COMMTIMEOUTS newTimeout = {
504 .ReadIntervalTimeout = MAXDWORD,
505 .ReadTotalTimeoutMultiplier = 0,
506 .ReadTotalTimeoutConstant = 0,
507 .WriteTotalTimeoutMultiplier = 0,
508 .WriteTotalTimeoutConstant = 0
509 };
510 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
511 msg_perr_strerror("Could not get serial port timeout settings: ");
512 return -1;
513 }
514 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
515 msg_perr_strerror("Could not set serial port timeout settings: ");
516 return -1;
517 }
518#else
519 ssize_t rv;
520 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000521 if (flags == -1) {
522 msg_perr_strerror("Could not get serial port mode: ");
523 return -1;
524 }
525 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
526 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
527 return -1;
528 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000529#endif
530
531 int i;
532 int wr_bytes = 0;
533 for (i = 0; i < timeout; i++) {
534 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000535#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000536 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
537 msg_pspew("wrote %lu bytes\n", rv);
538#else
539 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
540 msg_pspew("wrote %zd bytes\n", rv);
541#endif
542 if ((rv == -1) && (errno != EAGAIN)) {
543 msg_perr_strerror("Serial port write error: ");
544 ret = -1;
545 break;
546 }
547 if (rv > 0) {
548 wr_bytes += rv;
549 if (wr_bytes == writecnt) {
550 msg_pspew("write successful\n");
551 ret = 0;
552 break;
553 }
554 }
555 internal_delay(1000); /* 1ms units */
556 }
557 if (really_wrote != NULL)
558 *really_wrote = wr_bytes;
559
560 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000561#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000562 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
563 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000564 return -1;
565 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000566#else
567 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
568 msg_perr_strerror("Could not restore serial port blocking behavior: ");
569 return -1;
570 }
571#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000572 return ret;
573}