blob: d3a7e931ae291abbe6a5e55e5050ae6bf6f15a80 [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
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000021#include <string.h>
22#include <ctype.h>
23#include <fcntl.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000024#include <sys/stat.h>
25#include <errno.h>
26#include <inttypes.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000027#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +000028#include <conio.h>
29#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000030#include <termios.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000031#include <unistd.h>
32#include <sys/types.h>
33#include <sys/ioctl.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000034#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000035#include "flash.h"
36#include "programmer.h"
Urja Rannikko615ba182017-06-15 15:28:27 +030037#include "custom_baud.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000038
Stefan Taunere33c40e2013-04-13 00:29:30 +000039fdtype sp_fd = SER_INV_FD;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000040
Urja Rannikkodc445842016-01-04 05:08:40 +000041/* There is no way defined by POSIX to use arbitrary baud rates. It only defines some macros that can be used to
42 * specify respective baud rates and many implementations extend this list with further macros, cf. TERMIOS(3)
43 * and http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h
44 * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal
45 * with numerical user input.
46 *
Urja Rannikko615ba182017-06-15 15:28:27 +030047 * On Linux there is a non-standard way to use arbitrary baud rates that we use if there is no
48 * matching standard rate, see custom_baud.c
Urja Rannikkodc445842016-01-04 05:08:40 +000049 *
Peter Stugeb8ee2d62022-12-11 16:20:16 +010050 * On Darwin there is also a non-standard ioctl() to set arbitrary baud rates
51 * and any above 230400, see custom_baud_darwin.c and
52 * https://opensource.apple.com/source/IOSerialFamily/IOSerialFamily-91/tests/IOSerialTestLib.c.auto.html
53 *
Urja Rannikkodc445842016-01-04 05:08:40 +000054 * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for
55 * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see
56 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
57 */
58#if !IS_WINDOWS
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000059#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000060
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000061static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000062 BAUDENTRY(9600) /* unconditional default */
Urja Rannikkodc445842016-01-04 05:08:40 +000063#ifdef B19200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000064 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000065#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000066#ifdef B38400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000067 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000068#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000069#ifdef B57600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000070 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000071#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000072#ifdef B115200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000073 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000074#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000075#ifdef B230400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000076 BAUDENTRY(230400)
77#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000078#ifdef B460800
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000079 BAUDENTRY(460800)
80#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000081#ifdef B500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000082 BAUDENTRY(500000)
83#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000084#ifdef B576000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000085 BAUDENTRY(576000)
86#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000087#ifdef B921600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000088 BAUDENTRY(921600)
89#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000090#ifdef B1000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000091 BAUDENTRY(1000000)
92#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000093#ifdef B1152000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000094 BAUDENTRY(1152000)
95#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000096#ifdef B1500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000097 BAUDENTRY(1500000)
98#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000099#ifdef B2000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000100 BAUDENTRY(2000000)
101#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000102#ifdef B2500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000103 BAUDENTRY(2500000)
104#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000105#ifdef B3000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000106 BAUDENTRY(3000000)
107#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000108#ifdef B3500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000109 BAUDENTRY(3500000)
110#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000111#ifdef B4000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000112 BAUDENTRY(4000000)
113#endif
114 {0, 0} /* Terminator */
115};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000116
Stefan Tauner72587f82016-01-04 03:05:15 +0000117static const struct baudentry *round_baud(unsigned int baud)
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000118{
119 int i;
120 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
121 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
122 if (sp_baudtable[i].baud == baud)
123 return &sp_baudtable[i];
124
125 if (sp_baudtable[i].baud < baud) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000126 msg_pwarn("Warning: given baudrate %d rounded down to %d.\n",
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000127 baud, sp_baudtable[i].baud);
128 return &sp_baudtable[i];
129 }
130 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000131 msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud);
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000132 return &sp_baudtable[0];
133}
Urja Rannikkodc445842016-01-04 05:08:40 +0000134#endif
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 Tauner72587f82016-01-04 03:05:15 +0000157int serialport_config(fdtype fd, int baud)
Stefan Tauner184c52c2013-08-23 21:51:32 +0000158{
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 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000170 if (baud >= 0) {
Urja Rannikkodc445842016-01-04 05:08:40 +0000171 dcb.BaudRate = baud;
Stefan Tauner72587f82016-01-04 03:05:15 +0000172 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000173 dcb.ByteSize = 8;
174 dcb.Parity = NOPARITY;
175 dcb.StopBits = ONESTOPBIT;
176 if (!SetCommState(fd, &dcb)) {
177 msg_perr_strerror("Could not change serial port configuration: ");
178 return 1;
179 }
180 if (!GetCommState(fd, &dcb)) {
181 msg_perr_strerror("Could not fetch new serial port configuration: ");
182 return 1;
183 }
184 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
185#else
Peter Stugeb078ec62022-12-11 04:02:10 +0100186 int custom_baud = (baud >= 0 && use_custom_baud(baud, sp_baudtable));
Stefan Tauner184c52c2013-08-23 21:51:32 +0000187 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000188 if (tcgetattr(fd, &observed) != 0) {
189 msg_perr_strerror("Could not fetch original serial port configuration: ");
190 return 1;
191 }
192 wanted = observed;
Stefan Tauner72587f82016-01-04 03:05:15 +0000193 if (baud >= 0) {
Peter Stugeb078ec62022-12-11 04:02:10 +0100194 if (custom_baud) {
195 if (set_custom_baudrate(fd, baud, BEFORE_FLAGS, NULL)) {
Urja Rannikko615ba182017-06-15 15:28:27 +0300196 msg_perr_strerror("Could not set custom baudrate: ");
197 return 1;
198 }
199 /* We want whatever the termios looks like now, so the rest of the
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200200 setup doesn't mess up the custom rate. */
Urja Rannikko615ba182017-06-15 15:28:27 +0300201 if (tcgetattr(fd, &wanted) != 0) {
202 /* This should pretty much never happen (see above), but.. */
203 msg_perr_strerror("Could not fetch serial port configuration: ");
204 return 1;
205 }
Urja Rannikko615ba182017-06-15 15:28:27 +0300206 } else {
207 const struct baudentry *entry = round_baud(baud);
208 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
209 msg_perr_strerror("Could not set serial baud rate: ");
210 return 1;
211 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000212 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000213 }
214 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
215 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
Michael Zhilin2ec33f92016-12-02 14:41:26 +0000216 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);
Stefan Tauner184c52c2013-08-23 21:51:32 +0000217 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
218 wanted.c_oflag &= ~OPOST;
Peter Stugeb078ec62022-12-11 04:02:10 +0100219 if (custom_baud && set_custom_baudrate(fd, baud, WITH_FLAGS, &wanted)) {
220 msg_perr_strerror("Could not set custom baudrate: ");
221 return 1;
222 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000223 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
224 msg_perr_strerror("Could not change serial port configuration: ");
225 return 1;
226 }
227 if (tcgetattr(fd, &observed) != 0) {
228 msg_perr_strerror("Could not fetch new serial port configuration: ");
229 return 1;
230 }
231 if (observed.c_cflag != wanted.c_cflag ||
232 observed.c_lflag != wanted.c_lflag ||
233 observed.c_iflag != wanted.c_iflag ||
Stefan Tauner631bb022016-01-04 03:05:06 +0000234 observed.c_oflag != wanted.c_oflag) {
235 msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
236 msg_pdbg(" observed wanted\n"
237 "c_cflag: 0x%08lX 0x%08lX\n"
238 "c_lflag: 0x%08lX 0x%08lX\n"
239 "c_iflag: 0x%08lX 0x%08lX\n"
240 "c_oflag: 0x%08lX 0x%08lX\n",
241 (long)observed.c_cflag, (long)wanted.c_cflag,
242 (long)observed.c_lflag, (long)wanted.c_lflag,
243 (long)observed.c_iflag, (long)wanted.c_iflag,
244 (long)observed.c_oflag, (long)wanted.c_oflag
245 );
Stefan Tauner184c52c2013-08-23 21:51:32 +0000246 }
Peter Stugeb078ec62022-12-11 04:02:10 +0100247 if (custom_baud) {
248 if (set_custom_baudrate(fd, baud, AFTER_FLAGS, &wanted)) {
249 msg_perr_strerror("Could not set custom baudrate: ");
250 return 1;
251 }
252 msg_pdbg("Using custom baud rate.\n");
253 }
Stefan Tauner631bb022016-01-04 03:05:06 +0000254 if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
255 cfgetospeed(&observed) != cfgetospeed(&wanted)) {
256 msg_pwarn("Could not set baud rates exactly.\n");
257 msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
258 (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
259 }
Urja Rannikkodc445842016-01-04 05:08:40 +0000260 // FIXME: display actual baud rate - at least if none was specified by the user.
Stefan Tauner184c52c2013-08-23 21:51:32 +0000261#endif
262 return 0;
263}
264
Stefan Tauner72587f82016-01-04 03:05:15 +0000265fdtype sp_openserport(char *dev, int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000266{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000267 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000268#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000269 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000270 if ((strlen(dev) > 3) &&
271 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000272 (tolower((unsigned char)dev[1]) == 'o') &&
273 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000274 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000275 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000276 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000277 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000278 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000279 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000280 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000281 }
Uwe Hermann43959702010-03-13 17:28:29 +0000282 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
283 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000284 if (dev2 != dev)
285 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000286 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000287 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000288 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000289 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000290 if (serialport_config(fd, baud) != 0) {
291 CloseHandle(fd);
292 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000293 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000294 return fd;
295#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000296 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000297 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000298 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000299 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000300 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000301
302 /* Ensure that we use blocking I/O */
303 const int flags = fcntl(fd, F_GETFL);
304 if (flags == -1) {
305 msg_perr_strerror("Could not get serial port mode: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000306 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000307 }
308 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
309 msg_perr_strerror("Could not set serial port mode to blocking: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000310 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000311 }
312
Stefan Tauner184c52c2013-08-23 21:51:32 +0000313 if (serialport_config(fd, baud) != 0) {
Stefan Taunera3712812016-01-16 18:50:27 +0000314 goto err;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000315 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000316 return fd;
Stefan Taunera3712812016-01-16 18:50:27 +0000317err:
318 close(fd);
319 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000320#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000321}
322
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000323void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000324#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000325 DWORD ctl;
326
327 if(pin == PIN_TXD) {
328 ctl = val ? SETBREAK: CLRBREAK;
329 }
330 else if(pin == PIN_DTR) {
331 ctl = val ? SETDTR: CLRDTR;
332 }
333 else {
334 ctl = val ? SETRTS: CLRRTS;
335 }
336 EscapeCommFunction(sp_fd, ctl);
337#else
338 int ctl, s;
339
340 if(pin == PIN_TXD) {
341 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
342 }
343 else {
344 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
345 ioctl(sp_fd, TIOCMGET, &ctl);
346
347 if (val) {
348 ctl |= s;
349 }
350 else {
351 ctl &= ~s;
352 }
353 ioctl(sp_fd, TIOCMSET, &ctl);
354 }
355#endif
356}
357
358int sp_get_pin(enum SP_PIN pin) {
359 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000360#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000361 DWORD ctl;
362
363 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
364 GetCommModemStatus(sp_fd, &ctl);
365#else
366 int ctl;
367 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
368 ioctl(sp_fd, TIOCMGET, &ctl);
369#endif
370
371 return ((ctl & s) ? 1 : 0);
372
373}
374
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000375void sp_flush_incoming(void)
376{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000377#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000378 PurgeComm(sp_fd, PURGE_RXCLEAR);
379#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000380 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000381 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000382#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000383 return;
384}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000385
David Hendricks8bb20212011-06-14 01:35:36 +0000386int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000387{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000388#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000389 CloseHandle(sp_fd);
390#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000391 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000392#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000393 return 0;
394}
395
Mark Marshallf20b7be2014-05-09 21:16:21 +0000396int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000397{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000398#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000399 DWORD tmp = 0;
400#else
401 ssize_t tmp = 0;
402#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000403 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000404
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000405 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000406#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700407 if (!WriteFile(sp_fd, buf, writecnt, &tmp, NULL)) {
408 msg_perr("Serial port write error!\n");
409 return 1;
410 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000411#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000412 tmp = write(sp_fd, buf, writecnt);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000413 if (tmp == -1) {
414 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000415 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000416 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700417#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000418 if (!tmp) {
419 msg_pdbg2("Empty write\n");
420 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000421 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000422 if (empty_writes == 0) {
423 msg_perr("Serial port is unresponsive!\n");
424 return 1;
425 }
426 }
427 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000428 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000429 }
430
431 return 0;
432}
433
434int serialport_read(unsigned char *buf, unsigned int readcnt)
435{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000436#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000437 DWORD tmp = 0;
438#else
439 ssize_t tmp = 0;
440#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000441
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000442 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000443#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700444 if (!ReadFile(sp_fd, buf, readcnt, &tmp, NULL)) {
445 msg_perr("Serial port read error!\n");
446 return 1;
447 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000448#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000449 tmp = read(sp_fd, buf, readcnt);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000450 if (tmp == -1) {
451 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000452 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000453 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700454#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000455 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000456 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000457 readcnt -= tmp;
458 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000459 }
460
461 return 0;
462}
Stefan Tauner00e16082013-04-01 00:45:38 +0000463
464/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
465 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
466 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
467int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
468{
469 int ret = 1;
470 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000471#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000472 DWORD rv;
473 COMMTIMEOUTS oldTimeout;
474 COMMTIMEOUTS newTimeout = {
475 .ReadIntervalTimeout = MAXDWORD,
476 .ReadTotalTimeoutMultiplier = 0,
477 .ReadTotalTimeoutConstant = 0,
478 .WriteTotalTimeoutMultiplier = 0,
479 .WriteTotalTimeoutConstant = 0
480 };
481 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
482 msg_perr_strerror("Could not get serial port timeout settings: ");
483 return -1;
484 }
485 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000486 msg_perr_strerror("Could not set serial port timeout settings: ");
487 return -1;
488 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000489#else
490 ssize_t rv;
491 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000492 if (flags == -1) {
493 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000494 return -1;
495 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000496 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
497 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
498 return -1;
499 }
500#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000501
Nico Huber519be662018-12-23 20:03:35 +0100502 unsigned int i;
503 unsigned int rd_bytes = 0;
Stefan Tauner00e16082013-04-01 00:45:38 +0000504 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100505 msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000506#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700507 if (!ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL)) {
508 msg_perr_strerror("Serial port read error: ");
509 ret = -1;
510 break;
511 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000512 msg_pspew("read %lu bytes\n", rv);
513#else
514 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
515 msg_pspew("read %zd bytes\n", rv);
Stefan Tauner00e16082013-04-01 00:45:38 +0000516 if ((rv == -1) && (errno != EAGAIN)) {
517 msg_perr_strerror("Serial port read error: ");
518 ret = -1;
519 break;
520 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700521#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000522 if (rv > 0)
523 rd_bytes += rv;
524 if (rd_bytes == readcnt) {
525 ret = 0;
526 break;
527 }
528 internal_delay(1000); /* 1ms units */
529 }
530 if (really_read != NULL)
531 *really_read = rd_bytes;
532
533 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000534#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000535 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000536 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000537 ret = -1;
538 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000539#else
540 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
541 msg_perr_strerror("Could not restore serial port mode to blocking: ");
542 ret = -1;
543 }
544#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000545 return ret;
546}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000547
548/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
549 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
550 * 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 +0000551int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000552{
553 int ret = 1;
554 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000555#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000556 DWORD rv;
557 COMMTIMEOUTS oldTimeout;
558 COMMTIMEOUTS newTimeout = {
559 .ReadIntervalTimeout = MAXDWORD,
560 .ReadTotalTimeoutMultiplier = 0,
561 .ReadTotalTimeoutConstant = 0,
562 .WriteTotalTimeoutMultiplier = 0,
563 .WriteTotalTimeoutConstant = 0
564 };
565 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
566 msg_perr_strerror("Could not get serial port timeout settings: ");
567 return -1;
568 }
569 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
570 msg_perr_strerror("Could not set serial port timeout settings: ");
571 return -1;
572 }
573#else
574 ssize_t rv;
575 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000576 if (flags == -1) {
577 msg_perr_strerror("Could not get serial port mode: ");
578 return -1;
579 }
580 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
581 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
582 return -1;
583 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000584#endif
585
Nico Huber519be662018-12-23 20:03:35 +0100586 unsigned int i;
587 unsigned int wr_bytes = 0;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000588 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100589 msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000590#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700591 if (!WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL)) {
592 msg_perr_strerror("Serial port write error: ");
593 ret = -1;
594 break;
595 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000596 msg_pspew("wrote %lu bytes\n", rv);
597#else
598 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
599 msg_pspew("wrote %zd bytes\n", rv);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000600 if ((rv == -1) && (errno != EAGAIN)) {
601 msg_perr_strerror("Serial port write error: ");
602 ret = -1;
603 break;
604 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700605#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000606 if (rv > 0) {
607 wr_bytes += rv;
608 if (wr_bytes == writecnt) {
609 msg_pspew("write successful\n");
610 ret = 0;
611 break;
612 }
613 }
614 internal_delay(1000); /* 1ms units */
615 }
616 if (really_wrote != NULL)
617 *really_wrote = wr_bytes;
618
619 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000620#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000621 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
622 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000623 return -1;
624 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000625#else
626 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
627 msg_perr_strerror("Could not restore serial port blocking behavior: ");
628 return -1;
629 }
630#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000631 return ret;
632}