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