Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPP1.6: Fix handling of SetChargingProfile(TxProfile) #616

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ocpp/v16/charge_point_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ void ChargePointImpl::handleSetChargingProfileRequest(ocpp::Call<SetChargingProf
<< call.msg.csChargingProfiles.chargingProfilePurpose;
response.status = ChargingProfileStatus::Rejected;
} else if (this->smart_charging_handler->validate_profile(
profile, connector_id, true, this->configuration->getChargeProfileMaxStackLevel(),
profile, connector_id, false, this->configuration->getChargeProfileMaxStackLevel(),
this->configuration->getMaxChargingProfilesInstalled(),
this->configuration->getChargingScheduleMaxPeriods(),
this->configuration->getChargingScheduleAllowedChargingRateUnitVector())) {
Expand Down
28 changes: 16 additions & 12 deletions lib/ocpp/v16/smart_charging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ bool SmartChargingHandler::validate_profile(
profile.chargingSchedule.duration = max_recurrency_duration;
}
}
return true;
}

if (profile.chargingProfilePurpose == ChargingProfilePurposeType::ChargePointMaxProfile) {
if (connector_id == 0 and profile.chargingProfileKind != ChargingProfileKindType::Relative) {
return true;
Expand All @@ -365,24 +365,28 @@ bool SmartChargingHandler::validate_profile(
return true;
} else if (profile.chargingProfilePurpose == ChargingProfilePurposeType::TxProfile) {
if (connector_id == 0) {
EVLOG_info << "INVALID PROFILE - connector_id != 0 or no active transaction at this connector";
EVLOG_warning << "INVALID PROFILE - connector_id is 0";
return false;
}

if (!profile.transactionId.has_value() and ignore_no_transaction) {
return true;
}
const auto& connector = this->connectors.at(connector_id);

// transactionId is present so we need to check if a transaction exists
if (this->connectors.at(connector_id)->transaction == nullptr) {
EVLOG_info << "INVALID PROFILE - No active transaction at connector";
if (connector->transaction == nullptr && !ignore_no_transaction) {
EVLOG_warning << "INVALID PROFILE - No active transaction at this connector";
return false;
}

// check if the transactionId matches the active transaction
if (this->connectors.at(connector_id)->transaction->get_transaction_id() != profile.transactionId) {
EVLOG_info << "INVALID PROFILE - transaction_id doesnt match for purpose TxProfile";
return false;
if (profile.transactionId.has_value()) {
if (connector->transaction == nullptr) {
EVLOG_warning << "INVALID PROFILE - profile.transaction_id is present but no transaction is active at "
"this connector";
return false;
}

if (connector->transaction->get_transaction_id() != profile.transactionId) {
EVLOG_warning << "INVALID PROFILE - transaction_id doesn't match for purpose TxProfile";
return false;
}
}
}
return true;
Expand Down
114 changes: 113 additions & 1 deletion tests/lib/ocpp/v16/test_smart_charging_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace v16 {
* - PB04 Absolute ChargePointMaxProfile Profile with connector id 0
* - PB05 Absolute TxDefaultProfile
* - PB06 Absolute TxProfile ignore_no_transaction == true
* - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test
* - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches
*
* Negative Boundary Conditions:
* - NB01 Valid Profile, ConnectorID gt this->connectors.size()
Expand All @@ -40,6 +40,10 @@ namespace v16 {
* - NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule
* - NB11 Absolute ChargePointMaxProfile Profile with connector id not 0
* - NB12 Absolute TxProfile connector_id == 0
* - NB13 Absolute TxProfile && connector transaction == nullptr && ignore_no_transaction == true
* NB14 Absolute TxProfile && connector transactionId doesn't match && ignore_no_transaction == true
* NB15 Absolute TxProfile && connector transaction == nullptr && ignore_no_transaction == false
* NB16 Absolute TxProfile && connector transactionId doesn't match && ignore_no_transaction == false
*/
class ChargepointTestFixture : public testing::Test {
protected:
Expand Down Expand Up @@ -585,6 +589,27 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileIgnoreNoTransac
ASSERT_TRUE(sut);
}

/**
* PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches
*/
TEST_F(ChargepointTestFixture, ValidateProfile_AbsoluteTxProfileTransactionIdMatches__ReturnsTrue) {
auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A));
const std::vector<ChargingRateUnit>& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A};

addConnector(1);
auto handler = createSmartChargingHandler();

profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile.chargingProfileKind = ChargingProfileKindType::Absolute;
profile.transactionId = 1;
connectors.at(1)->transaction->set_transaction_id(1);
bool sut = handler->validate_profile(profile, connector_id, false, profile_max_stack_level,
max_charging_profiles_installed, charging_schedule_max_periods,
charging_schedule_allowed_charging_rate_units);

ASSERT_TRUE(sut);
}

/**
* NB12 Absolute TxProfile connector_id == 0
*/
Expand All @@ -602,6 +627,93 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileConnectorId0__R

ASSERT_FALSE(sut);
}

/**
* NB13 Absolute TxProfile && connector transaction == nullptr && ignore_no_transaction == true
*/

TEST_F(ChargepointTestFixture, ValidateProfile_AbsoluteTxProfileNoActiveTransactionIgnoreNoTransaction__ReturnsFalse) {
auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A));
const std::vector<ChargingRateUnit>& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A};

