Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com> |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 6 | * |
| 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 Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <ctype.h> |
| 23 | #include <fcntl.h> |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <errno.h> |
| 26 | #include <inttypes.h> |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 27 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 28 | #include <conio.h> |
| 29 | #else |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 30 | #include <termios.h> |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/ioctl.h> |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 34 | #endif |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 35 | #include "flash.h" |
| 36 | #include "programmer.h" |
Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 37 | #include "custom_baud.h" |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 38 | |
Stefan Tauner | e33c40e | 2013-04-13 00:29:30 +0000 | [diff] [blame] | 39 | fdtype sp_fd = SER_INV_FD; |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 40 | |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 41 | /* 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 Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 47 | * 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 Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 49 | * |
| 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 Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 55 | #define BAUDENTRY(baud) { B##baud, baud }, |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 56 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 57 | static const struct baudentry sp_baudtable[] = { |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 58 | BAUDENTRY(9600) /* unconditional default */ |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 59 | #ifdef B19200 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 60 | BAUDENTRY(19200) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 61 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 62 | #ifdef B38400 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 63 | BAUDENTRY(38400) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 64 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 65 | #ifdef B57600 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 66 | BAUDENTRY(57600) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 67 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 68 | #ifdef B115200 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 69 | BAUDENTRY(115200) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 70 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 71 | #ifdef B230400 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 72 | BAUDENTRY(230400) |
| 73 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 74 | #ifdef B460800 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 75 | BAUDENTRY(460800) |
| 76 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 77 | #ifdef B500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 78 | BAUDENTRY(500000) |
| 79 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 80 | #ifdef B576000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 81 | BAUDENTRY(576000) |
| 82 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 83 | #ifdef B921600 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 84 | BAUDENTRY(921600) |
| 85 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 86 | #ifdef B1000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 87 | BAUDENTRY(1000000) |
| 88 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 89 | #ifdef B1152000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 90 | BAUDENTRY(1152000) |
| 91 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 92 | #ifdef B1500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 93 | BAUDENTRY(1500000) |
| 94 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 95 | #ifdef B2000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 96 | BAUDENTRY(2000000) |
| 97 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 98 | #ifdef B2500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 99 | BAUDENTRY(2500000) |
| 100 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 101 | #ifdef B3000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 102 | BAUDENTRY(3000000) |
| 103 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 104 | #ifdef B3500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 105 | BAUDENTRY(3500000) |
| 106 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 107 | #ifdef B4000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 108 | BAUDENTRY(4000000) |
| 109 | #endif |
| 110 | {0, 0} /* Terminator */ |
| 111 | }; |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 112 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 113 | static const struct baudentry *round_baud(unsigned int baud) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 114 | { |
| 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 Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 122 | msg_pwarn("Warning: given baudrate %d rounded down to %d.\n", |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 123 | baud, sp_baudtable[i].baud); |
| 124 | return &sp_baudtable[i]; |
| 125 | } |
| 126 | } |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 127 | msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud); |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 128 | return &sp_baudtable[0]; |
| 129 | } |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 130 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 131 | |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 132 | /* 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. */ |
| 135 | static void msg_perr_strerror(const char *msg) |
| 136 | { |
| 137 | msg_perr("Error: %s", msg); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 138 | #if IS_WINDOWS |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 139 | 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 Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 153 | int serialport_config(fdtype fd, int baud) |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 154 | { |
| 155 | if (fd == SER_INV_FD) { |
| 156 | msg_perr("%s: File descriptor is invalid.\n", __func__); |
| 157 | return 1; |
| 158 | } |
| 159 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 160 | #if IS_WINDOWS |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 161 | DCB dcb; |
| 162 | if (!GetCommState(fd, &dcb)) { |
| 163 | msg_perr_strerror("Could not fetch original serial port configuration: "); |
| 164 | return 1; |
| 165 | } |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 166 | if (baud >= 0) { |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 167 | dcb.BaudRate = baud; |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 168 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 169 | 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 Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 183 | if (tcgetattr(fd, &observed) != 0) { |
| 184 | msg_perr_strerror("Could not fetch original serial port configuration: "); |
| 185 | return 1; |
| 186 | } |
| 187 | wanted = observed; |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 188 | if (baud >= 0) { |
Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 189 | 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 HAOUAS | e2c90c4 | 2018-08-18 09:04:41 +0200 | [diff] [blame] | 195 | setup doesn't mess up the custom rate. */ |
Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 196 | 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 Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 208 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 209 | } |
| 210 | wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 211 | wanted.c_cflag |= (CS8 | CLOCAL | CREAD); |
Michael Zhilin | 2ec33f9 | 2016-12-02 14:41:26 +0000 | [diff] [blame] | 212 | wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN); |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 213 | 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 Tauner | 631bb02 | 2016-01-04 03:05:06 +0000 | [diff] [blame] | 226 | 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 Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 238 | } |
Stefan Tauner | 631bb02 | 2016-01-04 03:05:06 +0000 | [diff] [blame] | 239 | 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 Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 245 | // FIXME: display actual baud rate - at least if none was specified by the user. |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 246 | #endif |
| 247 | return 0; |
| 248 | } |
| 249 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 250 | fdtype sp_openserport(char *dev, int baud) |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 251 | { |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 252 | fdtype fd; |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 253 | #if IS_WINDOWS |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 254 | char *dev2 = dev; |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 255 | if ((strlen(dev) > 3) && |
| 256 | (tolower((unsigned char)dev[0]) == 'c') && |
Carl-Daniel Hailfinger | 9e3a6c4 | 2010-10-08 12:40:09 +0000 | [diff] [blame] | 257 | (tolower((unsigned char)dev[1]) == 'o') && |
| 258 | (tolower((unsigned char)dev[2]) == 'm')) { |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 259 | dev2 = malloc(strlen(dev) + 5); |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 260 | if (!dev2) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 261 | msg_perr_strerror("Out of memory: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 262 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 263 | } |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 264 | strcpy(dev2, "\\\\.\\"); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 265 | strcpy(dev2 + 4, dev); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 266 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 267 | fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 268 | OPEN_EXISTING, 0, NULL); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 269 | if (dev2 != dev) |
| 270 | free(dev2); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 271 | if (fd == INVALID_HANDLE_VALUE) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 272 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 273 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 274 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 275 | if (serialport_config(fd, baud) != 0) { |
| 276 | CloseHandle(fd); |
| 277 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 278 | } |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 279 | return fd; |
| 280 | #else |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 281 | fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY); // Use O_NDELAY to ignore DCD state |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 282 | if (fd < 0) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 283 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 284 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 285 | } |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 286 | |
| 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 Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 291 | goto err; |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 292 | } |
| 293 | if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { |
| 294 | msg_perr_strerror("Could not set serial port mode to blocking: "); |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 295 | goto err; |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 298 | if (serialport_config(fd, baud) != 0) { |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 299 | goto err; |
Stefan Tauner | f966cc4 | 2013-04-01 00:45:57 +0000 | [diff] [blame] | 300 | } |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 301 | return fd; |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 302 | err: |
| 303 | close(fd); |
| 304 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 305 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 308 | void sp_set_pin(enum SP_PIN pin, int val) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 309 | #if IS_WINDOWS |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 310 | 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 | |
| 343 | int sp_get_pin(enum SP_PIN pin) { |
| 344 | int s; |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 345 | #if IS_WINDOWS |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 346 | 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 Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 360 | void sp_flush_incoming(void) |
| 361 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 362 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 363 | PurgeComm(sp_fd, PURGE_RXCLEAR); |
| 364 | #else |
Niklas Söderlund | 7145a50 | 2012-09-07 07:07:07 +0000 | [diff] [blame] | 365 | /* FIXME: error handling */ |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 366 | tcflush(sp_fd, TCIFLUSH); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 367 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 370 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 371 | int serialport_shutdown(void *data) |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 372 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 373 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 374 | CloseHandle(sp_fd); |
| 375 | #else |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 376 | close(sp_fd); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 377 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 381 | int serialport_write(const unsigned char *buf, unsigned int writecnt) |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 382 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 383 | #if IS_WINDOWS |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 384 | DWORD tmp = 0; |
| 385 | #else |
| 386 | ssize_t tmp = 0; |
| 387 | #endif |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 388 | unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */ |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 389 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 390 | while (writecnt > 0) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 391 | #if IS_WINDOWS |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 392 | if (!WriteFile(sp_fd, buf, writecnt, &tmp, NULL)) { |
| 393 | msg_perr("Serial port write error!\n"); |
| 394 | return 1; |
| 395 | } |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 396 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 397 | tmp = write(sp_fd, buf, writecnt); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 398 | if (tmp == -1) { |
| 399 | msg_perr("Serial port write error!\n"); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 400 | return 1; |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 401 | } |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 402 | #endif |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 403 | if (!tmp) { |
| 404 | msg_pdbg2("Empty write\n"); |
| 405 | empty_writes--; |
Urja Rannikko | f0111d2 | 2013-10-19 23:35:28 +0000 | [diff] [blame] | 406 | internal_delay(500); |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 407 | if (empty_writes == 0) { |
| 408 | msg_perr("Serial port is unresponsive!\n"); |
| 409 | return 1; |
| 410 | } |
| 411 | } |
| 412 | writecnt -= tmp; |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 413 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | int serialport_read(unsigned char *buf, unsigned int readcnt) |
| 420 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 421 | #if IS_WINDOWS |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 422 | DWORD tmp = 0; |
| 423 | #else |
| 424 | ssize_t tmp = 0; |
| 425 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 426 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 427 | while (readcnt > 0) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 428 | #if IS_WINDOWS |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 429 | if (!ReadFile(sp_fd, buf, readcnt, &tmp, NULL)) { |
| 430 | msg_perr("Serial port read error!\n"); |
| 431 | return 1; |
| 432 | } |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 433 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 434 | tmp = read(sp_fd, buf, readcnt); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 435 | if (tmp == -1) { |
| 436 | msg_perr("Serial port read error!\n"); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 437 | return 1; |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 438 | } |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 439 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 440 | if (!tmp) |
Stefan Tauner | 79587f5 | 2013-04-01 00:45:51 +0000 | [diff] [blame] | 441 | msg_pdbg2("Empty read\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 442 | readcnt -= tmp; |
| 443 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 448 | |
| 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. */ |
| 452 | int 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 Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 456 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 457 | 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 Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 471 | msg_perr_strerror("Could not set serial port timeout settings: "); |
| 472 | return -1; |
| 473 | } |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 474 | #else |
| 475 | ssize_t rv; |
| 476 | const int flags = fcntl(sp_fd, F_GETFL); |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 477 | if (flags == -1) { |
| 478 | msg_perr_strerror("Could not get serial port mode: "); |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 479 | return -1; |
| 480 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 481 | 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 Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 486 | |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 487 | unsigned int i; |
| 488 | unsigned int rd_bytes = 0; |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 489 | for (i = 0; i < timeout; i++) { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 490 | msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 491 | #if IS_WINDOWS |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 492 | 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 Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 497 | 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 Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 501 | if ((rv == -1) && (errno != EAGAIN)) { |
| 502 | msg_perr_strerror("Serial port read error: "); |
| 503 | ret = -1; |
| 504 | break; |
| 505 | } |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 506 | #endif |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 507 | 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 Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 519 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 520 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 521 | msg_perr_strerror("Could not restore serial port timeout settings: "); |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 522 | ret = -1; |
| 523 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 524 | #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 Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 530 | return ret; |
| 531 | } |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 532 | |
| 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 Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 536 | int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, unsigned int timeout, unsigned int *really_wrote) |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 537 | { |
| 538 | int ret = 1; |
| 539 | /* disable blocked i/o and declare platform-specific variables */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 540 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 541 | 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 Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 561 | 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 Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 569 | #endif |
| 570 | |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 571 | unsigned int i; |
| 572 | unsigned int wr_bytes = 0; |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 573 | for (i = 0; i < timeout; i++) { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 574 | msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 575 | #if IS_WINDOWS |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 576 | 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 Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 581 | 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 Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 585 | if ((rv == -1) && (errno != EAGAIN)) { |
| 586 | msg_perr_strerror("Serial port write error: "); |
| 587 | ret = -1; |
| 588 | break; |
| 589 | } |
David Hendricks | 7a6bce6 | 2020-07-02 09:36:50 -0700 | [diff] [blame] | 590 | #endif |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 591 | 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 Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 605 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 606 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
| 607 | msg_perr_strerror("Could not restore serial port timeout settings: "); |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 608 | return -1; |
| 609 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 610 | #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 Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 616 | return ret; |
| 617 | } |