blob: 76d34a2636cbce47fd58110d6f8a9e34feee93d4 [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
David Hendricks7a6bce62020-07-02 09:36:50 -0700394 if (!WriteFile(sp_fd, buf, writecnt, &tmp, NULL)) {
395 msg_perr("Serial port write error!\n");
396 return 1;
397 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000398#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000399 tmp = write(sp_fd, buf, writecnt);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000400 if (tmp == -1) {
401 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000402 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000403 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700404#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000405 if (!tmp) {
406 msg_pdbg2("Empty write\n");
407 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000408 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000409 if (empty_writes == 0) {
410 msg_perr("Serial port is unresponsive!\n");
411 return 1;
412 }
413 }
414 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000415 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000416 }
417
418 return 0;
419}
420
421int serialport_read(unsigned char *buf, unsigned int readcnt)
422{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000423#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000424 DWORD tmp = 0;
425#else
426 ssize_t tmp = 0;
427#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000428
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000429 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000430#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700431 if (!ReadFile(sp_fd, buf, readcnt, &tmp, NULL)) {
432 msg_perr("Serial port read error!\n");
433 return 1;
434 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000435#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000436 tmp = read(sp_fd, buf, readcnt);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000437 if (tmp == -1) {
438 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000439 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000440 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700441#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000442 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000443 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000444 readcnt -= tmp;
445 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000446 }
447
448 return 0;
449}
Stefan Tauner00e16082013-04-01 00:45:38 +0000450
451/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
452 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
453 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
454int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
455{
456 int ret = 1;
457 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000458#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000459 DWORD rv;
460 COMMTIMEOUTS oldTimeout;
461 COMMTIMEOUTS newTimeout = {
462 .ReadIntervalTimeout = MAXDWORD,
463 .ReadTotalTimeoutMultiplier = 0,
464 .ReadTotalTimeoutConstant = 0,
465 .WriteTotalTimeoutMultiplier = 0,
466 .WriteTotalTimeoutConstant = 0
467 };
468 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
469 msg_perr_strerror("Could not get serial port timeout settings: ");
470 return -1;
471 }
472 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000473 msg_perr_strerror("Could not set serial port timeout settings: ");
474 return -1;
475 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000476#else
477 ssize_t rv;
478 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000479 if (flags == -1) {
480 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000481 return -1;
482 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000483 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
484 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
485 return -1;
486 }
487#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000488
Nico Huber519be662018-12-23 20:03:35 +0100489 unsigned int i;
490 unsigned int rd_bytes = 0;
Stefan Tauner00e16082013-04-01 00:45:38 +0000491 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100492 msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000493#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700494 if (!ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL)) {
495 msg_perr_strerror("Serial port read error: ");
496 ret = -1;
497 break;
498 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000499 msg_pspew("read %lu bytes\n", rv);
500#else
501 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
502 msg_pspew("read %zd bytes\n", rv);
Stefan Tauner00e16082013-04-01 00:45:38 +0000503 if ((rv == -1) && (errno != EAGAIN)) {
504 msg_perr_strerror("Serial port read error: ");
505 ret = -1;
506 break;
507 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700508#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000509 if (rv > 0)
510 rd_bytes += rv;
511 if (rd_bytes == readcnt) {
512 ret = 0;
513 break;
514 }
515 internal_delay(1000); /* 1ms units */
516 }
517 if (really_read != NULL)
518 *really_read = rd_bytes;
519
520 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000521#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000522 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000523 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000524 ret = -1;
525 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000526#else
527 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
528 msg_perr_strerror("Could not restore serial port mode to blocking: ");
529 ret = -1;
530 }
531#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000532 return ret;
533}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000534
535/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
536 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
537 * 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 +0000538int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000539{
540 int ret = 1;
541 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000542#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000543 DWORD rv;
544 COMMTIMEOUTS oldTimeout;
545 COMMTIMEOUTS newTimeout = {
546 .ReadIntervalTimeout = MAXDWORD,
547 .ReadTotalTimeoutMultiplier = 0,
548 .ReadTotalTimeoutConstant = 0,
549 .WriteTotalTimeoutMultiplier = 0,
550 .WriteTotalTimeoutConstant = 0
551 };
552 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
553 msg_perr_strerror("Could not get serial port timeout settings: ");
554 return -1;
555 }
556 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
557 msg_perr_strerror("Could not set serial port timeout settings: ");
558 return -1;
559 }
560#else
561 ssize_t rv;
562 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000563 if (flags == -1) {
564 msg_perr_strerror("Could not get serial port mode: ");
565 return -1;
566 }
567 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
568 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
569 return -1;
570 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000571#endif
572
Nico Huber519be662018-12-23 20:03:35 +0100573 unsigned int i;
574 unsigned int wr_bytes = 0;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000575 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100576 msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000577#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700578 if (!WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL)) {
579 msg_perr_strerror("Serial port write error: ");
580 ret = -1;
581 break;
582 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000583 msg_pspew("wrote %lu bytes\n", rv);
584#else
585 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
586 msg_pspew("wrote %zd bytes\n", rv);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000587 if ((rv == -1) && (errno != EAGAIN)) {
588 msg_perr_strerror("Serial port write error: ");
589 ret = -1;
590 break;
591 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700592#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000593 if (rv > 0) {
594 wr_bytes += rv;
595 if (wr_bytes == writecnt) {
596 msg_pspew("write successful\n");
597 ret = 0;
598 break;
599 }
600 }
601 internal_delay(1000); /* 1ms units */
602 }
603 if (really_wrote != NULL)
604 *really_wrote = wr_bytes;
605
606 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000607#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000608 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
609 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000610 return -1;
611 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000612#else
613 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
614 msg_perr_strerror("Could not restore serial port blocking behavior: ");
615 return -1;
616 }
617#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000618 return ret;
619}