| 92 | } |
| 93 | |
| 94 | bool SerialEndpoint::write_data_serial(const std::vector<uint8_t>& data) { |
| 95 | // m_console->debug("Write data serial:{} bytes",data.size()); |
| 96 | if (m_fd == -1) { |
| 97 | // cannot send data at the time, UART not setup / doesn't exist. Limit |
| 98 | // message to once per second |
| 99 | if (!uart_log_warning_once) { |
| 100 | m_console->warn("Cannot send data, no fd"); |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | const auto before = std::chrono::steady_clock::now(); |
| 105 | // If we have a fd, but the write fails, most likely the UART disconnected |
| 106 | // but the linux driver hasn't noticed it yet. |
| 107 | const auto send_len = static_cast<int>(write(m_fd, data.data(), data.size())); |
| 108 | const auto send_delta = std::chrono::steady_clock::now() - before; |
| 109 | if (send_delta > std::chrono::milliseconds(100)) { |
| 110 | const auto send_delta_ms = |
| 111 | static_cast<float>( |
| 112 | std::chrono::duration_cast<std::chrono::microseconds>(send_delta) |
| 113 | .count()) / |
| 114 | 1000.0f; |
| 115 | m_console->warn("UART sending data took {}ms", send_delta_ms); |
| 116 | } |
| 117 | // m_console->debug("Written {} bytes",send_len); |
| 118 | // m_console->debug("{}",MEndpoint::get_tx_rx_stats()); |
| 119 | if (send_len != data.size()) { |
| 120 | m_n_failed_writes++; |
| 121 | const auto elapsed_since_last_log = |
| 122 | std::chrono::steady_clock::now() - m_last_log_serial_write_failed; |
| 123 | if (elapsed_since_last_log > |
| 124 | MIN_DELAY_BETWEEN_SERIAL_WRITE_FAILED_LOG_MESSAGES) { |
| 125 | m_console->warn("wrote {} instead of {} bytes,n failed:{}", send_len, |
| 126 | data.size(), m_n_failed_writes); |
| 127 | m_last_log_serial_write_failed = std::chrono::steady_clock::now(); |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | int SerialEndpoint::define_from_baudrate(int baudrate) { |
| 135 | switch (baudrate) { |