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

Harden against invalid connectorId for setChargingProfile/clearChargingProfile. #914

Closed
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
95 changes: 56 additions & 39 deletions lib/ocpp/v16/charge_point_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2293,34 +2293,41 @@ void ChargePointImpl::handleSetChargingProfileRequest(ocpp::Call<SetChargingProf
const int connector_id = call.msg.connectorId;

const auto supported_purpose_types = this->configuration->getSupportedChargingProfilePurposeTypes();
if (std::find(supported_purpose_types.begin(), supported_purpose_types.end(),
call.msg.csChargingProfiles.chargingProfilePurpose) == supported_purpose_types.end()) {
EVLOG_warning << "Rejecting SetChargingProfileRequest because purpose type is not supported: "
<< call.msg.csChargingProfiles.chargingProfilePurpose;
response.status = ChargingProfileStatus::Rejected;
} else if (this->smart_charging_handler->validate_profile(
profile, connector_id, false, this->configuration->getChargeProfileMaxStackLevel(),
this->configuration->getMaxChargingProfilesInstalled(),
this->configuration->getChargingScheduleMaxPeriods(),
this->configuration->getChargingScheduleAllowedChargingRateUnitVector())) {
response.status = ChargingProfileStatus::Accepted;
// If a charging profile with the same chargingProfileId, or the same combination of stackLevel /
// ChargingProfilePurpose, exists on the Charge Point, the new charging profile SHALL replace the
// existing charging profile, otherwise it SHALL be added.
this->smart_charging_handler->clear_all_profiles_with_filter(profile.chargingProfileId, std::nullopt,
std::nullopt, std::nullopt, true);
if (profile.chargingProfilePurpose == ChargingProfilePurposeType::ChargePointMaxProfile) {
this->smart_charging_handler->add_charge_point_max_profile(profile);
} else if (profile.chargingProfilePurpose == ChargingProfilePurposeType::TxDefaultProfile) {
this->smart_charging_handler->add_tx_default_profile(profile, connector_id);
} else if (profile.chargingProfilePurpose == ChargingProfilePurposeType::TxProfile) {
this->smart_charging_handler->add_tx_profile(profile, connector_id);
}
response.status = ChargingProfileStatus::Accepted;
} else {
response.status = ChargingProfileStatus::Rejected;
}

bool connectorIdInValidRange = true;
if (connector_id > this->configuration->getNumberOfConnectors() or connector_id < 0) {
connectorIdInValidRange = false;
}

if (connectorIdInValidRange) {
if (std::find(supported_purpose_types.begin(), supported_purpose_types.end(),
call.msg.csChargingProfiles.chargingProfilePurpose) == supported_purpose_types.end()) {
EVLOG_warning << "Rejecting SetChargingProfileRequest because purpose type is not supported: "
<< call.msg.csChargingProfiles.chargingProfilePurpose;
response.status = ChargingProfileStatus::Rejected;
} else if (this->smart_charging_handler->validate_profile(
profile, connector_id, false, this->configuration->getChargeProfileMaxStackLevel(),
this->configuration->getMaxChargingProfilesInstalled(),
this->configuration->getChargingScheduleMaxPeriods(),
this->configuration->getChargingScheduleAllowedChargingRateUnitVector())) {
response.status = ChargingProfileStatus::Accepted;
// If a charging profile with the same chargingProfileId, or the same combination of stackLevel /
// ChargingProfilePurpose, exists on the Charge Point, the new charging profile SHALL replace the
// existing charging profile, otherwise it SHALL be added.
this->smart_charging_handler->clear_all_profiles_with_filter(profile.chargingProfileId, std::nullopt,
std::nullopt, std::nullopt, true);
if (profile.chargingProfilePurpose == ChargingProfilePurposeType::ChargePointMaxProfile) {
this->smart_charging_handler->add_charge_point_max_profile(profile);
} else if (profile.chargingProfilePurpose == ChargingProfilePurposeType::TxDefaultProfile) {
this->smart_charging_handler->add_tx_default_profile(profile, connector_id);
} else if (profile.chargingProfilePurpose == ChargingProfilePurposeType::TxProfile) {
this->smart_charging_handler->add_tx_profile(profile, connector_id);
}
response.status = ChargingProfileStatus::Accepted;
} else {
response.status = ChargingProfileStatus::Rejected;
}
}
ocpp::CallResult<SetChargingProfileResponse> call_result(response, call.uniqueId);
this->message_dispatcher->dispatch_call_result(call_result);

Expand Down Expand Up @@ -2379,18 +2386,28 @@ void ChargePointImpl::handleClearChargingProfileRequest(ocpp::Call<ClearCharging
ClearChargingProfileResponse response;
response.status = ClearChargingProfileStatus::Unknown;

// clear all charging profiles
if (!call.msg.id && !call.msg.connectorId && !call.msg.chargingProfilePurpose && !call.msg.stackLevel) {
this->smart_charging_handler->clear_all_profiles();
response.status = ClearChargingProfileStatus::Accepted;
} else if (call.msg.id &&
this->smart_charging_handler->clear_all_profiles_with_filter(
call.msg.id, call.msg.connectorId, call.msg.stackLevel, call.msg.chargingProfilePurpose, true)) {
response.status = ClearChargingProfileStatus::Accepted;
} else if (!call.msg.id and
this->smart_charging_handler->clear_all_profiles_with_filter(
std::nullopt, call.msg.connectorId, call.msg.stackLevel, call.msg.chargingProfilePurpose, false)) {
response.status = ClearChargingProfileStatus::Accepted;
bool connectorIdInValidRange = true;
if (call.msg.connectorId.has_value()) {
const int connector_id = call.msg.connectorId.value();
if (connector_id > this->configuration->getNumberOfConnectors() or connector_id < 0) {
connectorIdInValidRange = false;
}
}

if (connectorIdInValidRange) {
// clear all charging profiles
if (!call.msg.id && !call.msg.connectorId && !call.msg.chargingProfilePurpose && !call.msg.stackLevel) {
this->smart_charging_handler->clear_all_profiles();
response.status = ClearChargingProfileStatus::Accepted;
} else if (call.msg.id &&
this->smart_charging_handler->clear_all_profiles_with_filter(
call.msg.id, call.msg.connectorId, call.msg.stackLevel, call.msg.chargingProfilePurpose, true)) {
response.status = ClearChargingProfileStatus::Accepted;
} else if (!call.msg.id and this->smart_charging_handler->clear_all_profiles_with_filter(
std::nullopt, call.msg.connectorId, call.msg.stackLevel,
call.msg.chargingProfilePurpose, false)) {
response.status = ClearChargingProfileStatus::Accepted;
}
}

ocpp::CallResult<ClearChargingProfileResponse> call_result(response, call.uniqueId);
Expand Down
Loading