blob: e61df74f11081dfc2f1916f2a62fec93bff49878 [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.
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000016 */
17
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000018#include "platform.h"
19
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000020#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000023#include <string.h>
24#include <ctype.h>
25#include <fcntl.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000026#include <sys/stat.h>
27#include <errno.h>
28#include <inttypes.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000029#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +000030#include <conio.h>
31#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000032#include <termios.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000033#include <unistd.h>
34#include <sys/types.h>
35#include <sys/ioctl.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000036#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000037#include "flash.h"
38#include "programmer.h"
Urja Rannikko615ba182017-06-15 15:28:27 +030039#include "custom_baud.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000040
Stefan Taunere33c40e2013-04-13 00:29:30 +000041fdtype sp_fd = SER_INV_FD;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000042
Urja Rannikkodc445842016-01-04 05:08:40 +000043/* There is no way defined by POSIX to use arbitrary baud rates. It only defines some macros that can be used to
44 * specify respective baud rates and many implementations extend this list with further macros, cf. TERMIOS(3)
45 * and http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h
46 * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal
47 * with numerical user input.
48 *
Urja Rannikko615ba182017-06-15 15:28:27 +030049 * On Linux there is a non-standard way to use arbitrary baud rates that we use if there is no
50 * matching standard rate, see custom_baud.c
Urja Rannikkodc445842016-01-04 05:08:40 +000051 *
52 * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for
53 * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see
54 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
55 */
56#if !IS_WINDOWS
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000057#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000058
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000059static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000060 BAUDENTRY(9600) /* unconditional default */
Urja Rannikkodc445842016-01-04 05:08:40 +000061#ifdef B19200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000062 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000063#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000064#ifdef B38400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000065 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000066#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000067#ifdef B57600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000068 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000069#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000070#ifdef B115200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000071 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000072#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000073#ifdef B230400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000074 BAUDENTRY(230400)
75#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000076#ifdef B460800
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000077 BAUDENTRY(460800)
78#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000079#ifdef B500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000080 BAUDENTRY(500000)
81#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000082#ifdef B576000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000083 BAUDENTRY(576000)
84#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000085#ifdef B921600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000086 BAUDENTRY(921600)
87#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000088#ifdef B1000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000089 BAUDENTRY(1000000)
90#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000091#ifdef B1152000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000092 BAUDENTRY(1152000)
93#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000094#ifdef B1500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000095 BAUDENTRY(1500000)
96#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000097#ifdef B2000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000098 BAUDENTRY(2000000)
99#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000100#ifdef B2500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000101 BAUDENTRY(2500000)
102#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000103#ifdef B3000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000104 BAUDENTRY(3000000)
105#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000106#ifdef B3500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000107 BAUDENTRY(3500000)
108#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000109#ifdef B4000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000110 BAUDENTRY(4000000)
111#endif
112 {0, 0} /* Terminator */
113};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000114
Stefan Tauner72587f82016-01-04 03:05:15 +0000115static const struct baudentry *round_baud(unsigned int baud)
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000116{
117 int i;
118 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
119 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
120 if (sp_baudtable[i].baud == baud)
121 return &sp_baudtable[i];
122
123 if (sp_baudtable[i].baud < baud) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000124 msg_pwarn("Warning: given baudrate %d rounded down to %d.\n",
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000125 baud, sp_baudtable[i].baud);
126 return &sp_baudtable[i];
127 }
128 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000129 msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud);
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000130 return &sp_baudtable[0];
131}
Urja Rannikkodc445842016-01-04 05:08:40 +0000132#endif
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);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000140#if IS_WINDOWS
Stefan Taunerbf88be92013-04-01 00:45:08 +0000141 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 Tauner72587f82016-01-04 03:05:15 +0000155int serialport_config(fdtype fd, int baud)
Stefan Tauner184c52c2013-08-23 21:51:32 +0000156{
157 if (fd == SER_INV_FD) {
158 msg_perr("%s: File descriptor is invalid.\n", __func__);
159 return 1;
160 }
161
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000162#if IS_WINDOWS
Stefan Tauner184c52c2013-08-23 21:51:32 +0000163 DCB dcb;
164 if (!GetCommState(fd, &dcb)) {
165 msg_perr_strerror("Could not fetch original serial port configuration: ");
166 return 1;
167 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000168 if (baud >= 0) {
Urja Rannikkodc445842016-01-04 05:08:40 +0000169 dcb.BaudRate = baud;
Stefan Tauner72587f82016-01-04 03:05:15 +0000170 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000171 dcb.ByteSize = 8;
172 dcb.Parity = NOPARITY;
173 dcb.StopBits = ONESTOPBIT;
174 if (!SetCommState(fd, &dcb)) {
175 msg_perr_strerror("Could not change serial port configuration: ");
176 return 1;
177 }
178 if (!GetCommState(fd, &dcb)) {
179 msg_perr_strerror("Could not fetch new serial port configuration: ");
180 return 1;
181 }
182 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
183#else
184 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000185 if (tcgetattr(fd, &observed) != 0) {
186 msg_perr_strerror("Could not fetch original serial port configuration: ");
187 return 1;
188 }
189 wanted = observed;
Stefan Tauner72587f82016-01-04 03:05:15 +0000190 if (baud >= 0) {
Urja Rannikko615ba182017-06-15 15:28:27 +0300191 if (use_custom_baud(baud, sp_baudtable)) {
192 if (set_custom_baudrate(fd, baud)) {
193 msg_perr_strerror("Could not set custom baudrate: ");
194 return 1;
195 }
196 /* We want whatever the termios looks like now, so the rest of the
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200197 setup doesn't mess up the custom rate. */
Urja Rannikko615ba182017-06-15 15:28:27 +0300198 if (tcgetattr(fd, &wanted) != 0) {
199 /* This should pretty much never happen (see above), but.. */
200 msg_perr_strerror("Could not fetch serial port configuration: ");
201 return 1;
202 }
203 msg_pdbg("Using custom baud rate.\n");
204 } else {
205 const struct baudentry *entry = round_baud(baud);
206 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
207 msg_perr_strerror("Could not set serial baud rate: ");
208 return 1;
209 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000210 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000211 }
212 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
213 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
Michael Zhilin2ec33f92016-12-02 14:41:26 +0000214 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);
Stefan Tauner184c52c2013-08-23 21:51:32 +0000215 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
216 wanted.c_oflag &= ~OPOST;
217 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
218 msg_perr_strerror("Could not change serial port configuration: ");
219 return 1;
220 }
221 if (tcgetattr(fd, &observed) != 0) {
222 msg_perr_strerror("Could not fetch new serial port configuration: ");
223 return 1;
224 }
225 if (observed.c_cflag != wanted.c_cflag ||
226 observed.c_lflag != wanted.c_lflag ||
227 observed.c_iflag != wanted.c_iflag ||
Stefan Tauner631bb022016-01-04 03:05:06 +0000228 observed.c_oflag != wanted.c_oflag) {
229 msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
230 msg_pdbg(" observed wanted\n"
231 "c_cflag: 0x%08lX 0x%08lX\n"
232 "c_lflag: 0x%08lX 0x%08lX\n"
233 "c_iflag: 0x%08lX 0x%08lX\n"
234 "c_oflag: 0x%08lX 0x%08lX\n",
235 (long)observed.c_cflag, (long)wanted.c_cflag,
236 (long)observed.c_lflag, (long)wanted.c_lflag,
237 (long)observed.c_iflag, (long)wanted.c_iflag,
238 (long)observed.c_oflag, (long)wanted.c_oflag
239 );
Stefan Tauner184c52c2013-08-23 21:51:32 +0000240 }
Stefan Tauner631bb022016-01-04 03:05:06 +0000241 if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
242 cfgetospeed(&observed) != cfgetospeed(&wanted)) {
243 msg_pwarn("Could not set baud rates exactly.\n");
244 msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
245 (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
246 }
Urja Rannikkodc445842016-01-04 05:08:40 +0000247 // FIXME: display actual baud rate - at least if none was specified by the user.
Stefan Tauner184c52c2013-08-23 21:51:32 +0000248#endif
249 return 0;
250}
251
Stefan Tauner72587f82016-01-04 03:05:15 +0000252fdtype sp_openserport(char *dev, int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000253{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000254 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000255#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000256 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000257 if ((strlen(dev) > 3) &&
258 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000259 (tolower((unsigned char)dev[1]) == 'o') &&
260 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000261 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000262 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000263 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000264 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000265 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000266 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000267 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000268 }
Uwe Hermann43959702010-03-13 17:28:29 +0000269 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
270 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000271 if (dev2 != dev)
272 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000273 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000274 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000275 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000276 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000277 if (serialport_config(fd, baud) != 0) {
278 CloseHandle(fd);
279 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000280 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000281 return fd;
282#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000283 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000284 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000285 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000286 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000287 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000288
289 /* Ensure that we use blocking I/O */
290 const int flags = fcntl(fd, F_GETFL);
291 if (flags == -1) {
292 msg_perr_strerror("Could not get serial port mode: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000293 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000294 }
295 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
296 msg_perr_strerror("Could not set serial port mode to blocking: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000297 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000298 }
299
Stefan Tauner184c52c2013-08-23 21:51:32 +0000300 if (serialport_config(fd, baud) != 0) {
Stefan Taunera3712812016-01-16 18:50:27 +0000301 goto err;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000302 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000303 return fd;
Stefan Taunera3712812016-01-16 18:50:27 +0000304err:
305 close(fd);
306 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000307#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000308}
309
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000310void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000311#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000312 DWORD ctl;
313
314 if(pin == PIN_TXD) {
315 ctl = val ? SETBREAK: CLRBREAK;
316 }
317 else if(pin == PIN_DTR) {
318 ctl = val ? SETDTR: CLRDTR;
319 }
320 else {
321 ctl = val ? SETRTS: CLRRTS;
322 }
323 EscapeCommFunction(sp_fd, ctl);
324#else
325 int ctl, s;
326
327 if(pin == PIN_TXD) {
328 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
329 }
330 else {
331 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
332 ioctl(sp_fd, TIOCMGET, &ctl);
333
334 if (val) {
335 ctl |= s;
336 }
337 else {
338 ctl &= ~s;
339 }
340 ioctl(sp_fd, TIOCMSET, &ctl);
341 }
342#endif
343}
344
345int sp_get_pin(enum SP_PIN pin) {
346 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000347#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000348 DWORD ctl;
349
350 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
351 GetCommModemStatus(sp_fd, &ctl);
352#else
353 int ctl;
354 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
355 ioctl(sp_fd, TIOCMGET, &ctl);
356#endif
357
358 return ((ctl & s) ? 1 : 0);
359
360}
361
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000362void sp_flush_incoming(void)
363{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000364#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000365 PurgeComm(sp_fd, PURGE_RXCLEAR);
366#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000367 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000368 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000369#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000370 return;
371}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000372
David Hendricks8bb20212011-06-14 01:35:36 +0000373int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000374{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000375#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000376 CloseHandle(sp_fd);
377#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000378 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000379#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000380 return 0;
381}
382
Mark Marshallf20b7be2014-05-09 21:16:21 +0000383int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000384{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000385#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000386 DWORD tmp = 0;
387#else
388 ssize_t tmp = 0;
389#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000390 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000391
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000392 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000393#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000394 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
395#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000396 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000397#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000398 if (tmp == -1) {
399 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000400 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000401 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000402 if (!tmp) {
403 msg_pdbg2("Empty write\n");
404 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000405 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000406 if (empty_writes == 0) {
407 msg_perr("Serial port is unresponsive!\n");
408 return 1;
409 }
410 }
411 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000412 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000413 }
414
415 return 0;
416}
417
418int serialport_read(unsigned char *buf, unsigned int readcnt)
419{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000420#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000421 DWORD tmp = 0;
422#else
423 ssize_t tmp = 0;
424#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000425
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000426 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000427#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000428 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
429#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000430 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000431#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000432 if (tmp == -1) {
433 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000434 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000435 }
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000436 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000437 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000438 readcnt -= tmp;
439 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000440 }
441
442 return 0;
443}
Stefan Tauner00e16082013-04-01 00:45:38 +0000444
445/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
446 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
447 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
448int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
449{
450 int ret = 1;
451 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000452#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000453 DWORD rv;
454 COMMTIMEOUTS oldTimeout;
455 COMMTIMEOUTS newTimeout = {
456 .ReadIntervalTimeout = MAXDWORD,
457 .ReadTotalTimeoutMultiplier = 0,
458 .ReadTotalTimeoutConstant = 0,
459 .WriteTotalTimeoutMultiplier = 0,
460 .WriteTotalTimeoutConstant = 0
461 };
462 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
463 msg_perr_strerror("Could not get serial port timeout settings: ");
464 return -1;
465 }
466 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000467 msg_perr_strerror("Could not set serial port timeout settings: ");
468 return -1;
469 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000470#else
471 ssize_t rv;
472 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000473 if (flags == -1) {
474 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000475 return -1;
476 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000477 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
478 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
479 return -1;
480 }
481#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000482
483 int i;
484 int rd_bytes = 0;
485 for (i = 0; i < timeout; i++) {
486 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000487#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000488 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
489 msg_pspew("read %lu bytes\n", rv);
490#else
491 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
492 msg_pspew("read %zd bytes\n", rv);
493#endif
494 if ((rv == -1) && (errno != EAGAIN)) {
495 msg_perr_strerror("Serial port read error: ");
496 ret = -1;
497 break;
498 }
499 if (rv > 0)
500 rd_bytes += rv;
501 if (rd_bytes == readcnt) {
502 ret = 0;
503 break;
504 }
505 internal_delay(1000); /* 1ms units */
506 }
507 if (really_read != NULL)
508 *really_read = rd_bytes;
509
510 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000511#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000512 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000513 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000514 ret = -1;
515 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000516#else
517 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
518 msg_perr_strerror("Could not restore serial port mode to blocking: ");
519 ret = -1;
520 }
521#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000522 return ret;
523}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000524
525/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
526 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
527 * 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 +0000528int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000529{
530 int ret = 1;
531 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000532#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000533 DWORD rv;
534 COMMTIMEOUTS oldTimeout;
535 COMMTIMEOUTS newTimeout = {
536 .ReadIntervalTimeout = MAXDWORD,
537 .ReadTotalTimeoutMultiplier = 0,
538 .ReadTotalTimeoutConstant = 0,
539 .WriteTotalTimeoutMultiplier = 0,
540 .WriteTotalTimeoutConstant = 0
541 };
542 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
543 msg_perr_strerror("Could not get serial port timeout settings: ");
544 return -1;
545 }
546 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
547 msg_perr_strerror("Could not set serial port timeout settings: ");
548 return -1;
549 }
550#else
551 ssize_t rv;
552 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000553 if (flags == -1) {
554 msg_perr_strerror("Could not get serial port mode: ");
555 return -1;
556 }
557 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
558 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
559 return -1;
560 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000561#endif
562
563 int i;
564 int wr_bytes = 0;
565 for (i = 0; i < timeout; i++) {
566 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000567#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000568 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
569 msg_pspew("wrote %lu bytes\n", rv);
570#else
571 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
572 msg_pspew("wrote %zd bytes\n", rv);
573#endif
574 if ((rv == -1) && (errno != EAGAIN)) {
575 msg_perr_strerror("Serial port write error: ");
576 ret = -1;
577 break;
578 }
579 if (rv > 0) {
580 wr_bytes += rv;
581 if (wr_bytes == writecnt) {
582 msg_pspew("write successful\n");
583 ret = 0;
584 break;
585 }
586 }
587 internal_delay(1000); /* 1ms units */
588 }
589 if (really_wrote != NULL)
590 *really_wrote = wr_bytes;
591
592 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000593#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000594 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
595 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000596 return -1;
597 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000598#else
599 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
600 msg_perr_strerror("Could not restore serial port blocking behavior: ");
601 return -1;
602 }
603#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000604 return ret;
605}