blob: 72f9ef68664af43a61f1cbe735e0788ee9a6c5b4 [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
182 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000183 if (tcgetattr(fd, &observed) != 0) {
184 msg_perr_strerror("Could not fetch original serial port configuration: ");
185 return 1;
186 }
187 wanted = observed;
Stefan Tauner72587f82016-01-04 03:05:15 +0000188 if (baud >= 0) {
Urja Rannikko615ba182017-06-15 15:28:27 +0300189 if (use_custom_baud(baud, sp_baudtable)) {
190 if (set_custom_baudrate(fd, baud)) {
191 msg_perr_strerror("Could not set custom baudrate: ");
192 return 1;
193 }
194 /* We want whatever the termios looks like now, so the rest of the
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200195 setup doesn't mess up the custom rate. */
Urja Rannikko615ba182017-06-15 15:28:27 +0300196 if (tcgetattr(fd, &wanted) != 0) {
197 /* This should pretty much never happen (see above), but.. */
198 msg_perr_strerror("Could not fetch serial port configuration: ");
199 return 1;
200 }
201 msg_pdbg("Using custom baud rate.\n");
202 } 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;
215 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
216 msg_perr_strerror("Could not change serial port configuration: ");
217 return 1;
218 }
219 if (tcgetattr(fd, &observed) != 0) {
220 msg_perr_strerror("Could not fetch new serial port configuration: ");
221 return 1;
222 }
223 if (observed.c_cflag != wanted.c_cflag ||
224 observed.c_lflag != wanted.c_lflag ||
225 observed.c_iflag != wanted.c_iflag ||
Stefan Tauner631bb022016-01-04 03:05:06 +0000226 observed.c_oflag != wanted.c_oflag) {
227 msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
228 msg_pdbg(" observed wanted\n"
229 "c_cflag: 0x%08lX 0x%08lX\n"
230 "c_lflag: 0x%08lX 0x%08lX\n"
231 "c_iflag: 0x%08lX 0x%08lX\n"
232 "c_oflag: 0x%08lX 0x%08lX\n",
233 (long)observed.c_cflag, (long)wanted.c_cflag,
234 (long)observed.c_lflag, (long)wanted.c_lflag,
235 (long)observed.c_iflag, (long)wanted.c_iflag,
236 (long)observed.c_oflag, (long)wanted.c_oflag
237 );
Stefan Tauner184c52c2013-08-23 21:51:32 +0000238 }
Stefan Tauner631bb022016-01-04 03:05:06 +0000239 if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
240 cfgetospeed(&observed) != cfgetospeed(&wanted)) {
241 msg_pwarn("Could not set baud rates exactly.\n");
242 msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
243 (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
244 }
Urja Rannikkodc445842016-01-04 05:08:40 +0000245 // FIXME: display actual baud rate - at least if none was specified by the user.
Stefan Tauner184c52c2013-08-23 21:51:32 +0000246#endif
247 return 0;
248}
249
Stefan Tauner72587f82016-01-04 03:05:15 +0000250fdtype sp_openserport(char *dev, int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000251{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000252 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000253#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000254 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000255 if ((strlen(dev) > 3) &&
256 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000257 (tolower((unsigned char)dev[1]) == 'o') &&
258 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000259 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000260 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000261 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000262 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000263 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000264 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000265 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000266 }
Uwe Hermann43959702010-03-13 17:28:29 +0000267 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
268 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000269 if (dev2 != dev)
270 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000271 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000272 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000273 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000274 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000275 if (serialport_config(fd, baud) != 0) {
276 CloseHandle(fd);
277 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000278 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000279 return fd;
280#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000281 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000282 if (fd < 0) {
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;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000285 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000286
287 /* Ensure that we use blocking I/O */
288 const int flags = fcntl(fd, F_GETFL);
289 if (flags == -1) {
290 msg_perr_strerror("Could not get serial port mode: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000291 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000292 }
293 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
294 msg_perr_strerror("Could not set serial port mode to blocking: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000295 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000296 }
297
Stefan Tauner184c52c2013-08-23 21:51:32 +0000298 if (serialport_config(fd, baud) != 0) {
Stefan Taunera3712812016-01-16 18:50:27 +0000299 goto err;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000300 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000301 return fd;
Stefan Taunera3712812016-01-16 18:50:27 +0000302err:
303 close(fd);
304 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000305#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000306}
307
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000308void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000309#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000310 DWORD ctl;
311
312 if(pin == PIN_TXD) {
313 ctl = val ? SETBREAK: CLRBREAK;
314 }
315 else if(pin == PIN_DTR) {
316 ctl = val ? SETDTR: CLRDTR;
317 }
318 else {
319 ctl = val ? SETRTS: CLRRTS;
320 }
321 EscapeCommFunction(sp_fd, ctl);
322#else
323 int ctl, s;
324
325 if(pin == PIN_TXD) {
326 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
327 }
328 else {
329 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
330 ioctl(sp_fd, TIOCMGET, &ctl);
331
332 if (val) {
333 ctl |= s;
334 }
335 else {
336 ctl &= ~s;
337 }
338 ioctl(sp_fd, TIOCMSET, &ctl);
339 }
340#endif
341}
342
343int sp_get_pin(enum SP_PIN pin) {
344 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000345#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000346 DWORD ctl;
347
348 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
349 GetCommModemStatus(sp_fd, &ctl);
350#else
351 int ctl;
352 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
353 ioctl(sp_fd, TIOCMGET, &ctl);
354#endif
355
356 return ((ctl & s) ? 1 : 0);
357
358}
359
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000360void sp_flush_incoming(void)
361{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000362#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000363 PurgeComm(sp_fd, PURGE_RXCLEAR);
364#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000365 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000366 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000367#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000368 return;
369}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000370
David Hendricks8bb20212011-06-14 01:35:36 +0000371int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000372{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000373#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000374 CloseHandle(sp_fd);
375#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000376 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000377#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000378 return 0;
379}
380
Mark Marshallf20b7be2014-05-09 21:16:21 +0000381int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000382{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000383#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000384 DWORD tmp = 0;
385#else
386 ssize_t tmp = 0;
387#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000388 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000389
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000390 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000391#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700392 if (!WriteFile(sp_fd, buf, writecnt, &tmp, NULL)) {
393 msg_perr("Serial port write error!\n");
394 return 1;
395 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000396#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000397 tmp = write(sp_fd, buf, writecnt);
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 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700402#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000403 if (!tmp) {
404 msg_pdbg2("Empty write\n");
405 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000406 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000407 if (empty_writes == 0) {
408 msg_perr("Serial port is unresponsive!\n");
409 return 1;
410 }
411 }
412 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000413 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000414 }
415
416 return 0;
417}
418
419int serialport_read(unsigned char *buf, unsigned int readcnt)
420{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000421#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000422 DWORD tmp = 0;
423#else
424 ssize_t tmp = 0;
425#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000426
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000427 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000428#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700429 if (!ReadFile(sp_fd, buf, readcnt, &tmp, NULL)) {
430 msg_perr("Serial port read error!\n");
431 return 1;
432 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000433#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000434 tmp = read(sp_fd, buf, readcnt);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000435 if (tmp == -1) {
436 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000437 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000438 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700439#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000440 if (!tmp)
Stefan Tauner79587f52013-04-01 00:45:51 +0000441 msg_pdbg2("Empty read\n");
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000442 readcnt -= tmp;
443 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000444 }
445
446 return 0;
447}
Stefan Tauner00e16082013-04-01 00:45:38 +0000448
449/* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns
450 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
451 * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */
452int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read)
453{
454 int ret = 1;
455 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000456#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000457 DWORD rv;
458 COMMTIMEOUTS oldTimeout;
459 COMMTIMEOUTS newTimeout = {
460 .ReadIntervalTimeout = MAXDWORD,
461 .ReadTotalTimeoutMultiplier = 0,
462 .ReadTotalTimeoutConstant = 0,
463 .WriteTotalTimeoutMultiplier = 0,
464 .WriteTotalTimeoutConstant = 0
465 };
466 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
467 msg_perr_strerror("Could not get serial port timeout settings: ");
468 return -1;
469 }
470 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000471 msg_perr_strerror("Could not set serial port timeout settings: ");
472 return -1;
473 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000474#else
475 ssize_t rv;
476 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000477 if (flags == -1) {
478 msg_perr_strerror("Could not get serial port mode: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000479 return -1;
480 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000481 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
482 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
483 return -1;
484 }
485#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000486
Nico Huber519be662018-12-23 20:03:35 +0100487 unsigned int i;
488 unsigned int rd_bytes = 0;
Stefan Tauner00e16082013-04-01 00:45:38 +0000489 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100490 msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000491#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700492 if (!ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL)) {
493 msg_perr_strerror("Serial port read error: ");
494 ret = -1;
495 break;
496 }
Stefan Tauner00e16082013-04-01 00:45:38 +0000497 msg_pspew("read %lu bytes\n", rv);
498#else
499 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
500 msg_pspew("read %zd bytes\n", rv);
Stefan Tauner00e16082013-04-01 00:45:38 +0000501 if ((rv == -1) && (errno != EAGAIN)) {
502 msg_perr_strerror("Serial port read error: ");
503 ret = -1;
504 break;
505 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700506#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000507 if (rv > 0)
508 rd_bytes += rv;
509 if (rd_bytes == readcnt) {
510 ret = 0;
511 break;
512 }
513 internal_delay(1000); /* 1ms units */
514 }
515 if (really_read != NULL)
516 *really_read = rd_bytes;
517
518 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000519#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000520 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000521 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000522 ret = -1;
523 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000524#else
525 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
526 msg_perr_strerror("Could not restore serial port mode to blocking: ");
527 ret = -1;
528 }
529#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000530 return ret;
531}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000532
533/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
534 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
535 * 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 +0000536int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000537{
538 int ret = 1;
539 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000540#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000541 DWORD rv;
542 COMMTIMEOUTS oldTimeout;
543 COMMTIMEOUTS newTimeout = {
544 .ReadIntervalTimeout = MAXDWORD,
545 .ReadTotalTimeoutMultiplier = 0,
546 .ReadTotalTimeoutConstant = 0,
547 .WriteTotalTimeoutMultiplier = 0,
548 .WriteTotalTimeoutConstant = 0
549 };
550 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
551 msg_perr_strerror("Could not get serial port timeout settings: ");
552 return -1;
553 }
554 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
555 msg_perr_strerror("Could not set serial port timeout settings: ");
556 return -1;
557 }
558#else
559 ssize_t rv;
560 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000561 if (flags == -1) {
562 msg_perr_strerror("Could not get serial port mode: ");
563 return -1;
564 }
565 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
566 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
567 return -1;
568 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000569#endif
570
Nico Huber519be662018-12-23 20:03:35 +0100571 unsigned int i;
572 unsigned int wr_bytes = 0;
Stefan Taunerae3d8372013-04-01 00:45:45 +0000573 for (i = 0; i < timeout; i++) {
Nico Huber519be662018-12-23 20:03:35 +0100574 msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000575#if IS_WINDOWS
David Hendricks7a6bce62020-07-02 09:36:50 -0700576 if (!WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL)) {
577 msg_perr_strerror("Serial port write error: ");
578 ret = -1;
579 break;
580 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000581 msg_pspew("wrote %lu bytes\n", rv);
582#else
583 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
584 msg_pspew("wrote %zd bytes\n", rv);
Stefan Taunerae3d8372013-04-01 00:45:45 +0000585 if ((rv == -1) && (errno != EAGAIN)) {
586 msg_perr_strerror("Serial port write error: ");
587 ret = -1;
588 break;
589 }
David Hendricks7a6bce62020-07-02 09:36:50 -0700590#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000591 if (rv > 0) {
592 wr_bytes += rv;
593 if (wr_bytes == writecnt) {
594 msg_pspew("write successful\n");
595 ret = 0;
596 break;
597 }
598 }
599 internal_delay(1000); /* 1ms units */
600 }
601 if (really_wrote != NULL)
602 *really_wrote = wr_bytes;
603
604 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000605#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000606 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
607 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000608 return -1;
609 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000610#else
611 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
612 msg_perr_strerror("Could not restore serial port blocking behavior: ");
613 return -1;
614 }
615#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000616 return ret;
617}