Skip to content

Commit

Permalink
OCPP&OCPP201: explicitly handle all cases in switch statements
Browse files Browse the repository at this point in the history
Remove default: statements, turn on compiler error on enum conversion switch-case statements. This ensures that all cases are explicitly handled.
This should also ensure that throw statements in the conversions functions are never be reached, they remain to prevent "control reaches end of non-void function" compiler warnings

Signed-off-by: Kai-Uwe Hermann <[email protected]>
  • Loading branch information
hikinggrass committed Sep 25, 2024
1 parent ce27c79 commit f42780c
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 176 deletions.
12 changes: 12 additions & 0 deletions lib/staging/ocpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ target_include_directories(ocpp_evse_security
"$<TARGET_PROPERTY:generate_cpp_files,EVEREST_GENERATED_INCLUDE_DIR>"
)

target_compile_options(ocpp_evse_security
PRIVATE
-Wimplicit-fallthrough
-Werror=switch-enum
)

add_dependencies(ocpp_evse_security generate_cpp_files)

target_link_libraries(ocpp_evse_security
Expand All @@ -39,6 +45,12 @@ target_include_directories(ocpp_conversions
"$<TARGET_PROPERTY:generate_cpp_files,EVEREST_GENERATED_INCLUDE_DIR>"
)

target_compile_options(ocpp_conversions
PRIVATE
-Wimplicit-fallthrough
-Werror=switch-enum
)

add_dependencies(ocpp_conversions generate_cpp_files)

target_link_libraries(ocpp_conversions
Expand Down
74 changes: 28 additions & 46 deletions lib/staging/ocpp/evse_security_ocpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,8 @@ ocpp::CaCertificateType to_ocpp(types::evse_security::CaCertificateType other) {
return ocpp::CaCertificateType::CSMS;
case types::evse_security::CaCertificateType::MF:
return ocpp::CaCertificateType::MF;
default:
throw std::runtime_error(
"Could not convert types::evse_security::CaCertificateType to ocpp::CaCertificateType");
}
throw std::runtime_error("Could not convert types::evse_security::CaCertificateType to ocpp::CaCertificateType");
}

ocpp::LeafCertificateType to_ocpp(types::evse_security::LeafCertificateType other) {
Expand All @@ -156,10 +154,9 @@ ocpp::LeafCertificateType to_ocpp(types::evse_security::LeafCertificateType othe
return ocpp::LeafCertificateType::MF;
case types::evse_security::LeafCertificateType::MO:
return ocpp::LeafCertificateType::MO;
default:
throw std::runtime_error(
"Could not convert types::evse_security::LeafCertificateType to ocpp::CertificateSigningUseEnum");
}
throw std::runtime_error(
"Could not convert types::evse_security::LeafCertificateType to ocpp::CertificateSigningUseEnum");
}

ocpp::CertificateType to_ocpp(types::evse_security::CertificateType other) {
Expand All @@ -174,9 +171,8 @@ ocpp::CertificateType to_ocpp(types::evse_security::CertificateType other) {
return ocpp::CertificateType::V2GCertificateChain;
case types::evse_security::CertificateType::MFRootCertificate:
return ocpp::CertificateType::MFRootCertificate;
default:
throw std::runtime_error("Could not convert types::evse_security::CertificateType to ocpp::CertificateType");
}
throw std::runtime_error("Could not convert types::evse_security::CertificateType to ocpp::CertificateType");
}

ocpp::HashAlgorithmEnumType to_ocpp(types::evse_security::HashAlgorithm other) {
Expand All @@ -187,10 +183,8 @@ ocpp::HashAlgorithmEnumType to_ocpp(types::evse_security::HashAlgorithm other) {
return ocpp::HashAlgorithmEnumType::SHA384;
case types::evse_security::HashAlgorithm::SHA512:
return ocpp::HashAlgorithmEnumType::SHA512;
default:
throw std::runtime_error(
"Could not convert types::evse_security::HashAlgorithm to ocpp::HashAlgorithmEnumType");
}
throw std::runtime_error("Could not convert types::evse_security::HashAlgorithm to ocpp::HashAlgorithmEnumType");
}

ocpp::InstallCertificateResult to_ocpp(types::evse_security::InstallCertificateResult other) {
Expand All @@ -213,10 +207,9 @@ ocpp::InstallCertificateResult to_ocpp(types::evse_security::InstallCertificateR
return ocpp::InstallCertificateResult::WriteError;
case types::evse_security::InstallCertificateResult::Accepted:
return ocpp::InstallCertificateResult::Accepted;
default:
throw std::runtime_error(
"Could not convert types::evse_security::InstallCertificateResult to ocpp::InstallCertificateResult");
}
throw std::runtime_error(
"Could not convert types::evse_security::InstallCertificateResult to ocpp::InstallCertificateResult");
}

ocpp::CertificateValidationResult to_ocpp(types::evse_security::CertificateValidationResult other) {
Expand All @@ -233,10 +226,11 @@ ocpp::CertificateValidationResult to_ocpp(types::evse_security::CertificateValid
return ocpp::CertificateValidationResult::InvalidChain;
case types::evse_security::CertificateValidationResult::Unknown:
return ocpp::CertificateValidationResult::Unknown;
default:
throw std::runtime_error("Could not convert types::evse_security::CertificateValidationResult to "
"ocpp::CertificateValidationResult");
case types::evse_security::CertificateValidationResult::Expired:
return ocpp::CertificateValidationResult::Expired;
}
throw std::runtime_error("Could not convert types::evse_security::CertificateValidationResult to "
"ocpp::CertificateValidationResult");
}

ocpp::GetCertificateInfoStatus to_ocpp(types::evse_security::GetCertificateInfoStatus other) {
Expand All @@ -251,10 +245,9 @@ ocpp::GetCertificateInfoStatus to_ocpp(types::evse_security::GetCertificateInfoS
return ocpp::GetCertificateInfoStatus::NotFoundValid;
case types::evse_security::GetCertificateInfoStatus::PrivateKeyNotFound:
return ocpp::GetCertificateInfoStatus::PrivateKeyNotFound;
default:
throw std::runtime_error("Could not convert types::evse_security::GetCertificateInfoStatus to "
"ocpp::GetCertificateInfoStatus");
}
throw std::runtime_error("Could not convert types::evse_security::GetCertificateInfoStatus to "
"ocpp::GetCertificateInfoStatus");
}

ocpp::DeleteCertificateResult to_ocpp(types::evse_security::DeleteCertificateResult other) {
Expand All @@ -265,10 +258,9 @@ ocpp::DeleteCertificateResult to_ocpp(types::evse_security::DeleteCertificateRes
return ocpp::DeleteCertificateResult::Failed;
case types::evse_security::DeleteCertificateResult::NotFound:
return ocpp::DeleteCertificateResult::NotFound;
default:
throw std::runtime_error(
"Could not convert types::evse_security::DeleteCertificateResult to ocpp::DeleteCertificateResult");
}
throw std::runtime_error(
"Could not convert types::evse_security::DeleteCertificateResult to ocpp::DeleteCertificateResult");
}

ocpp::GetCertificateSignRequestStatus to_ocpp(types::evse_security::GetCertificateSignRequestStatus other) {
Expand All @@ -281,10 +273,9 @@ ocpp::GetCertificateSignRequestStatus to_ocpp(types::evse_security::GetCertifica
return ocpp::GetCertificateSignRequestStatus::KeyGenError;
case types::evse_security::GetCertificateSignRequestStatus::GenerationError:
return ocpp::GetCertificateSignRequestStatus::GenerationError;
default:
throw std::runtime_error("Could not convert types::evse_security::GetCertificateSignRequestStatus to "
"ocpp::GetCertificateSignRequestStatus");
}
throw std::runtime_error("Could not convert types::evse_security::GetCertificateSignRequestStatus to "
"ocpp::GetCertificateSignRequestStatus");
}

ocpp::CertificateHashDataType to_ocpp(types::evse_security::CertificateHashData other) {
Expand Down Expand Up @@ -362,10 +353,8 @@ types::evse_security::CaCertificateType from_ocpp(ocpp::CaCertificateType other)
return types::evse_security::CaCertificateType::CSMS;
case ocpp::CaCertificateType::MF:
return types::evse_security::CaCertificateType::MF;
default:
throw std::runtime_error(
"Could not convert types::evse_security::CaCertificateType to ocpp::CaCertificateType");
}
throw std::runtime_error("Could not convert types::evse_security::CaCertificateType to ocpp::CaCertificateType");
}

