From aba6f3b99312c4f16e54e0127a93127d71baf6ee Mon Sep 17 00:00:00 2001 From: Kai Hermann Date: Mon, 11 Dec 2023 17:49:21 +0100 Subject: [PATCH] Fix potential crashes and fix TxProfile during transaction (#299) * Smart charging: set ignore_no_transaction in validate_profile call This was a bit too strict since it does reject some valid charging profiles (like TxProfiles that omit the transaction id) * Log an error if attempting to stop a transaction that is unknown to libocpp * Check if websocket is nullptr in authorize_id_token Signed-off-by: Kai-Uwe Hermann --- lib/ocpp/v16/charge_point_impl.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/ocpp/v16/charge_point_impl.cpp b/lib/ocpp/v16/charge_point_impl.cpp index 914423e11..138e8c534 100644 --- a/lib/ocpp/v16/charge_point_impl.cpp +++ b/lib/ocpp/v16/charge_point_impl.cpp @@ -1865,7 +1865,7 @@ void ChargePointImpl::handleSetChargingProfileRequest(ocpp::Callsmart_charging_handler->validate_profile( - profile, connector_id, false, this->configuration->getChargeProfileMaxStackLevel(), + profile, connector_id, true, this->configuration->getChargeProfileMaxStackLevel(), this->configuration->getMaxChargingProfilesInstalled(), this->configuration->getChargingScheduleMaxPeriods(), this->configuration->getChargingScheduleAllowedChargingRateUnitVector())) { @@ -2550,8 +2550,10 @@ IdTagInfo ChargePointImpl::authorize_id_token(CiString<20> idTag) { // - LocalPreAuthorize is true and CP is online // OR // - LocalAuthorizeOffline is true and CP is offline - if ((this->configuration->getLocalPreAuthorize() && this->websocket->is_connected()) || - (this->configuration->getLocalAuthorizeOffline() && !this->websocket->is_connected())) { + if ((this->configuration->getLocalPreAuthorize() && + (this->websocket != nullptr && this->websocket->is_connected())) || + (this->configuration->getLocalAuthorizeOffline() && + (this->websocket == nullptr || !this->websocket->is_connected()))) { if (this->configuration->getLocalAuthListEnabled()) { const auto auth_list_entry_opt = this->database_handler->get_local_authorization_list_entry(idTag); if (auth_list_entry_opt.has_value()) { @@ -3203,13 +3205,19 @@ void ChargePointImpl::on_transaction_stopped(const int32_t connector, const std: const Reason& reason, ocpp::DateTime timestamp, float energy_wh_import, std::optional> id_tag_end, std::optional signed_meter_value) { + auto transaction = this->transaction_handler->get_transaction(connector); + if (transaction == nullptr) { + EVLOG_error << "Trying to stop a transaction that is unknown on connector: " << connector + << ", with session_id: " << session_id; + return; + } if (signed_meter_value) { const auto meter_value = this->get_signed_meter_value(signed_meter_value.value(), ReadingContext::Transaction_End, timestamp); - this->transaction_handler->get_transaction(connector)->add_meter_value(meter_value); + transaction->add_meter_value(meter_value); } const auto stop_energy_wh = std::make_shared(timestamp, energy_wh_import); - this->transaction_handler->get_transaction(connector)->add_stop_energy_wh(stop_energy_wh); + transaction->add_stop_energy_wh(stop_energy_wh); this->status->submit_event(connector, FSMEvent::TransactionStoppedAndUserActionRequired, ocpp::DateTime()); this->stop_transaction(connector, reason, id_tag_end);