From 7fa0d4c021f85bd7914ad6146c08de7da9532398 Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sat, 18 May 2024 21:53:24 +0200 Subject: [PATCH 1/3] add build flag MO_ENABLE_V16_RESERVATION --- CHANGELOG.md | 1 + src/MicroOcpp.cpp | 3 ++ .../Model/ConnectorBase/Connector.cpp | 35 ++++++++++++------- src/MicroOcpp/Model/Model.cpp | 20 +++++++---- src/MicroOcpp/Model/Model.h | 12 +++++-- .../Model/Reservation/Reservation.cpp | 8 ++++- src/MicroOcpp/Model/Reservation/Reservation.h | 11 ++++-- .../Model/Reservation/ReservationService.cpp | 8 ++++- .../Model/Reservation/ReservationService.h | 11 ++++-- .../Operations/CancelReservation.cpp | 21 +++++------ src/MicroOcpp/Operations/CancelReservation.h | 17 +++++---- src/MicroOcpp/Operations/ReserveNow.cpp | 6 ++++ src/MicroOcpp/Operations/ReserveNow.h | 11 ++++-- src/MicroOcpp/Version.h | 5 +++ tests/Reservation.cpp | 4 +++ 15 files changed, 125 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 829d8a0d..aea9a28c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - File index ([#270](https://github.com/matth-x/MicroOcpp/pull/270)) - Config `Cst_TxStartOnPowerPathClosed` to put back TxStartPoint ([#271](https://github.com/matth-x/MicroOcpp/pull/271)) +- Build flag `MO_ENABLE_V16_RESERVATION=0` disables Reservation module - Function `bool isConnected()` in `Connection` interface ([#282](https://github.com/matth-x/MicroOcpp/pull/282)) - Build flags for customizing memory limits of SmartCharging ([#260](https://github.com/matth-x/MicroOcpp/pull/260)) - SConscript ([#287](https://github.com/matth-x/MicroOcpp/pull/287)) diff --git a/src/MicroOcpp.cpp b/src/MicroOcpp.cpp index 3da86414..34921786 100644 --- a/src/MicroOcpp.cpp +++ b/src/MicroOcpp.cpp @@ -289,8 +289,11 @@ void mocpp_initialize(Connection& connection, const char *bootNotificationCreden new HeartbeatService(*context))); model.setAuthorizationService(std::unique_ptr( new AuthorizationService(*context, filesystem))); + +#if MO_ENABLE_V16_RESERVATION model.setReservationService(std::unique_ptr( new ReservationService(*context, MO_NUMCONNECTORS))); +#endif #if MO_ENABLE_V201 model.setVariableService(std::unique_ptr( diff --git a/src/MicroOcpp/Model/ConnectorBase/Connector.cpp b/src/MicroOcpp/Model/ConnectorBase/Connector.cpp index 1f7564db..a33ddbad 100644 --- a/src/MicroOcpp/Model/ConnectorBase/Connector.cpp +++ b/src/MicroOcpp/Model/ConnectorBase/Connector.cpp @@ -112,9 +112,13 @@ ChargePointStatus Connector::getStatus() { } else { res = ChargePointStatus::Charging; } - } else if (model.getReservationService() && model.getReservationService()->getReservation(connectorId)) { + } + #if MO_ENABLE_V16_RESERVATION + else if (model.getReservationService() && model.getReservationService()->getReservation(connectorId)) { res = ChargePointStatus::Reserved; - } else if ((!transaction || !transaction->isActive()) && //no transaction preparation + } + #endif + else if ((!transaction || !transaction->isActive()) && //no transaction preparation (!connectorPluggedInput || !connectorPluggedInput()) && //no vehicle plugged (!occupiedInput || !occupiedInput())) { //occupied override clear res = ChargePointStatus::Available; @@ -609,17 +613,22 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { } } - Reservation *reservation = nullptr; + int reservationId = -1; bool offlineBlockedResv = false; //if offline authorization will be blocked by reservation + #if MO_ENABLE_V16_RESERVATION //check if blocked by reservation if (model.getReservationService()) { const char *parentIdTag = localAuth ? localAuth->getParentIdTag() : nullptr; - reservation = model.getReservationService()->getReservation( + auto reservation = model.getReservationService()->getReservation( connectorId, idTag, parentIdTag); + + if (reservation) { + reservationId = reservation->getReservationId(); + } if (reservation && !reservation->matches( idTag, @@ -640,6 +649,7 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { } } } + #endif //MO_ENABLE_V16_RESERVATION transaction = allocateTransaction(); @@ -661,8 +671,8 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { if (localAuth && localPreAuthorizeBool && localPreAuthorizeBool->getBool()) { MO_DBG_DEBUG("Begin transaction process (%s), preauthorized locally", idTag != nullptr ? idTag : ""); - if (reservation) { - transaction->setReservationId(reservation->getReservationId()); + if (reservationId >= 0) { + transaction->setReservationId(reservationId); } transaction->setAuthorized(); @@ -692,6 +702,7 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { return; } + #if MO_ENABLE_V16_RESERVATION if (model.getReservationService()) { auto reservation = model.getReservationService()->getReservation( connectorId, @@ -714,6 +725,7 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { } } } + #endif //MO_ENABLE_V16_RESERVATION MO_DBG_DEBUG("Authorized transaction process (%s)", tx->getIdTag()); tx->setAuthorized(); @@ -724,13 +736,10 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { //capture local auth and reservation check in for timeout handler bool localAuthFound = localAuth; - bool reservationFound = reservation; - int reservationId = reservation ? reservation->getReservationId() : -1; authorize->setOnTimeoutListener([this, tx, offlineBlockedAuth, offlineBlockedResv, localAuthFound, - reservationFound, reservationId] () { if (offlineBlockedAuth) { @@ -753,7 +762,7 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { if (localAuthFound && localAuthorizeOfflineBool && localAuthorizeOfflineBool->getBool()) { MO_DBG_DEBUG("Offline transaction process (%s), locally authorized", tx->getIdTag()); - if (reservationFound) { + if (reservationId >= 0) { tx->setReservationId(reservationId); } tx->setAuthorized(); @@ -765,7 +774,7 @@ std::shared_ptr Connector::beginTransaction(const char *idTag) { if (allowOfflineTxForUnknownIdBool && allowOfflineTxForUnknownIdBool->getBool()) { MO_DBG_DEBUG("Offline transaction process (%s), allow unknown ID", tx->getIdTag()); - if (reservationFound) { + if (reservationId >= 0) { tx->setReservationId(reservationId); } tx->setAuthorized(); @@ -811,7 +820,8 @@ std::shared_ptr Connector::beginTransaction_authorized(const char * MO_DBG_DEBUG("Begin transaction process (%s), already authorized", idTag != nullptr ? idTag : ""); transaction->setAuthorized(); - + + #if MO_ENABLE_V16_RESERVATION if (model.getReservationService()) { if (auto reservation = model.getReservationService()->getReservation(connectorId, idTag, parentIdTag)) { if (reservation->matches(idTag, parentIdTag)) { @@ -819,6 +829,7 @@ std::shared_ptr Connector::beginTransaction_authorized(const char * } } } + #endif //MO_ENABLE_V16_RESERVATION transaction->commit(); diff --git a/src/MicroOcpp/Model/Model.cpp b/src/MicroOcpp/Model/Model.cpp index 009ea0d4..7b6c1126 100644 --- a/src/MicroOcpp/Model/Model.cpp +++ b/src/MicroOcpp/Model/Model.cpp @@ -54,25 +54,27 @@ void Model::loop() { if (chargeControlCommon) chargeControlCommon->loop(); - + if (smartChargingService) smartChargingService->loop(); - + if (heartbeatService) heartbeatService->loop(); - + if (meteringService) meteringService->loop(); - + if (diagnosticsService) diagnosticsService->loop(); - + if (firmwareService) firmwareService->loop(); - + +#if MO_ENABLE_V16_RESERVATION if (reservationService) reservationService->loop(); - +#endif //MO_ENABLE_V16_RESERVATION + if (resetService) resetService->loop(); @@ -171,6 +173,7 @@ AuthorizationService *Model::getAuthorizationService() { return authorizationService.get(); } +#if MO_ENABLE_V16_RESERVATION void Model::setReservationService(std::unique_ptr rs) { reservationService = std::move(rs); capabilitiesUpdated = true; @@ -179,6 +182,7 @@ void Model::setReservationService(std::unique_ptr rs) { ReservationService *Model::getReservationService() { return reservationService.get(); } +#endif //MO_ENABLE_V16_RESERVATION void Model::setBootService(std::unique_ptr bs){ bootService = std::move(bs); @@ -286,12 +290,14 @@ void Model::updateSupportedStandardProfiles() { } } +#if MO_ENABLE_V16_RESERVATION if (reservationService) { if (!strstr(supportedFeatureProfilesString->getString(), "Reservation")) { if (!buf.empty()) buf += ','; buf += "Reservation"; } } +#endif //MO_ENABLE_V16_RESERVATION if (smartChargingService) { if (!strstr(supportedFeatureProfilesString->getString(), "SmartCharging")) { diff --git a/src/MicroOcpp/Model/Model.h b/src/MicroOcpp/Model/Model.h index 7a2c3500..5b98fd8a 100644 --- a/src/MicroOcpp/Model/Model.h +++ b/src/MicroOcpp/Model/Model.h @@ -21,10 +21,13 @@ class FirmwareService; class DiagnosticsService; class HeartbeatService; class AuthorizationService; -class ReservationService; class BootService; class ResetService; +#if MO_ENABLE_V16_RESERVATION +class ReservationService; +#endif //MO_ENABLE_V16_RESERVATION + #if MO_ENABLE_CERT_MGMT class CertificateService; #endif //MO_ENABLE_CERT_MGMT @@ -49,10 +52,13 @@ class Model { std::unique_ptr diagnosticsService; std::unique_ptr heartbeatService; std::unique_ptr authorizationService; - std::unique_ptr reservationService; std::unique_ptr bootService; std::unique_ptr resetService; +#if MO_ENABLE_V16_RESERVATION + std::unique_ptr reservationService; +#endif //MO_ENABLE_V16_RESERVATION + #if MO_ENABLE_CERT_MGMT std::unique_ptr certService; #endif //MO_ENABLE_CERT_MGMT @@ -110,8 +116,10 @@ class Model { void setAuthorizationService(std::unique_ptr authorizationService); AuthorizationService *getAuthorizationService(); +#if MO_ENABLE_V16_RESERVATION void setReservationService(std::unique_ptr reservationService); ReservationService *getReservationService(); +#endif //MO_ENABLE_V16_RESERVATION void setBootService(std::unique_ptr bs); BootService *getBootService() const; diff --git a/src/MicroOcpp/Model/Reservation/Reservation.cpp b/src/MicroOcpp/Model/Reservation/Reservation.cpp index 302ac8cb..78c40b60 100644 --- a/src/MicroOcpp/Model/Reservation/Reservation.cpp +++ b/src/MicroOcpp/Model/Reservation/Reservation.cpp @@ -1,7 +1,11 @@ // matth-x/MicroOcpp -// Copyright Matthias Akstaller 2019 - 2023 +// Copyright Matthias Akstaller 2019 - 2024 // MIT License +#include + +#if MO_ENABLE_V16_RESERVATION + #include #include #include @@ -130,3 +134,5 @@ void Reservation::clear() { configuration_save(); } + +#endif //MO_ENABLE_V16_RESERVATION diff --git a/src/MicroOcpp/Model/Reservation/Reservation.h b/src/MicroOcpp/Model/Reservation/Reservation.h index 6722df1b..9747d74e 100644 --- a/src/MicroOcpp/Model/Reservation/Reservation.h +++ b/src/MicroOcpp/Model/Reservation/Reservation.h @@ -1,9 +1,13 @@ // matth-x/MicroOcpp -// Copyright Matthias Akstaller 2019 - 2023 +// Copyright Matthias Akstaller 2019 - 2024 // MIT License -#ifndef RESERVATION_H -#define RESERVATION_H +#ifndef MO_RESERVATION_H +#define MO_RESERVATION_H + +#include + +#if MO_ENABLE_V16_RESERVATION #include #include @@ -65,4 +69,5 @@ class Reservation { } +#endif //MO_ENABLE_V16_RESERVATION #endif diff --git a/src/MicroOcpp/Model/Reservation/ReservationService.cpp b/src/MicroOcpp/Model/Reservation/ReservationService.cpp index 63782cda..5f3dc4fc 100644 --- a/src/MicroOcpp/Model/Reservation/ReservationService.cpp +++ b/src/MicroOcpp/Model/Reservation/ReservationService.cpp @@ -1,7 +1,11 @@ // matth-x/MicroOcpp -// Copyright Matthias Akstaller 2019 - 2023 +// Copyright Matthias Akstaller 2019 - 2024 // MIT License +#include + +#if MO_ENABLE_V16_RESERVATION + #include #include #include @@ -210,3 +214,5 @@ bool ReservationService::updateReservation(int reservationId, unsigned int conne MO_DBG_ERR("error finding blocking reservation"); return false; } + +#endif //MO_ENABLE_V16_RESERVATION diff --git a/src/MicroOcpp/Model/Reservation/ReservationService.h b/src/MicroOcpp/Model/Reservation/ReservationService.h index 87a6a486..d33d4246 100644 --- a/src/MicroOcpp/Model/Reservation/ReservationService.h +++ b/src/MicroOcpp/Model/Reservation/ReservationService.h @@ -1,9 +1,13 @@ // matth-x/MicroOcpp -// Copyright Matthias Akstaller 2019 - 2023 +// Copyright Matthias Akstaller 2019 - 2024 // MIT License -#ifndef RESERVATIONSERVICE_H -#define RESERVATIONSERVICE_H +#ifndef MO_RESERVATIONSERVICE_H +#define MO_RESERVATIONSERVICE_H + +#include + +#if MO_ENABLE_V16_RESERVATION #include @@ -44,4 +48,5 @@ class ReservationService { } +#endif //MO_ENABLE_V16_RESERVATION #endif diff --git a/src/MicroOcpp/Operations/CancelReservation.cpp b/src/MicroOcpp/Operations/CancelReservation.cpp index 25088fe7..4b1929e0 100644 --- a/src/MicroOcpp/Operations/CancelReservation.cpp +++ b/src/MicroOcpp/Operations/CancelReservation.cpp @@ -1,15 +1,18 @@ // matth-x/MicroOcpp -// Copyright Matthias Akstaller 2019 - 2023 +// Copyright Matthias Akstaller 2019 - 2024 // MIT License +#include + +#if MO_ENABLE_V16_RESERVATION + #include -#include #include #include using MicroOcpp::Ocpp16::CancelReservation; -CancelReservation::CancelReservation(Model& model) : model(model) { +CancelReservation::CancelReservation(ReservationService& reservationService) : reservationService(reservationService) { } @@ -23,13 +26,9 @@ void CancelReservation::processReq(JsonObject payload) { return; } - if (model.getReservationService()) { - if (auto reservation = model.getReservationService()->getReservationById(payload["reservationId"])) { - found = true; - reservation->clear(); - } - } else { - errorCode = "InternalError"; + if (auto reservation = reservationService.getReservationById(payload["reservationId"])) { + found = true; + reservation->clear(); } } @@ -43,3 +42,5 @@ std::unique_ptr CancelReservation::createConf(){ } return doc; } + +#endif //MO_ENABLE_V16_RESERVATION diff --git a/src/MicroOcpp/Operations/CancelReservation.h b/src/MicroOcpp/Operations/CancelReservation.h index b268a8fd..3b0312c4 100644 --- a/src/MicroOcpp/Operations/CancelReservation.h +++ b/src/MicroOcpp/Operations/CancelReservation.h @@ -1,25 +1,29 @@ // matth-x/MicroOcpp -// Copyright Matthias Akstaller 2019 - 2023 +// Copyright Matthias Akstaller 2019 - 2024 // MIT License -#ifndef CANCELRESERVATION_H -#define CANCELRESERVATION_H +#ifndef MO_CANCELRESERVATION_H +#define MO_CANCELRESERVATION_H + +#include + +#if MO_ENABLE_V16_RESERVATION #include namespace MicroOcpp { -class Model; +class ReservationService; namespace Ocpp16 { class CancelReservation : public Operation { private: - Model& model; + ReservationService& reservationService; bool found = false; const char *errorCode = nullptr; public: - CancelReservation(Model& model); + CancelReservation(ReservationService& reservationService); const char* getOperationType() override; @@ -33,4 +37,5 @@ class CancelReservation : public Operation { } //end namespace Ocpp16 } //end namespace MicroOcpp +#endif //MO_ENABLE_V16_RESERVATION #endif diff --git a/src/MicroOcpp/Operations/ReserveNow.cpp b/src/MicroOcpp/Operations/ReserveNow.cpp index 432a6423..7e3a2c0e 100644 --- a/src/MicroOcpp/Operations/ReserveNow.cpp +++ b/src/MicroOcpp/Operations/ReserveNow.cpp @@ -2,6 +2,10 @@ // Copyright Matthias Akstaller 2019 - 2024 // MIT License +#include + +#if MO_ENABLE_V16_RESERVATION + #include #include #include @@ -123,3 +127,5 @@ std::unique_ptr ReserveNow::createConf(){ return doc; } + +#endif //MO_ENABLE_V16_RESERVATION diff --git a/src/MicroOcpp/Operations/ReserveNow.h b/src/MicroOcpp/Operations/ReserveNow.h index ddaba236..92cb717d 100644 --- a/src/MicroOcpp/Operations/ReserveNow.h +++ b/src/MicroOcpp/Operations/ReserveNow.h @@ -1,9 +1,13 @@ // matth-x/MicroOcpp -// Copyright Matthias Akstaller 2019 - 2023 +// Copyright Matthias Akstaller 2019 - 2024 // MIT License -#ifndef RESERVENOW_H -#define RESERVENOW_H +#ifndef MO_RESERVENOW_H +#define MO_RESERVENOW_H + +#include + +#if MO_ENABLE_V16_RESERVATION #include @@ -35,4 +39,5 @@ class ReserveNow : public Operation { } //end namespace Ocpp16 } //end namespace MicroOcpp +#endif //MO_ENABLE_V16_RESERVATION #endif diff --git a/src/MicroOcpp/Version.h b/src/MicroOcpp/Version.h index c7ee0d77..896bb8d3 100644 --- a/src/MicroOcpp/Version.h +++ b/src/MicroOcpp/Version.h @@ -39,4 +39,9 @@ struct ProtocolVersion { #define MO_ENABLE_CERT_MGMT MO_ENABLE_V201 #endif +// Reservations, OCPP 1.6 implementation +#ifndef MO_ENABLE_V16_RESERVATION +#define MO_ENABLE_V16_RESERVATION 1 +#endif + #endif diff --git a/tests/Reservation.cpp b/tests/Reservation.cpp index 8b5c5a50..1270b5db 100644 --- a/tests/Reservation.cpp +++ b/tests/Reservation.cpp @@ -12,6 +12,8 @@ #include #include +#if MO_ENABLE_V16_RESERVATION + #define BASE_TIME "2023-01-01T00:00:00.000Z" @@ -504,3 +506,5 @@ TEST_CASE( "Reservation" ) { mocpp_deinitialize(); } + +#endif //MO_ENABLE_V16_RESERVATION From 6d77ccfdab049d4aa010f3c190c7d4014410e93a Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sat, 18 May 2024 22:02:20 +0200 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aea9a28c..d0b0c891 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ - File index ([#270](https://github.com/matth-x/MicroOcpp/pull/270)) - Config `Cst_TxStartOnPowerPathClosed` to put back TxStartPoint ([#271](https://github.com/matth-x/MicroOcpp/pull/271)) -- Build flag `MO_ENABLE_V16_RESERVATION=0` disables Reservation module +- Build flag `MO_ENABLE_V16_RESERVATION=0` disables Reservation module ([#302](https://github.com/matth-x/MicroOcpp/pull/302)) - Function `bool isConnected()` in `Connection` interface ([#282](https://github.com/matth-x/MicroOcpp/pull/282)) - Build flags for customizing memory limits of SmartCharging ([#260](https://github.com/matth-x/MicroOcpp/pull/260)) - SConscript ([#287](https://github.com/matth-x/MicroOcpp/pull/287)) From 53e9d56255a1ae303672b44a966ef6410e45ad0e Mon Sep 17 00:00:00 2001 From: matth-x <63792403+matth-x@users.noreply.github.com> Date: Sat, 18 May 2024 22:08:14 +0200 Subject: [PATCH 3/3] fix compilation error --- src/MicroOcpp/Model/Reservation/ReservationService.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MicroOcpp/Model/Reservation/ReservationService.cpp b/src/MicroOcpp/Model/Reservation/ReservationService.cpp index 5f3dc4fc..41c185de 100644 --- a/src/MicroOcpp/Model/Reservation/ReservationService.cpp +++ b/src/MicroOcpp/Model/Reservation/ReservationService.cpp @@ -28,8 +28,8 @@ ReservationService::ReservationService(Context& context, unsigned int numConnect reserveConnectorZeroSupportedBool = declareConfiguration("ReserveConnectorZeroSupported", true, CONFIGURATION_VOLATILE, true); - context.getOperationRegistry().registerOperation("CancelReservation", [&context] () { - return new Ocpp16::CancelReservation(context.getModel());}); + context.getOperationRegistry().registerOperation("CancelReservation", [this] () { + return new Ocpp16::CancelReservation(*this);}); context.getOperationRegistry().registerOperation("ReserveNow", [&context] () { return new Ocpp16::ReserveNow(context.getModel());}); }