types::evse_security::LeafCertificateType from_ocpp(ocpp::CertificateSigningUseEnum other) {
Expand All @@ -376,10 +365,9 @@ types::evse_security::LeafCertificateType from_ocpp(ocpp::CertificateSigningUseE
return types::evse_security::LeafCertificateType::V2G;
case ocpp::CertificateSigningUseEnum::ManufacturerCertificate:
return types::evse_security::LeafCertificateType::MF;
default:
throw std::runtime_error(
"Could not convert ocpp::CertificateSigningUseEnum to types::evse_security::LeafCertificateType");
}
throw std::runtime_error(
"Could not convert ocpp::CertificateSigningUseEnum to types::evse_security::LeafCertificateType");
}

types::evse_security::LeafCertificateType from_ocpp(ocpp::LeafCertificateType other) {
Expand All @@ -392,10 +380,9 @@ types::evse_security::LeafCertificateType from_ocpp(ocpp::LeafCertificateType ot
return types::evse_security::LeafCertificateType::MF;
case ocpp::LeafCertificateType::MO:
return types::evse_security::LeafCertificateType::MO;
default:
throw std::runtime_error(
"Could not convert ocpp::CertificateSigningUseEnum to types::evse_security::LeafCertificateType");
}
throw std::runtime_error(
"Could not convert ocpp::CertificateSigningUseEnum to types::evse_security::LeafCertificateType");
}

