Skip to content

Commit

Permalink
Moved functionality to create message id from message_queue to call_t…
Browse files Browse the repository at this point in the history
…ypes and made it a free function. Removed unused imports and usage of boost/uuid headers (#869)

Signed-off-by: Piet Gömpel <[email protected]>
  • Loading branch information
Pietfried authored Nov 19, 2024
1 parent 0fbecf4 commit 350430f
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 106 deletions.
12 changes: 11 additions & 1 deletion include/ocpp/common/call_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ enum class MessageTypeId {
UNKNOWN,
};

/// \brief Creates a unique message ID
/// \returns the unique message ID
MessageId create_message_id();

/// \brief Contains a OCPP Call message
template <class T> struct Call {
T msg;
Expand All @@ -61,7 +65,13 @@ template <class T> struct Call {
Call() {
}

/// \brief Creates a new Call message object with the given OCPP message \p msg and \p uniqueID
/// \brief Creates a new Call message object with the given OCPP message \p msg
explicit Call(T msg) {
this->msg = msg;
this->uniqueId = create_message_id();
}

/// \brief Creates a new Call message object with the given OCPP message \p msg and \p uniqueId
Call(T msg, MessageId uniqueId) {
this->msg = msg;
this->uniqueId = uniqueId;
Expand Down
6 changes: 0 additions & 6 deletions include/ocpp/common/charging_station_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ class ChargingStationBase {
boost::asio::io_service io_service;
std::thread io_service_thread;

boost::uuids::random_generator uuid_generator;

/// \brief Generates a uuid
/// \return uuid
std::string uuid();

public:
/// \brief Constructor for ChargingStationBase
/// \param evse_security Pointer to evse_security that manages security related operations; if nullptr
Expand Down
20 changes: 3 additions & 17 deletions include/ocpp/common/message_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#include <set>
#include <thread>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

#include <everest/timer.hpp>

#include <ocpp/common/call_types.hpp>
Expand Down Expand Up @@ -186,7 +182,6 @@ template <typename M> class MessageQueue {
bool running;
bool new_message;
bool is_registration_status_accepted;
boost::uuids::random_generator uuid_generator;
std::recursive_mutex next_message_mutex;
std::optional<MessageId> next_message_to_send;

Expand Down Expand Up @@ -405,7 +400,6 @@ template <typename M> class MessageQueue {
running(true),
new_message(false),
is_registration_status_accepted(false),
uuid_generator(boost::uuids::random_generator()),
start_transaction_message_retry_callback(start_transaction_message_retry_callback) {

this->send_callback = send_callback;
Expand Down Expand Up @@ -641,7 +635,7 @@ template <typename M> class MessageQueue {
// MeterValues have to be delivered in chronological order

// intentionally break this message for testing...
// message->message[CALL_PAYLOAD]["broken"] = this->createMessageId();
// message->message[CALL_PAYLOAD]["broken"] = ocpp::create_message_id();
this->add_to_transaction_message_queue(control_message);
} else {
// all other messages are allowed to "jump the queue" to improve user experience
Expand Down Expand Up @@ -833,7 +827,7 @@ template <typename M> class MessageQueue {
EVLOG_warning << "Message shall be persisted and will therefore be sent again";
// Generate a new message ID for the retry
const auto old_message_id = this->in_flight->message[MESSAGE_ID];
this->in_flight->message[MESSAGE_ID] = this->createMessageId();
this->in_flight->message[MESSAGE_ID] = ocpp::create_message_id();
if (this->config.transaction_message_retry_interval > 0) {
// exponential backoff
this->in_flight->timestamp =
Expand Down Expand Up @@ -890,7 +884,7 @@ template <typename M> class MessageQueue {
} else if (is_boot_notification_message(this->in_flight->messageType)) {
EVLOG_warning << "Message is BootNotification.req and will therefore be sent again";
// Generate a new message ID for the retry
this->in_flight->message[MESSAGE_ID] = this->createMessageId();
this->in_flight->message[MESSAGE_ID] = ocpp::create_message_id();
// Spec does not define how to handle retries for BootNotification.req: We use the
// the boot_notification_retry_interval_seconds
this->in_flight->timestamp =
Expand Down Expand Up @@ -1013,14 +1007,6 @@ template <typename M> class MessageQueue {
this->config.message_timeout_seconds = timeout;
}

/// \brief Creates a unique message ID
/// \returns the unique message ID
MessageId createMessageId() {
std::stringstream s;
s << this->uuid_generator();
return MessageId(s.str());
}

/// \brief Adds the given \p transaction_id to the message_id_transaction_id_map using the key \p
/// stop_transaction_message_id
void add_stopped_transaction_id(std::string stop_transaction_message_id, int32_t transaction_id) {
Expand Down
4 changes: 0 additions & 4 deletions include/ocpp/v16/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#include <ocpp/v16/ocpp_types.hpp>
#include <ocpp/v16/types.hpp>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

namespace ocpp {
namespace v16 {

Expand Down
3 changes: 1 addition & 2 deletions include/ocpp/v201/charge_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,8 @@ class ChargePoint : public ChargePointInterface, private ocpp::ChargingStationBa
template <class RequestType, class ResponseType>
std::function<ResponseType(RequestType)> send_callback(MessageType expected_response_message_type) {
return [this, expected_response_message_type](auto request) {
MessageId message_id = MessageId(to_string(this->uuid_generator()));
const auto enhanced_response =
this->message_dispatcher->dispatch_call_async(ocpp::Call<RequestType>(request, message_id)).get();
this->message_dispatcher->dispatch_call_async(ocpp::Call<RequestType>(request)).get();
if (enhanced_response.messageType != expected_response_message_type) {
throw UnexpectedMessageTypeFromCSMS(
std::string("Got unexpected message type from CSMS, expected: ") +
Expand Down
12 changes: 12 additions & 0 deletions lib/ocpp/common/call_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@

#include <ocpp/common/call_types.hpp>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

namespace ocpp {

MessageId create_message_id() {
static boost::uuids::random_generator uuid_generator;
boost::uuids::uuid uuid = uuid_generator();
std::stringstream s;
s << uuid;
return MessageId(s.str());
}

bool operator<(const MessageId& lhs, const MessageId& rhs) {
return lhs.get() < rhs.get();
}
Expand Down
9 changes: 1 addition & 8 deletions lib/ocpp/common/charging_station_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

namespace ocpp {
ChargingStationBase::ChargingStationBase(const std::shared_ptr<EvseSecurity> evse_security,
const std::optional<SecurityConfiguration> security_configuration) :
uuid_generator(boost::uuids::random_generator()) {
const std::optional<SecurityConfiguration> security_configuration) {

if (evse_security != nullptr) {
this->evse_security = evse_security;
Expand All @@ -28,10 +27,4 @@ ChargingStationBase::~ChargingStationBase() {
io_service_thread.join();
}

std::string ChargingStationBase::uuid() {
std::stringstream s;
s << this->uuid_generator();
return s.str();
}

} // namespace ocpp
36 changes: 18 additions & 18 deletions lib/ocpp/v16/charge_point_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ void ChargePointImpl::heartbeat(bool initiated_by_trigger_message) {
EVLOG_debug << "Sending heartbeat";
HeartbeatRequest req;

ocpp::Call<HeartbeatRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<HeartbeatRequest> call(req);
this->message_dispatcher->dispatch_call(call, initiated_by_trigger_message);
}

Expand All @@ -435,7 +435,7 @@ void ChargePointImpl::boot_notification(bool initiated_by_trigger_message) {
req.meterSerialNumber = this->configuration->getMeterSerialNumber();
req.meterType = this->configuration->getMeterType();

ocpp::Call<BootNotificationRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<BootNotificationRequest> call(req);
this->message_dispatcher->dispatch_call(call, initiated_by_trigger_message);
}

Expand Down Expand Up @@ -953,7 +953,7 @@ void ChargePointImpl::send_meter_value(int32_t connector, MeterValue meter_value
}

MeterValuesRequest req;
const auto message_id = this->message_queue->createMessageId();
const auto message_id = ocpp::create_message_id();
// connector = 0 designates the main measurement
// connector > 0 designates a connector of the charge point
req.connectorId = connector;
Expand Down Expand Up @@ -2618,7 +2618,7 @@ void ChargePointImpl::sign_certificate(const ocpp::CertificateSigningUseEnum& ce

req.csr = response.csr.value();

ocpp::Call<SignCertificateRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<SignCertificateRequest> call(req);
this->message_dispatcher->dispatch_call(call, initiated_by_trigger_message);
}

Expand Down Expand Up @@ -2817,7 +2817,7 @@ void ChargePointImpl::securityEventNotification(const CiString<50>& event_type,
}

if (critical_security_event and !this->configuration->getDisableSecurityEventNotifications()) {
ocpp::Call<SecurityEventNotificationRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<SecurityEventNotificationRequest> call(req);
this->message_dispatcher->dispatch_call(call);
}

Expand All @@ -2838,7 +2838,7 @@ void ChargePointImpl::log_status_notification(UploadLogStatusEnumType status, in
this->log_status = status;
this->log_status_request_id = requestId;

ocpp::Call<LogStatusNotificationRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<LogStatusNotificationRequest> call(req);
this->message_dispatcher->dispatch_call(call, initiated_by_trigger_message);
}

Expand All @@ -2865,7 +2865,7 @@ void ChargePointImpl::signed_firmware_update_status_notification(FirmwareStatusE
this->signed_firmware_status = status;
this->signed_firmware_status_request_id = requestId;

ocpp::Call<SignedFirmwareStatusNotificationRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<SignedFirmwareStatusNotificationRequest> call(req);
this->message_dispatcher->dispatch_call(call, initiated_by_trigger_message);

if (status == FirmwareStatusEnumType::InvalidSignature) {
Expand Down Expand Up @@ -3234,7 +3234,7 @@ void ChargePointImpl::status_notification(const int32_t connector, const ChargeP
request.info = info;
request.vendorId = vendor_id;
request.vendorErrorCode = vendor_error_code;
ocpp::Call<StatusNotificationRequest> call(request, this->message_queue->createMessageId());
ocpp::Call<StatusNotificationRequest> call(request);
this->message_dispatcher->dispatch_call(call, initiated_by_trigger_message);
}

Expand Down Expand Up @@ -3280,7 +3280,7 @@ IdTagInfo ChargePointImpl::authorize_id_token(CiString<20> idTag, const bool aut
AuthorizeRequest req;
req.idTag = idTag;

ocpp::Call<AuthorizeRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<AuthorizeRequest> call(req);

auto authorize_future = this->message_dispatcher->dispatch_call_async(call);

Expand Down Expand Up @@ -3494,7 +3494,7 @@ ocpp::v201::AuthorizeResponse ChargePointImpl::data_transfer_pnc_authorize(
req.data.emplace(json(authorize_req).dump());

// Send the DataTransfer(Authorize) to the CSMS
Call<DataTransferRequest> call(req, this->message_queue->createMessageId());
Call<DataTransferRequest> call(req);
auto authorize_future = this->message_dispatcher->dispatch_call_async(call);

if (authorize_future.wait_for(DEFAULT_WAIT_FOR_FUTURE_TIMEOUT) == std::future_status::timeout) {
Expand Down Expand Up @@ -3576,7 +3576,7 @@ void ChargePointImpl::data_transfer_pnc_sign_certificate() {
csr_req.certificateType = ocpp::v201::CertificateSigningUseEnum::V2GCertificate;
req.data.emplace(json(csr_req).dump());

Call<DataTransferRequest> call(req, this->message_queue->createMessageId());
Call<DataTransferRequest> call(req);
this->message_dispatcher->dispatch_call(call);
}

Expand All @@ -3602,7 +3602,7 @@ void ChargePointImpl::data_transfer_pnc_get_15118_ev_certificate(

req.data.emplace(json(cert_req).dump());

Call<DataTransferRequest> call(req, this->message_queue->createMessageId());
Call<DataTransferRequest> call(req);
auto future = this->message_dispatcher->dispatch_call_async(call);

if (future.wait_for(DEFAULT_WAIT_FOR_FUTURE_TIMEOUT) == std::future_status::timeout) {
Expand Down Expand Up @@ -3652,7 +3652,7 @@ void ChargePointImpl::data_transfer_pnc_get_certificate_status(const ocpp::v201:

req.data.emplace(json(cert_status_req).dump());

Call<DataTransferRequest> call(req, this->message_queue->createMessageId());
Call<DataTransferRequest> call(req);
auto future = this->message_dispatcher->dispatch_call_async(call);

if (future.wait_for(DEFAULT_WAIT_FOR_FUTURE_TIMEOUT) == std::future_status::timeout) {
Expand Down Expand Up @@ -3907,7 +3907,7 @@ std::optional<DataTransferResponse> ChargePointImpl::data_transfer(const CiStrin

DataTransferResponse response;
response.status = DataTransferStatus::Rejected;
ocpp::Call<DataTransferRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<DataTransferRequest> call(req);
auto data_transfer_future = this->message_dispatcher->dispatch_call_async(call);

if (this->websocket == nullptr or !this->websocket->is_connected()) {
Expand Down Expand Up @@ -3987,7 +3987,7 @@ void ChargePointImpl::start_transaction(std::shared_ptr<Transaction> transaction
req.idTag = transaction->get_id_tag();
req.meterStart = std::round(transaction->get_start_energy_wh()->energy_Wh);
req.timestamp = transaction->get_start_energy_wh()->timestamp;
const auto message_id = this->message_queue->createMessageId();
const auto message_id = ocpp::create_message_id();

try {
this->database_handler->insert_transaction(
Expand Down Expand Up @@ -4164,7 +4164,7 @@ void ChargePointImpl::stop_transaction(int32_t connector, Reason reason, std::op
req.transactionData.emplace(transaction_data);
}

auto message_id = this->message_queue->createMessageId();
auto message_id = ocpp::create_message_id();
ocpp::Call<StopTransactionRequest> call(req, message_id);

const auto max_message_size = this->configuration->getMaxMessageSize();
Expand Down Expand Up @@ -4322,7 +4322,7 @@ void ChargePointImpl::diagnostic_status_notification(DiagnosticsStatus status, b
req.status = status;
this->diagnostics_status = status;

ocpp::Call<DiagnosticsStatusNotificationRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<DiagnosticsStatusNotificationRequest> call(req);
this->message_dispatcher->dispatch_call_async(call, true);
}

Expand All @@ -4344,7 +4344,7 @@ void ChargePointImpl::firmware_status_notification(FirmwareStatus status, bool i

this->firmware_status = status;

ocpp::Call<FirmwareStatusNotificationRequest> call(req, this->message_queue->createMessageId());
ocpp::Call<FirmwareStatusNotificationRequest> call(req);
this->message_dispatcher->dispatch_call_async(call, initiated_by_trigger_message);

if (this->firmware_update_is_pending) {
Expand Down
Loading

0 comments on commit 350430f

Please sign in to comment.