SIM-119: Update value of MeasurementTypeName and MeasurementTypeID to constraint
[sim/e2-interface.git] / e2sim / src / messagerouting / e2ap_asn1c_codec.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2020 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
22 void e2ap_asn1c_print_pdu(const E2AP_PDU_t* pdu)
23 {
24   //  xer_fprint(stdout, &asn_DEF_E2AP_PDU, (void *)pdu);
25   xer_fprint(stdout, &asn_DEF_E2AP_PDU, pdu);
26 }
27
28 void asn1c_xer_print(asn_TYPE_descriptor_t *typeDescriptor, void *data)
29 {
30   xer_fprint(stdout, typeDescriptor, (void *)data);
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   printf("xmlpdu1\n");
42
43   uint8_t         buf[MAX_XML_BUFFER];
44   asn_dec_rval_t  rval;
45   size_t          size;
46   FILE            *f;
47
48   char XML_path[300];
49   char *work_dir = getenv(WORKDIR_ENV);
50
51   printf("xmlpdu2\n");  
52
53   strcpy(XML_path, work_dir);
54   strcat(XML_path, E2AP_XML_DIR);
55   strcat(XML_path, xml_message);
56
57   printf("xmlpdu4\n");  
58
59   LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
60   memset(buf, 0, sizeof(buf));
61
62   printf("xmlpdu3\n");  
63
64   f = fopen(XML_path, "r");
65   if(!f){
66      LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
67   }
68
69   printf("xmlpdu5\n");  
70
71   assert(f);
72
73   printf("xmlpdu6\n");  
74
75   size = fread(buf, 1, sizeof(buf), f);
76   if(size == 0 || size == sizeof(buf))
77   {
78     LOG_E("Input too long: %s", XML_path);
79     exit(1);
80   }
81
82   fclose(f);
83
84   printf("xmlpdu7\n");  
85
86   rval = xer_decode(0, &asn_DEF_E2AP_PDU, (void **)&pdu, buf, size);
87
88   printf("xmlpdu8\n");  
89
90   assert(rval.code == RC_OK);
91
92   return pdu;
93 }
94
95
96 E2setupRequest_t* smaller_e2ap_xml_to_pdu(char const* xml_message)
97 {
98   // E2AP_PDU_t *pdu = new E2AP_PDU_t();
99   E2AP_PDU_t *pdu = calloc(1, sizeof(E2AP_PDU_t));
100
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));
104
105   printf("xmlpdu1\n");
106
107   uint8_t         buf[MAX_XML_BUFFER];
108   asn_dec_rval_t  rval;
109   size_t          size;
110   FILE            *f;
111
112   char XML_path[300];
113   char *work_dir = getenv(WORKDIR_ENV);
114
115   printf("xmlpdu2\n");  
116
117   strcpy(XML_path, work_dir);
118   strcat(XML_path, E2AP_XML_DIR);
119   strcat(XML_path, xml_message);
120
121   printf("xmlpdu4\n");  
122
123   LOG_D("Generate E2AP PDU from XML file: %s\n", XML_path);
124   memset(buf, 0, sizeof(buf));
125
126   printf("xmlpdu3\n");  
127
128   f = fopen(XML_path, "r");
129   if(!f){
130      LOG_E("Unable to open %s. Make sure you have set the Environment Variable E2SIM_DIR, see README", XML_path)
131   }
132
133   printf("xmlpdu5\n");  
134
135   assert(f);
136
137   printf("xmlpdu6\n");  
138
139   size = fread(buf, 1, sizeof(buf), f);
140   if(size == 0 || size == sizeof(buf))
141   {
142     LOG_E("Input too long: %s", XML_path);
143     exit(1);
144   }
145
146   fclose(f);
147
148   printf("xmlpdu7\n");
149
150   rval = xer_decode(0, &asn_DEF_E2setupRequest, (void **)&e2setuprequest, buf, size);
151
152   printf("xmlpdu8\n");  
153
154   assert(rval.code == RC_OK);
155
156   return e2setuprequest;
157 }
158
159
160 int e2ap_asn1c_encode_pdu(E2AP_PDU_t* pdu, unsigned char **buffer)
161 {
162   int len;
163
164   *buffer = NULL;
165   assert(pdu != NULL);
166   assert(buffer != NULL);
167
168   len = aper_encode_to_new_buffer(&asn_DEF_E2AP_PDU, 0, pdu, (void **)buffer);
169
170   if (len < 0)  {
171     LOG_E("[E2AP ASN] Unable to aper encode");
172     exit(1);
173   }
174   else {
175     LOG_D("[E2AP ASN] Encoded succesfully, encoded size = %d", len);
176   }
177
178   ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
179
180   return len;
181 }
182
183 void e2ap_asn1c_decode_pdu(E2AP_PDU_t* pdu, unsigned char *buffer, int len)
184 {
185   asn_dec_rval_t dec_ret;
186
187   assert(buffer != NULL);
188
189   dec_ret = aper_decode(NULL, &asn_DEF_E2AP_PDU, (void **)&pdu, buffer, len, 0, 0);
190
191   if (dec_ret.code != RC_OK) {
192     LOG_E("[E2AP ASN] Failed to decode pdu");
193     exit(1);
194   }
195   else {
196     LOG_D("[E2AP ASN] Decoded succesfully");
197   }
198 }
199
200 int e2ap_asn1c_get_procedureCode(E2AP_PDU_t* pdu)
201 {
202   int procedureCode = -1;
203
204   switch(pdu->present)
205   {
206     case E2AP_PDU_PR_initiatingMessage:
207       LOG_I("Initiating E2AP PDU PR message");
208       procedureCode = pdu->choice.initiatingMessage->procedureCode;
209       break;
210
211     case E2AP_PDU_PR_successfulOutcome:
212       procedureCode = pdu->choice.successfulOutcome->procedureCode;
213       break;
214
215     case E2AP_PDU_PR_unsuccessfulOutcome:
216       procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
217       break;
218
219     default:
220       LOG_E("[E2AP] Error: Unknown index %d in E2AP PDU", (int)pdu->present);
221       break;
222   }
223
224   return procedureCode;
225 }