Skip to content

Commit

Permalink
Remove THROWS from (#713)
Browse files Browse the repository at this point in the history
- BSPs
- SerialCommHub
- System
- PacketSniffer
- Ev/EvseSlac

Signed-off-by: Cornelius Claussen <[email protected]>
Co-authored-by: Piet Gömpel <[email protected]>
  • Loading branch information
corneliusclaussen and Pietfried authored Jun 21, 2024
1 parent 6d67d7e commit bbce3e1
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 60 deletions.
4 changes: 4 additions & 0 deletions errors/evse_board_support.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 5 additions & 0 deletions errors/generic.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions interfaces/ev_board_support.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ vars:
BSP Measurements
type: object
$ref: /board_support_common#/BspMeasurement
errors:
- reference: /errors/generic
2 changes: 2 additions & 0 deletions interfaces/ev_slac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions interfaces/slac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions modules/EvSlac/main/ev_slacImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions modules/EvseManager/ErrorHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
3 changes: 2 additions & 1 deletion modules/EvseManager/ErrorHandling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ enum class BspErrors : std::uint8_t {
MREC25BrokenLatch,
MREC26CutCable,
VendorError,
last = VendorError
CommunicationFault,
last = CommunicationFault
};

enum class EvseManagerErrors : std::uint8_t {
Expand Down
7 changes: 5 additions & 2 deletions modules/EvseSlac/main/slacImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions modules/MicroMegaWattBSP/MicroMegaWattBSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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
11 changes: 8 additions & 3 deletions modules/MicroMegaWattBSP/umwc_comms/evSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions modules/MicroMegaWattBSP/umwc_comms/evSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class evSerial {
~evSerial();

bool openDevice(const char* device, int baud);
bool is_open() {
return fd > 0;
};

void readThread();
void run();
Expand Down
6 changes: 3 additions & 3 deletions modules/PacketSniffer/PacketSniffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 11 additions & 3 deletions modules/PhyVersoBSP/PhyVersoBSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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([&]() {
Expand All @@ -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
11 changes: 8 additions & 3 deletions modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions modules/PhyVersoBSP/phyverso_mcu_comms/evSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<tiny_modbus::Parity>(config.parity), config.rtscts,
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;
}
}

Expand Down
8 changes: 8 additions & 0 deletions modules/SerialCommHub/tiny_modbus_rtu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,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;
Expand Down Expand Up @@ -421,6 +425,10 @@ std::vector<uint16_t> TinyModbusRTU::txrx_impl(uint8_t device_address, FunctionC
uint16_t first_register_address, uint16_t register_quantity,
bool wait_for_reply, std::vector<uint16_t> 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))
Expand Down
10 changes: 9 additions & 1 deletion modules/System/main/systemImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]() {
Expand Down
17 changes: 10 additions & 7 deletions modules/YetiDriver/YetiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions modules/YetiDriver/board_support/evse_board_supportImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,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.";
}
}

Expand Down
10 changes: 7 additions & 3 deletions modules/YetiDriver/yeti_comms/evSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions modules/YetiDriver/yeti_comms/evSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class evSerial {
~evSerial();

bool openDevice(const char* device, int baud);
bool is_open() {
return fd > 0;
};

void readThread();
void run();
Expand Down
Loading

0 comments on commit bbce3e1

Please sign in to comment.