1 /*****************************************************************************
3 # Copyright 2020 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)
24 // xer_fprint(stdout, &asn_DEF_E2AP_PDU, (void *)pdu);
25 xer_fprint(stdout, &asn_DEF_E2AP_PDU, pdu);
28 void asn1c_xer_print(asn_TYPE_descriptor_t *typeDescriptor, void *data)
30 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));
43 uint8_t buf[MAX_XML_BUFFER];
49 char *work_dir = getenv(WORKDIR_ENV);
53 strcpy(XML_path, work_dir);
54 strcat(XML_path, E2AP_XML_DIR);
55 strcat(XML_path, xml_message);
59 LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
60 memset(buf, 0, sizeof(buf));
64 f = fopen(XML_path, "r");
66 LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
75 size = fread(buf, 1, sizeof(buf), f);
76 if(size == 0 || size == sizeof(buf))
78 LOG_E("Input too long: %s", XML_path);
86 rval = xer_decode(0, &asn_DEF_E2AP_PDU, (void **)&pdu, buf, size);
90 assert(rval.code == RC_OK);
96 E2setupRequest_t* smaller_e2ap_xml_to_pdu(char const* xml_message)
98 // E2AP_PDU_t *pdu = new E2AP_PDU_t();
99 E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
101 // GlobalE2node_ID_t *globale2nodeid = (GlobalE2node_ID_t*)calloc(1, sizeof(GlobalE2node_ID_t));
102 GlobalE2node_ID_t *globale2nodeid = (GlobalE2node_ID_t*)calloc(1, sizeof(GlobalE2node_ID_t));
103 E2setupRequest_t *e2setuprequest = (E2setupRequest_t*)calloc(1,sizeof(E2setupRequest_t));
107 uint8_t buf[MAX_XML_BUFFER];
113 char *work_dir = getenv(WORKDIR_ENV);
117 strcpy(XML_path, work_dir);
118 strcat(XML_path, E2AP_XML_DIR);
119 strcat(XML_path, xml_message);
123 LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
124 memset(buf, 0, sizeof(buf));
128 f = fopen(XML_path, "r");
130 LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
139 size = fread(buf, 1, sizeof(buf), f);
140 if(size == 0 || size == sizeof(buf))
142 LOG_E("Input too long: %s", XML_path);
150 rval = xer_decode(0, &asn_DEF_E2setupRequest, (void **)&e2setuprequest, buf, size);
154 assert(rval.code == RC_OK);
156 return e2setuprequest;
160 int e2ap_asn1c_encode_pdu(E2AP_PDU_t* pdu, unsigned char **buffer)
166 assert(buffer != NULL);
168 len = aper_encode_to_new_buffer(&asn_DEF_E2AP_PDU, 0, pdu, (void **)buffer);
171 LOG_E("[E2AP ASN] Unable to aper encode");
175 LOG_D("[E2AP ASN] Encoded succesfully, encoded size = %d", len);
178 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
183 void e2ap_asn1c_decode_pdu(E2AP_PDU_t* pdu, unsigned char *buffer, int len)
185 asn_dec_rval_t dec_ret;
187 assert(buffer != NULL);
189 dec_ret = aper_decode(NULL, &asn_DEF_E2AP_PDU, (void **)&pdu, buffer, len, 0, 0);
191 if (dec_ret.code != RC_OK) {
192 LOG_E("[E2AP ASN] Failed to decode pdu");
196 LOG_D("[E2AP ASN] Decoded succesfully");
200 int e2ap_asn1c_get_procedureCode(E2AP_PDU_t* pdu)
202 int procedureCode = -1;
206 case E2AP_PDU_PR_initiatingMessage:
207 LOG_I("Initiating E2AP PDU PR message");
208 procedureCode = pdu->choice.initiatingMessage->procedureCode;
211 case E2AP_PDU_PR_successfulOutcome:
212 procedureCode = pdu->choice.successfulOutcome->procedureCode;
215 case E2AP_PDU_PR_unsuccessfulOutcome:
216 procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
220 LOG_E("[E2AP] Error: Unknown index %d in E2AP PDU", (int)pdu->present);
224 return procedureCode;