types::evse_security::CertificateType from_ocpp(ocpp::CertificateType other) {
Expand All @@ -410,9 +397,8 @@ types::evse_security::CertificateType from_ocpp(ocpp::CertificateType other) {
return types::evse_security::CertificateType::V2GCertificateChain;
case ocpp::CertificateType::MFRootCertificate:
return types::evse_security::CertificateType::MFRootCertificate;
default:
throw std::runtime_error("Could not convert ocpp::CertificateType to types::evse_security::CertificateType");
}
throw std::runtime_error("Could not convert ocpp::CertificateType to types::evse_security::CertificateType");
}

types::evse_security::HashAlgorithm from_ocpp(ocpp::HashAlgorithmEnumType other) {
Expand All @@ -423,10 +409,8 @@ types::evse_security::HashAlgorithm from_ocpp(ocpp::HashAlgorithmEnumType other)
return types::evse_security::HashAlgorithm::SHA384;
case ocpp::HashAlgorithmEnumType::SHA512:
return types::evse_security::HashAlgorithm::SHA512;
default:
throw std::runtime_error(
"Could not convert ocpp::HashAlgorithmEnumType to types::evse_security::HashAlgorithm");
}
throw std::runtime_error("Could not convert ocpp::HashAlgorithmEnumType to types::evse_security::HashAlgorithm");
}

types::evse_security::InstallCertificateResult from_ocpp(ocpp::InstallCertificateResult other) {
Expand All @@ -449,10 +433,9 @@ types::evse_security::InstallCertificateResult from_ocpp(ocpp::InstallCertificat
return types::evse_security::InstallCertificateResult::WriteError;
case ocpp::InstallCertificateResult::Accepted:
return types::evse_security::InstallCertificateResult::Accepted;
default:
throw std::runtime_error(
"Could not convert ocpp::InstallCertificateResult to types::evse_security::InstallCertificateResult");
}
throw std::runtime_error(
"Could not convert ocpp::InstallCertificateResult to types::evse_security::InstallCertificateResult");
}

types::evse_security::DeleteCertificateResult from_ocpp(ocpp::DeleteCertificateResult other) {
Expand All @@ -463,10 +446,9 @@ types::evse_security::DeleteCertificateResult from_ocpp(ocpp::DeleteCertificateR
return types::evse_security::DeleteCertificateResult::Failed;
case ocpp::DeleteCertificateResult::NotFound:
return types::evse_security::DeleteCertificateResult::NotFound;
default:
throw std::runtime_error(
"Could not convert ocpp::DeleteCertificateResult to types::evse_security::DeleteCertificateResult");
}
throw std::runtime_error(
"Could not convert ocpp::DeleteCertificateResult to types::evse_security::DeleteCertificateResult");
}

