Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / src / E2AP / e2ap_asn1c_codec.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 # Copyright 2019 Nokia                                                       *
5 #                                                                            *
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                                    *
9 #                                                                            *
10 #      http://www.apache.org/licenses/LICENSE-2.0                            *
11 #                                                                            *
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.                                             *
17 #                                                                            *
18 ******************************************************************************/
19 #include "e2ap_asn1c_codec.h"
20
21 void e2ap_asn1c_print_pdu(const E2AP_PDU_t* pdu)
22 {
23   xer_fprint(stdout, &asn_DEF_E2AP_PDU, (void *)pdu);
24   printf("\n");
25 }
26
27 void asn1c_xer_print(asn_TYPE_descriptor_t *typeDescriptor, void *data)
28 {
29   xer_fprint(stdout, typeDescriptor, (void *)data);
30   printf("\n");
31 }
32
33
34 E2AP_PDU_t* e2ap_xml_to_pdu(char const* xml_message)
35 {
36   // E2AP_PDU_t *pdu = new E2AP_PDU_t();
37   E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
38
39   assert(pdu != 0);
40
41   uint8_t         buf[MAX_XML_BUFFER];
42   asn_dec_rval_t  rval;
43   size_t          size;
44   FILE            *f;
45
46   char XML_path[300];
47   char *work_dir = getenv(WORKDIR_ENV);
48
49   strcpy(XML_path, work_dir);
50   strcat(XML_path, E2AP_XML_DIR);
51   strcat(XML_path, xml_message);
52
53   LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
54   memset(buf, 0, sizeof(buf));
55
56   f = fopen(XML_path, "r");
57   if(!f){
58      LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
59   }
60
61   assert(f);
62
63   size = fread(buf, 1, sizeof(buf), f);
64   if(size == 0 || size == sizeof(buf))
65   {
66     LOG_E("Input too long: %s", XML_path);
67     exit(1);
68   }
69
70   fclose(f);
71
72   rval = xer_decode(0, &asn_DEF_E2AP_PDU, (void **)&pdu, buf, size);
73
74   assert(rval.code == RC_OK);
75
76   return pdu;
77 }
78
79 int e2ap_asn1c_encode_pdu(E2AP_PDU_t* pdu, unsigned char **buffer)
80 {
81   int len;
82
83   *buffer = NULL;
84   assert(pdu != NULL);
85   assert(buffer != NULL);
86
87   len = aper_encode_to_new_buffer(&asn_DEF_E2AP_PDU, 0, pdu, (void **)buffer);
88
89   if (len < 0)  {
90     LOG_E("[E2AP ASN] Unable to aper encode");
91     exit(1);
92   }
93   else {
94     LOG_D("[E2AP ASN] Encoded succesfully, encoded size = %d", len);
95   }
96
97   ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_E2AP_PDU, pdu);
98
99   return len;
100 }
101
102 void e2ap_asn1c_decode_pdu(E2AP_PDU_t* pdu, unsigned char *buffer, int len)
103 {
104   asn_dec_rval_t dec_ret;
105
106   assert(buffer != NULL);
107
108   dec_ret = aper_decode(NULL, &asn_DEF_E2AP_PDU, (void **)&pdu, buffer, len, 0, 0);
109
110   if (dec_ret.code != RC_OK) {
111     LOG_E("[E2AP ASN] Failed to decode pdu");
112     exit(1);
113   }
114   else {
115     LOG_D("[E2AP ASN] Decoded succesfully");
116   }
117 }
118
119 int e2ap_asn1c_get_procedureCode(E2AP_PDU_t* pdu)
120 {
121   int procedureCode = -1;
122
123   switch(pdu->present)
124   {
125     case E2AP_PDU_PR_initiatingMessage:
126       procedureCode = pdu->choice.initiatingMessage->procedureCode;
127       break;
128
129     case E2AP_PDU_PR_successfulOutcome:
130       procedureCode = pdu->choice.successfulOutcome->procedureCode;
131       break;
132
133     case E2AP_PDU_PR_unsuccessfulOutcome:
134       procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
135       break;
136
137     default:
138       LOG_E("[E2AP] Error: Unknown index %d in E2AP PDU", (int)pdu->present);
139       break;
140   }
141
142   return procedureCode;
143 }