ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
call_types.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_COMMON_CALL_TYPES_HPP
4#define OCPP_COMMON_CALL_TYPES_HPP
5
6#include <iostream>
7#include <nlohmann/json_fwd.hpp>
8#include <sstream>
9#include <stdexcept>
10
11#include <chrono>
12#include <cstddef>
13#include <iostream>
14#include <string>
15
16#include <ocpp/common/cistring.hpp>
17
18using json = nlohmann::json;
19
20namespace ocpp {
21
22// The locations inside the message array
23const auto MESSAGE_TYPE_ID = 0;
24const auto MESSAGE_ID = 1;
25const auto CALL_ACTION = 2;
26const auto CALL_PAYLOAD = 3;
27const auto CALLRESULT_PAYLOAD = 2;
28const auto CALLERROR_ERROR_CODE = 2;
29const auto CALLERROR_ERROR_DESCRIPTION = 3;
30const auto CALLERROR_ERROR_DETAILS = 4;
31
34class MessageId : public CiString<36> {
36};
37
39bool operator<(const MessageId& lhs, const MessageId& rhs);
40
42void to_json(json& j, const MessageId& k);
43
45void from_json(const json& j, MessageId& k);
46
48enum class MessageTypeId {
49 CALL = 2,
50 CALLRESULT = 3,
51 CALLERROR = 4,
52 UNKNOWN,
53};
54
57MessageId create_message_id();
58
60template <class T> struct Call {
61 T msg;
62 MessageId uniqueId;
63
65 Call() {
66 }
67
69 explicit Call(T msg) {
70 this->msg = msg;
71 this->uniqueId = create_message_id();
72 }
73
75 Call(T msg, MessageId uniqueId) {
76 this->msg = msg;
77 this->uniqueId = uniqueId;
78 }
79
81 friend void to_json(json& j, const Call& c) {
82 j = json::array();
83 j.push_back(MessageTypeId::CALL);
84 j.push_back(c.uniqueId.get());
85 j.push_back(c.msg.get_type());
86 j.push_back(json(c.msg));
87 }
88
90 friend void from_json(const json& j, Call& c) {
91 // the required parts of the message
92 c.msg = j.at(CALL_PAYLOAD);
93 c.uniqueId.set(j.at(MESSAGE_ID));
94 }
95
98 friend std::ostream& operator<<(std::ostream& os, const Call& c) {
99 os << json(c).dump(4);
100 return os;
101 }
102};
103
105template <class T> struct CallResult {
106 T msg;
107 MessageId uniqueId;
108
111 }
112
114 CallResult(T msg, MessageId uniqueId) {
115 this->msg = msg;
116 this->uniqueId = uniqueId;
117 }
118
120 friend void to_json(json& j, const CallResult& c) {
121 j = json::array();
122 j.push_back(MessageTypeId::CALLRESULT);
123 j.push_back(c.uniqueId.get());
124 j.push_back(json(c.msg));
125 }
126
128 friend void from_json(const json& j, CallResult& c) {
129 // the required parts of the message
130 c.msg = j.at(CALLRESULT_PAYLOAD);
131 c.uniqueId.set(j.at(MESSAGE_ID));
132 }
133
136 friend std::ostream& operator<<(std::ostream& os, const CallResult& c) {
137 os << json(c).dump(4);
138 return os;
139 }
140};
141
143struct CallError {
144 MessageId uniqueId;
145 std::string errorCode;
146 std::string errorDescription;
147 json errorDetails;
148
150 CallError();
151
154 CallError(const MessageId& uniqueId, const std::string& errorCode, const std::string& errorDescription,
155 const json& errorDetails);
156};
157
159void to_json(json& j, const CallError& c);
160
162void from_json(const json& j, CallError& c);
163
166std::ostream& operator<<(std::ostream& os, const CallError& c);
167
168} // namespace ocpp
169
170#endif
Contains a CaseInsensitive string implementation that only allows printable ASCII characters.
Definition: cistring.hpp:16
CiString()
Creates a string.
Definition: cistring.hpp:30
Contains a MessageId implementation based on a case insensitive string with a maximum length of 36 pr...
Definition: call_types.hpp:34
void set(const std::string &data, StringTooLarge to_large=StringTooLarge::Throw)
Sets the content of the string to the given data.
Definition: string.hpp:49
std::string get() const
Provides a std::string representation of the string.
Definition: string.hpp:44
Contains a OCPP CallError message.
Definition: call_types.hpp:143
CallError()
Creates a new CallError message object.
Definition: types.cpp:86
Contains a OCPP CallResult message.
Definition: call_types.hpp:105
friend void to_json(json &j, const CallResult &c)
Conversion from a given CallResult message c to a given json object j.
Definition: call_types.hpp:120
CallResult()
Creates a new CallResult message object.
Definition: call_types.hpp:110
friend std::ostream & operator<<(std::ostream &os, const CallResult &c)
Writes the given case CallResult c to the given output stream os.
Definition: call_types.hpp:136
friend void from_json(const json &j, CallResult &c)
Conversion from a given json object j to a given CallResult message c.
Definition: call_types.hpp:128
CallResult(T msg, MessageId uniqueId)
Creates a new CallResult message object with the given OCPP message msg and uniqueID.
Definition: call_types.hpp:114
Contains a OCPP Call message.
Definition: call_types.hpp:60
friend std::ostream & operator<<(std::ostream &os, const Call &c)
Writes the given case Call c to the given output stream os.
Definition: call_types.hpp:98
friend void to_json(json &j, const Call &c)
Conversion from a given Call message c to a given json object j.
Definition: call_types.hpp:81
Call(T msg)
Creates a new Call message object with the given OCPP message msg.
Definition: call_types.hpp:69
Call()
Creates a new Call message object.
Definition: call_types.hpp:65
Call(T msg, MessageId uniqueId)
Creates a new Call message object with the given OCPP message msg and uniqueId.
Definition: call_types.hpp:75
friend void from_json(const json &j, Call &c)
Conversion from a given json object j to a given Call message c.
Definition: call_types.hpp:90