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 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 18 | #include "platform.h" |
| 19 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <ctype.h> |
| 25 | #include <fcntl.h> |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 26 | #include <sys/stat.h> |
| 27 | #include <errno.h> |
| 28 | #include <inttypes.h> |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 29 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 30 | #include <conio.h> |
| 31 | #else |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 32 | #include <termios.h> |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/ioctl.h> |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 36 | #endif |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 37 | #include "flash.h" |
| 38 | #include "programmer.h" |
Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 39 | #include "custom_baud.h" |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 40 | |
Stefan Tauner | e33c40e | 2013-04-13 00:29:30 +0000 | [diff] [blame] | 41 | fdtype sp_fd = SER_INV_FD; |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 42 | |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 43 | /* There is no way defined by POSIX to use arbitrary baud rates. It only defines some macros that can be used to |
| 44 | * specify respective baud rates and many implementations extend this list with further macros, cf. TERMIOS(3) |
| 45 | * and http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=blob;f=include/uapi/asm-generic/termbits.h |
| 46 | * The code below creates a mapping in sp_baudtable between these macros and the numerical baud rates to deal |
| 47 | * with numerical user input. |
| 48 | * |
Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 49 | * On Linux there is a non-standard way to use arbitrary baud rates that we use if there is no |
| 50 | * matching standard rate, see custom_baud.c |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 51 | * |
| 52 | * On Windows there exist similar macros (starting with CBR_ instead of B) but they are only defined for |
| 53 | * backwards compatibility and the API supports arbitrary baud rates in the same manner as the macros, see |
| 54 | * http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx |
| 55 | */ |
| 56 | #if !IS_WINDOWS |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 57 | #define BAUDENTRY(baud) { B##baud, baud }, |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 58 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 59 | static const struct baudentry sp_baudtable[] = { |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 60 | BAUDENTRY(9600) /* unconditional default */ |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 61 | #ifdef B19200 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 62 | BAUDENTRY(19200) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 63 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 64 | #ifdef B38400 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 65 | BAUDENTRY(38400) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 66 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 67 | #ifdef B57600 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 68 | BAUDENTRY(57600) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 69 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 70 | #ifdef B115200 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 71 | BAUDENTRY(115200) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 72 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 73 | #ifdef B230400 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 74 | BAUDENTRY(230400) |
| 75 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 76 | #ifdef B460800 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 77 | BAUDENTRY(460800) |
| 78 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 79 | #ifdef B500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 80 | BAUDENTRY(500000) |
| 81 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 82 | #ifdef B576000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 83 | BAUDENTRY(576000) |
| 84 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 85 | #ifdef B921600 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 86 | BAUDENTRY(921600) |
| 87 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 88 | #ifdef B1000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 89 | BAUDENTRY(1000000) |
| 90 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 91 | #ifdef B1152000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 92 | BAUDENTRY(1152000) |
| 93 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 94 | #ifdef B1500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 95 | BAUDENTRY(1500000) |
| 96 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 97 | #ifdef B2000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 98 | BAUDENTRY(2000000) |
| 99 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 100 | #ifdef B2500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 101 | BAUDENTRY(2500000) |
| 102 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 103 | #ifdef B3000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 104 | BAUDENTRY(3000000) |
| 105 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 106 | #ifdef B3500000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 107 | BAUDENTRY(3500000) |
| 108 | #endif |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 109 | #ifdef B4000000 |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 110 | BAUDENTRY(4000000) |
| 111 | #endif |
| 112 | {0, 0} /* Terminator */ |
| 113 | }; |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 114 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 115 | static const struct baudentry *round_baud(unsigned int baud) |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 116 | { |
| 117 | int i; |
| 118 | /* Round baud rate to next lower entry in sp_baudtable if it exists, else use the lowest entry. */ |
| 119 | for (i = ARRAY_SIZE(sp_baudtable) - 2; i >= 0 ; i--) { |
| 120 | if (sp_baudtable[i].baud == baud) |
| 121 | return &sp_baudtable[i]; |
| 122 | |
| 123 | if (sp_baudtable[i].baud < baud) { |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 124 | msg_pwarn("Warning: given baudrate %d rounded down to %d.\n", |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 125 | baud, sp_baudtable[i].baud); |
| 126 | return &sp_baudtable[i]; |
| 127 | } |
| 128 | } |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 129 | msg_pinfo("Using slowest possible baudrate: %d.\n", sp_baudtable[0].baud); |
Stefan Tauner | da5b17c | 2013-04-01 00:45:32 +0000 | [diff] [blame] | 130 | return &sp_baudtable[0]; |
| 131 | } |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 132 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 133 | |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 134 | /* Uses msg_perr to print the last system error. |
| 135 | * Prints "Error: " followed first by \c msg and then by the description of the last error retrieved via |
| 136 | * strerror() or FormatMessage() and ending with a linebreak. */ |
| 137 | static void msg_perr_strerror(const char *msg) |
| 138 | { |
| 139 | msg_perr("Error: %s", msg); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 140 | #if IS_WINDOWS |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 141 | char *lpMsgBuf; |
| 142 | DWORD nErr = GetLastError(); |
| 143 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr, |
| 144 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); |
| 145 | msg_perr(lpMsgBuf); |
| 146 | /* At least some formatted messages contain a line break at the end. Make sure to always print one */ |
| 147 | if (lpMsgBuf[strlen(lpMsgBuf)-1] != '\n') |
| 148 | msg_perr("\n"); |
| 149 | LocalFree(lpMsgBuf); |
| 150 | #else |
| 151 | msg_perr("%s\n", strerror(errno)); |
| 152 | #endif |
| 153 | } |
| 154 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 155 | int serialport_config(fdtype fd, int baud) |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 156 | { |
| 157 | if (fd == SER_INV_FD) { |
| 158 | msg_perr("%s: File descriptor is invalid.\n", __func__); |
| 159 | return 1; |
| 160 | } |
| 161 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 162 | #if IS_WINDOWS |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 163 | DCB dcb; |
| 164 | if (!GetCommState(fd, &dcb)) { |
| 165 | msg_perr_strerror("Could not fetch original serial port configuration: "); |
| 166 | return 1; |
| 167 | } |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 168 | if (baud >= 0) { |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 169 | dcb.BaudRate = baud; |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 170 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 171 | dcb.ByteSize = 8; |
| 172 | dcb.Parity = NOPARITY; |
| 173 | dcb.StopBits = ONESTOPBIT; |
| 174 | if (!SetCommState(fd, &dcb)) { |
| 175 | msg_perr_strerror("Could not change serial port configuration: "); |
| 176 | return 1; |
| 177 | } |
| 178 | if (!GetCommState(fd, &dcb)) { |
| 179 | msg_perr_strerror("Could not fetch new serial port configuration: "); |
| 180 | return 1; |
| 181 | } |
| 182 | msg_pdbg("Baud rate is %ld.\n", dcb.BaudRate); |
| 183 | #else |
| 184 | struct termios wanted, observed; |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 185 | if (tcgetattr(fd, &observed) != 0) { |
| 186 | msg_perr_strerror("Could not fetch original serial port configuration: "); |
| 187 | return 1; |
| 188 | } |
| 189 | wanted = observed; |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 190 | if (baud >= 0) { |
Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 191 | if (use_custom_baud(baud, sp_baudtable)) { |
| 192 | if (set_custom_baudrate(fd, baud)) { |
| 193 | msg_perr_strerror("Could not set custom baudrate: "); |
| 194 | return 1; |
| 195 | } |
| 196 | /* 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] | 197 | setup doesn't mess up the custom rate. */ |
Urja Rannikko | 615ba18 | 2017-06-15 15:28:27 +0300 | [diff] [blame] | 198 | if (tcgetattr(fd, &wanted) != 0) { |
| 199 | /* This should pretty much never happen (see above), but.. */ |
| 200 | msg_perr_strerror("Could not fetch serial port configuration: "); |
| 201 | return 1; |
| 202 | } |
| 203 | msg_pdbg("Using custom baud rate.\n"); |
| 204 | } else { |
| 205 | const struct baudentry *entry = round_baud(baud); |
| 206 | if (cfsetispeed(&wanted, entry->flag) != 0 || cfsetospeed(&wanted, entry->flag) != 0) { |
| 207 | msg_perr_strerror("Could not set serial baud rate: "); |
| 208 | return 1; |
| 209 | } |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 210 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 211 | } |
| 212 | wanted.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS); |
| 213 | wanted.c_cflag |= (CS8 | CLOCAL | CREAD); |
Michael Zhilin | 2ec33f9 | 2016-12-02 14:41:26 +0000 | [diff] [blame] | 214 | wanted.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN); |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 215 | wanted.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR); |
| 216 | wanted.c_oflag &= ~OPOST; |
| 217 | if (tcsetattr(fd, TCSANOW, &wanted) != 0) { |
| 218 | msg_perr_strerror("Could not change serial port configuration: "); |
| 219 | return 1; |
| 220 | } |
| 221 | if (tcgetattr(fd, &observed) != 0) { |
| 222 | msg_perr_strerror("Could not fetch new serial port configuration: "); |
| 223 | return 1; |
| 224 | } |
| 225 | if (observed.c_cflag != wanted.c_cflag || |
| 226 | observed.c_lflag != wanted.c_lflag || |
| 227 | observed.c_iflag != wanted.c_iflag || |
Stefan Tauner | 631bb02 | 2016-01-04 03:05:06 +0000 | [diff] [blame] | 228 | observed.c_oflag != wanted.c_oflag) { |
| 229 | msg_pwarn("Some requested serial options did not stick, continuing anyway.\n"); |
| 230 | msg_pdbg(" observed wanted\n" |
| 231 | "c_cflag: 0x%08lX 0x%08lX\n" |
| 232 | "c_lflag: 0x%08lX 0x%08lX\n" |
| 233 | "c_iflag: 0x%08lX 0x%08lX\n" |
| 234 | "c_oflag: 0x%08lX 0x%08lX\n", |
| 235 | (long)observed.c_cflag, (long)wanted.c_cflag, |
| 236 | (long)observed.c_lflag, (long)wanted.c_lflag, |
| 237 | (long)observed.c_iflag, (long)wanted.c_iflag, |
| 238 | (long)observed.c_oflag, (long)wanted.c_oflag |
| 239 | ); |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 240 | } |
Stefan Tauner | 631bb02 | 2016-01-04 03:05:06 +0000 | [diff] [blame] | 241 | if (cfgetispeed(&observed) != cfgetispeed(&wanted) || |
| 242 | cfgetospeed(&observed) != cfgetospeed(&wanted)) { |
| 243 | msg_pwarn("Could not set baud rates exactly.\n"); |
| 244 | msg_pdbg("Actual baud flags are: ispeed: 0x%08lX, ospeed: 0x%08lX\n", |
| 245 | (long)cfgetispeed(&observed), (long)cfgetospeed(&observed)); |
| 246 | } |
Urja Rannikko | dc44584 | 2016-01-04 05:08:40 +0000 | [diff] [blame] | 247 | // 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] | 248 | #endif |
| 249 | return 0; |
| 250 | } |
| 251 | |
Stefan Tauner | 72587f8 | 2016-01-04 03:05:15 +0000 | [diff] [blame] | 252 | fdtype sp_openserport(char *dev, int baud) |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 253 | { |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 254 | fdtype fd; |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 255 | #if IS_WINDOWS |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 256 | char *dev2 = dev; |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 257 | if ((strlen(dev) > 3) && |
| 258 | (tolower((unsigned char)dev[0]) == 'c') && |
Carl-Daniel Hailfinger | 9e3a6c4 | 2010-10-08 12:40:09 +0000 | [diff] [blame] | 259 | (tolower((unsigned char)dev[1]) == 'o') && |
| 260 | (tolower((unsigned char)dev[2]) == 'm')) { |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 261 | dev2 = malloc(strlen(dev) + 5); |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 262 | if (!dev2) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 263 | msg_perr_strerror("Out of memory: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 264 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 265 | } |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 266 | strcpy(dev2, "\\\\.\\"); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 267 | strcpy(dev2 + 4, dev); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 268 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 269 | fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 270 | OPEN_EXISTING, 0, NULL); |
Patrick Georgi | 06602c2 | 2010-01-26 20:58:40 +0000 | [diff] [blame] | 271 | if (dev2 != dev) |
| 272 | free(dev2); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 273 | if (fd == INVALID_HANDLE_VALUE) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 274 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 275 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 276 | } |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 277 | if (serialport_config(fd, baud) != 0) { |
| 278 | CloseHandle(fd); |
| 279 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 280 | } |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 281 | return fd; |
| 282 | #else |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 283 | 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] | 284 | if (fd < 0) { |
Stefan Tauner | bf88be9 | 2013-04-01 00:45:08 +0000 | [diff] [blame] | 285 | msg_perr_strerror("Cannot open serial port: "); |
Stefan Tauner | bb4fed7 | 2012-09-01 21:47:19 +0000 | [diff] [blame] | 286 | return SER_INV_FD; |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 287 | } |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 288 | |
| 289 | /* Ensure that we use blocking I/O */ |
| 290 | const int flags = fcntl(fd, F_GETFL); |
| 291 | if (flags == -1) { |
| 292 | msg_perr_strerror("Could not get serial port mode: "); |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 293 | goto err; |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 294 | } |
| 295 | if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { |
| 296 | msg_perr_strerror("Could not set serial port mode to blocking: "); |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 297 | goto err; |
Stefan Tauner | a4d60f3 | 2016-01-04 03:04:36 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Stefan Tauner | 184c52c | 2013-08-23 21:51:32 +0000 | [diff] [blame] | 300 | if (serialport_config(fd, baud) != 0) { |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 301 | goto err; |
Stefan Tauner | f966cc4 | 2013-04-01 00:45:57 +0000 | [diff] [blame] | 302 | } |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 303 | return fd; |
Stefan Tauner | a371281 | 2016-01-16 18:50:27 +0000 | [diff] [blame] | 304 | err: |
| 305 | close(fd); |
| 306 | return SER_INV_FD; |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 307 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 310 | void sp_set_pin(enum SP_PIN pin, int val) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 311 | #if IS_WINDOWS |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 312 | DWORD ctl; |
| 313 | |
| 314 | if(pin == PIN_TXD) { |
| 315 | ctl = val ? SETBREAK: CLRBREAK; |
| 316 | } |
| 317 | else if(pin == PIN_DTR) { |
| 318 | ctl = val ? SETDTR: CLRDTR; |
| 319 | } |
| 320 | else { |
| 321 | ctl = val ? SETRTS: CLRRTS; |
| 322 | } |
| 323 | EscapeCommFunction(sp_fd, ctl); |
| 324 | #else |
| 325 | int ctl, s; |
| 326 | |
| 327 | if(pin == PIN_TXD) { |
| 328 | ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0); |
| 329 | } |
| 330 | else { |
| 331 | s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS; |
| 332 | ioctl(sp_fd, TIOCMGET, &ctl); |
| 333 | |
| 334 | if (val) { |
| 335 | ctl |= s; |
| 336 | } |
| 337 | else { |
| 338 | ctl &= ~s; |
| 339 | } |
| 340 | ioctl(sp_fd, TIOCMSET, &ctl); |
| 341 | } |
| 342 | #endif |
| 343 | } |
| 344 | |
| 345 | int sp_get_pin(enum SP_PIN pin) { |
| 346 | int s; |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 347 | #if IS_WINDOWS |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 348 | DWORD ctl; |
| 349 | |
| 350 | s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON; |
| 351 | GetCommModemStatus(sp_fd, &ctl); |
| 352 | #else |
| 353 | int ctl; |
| 354 | s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR; |
| 355 | ioctl(sp_fd, TIOCMGET, &ctl); |
| 356 | #endif |
| 357 | |
| 358 | return ((ctl & s) ? 1 : 0); |
| 359 | |
| 360 | } |
| 361 | |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 362 | void sp_flush_incoming(void) |
| 363 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 364 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 365 | PurgeComm(sp_fd, PURGE_RXCLEAR); |
| 366 | #else |
Niklas Söderlund | 7145a50 | 2012-09-07 07:07:07 +0000 | [diff] [blame] | 367 | /* FIXME: error handling */ |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 368 | tcflush(sp_fd, TCIFLUSH); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 369 | #endif |
Carl-Daniel Hailfinger | e51ea10 | 2009-11-23 19:20:11 +0000 | [diff] [blame] | 370 | return; |
| 371 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 372 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 373 | int serialport_shutdown(void *data) |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 374 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 375 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 376 | CloseHandle(sp_fd); |
| 377 | #else |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 378 | close(sp_fd); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 379 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 383 | int serialport_write(const unsigned char *buf, unsigned int writecnt) |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 384 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 385 | #if IS_WINDOWS |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 386 | DWORD tmp = 0; |
| 387 | #else |
| 388 | ssize_t tmp = 0; |
| 389 | #endif |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 390 | unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */ |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 391 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 392 | while (writecnt > 0) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 393 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 394 | WriteFile(sp_fd, buf, writecnt, &tmp, NULL); |
| 395 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 396 | tmp = write(sp_fd, buf, writecnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 397 | #endif |
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 | } |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 402 | if (!tmp) { |
| 403 | msg_pdbg2("Empty write\n"); |
| 404 | empty_writes--; |
Urja Rannikko | f0111d2 | 2013-10-19 23:35:28 +0000 | [diff] [blame] | 405 | internal_delay(500); |
Stefan Tauner | 62574aa | 2012-11-30 16:46:41 +0000 | [diff] [blame] | 406 | if (empty_writes == 0) { |
| 407 | msg_perr("Serial port is unresponsive!\n"); |
| 408 | return 1; |
| 409 | } |
| 410 | } |
| 411 | writecnt -= tmp; |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 412 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int serialport_read(unsigned char *buf, unsigned int readcnt) |
| 419 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 420 | #if IS_WINDOWS |
Uwe Hermann | d5e85d6 | 2011-07-03 19:44:12 +0000 | [diff] [blame] | 421 | DWORD tmp = 0; |
| 422 | #else |
| 423 | ssize_t tmp = 0; |
| 424 | #endif |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 425 | |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 426 | while (readcnt > 0) { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 427 | #if IS_WINDOWS |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 428 | ReadFile(sp_fd, buf, readcnt, &tmp, NULL); |
| 429 | #else |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 430 | tmp = read(sp_fd, buf, readcnt); |
Patrick Georgi | e48654c | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 431 | #endif |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 432 | if (tmp == -1) { |
| 433 | msg_perr("Serial port read error!\n"); |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 434 | return 1; |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 435 | } |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 436 | if (!tmp) |
Stefan Tauner | 79587f5 | 2013-04-01 00:45:51 +0000 | [diff] [blame] | 437 | msg_pdbg2("Empty read\n"); |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 438 | readcnt -= tmp; |
| 439 | buf += tmp; |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 444 | |
| 445 | /* Tries up to timeout ms to read readcnt characters and places them into the array starting at c. Returns |
| 446 | * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors. |
| 447 | * If really_read is not NULL, this function sets its contents to the number of bytes read successfully. */ |
| 448 | int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned int timeout, unsigned int *really_read) |
| 449 | { |
| 450 | int ret = 1; |
| 451 | /* disable blocked i/o and declare platform-specific variables */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 452 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 453 | DWORD rv; |
| 454 | COMMTIMEOUTS oldTimeout; |
| 455 | COMMTIMEOUTS newTimeout = { |
| 456 | .ReadIntervalTimeout = MAXDWORD, |
| 457 | .ReadTotalTimeoutMultiplier = 0, |
| 458 | .ReadTotalTimeoutConstant = 0, |
| 459 | .WriteTotalTimeoutMultiplier = 0, |
| 460 | .WriteTotalTimeoutConstant = 0 |
| 461 | }; |
| 462 | if(!GetCommTimeouts(sp_fd, &oldTimeout)) { |
| 463 | msg_perr_strerror("Could not get serial port timeout settings: "); |
| 464 | return -1; |
| 465 | } |
| 466 | if(!SetCommTimeouts(sp_fd, &newTimeout)) { |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 467 | msg_perr_strerror("Could not set serial port timeout settings: "); |
| 468 | return -1; |
| 469 | } |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 470 | #else |
| 471 | ssize_t rv; |
| 472 | const int flags = fcntl(sp_fd, F_GETFL); |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 473 | if (flags == -1) { |
| 474 | msg_perr_strerror("Could not get serial port mode: "); |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 475 | return -1; |
| 476 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 477 | if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
| 478 | msg_perr_strerror("Could not set serial port mode to non-blocking: "); |
| 479 | return -1; |
| 480 | } |
| 481 | #endif |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 482 | |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame^] | 483 | unsigned int i; |
| 484 | unsigned int rd_bytes = 0; |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 485 | for (i = 0; i < timeout; i++) { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame^] | 486 | msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 487 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 488 | ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL); |
| 489 | msg_pspew("read %lu bytes\n", rv); |
| 490 | #else |
| 491 | rv = read(sp_fd, c + rd_bytes, readcnt - rd_bytes); |
| 492 | msg_pspew("read %zd bytes\n", rv); |
| 493 | #endif |
| 494 | if ((rv == -1) && (errno != EAGAIN)) { |
| 495 | msg_perr_strerror("Serial port read error: "); |
| 496 | ret = -1; |
| 497 | break; |
| 498 | } |
| 499 | if (rv > 0) |
| 500 | rd_bytes += rv; |
| 501 | if (rd_bytes == readcnt) { |
| 502 | ret = 0; |
| 503 | break; |
| 504 | } |
| 505 | internal_delay(1000); /* 1ms units */ |
| 506 | } |
| 507 | if (really_read != NULL) |
| 508 | *really_read = rd_bytes; |
| 509 | |
| 510 | /* restore original blocking behavior */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 511 | #if IS_WINDOWS |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 512 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 513 | msg_perr_strerror("Could not restore serial port timeout settings: "); |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 514 | ret = -1; |
| 515 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 516 | #else |
| 517 | if (fcntl(sp_fd, F_SETFL, flags) != 0) { |
| 518 | msg_perr_strerror("Could not restore serial port mode to blocking: "); |
| 519 | ret = -1; |
| 520 | } |
| 521 | #endif |
Stefan Tauner | 00e1608 | 2013-04-01 00:45:38 +0000 | [diff] [blame] | 522 | return ret; |
| 523 | } |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 524 | |
| 525 | /* Tries up to timeout ms to write writecnt characters from the array starting at buf. Returns |
| 526 | * 0 on success, positive values on temporary errors (e.g. timeouts) and negative ones on permanent errors. |
| 527 | * 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] | 528 | 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] | 529 | { |
| 530 | int ret = 1; |
| 531 | /* disable blocked i/o and declare platform-specific variables */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 532 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 533 | DWORD rv; |
| 534 | COMMTIMEOUTS oldTimeout; |
| 535 | COMMTIMEOUTS newTimeout = { |
| 536 | .ReadIntervalTimeout = MAXDWORD, |
| 537 | .ReadTotalTimeoutMultiplier = 0, |
| 538 | .ReadTotalTimeoutConstant = 0, |
| 539 | .WriteTotalTimeoutMultiplier = 0, |
| 540 | .WriteTotalTimeoutConstant = 0 |
| 541 | }; |
| 542 | if(!GetCommTimeouts(sp_fd, &oldTimeout)) { |
| 543 | msg_perr_strerror("Could not get serial port timeout settings: "); |
| 544 | return -1; |
| 545 | } |
| 546 | if(!SetCommTimeouts(sp_fd, &newTimeout)) { |
| 547 | msg_perr_strerror("Could not set serial port timeout settings: "); |
| 548 | return -1; |
| 549 | } |
| 550 | #else |
| 551 | ssize_t rv; |
| 552 | const int flags = fcntl(sp_fd, F_GETFL); |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 553 | if (flags == -1) { |
| 554 | msg_perr_strerror("Could not get serial port mode: "); |
| 555 | return -1; |
| 556 | } |
| 557 | if (fcntl(sp_fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
| 558 | msg_perr_strerror("Could not set serial port mode to non-blocking: "); |
| 559 | return -1; |
| 560 | } |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 561 | #endif |
| 562 | |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame^] | 563 | unsigned int i; |
| 564 | unsigned int wr_bytes = 0; |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 565 | for (i = 0; i < timeout; i++) { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame^] | 566 | msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes); |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 567 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 568 | WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL); |
| 569 | msg_pspew("wrote %lu bytes\n", rv); |
| 570 | #else |
| 571 | rv = write(sp_fd, buf + wr_bytes, writecnt - wr_bytes); |
| 572 | msg_pspew("wrote %zd bytes\n", rv); |
| 573 | #endif |
| 574 | if ((rv == -1) && (errno != EAGAIN)) { |
| 575 | msg_perr_strerror("Serial port write error: "); |
| 576 | ret = -1; |
| 577 | break; |
| 578 | } |
| 579 | if (rv > 0) { |
| 580 | wr_bytes += rv; |
| 581 | if (wr_bytes == writecnt) { |
| 582 | msg_pspew("write successful\n"); |
| 583 | ret = 0; |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | internal_delay(1000); /* 1ms units */ |
| 588 | } |
| 589 | if (really_wrote != NULL) |
| 590 | *really_wrote = wr_bytes; |
| 591 | |
| 592 | /* restore original blocking behavior */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 593 | #if IS_WINDOWS |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 594 | if (!SetCommTimeouts(sp_fd, &oldTimeout)) { |
| 595 | msg_perr_strerror("Could not restore serial port timeout settings: "); |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 596 | return -1; |
| 597 | } |
Stefan Reinauer | 0df8446 | 2014-05-27 22:10:15 +0000 | [diff] [blame] | 598 | #else |
| 599 | if (fcntl(sp_fd, F_SETFL, flags) != 0) { |
| 600 | msg_perr_strerror("Could not restore serial port blocking behavior: "); |
| 601 | return -1; |
| 602 | } |
| 603 | #endif |
Stefan Tauner | ae3d837 | 2013-04-01 00:45:45 +0000 | [diff] [blame] | 604 | return ret; |
| 605 | } |