blob: cfc9b1e671179844038f9f57d134d152002573fd [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 *
50 * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for
51 * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see
52 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
53 */
54#if !IS_WINDOWS
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000055#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000056
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000057static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000058 BAUDENTRY(9600) /* unconditional default */
Urja Rannikkodc445842016-01-04 05:08:40 +000059#ifdef B19200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000060 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000061#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000062#ifdef B38400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000063 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000064#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000065#ifdef B57600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000066 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000067#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000068#ifdef B115200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000069 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000070#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000071#ifdef B230400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000072 BAUDENTRY(230400)
73#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000074#ifdef B460800
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000075 BAUDENTRY(460800)
76#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000077#ifdef B500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000078 BAUDENTRY(500000)
79#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000080#ifdef B576000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000081 BAUDENTRY(576000)
82#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000083#ifdef B921600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000084 BAUDENTRY(921600)
85#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000086#ifdef B1000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000087 BAUDENTRY(1000000)
88#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000089#ifdef B1152000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000090 BAUDENTRY(1152000)
91#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000092#ifdef B1500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000093 BAUDENTRY(1500000)
94#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000095#ifdef B2000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000096 BAUDENTRY(2000000)
97#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000098#ifdef B2500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000099 BAUDENTRY(2500000)
100#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000101#ifdef B3000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000102 BAUDENTRY(3000000)
103#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000104#ifdef B3500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000105 BAUDENTRY(3500000)
106#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000107#ifdef B4000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000108 BAUDENTRY(4000000)
109#endif
110 {0, 0} /* Terminator */
111};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000112
Stefan Tauner72587f82016-01-04 03:05:15 +0000113static const struct baudentry *round_baud(unsigned int baud)
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000114{
115 int i;
116 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
117 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
118 if (sp_baudtable[i].baud == baud)
119 return &sp_baudtable[i];
120
121 if (sp_baudtable[i].baud < baud) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000122 msg_pwarn("Warning: given baudrate %d rounded down to %d.\n",
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000123 baud, sp_baudtable[i].baud);
124 return &sp_baudtable[i];
125 }
126 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000127 msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud);
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000128 return &sp_baudtable[0];
129}
Urja Rannikkodc445842016-01-04 05:08:40 +0000130#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000131
Stefan Taunerbf88be92013-04-01 00:45:08 +0000132/* Uses msg_perr to print the last system error.
133 * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
134 * strerror() or FormatMessage() and ending with a linebreak. */
135static void msg_perr_strerror(const char *msg)
136{
137 msg_perr("Error: %s", msg);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000138#if IS_WINDOWS
Stefan Taunerbf88be92013-04-01 00:45:08 +0000139 char *lpMsgBuf;
140 DWORD nErr = GetLastError();
141 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
142 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
143 msg_perr(lpMsgBuf);
144 /* At least some formatted messages contain a line break at the end. Make sure to always print one */
145 if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
146 msg_perr("\n");
147 LocalFree(lpMsgBuf);
148#else
149 msg_perr("%s\n", strerror(errno));
150#endif
151}
152
Stefan Tauner72587f82016-01-04 03:05:15 +0000153int serialport_config(fdtype fd, int baud)
Stefan Tauner184c52c2013-08-23 21:51:32 +0000154{
155 if (fd == SER_INV_FD) {
156 msg_perr("%s: File descriptor is invalid.\n", __func__);
157 return 1;
158 }
159
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000160#if IS_WINDOWS
Stefan Tauner184c52c2013-08-23 21:51:32 +0000161 DCB dcb;
162 if (!GetCommState(fd, &dcb)) {
163 msg_perr_strerror("Could not fetch original serial port configuration: ");
164 return 1;
165 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000166 if (baud >= 0) {
Urja Rannikkodc445842016-01-04 05:08:40 +0000167 dcb.BaudRate = baud;
Stefan Tauner72587f82016-01-04 03:05:15 +0000168 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000169 dcb.ByteSize = 8;
170 dcb.Parity = NOPARITY;
171 dcb.StopBits = ONESTOPBIT;
172 if (!SetCommState(fd, &dcb)) {
173 msg_perr_strerror("Could not change serial port configuration: ");
174 return 1;
175 }
176 if (!GetCommState(fd, &dcb)) {
177 msg_perr_strerror("Could not fetch new serial port configuration: ");
178 return 1;
179 }
180 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
181#else
Peter Stugeb078ec62022-12-11 04:02:10 +0100182 int custom_baud = (baud >= 0 && use_custom_baud(baud, sp_baudtable));
Stefan Tauner184c52c2013-08-23 21:51:32 +0000183 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000184 if (tcgetattr(fd, &observed) != 0) {
185 msg_perr_strerror("Could not fetch original serial port configuration: ");
186 return 1;
187 }
188 wanted = observed;
Stefan Tauner72587f82016-01-04 03:05:15 +0000189 if (baud >= 0) {
Peter Stugeb078ec62022-12-11 04:02:10 +0100190 if (custom_baud) {
191 if (set_custom_baudrate(fd, baud, BEFORE_FLAGS, NULL)) {
Urja Rannikko615ba182017-06-15 15:28:27 +0300192 msg_perr_strerror("Could not set custom baudrate: ");
193 return 1;
194 }
195 /* We want whatever the termios looks like now, so the rest of the
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200196 setup doesn't mess up the custom rate. */
Urja Rannikko615ba182017-06-15 15:28:27 +0300197 if (tcgetattr(fd, &wanted) != 0) {
198 /* This should pretty much never happen (see above), but.. */
199 msg_perr_strerror("Could not fetch serial port configuration: ");
200 return 1;
201 }
Urja Rannikko615ba182017-06-15 15:28:27 +0300202 } else {
203 const struct baudentry *entry = round_baud(baud);
204 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
205 msg_perr_strerror("Could not set serial baud rate: ");
206 return 1;
207 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000208 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000209 }
210 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
211 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
Michael Zhilin2ec33f92016-12-02 14:41:26 +0000212 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN);
Stefan Tauner184c52c2013-08-23 21:51:32 +0000213 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
214 wanted.c_oflag &= ~OPOST;
Peter Stugeb078ec62022-12-11 04:02:10 +0100215 if (custom_baud && set_custom_baudrate(fd, baud, WITH_FLAGS, &wanted)) {
216 msg_perr_strerror("Could not set custom baudrate: ");
217 return 1;
218 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000219 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
220 msg_perr_strerror("Could not change serial port configuration: ");
221 return 1;
222 }
223 if (tcgetattr(fd, &observed) != 0) {
224 msg_perr_strerror("Could not fetch new serial port configuration: ");
225 return 1;
226 }
227 if (observed.c_cflag != wanted.c_cflag ||
228 observed.c_lflag != wanted.c_lflag ||
229 observed.c_iflag != wanted.c_iflag ||
Stefan Tauner631bb022016-01-04 03:05:06 +0000230 observed.c_oflag != wanted.c_oflag) {
231 msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
232 msg_pdbg(" observed wanted\n"
233 "c_cflag: 0x%08lX 0x%08lX\n"
234 "c_lflag: 0x%08lX 0x%08lX\n"
235 "c_iflag: 0x%08lX 0x%08lX\n"
236 "c_oflag: 0x%08lX 0x%08lX\n",
237 (long)observed.c_cflag, (long)wanted.c_cflag,
238 (long)observed.c_lflag, (long)wanted.c_lflag,
239 (long)observed.c_iflag, (long)wanted.c_iflag,
240 (long)observed.c_oflag, (long)wanted.c_oflag
241 );
Stefan Tauner184c52c2013-08-23 21:51:32 +0000242 }
Peter Stugeb078ec62022-12-11 04:02:10 +0100243 if (custom_baud) {
244 if (set_custom_baudrate(fd, baud, AFTER_FLAGS, &wanted)) {
245 msg_perr_strerror("Could not set custom baudrate: ");
246 return 1;
247 }
248 msg_pdbg("Using custom baud rate.\n");
249 }
Stefan Tauner631bb022016-01-04 03:05:06 +0000250 if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
251 cfgetospeed(&observed) != cfgetospeed(&wanted)) {
252 msg_pwarn("Could not set baud rates exactly.\n");
253 msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
254 (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
255 }
Urja Rannikkodc445842016-01-04 05:08:40 +0000256 // FIXME: display actual baud rate - at least if none was specified by the user.
Stefan Tauner184c52c2013-08-23 21:51:32 +0000257#endif
258 return 0;
259}
260
Stefan Tauner72587f82016-01-04 03:05:15 +0000261fdtype sp_openserport(char *dev, int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000262{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000263 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000264#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000265 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000266 if ((strlen(dev) > 3) &&
267 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000268 (tolower((unsigned char)dev[1]) == 'o') &&
269 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000270 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000271 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000272 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000273 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000274 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000275 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000276 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000277 }
Uwe Hermann43959702010-03-13 17:28:29 +0000278 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
279 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000280 if (dev2 != dev)
281 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000282 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000283 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000284 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000285 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000286 if (serialport_config(fd, baud) != 0) {
287 CloseHandle(fd);
288 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000289 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000290 return fd;
291#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000292 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000293 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000294 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000295 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000296 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000297
298 /* Ensure that we use blocking I/O */
299 const int flags = fcntl(fd, F_GETFL);
300 if (flags == -1) {
301 msg_perr_strerror("Could not get serial port mode: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000302 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000303 }
304 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
305 msg_perr_strerror("Could not set serial port mode to blocking: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000306 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000307 }
308
Stefan Tauner184c52c2013-08-23 21:51:32 +0000309 if (serialport_config(fd, baud) != 0) {
Stefan Taunera3712812016-01-16 18:50:27 +0000310 goto err;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000311 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000312 return fd;
Stefan Taunera3712812016-01-16 18:50:27 +0000313err:
314 close(fd);
315 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000316#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000317}
318
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000319void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000320#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000321 DWORD ctl;
322
323 if(pin == PIN_TXD) {
324 ctl = val ? SETBREAK: CLRBREAK;
325 }
326 else if(pin == PIN_DTR) {
327 ctl = val ? SETDTR: CLRDTR;
328 }
329 else {
330 ctl = val ? SETRTS: CLRRTS;
331 }
332 EscapeCommFunction(sp_fd, ctl);
333#else
334 int ctl, s;
335
336 if(pin == PIN_TXD) {
337 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
338 }
339 else {
340 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
341 ioctl(sp_fd, TIOCMGET, &ctl);
342
343 if (val) {
344 ctl |= s;
345 }
346 else {
347 ctl &= ~s;
348 }
349 ioctl(sp_fd, TIOCMSET, &ctl);
350 }
351#endif
352}
353
354int sp_get_pin(enum SP_PIN pin) {
355 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000356#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000357 DWORD ctl;
358
359 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
360 GetCommModemStatus(sp_fd, &ctl);
361#else
362 int ctl;
363 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
364 ioctl(sp_fd, TIOCMGET, &ctl);
365#endif
366
367 return ((ctl & s) ? 1 : 0);
368
369}
370
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000371void sp_flush_incoming(void)
372{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000373#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000374 PurgeComm(sp_fd, PURGE_RXCLEAR);
375#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000376 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000377 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000378#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000379 return;
380}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000381
David Hendricks8bb20212011-06-14 01:35:36 +0000382int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000383{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000384#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000385 CloseHandle(sp_fd);
386#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000387 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000388#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000389 return 0;
390}
391
Mark Marshallf20b7be2014-05-09 21:16:21 +0000392int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000393{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000394#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000395 DWORD tmp = 0;
396#else
397 ssize_t tmp = 0;
398#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000399 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000400
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000401 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000402#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700403 if (!WriteFile(sp_fd, buf, writecnt, &tmp, NULL)) {
404 msg_perr("Serial port write error!\n");
405 return 1;
406 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000407#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000408 tmp = write(sp_fd, buf, writecnt);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000409 if (tmp == -1) {
410 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000411 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000412 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700413#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000414 if (!tmp) {
415 msg_pdbg2("Empty write\n");
416 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000417 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000418 if (empty_writes == 0) {
419 msg_perr("Serial port is unresponsive!\n");
420 return 1;
421 }
422 }
423 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000424 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000425 }
426
427 return 0;
428}
429
430int serialport_read(unsigned char *buf, unsigned int readcnt)
431{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000432#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000433 DWORD tmp = 0;
434#else
435 ssize_t tmp = 0;
436#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000437
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000438 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000439#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700440 if (!ReadFile(sp_fd, buf, readcnt, &tmp, NULL)) {
441 msg_perr("Serial port read error!\n");
442 return 1;
443 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000444#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000445 tmp = read(sp_fd, buf, readcnt);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000446 if (tmp == -1) {
447 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000448 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000449 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700450#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000451 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000452 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000453 readcnt -= tmp;
454 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000455 }
456
457 return 0;
458}
Stefan Tauner00e16082013-04-01 00:45:38 +0000459
460/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
461 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
462 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
463int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
464{
465 int ret = 1;
466 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000467#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000468 DWORD rv;
469 COMMTIMEOUTS oldTimeout;
470 COMMTIMEOUTS newTimeout = {
471 .ReadIntervalTimeout = MAXDWORD,
472 .ReadTotalTimeoutMultiplier = 0,
473 .ReadTotalTimeoutConstant = 0,
474 .WriteTotalTimeoutMultiplier = 0,
475 .WriteTotalTimeoutConstant = 0
476 };
477 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
478 msg_perr_strerror("Could not get serial port timeout settings: ");
479 return -1;
480 }
481 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000482 msg_perr_strerror("Could not set serial port timeout settings: ");
483 return -1;
484 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000485#else
486 ssize_t rv;
487 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000488 if (flags == -1) {
489 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000490 return -1;
491 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000492 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
493 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
494 return -1;
495 }
496#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000497
Nico Huber519be662018-12-23 20:03:35 +0100498 unsigned int i;
499 unsigned int rd_bytes = 0;
Stefan Tauner00e16082013-04-01 00:45:38 +0000500 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100501 msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000502#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700503 if (!ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL)) {
504 msg_perr_strerror("Serial port read error: ");
505 ret = -1;
506 break;
507 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000508 msg_pspew("read %lu bytes\n", rv);
509#else
510 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
511 msg_pspew("read %zd bytes\n", rv);
Stefan Tauner00e16082013-04-01 00:45:38 +0000512 if ((rv == -1) && (errno != EAGAIN)) {
513 msg_perr_strerror("Serial port read error: ");
514 ret = -1;
515 break;
516 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700517#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000518 if (rv > 0)
519 rd_bytes += rv;
520 if (rd_bytes == readcnt) {
521 ret = 0;
522 break;
523 }
524 internal_delay(1000); /* 1ms units */
525 }
526 if (really_read != NULL)
527 *really_read = rd_bytes;
528
529 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000530#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000531 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000532 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000533 ret = -1;
534 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000535#else
536 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
537 msg_perr_strerror("Could not restore serial port mode to blocking: ");
538 ret = -1;
539 }
540#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000541 return ret;
542}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000543
544/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
545 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
546 * 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 +0000547int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000548{
549 int ret = 1;
550 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000551#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000552 DWORD rv;
553 COMMTIMEOUTS oldTimeout;
554 COMMTIMEOUTS newTimeout = {
555 .ReadIntervalTimeout = MAXDWORD,
556 .ReadTotalTimeoutMultiplier = 0,
557 .ReadTotalTimeoutConstant = 0,
558 .WriteTotalTimeoutMultiplier = 0,
559 .WriteTotalTimeoutConstant = 0
560 };
561 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
562 msg_perr_strerror("Could not get serial port timeout settings: ");
563 return -1;
564 }
565 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
566 msg_perr_strerror("Could not set serial port timeout settings: ");
567 return -1;
568 }
569#else
570 ssize_t rv;
571 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000572 if (flags == -1) {
573 msg_perr_strerror("Could not get serial port mode: ");
574 return -1;
575 }
576 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
577 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
578 return -1;
579 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000580#endif
581
Nico Huber519be662018-12-23 20:03:35 +0100582 unsigned int i;
583 unsigned int wr_bytes = 0;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000584 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100585 msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000586#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700587 if (!WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL)) {
588 msg_perr_strerror("Serial port write error: ");
589 ret = -1;
590 break;
591 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000592 msg_pspew("wrote %lu bytes\n", rv);
593#else
594 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
595 msg_pspew("wrote %zd bytes\n", rv);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000596 if ((rv == -1) && (errno != EAGAIN)) {
597 msg_perr_strerror("Serial port write error: ");
598 ret = -1;
599 break;
600 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700601#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000602 if (rv > 0) {
603 wr_bytes += rv;
604 if (wr_bytes == writecnt) {
605 msg_pspew("write successful\n");
606 ret = 0;
607 break;
608 }
609 }
610 internal_delay(1000); /* 1ms units */
611 }
612 if (really_wrote != NULL)
613 *really_wrote = wr_bytes;
614
615 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000616#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000617 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
618 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000619 return -1;
620 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000621#else
622 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
623 msg_perr_strerror("Could not restore serial port blocking behavior: ");
624 return -1;
625 }
626#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000627 return ret;
628}