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"
39constexpr const char* LOG_TAG =
"Asn1Enc";
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 "
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)));
63 ASN_SEQUENCE_ADD(&dst->list,
id);
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]));
80bool is_utf8(
const char* text,
size_t size) {
81 const auto* p =
reinterpret_cast<const unsigned char*
>(text);
84 const unsigned char c = p[i];
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; }
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);
97 if ((len == 3 && cp < 0x800) || (len == 4 && (cp < 0x10000 || cp > 0x10FFFF))
98 || (cp >= 0xD800 && cp <= 0xDFFF))
return false;
106template <
typename Resp>
107void set_reason(Resp* out,
const std::optional<Cause>& cause,
const std::optional<std::string>& detail) {
109 out->cause =
static_cast<decltype(out-
>cause)>(calloc(1,
sizeof(*out->cause)));
110 *out->cause =
static_cast<long>(*cause);
112 if (detail && !detail->empty()) {
116 while (size > 0 && size < detail->size()
117 && (
static_cast<unsigned char>((*detail)[size]) & 0xC0u) == 0x80u) {
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));
125template <
typename Resp>
126void get_reason(
const Resp* in, std::optional<Cause>& cause, std::optional<std::string>& detail) {
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);
142 E3_PDU* asn1_pdu = pdu_to_asn1(pdu);
144 E3_LOG_ERROR(LOG_TAG) <<
"Failed to convert PDU to ASN.1";
145 return tl::unexpected(ErrorCode::ENCODE_FAILED);
149 std::vector<uint8_t> buffer(BUFFER_SIZE);
152 asn_enc_rval_t enc_rval = aper_encode_to_buffer(
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);
168 const size_t encoded_bytes = (
static_cast<size_t>(enc_rval.encoded) + 7) / 8;
169 buffer.resize(encoded_bytes);
171 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
174 msg.
buffer = std::move(buffer);
177 E3_LOG_DEBUG(LOG_TAG) <<
"Encoded PDU: " << enc_rval.encoded <<
" bytes";
190 if (!data || size == 0) {
191 return tl::unexpected(ErrorCode::INVALID_PARAM);
195 E3_PDU* asn1_pdu =
nullptr;
196 asn_dec_rval_t dec_rval = aper_decode(
199 reinterpret_cast<void**
>(&asn1_pdu),
205 if (dec_rval.code != RC_OK) {
206 E3_LOG_ERROR(LOG_TAG) <<
"APER decoding failed with code " << dec_rval.code;
208 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
210 return tl::unexpected(ErrorCode::DECODE_FAILED);
214 Pdu pdu = asn1_to_pdu(asn1_pdu);
216 ASN_STRUCT_FREE(asn_DEF_E3_PDU, asn1_pdu);
225E3_PDU* Asn1E3Encoder::pdu_to_asn1(
const Pdu& pdu)
const {
226 E3_PDU* asn1_pdu =
static_cast<E3_PDU*
>(calloc(1,
sizeof(E3_PDU)));
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);
251 const auto* req = std::get_if<SetupRequest>(&pdu.
choice);
252 if (!req) { free(asn1_pdu);
return nullptr; }
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; }
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()));
265 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupRequest->dAppName,
266 req->dapp_name.c_str(),
267 static_cast<int>(req->dapp_name.size()));
270 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupRequest->dAppVersion,
271 req->dapp_version.c_str(),
272 static_cast<int>(req->dapp_version.size()));
275 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupRequest->vendor,
277 static_cast<int>(req->vendor.size()));
282 const auto* resp = std::get_if<SetupResponse>(&pdu.
choice);
283 if (!resp) { free(asn1_pdu);
return nullptr; }
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; }
290 asn1_pdu->msg.choice.setupResponse->requestId = resp->request_id;
291 asn1_pdu->msg.choice.setupResponse->responseCode =
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()));
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();
311 OCTET_STRING_fromBuf(&asn1_pdu->msg.choice.setupResponse->ranIdentifier,
312 resp->ran_identifier.c_str(),
313 static_cast<int>(resp->ran_identifier.size()));
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;
323 for (uint32_t tel_id : func.telemetry_identifier_list) {
324 long*
id =
static_cast<long*
>(malloc(
sizeof(
long)));
326 ASN_SEQUENCE_ADD(&ran_func->telemetryIdentifierList,
id);
330 for (uint32_t ctrl_id : func.control_identifier_list) {
331 long*
id =
static_cast<long*
>(malloc(
sizeof(
long)));
333 ASN_SEQUENCE_ADD(&ran_func->controlIdentifierList,
id);
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()));
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)));
348 ASN_SEQUENCE_ADD(&asn1_pdu->msg.choice.setupResponse->ranFunctionList->list, ran_func);
351 set_reason(asn1_pdu->msg.choice.setupResponse, resp->cause, resp->detail);
356 const auto* req = std::get_if<SubscriptionRequest>(&pdu.
choice);
357 if (!req) { free(asn1_pdu);
return nullptr; }
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; }
364 asn1_pdu->msg.choice.subscriptionRequest->dAppIdentifier = req->dapp_identifier;
365 asn1_pdu->msg.choice.subscriptionRequest->ranFunctionIdentifier = req->ran_function_identifier;
368 for (uint32_t tel_id : req->telemetry_identifier_list) {
369 long*
id =
static_cast<long*
>(malloc(
sizeof(
long)));
371 ASN_SEQUENCE_ADD(&asn1_pdu->msg.choice.subscriptionRequest->telemetryIdentifierList,
id);
375 for (uint32_t ctrl_id : req->control_identifier_list) {
376 long*
id =
static_cast<long*
>(malloc(
sizeof(
long)));
378 ASN_SEQUENCE_ADD(&asn1_pdu->msg.choice.subscriptionRequest->controlIdentifierList,
id);
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();
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();
397 const auto* del = std::get_if<SubscriptionDelete>(&pdu.
choice);
398 if (!del) { free(asn1_pdu);
return nullptr; }
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; }
405 asn1_pdu->msg.choice.subscriptionDelete->dAppIdentifier = del->dapp_identifier;
406 asn1_pdu->msg.choice.subscriptionDelete->subscriptionId = del->subscription_id;
411 const auto* resp = std::get_if<SubscriptionResponse>(&pdu.
choice);
412 if (!resp) { free(asn1_pdu);
return nullptr; }
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; }
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 =
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();
430 if (resp->telemetry_identifier_list.has_value()) {
431 set_id_list(asn1_pdu->msg.choice.subscriptionResponse->telemetryIdentifierList,
432 *resp->telemetry_identifier_list);
434 if (resp->control_identifier_list.has_value()) {
435 set_id_list(asn1_pdu->msg.choice.subscriptionResponse->controlIdentifierList,
436 *resp->control_identifier_list);
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();
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();
448 set_reason(asn1_pdu->msg.choice.subscriptionResponse, resp->cause, resp->detail);
453 const auto* msg = std::get_if<IndicationMessage>(&pdu.
choice);
454 if (!msg) { free(asn1_pdu);
return nullptr; }
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; }
461 asn1_pdu->msg.choice.indicationMessage->dAppIdentifier = msg->dapp_identifier;
462 asn1_pdu->msg.choice.indicationMessage->ranFunctionIdentifier = msg->ran_function_identifier;
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()));
472 const auto* action = std::get_if<DAppControlAction>(&pdu.
choice);
473 if (!action) { free(asn1_pdu);
return nullptr; }
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; }
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;
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);
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()));
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);
509 *asn1_pdu->msg.choice.dAppControlAction->requestAck = 1;
515 const auto* report = std::get_if<DAppReport>(&pdu.
choice);
516 if (!report) { free(asn1_pdu);
return nullptr; }
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; }
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);
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()));
535 const auto* action = std::get_if<XAppControlAction>(&pdu.
choice);
536 if (!action) { free(asn1_pdu);
return nullptr; }
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; }
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);
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()));
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);
559 *asn1_pdu->msg.choice.xAppControlAction->requestAck = 1;
567 const auto* ack = std::get_if<MessageAck>(&pdu.
choice);
568 if (!ack) { free(asn1_pdu);
return nullptr; }
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; }
575 asn1_pdu->msg.choice.messageAck->requestId = ack->request_id;
576 asn1_pdu->msg.choice.messageAck->responseCode =
578 set_reason(asn1_pdu->msg.choice.messageAck, ack->cause, ack->detail);
583 const auto* resp = std::get_if<ControlResponse>(&pdu.
choice);
584 if (!resp) { free(asn1_pdu);
return nullptr; }
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; }
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;
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);
604 OCTET_STRING_fromBuf(out->controlOutcome,
605 reinterpret_cast<const char*
>(resp->control_outcome->data()),
606 static_cast<int>(resp->control_outcome->size()));
612 const auto* msg = std::get_if<ReleaseMessage>(&pdu.
choice);
613 if (!msg) { free(asn1_pdu);
return nullptr; }
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; }
620 asn1_pdu->msg.choice.releaseMessage->dAppIdentifier = msg->dapp_identifier;
636Pdu Asn1E3Encoder::asn1_to_pdu(
const E3_PDU* asn1_pdu)
const {
640 pdu.
message_id =
static_cast<uint32_t
>(asn1_pdu->id);
647 pdu.timestamp = asn1_pdu->timestamp
648 ?
static_cast<uint64_t
>(*asn1_pdu->timestamp)
652 switch (asn1_pdu->msg.present) {
653 case E3_PDU__msg_PR_setupRequest: {
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);
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);
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);
671 const OCTET_STRING_t* vendor = &asn1_pdu->msg.choice.setupRequest->vendor;
672 req.vendor.assign(
reinterpret_cast<const char*
>(vendor->buf), vendor->size);
678 case E3_PDU__msg_PR_setupResponse: {
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)
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);
694 if (asn1_pdu->msg.choice.setupResponse->dAppIdentifier) {
695 resp.dapp_identifier = *asn1_pdu->msg.choice.setupResponse->dAppIdentifier;
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);
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];
709 func.ran_function_identifier =
static_cast<uint32_t
>(asn_func->ranFunctionIdentifier);
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]));
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]));
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);
731 get_reason(asn1_pdu->msg.choice.setupResponse, resp.cause, resp.detail);
737 case E3_PDU__msg_PR_subscriptionRequest: {
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);
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]));
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]));
759 if (asn1_pdu->msg.choice.subscriptionRequest->subscriptionTime) {
760 req.subscription_time = *asn1_pdu->msg.choice.subscriptionRequest->subscriptionTime;
763 if (asn1_pdu->msg.choice.subscriptionRequest->periodicity) {
764 req.periodicity =
static_cast<uint32_t
>(*asn1_pdu->msg.choice.subscriptionRequest->periodicity);
771 case E3_PDU__msg_PR_subscriptionDelete: {
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);
782 case E3_PDU__msg_PR_subscriptionResponse: {
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)
792 if (asn1_pdu->msg.choice.subscriptionResponse->subscriptionId) {
793 resp.subscription_id = *asn1_pdu->msg.choice.subscriptionResponse->subscriptionId;
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);
803 if (asn1_pdu->msg.choice.subscriptionResponse->periodicity) {
804 resp.periodicity =
static_cast<uint32_t
>(
805 *asn1_pdu->msg.choice.subscriptionResponse->periodicity);
807 get_reason(asn1_pdu->msg.choice.subscriptionResponse, resp.cause, resp.detail);
813 case E3_PDU__msg_PR_indicationMessage: {
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);
821 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.indicationMessage->protocolData;
822 msg.protocol_data.assign(data->buf, data->buf + data->size);
828 case E3_PDU__msg_PR_dAppControlAction: {
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)
839 action.request_ack = asn1_pdu->msg.choice.dAppControlAction->requestAck
840 && *asn1_pdu->msg.choice.dAppControlAction->requestAck;
842 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.dAppControlAction->actionData;
843 action.action_data.assign(data->buf, data->buf + data->size);
849 case E3_PDU__msg_PR_dAppReport: {
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);
857 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.dAppReport->reportData;
858 report.report_data.assign(data->buf, data->buf + data->size);
864 case E3_PDU__msg_PR_xAppControlAction: {
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);
872 action.request_ack = asn1_pdu->msg.choice.xAppControlAction->requestAck
873 && *asn1_pdu->msg.choice.xAppControlAction->requestAck;
875 const OCTET_STRING_t* data = &asn1_pdu->msg.choice.xAppControlAction->xAppControlData;
876 action.xapp_control_data.assign(data->buf, data->buf + data->size);
882 case E3_PDU__msg_PR_messageAck: {
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);
895 case E3_PDU__msg_PR_controlResponse: {
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);
916 case E3_PDU__msg_PR_releaseMessage: {
920 msg.dapp_identifier =
static_cast<uint32_t
>(asn1_pdu->msg.choice.releaseMessage->dAppIdentifier);
927 E3_LOG_ERROR(LOG_TAG) <<
"Unknown ASN.1 PDU type: " << asn1_pdu->msg.present;
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)
#define E3_LOG_ERROR(component)
ResponseCode
E3AP response codes.
@ NEGATIVE
Request rejected.
@ POSITIVE
Request accepted.
@ MESSAGE_ACK
DEPRECATED TO BE REMOVED: libe3 no longer sends it, see CONTROL_RESPONSE.
@ ASN1
ASN.1 PER encoding (standard O-RAN format).
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.
constexpr size_t MAX_DETAIL_SIZE
Largest detail text of a negative response, in bytes (E3-Text).
std::vector< uint8_t > buffer
Encoded data buffer.
EncodingFormat format
Format used for encoding.
Generic E3AP PDU structure.
PduType type
Which alternative of choice is set.
PduChoice choice
The message itself.
uint64_t timestamp
Producer timestamp, in nanoseconds since the Unix epoch.
uint32_t message_id
Unique message identifier.