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"
22 void e2ap_asn1c_print_pdu(const E2AP_PDU_t* pdu)
25 // xer_fprint(stdout, &asn_DEF_E2AP_PDU, (void *)pdu);
26 xer_fprint(stdout, &asn_DEF_E2AP_PDU, pdu);
31 void asn1c_xer_print(asn_TYPE_descriptor_t *typeDescriptor, void *data)
33 xer_fprint(stdout, typeDescriptor, (void *)data);
38 E2AP_PDU_t* e2ap_xml_to_pdu(char const* xml_message)
40 // E2AP_PDU_t *pdu = new E2AP_PDU_t();
41 E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
47 uint8_t buf[MAX_XML_BUFFER];
53 char *work_dir = getenv(WORKDIR_ENV);
57 strcpy(XML_path, work_dir);
58 strcat(XML_path, E2AP_XML_DIR);
59 strcat(XML_path, xml_message);
63 LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
64 memset(buf, 0, sizeof(buf));
68 f = fopen(XML_path, "r");
70 LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
79 size = fread(buf, 1, sizeof(buf), f);
80 if(size == 0 || size == sizeof(buf))
82 LOG_E("Input too long: %s", XML_path);
90 rval = xer_decode(0, &asn_DEF_E2AP_PDU, (void **)&pdu, buf, size);
94 assert(rval.code == RC_OK);
100 E2setupRequest_t* smaller_e2ap_xml_to_pdu(char const* xml_message)
102 // E2AP_PDU_t *pdu = new E2AP_PDU_t();
103 E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
105 // GlobalE2node_ID_t *globale2nodeid = (GlobalE2node_ID_t*)calloc(1, sizeof(GlobalE2node_ID_t));
106 GlobalE2node_ID_t *globale2nodeid = (GlobalE2node_ID_t*)calloc(1, sizeof(GlobalE2node_ID_t));
107 E2setupRequest_t *e2setuprequest = (E2setupRequest_t*)calloc(1,sizeof(E2setupRequest_t));
111 uint8_t buf[MAX_XML_BUFFER];
117 char *work_dir = getenv(WORKDIR_ENV);
121 strcpy(XML_path, work_dir);
122 strcat(XML_path, E2AP_XML_DIR);
123 strcat(XML_path, xml_message);
127 LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
128 memset(buf, 0, sizeof(buf));
132 f = fopen(XML_path, "r");
134 LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
143 size = fread(buf, 1, sizeof(buf), f);
144 if(size == 0 || size == sizeof(buf))
146 LOG_E("Input too long: %s", XML_path);
154 rval = xer_decode(0, &asn_DEF_E2setupRequest, (void **)&e2setuprequest, buf, size);
158 assert(rval.code == RC_OK);
160 return e2setuprequest;
164 int e2ap_asn1c_encode_pdu(E2AP_PDU_t* pdu, unsigned char **buffer)
170 assert(buffer != NULL);
172 len = aper_encode_to_new_buffer(&asn_DEF_E2AP_PDU, 0, pdu, (void **)buffer);
175 LOG_E("[E2AP ASN] Unable to aper encode");
179 LOG_D("[E2AP ASN] Encoded succesfully, encoded size = %d", len);
182 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_E2AP_PDU, pdu);
187 void e2ap_asn1c_decode_pdu(E2AP_PDU_t* pdu, unsigned char *buffer, int len)
189 asn_dec_rval_t dec_ret;
191 assert(buffer != NULL);
193 dec_ret = aper_decode(NULL, &asn_DEF_E2AP_PDU, (void **)&pdu, buffer, len, 0, 0);
195 if (dec_ret.code != RC_OK) {
196 LOG_E("[E2AP ASN] Failed to decode pdu");
200 LOG_D("[E2AP ASN] Decoded succesfully");
204 int e2ap_asn1c_get_procedureCode(E2AP_PDU_t* pdu)
206 int procedureCode = -1;
210 case E2AP_PDU_PR_initiatingMessage:
211 fprintf(stderr,"initiating message\n");
212 procedureCode = pdu->choice.initiatingMessage->procedureCode;
215 case E2AP_PDU_PR_successfulOutcome:
216 procedureCode = pdu->choice.successfulOutcome->procedureCode;
219 case E2AP_PDU_PR_unsuccessfulOutcome:
220 procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
224 LOG_E("[E2AP] Error: Unknown index %d in E2AP PDU", (int)pdu->present);
228 return procedureCode;