addConnector(1);
auto handler = createSmartChargingHandler();

profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile.chargingProfileKind = ChargingProfileKindType::Absolute;
profile.transactionId = 1;
connectors.at(1)->transaction = nullptr;
bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level,
max_charging_profiles_installed, charging_schedule_max_periods,
charging_schedule_allowed_charging_rate_units);

ASSERT_FALSE(sut);
}

/**
* NB14 Absolute TxProfile && connector transactionId doesn't match && ignore_no_transaction == true
*/

TEST_F(ChargepointTestFixture, ValidateProfile_AbsoluteTxProfileTransactionIdNoMatchIgnoreNoTransaction__ReturnsFalse) {
auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A));
const std::vector<ChargingRateUnit>& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A};

addConnector(1);
auto handler = createSmartChargingHandler();

profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile.chargingProfileKind = ChargingProfileKindType::Absolute;
profile.transactionId = 1;
bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level,
max_charging_profiles_installed, charging_schedule_max_periods,
charging_schedule_allowed_charging_rate_units);

ASSERT_FALSE(sut);
}

/**
* NB15 Absolute TxProfile && connector transaction == nullptr && ignore_no_transaction == false
*/

TEST_F(ChargepointTestFixture, ValidateProfile_AbsoluteTxProfileNoActiveTransaction__ReturnsFalse) {
auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A));
const std::vector<ChargingRateUnit>& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A};

addConnector(1);
auto handler = createSmartChargingHandler();

profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile.chargingProfileKind = ChargingProfileKindType::Absolute;
profile.transactionId = 1;
connectors.at(1)->transaction = nullptr;
bool sut = handler->validate_profile(profile, connector_id, false, profile_max_stack_level,
max_charging_profiles_installed, charging_schedule_max_periods,
charging_schedule_allowed_charging_rate_units);

ASSERT_FALSE(sut);
}

/**
* NB16 Absolute TxProfile && connector transactionId doesn't match && ignore_no_transaction == false
*/

TEST_F(ChargepointTestFixture, ValidateProfile_AbsoluteTxProfileTransactionIdNoMatch__ReturnsFalse) {
auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A));
const std::vector<ChargingRateUnit>& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A};

addConnector(1);
auto handler = createSmartChargingHandler();

profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile;
profile.chargingProfileKind = ChargingProfileKindType::Absolute;
profile.transactionId = 1;
bool sut = handler->validate_profile(profile, connector_id, false, profile_max_stack_level,
max_charging_profiles_installed, charging_schedule_max_periods,
charging_schedule_allowed_charging_rate_units);

ASSERT_FALSE(sut);
}

/**
*
* 2. Testing the branches within ClearAllProfilesWithFilter
Expand Down
Loading