| 1 | /* |
| 2 | RailControl - Model Railway Control Software |
| 3 | |
| 4 | Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 | |
| 6 | RailControl is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 3, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | RailControl is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | See the GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with RailControl; see the file LICENCE. If not see |
| 18 | <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <termios.h> |
| 23 | |
| 24 | #include "Network/Select.h" |
| 25 | #include "Network/Serial.h" |
| 26 | |
| 27 | namespace Network |
| 28 | { |
| 29 | void Serial::Init() |
| 30 | { |
| 31 | fileHandle = open(tty.c_str(), O_RDWR | O_NOCTTY); |
| 32 | if (!IsConnected()) |
| 33 | { |
| 34 | logger->Error(Languages::TextUnableToOpenSerial, tty); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | struct termios options; |
| 39 | options.c_cflag = 0; |
| 40 | options.c_cc[VMIN] = 1; // read one byte at least |
| 41 | options.c_cc[VTIME] = 0; // timeout disabled |
| 42 | options.c_lflag = 0; |
| 43 | options.c_iflag = 0; |
| 44 | options.c_oflag = 0; |
| 45 | cfsetispeed(&options, dataSpeed); |
| 46 | cfsetospeed(&options, dataSpeed); |
| 47 | switch (dataBits) |
| 48 | { |
| 49 | case 5: |
| 50 | options.c_cflag |= CS5; // 5 data bits |
| 51 | break; |
| 52 | |
| 53 | case 6: |
| 54 | options.c_cflag |= CS6; // 6 data bits |
| 55 | break; |
| 56 | |
| 57 | case 7: |
| 58 | options.c_cflag |= CS7; // 7 data bits |
| 59 | break; |
| 60 | |
| 61 | case 8: |
| 62 | default: |
| 63 | options.c_cflag |= CS8; // 8 data bits |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | if (stopBits == 2) |
| 68 | { |
| 69 | options.c_cflag |= CSTOPB; // 2 stop bit |
| 70 | } |
| 71 | // else 1 stop bit |
| 72 | |
| 73 | switch (parity) |
| 74 | { |
| 75 | case 'E': |
| 76 | case 'e': |
| 77 | options.c_cflag |= PARENB; // even parity |
| 78 | break; |
| 79 | |
| 80 | case 'O': |
| 81 | case 'o': |
| 82 | options.c_cflag |= PARENB; |
| 83 | options.c_cflag |= PARODD; // odd parity |
| 84 | break; |
| 85 | |
| 86 | // default: no parity |
| 87 | } |
| 88 | |
| 89 | // CSIZE not set: no datasize |
| 90 | if (hardwareFlowControl == true) |
| 91 | { |
| 92 | options.c_cflag |= CRTSCTS; // hardware flow control |
| 93 | } |
| 94 | options.c_cflag |= CLOCAL; // ignore control lines |
| 95 | options.c_cflag |= CREAD; // enable receiver |
| 96 | tcsetattr(fileHandle, TCSANOW, &options); // store options |
| 97 | |
| 98 | ClearBuffers(); |
| 99 | } |
| 100 | |
| 101 | void Serial::Close() |
| 102 | { |
| 103 | if (!IsConnected()) |
| 104 | { |
| 105 | return; |
| 106 | } |
| 107 | close(fileHandle); |
| 108 | fileHandle = -1; |
| 109 | } |
| 110 | |
| 111 | bool Serial::Receive(std::string& data, const size_t maxData, const unsigned int timeoutS, const unsigned int timeoutUS) |
| 112 | { |
| 113 | const size_t dataBufferSize = 1024; |
| 114 | unsigned char dataBuffer[dataBufferSize]; |
| 115 | const size_t max = maxData > dataBufferSize ? dataBufferSize : maxData; |
| 116 | ssize_t ret = Receive(dataBuffer, max, timeoutS, timeoutUS); |
| 117 | if (ret <= 0) |
| 118 | { |
| 119 | return false; |
| 120 | } |
| 121 | data.append(reinterpret_cast<char*>(dataBuffer), ret); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | ssize_t Serial::Receive(unsigned char* data, const size_t maxData, const unsigned int timeoutS, const unsigned int timeoutUS) |
| 126 | { |
| 127 | if (!IsConnected()) |
| 128 | { |
| 129 | return -1; |
| 130 | } |
| 131 | fd_set set; |
| 132 | FD_ZERO(&set); |
| 133 | FD_SET(fileHandle, &set); |
| 134 | struct timeval tvTimeout; |
| 135 | tvTimeout.tv_sec = timeoutS; |
| 136 | tvTimeout.tv_usec = timeoutUS; |
| 137 | |
| 138 | ssize_t ret = TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &tvTimeout)); |
| 139 | if (ret <= 0) |
| 140 | { |
| 141 | return -1; |
| 142 | } |
| 143 | ret = read(fileHandle, data, maxData); |
| 144 | if (ret <= 0) // FIXME: why not (ret < 0)? |
| 145 | { |
| 146 | return -1; |
| 147 | } |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | bool Serial::ReceiveExact(std::string& data, const size_t length, const unsigned int timeoutS, const unsigned int timeoutUS) |
| 152 | { |
| 153 | size_t startSize = data.length(); |
| 154 | size_t endSize = startSize + length; |
| 155 | while (endSize > data.length()) |
| 156 | { |
| 157 | bool ret = Receive(data, endSize - data.length(), timeoutS, timeoutUS); |
| 158 | if (ret == false) |
| 159 | { |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | ssize_t Serial::ReceiveExact(unsigned char* data, const size_t length, const unsigned int timeoutS, const unsigned int timeoutUS) |
| 167 | { |
| 168 | size_t actualSize = 0; |
| 169 | size_t endSize = length; |
| 170 | while (actualSize < endSize) |
| 171 | { |
| 172 | ssize_t ret = Receive(data + actualSize, endSize - actualSize, timeoutS, timeoutUS); |
| 173 | if (ret <= 0) |
| 174 | { |
| 175 | return actualSize; |
| 176 | } |
| 177 | actualSize += ret; |
| 178 | } |
| 179 | return actualSize; |
| 180 | } |
| 181 | } |