types::evse_security::CertificateHashData from_ocpp(ocpp::CertificateHashDataType other) {
Expand Down
27 changes: 10 additions & 17 deletions lib/staging/ocpp/ocpp_conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ to_everest_display_message_priority(const ocpp::v201::MessagePriorityEnum& prior
return types::display_message::MessagePriorityEnum::InFront;
case ocpp::v201::MessagePriorityEnum::NormalCycle:
return types::display_message::MessagePriorityEnum::NormalCycle;
default:
throw std::out_of_range(
"Could not convert ocpp::v201::MessagePriorityEnum to types::display_message::MessagePriorityEnum");
}
throw std::out_of_range(
"Could not convert ocpp::v201::MessagePriorityEnum to types::display_message::MessagePriorityEnum");
}

ocpp::v201::MessagePriorityEnum
Expand All @@ -27,10 +26,9 @@ to_ocpp_201_message_priority(const types::display_message::MessagePriorityEnum&
return ocpp::v201::MessagePriorityEnum::InFront;
case types::display_message::MessagePriorityEnum::NormalCycle:
return ocpp::v201::MessagePriorityEnum::NormalCycle;
default:
throw std::out_of_range(
"Could not convert types::display_message::MessagePriorityEnum to ocpp::v201::MessagePriorityEnum");
}
throw std::out_of_range(
"Could not convert types::display_message::MessagePriorityEnum to ocpp::v201::MessagePriorityEnum");
}

types::display_message::MessageStateEnum to_everest_display_message_state(const ocpp::v201::MessageStateEnum& state) {
Expand All @@ -43,10 +41,9 @@ types::display_message::MessageStateEnum to_everest_display_message_state(const
return types::display_message::MessageStateEnum::Idle;
case ocpp::v201::MessageStateEnum::Unavailable:
return types::display_message::MessageStateEnum::Unavailable;
default:
throw std::out_of_range(
"Could not convert ocpp::v201::MessageStateEnum to types::display_message::MessageStateEnum");
}
throw std::out_of_range(
"Could not convert ocpp::v201::MessageStateEnum to types::display_message::MessageStateEnum");
}

ocpp::v201::MessageStateEnum to_ocpp_201_display_message_state(const types::display_message::MessageStateEnum& state) {
Expand All @@ -59,10 +56,9 @@ ocpp::v201::MessageStateEnum to_ocpp_201_display_message_state(const types::disp
return ocpp::v201::MessageStateEnum::Idle;
case types::display_message::MessageStateEnum::Unavailable:
return ocpp::v201::MessageStateEnum::Unavailable;
default:
throw std::out_of_range(
"Could not convert types::display_message::MessageStateEnum to ocpp::v201::MessageStateEnum");
}
throw std::out_of_range(
"Could not convert types::display_message::MessageStateEnum to ocpp::v201::MessageStateEnum");
}

types::display_message::MessageFormat
Expand All @@ -76,10 +72,8 @@ to_everest_display_message_format(const ocpp::v201::MessageFormatEnum& message_f
return types::display_message::MessageFormat::URI;
case ocpp::v201::MessageFormatEnum::UTF8:
return types::display_message::MessageFormat::UTF8;
default:
throw std::out_of_range(
"Could not convert ocpp::v201::MessageFormat to types::display_message::MessageFormatEnum");
}
throw std::out_of_range("Could not convert ocpp::v201::MessageFormat to types::display_message::MessageFormatEnum");
}

ocpp::v201::MessageFormatEnum to_ocpp_201_message_format_enum(const types::display_message::MessageFormat& format) {
Expand Down Expand Up @@ -205,9 +199,8 @@ types::session_cost::SessionStatus to_everest_running_cost_state(const ocpp::Run
return types::session_cost::SessionStatus::Idle;
case ocpp::RunningCostState::Finished:
return types::session_cost::SessionStatus::Finished;
default:
throw std::out_of_range("Could not convert ocpp::RunningCostState to types::session_cost::SessionStatus");
}
throw std::out_of_range("Could not convert ocpp::RunningCostState to types::session_cost::SessionStatus");
}

types::session_cost::SessionCostChunk create_session_cost_chunk(const double& price, const uint32_t& number_of_decimals,
Expand Down
6 changes: 6 additions & 0 deletions modules/OCPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ target_link_libraries(${MODULE_NAME}
everest::ocpp_evse_security
everest::ocpp_conversions
)

target_compile_options(${MODULE_NAME}
PRIVATE
-Wimplicit-fallthrough
-Werror=switch-enum
)
# ev@bcc62523-e22b-41d7-ba2f-825b493a3c97:v1

target_sources(${MODULE_NAME}
Expand Down
Loading

0 comments on commit f42780c

Please sign in to comment.