libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
asn1_encoder.cpp
Go to the documentation of this file.
1/**
2 * @file asn1_encoder.cpp
3 * @brief ASN.1 APER Encoder implementation
4 *
5 * Ported from the original C implementation's e3ap_handler.c ASN.1 functions.
6 *
7 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11#include "asn1_encoder.hpp"
12#include "libe3/logger.hpp"
13
14// ASN.1 generated headers
15extern "C" {
16#include "E3-PDU.h"
17#include "E3-SetupRequest.h"
18#include "E3-SetupResponse.h"
19#include "E3-RanFunctionDefinition.h"
20#include "E3-SubscriptionRequest.h"
21#include "E3-SubscriptionDelete.h"
22#include "E3-SubscriptionResponse.h"
23#include "E3-IndicationMessage.h"
24#include "E3-DAppControlAction.h"
25#include "E3-DAppReport.h"
26#include "E3-XAppControlAction.h"
27#include "E3-ReleaseMessage.h"
28#include "E3-MessageAck.h"
29#include "E3-ControlResponse.h"
30#include "E3-Timestamp.h"
31}
32
33#include <algorithm>
34#include <cstring>
35
36namespace libe3 {
37
38namespace {
39constexpr const char* LOG_TAG = "Asn1Enc";
40
41// E3-Timestamp is an unconstrained INTEGER, which asn1c renders as a native
42// signed integer rather than the arbitrary-precision INTEGER_t. That keeps the
43// codec allocation-free on the indication path, at the cost of being tied to
44// the platform's long. Fail the build rather than silently truncating
45// Pdu::timestamp anywhere that type is narrower than the field it carries.
46//
47// The signedness costs nothing in practice: the conversions below are
48// two's-complement round trips, so the full uint64_t range survives intact.
49// Only past 2262-04-11, where a nanosecond stamp crosses 2^63, does the value
50// travel as a negative INTEGER, and it still decodes back bit-for-bit.
51static_assert(sizeof(E3_Timestamp_t) >= sizeof(uint64_t),
52 "E3_Timestamp_t is narrower than Pdu::timestamp; "
53 "give E3-Timestamp an explicit upper bound to force INTEGER_t "
54 "on this platform");
55
56// asn1c gives each optional id list its own anonymous struct, hence the template.
57template <typename SeqPtr>
58void set_id_list(SeqPtr& dst, const std::vector<uint32_t>& ids) {
59 dst = static_cast<SeqPtr>(calloc(1, sizeof(*dst)));
60 for (uint32_t v : ids) {
61 long* id = static_cast<long*>(malloc(sizeof(long)));
62 *id = v;
63 ASN_SEQUENCE_ADD(&dst->list, id);
64 }
65}
66
67template <typename SeqPtr>
68std::optional<std::vector<uint32_t>> get_id_list(const SeqPtr& src) {
69 if (!src) return std::nullopt;
70 std::vector<uint32_t> ids;
71 for (int i = 0; i < src->list.count; i++) {
72 ids.push_back(static_cast<uint32_t>(*src->list.array[i]));
73 }
74 return ids;
75}
76
77// True when `text` is well-formed UTF-8: shortest forms only, no surrogates,
78// nothing past U+10FFFF. E3-Text is UTF-8 carried as an OCTET STRING, so the
79// grammar does not check it.
80bool is_utf8(const char* text, size_t size) {
81 const auto* p = reinterpret_cast<const unsigned char*>(text);
82 size_t i = 0;
83 while (i < size) {
84 const unsigned char c = p[i];
85 size_t len = 0;
86 uint32_t cp = 0;
87 if (c < 0x80) { i++; continue; }
88 if (c >= 0xC2 && c <= 0xDF) { len = 2; cp = c & 0x1Fu; }
89 else if (c >= 0xE0 && c <= 0xEF) { len = 3; cp = c & 0x0Fu; }
90 else if (c >= 0xF0 && c <= 0xF4) { len = 4; cp = c & 0x07u; }
91 else return false;
92 if (i + len > size) return false;
93 for (size_t k = 1; k < len; k++) {
94 if ((p[i + k] & 0xC0u) != 0x80u) return false;
95 cp = (cp << 6) | (p[i + k] & 0x3Fu);
96 }
97 if ((len == 3 && cp < 0x800) || (len == 4 && (cp < 0x10000 || cp > 0x10FFFF))
98 || (cp >= 0xD800 && cp <= 0xDFFF)) return false;
99 i += len;
100 }
101 return true;
102}
103
104// `cause` and `detail` are the same two optional members on E3-SetupResponse,
105// E3-SubscriptionResponse, E3-MessageAck and E3-ControlResponse, hence the template.
106template <typename Resp>
107void set_reason(Resp* out, const std::optional<Cause>& cause, const std::optional<std::string>& detail) {
108 if (cause) {
109 out->cause = static_cast<decltype(out->cause)>(calloc(1, sizeof(*out->cause)));
110 *out->cause = static_cast<long>(*cause);
111 }
112 if (detail && !detail->empty()) {
113 // The bound is on bytes. Cut at a character boundary, so a long text
114 // is shortened and not turned into invalid UTF-8.
115 size_t size = std::min(detail->size(), MAX_DETAIL_SIZE);
116 while (size > 0 && size < detail->size()
117 && (static_cast<unsigned char>((*detail)[size]) & 0xC0u) == 0x80u) {
118 size--;
119 }
120 out->detail = static_cast<decltype(out->detail)>(calloc(1, sizeof(*out->detail)));
121 OCTET_STRING_fromBuf(out->detail, detail->data(), static_cast<int>(size));
122 }
123}
124
125template <typename Resp>
126void get_reason(const Resp* in, std::optional<Cause>& cause, std::optional<std::string>& detail) {
127 // An unknown extension value reads as "unspecified": still a refusal.
128 if (in->cause) cause = cause_from_value(static_cast<long>(*in->cause));
129 // A text that is not UTF-8 is dropped, not passed on to a log or a JSON writer.
130 if (in->detail && in->detail->buf && in->detail->size > 0
131 && is_utf8(reinterpret_cast<const char*>(in->detail->buf), in->detail->size)) {
132 detail = std::string(reinterpret_cast<const char*>(in->detail->buf), in->detail->size);
133 }
134}
135} // anonymous namespace
136
137// ============================================================================
138// Encoding
139// ============================================================================
140
142 E3_PDU* asn1_pdu = pdu_to_asn1(pdu);
143 if (!asn1_pdu) {
144 E3_LOG_ERROR(LOG_TAG) << "Failed to convert PDU to ASN.1";
145 return tl::unexpected(ErrorCode::ENCODE_FAILED);
146 }
147
148 // Allocate buffer for encoding
149 std::vector<uint8_t> buffer(BUFFER_SIZE);
150
151 // Encode using ASN.1 APER
152 asn_enc_rval_t enc_rval = aper_encode_to_buffer(
153 &asn_DEF_E3_PDU,
154 nullptr,
155 asn1_pdu,
156 buffer.data(),
157 buffer.size()
158 );
159
160 if (enc_rval.encoded == -1) {
161 E3_LOG_ERROR(LOG_TAG) << "APER encoding failed for type: "
162 << (enc_rval.failed_type ? enc_rval.failed_type->name : "Unknown");
163 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
164 return tl::unexpected(ErrorCode::ENCODE_FAILED);
165 }
166
167 // Resize buffer to actual encoded size
168 const size_t encoded_bytes = (static_cast<size_t>(enc_rval.encoded) + 7) / 8;
169 buffer.resize(encoded_bytes);
170
171 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
172
173 EncodedMessage msg;
174 msg.buffer = std::move(buffer);
176
177 E3_LOG_DEBUG(LOG_TAG) << "Encoded PDU: " << enc_rval.encoded << " bytes";
178 return msg;
179}
180
181// ============================================================================
182// Decoding
183// ============================================================================
184
186 return decode(encoded.buffer.data(), encoded.buffer.size());
187}
188
189EncodeResult<Pdu> Asn1E3Encoder::decode(const uint8_t* data, size_t size) {
190 if (!data || size == 0) {
191 return tl::unexpected(ErrorCode::INVALID_PARAM);
192 }
193
194 // Decode ASN.1 buffer
195 E3_PDU* asn1_pdu = nullptr;
196 asn_dec_rval_t dec_rval = aper_decode(
197 nullptr,
198 &asn_DEF_E3_PDU,
199 reinterpret_cast<void**>(&asn1_pdu),
200 data,
201 size,
202 0, 0
203 );
204
205 if (dec_rval.code != RC_OK) {
206 E3_LOG_ERROR(LOG_TAG) << "APER decoding failed with code " << dec_rval.code;
207 if (asn1_pdu) {
208 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
209 }
210 return tl::unexpected(ErrorCode::DECODE_FAILED);
211 }
212
213 // Convert to generic PDU
214 Pdu pdu = asn1_to_pdu(asn1_pdu);
215
216 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
217
218 return pdu;
219}
220
221// ============================================================================
222// PDU Conversion: Generic -> ASN.1
223// ============================================================================
224
225E3_PDU* Asn1E3Encoder::pdu_to_asn1(const Pdu& pdu) const {
226 E3_PDU* asn1_pdu = static_cast<E3_PDU*>(calloc(1, sizeof(E3_PDU)));
227 if (!asn1_pdu) {
228 return nullptr;
229 }
230
231 // Set top-level message ID (E3-PDU.id)
232 asn1_pdu->id = pdu.message_id;
233
234 // Set the optional envelope timestamp (E3-PDU.timestamp). Zero is the
235 // "no producer reference" sentinel and is sent as an absent field, so a
236 // producer that does not stamp its PDUs pays one preamble bit, not nine
237 // octets. Failing to allocate is not fatal: the PDU still carries its
238 // payload, it just loses the timestamp, which is exactly what an
239 // unstamped PDU looks like.
240 if (pdu.timestamp != 0) {
241 asn1_pdu->timestamp =
242 static_cast<E3_Timestamp_t*>(calloc(1, sizeof(E3_Timestamp_t)));
243 if (asn1_pdu->timestamp) {
244 *asn1_pdu->timestamp = static_cast<E3_Timestamp_t>(pdu.timestamp);
245 }
246 }
247
248
249 switch (pdu.type) {
251 const auto* req = std::get_if<SetupRequest>(&pdu.choice);
252 if (!req) { free(asn1_pdu); return nullptr; }
253
254 asn1_pdu->msg.present = E3_PDU__msg_PR_setupRequest;
255 asn1_pdu->msg.choice.setupRequest = static_cast<E3_SetupRequest_t*>(
256 calloc(1, sizeof(E3_SetupRequest_t)));
257 if (!asn1_pdu->msg.choice.setupRequest) { free(asn1_pdu); return nullptr; }
258
259 // Set e3apProtocolVersion string
260 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupRequest->e3apProtocolVersion,
261 req->e3ap_protocol_version.c_str(),
262 static_cast<int>(req->e3ap_protocol_version.size()));
263
264 // Set dAppName string
265 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupRequest->dAppName,
266 req->dapp_name.c_str(),
267 static_cast<int>(req->dapp_name.size()));
268
269 // Set dAppVersion string
270 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupRequest->dAppVersion,
271 req->dapp_version.c_str(),
272 static_cast<int>(req->dapp_version.size()));
273
274 // Set vendor string
275 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupRequest->vendor,
276 req->vendor.c_str(),
277 static_cast<int>(req->vendor.size()));
278 break;
279 }
280
282 const auto* resp = std::get_if<SetupResponse>(&pdu.choice);
283 if (!resp) { free(asn1_pdu); return nullptr; }
284
285 asn1_pdu->msg.present = E3_PDU__msg_PR_setupResponse;
286 asn1_pdu->msg.choice.setupResponse = static_cast<E3_SetupResponse_t*>(
287 calloc(1, sizeof(E3_SetupResponse_t)));
288 if (!asn1_pdu->msg.choice.setupResponse) { free(asn1_pdu); return nullptr; }
289
290 asn1_pdu->msg.choice.setupResponse->requestId = resp->request_id;
291 asn1_pdu->msg.choice.setupResponse->responseCode =
292 (resp->response_code == ResponseCode::POSITIVE) ? 0 : 1;
293
294 // Set optional e3apProtocolVersion
295 if (resp->e3ap_protocol_version.has_value()) {
296 asn1_pdu->msg.choice.setupResponse->e3apProtocolVersion =
297 static_cast<OCTET_STRING_t*>(calloc(1, sizeof(OCTET_STRING_t)));
298 OCTET_STRING_fromBuf(asn1_pdu->msg.choice.setupResponse->e3apProtocolVersion,
299 resp->e3ap_protocol_version.value().c_str(),
300 static_cast<int>(resp->e3ap_protocol_version.value().size()));
301 }
302
303 // Set optional dAppIdentifier
304 if (resp->dapp_identifier.has_value()) {
305 asn1_pdu->msg.choice.setupResponse->dAppIdentifier =
306 static_cast<long*>(malloc(sizeof(long)));
307 *asn1_pdu->msg.choice.setupResponse->dAppIdentifier = resp->dapp_identifier.value();
308 }
309
310 // Set mandatory ranIdentifier
311 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupResponse->ranIdentifier,
312 resp->ran_identifier.c_str(),
313 static_cast<int>(resp->ran_identifier.size()));
314
315 // Set optional ranFunctionList
316 if (!resp->ran_function_list.empty()) {
317 for (const auto& func : resp->ran_function_list) {
318 E3_RanFunctionDefinition_t* ran_func =
319 static_cast<E3_RanFunctionDefinition_t*>(calloc(1, sizeof(E3_RanFunctionDefinition_t)));
320 ran_func->ranFunctionIdentifier = func.ran_function_identifier;
321
322 // Encode telemetryIdentifierList
323 for (uint32_t tel_id : func.telemetry_identifier_list) {
324 long* id = static_cast<long*>(malloc(sizeof(long)));
325 *id = tel_id;
326 ASN_SEQUENCE_ADD(&ran_func->telemetryIdentifierList, id);
327 }
328
329 // Encode controlIdentifierList
330 for (uint32_t ctrl_id : func.control_identifier_list) {
331 long* id = static_cast<long*>(malloc(sizeof(long)));
332 *id = ctrl_id;
333 ASN_SEQUENCE_ADD(&ran_func->controlIdentifierList, id);
334 }
335
336 OCTET_STRING_fromBuf(&ran_func->ranFunctionData,
337 reinterpret_cast<const char*>(func.ran_function_data.data()),
338 static_cast<int>(func.ran_function_data.size()));
339
340 // Ensure ranFunctionList container is allocated (optional field)
341 if (!asn1_pdu->msg.choice.setupResponse->ranFunctionList) {
342 asn1_pdu->msg.choice.setupResponse->ranFunctionList =
343 static_cast<decltype(asn1_pdu->msg.choice.setupResponse->ranFunctionList)>(
344 calloc(1, sizeof(*asn1_pdu->msg.choice.setupResponse->ranFunctionList)));
345 }
346
347 // Append the ran function into the sequence's internal list
348 ASN_SEQUENCE_ADD(&asn1_pdu->msg.choice.setupResponse->ranFunctionList->list, ran_func);
349 }
350 }
351 set_reason(asn1_pdu->msg.choice.setupResponse, resp->cause, resp->detail);
352 break;
353 }
354
356 const auto* req = std::get_if<SubscriptionRequest>(&pdu.choice);
357 if (!req) { free(asn1_pdu); return nullptr; }
358
359 asn1_pdu->msg.present = E3_PDU__msg_PR_subscriptionRequest;
360 asn1_pdu->msg.choice.subscriptionRequest = static_cast<E3_SubscriptionRequest_t*>(
361 calloc(1, sizeof(E3_SubscriptionRequest_t)));
362 if (!asn1_pdu->msg.choice.subscriptionRequest) { free(asn1_pdu); return nullptr; }
363
364 asn1_pdu->msg.choice.subscriptionRequest->dAppIdentifier = req->dapp_identifier;
365 asn1_pdu->msg.choice.subscriptionRequest->ranFunctionIdentifier = req->ran_function_identifier;
366
367 // Encode telemetryIdentifierList
368 for (uint32_t tel_id : req->telemetry_identifier_list) {
369 long* id = static_cast<long*>(malloc(sizeof(long)));
370 *id = tel_id;
371 ASN_SEQUENCE_ADD(&asn1_pdu->msg.choice.subscriptionRequest->telemetryIdentifierList, id);
372 }
373
374 // Encode controlIdentifierList
375 for (uint32_t ctrl_id : req->control_identifier_list) {
376 long* id = static_cast<long*>(malloc(sizeof(long)));
377 *id = ctrl_id;
378 ASN_SEQUENCE_ADD(&asn1_pdu->msg.choice.subscriptionRequest->controlIdentifierList, id);
379 }
380
381 // Set optional subscriptionTime
382 if (req->subscription_time.has_value()) {
383 asn1_pdu->msg.choice.subscriptionRequest->subscriptionTime =
384 static_cast<long*>(malloc(sizeof(long)));
385 *asn1_pdu->msg.choice.subscriptionRequest->subscriptionTime = req->subscription_time.value();
386 }
387 // Set optional periodicity
388 if (req->periodicity.has_value()) {
389 asn1_pdu->msg.choice.subscriptionRequest->periodicity =
390 static_cast<long*>(malloc(sizeof(long)));
391 *asn1_pdu->msg.choice.subscriptionRequest->periodicity = req->periodicity.value();
392 }
393 break;
394 }
395
397 const auto* del = std::get_if<SubscriptionDelete>(&pdu.choice);
398 if (!del) { free(asn1_pdu); return nullptr; }
399
400 asn1_pdu->msg.present = E3_PDU__msg_PR_subscriptionDelete;
401 asn1_pdu->msg.choice.subscriptionDelete = static_cast<E3_SubscriptionDelete_t*>(
402 calloc(1, sizeof(E3_SubscriptionDelete_t)));
403 if (!asn1_pdu->msg.choice.subscriptionDelete) { free(asn1_pdu); return nullptr; }
404
405 asn1_pdu->msg.choice.subscriptionDelete->dAppIdentifier = del->dapp_identifier;
406 asn1_pdu->msg.choice.subscriptionDelete->subscriptionId = del->subscription_id;
407 break;
408 }
409
411 const auto* resp = std::get_if<SubscriptionResponse>(&pdu.choice);
412 if (!resp) { free(asn1_pdu); return nullptr; }
413
414 asn1_pdu->msg.present = E3_PDU__msg_PR_subscriptionResponse;
415 asn1_pdu->msg.choice.subscriptionResponse = static_cast<E3_SubscriptionResponse_t*>(
416 calloc(1, sizeof(E3_SubscriptionResponse_t)));
417 if (!asn1_pdu->msg.choice.subscriptionResponse) { free(asn1_pdu); return nullptr; }
418
419 asn1_pdu->msg.choice.subscriptionResponse->requestId = resp->request_id;
420 asn1_pdu->msg.choice.subscriptionResponse->dAppIdentifier = resp->dapp_identifier;
421 asn1_pdu->msg.choice.subscriptionResponse->responseCode =
422 (resp->response_code == ResponseCode::POSITIVE) ? 0 : 1;
423
424 // Set optional subscriptionId
425 if (resp->subscription_id.has_value()) {
426 asn1_pdu->msg.choice.subscriptionResponse->subscriptionId =
427 static_cast<long*>(malloc(sizeof(long)));
428 *asn1_pdu->msg.choice.subscriptionResponse->subscriptionId = resp->subscription_id.value();
429 }
430 if (resp->telemetry_identifier_list.has_value()) {
431 set_id_list(asn1_pdu->msg.choice.subscriptionResponse->telemetryIdentifierList,
432 *resp->telemetry_identifier_list);
433 }
434 if (resp->control_identifier_list.has_value()) {
435 set_id_list(asn1_pdu->msg.choice.subscriptionResponse->controlIdentifierList,
436 *resp->control_identifier_list);
437 }
438 if (resp->ran_function_identifier.has_value()) {
439 asn1_pdu->msg.choice.subscriptionResponse->ranFunctionIdentifier =
440 static_cast<long*>(malloc(sizeof(long)));
441 *asn1_pdu->msg.choice.subscriptionResponse->ranFunctionIdentifier = resp->ran_function_identifier.value();
442 }
443 if (resp->periodicity.has_value()) {
444 asn1_pdu->msg.choice.subscriptionResponse->periodicity =
445 static_cast<long*>(malloc(sizeof(long)));
446 *asn1_pdu->msg.choice.subscriptionResponse->periodicity = resp->periodicity.value();
447 }
448 set_reason(asn1_pdu->msg.choice.subscriptionResponse, resp->cause, resp->detail);
449 break;
450 }
451
453 const auto* msg = std::get_if<IndicationMessage>(&pdu.choice);
454 if (!msg) { free(asn1_pdu); return nullptr; }
455
456 asn1_pdu->msg.present = E3_PDU__msg_PR_indicationMessage;
457 asn1_pdu->msg.choice.indicationMessage = static_cast<E3_IndicationMessage_t*>(
458 calloc(1, sizeof(E3_IndicationMessage_t)));
459 if (!asn1_pdu->msg.choice.indicationMessage) { free(asn1_pdu); return nullptr; }
460
461 asn1_pdu->msg.choice.indicationMessage->dAppIdentifier = msg->dapp_identifier;
462 asn1_pdu->msg.choice.indicationMessage->ranFunctionIdentifier = msg->ran_function_identifier;
463
464 // Copy protocol data
465 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.indicationMessage->protocolData,
466 reinterpret_cast<const char*>(msg->protocol_data.data()),
467 static_cast<int>(msg->protocol_data.size()));
468 break;
469 }
470
472 const auto* action = std::get_if<DAppControlAction>(&pdu.choice);
473 if (!action) { free(asn1_pdu); return nullptr; }
474
475 asn1_pdu->msg.present = E3_PDU__msg_PR_dAppControlAction;
476 asn1_pdu->msg.choice.dAppControlAction = static_cast<E3_DAppControlAction_t*>(
477 calloc(1, sizeof(E3_DAppControlAction_t)));
478 if (!asn1_pdu->msg.choice.dAppControlAction) { free(asn1_pdu); return nullptr; }
479
480 asn1_pdu->msg.choice.dAppControlAction->dAppIdentifier = action->dapp_identifier;
481 asn1_pdu->msg.choice.dAppControlAction->ranFunctionIdentifier = action->ran_function_identifier;
482 asn1_pdu->msg.choice.dAppControlAction->controlIdentifier = action->control_identifier;
483
484 // sequenceId is OPTIONAL here: present only when this control
485 // re-issues an xApp one. Zero is the absent sentinel, same
486 // convention as the envelope timestamp above.
487 if (action->sequence_id != 0) {
488 asn1_pdu->msg.choice.dAppControlAction->sequenceId =
489 static_cast<E3_SequenceID_t*>(calloc(1, sizeof(E3_SequenceID_t)));
490 if (asn1_pdu->msg.choice.dAppControlAction->sequenceId) {
491 *asn1_pdu->msg.choice.dAppControlAction->sequenceId =
492 static_cast<E3_SequenceID_t>(action->sequence_id);
493 }
494 }
495
496 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.dAppControlAction->actionData,
497 reinterpret_cast<const char*>(action->action_data.data()),
498 static_cast<int>(action->action_data.size()));
499
500 // requestAck is an extension addition, written only when true, so a
501 // control that does not ask is encoded as it always was.
502 if (action->request_ack) {
503 asn1_pdu->msg.choice.dAppControlAction->requestAck =
504 static_cast<BOOLEAN_t*>(calloc(1, sizeof(BOOLEAN_t)));
505 if (!asn1_pdu->msg.choice.dAppControlAction->requestAck) {
506 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
507 return nullptr;
508 }
509 *asn1_pdu->msg.choice.dAppControlAction->requestAck = 1;
510 }
511 break;
512 }
513
515 const auto* report = std::get_if<DAppReport>(&pdu.choice);
516 if (!report) { free(asn1_pdu); return nullptr; }
517
518 asn1_pdu->msg.present = E3_PDU__msg_PR_dAppReport;
519 asn1_pdu->msg.choice.dAppReport = static_cast<E3_DAppReport_t*>(
520 calloc(1, sizeof(E3_DAppReport_t)));
521 if (!asn1_pdu->msg.choice.dAppReport) { free(asn1_pdu); return nullptr; }
522
523 asn1_pdu->msg.choice.dAppReport->dAppIdentifier = report->dapp_identifier;
524 asn1_pdu->msg.choice.dAppReport->ranFunctionIdentifier = report->ran_function_identifier;
525 asn1_pdu->msg.choice.dAppReport->sequenceId =
526 static_cast<E3_SequenceID_t>(report->sequence_id);
527
528 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.dAppReport->reportData,
529 reinterpret_cast<const char*>(report->report_data.data()),
530 static_cast<int>(report->report_data.size()));
531 break;
532 }
533
535 const auto* action = std::get_if<XAppControlAction>(&pdu.choice);
536 if (!action) { free(asn1_pdu); return nullptr; }
537
538 asn1_pdu->msg.present = E3_PDU__msg_PR_xAppControlAction;
539 asn1_pdu->msg.choice.xAppControlAction = static_cast<E3_XAppControlAction_t*>(
540 calloc(1, sizeof(E3_XAppControlAction_t)));
541 if (!asn1_pdu->msg.choice.xAppControlAction) { free(asn1_pdu); return nullptr; }
542
543 asn1_pdu->msg.choice.xAppControlAction->dAppIdentifier = action->dapp_identifier;
544 asn1_pdu->msg.choice.xAppControlAction->ranFunctionIdentifier = action->ran_function_identifier;
545 asn1_pdu->msg.choice.xAppControlAction->sequenceId =
546 static_cast<E3_SequenceID_t>(action->sequence_id);
547
548 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.xAppControlAction->xAppControlData,
549 reinterpret_cast<const char*>(action->xapp_control_data.data()),
550 static_cast<int>(action->xapp_control_data.size()));
551
552 if (action->request_ack) {
553 asn1_pdu->msg.choice.xAppControlAction->requestAck =
554 static_cast<BOOLEAN_t*>(calloc(1, sizeof(BOOLEAN_t)));
555 if (!asn1_pdu->msg.choice.xAppControlAction->requestAck) {
556 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
557 return nullptr;
558 }
559 *asn1_pdu->msg.choice.xAppControlAction->requestAck = 1;
560 }
561 break;
562 }
563
565 // DEPRECATED TO BE REMOVED: libe3 does not send one; kept so the index of
566 // controlResponse is OCUDU's and a message from another RAN is read.
567 const auto* ack = std::get_if<MessageAck>(&pdu.choice);
568 if (!ack) { free(asn1_pdu); return nullptr; }
569
570 asn1_pdu->msg.present = E3_PDU__msg_PR_messageAck;
571 asn1_pdu->msg.choice.messageAck = static_cast<E3_MessageAck_t*>(
572 calloc(1, sizeof(E3_MessageAck_t)));
573 if (!asn1_pdu->msg.choice.messageAck) { free(asn1_pdu); return nullptr; }
574
575 asn1_pdu->msg.choice.messageAck->requestId = ack->request_id;
576 asn1_pdu->msg.choice.messageAck->responseCode =
577 (ack->response_code == ResponseCode::POSITIVE) ? 0 : 1;
578 set_reason(asn1_pdu->msg.choice.messageAck, ack->cause, ack->detail);
579 break;
580 }
581
583 const auto* resp = std::get_if<ControlResponse>(&pdu.choice);
584 if (!resp) { free(asn1_pdu); return nullptr; }
585
586 asn1_pdu->msg.present = E3_PDU__msg_PR_controlResponse;
587 asn1_pdu->msg.choice.controlResponse = static_cast<E3_ControlResponse_t*>(
588 calloc(1, sizeof(E3_ControlResponse_t)));
589 if (!asn1_pdu->msg.choice.controlResponse) { free(asn1_pdu); return nullptr; }
590
591 E3_ControlResponse_t* out = asn1_pdu->msg.choice.controlResponse;
592 out->requestId = resp->request_id;
593 out->dAppIdentifier = resp->dapp_identifier;
594 out->ranFunctionIdentifier = resp->ran_function_identifier;
595 out->controlIdentifier = resp->control_identifier;
596 out->responseCode = (resp->response_code == ResponseCode::POSITIVE) ? 0 : 1;
597 set_reason(out, resp->cause, resp->detail);
598 if (resp->control_outcome && !resp->control_outcome->empty()) {
599 out->controlOutcome = static_cast<OCTET_STRING_t*>(calloc(1, sizeof(OCTET_STRING_t)));
600 if (!out->controlOutcome) {
601 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
602 return nullptr;
603 }
604 OCTET_STRING_fromBuf(out->controlOutcome,
605 reinterpret_cast<const char*>(resp->control_outcome->data()),
606 static_cast<int>(resp->control_outcome->size()));
607 }
608 break;
609 }
610
612 const auto* msg = std::get_if<ReleaseMessage>(&pdu.choice);
613 if (!msg) { free(asn1_pdu); return nullptr; }
614
615 asn1_pdu->msg.present = E3_PDU__msg_PR_releaseMessage;
616 asn1_pdu->msg.choice.releaseMessage = static_cast<E3_ReleaseMessage_t*>(
617 calloc(1, sizeof(E3_ReleaseMessage_t)));
618 if (!asn1_pdu->msg.choice.releaseMessage) { free(asn1_pdu); return nullptr; }
619
620 asn1_pdu->msg.choice.releaseMessage->dAppIdentifier = msg->dapp_identifier;
621 break;
622 }
623
624 default:
625 free(asn1_pdu);
626 return nullptr;
627 }
628
629 return asn1_pdu;
630}
631
632// ============================================================================
633// PDU Conversion: ASN.1 -> Generic
634// ============================================================================
635
636Pdu Asn1E3Encoder::asn1_to_pdu(const E3_PDU* asn1_pdu) const {
637 Pdu pdu;
638
639 // Read top-level message ID (E3-PDU.id)
640 pdu.message_id = static_cast<uint32_t>(asn1_pdu->id);
641
642 // Read the optional envelope timestamp (E3-PDU.timestamp). The assignment
643 // is unconditional on purpose: Pdu's default constructor stamps the
644 // current clock, so without the else branch an absent field would decode
645 // to the *receiver's* wall clock at decode time rather than to the
646 // "unset" sentinel, which is far harder to notice than a zero.
647 pdu.timestamp = asn1_pdu->timestamp
648 ? static_cast<uint64_t>(*asn1_pdu->timestamp)
649 : 0;
650
651
652 switch (asn1_pdu->msg.present) {
653 case E3_PDU__msg_PR_setupRequest: {
654 pdu.type = PduType::SETUP_REQUEST;
655
656 SetupRequest req;
657
658 // Extract e3apProtocolVersion string
659 const OCTET_STRING_t* proto_ver = &asn1_pdu->msg.choice.setupRequest->e3apProtocolVersion;
660 req.e3ap_protocol_version.assign(reinterpret_cast<const char*>(proto_ver->buf), proto_ver->size);
661
662 // Extract dAppName string
663 const OCTET_STRING_t* dapp_name = &asn1_pdu->msg.choice.setupRequest->dAppName;
664 req.dapp_name.assign(reinterpret_cast<const char*>(dapp_name->buf), dapp_name->size);
665
666 // Extract dAppVersion string
667 const OCTET_STRING_t* dapp_ver = &asn1_pdu->msg.choice.setupRequest->dAppVersion;
668 req.dapp_version.assign(reinterpret_cast<const char*>(dapp_ver->buf), dapp_ver->size);
669
670 // Extract vendor string
671 const OCTET_STRING_t* vendor = &asn1_pdu->msg.choice.setupRequest->vendor;
672 req.vendor.assign(reinterpret_cast<const char*>(vendor->buf), vendor->size);
673
674 pdu.choice = req;
675 break;
676 }
677
678 case E3_PDU__msg_PR_setupResponse: {
679 pdu.type = PduType::SETUP_RESPONSE;
680
681 SetupResponse resp;
682 resp.request_id = static_cast<uint32_t>(asn1_pdu->msg.choice.setupResponse->requestId);
683 resp.response_code = (asn1_pdu->msg.choice.setupResponse->responseCode == 0)
685
686 // Extract optional e3apProtocolVersion
687 if (asn1_pdu->msg.choice.setupResponse->e3apProtocolVersion) {
688 const OCTET_STRING_t* proto_ver = asn1_pdu->msg.choice.setupResponse->e3apProtocolVersion;
689 resp.e3ap_protocol_version = std::string(
690 reinterpret_cast<const char*>(proto_ver->buf), proto_ver->size);
691 }
692
693 // Extract optional dAppIdentifier
694 if (asn1_pdu->msg.choice.setupResponse->dAppIdentifier) {
695 resp.dapp_identifier = *asn1_pdu->msg.choice.setupResponse->dAppIdentifier;
696 }
697
698 // Extract mandatory ranIdentifier
699 const OCTET_STRING_t* ran_id = &asn1_pdu->msg.choice.setupResponse->ranIdentifier;
700 resp.ran_identifier.assign(reinterpret_cast<const char*>(ran_id->buf), ran_id->size);
701
702 // Extract ranFunctionList
703 if (asn1_pdu->msg.choice.setupResponse->ranFunctionList) {
704 int count = asn1_pdu->msg.choice.setupResponse->ranFunctionList->list.count;
705 for (int i = 0; i < count; i++) {
706 E3_RanFunctionDefinition_t* asn_func =
707 asn1_pdu->msg.choice.setupResponse->ranFunctionList->list.array[i];
708 RanFunctionDef func;
709 func.ran_function_identifier = static_cast<uint32_t>(asn_func->ranFunctionIdentifier);
710
711 // Decode telemetryIdentifierList
712 int tel_count = asn_func->telemetryIdentifierList.list.count;
713 for (int j = 0; j < tel_count; j++) {
714 func.telemetry_identifier_list.push_back(
715 static_cast<uint32_t>(*asn_func->telemetryIdentifierList.list.array[j]));
716 }
717
718 // Decode controlIdentifierList
719 int ctrl_count = asn_func->controlIdentifierList.list.count;
720 for (int j = 0; j < ctrl_count; j++) {
721 func.control_identifier_list.push_back(
722 static_cast<uint32_t>(*asn_func->controlIdentifierList.list.array[j]));
723 }
724
725 func.ran_function_data.assign(
726 asn_func->ranFunctionData.buf,
727 asn_func->ranFunctionData.buf + asn_func->ranFunctionData.size);
728 resp.ran_function_list.push_back(func);
729 }
730 }
731 get_reason(asn1_pdu->msg.choice.setupResponse, resp.cause, resp.detail);
732
733 pdu.choice = resp;
734 break;
735 }
736
737 case E3_PDU__msg_PR_subscriptionRequest: {
739
740 SubscriptionRequest req;
741 req.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.subscriptionRequest->dAppIdentifier);
742 req.ran_function_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.subscriptionRequest->ranFunctionIdentifier);
743
744 // Decode telemetryIdentifierList
745 int tel_count = asn1_pdu->msg.choice.subscriptionRequest->telemetryIdentifierList.list.count;
746 for (int i = 0; i < tel_count; i++) {
747 req.telemetry_identifier_list.push_back(
748 static_cast<uint32_t>(*asn1_pdu->msg.choice.subscriptionRequest->telemetryIdentifierList.list.array[i]));
749 }
750
751 // Decode controlIdentifierList
752 int ctrl_count = asn1_pdu->msg.choice.subscriptionRequest->controlIdentifierList.list.count;
753 for (int i = 0; i < ctrl_count; i++) {
754 req.control_identifier_list.push_back(
755 static_cast<uint32_t>(*asn1_pdu->msg.choice.subscriptionRequest->controlIdentifierList.list.array[i]));
756 }
757
758 // Decode optional subscriptionTime
759 if (asn1_pdu->msg.choice.subscriptionRequest->subscriptionTime) {
760 req.subscription_time = *asn1_pdu->msg.choice.subscriptionRequest->subscriptionTime;
761 }
762 // Decode optional periodicity
763 if (asn1_pdu->msg.choice.subscriptionRequest->periodicity) {
764 req.periodicity = static_cast<uint32_t>(*asn1_pdu->msg.choice.subscriptionRequest->periodicity);
765 }
766
767 pdu.choice = req;
768 break;
769 }
770
771 case E3_PDU__msg_PR_subscriptionDelete: {
773
774 SubscriptionDelete del;
775 del.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.subscriptionDelete->dAppIdentifier);
776 del.subscription_id = static_cast<uint32_t>(asn1_pdu->msg.choice.subscriptionDelete->subscriptionId);
777
778 pdu.choice = del;
779 break;
780 }
781
782 case E3_PDU__msg_PR_subscriptionResponse: {
784
785 SubscriptionResponse resp;
786 resp.request_id = static_cast<uint32_t>(asn1_pdu->msg.choice.subscriptionResponse->requestId);
787 resp.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.subscriptionResponse->dAppIdentifier);
788 resp.response_code = (asn1_pdu->msg.choice.subscriptionResponse->responseCode == 0)
790
791 // Decode optional subscriptionId
792 if (asn1_pdu->msg.choice.subscriptionResponse->subscriptionId) {
793 resp.subscription_id = *asn1_pdu->msg.choice.subscriptionResponse->subscriptionId;
794 }
795 resp.telemetry_identifier_list =
796 get_id_list(asn1_pdu->msg.choice.subscriptionResponse->telemetryIdentifierList);
797 resp.control_identifier_list =
798 get_id_list(asn1_pdu->msg.choice.subscriptionResponse->controlIdentifierList);
799 if (asn1_pdu->msg.choice.subscriptionResponse->ranFunctionIdentifier) {
800 resp.ran_function_identifier = static_cast<uint32_t>(
801 *asn1_pdu->msg.choice.subscriptionResponse->ranFunctionIdentifier);
802 }
803 if (asn1_pdu->msg.choice.subscriptionResponse->periodicity) {
804 resp.periodicity = static_cast<uint32_t>(
805 *asn1_pdu->msg.choice.subscriptionResponse->periodicity);
806 }
807 get_reason(asn1_pdu->msg.choice.subscriptionResponse, resp.cause, resp.detail);
808
809 pdu.choice = resp;
810 break;
811 }
812
813 case E3_PDU__msg_PR_indicationMessage: {
815
816 IndicationMessage msg;
817 msg.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.indicationMessage->dAppIdentifier);
818 msg.ran_function_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.indicationMessage->ranFunctionIdentifier);
819
820 // Copy protocol data
821 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.indicationMessage->protocolData;
822 msg.protocol_data.assign(data->buf, data->buf + data->size);
823
824 pdu.choice = msg;
825 break;
826 }
827
828 case E3_PDU__msg_PR_dAppControlAction: {
830
831 DAppControlAction action;
832 action.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.dAppControlAction->dAppIdentifier);
833 action.ran_function_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.dAppControlAction->ranFunctionIdentifier);
834 action.control_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.dAppControlAction->controlIdentifier);
835 action.sequence_id = asn1_pdu->msg.choice.dAppControlAction->sequenceId
836 ? static_cast<uint32_t>(*asn1_pdu->msg.choice.dAppControlAction->sequenceId)
837 : 0;
838
839 action.request_ack = asn1_pdu->msg.choice.dAppControlAction->requestAck
840 && *asn1_pdu->msg.choice.dAppControlAction->requestAck;
841
842 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.dAppControlAction->actionData;
843 action.action_data.assign(data->buf, data->buf + data->size);
844
845 pdu.choice = action;
846 break;
847 }
848
849 case E3_PDU__msg_PR_dAppReport: {
850 pdu.type = PduType::DAPP_REPORT;
851
852 DAppReport report;
853 report.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.dAppReport->dAppIdentifier);
854 report.ran_function_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.dAppReport->ranFunctionIdentifier);
855 report.sequence_id = static_cast<uint32_t>(asn1_pdu->msg.choice.dAppReport->sequenceId);
856
857 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.dAppReport->reportData;
858 report.report_data.assign(data->buf, data->buf + data->size);
859
860 pdu.choice = report;
861 break;
862 }
863
864 case E3_PDU__msg_PR_xAppControlAction: {
866
867 XAppControlAction action;
868 action.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.xAppControlAction->dAppIdentifier);
869 action.ran_function_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.xAppControlAction->ranFunctionIdentifier);
870 action.sequence_id = static_cast<uint32_t>(asn1_pdu->msg.choice.xAppControlAction->sequenceId);
871
872 action.request_ack = asn1_pdu->msg.choice.xAppControlAction->requestAck
873 && *asn1_pdu->msg.choice.xAppControlAction->requestAck;
874
875 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.xAppControlAction->xAppControlData;
876 action.xapp_control_data.assign(data->buf, data->buf + data->size);
877
878 pdu.choice = action;
879 break;
880 }
881
882 case E3_PDU__msg_PR_messageAck: {
883 pdu.type = PduType::MESSAGE_ACK;
884
885 MessageAck ack;
886 ack.request_id = static_cast<uint32_t>(asn1_pdu->msg.choice.messageAck->requestId);
887 ack.response_code = (asn1_pdu->msg.choice.messageAck->responseCode == 0)
889 get_reason(asn1_pdu->msg.choice.messageAck, ack.cause, ack.detail);
890
891 pdu.choice = ack;
892 break;
893 }
894
895 case E3_PDU__msg_PR_controlResponse: {
896 pdu.type = PduType::CONTROL_RESPONSE;
897
898 const E3_ControlResponse_t* in = asn1_pdu->msg.choice.controlResponse;
899 ControlResponse resp;
900 resp.request_id = static_cast<uint32_t>(in->requestId);
901 resp.dapp_identifier = static_cast<uint32_t>(in->dAppIdentifier);
902 resp.ran_function_identifier = static_cast<uint32_t>(in->ranFunctionIdentifier);
903 resp.control_identifier = static_cast<uint32_t>(in->controlIdentifier);
904 resp.response_code = (in->responseCode == 0)
906 get_reason(in, resp.cause, resp.detail);
907 if (in->controlOutcome && in->controlOutcome->buf && in->controlOutcome->size > 0) {
908 resp.control_outcome = std::vector<uint8_t>(
909 in->controlOutcome->buf, in->controlOutcome->buf + in->controlOutcome->size);
910 }
911
912 pdu.choice = resp;
913 break;
914 }
915
916 case E3_PDU__msg_PR_releaseMessage: {
917 pdu.type = PduType::RELEASE_MESSAGE;
918
919 ReleaseMessage msg;
920 msg.dapp_identifier = static_cast<uint32_t>(asn1_pdu->msg.choice.releaseMessage->dAppIdentifier);
921
922 pdu.choice = msg;
923 break;
924 }
925
926 default:
927 E3_LOG_ERROR(LOG_TAG) << "Unknown ASN.1 PDU type: " << asn1_pdu->msg.present;
928 break;
929 }
930
931 return pdu;
932}
933
934} // namespace libe3
ASN.1 APER Encoder for E3AP PDUs.
EncodeResult< Pdu > decode(const EncodedMessage &encoded) override
Decode bytes to a PDU.
EncodeResult< EncodedMessage > encode(const Pdu &pdu) override
Encode a PDU to bytes.
Logging facility for libe3.
#define E3_LOG_DEBUG(component)
Definition logger.hpp:228
#define E3_LOG_ERROR(component)
Definition logger.hpp:225
ResponseCode
E3AP response codes.
Definition types.hpp:103
@ NEGATIVE
Request rejected.
Definition types.hpp:105
@ POSITIVE
Request accepted.
Definition types.hpp:104
@ SUBSCRIPTION_DELETE
Definition types.hpp:148
@ SUBSCRIPTION_RESPONSE
Definition types.hpp:149
@ SUBSCRIPTION_REQUEST
Definition types.hpp:147
@ XAPP_CONTROL_ACTION
Definition types.hpp:153
@ DAPP_CONTROL_ACTION
Definition types.hpp:151
@ MESSAGE_ACK
DEPRECATED TO BE REMOVED: libe3 no longer sends it, see CONTROL_RESPONSE.
Definition types.hpp:155
@ ASN1
ASN.1 PER encoding (standard O-RAN format).
Definition types.hpp:54
tl::expected< T, ErrorCode > EncodeResult
Result type for encoding/decoding operations.
Cause cause_from_value(long value) noexcept
Read a Cause from its wire value.
Definition types.hpp:790
constexpr size_t MAX_DETAIL_SIZE
Largest detail text of a negative response, in bytes (E3-Text).
Definition types.hpp:209
Encoded message wrapper.
Definition types.hpp:219
std::vector< uint8_t > buffer
Encoded data buffer.
Definition types.hpp:220
EncodingFormat format
Format used for encoding.
Definition types.hpp:221
Generic E3AP PDU structure.
Definition types.hpp:448
PduType type
Which alternative of choice is set.
Definition types.hpp:449
PduChoice choice
The message itself.
Definition types.hpp:450
uint64_t timestamp
Producer timestamp, in nanoseconds since the Unix epoch.
Definition types.hpp:466
uint32_t message_id
Unique message identifier.
Definition types.hpp:451