blob: 608464b7464f70fea328e07a7a8c107532327cb4 [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000022#include "platform.h"
23
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000024#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000027#include <string.h>
28#include <ctype.h>
29#include <fcntl.h>
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000030#include <sys/stat.h>
31#include <errno.h>
32#include <inttypes.h>
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000033#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +000034#include <conio.h>
35#else
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000036#include <termios.h>
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +000037#include <unistd.h>
38#include <sys/types.h>
39#include <sys/ioctl.h>
Patrick Georgie48654c2010-01-06 22:14:39 +000040#endif
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000041#include "flash.h"
42#include "programmer.h"
Urja Rannikko615ba182017-06-15 15:28:27 +030043#include "custom_baud.h"
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000044
Stefan Taunere33c40e2013-04-13 00:29:30 +000045fdtype sp_fd = SER_INV_FD;
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000046
Urja Rannikkodc445842016-01-04 05:08:40 +000047/* There is no way defined by POSIX to use arbitrary baud rates. It only defines some macros that can be used to
48 * specify respective baud rates and many implementations extend this list with further macros, cf. TERMIOS(3)
49 * and http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h
50 * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal
51 * with numerical user input.
52 *
Urja Rannikko615ba182017-06-15 15:28:27 +030053 * On Linux there is a non-standard way to use arbitrary baud rates that we use if there is no
54 * matching standard rate, see custom_baud.c
Urja Rannikkodc445842016-01-04 05:08:40 +000055 *
56 * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for
57 * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see
58 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
59 */
60#if !IS_WINDOWS
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000061#define BAUDENTRY(baud) { B##baud, baud },
Stefan Taunerda5b17c2013-04-01 00:45:32 +000062
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000063static const struct baudentry sp_baudtable[] = {
Stefan Taunerda5b17c2013-04-01 00:45:32 +000064 BAUDENTRY(9600) /* unconditional default */
Urja Rannikkodc445842016-01-04 05:08:40 +000065#ifdef B19200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000066 BAUDENTRY(19200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000067#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000068#ifdef B38400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000069 BAUDENTRY(38400)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000070#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000071#ifdef B57600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000072 BAUDENTRY(57600)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000073#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000074#ifdef B115200
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000075 BAUDENTRY(115200)
Stefan Taunerda5b17c2013-04-01 00:45:32 +000076#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000077#ifdef B230400
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000078 BAUDENTRY(230400)
79#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000080#ifdef B460800
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000081 BAUDENTRY(460800)
82#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000083#ifdef B500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000084 BAUDENTRY(500000)
85#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000086#ifdef B576000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000087 BAUDENTRY(576000)
88#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000089#ifdef B921600
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000090 BAUDENTRY(921600)
91#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000092#ifdef B1000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000093 BAUDENTRY(1000000)
94#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000095#ifdef B1152000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000096 BAUDENTRY(1152000)
97#endif
Urja Rannikkodc445842016-01-04 05:08:40 +000098#ifdef B1500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +000099 BAUDENTRY(1500000)
100#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000101#ifdef B2000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000102 BAUDENTRY(2000000)
103#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000104#ifdef B2500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000105 BAUDENTRY(2500000)
106#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000107#ifdef B3000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000108 BAUDENTRY(3000000)
109#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000110#ifdef B3500000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000111 BAUDENTRY(3500000)
112#endif
Urja Rannikkodc445842016-01-04 05:08:40 +0000113#ifdef B4000000
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000114 BAUDENTRY(4000000)
115#endif
116 {0, 0} /* Terminator */
117};
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000118
Stefan Tauner72587f82016-01-04 03:05:15 +0000119static const struct baudentry *round_baud(unsigned int baud)
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000120{
121 int i;
122 /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */
123 for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) {
124 if (sp_baudtable[i].baud == baud)
125 return &sp_baudtable[i];
126
127 if (sp_baudtable[i].baud < baud) {
Stefan Tauner72587f82016-01-04 03:05:15 +0000128 msg_pwarn("Warning: given baudrate %d rounded down to %d.\n",
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000129 baud, sp_baudtable[i].baud);
130 return &sp_baudtable[i];
131 }
132 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000133 msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud);
Stefan Taunerda5b17c2013-04-01 00:45:32 +0000134 return &sp_baudtable[0];
135}
Urja Rannikkodc445842016-01-04 05:08:40 +0000136#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000137
Stefan Taunerbf88be92013-04-01 00:45:08 +0000138/* Uses msg_perr to print the last system error.
139 * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via
140 * strerror() or FormatMessage() and ending with a linebreak. */
141static void msg_perr_strerror(const char *msg)
142{
143 msg_perr("Error: %s", msg);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000144#if IS_WINDOWS
Stefan Taunerbf88be92013-04-01 00:45:08 +0000145 char *lpMsgBuf;
146 DWORD nErr = GetLastError();
147 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
148 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
149 msg_perr(lpMsgBuf);
150 /* At least some formatted messages contain a line break at the end. Make sure to always print one */
151 if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n')
152 msg_perr("\n");
153 LocalFree(lpMsgBuf);
154#else
155 msg_perr("%s\n", strerror(errno));
156#endif
157}
158
Stefan Tauner72587f82016-01-04 03:05:15 +0000159int serialport_config(fdtype fd, int baud)
Stefan Tauner184c52c2013-08-23 21:51:32 +0000160{
161 if (fd == SER_INV_FD) {
162 msg_perr("%s: File descriptor is invalid.\n", __func__);
163 return 1;
164 }
165
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000166#if IS_WINDOWS
Stefan Tauner184c52c2013-08-23 21:51:32 +0000167 DCB dcb;
168 if (!GetCommState(fd, &dcb)) {
169 msg_perr_strerror("Could not fetch original serial port configuration: ");
170 return 1;
171 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000172 if (baud >= 0) {
Urja Rannikkodc445842016-01-04 05:08:40 +0000173 dcb.BaudRate = baud;
Stefan Tauner72587f82016-01-04 03:05:15 +0000174 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000175 dcb.ByteSize = 8;
176 dcb.Parity = NOPARITY;
177 dcb.StopBits = ONESTOPBIT;
178 if (!SetCommState(fd, &dcb)) {
179 msg_perr_strerror("Could not change serial port configuration: ");
180 return 1;
181 }
182 if (!GetCommState(fd, &dcb)) {
183 msg_perr_strerror("Could not fetch new serial port configuration: ");
184 return 1;
185 }
186 msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate);
187#else
188 struct termios wanted, observed;
Stefan Tauner184c52c2013-08-23 21:51:32 +0000189 if (tcgetattr(fd, &observed) != 0) {
190 msg_perr_strerror("Could not fetch original serial port configuration: ");
191 return 1;
192 }
193 wanted = observed;
Stefan Tauner72587f82016-01-04 03:05:15 +0000194 if (baud >= 0) {
Urja Rannikko615ba182017-06-15 15:28:27 +0300195 if (use_custom_baud(baud, sp_baudtable)) {
196 if (set_custom_baudrate(fd, baud)) {
197 msg_perr_strerror("Could not set custom baudrate: ");
198 return 1;
199 }
200 /* We want whatever the termios looks like now, so the rest of the
201 setup doesnt mess up the custom rate. */
202 if (tcgetattr(fd, &wanted) != 0) {
203 /* This should pretty much never happen (see above), but.. */
204 msg_perr_strerror("Could not fetch serial port configuration: ");
205 return 1;
206 }
207 msg_pdbg("Using custom baud rate.\n");
208 } else {
209 const struct baudentry *entry = round_baud(baud);
210 if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) {
211 msg_perr_strerror("Could not set serial baud rate: ");
212 return 1;
213 }
Stefan Tauner72587f82016-01-04 03:05:15 +0000214 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000215 }
216 wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
217 wanted.c_cflag |= (CS8 | CLOCAL | CREAD);
218 wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
219 wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
220 wanted.c_oflag &= ~OPOST;
221 if (tcsetattr(fd, TCSANOW, &wanted) != 0) {
222 msg_perr_strerror("Could not change serial port configuration: ");
223 return 1;
224 }
225 if (tcgetattr(fd, &observed) != 0) {
226 msg_perr_strerror("Could not fetch new serial port configuration: ");
227 return 1;
228 }
229 if (observed.c_cflag != wanted.c_cflag ||
230 observed.c_lflag != wanted.c_lflag ||
231 observed.c_iflag != wanted.c_iflag ||
Stefan Tauner631bb022016-01-04 03:05:06 +0000232 observed.c_oflag != wanted.c_oflag) {
233 msg_pwarn("Some requested serial options did not stick, continuing anyway.\n");
234 msg_pdbg(" observed wanted\n"
235 "c_cflag: 0x%08lX 0x%08lX\n"
236 "c_lflag: 0x%08lX 0x%08lX\n"
237 "c_iflag: 0x%08lX 0x%08lX\n"
238 "c_oflag: 0x%08lX 0x%08lX\n",
239 (long)observed.c_cflag, (long)wanted.c_cflag,
240 (long)observed.c_lflag, (long)wanted.c_lflag,
241 (long)observed.c_iflag, (long)wanted.c_iflag,
242 (long)observed.c_oflag, (long)wanted.c_oflag
243 );
Stefan Tauner184c52c2013-08-23 21:51:32 +0000244 }
Stefan Tauner631bb022016-01-04 03:05:06 +0000245 if (cfgetispeed(&observed) != cfgetispeed(&wanted) ||
246 cfgetospeed(&observed) != cfgetospeed(&wanted)) {
247 msg_pwarn("Could not set baud rates exactly.\n");
248 msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n",
249 (long)cfgetispeed(&observed), (long)cfgetospeed(&observed));
250 }
Urja Rannikkodc445842016-01-04 05:08:40 +0000251 // FIXME: display actual baud rate - at least if none was specified by the user.
Stefan Tauner184c52c2013-08-23 21:51:32 +0000252#endif
253 return 0;
254}
255
Stefan Tauner72587f82016-01-04 03:05:15 +0000256fdtype sp_openserport(char *dev, int baud)
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000257{
Stefan Tauner184c52c2013-08-23 21:51:32 +0000258 fdtype fd;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000259#if IS_WINDOWS
Uwe Hermann43959702010-03-13 17:28:29 +0000260 char *dev2 = dev;
Stefan Taunerbf88be92013-04-01 00:45:08 +0000261 if ((strlen(dev) > 3) &&
262 (tolower((unsigned char)dev[0]) == 'c') &&
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000263 (tolower((unsigned char)dev[1]) == 'o') &&
264 (tolower((unsigned char)dev[2]) == 'm')) {
Uwe Hermann43959702010-03-13 17:28:29 +0000265 dev2 = malloc(strlen(dev) + 5);
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000266 if (!dev2) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000267 msg_perr_strerror("Out of memory: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000268 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000269 }
Patrick Georgi06602c22010-01-26 20:58:40 +0000270 strcpy(dev2, "\\\\.\\");
Uwe Hermann43959702010-03-13 17:28:29 +0000271 strcpy(dev2 + 4, dev);
Patrick Georgi06602c22010-01-26 20:58:40 +0000272 }
Uwe Hermann43959702010-03-13 17:28:29 +0000273 fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
274 OPEN_EXISTING, 0, NULL);
Patrick Georgi06602c22010-01-26 20:58:40 +0000275 if (dev2 != dev)
276 free(dev2);
Patrick Georgie48654c2010-01-06 22:14:39 +0000277 if (fd == INVALID_HANDLE_VALUE) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000278 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000279 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000280 }
Stefan Tauner184c52c2013-08-23 21:51:32 +0000281 if (serialport_config(fd, baud) != 0) {
282 CloseHandle(fd);
283 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000284 }
Patrick Georgie48654c2010-01-06 22:14:39 +0000285 return fd;
286#else
Stefan Taunera4d60f32016-01-04 03:04:36 +0000287 fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000288 if (fd < 0) {
Stefan Taunerbf88be92013-04-01 00:45:08 +0000289 msg_perr_strerror("Cannot open serial port: ");
Stefan Taunerbb4fed72012-09-01 21:47:19 +0000290 return SER_INV_FD;
Niklas Söderlund2a95e872012-07-30 19:42:33 +0000291 }
Stefan Taunera4d60f32016-01-04 03:04:36 +0000292
293 /* Ensure that we use blocking I/O */
294 const int flags = fcntl(fd, F_GETFL);
295 if (flags == -1) {
296 msg_perr_strerror("Could not get serial port mode: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000297 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000298 }
299 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
300 msg_perr_strerror("Could not set serial port mode to blocking: ");
Stefan Taunera3712812016-01-16 18:50:27 +0000301 goto err;
Stefan Taunera4d60f32016-01-04 03:04:36 +0000302 }
303
Stefan Tauner184c52c2013-08-23 21:51:32 +0000304 if (serialport_config(fd, baud) != 0) {
Stefan Taunera3712812016-01-16 18:50:27 +0000305 goto err;
Stefan Taunerf966cc42013-04-01 00:45:57 +0000306 }
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000307 return fd;
Stefan Taunera3712812016-01-16 18:50:27 +0000308err:
309 close(fd);
310 return SER_INV_FD;
Patrick Georgie48654c2010-01-06 22:14:39 +0000311#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000312}
313
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000314void sp_set_pin(enum SP_PIN pin, int val) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000315#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000316 DWORD ctl;
317
318 if(pin == PIN_TXD) {
319 ctl = val ? SETBREAK: CLRBREAK;
320 }
321 else if(pin == PIN_DTR) {
322 ctl = val ? SETDTR: CLRDTR;
323 }
324 else {
325 ctl = val ? SETRTS: CLRRTS;
326 }
327 EscapeCommFunction(sp_fd, ctl);
328#else
329 int ctl, s;
330
331 if(pin == PIN_TXD) {
332 ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
333 }
334 else {
335 s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
336 ioctl(sp_fd, TIOCMGET, &ctl);
337
338 if (val) {
339 ctl |= s;
340 }
341 else {
342 ctl &= ~s;
343 }
344 ioctl(sp_fd, TIOCMSET, &ctl);
345 }
346#endif
347}
348
349int sp_get_pin(enum SP_PIN pin) {
350 int s;
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000351#if IS_WINDOWS
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000352 DWORD ctl;
353
354 s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
355 GetCommModemStatus(sp_fd, &ctl);
356#else
357 int ctl;
358 s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
359 ioctl(sp_fd, TIOCMGET, &ctl);
360#endif
361
362 return ((ctl & s) ? 1 : 0);
363
364}
365
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000366void sp_flush_incoming(void)
367{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000368#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000369 PurgeComm(sp_fd, PURGE_RXCLEAR);
370#else
Niklas Söderlund7145a502012-09-07 07:07:07 +0000371 /* FIXME: error handling */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000372 tcflush(sp_fd, TCIFLUSH);
Patrick Georgie48654c2010-01-06 22:14:39 +0000373#endif
Carl-Daniel Hailfingere51ea102009-11-23 19:20:11 +0000374 return;
375}
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000376
David Hendricks8bb20212011-06-14 01:35:36 +0000377int serialport_shutdown(void *data)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000378{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000379#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000380 CloseHandle(sp_fd);
381#else
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000382 close(sp_fd);
Patrick Georgie48654c2010-01-06 22:14:39 +0000383#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000384 return 0;
385}
386
Mark Marshallf20b7be2014-05-09 21:16:21 +0000387int serialport_write(const unsigned char *buf, unsigned int writecnt)
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000388{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000389#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000390 DWORD tmp = 0;
391#else
392 ssize_t tmp = 0;
393#endif
Stefan Tauner62574aa2012-11-30 16:46:41 +0000394 unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000395
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000396 while (writecnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000397#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000398 WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
399#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000400 tmp = write(sp_fd, buf, writecnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000401#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000402 if (tmp == -1) {
403 msg_perr("Serial port write error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000404 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000405 }
Stefan Tauner62574aa2012-11-30 16:46:41 +0000406 if (!tmp) {
407 msg_pdbg2("Empty write\n");
408 empty_writes--;
Urja Rannikkof0111d22013-10-19 23:35:28 +0000409 internal_delay(500);
Stefan Tauner62574aa2012-11-30 16:46:41 +0000410 if (empty_writes == 0) {
411 msg_perr("Serial port is unresponsive!\n");
412 return 1;
413 }
414 }
415 writecnt -= tmp;
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000416 buf += tmp;
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000417 }
418
419 return 0;
420}
421
422int serialport_read(unsigned char *buf, unsigned int readcnt)
423{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000424#if IS_WINDOWS
Uwe Hermannd5e85d62011-07-03 19:44:12 +0000425 DWORD tmp = 0;
426#else
427 ssize_t tmp = 0;
428#endif
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000429
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000430 while (readcnt > 0) {
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000431#if IS_WINDOWS
Patrick Georgie48654c2010-01-06 22:14:39 +0000432 ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
433#else
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000434 tmp = read(sp_fd, buf, readcnt);
Patrick Georgie48654c2010-01-06 22:14:39 +0000435#endif
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000436 if (tmp == -1) {
437 msg_perr("Serial port read error!\n");
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000438 return 1;
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000439 }
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
487 int i;
488 int rd_bytes = 0;
489 for (i = 0; i < timeout; i++) {
490 msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000491#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000492 ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
493 msg_pspew("read %lu bytes\n", rv);
494#else
495 rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes);
496 msg_pspew("read %zd bytes\n", rv);
497#endif
498 if ((rv == -1) && (errno != EAGAIN)) {
499 msg_perr_strerror("Serial port read error: ");
500 ret = -1;
501 break;
502 }
503 if (rv > 0)
504 rd_bytes += rv;
505 if (rd_bytes == readcnt) {
506 ret = 0;
507 break;
508 }
509 internal_delay(1000); /* 1ms units */
510 }
511 if (really_read != NULL)
512 *really_read = rd_bytes;
513
514 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000515#if IS_WINDOWS
Stefan Tauner00e16082013-04-01 00:45:38 +0000516 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
Stefan Reinauer0df84462014-05-27 22:10:15 +0000517 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Tauner00e16082013-04-01 00:45:38 +0000518 ret = -1;
519 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000520#else
521 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
522 msg_perr_strerror("Could not restore serial port mode to blocking: ");
523 ret = -1;
524 }
525#endif
Stefan Tauner00e16082013-04-01 00:45:38 +0000526 return ret;
527}
Stefan Taunerae3d8372013-04-01 00:45:45 +0000528
529/* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns
530 * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors.
531 * 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 +0000532int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote)
Stefan Taunerae3d8372013-04-01 00:45:45 +0000533{
534 int ret = 1;
535 /* disable blocked i/o and declare platform-specific variables */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000536#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000537 DWORD rv;
538 COMMTIMEOUTS oldTimeout;
539 COMMTIMEOUTS newTimeout = {
540 .ReadIntervalTimeout = MAXDWORD,
541 .ReadTotalTimeoutMultiplier = 0,
542 .ReadTotalTimeoutConstant = 0,
543 .WriteTotalTimeoutMultiplier = 0,
544 .WriteTotalTimeoutConstant = 0
545 };
546 if(!GetCommTimeouts(sp_fd, &oldTimeout)) {
547 msg_perr_strerror("Could not get serial port timeout settings: ");
548 return -1;
549 }
550 if(!SetCommTimeouts(sp_fd, &newTimeout)) {
551 msg_perr_strerror("Could not set serial port timeout settings: ");
552 return -1;
553 }
554#else
555 ssize_t rv;
556 const int flags = fcntl(sp_fd, F_GETFL);
Stefan Reinauer0df84462014-05-27 22:10:15 +0000557 if (flags == -1) {
558 msg_perr_strerror("Could not get serial port mode: ");
559 return -1;
560 }
561 if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
562 msg_perr_strerror("Could not set serial port mode to non-blocking: ");
563 return -1;
564 }
Stefan Taunerae3d8372013-04-01 00:45:45 +0000565#endif
566
567 int i;
568 int wr_bytes = 0;
569 for (i = 0; i < timeout; i++) {
570 msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000571#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000572 WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
573 msg_pspew("wrote %lu bytes\n", rv);
574#else
575 rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes);
576 msg_pspew("wrote %zd bytes\n", rv);
577#endif
578 if ((rv == -1) && (errno != EAGAIN)) {
579 msg_perr_strerror("Serial port write error: ");
580 ret = -1;
581 break;
582 }
583 if (rv > 0) {
584 wr_bytes += rv;
585 if (wr_bytes == writecnt) {
586 msg_pspew("write successful\n");
587 ret = 0;
588 break;
589 }
590 }
591 internal_delay(1000); /* 1ms units */
592 }
593 if (really_wrote != NULL)
594 *really_wrote = wr_bytes;
595
596 /* restore original blocking behavior */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000597#if IS_WINDOWS
Stefan Taunerae3d8372013-04-01 00:45:45 +0000598 if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
599 msg_perr_strerror("Could not restore serial port timeout settings: ");
Stefan Taunerae3d8372013-04-01 00:45:45 +0000600 return -1;
601 }
Stefan Reinauer0df84462014-05-27 22:10:15 +0000602#else
603 if (fcntl(sp_fd, F_SETFL, flags) != 0) {
604 msg_perr_strerror("Could not restore serial port blocking behavior: ");
605 return -1;
606 }
607#endif
Stefan Taunerae3d8372013-04-01 00:45:45 +0000608 return ret;
609}