From 0d877b3fcba555fc9e7ecad53967cba5830a80f7 Mon Sep 17 00:00:00 2001 From: Cornelius Claussen Date: Fri, 7 Jun 2024 12:55:27 +0200 Subject: [PATCH] Remove THROWS from - BSPs - SerialCommHub - System - PacketSniffer - Ev/EvseSlac Signed-off-by: Cornelius Claussen --- errors/evse_board_support.yaml | 4 +++ errors/generic.yaml | 5 ++++ interfaces/ev_board_support.yaml | 2 ++ interfaces/ev_slac.yaml | 2 ++ interfaces/slac.yaml | 2 ++ modules/EvSlac/main/ev_slacImpl.cpp | 7 +++-- modules/EvseManager/ErrorHandling.cpp | 6 +++++ modules/EvseManager/ErrorHandling.hpp | 3 ++- modules/EvseSlac/main/slacImpl.cpp | 7 +++-- modules/MicroMegaWattBSP/MicroMegaWattBSP.cpp | 17 +++++++----- .../MicroMegaWattBSP/umwc_comms/evSerial.cpp | 11 +++++--- .../MicroMegaWattBSP/umwc_comms/evSerial.h | 3 +++ modules/PacketSniffer/PacketSniffer.cpp | 6 ++--- modules/PhyVersoBSP/PhyVersoBSP.cpp | 14 +++++++--- .../phyverso_mcu_comms/evSerial.cpp | 11 +++++--- .../PhyVersoBSP/phyverso_mcu_comms/evSerial.h | 3 +++ .../main/serial_communication_hubImpl.cpp | 2 +- modules/SerialCommHub/tiny_modbus_rtu.cpp | 8 ++++++ modules/System/main/systemImpl.cpp | 10 ++++++- modules/YetiDriver/YetiDriver.cpp | 17 +++++++----- .../board_support/evse_board_supportImpl.cpp | 3 +-- modules/YetiDriver/yeti_comms/evSerial.cpp | 10 ++++--- modules/YetiDriver/yeti_comms/evSerial.h | 3 +++ modules/YetiEvDriver/YetiEvDriver.cpp | 13 +++++----- .../YetiEvDriver/evyeti_comms/evSerial.cpp | 11 +++++--- modules/YetiEvDriver/evyeti_comms/evSerial.h | 26 ++++++++++--------- 26 files changed, 146 insertions(+), 60 deletions(-) create mode 100644 errors/generic.yaml diff --git a/errors/evse_board_support.yaml b/errors/evse_board_support.yaml index c577ef2650..df0a1be64c 100644 --- a/errors/evse_board_support.yaml +++ b/errors/evse_board_support.yaml @@ -54,3 +54,7 @@ errors: - name: VendorWarning description: >- Vendor specific error code. Charging may continue. + - name: CommunicationFault + description: >- + The communication to the hardware or underlying driver is lost or has errors. + diff --git a/errors/generic.yaml b/errors/generic.yaml new file mode 100644 index 0000000000..3e260f1cc6 --- /dev/null +++ b/errors/generic.yaml @@ -0,0 +1,5 @@ +description: >- + Generic errors used by multiple modules +errors: + - name: CommunicationFault + description: Communication with the underlying hardware or device has a fault diff --git a/interfaces/ev_board_support.yaml b/interfaces/ev_board_support.yaml index 438046af9e..33f551c976 100644 --- a/interfaces/ev_board_support.yaml +++ b/interfaces/ev_board_support.yaml @@ -56,3 +56,5 @@ vars: BSP Measurements type: object $ref: /board_support_common#/BspMeasurement +errors: + - reference: /errors/generic \ No newline at end of file diff --git a/interfaces/ev_slac.yaml b/interfaces/ev_slac.yaml index 4b711738bb..388d98fb27 100644 --- a/interfaces/ev_slac.yaml +++ b/interfaces/ev_slac.yaml @@ -27,3 +27,5 @@ vars: Inform higher layers about the MAC address of the charging connector type: string pattern: ^[A-F0-9]{2}(:[A-F0-9]{2}){5}$ +errors: + - reference: /errors/generic \ No newline at end of file diff --git a/interfaces/slac.yaml b/interfaces/slac.yaml index c36a47d189..c8c61df177 100644 --- a/interfaces/slac.yaml +++ b/interfaces/slac.yaml @@ -58,3 +58,5 @@ vars: Inform higher layers about the MAC address of the vehicle (upper case) type: string pattern: ^[A-F0-9]{2}(:[A-F0-9]{2}){5}$ +errors: + - reference: /errors/generic \ No newline at end of file diff --git a/modules/EvSlac/main/ev_slacImpl.cpp b/modules/EvSlac/main/ev_slacImpl.cpp index cb14a0d757..70a8edc97e 100644 --- a/modules/EvSlac/main/ev_slacImpl.cpp +++ b/modules/EvSlac/main/ev_slacImpl.cpp @@ -33,8 +33,11 @@ void ev_slacImpl::run() { try { slac_io.init(config.device); } catch (const std::exception& e) { - EVLOG_AND_THROW(Everest::EverestBaseRuntimeError( - fmt::format("Couldn't open device {} for SLAC communication. Reason: {}", config.device, e.what()))); + EVLOG_error << fmt::format("Couldn't open device {} for SLAC communication. Reason: {}", config.device, + e.what()); + raise_error( + error_factory->create_error("generic/CommunicationFault", "", "Could not open device " + config.device)); + return; } // setup callbacks diff --git a/modules/EvseManager/ErrorHandling.cpp b/modules/EvseManager/ErrorHandling.cpp index 2239fcc96f..6907669ec1 100644 --- a/modules/EvseManager/ErrorHandling.cpp +++ b/modules/EvseManager/ErrorHandling.cpp @@ -442,6 +442,12 @@ bool ErrorHandling::modify_error_bsp(const Everest::error::Error& error, bool ac if (hlc && active) { r_hlc[0]->call_send_error(types::iso15118_charger::EvseError::Error_Malfunction); } + } else if (error_type == "evse_board_support/CommunicationFault") { + active_errors.bsp.set(BspErrors::CommunicationFault, active); + evse_error = types::evse_manager::ErrorEnum::VendorError; + if (hlc && active) { + r_hlc[0]->call_send_error(types::iso15118_charger::EvseError::Error_Malfunction); + } } else { // Errors that do not stop charging if (error_type == "evse_board_support/MREC3HighTemperature") { diff --git a/modules/EvseManager/ErrorHandling.hpp b/modules/EvseManager/ErrorHandling.hpp index 1fc06a12c6..38b77daae2 100644 --- a/modules/EvseManager/ErrorHandling.hpp +++ b/modules/EvseManager/ErrorHandling.hpp @@ -63,7 +63,8 @@ enum class BspErrors : std::uint8_t { MREC25BrokenLatch, MREC26CutCable, VendorError, - last = VendorError + CommunicationFault, + last = CommunicationFault }; enum class EvseManagerErrors : std::uint8_t { diff --git a/modules/EvseSlac/main/slacImpl.cpp b/modules/EvseSlac/main/slacImpl.cpp index d9c40bd0bd..945e8400a4 100644 --- a/modules/EvseSlac/main/slacImpl.cpp +++ b/modules/EvseSlac/main/slacImpl.cpp @@ -44,8 +44,11 @@ void slacImpl::run() { try { slac_io.init(config.device); } catch (const std::exception& e) { - EVLOG_AND_THROW(Everest::EverestBaseRuntimeError( - fmt::format("Couldn't open device {} for SLAC communication. Reason: {}", config.device, e.what()))); + EVLOG_error << fmt::format("Couldn't open device {} for SLAC communication. Reason: {}", config.device, + e.what()); + raise_error( + error_factory->create_error("generic/CommunicationFault", "", "Could not open device " + config.device)); + return; } // setup callbacks diff --git a/modules/MicroMegaWattBSP/MicroMegaWattBSP.cpp b/modules/MicroMegaWattBSP/MicroMegaWattBSP.cpp index fe01d1cc51..b0848ce686 100644 --- a/modules/MicroMegaWattBSP/MicroMegaWattBSP.cpp +++ b/modules/MicroMegaWattBSP/MicroMegaWattBSP.cpp @@ -7,8 +7,7 @@ namespace module { void MicroMegaWattBSP::init() { // initialize serial driver if (!serial.openDevice(config.serial_port.c_str(), config.baud_rate)) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestConfigError, "Could not open serial port ", config.serial_port, - " with baud rate ", config.baud_rate)); + EVLOG_error << "Could not open serial port " << config.serial_port << " with baud rate " << config.baud_rate; return; } @@ -21,13 +20,11 @@ void MicroMegaWattBSP::ready() { serial.run(); if (!serial.reset(config.reset_gpio)) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "uMWC reset not successful.")); + EVLOG_error << "uMWC reset not successful."; } - serial.signalSpuriousReset.connect( - [this]() { EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "uMWC uC spurious reset!")); }); - serial.signalConnectionTimeout.connect( - [this]() { EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "uMWC UART timeout!")); }); + serial.signalSpuriousReset.connect([this]() { EVLOG_error << "uMWC uC spurious reset!"; }); + serial.signalConnectionTimeout.connect([this]() { EVLOG_error << "uMWC UART timeout!"; }); serial.signalTelemetry.connect([this](Telemetry t) { mqtt.publish("everest_external/umwc/cp_hi", t.cp_hi); @@ -41,6 +38,12 @@ void MicroMegaWattBSP::ready() { invoke_ready(*p_powermeter); invoke_ready(*p_board_support); invoke_ready(*p_dc_supply); + + if (not serial.is_open()) { + auto err = p_board_support->error_factory->create_error("evse_board_support/CommunicationFault", "", + "Could not open serial port."); + p_board_support->raise_error(err); + } } } // namespace module diff --git a/modules/MicroMegaWattBSP/umwc_comms/evSerial.cpp b/modules/MicroMegaWattBSP/umwc_comms/evSerial.cpp index bfff5ae4bc..810e6728ab 100644 --- a/modules/MicroMegaWattBSP/umwc_comms/evSerial.cpp +++ b/modules/MicroMegaWattBSP/umwc_comms/evSerial.cpp @@ -248,13 +248,18 @@ void evSerial::readThread() { while (true) { if (readThreadHandle.shouldExit()) break; - int n = read(fd, buf, sizeof buf); - // printf ("read %u bytes.\n", n); - cobsDecode(buf, n); + if (fd > 0) { + int n = read(fd, buf, sizeof buf); + cobsDecode(buf, n); + } } } bool evSerial::linkWrite(EverestToMcu* m) { + if (fd <= 0) { + return false; + } + uint8_t tx_packet_buf[1024]; uint8_t encode_buf[1500]; pb_ostream_t ostream = pb_ostream_from_buffer(tx_packet_buf, sizeof(tx_packet_buf) - 4); diff --git a/modules/MicroMegaWattBSP/umwc_comms/evSerial.h b/modules/MicroMegaWattBSP/umwc_comms/evSerial.h index befa7d3926..741416d4c7 100644 --- a/modules/MicroMegaWattBSP/umwc_comms/evSerial.h +++ b/modules/MicroMegaWattBSP/umwc_comms/evSerial.h @@ -18,6 +18,9 @@ class evSerial { ~evSerial(); bool openDevice(const char* device, int baud); + bool is_open() { + return fd > 0; + }; void readThread(); void run(); diff --git a/modules/PacketSniffer/PacketSniffer.cpp b/modules/PacketSniffer/PacketSniffer.cpp index 593ab861c5..2bfa6ce6b1 100644 --- a/modules/PacketSniffer/PacketSniffer.cpp +++ b/modules/PacketSniffer/PacketSniffer.cpp @@ -17,13 +17,13 @@ void PacketSniffer::init() { p_handle = pcap_open_live(config.device.c_str(), BUFFERSIZE, PROMISC_MODE, PACKET_BUFFER_TIMEOUT_MS, errbuf); if (p_handle == nullptr) { - EVLOG_AND_THROW(Everest::EverestConfigError(fmt::format("Could not open device {}", config.device))); + EVLOG_error << fmt::format("Could not open device {}. Sniffing disabled.", config.device); return; } if (pcap_datalink(p_handle) != DLT_EN10MB) { - EVLOG_AND_THROW(Everest::EverestConfigError( - fmt::format("Device {} doesn't provide Ethernet headers - not supported", config.device))); + EVLOG_error << fmt::format("Device {} doesn't provide Ethernet headers - not supported. Sniffing disabled.", + config.device); return; } diff --git a/modules/PhyVersoBSP/PhyVersoBSP.cpp b/modules/PhyVersoBSP/PhyVersoBSP.cpp index 2bcc83d082..dd75709b59 100644 --- a/modules/PhyVersoBSP/PhyVersoBSP.cpp +++ b/modules/PhyVersoBSP/PhyVersoBSP.cpp @@ -9,8 +9,7 @@ namespace module { void PhyVersoBSP::init() { // initialize serial driver if (!serial.open_device(config.serial_port.c_str(), config.baud_rate)) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestConfigError, "Could not open serial port ", config.serial_port, - " with baud rate ", config.baud_rate)); + EVLOG_error << "Could not open serial port " << config.serial_port << " with baud rate " << config.baud_rate; return; } @@ -32,7 +31,7 @@ void PhyVersoBSP::init() { } if (!verso_config.open_file(mcu_config_file.string())) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestConfigError, "Could not open config file ", mcu_config_file)); + EVLOG_error << "Could not open config file " << mcu_config_file; } serial.signal_config_request.connect([&]() { @@ -53,6 +52,15 @@ void PhyVersoBSP::ready() { invoke_ready(*p_phyverso_mcu_temperature); invoke_ready(*p_system_specific_data_1); invoke_ready(*p_system_specific_data_2); + + if (not serial.is_open()) { + auto err = p_connector_1->error_factory->create_error("evse_board_support/CommunicationFault", "", + "Could not open serial port."); + p_connector_1->raise_error(err); + err = p_connector_2->error_factory->create_error("evse_board_support/CommunicationFault", "", + "Could not open serial port."); + p_connector_2->raise_error(err); + } } } // namespace module diff --git a/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.cpp b/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.cpp index 95137cdf0c..920ae5f5e8 100644 --- a/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.cpp +++ b/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.cpp @@ -308,13 +308,18 @@ void evSerial::read_thread() { while (true) { if (read_thread_handle.shouldExit()) break; - n = read(fd, buf, sizeof buf); - // printf ("read %u bytes.\n", n); - cobs_decode(buf, n); + if (fd > 0) { + n = read(fd, buf, sizeof buf); + cobs_decode(buf, n); + } } } bool evSerial::link_write(EverestToMcu* m) { + if (fd <= 0) { + return false; + } + uint8_t tx_packet_buf[1024]; uint8_t encode_buf[1500]; pb_ostream_t ostream = pb_ostream_from_buffer(tx_packet_buf, sizeof(tx_packet_buf) - 4); diff --git a/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.h b/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.h index 96ca5c6540..e2823f4779 100644 --- a/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.h +++ b/modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.h @@ -88,6 +88,9 @@ class evSerial { ~evSerial(); bool open_device(const char* device, int baud); + bool is_open() { + return fd > 0; + }; void read_thread(); void run(); diff --git a/modules/SerialCommHub/main/serial_communication_hubImpl.cpp b/modules/SerialCommHub/main/serial_communication_hubImpl.cpp index 9848fc5ed6..f930968f63 100644 --- a/modules/SerialCommHub/main/serial_communication_hubImpl.cpp +++ b/modules/SerialCommHub/main/serial_communication_hubImpl.cpp @@ -41,7 +41,7 @@ void serial_communication_hubImpl::init() { if (!modbus.open_device(config.serial_port, config.baudrate, config.ignore_echo, rxtx_gpio_settings, static_cast(config.parity), milliseconds(config.initial_timeout_ms), milliseconds(config.within_message_timeout_ms))) { - EVLOG_AND_THROW(Everest::EverestConfigError(fmt::format("Cannot open serial port {}.", config.serial_port))); + EVLOG_error << "Cannot open serial port {}, ModBus will not work.", config.serial_port; } } diff --git a/modules/SerialCommHub/tiny_modbus_rtu.cpp b/modules/SerialCommHub/tiny_modbus_rtu.cpp index fb5de291a3..9b4e610218 100644 --- a/modules/SerialCommHub/tiny_modbus_rtu.cpp +++ b/modules/SerialCommHub/tiny_modbus_rtu.cpp @@ -274,6 +274,10 @@ bool TinyModbusRTU::open_device(const std::string& device, int _baud, bool _igno } int TinyModbusRTU::read_reply(uint8_t* rxbuf, int rxbuf_len) { + if (fd <= 0) { + return 0; + } + // Lambda to convert std::chrono to timeval. auto to_timeval = [](const auto& time) { using namespace std::chrono; @@ -416,6 +420,10 @@ std::vector TinyModbusRTU::txrx_impl(uint8_t device_address, FunctionC uint16_t first_register_address, uint16_t register_quantity, bool wait_for_reply, std::vector request) { { + if (fd <= 0) { + return {}; + } + auto req = function == FunctionCode::WRITE_SINGLE_HOLDING_REGISTER ? _make_single_write_request(device_address, first_register_address, wait_for_reply, request.at(0)) diff --git a/modules/System/main/systemImpl.cpp b/modules/System/main/systemImpl.cpp index ecb34cade6..18057853e1 100644 --- a/modules/System/main/systemImpl.cpp +++ b/modules/System/main/systemImpl.cpp @@ -36,7 +36,8 @@ fs::path create_temp_file(const fs::path& dir, const std::string& prefix) { auto fd = mkstemp(fn_template_buffer.data()); if (fd == -1) { - EVLOG_AND_THROW(Everest::EverestBaseRuntimeError("Failed to create temporary file at: " + fn_template)); + EVLOG_error << "Failed to create temporary file at: " << fn_template; + return {}; } // close the file descriptor @@ -64,6 +65,13 @@ void systemImpl::standard_firmware_update(const types::system::FirmwareUpdateReq const auto date_time = Everest::Date::to_rfc3339(date::utc_clock::now()); const auto firmware_file_path = create_temp_file(fs::temp_directory_path(), "firmware-" + date_time); + + if (firmware_file_path.empty()) { + EVLOG_error << "Firmware update ignored, cannot write temporary file."; + publish_firmware_update_status({types::system::FirmwareUpdateStatusEnum::DownloadFailed}); + return; + } + const auto constants = this->scripts_path / CONSTANTS; this->update_firmware_thread = std::thread([this, firmware_update_request, firmware_file_path, constants]() { diff --git a/modules/YetiDriver/YetiDriver.cpp b/modules/YetiDriver/YetiDriver.cpp index 33267b7190..bd0e4d3147 100644 --- a/modules/YetiDriver/YetiDriver.cpp +++ b/modules/YetiDriver/YetiDriver.cpp @@ -10,8 +10,7 @@ void YetiDriver::init() { // initialize serial driver if (!serial.openDevice(config.serial_port.c_str(), config.baud_rate)) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestConfigError, "Could not open serial port ", config.serial_port, - " with baud rate ", config.baud_rate)); + EVLOG_error << "Could not open serial port " << config.serial_port << " with baud rate " << config.baud_rate; return; } @@ -69,13 +68,11 @@ void YetiDriver::ready() { serial.run(); if (!serial.reset(config.reset_gpio_chip, config.reset_gpio)) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "Yeti reset not successful.")); + EVLOG_error << "Yeti reset not successful."; } - serial.signalSpuriousReset.connect( - [this]() { EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "Yeti uC spurious reset!")); }); - serial.signalConnectionTimeout.connect( - [this]() { EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "Yeti UART timeout!")); }); + serial.signalSpuriousReset.connect([this]() { EVLOG_error << "Yeti uC spurious reset!"; }); + serial.signalConnectionTimeout.connect([this]() { EVLOG_error << "Yeti UART timeout!"; }); invoke_ready(*p_powermeter); invoke_ready(*p_board_support); @@ -96,6 +93,12 @@ void YetiDriver::ready() { }); serial.signalErrorFlags.connect([this](ErrorFlags e) { error_handling(e); }); + + if (not serial.is_open()) { + auto err = p_board_support->error_factory->create_error("evse_board_support/CommunicationFault", "", + "Could not open serial port."); + p_board_support->raise_error(err); + } } void YetiDriver::publish_external_telemetry_livedata(const std::string& topic, const Everest::TelemetryMap& data) { diff --git a/modules/YetiDriver/board_support/evse_board_supportImpl.cpp b/modules/YetiDriver/board_support/evse_board_supportImpl.cpp index f33fec3f3f..aa1d279c1d 100644 --- a/modules/YetiDriver/board_support/evse_board_supportImpl.cpp +++ b/modules/YetiDriver/board_support/evse_board_supportImpl.cpp @@ -147,8 +147,7 @@ void evse_board_supportImpl::wait_for_caps() { std::this_thread::sleep_for(std::chrono::milliseconds(100)); } if (i == 50) { - EVLOG_AND_THROW( - Everest::EverestTimeoutError("Did not receive hardware capabilities from Yeti hardware, exiting.")); + EVLOG_error << "Did not receive hardware capabilities from Yeti hardware, using defaults."; } } diff --git a/modules/YetiDriver/yeti_comms/evSerial.cpp b/modules/YetiDriver/yeti_comms/evSerial.cpp index c19d6b92fe..fa84608b5c 100644 --- a/modules/YetiDriver/yeti_comms/evSerial.cpp +++ b/modules/YetiDriver/yeti_comms/evSerial.cpp @@ -248,13 +248,17 @@ void evSerial::readThread() { while (true) { if (readThreadHandle.shouldExit()) break; - n = read(fd, buf, sizeof buf); - // printf ("read %u bytes.\n", n); - cobsDecode(buf, n); + if (fd > 0) { + n = read(fd, buf, sizeof buf); + cobsDecode(buf, n); + } } } bool evSerial::linkWrite(EverestToMcu* m) { + if (fd <= 0) { + return false; + } uint8_t tx_packet_buf[1024]; uint8_t encode_buf[1500]; pb_ostream_t ostream = pb_ostream_from_buffer(tx_packet_buf, sizeof(tx_packet_buf) - 4); diff --git a/modules/YetiDriver/yeti_comms/evSerial.h b/modules/YetiDriver/yeti_comms/evSerial.h index d668569f77..675751713e 100644 --- a/modules/YetiDriver/yeti_comms/evSerial.h +++ b/modules/YetiDriver/yeti_comms/evSerial.h @@ -18,6 +18,9 @@ class evSerial { ~evSerial(); bool openDevice(const char* device, int baud); + bool is_open() { + return fd > 0; + }; void readThread(); void run(); diff --git a/modules/YetiEvDriver/YetiEvDriver.cpp b/modules/YetiEvDriver/YetiEvDriver.cpp index adbe0c39ac..b2af7c2442 100644 --- a/modules/YetiEvDriver/YetiEvDriver.cpp +++ b/modules/YetiEvDriver/YetiEvDriver.cpp @@ -8,8 +8,9 @@ void YetiEvDriver::init() { // initialize serial driver if (!serial.openDevice(config.serial_port.c_str(), config.baud_rate)) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestConfigError, "Could not open serial port ", config.serial_port, - " with baud rate ", config.baud_rate)); + EVLOG_error << "Could not open serial port " << config.serial_port << " with baud rate " << config.baud_rate; + p_ev_board_support->raise_error(p_ev_board_support->error_factory->create_error( + "generic/CommunicationFault", "", "Could not open serial port")); return; } @@ -20,13 +21,11 @@ void YetiEvDriver::ready() { serial.run(); if (!serial.reset(config.reset_gpio)) { - EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "EVYeti reset not successful.")); + EVLOG_error << "EVYeti reset not successful."; } - serial.signalSpuriousReset.connect( - [this]() { EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "EVYeti uC spurious reset!")); }); - serial.signalConnectionTimeout.connect( - [this]() { EVLOG_AND_THROW(EVEXCEPTION(Everest::EverestInternalError, "EVYeti UART timeout!")); }); + serial.signalSpuriousReset.connect([this]() { EVLOG_error << "EVYeti uC spurious reset!"; }); + serial.signalConnectionTimeout.connect([this]() { EVLOG_error << "EVYeti UART timeout!"; }); serial.signalEvent.connect([this](Event e) { types::board_support_common::BspEvent bspe; diff --git a/modules/YetiEvDriver/evyeti_comms/evSerial.cpp b/modules/YetiEvDriver/evyeti_comms/evSerial.cpp index 4bdabf331a..99a29c959a 100644 --- a/modules/YetiEvDriver/evyeti_comms/evSerial.cpp +++ b/modules/YetiEvDriver/evyeti_comms/evSerial.cpp @@ -235,13 +235,18 @@ void evSerial::readThread() { while (true) { if (readThreadHandle.shouldExit()) break; - n = read(fd, buf, sizeof buf); - // printf ("read %u bytes.\n", n); - cobsDecode(buf, n); + if (fd > 0) { + n = read(fd, buf, sizeof buf); + cobsDecode(buf, n); + } } } bool evSerial::linkWrite(HiToLo* m) { + if (fd <= 0) { + return false; + } + uint8_t tx_packet_buf[1024]; uint8_t encode_buf[1500]; pb_ostream_t ostream = pb_ostream_from_buffer(tx_packet_buf, sizeof(tx_packet_buf) - 4); diff --git a/modules/YetiEvDriver/evyeti_comms/evSerial.h b/modules/YetiEvDriver/evyeti_comms/evSerial.h index e1a9f683bd..4445992e2c 100644 --- a/modules/YetiEvDriver/evyeti_comms/evSerial.h +++ b/modules/YetiEvDriver/evyeti_comms/evSerial.h @@ -3,22 +3,25 @@ #ifndef YETI_SERIAL #define YETI_SERIAL -#include -#include -#include -#include -#include "lo2hi.pb.h" #include "hi2lo.pb.h" +#include "lo2hi.pb.h" #include #include +#include +#include +#include +#include class evSerial { - public: +public: evSerial(); ~evSerial(); bool openDevice(const char* device, int baud); + bool is_open() { + return fd > 0; + }; void readThread(); void run(); @@ -39,8 +42,7 @@ class evSerial { sigslot::signal<> signalSpuriousReset; sigslot::signal<> signalConnectionTimeout; - private: - +private: // Serial interface bool setSerialAttributes(); int fd; @@ -51,18 +53,18 @@ class evSerial { void handlePacket(uint8_t* buf, int len); void cobsDecode(uint8_t* buf, int len); void cobsDecodeByte(uint8_t byte); - size_t cobsEncode(const void *data, size_t length, uint8_t *buffer); + size_t cobsEncode(const void* data, size_t length, uint8_t* buffer); uint8_t msg[2048]; uint8_t code; uint8_t block; - uint8_t *decode; - uint32_t crc32(uint8_t *buf, int len); + uint8_t* decode; + uint32_t crc32(uint8_t* buf, int len); // Read thread for serial port Everest::Thread readThreadHandle; Everest::Thread timeoutDetectionThreadHandle; - bool linkWrite(HiToLo *m); + bool linkWrite(HiToLo* m); volatile bool reset_done_flag; volatile bool forced_reset;