1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
4 # Copyright 2019 Nokia *
6 # Licensed under the Apache License, Version 2.0 (the "License"); *
7 # you may not use this file except in compliance with the License. *
8 # You may obtain a copy of the License at *
10 # http://www.apache.org/licenses/LICENSE-2.0 *
12 # Unless required by applicable law or agreed to in writing, software *
13 # distributed under the License is distributed on an "AS IS" BASIS, *
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 # See the License for the specific language governing permissions and *
16 # limitations under the License. *
18 ******************************************************************************/
19 #include "e2ap_asn1c_codec.h"
21 void e2ap_asn1c_print_pdu(const E2AP_PDU_t* pdu)
23 xer_fprint(stdout, &asn_DEF_E2AP_PDU, (void *)pdu);
27 void asn1c_xer_print(asn_TYPE_descriptor_t *typeDescriptor, void *data)
29 xer_fprint(stdout, typeDescriptor, (void *)data);
34 E2AP_PDU_t* e2ap_xml_to_pdu(char const* xml_message)
36 // E2AP_PDU_t *pdu = new E2AP_PDU_t();
37 E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
41 uint8_t buf[MAX_XML_BUFFER];
47 char *work_dir = getenv(WORKDIR_ENV);
49 strcpy(XML_path, work_dir);
50 strcat(XML_path, E2AP_XML_DIR);
51 strcat(XML_path, xml_message);
53 LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
54 memset(buf, 0, sizeof(buf));
56 f = fopen(XML_path, "r");
58 LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
63 size = fread(buf, 1, sizeof(buf), f);
64 if(size == 0 || size == sizeof(buf))
66 LOG_E("Input too long: %s", XML_path);
72 rval = xer_decode(0, &asn_DEF_E2AP_PDU, (void **)&pdu, buf, size);
74 assert(rval.code == RC_OK);
79 int e2ap_asn1c_encode_pdu(E2AP_PDU_t* pdu, unsigned char **buffer)
85 assert(buffer != NULL);
87 len = aper_encode_to_new_buffer(&asn_DEF_E2AP_PDU, 0, pdu, (void **)buffer);
90 LOG_E("[E2AP ASN] Unable to aper encode");
94 LOG_D("[E2AP ASN] Encoded succesfully, encoded size = %d", len);
97 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_E2AP_PDU, pdu);
102 void e2ap_asn1c_decode_pdu(E2AP_PDU_t* pdu, unsigned char *buffer, int len)
104 asn_dec_rval_t dec_ret;
106 assert(buffer != NULL);
108 dec_ret = aper_decode(NULL, &asn_DEF_E2AP_PDU, (void **)&pdu, buffer, len, 0, 0);
110 if (dec_ret.code != RC_OK) {
111 LOG_E("[E2AP ASN] Failed to decode pdu");
115 LOG_D("[E2AP ASN] Decoded succesfully");
119 int e2ap_asn1c_get_procedureCode(E2AP_PDU_t* pdu)
121 int procedureCode = -1;
125 case E2AP_PDU_PR_initiatingMessage:
126 procedureCode = pdu->choice.initiatingMessage->procedureCode;
129 case E2AP_PDU_PR_successfulOutcome:
130 procedureCode = pdu->choice.successfulOutcome->procedureCode;
133 case E2AP_PDU_PR_unsuccessfulOutcome:
134 procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
138 LOG_E("[E2AP] Error: Unknown index %d in E2AP PDU", (int)pdu->present);
142 return procedureCode;