-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved DataTransfer functionality to DataTransfer functional block usi…
…ng the targeted design. Added test cases for the new functional block Signed-off-by: Piet Gömpel <[email protected]>
- Loading branch information
Showing
9 changed files
with
369 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
|
||
#pragma once | ||
|
||
#include <ocpp/v201/message_dispatcher.hpp> | ||
#include <ocpp/v201/messages/DataTransfer.hpp> | ||
|
||
namespace ocpp { | ||
namespace v201 { | ||
|
||
class DataTransferInterface { | ||
|
||
public: | ||
/// \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; | ||
|
||
/// \brief Handles the given DataTransfer.req \p call by the CSMS by responding with a CallResult | ||
virtual void handle_data_transfer_req(Call<DataTransferRequest> call) = 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; | ||
std::function<bool()> is_websocket_connected; | ||
|
||
public: | ||
DataTransfer(MessageDispatcherInterface<MessageType>& message_dispatcher, | ||
const std::optional<std::function<DataTransferResponse(const DataTransferRequest& request)>>& | ||
data_transfer_callback, | ||
const std::function<bool()> is_websocket_connected, const std::chrono::seconds response_timeout) : | ||
message_dispatcher(message_dispatcher), | ||
data_transfer_callback(data_transfer_callback), | ||
is_websocket_connected(is_websocket_connected), | ||
response_timeout(response_timeout){}; | ||
|
||
void handle_data_transfer_req(Call<DataTransferRequest> call) 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
|
||
#include <ocpp/common/constants.hpp> | ||
#include <ocpp/v201/functional_blocks/data_transfer.hpp> | ||
|
||
namespace ocpp { | ||
namespace v201 { | ||
|
||
void DataTransfer::handle_data_transfer_req(Call<DataTransferRequest> call) { | ||
const auto msg = call.msg; | ||
DataTransferResponse response; | ||
response.status = DataTransferStatusEnum::UnknownVendorId; | ||
|
||
if (this->data_transfer_callback.has_value()) { | ||
response = this->data_transfer_callback.value()(call.msg); | ||
} else { | ||
response.status = DataTransferStatusEnum::UnknownVendorId; | ||
EVLOG_warning << "Received a DataTransferRequest but no data transfer callback was registered"; | ||
} | ||
|
||
ocpp::CallResult<DataTransferResponse> call_result(response, call.uniqueId); | ||
this->message_dispatcher.dispatch_call_result(call_result); | ||
} | ||
|
||
std::optional<DataTransferResponse> DataTransfer::data_transfer_req(const CiString<255>& vendorId, | ||
const std::optional<CiString<50>>& messageId, | ||
const std::optional<json>& data) { | ||
DataTransferRequest req; | ||
req.vendorId = vendorId; | ||
req.messageId = messageId; | ||
req.data = data; | ||
|
||
return this->data_transfer_req(req); | ||
} | ||
|
||
std::optional<DataTransferResponse> DataTransfer::data_transfer_req(const DataTransferRequest& request) { | ||
DataTransferResponse response; | ||
response.status = DataTransferStatusEnum::Rejected; | ||
|
||
ocpp::Call<DataTransferRequest> call(request); | ||
auto data_transfer_future = this->message_dispatcher.dispatch_call_async(call); | ||
|
||
if (not this->is_websocket_connected()) { | ||
return std::nullopt; | ||
} | ||
|
||
if (data_transfer_future.wait_for(this->response_timeout) == std::future_status::timeout) { | ||
EVLOG_warning << "Waiting for DataTransfer.conf future timed out"; | ||
return std::nullopt; | ||
} | ||
|
||
auto enhanced_message = data_transfer_future.get(); | ||
|
||
if (enhanced_message.offline) { | ||
return std::nullopt; | ||
} | ||
|
||
if (enhanced_message.messageType == MessageType::DataTransferResponse) { | ||
try { | ||
ocpp::CallResult<DataTransferResponse> call_result = enhanced_message.message; | ||
response = call_result.msg; | ||
} catch (const EnumConversionException& e) { | ||
EVLOG_error << "EnumConversionException during handling of message: " << e.what(); | ||
auto call_error = CallError(enhanced_message.uniqueId, "FormationViolation", e.what(), json({})); | ||
this->message_dispatcher.dispatch_call_error(call_error); | ||
return std::nullopt; | ||
} catch (const json::exception& e) { | ||
EVLOG_error << "Unable to parse DataTransfer.conf from CSMS: " << enhanced_message.message; | ||
auto call_error = CallError(enhanced_message.uniqueId, "FormationViolation", e.what(), json({})); | ||
this->message_dispatcher.dispatch_call_error(call_error); | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
|
||
}; // namespace v201 | ||
} // namespace ocpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
target_include_directories(libocpp_unit_tests PUBLIC | ||
../mocks | ||
${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
target_sources(libocpp_unit_tests PRIVATE | ||
test_data_transfer.cpp) |
Oops, something went wrong.