Skip to content

Commit

Permalink
Feature/data transfer functional block (#871)
Browse files Browse the repository at this point in the history
* Added message handler interface to be implemented by specific functional blocks
* Moved DataTransfer functionality to DataTransfer functional block using the targeted design. Added test cases for the new functional block

---------

Signed-off-by: Piet Gömpel <[email protected]>
  • Loading branch information
Pietfried authored Nov 20, 2024
1 parent 94a6844 commit 18ca071
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 179 deletions.
9 changes: 9 additions & 0 deletions doc/message_dispatching.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class v201_MessageDispatcher {
- RegistrationStatusEnum& registration_status
}
class v201_MessageHandlerInterface {
+handle_message(EnhancedMessage~v201_MessageType~ message)
}
class v16_MessageHandlerInterface {
+handle_message(EnhancedMessage~v16_MessageType~ message)
}
class v201_DataTransferInterface {
+data_transfer_req(request: DataTransferRequest): std::optional~DataTransferResponse~
+handle_data_transfer_req(call: Call~DataTransferRequest~)
Expand All @@ -44,6 +52,7 @@ class v16_ChargePoint {
MessageDispatcherInterface <|-- v16_MessageDispatcher
MessageDispatcherInterface <|-- v201_MessageDispatcher
v201_DataTransferInterface <|-- v201_DataTransfer
v201_MessageHandlerInterface <|-- v201_DataTransferInterface
MessageDispatcherInterface *-- v201_DataTransfer
MessageDispatcherInterface *-- v201_ChargePoint
v201_DataTransferInterface *-- v201_ChargePoint
Expand Down
5 changes: 2 additions & 3 deletions include/ocpp/v201/charge_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <set>

#include <ocpp/common/message_dispatcher.hpp>
#include <ocpp/v201/functional_blocks/data_transfer.hpp>

#include <ocpp/common/charging_station_base.hpp>

Expand Down Expand Up @@ -388,6 +389,7 @@ class ChargePoint : public ChargePointInterface, private ocpp::ChargingStationBa
std::unique_ptr<ConnectivityManager> connectivity_manager;

std::unique_ptr<MessageDispatcherInterface<MessageType>> message_dispatcher;
std::unique_ptr<DataTransferInterface> data_transfer;

// utility
std::shared_ptr<MessageQueue<v201::MessageType>> message_queue;
Expand Down Expand Up @@ -759,9 +761,6 @@ class ChargePoint : public ChargePointInterface, private ocpp::ChargingStationBa
void handle_set_display_message(Call<SetDisplayMessageRequest> call);
void handle_clear_display_message(Call<ClearDisplayMessageRequest> call);

// Functional Block P: DataTransfer
void handle_data_transfer_req(Call<DataTransferRequest> call);

// Generates async sending callbacks
template <class RequestType, class ResponseType>
std::function<ResponseType(RequestType)> send_callback(MessageType expected_response_message_type) {
Expand Down
60 changes: 60 additions & 0 deletions include/ocpp/v201/functional_blocks/data_transfer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#pragma once

#include <ocpp/v201/message_dispatcher.hpp>
#include <ocpp/v201/message_handler.hpp>
#include <ocpp/v201/messages/DataTransfer.hpp>

namespace ocpp {
namespace v201 {

class DataTransferInterface : public MessageHandlerInterface {

public:
virtual ~DataTransferInterface(){};

/// \brief Sends a DataTransfer.req message to the CSMS using the given parameters
/// \param vendorId
/// \param messageId
/// \param data
/// \return DataTransferResponse containing the result from CSMS
virtual std::optional<DataTransferResponse> data_transfer_req(const CiString<255>& vendorId,
const std::optional<CiString<50>>& messageId,
const std::optional<json>& data) = 0;

/// \brief Sends a DataTransfer.req message to the CSMS using the given \p request
/// \param request message shall be sent to the CSMS
/// \return DataTransferResponse containing the result from CSMS. In case no response is received from the CSMS
/// because the message timed out or the charging station is offline, std::nullopt is returned
virtual std::optional<DataTransferResponse> data_transfer_req(const DataTransferRequest& request) = 0;
};

class DataTransfer : public DataTransferInterface {

private:
MessageDispatcherInterface<MessageType>& message_dispatcher;
std::optional<std::function<DataTransferResponse(const DataTransferRequest& request)>> data_transfer_callback;
std::chrono::seconds response_timeout;

public:
DataTransfer(MessageDispatcherInterface<MessageType>& message_dispatcher,
const std::optional<std::function<DataTransferResponse(const DataTransferRequest& request)>>&
data_transfer_callback,
const std::chrono::seconds response_timeout) :
message_dispatcher(message_dispatcher),
data_transfer_callback(data_transfer_callback),
response_timeout(response_timeout){};

void handle_message(const EnhancedMessage<MessageType>& message) override;

std::optional<DataTransferResponse> data_transfer_req(const CiString<255>& vendorId,
const std::optional<CiString<50>>& messageId,
const std::optional<json>& data) override;

std::optional<DataTransferResponse> data_transfer_req(const DataTransferRequest& request) override;
};

} // namespace v201
} // namespace ocpp
39 changes: 39 additions & 0 deletions include/ocpp/v201/message_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#pragma once

#include <ocpp/common/message_queue.hpp>

namespace ocpp {
namespace v201 {

/// \brief Interface for handling OCPP2.0.1 CALL messages from the CSMS. Classes implementing a functional block shall
/// extend this interface.
class MessageHandlerInterface {

public:
virtual ~MessageHandlerInterface() {
}
/// \brief Handles the given \p message from the CSMS. This includes dispatching a CALLRESULT as a response to the
/// incoming \p message .
/// @param message
virtual void handle_message(const EnhancedMessage<MessageType>& message) = 0;
};

class MessageTypeNotImplementedException : public std::exception {
private:
std::string message;

public:
MessageTypeNotImplementedException(MessageType message_type) :
message("Message is not implemented: " + conversions::messagetype_to_string(message_type)) {
}

const char* what() const noexcept override {
return message.c_str();
}
};

} // namespace v201
} // namespace ocpp
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ if(LIBOCPP_ENABLE_V201)
ocpp/v201/component_state_manager.cpp
ocpp/v201/connectivity_manager.cpp
ocpp/v201/message_dispatcher.cpp
ocpp/v201/functional_blocks/data_transfer.cpp
)
add_subdirectory(ocpp/v201/messages)
endif()
Expand Down
Loading

0 comments on commit 18ca071

Please sign in to comment.