libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
e3_encoder.hpp
Go to the documentation of this file.
1/**
2 * @file e3_encoder.hpp
3 * @brief Abstract E3 Encoder interface for PDU encoding/decoding
4 *
5 * Defines the abstract interface for encoding and decoding E3AP PDUs.
6 * Ported from the original C implementation's e3ap_handler functions.
7 *
8 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12#ifndef LIBE3_E3_ENCODER_HPP
13#define LIBE3_E3_ENCODER_HPP
14
15#include "types.hpp"
16#include <memory>
17#include <optional>
18#include <tl/expected.hpp>
19
20namespace libe3 {
21
22/**
23 * @brief Result type for encoding/decoding operations
24 */
25template<typename T>
26using EncodeResult = tl::expected<T, ErrorCode>;
27
28/**
29 * @brief Abstract base class for E3AP encoders
30 *
31 * This class defines the interface for encoding and decoding E3AP PDUs.
32 * The design allows for multiple encoding formats (ASN.1, JSON) while
33 * maintaining the same API.
34 *
35 * Derived classes:
36 * - ASN1E3Encoder: ASN.1 PER encoding (O-RAN standard)
37 * - JsonE3Encoder: JSON encoding (for development/debugging)
38 */
39class E3Encoder {
40public:
41 virtual ~E3Encoder() = default;
42
43 // Non-copyable, movable
44 E3Encoder(const E3Encoder&) = delete;
45 E3Encoder& operator=(const E3Encoder&) = delete;
46 /// Movable.
47 E3Encoder(E3Encoder&&) = default;
48 /// Movable.
49 /// @return This encoder
51
52 /**
53 * @brief Encode a PDU to bytes
54 * @param pdu PDU to encode
55 * @return Encoded message on success, error code on failure
56 */
57 virtual EncodeResult<EncodedMessage> encode(const Pdu& pdu) = 0;
58
59 /**
60 * @brief Decode bytes to a PDU
61 * @param encoded Encoded message to decode
62 * @return Decoded PDU on success, error code on failure
63 */
64 virtual EncodeResult<Pdu> decode(const EncodedMessage& encoded) = 0;
65
66 /**
67 * @brief Decode bytes to a PDU
68 * @param data Raw data buffer
69 * @param size Size of data
70 * @return Decoded PDU on success, error code on failure
71 */
72 virtual EncodeResult<Pdu> decode(const uint8_t* data, size_t size) = 0;
73
74 /**
75 * @brief Get the encoding format
76 * @return The format this encoder reads and writes
77 */
78 virtual EncodingFormat format() const noexcept = 0;
79
80 // Convenience methods for creating specific PDUs
81
82 /**
83 * @brief Create and encode a Setup Request PDU
84 * @param message_id Unique message ID
85 * @param e3ap_protocol_version E3AP protocol version string (e.g., "0.0.0"; 1..32 bytes over ASN.1)
86 * @param dapp_name Name of the dApp (1..64 bytes over ASN.1)
87 * @param dapp_version Version of the dApp (e.g., "0.0.0"; 1..32 bytes over ASN.1)
88 * @param vendor Vendor name (1..64 bytes over ASN.1)
89 * @return Encoded message on success, error code on failure
90 */
92 uint32_t message_id,
93 const std::string& e3ap_protocol_version,
94 const std::string& dapp_name,
95 const std::string& dapp_version,
96 const std::string& vendor
97 );
98
99 /**
100 * @brief Create and encode a Setup Response PDU
101 * @param message_id Unique message ID
102 * @param request_id ID of the corresponding SetupRequest
103 * @param response_code Response code (positive/negative)
104 * @param e3ap_protocol_version E3AP protocol version (optional)
105 * @param dapp_identifier Assigned dApp identifier (optional)
106 * @param ran_identifier RAN identifier
107 * @param ran_function_list List of available RAN functions (optional).
108 * std::nullopt and an empty list both encode with the OPTIONAL
109 * ranFunctionList absent on the wire.
110 * @param cause Why the setup was refused (optional, negative only)
111 * @param detail Text for the cause, up to MAX_DETAIL_SIZE bytes (optional)
112 * @return Encoded message on success, error code on failure
113 */
115 uint32_t message_id,
116 uint32_t request_id,
117 ResponseCode response_code,
118 const std::optional<std::string>& e3ap_protocol_version = std::nullopt,
119 const std::optional<uint32_t>& dapp_identifier = std::nullopt,
120 const std::string& ran_identifier = "",
121 const std::optional<std::vector<RanFunctionDef>>& ran_function_list = std::nullopt,
122 const std::optional<Cause>& cause = std::nullopt,
123 const std::optional<std::string>& detail = std::nullopt
124 );
125
126 /**
127 * @brief Create and encode a Subscription Request PDU
128 * @param message_id Unique message ID
129 * @param dapp_identifier dApp identifier
130 * @param ran_function_identifier RAN function to subscribe to
131 * @param telemetry_identifier_list List of telemetry identifiers
132 * @param control_identifier_list List of control identifiers
133 * @param subscription_time How long to keep the subscription (0-86400 sec, optional)
134 * @return Encoded message on success, error code on failure
135 */
137 uint32_t message_id,
138 uint32_t dapp_identifier,
139 uint32_t ran_function_identifier,
140 const std::vector<uint32_t>& telemetry_identifier_list,
141 const std::vector<uint32_t>& control_identifier_list,
142 const std::optional<uint32_t>& subscription_time = std::nullopt
143 );
144
145 /**
146 * @brief Create and encode a Subscription Delete PDU
147 * @param message_id Unique message ID
148 * @param dapp_identifier dApp identifier
149 * @param subscription_id Subscription ID to delete
150 * @return Encoded message on success, error code on failure
151 */
153 uint32_t message_id,
154 uint32_t dapp_identifier,
155 uint32_t subscription_id
156 );
157
158 /**
159 * @brief Create and encode a Subscription Response PDU
160 * @param message_id Unique message ID
161 * @param request_id ID of the corresponding SubscriptionRequest
162 * @param dapp_identifier dApp identifier
163 * @param response_code Response code (positive/negative)
164 * @param subscription_id Subscription ID (optional)
165 * @param cause Why the subscription was refused (optional, negative only)
166 * @param detail Text for the cause, up to MAX_DETAIL_SIZE bytes (optional)
167 * @return Encoded message on success, error code on failure
168 */
170 uint32_t message_id,
171 uint32_t request_id,
172 uint32_t dapp_identifier,
173 ResponseCode response_code,
174 const std::optional<uint32_t>& subscription_id = std::nullopt,
175 const std::optional<Cause>& cause = std::nullopt,
176 const std::optional<std::string>& detail = std::nullopt
177 );
178
179 /**
180 * @brief Create and encode an Indication Message PDU
181 * @param message_id Unique message ID
182 * @param dapp_identifier dApp identifier
183 * @param ran_function_identifier RAN function identifier
184 * @param protocol_data Protocol data
185 * @return Encoded message on success, error code on failure
186 */
188 uint32_t message_id,
189 uint32_t dapp_identifier,
190 uint32_t ran_function_identifier,
191 const std::vector<uint8_t>& protocol_data
192 );
193
194 /**
195 * @brief Create and encode a dApp Control Action PDU
196 * @param message_id Unique message ID
197 * @param dapp_identifier dApp identifier
198 * @param ran_function_identifier RAN function identifier
199 * @param control_identifier Control identifier
200 * @param action_data Action data
201 * @return Encoded message on success, error code on failure
202 */
204 uint32_t message_id,
205 uint32_t dapp_identifier,
206 uint32_t ran_function_identifier,
207 uint32_t control_identifier,
208 const std::vector<uint8_t>& action_data
209 );
210
211 /**
212 * @brief Create and encode a dApp Report PDU
213 * @param message_id Unique message ID
214 * @param dapp_identifier dApp identifier
215 * @param ran_function_identifier RAN function identifier
216 * @param report_data Report data
217 * @return Encoded message on success, error code on failure
218 */
220 uint32_t message_id,
221 uint32_t dapp_identifier,
222 uint32_t ran_function_identifier,
223 const std::vector<uint8_t>& report_data
224 );
225
226 /**
227 * @brief Create and encode an xApp Control Action PDU
228 * @param message_id Unique message ID
229 * @param dapp_identifier dApp identifier
230 * @param ran_function_identifier RAN function identifier
231 * @param xapp_control_data xApp control data
232 * @return Encoded message on success, error code on failure
233 */
235 uint32_t message_id,
236 uint32_t dapp_identifier,
237 uint32_t ran_function_identifier,
238 const std::vector<uint8_t>& xapp_control_data
239 );
240
241 /**
242 * @brief Create and encode a Message Acknowledgment PDU
243 *
244 * DEPRECATED TO BE REMOVED. libe3 does not send one; use encode_control_response().
245 * @param message_id Unique message ID
246 * @param request_id Message ID of the request being acknowledged
247 * @param response_code Positive or negative
248 * @param cause Why the request was refused (optional, negative only)
249 * @param detail Text for the cause, up to MAX_DETAIL_SIZE bytes (optional)
250 * @return Encoded message on success, error code on failure
251 */
253 uint32_t message_id,
254 uint32_t request_id,
255 ResponseCode response_code,
256 const std::optional<Cause>& cause = std::nullopt,
257 const std::optional<std::string>& detail = std::nullopt
258 );
259
260 /**
261 * @brief Create and encode a Control Response PDU
262 * @param message_id Unique message ID
263 * @param response The response; its `request_id` is the message ID of the control it answers
264 * @return Encoded message on success, error code on failure
265 */
267 uint32_t message_id,
268 const ControlResponse& response
269 );
270
271protected:
272 E3Encoder() = default;
273};
274
275/**
276 * @brief Factory function to create appropriate encoder
277 *
278 * Creates an encoder instance based on the encoding format specified.
279 *
280 * @param format Encoding format to use
281 * @return Unique pointer to created encoder, nullptr on failure
282 */
284
285} // namespace libe3
286
287#endif // LIBE3_E3_ENCODER_HPP
EncodeResult< EncodedMessage > encode_subscription_response(uint32_t message_id, uint32_t request_id, uint32_t dapp_identifier, ResponseCode response_code, const std::optional< uint32_t > &subscription_id=std::nullopt, const std::optional< Cause > &cause=std::nullopt, const std::optional< std::string > &detail=std::nullopt)
Create and encode a Subscription Response PDU.
virtual ~E3Encoder()=default
EncodeResult< EncodedMessage > encode_setup_request(uint32_t message_id, const std::string &e3ap_protocol_version, const std::string &dapp_name, const std::string &dapp_version, const std::string &vendor)
Create and encode a Setup Request PDU.
E3Encoder & operator=(E3Encoder &&)=default
E3Encoder & operator=(const E3Encoder &)=delete
E3Encoder(E3Encoder &&)=default
Movable.
virtual EncodeResult< Pdu > decode(const EncodedMessage &encoded)=0
Decode bytes to a PDU.
E3Encoder(const E3Encoder &)=delete
EncodeResult< EncodedMessage > encode_dapp_control_action(uint32_t message_id, uint32_t dapp_identifier, uint32_t ran_function_identifier, uint32_t control_identifier, const std::vector< uint8_t > &action_data)
Create and encode a dApp Control Action PDU.
EncodeResult< EncodedMessage > encode_message_ack(uint32_t message_id, uint32_t request_id, ResponseCode response_code, const std::optional< Cause > &cause=std::nullopt, const std::optional< std::string > &detail=std::nullopt)
Create and encode a Message Acknowledgment PDU.
virtual EncodingFormat format() const noexcept=0
Get the encoding format.
EncodeResult< EncodedMessage > encode_xapp_control_action(uint32_t message_id, uint32_t dapp_identifier, uint32_t ran_function_identifier, const std::vector< uint8_t > &xapp_control_data)
Create and encode an xApp Control Action PDU.
EncodeResult< EncodedMessage > encode_subscription_request(uint32_t message_id, uint32_t dapp_identifier, uint32_t ran_function_identifier, const std::vector< uint32_t > &telemetry_identifier_list, const std::vector< uint32_t > &control_identifier_list, const std::optional< uint32_t > &subscription_time=std::nullopt)
Create and encode a Subscription Request PDU.
EncodeResult< EncodedMessage > encode_subscription_delete(uint32_t message_id, uint32_t dapp_identifier, uint32_t subscription_id)
Create and encode a Subscription Delete PDU.
virtual EncodeResult< Pdu > decode(const uint8_t *data, size_t size)=0
Decode bytes to a PDU.
virtual EncodeResult< EncodedMessage > encode(const Pdu &pdu)=0
Encode a PDU to bytes.
EncodeResult< EncodedMessage > encode_indication_message(uint32_t message_id, uint32_t dapp_identifier, uint32_t ran_function_identifier, const std::vector< uint8_t > &protocol_data)
Create and encode an Indication Message PDU.
EncodeResult< EncodedMessage > encode_control_response(uint32_t message_id, const ControlResponse &response)
Create and encode a Control Response PDU.
EncodeResult< EncodedMessage > encode_setup_response(uint32_t message_id, uint32_t request_id, ResponseCode response_code, const std::optional< std::string > &e3ap_protocol_version=std::nullopt, const std::optional< uint32_t > &dapp_identifier=std::nullopt, const std::string &ran_identifier="", const std::optional< std::vector< RanFunctionDef > > &ran_function_list=std::nullopt, const std::optional< Cause > &cause=std::nullopt, const std::optional< std::string > &detail=std::nullopt)
Create and encode a Setup Response PDU.
EncodeResult< EncodedMessage > encode_dapp_report(uint32_t message_id, uint32_t dapp_identifier, uint32_t ran_function_identifier, const std::vector< uint8_t > &report_data)
Create and encode a dApp Report PDU.
std::unique_ptr< E3Encoder > create_encoder(EncodingFormat format)
Factory function to create appropriate encoder.
ResponseCode
E3AP response codes.
Definition types.hpp:103
EncodingFormat
E3AP encoding formats supported by the library.
Definition types.hpp:53
tl::expected< T, ErrorCode > EncodeResult
Result type for encoding/decoding operations.
Cause
Why a RAN refused a request (E3-Cause).
Definition types.hpp:115
E3AP Control Response structure.
Definition types.hpp:366
Encoded message wrapper.
Definition types.hpp:219
Generic E3AP PDU structure.
Definition types.hpp:448
RAN Function Definition for Setup Response.
Definition types.hpp:263
E3AP Protocol Types - Vendor-neutral type definitions.