memory leak issues fixed
[ric-app/rc.git] / e2ap / wrapper.c
1 #include <errno.h>
2 #include "wrapper.h"
3
4 size_t encode_E2AP_PDU(E2AP_PDU_t* pdu, void* buffer, size_t buf_size)
5 {
6     asn_enc_rval_t encode_result;
7     encode_result = aper_encode_to_buffer(&asn_DEF_E2AP_PDU, NULL, pdu, buffer, buf_size);
8     ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
9     if(encode_result.encoded == -1) {
10         fprintf(stderr, "Cannot encode %s: %s\n", encode_result.failed_type->name, strerror(errno));
11         return -1;
12     } else {
13           return encode_result.encoded;
14     }
15 }
16
17 E2AP_PDU_t* decode_E2AP_PDU(const void* buffer, size_t buf_size)
18 {
19     asn_dec_rval_t decode_result;
20     E2AP_PDU_t *pdu = 0;
21     decode_result = aper_decode_complete(NULL, &asn_DEF_E2AP_PDU, (void **)&pdu, buffer, buf_size);
22     if(decode_result.code == RC_OK) {
23         return pdu;
24     } else {
25         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
26         return 0;
27     }
28 }
29
30 ssize_t e2ap_encode_ric_control_request_message(void *buffer, size_t buf_size, long ricRequestorID, long ricRequestSequenceNumber, 
31                   long ranFunctionID, void *ricControlHdr, size_t ricControlHdrSize, void *ricControlMsg, size_t ricControlMsgSize)
32 {
33   
34     E2AP_PDU_t *init = (E2AP_PDU_t *)calloc(1, sizeof(E2AP_PDU_t));
35     if(!init) {
36         fprintf(stderr, "alloc E2AP_PDU failed\n");
37         return -1;
38     }
39
40     
41     InitiatingMessage_t *initiatingMsg = (InitiatingMessage_t *)calloc(1, sizeof(InitiatingMessage_t));
42     if(!initiatingMsg) {
43         fprintf(stderr, "alloc InitiatingMessage failed\n");
44         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, init);
45         return -1;
46     }
47
48         init->choice.initiatingMessage = initiatingMsg;
49     init->present = E2AP_PDU_PR_initiatingMessage;
50
51     initiatingMsg->procedureCode = ProcedureCode_id_RICcontrol;
52     initiatingMsg->criticality = Criticality_reject;
53     initiatingMsg->value.present = InitiatingMessage__value_PR_RICcontrolRequest;
54
55     RICcontrolRequest_t *control_request = &initiatingMsg->value.choice.RICcontrolRequest;
56
57
58     //RICrequestID
59     RICcontrolRequest_IEs_t *controlReqID = (RICcontrolRequest_IEs_t *)calloc(1, sizeof(RICcontrolRequest_IEs_t));
60     if(!controlReqID) {
61         fprintf(stderr, "alloc RICrequestID failed\n");
62         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, init);
63         return -1;
64     }
65
66     controlReqID->criticality = Criticality_reject;
67     controlReqID->id = ProtocolIE_ID_id_RICrequestID;
68
69     controlReqID->value.present = RICcontrolRequest_IEs__value_PR_RICrequestID;
70     RICrequestID_t *ricrequest_ie = &controlReqID->value.choice.RICrequestID;
71     ricrequest_ie->ricRequestorID = ricRequestorID;
72     ricrequest_ie->ricInstanceID = ricRequestSequenceNumber;
73     ASN_SEQUENCE_ADD(&control_request->protocolIEs.list, controlReqID);
74
75     //RICfunctionID
76     RICcontrolRequest_IEs_t *controlReqFunID = (RICcontrolRequest_IEs_t *)calloc(1, sizeof(RICcontrolRequest_IEs_t));
77     if(!controlReqFunID) {
78         fprintf(stderr, "alloc RICrequestID failed\n");
79         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, init);
80         return -1;
81     }
82
83     controlReqFunID->criticality = Criticality_reject;
84     controlReqFunID->id = ProtocolIE_ID_id_RANfunctionID;
85     controlReqFunID->value.present = RICcontrolRequest_IEs__value_PR_RANfunctionID;
86     RANfunctionID_t *ranfunction_ie = &controlReqFunID->value.choice.RANfunctionID;
87     *ranfunction_ie = ranFunctionID;
88     ASN_SEQUENCE_ADD(&control_request->protocolIEs.list, controlReqFunID);
89
90     // RICControlHdr
91     RICcontrolRequest_IEs_t *controlReqHdr = (RICcontrolRequest_IEs_t *)calloc(1, sizeof(RICcontrolRequest_IEs_t));
92     if(!controlReqHdr) {
93         fprintf(stderr, "alloc RICcontrolRequest_IEs_t failed\n");
94         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, init);
95         return -1;
96     }
97     controlReqHdr->criticality = Criticality_reject;
98     controlReqHdr->id = ProtocolIE_ID_id_RICcontrolHeader;
99     controlReqHdr->value.present = RICcontrolRequest_IEs__value_PR_RICcontrolHeader;
100     RICcontrolHeader_t *controlHdr = &controlReqHdr->value.choice.RICcontrolHeader;
101     controlHdr->buf = (uint8_t *)calloc(1, ricControlHdrSize);
102     if(!controlHdr->buf) {
103         fprintf(stderr, "alloc RICcontrolHeader_t buf failed\n");
104         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, init);
105         return -1;
106     }
107
108     memcpy(controlHdr->buf, ricControlHdr, ricControlHdrSize);
109     controlHdr->size = ricControlHdrSize;
110     ASN_SEQUENCE_ADD(&control_request->protocolIEs.list, controlReqHdr);
111
112     //Ric Control Message
113     RICcontrolRequest_IEs_t *controlReqMsg = (RICcontrolRequest_IEs_t *)calloc(1, sizeof(RICcontrolRequest_IEs_t));
114     if(!controlReqMsg) {
115         fprintf(stderr, "alloc RICcontrolRequest_IEs_t failed\n");
116         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, init);
117         return -1;
118     }
119     controlReqMsg->criticality = Criticality_reject;
120     controlReqMsg->id = ProtocolIE_ID_id_RICcontrolMessage;
121     controlReqMsg->value.present = RICcontrolRequest_IEs__value_PR_RICcontrolMessage;
122     RICcontrolMessage_t *controlMsg = &controlReqMsg->value.choice.RICcontrolMessage;
123     controlMsg->buf = (uint8_t *)calloc(1, ricControlMsgSize);
124     if(!controlMsg->buf) {
125         fprintf(stderr, "alloc RICcontrolMessage_t buf failed\n");
126         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, init);
127         return -1;
128     }
129
130     memcpy(controlMsg->buf, ricControlMsg, ricControlMsgSize);
131     controlMsg->size = ricControlMsgSize;
132     ASN_SEQUENCE_ADD(&control_request->protocolIEs.list, controlReqMsg);
133
134     fprintf(stderr, "showing xer of asn_DEF_E2AP_PDU data\n");
135     xer_fprint(stderr, &asn_DEF_E2AP_PDU, init);
136     fprintf(stderr, "\n");
137     fprintf(stderr, "After xer of asn_DEF_E2AP_PDU data\n");
138    
139     return encode_E2AP_PDU(init, buffer, buf_size);
140 }
141
142 RICControlAcknowledge* e2ap_decode_ric_control_acknowledge_message(void *buffer, size_t buf_size)
143 {
144     E2AP_PDU_t *pdu = decode_E2AP_PDU(buffer, buf_size);
145     if ( pdu != NULL && pdu->present == E2AP_PDU_PR_successfulOutcome)
146     {
147         SuccessfulOutcome_t* successfulOutcome = pdu->choice.successfulOutcome;
148         if ( successfulOutcome->procedureCode == ProcedureCode_id_RICcontrol
149             && successfulOutcome->value.present == SuccessfulOutcome__value_PR_RICcontrolAcknowledge)
150         {
151                 RICcontrolAcknowledge_t *controlAck = &(successfulOutcome->value.choice.RICcontrolAcknowledge);
152                 RICControlAcknowledge *msg = (RICControlAcknowledge *)calloc(1, sizeof(RICControlAcknowledge));
153                 int i = 0;
154                  for (i; i < controlAck->protocolIEs.list.count; ++i )
155             {
156                 if(controlAck->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICrequestID)
157                 {
158                     msg->requestorID = controlAck->protocolIEs.list.array[i]->value.choice.RICrequestID.ricRequestorID;
159                     msg->instanceID = controlAck->protocolIEs.list.array[i]->value.choice.RICrequestID.ricInstanceID;
160                 }
161                 else if (controlAck->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RANfunctionID) {
162                     msg->ranfunctionID = controlAck->protocolIEs.list.array[i]->value.choice.RANfunctionID;
163                 }
164                 else if(controlAck->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICcallProcessID) {
165                     size_t callProcessIDSize = controlAck->protocolIEs.list.array[i]->value.choice.RICcallProcessID.size;
166                     msg->callProcessID = calloc(1, callProcessIDSize);
167                     if (!msg->callProcessID) {
168                         fprintf(stderr, "alloc RICcallProcessID failed\n");
169                         e2ap_free_decoded_ric_control_ack(msg);
170                         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
171                         return NULL;
172                     }
173
174                     memcpy(msg->callProcessID, controlAck->protocolIEs.list.array[i]->value.choice.RICcallProcessID.buf, callProcessIDSize);
175                     msg->callProcessIDSize = callProcessIDSize;
176                 }
177                 else if(controlAck->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICcontrolOutcome) {
178                     size_t ricControlOutComeSize = controlAck->protocolIEs.list.array[i]->value.choice.RICcontrolOutcome.size;
179                     msg->ricControlOutCome = calloc(1, ricControlOutComeSize);
180                     if (!msg->ricControlOutCome) {
181                         fprintf(stderr, "alloc ricControlOutCome failed\n");
182                         e2ap_free_decoded_ric_control_ack(msg);
183                         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
184                         return NULL;
185                     }
186
187                     memcpy(msg->ricControlOutCome, controlAck->protocolIEs.list.array[i]->value.choice.RICcontrolOutcome.buf, ricControlOutComeSize);
188                     msg->ricControlOutComeSize = ricControlOutComeSize;
189                 }
190         }
191                  if(pdu != NULL)
192                         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
193                 return msg;
194         }
195         }
196
197     if(pdu != NULL)
198         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
199     return NULL;
200 }
201  
202 void e2ap_free_decoded_ric_control_ack(RICControlAcknowledge* msg) {
203     if(msg == NULL) {
204         return;
205     }
206     if(msg->callProcessID != NULL) {
207         free(msg->callProcessID);
208         msg->callProcessID = NULL;
209     }
210     if(msg->ricControlOutCome != NULL) {
211         free(msg->ricControlOutCome);
212         msg->ricControlOutCome = NULL;
213     }
214         free(msg);
215     msg = NULL;
216 }
217
218 RICControlFailure* e2ap_decode_ric_control_failure_message(void *buffer, size_t buf_size)
219 {
220     E2AP_PDU_t *pdu = decode_E2AP_PDU(buffer, buf_size);
221     if ( pdu != NULL && pdu->present == E2AP_PDU_PR_unsuccessfulOutcome)
222     {
223         UnsuccessfulOutcome_t* unSuccessfulOutcome = pdu->choice.unsuccessfulOutcome;
224         if ( unSuccessfulOutcome->procedureCode == ProcedureCode_id_RICcontrol
225             && unSuccessfulOutcome->value.present == UnsuccessfulOutcome__value_PR_RICcontrolFailure)
226         {
227                 RICcontrolFailure_t *controlFailure = &(unSuccessfulOutcome->value.choice.RICcontrolFailure);
228                 RICControlFailure *msg = (RICControlFailure *)calloc(1, sizeof(RICControlFailure));
229                 int i = 0;
230                  for (i; i < controlFailure->protocolIEs.list.count; ++i )
231             {
232                 if(controlFailure->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICrequestID)
233                 {
234                     msg->requestorID = controlFailure->protocolIEs.list.array[i]->value.choice.RICrequestID.ricRequestorID;
235                     msg->instanceID = controlFailure->protocolIEs.list.array[i]->value.choice.RICrequestID.ricInstanceID;
236                 }
237                 else if (controlFailure->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RANfunctionID) {
238                     msg->ranfunctionID = controlFailure->protocolIEs.list.array[i]->value.choice.RANfunctionID;
239                 }
240                 else if(controlFailure->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICcallProcessID) {
241                     size_t callProcessIDSize = controlFailure->protocolIEs.list.array[i]->value.choice.RICcallProcessID.size;
242                     msg->callProcessID = calloc(1, callProcessIDSize);
243                     if (!msg->callProcessID) {
244                         fprintf(stderr, "alloc RICcallProcessID failed\n");
245                         e2ap_free_decoded_ric_control_failure(msg);
246                         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
247                          return NULL;
248                     }
249
250                 memcpy(msg->callProcessID, controlFailure->protocolIEs.list.array[i]->value.choice.RICcallProcessID.buf, callProcessIDSize);
251                     msg->callProcessIDSize = callProcessIDSize;
252                 }
253                 else if(controlFailure->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICcontrolOutcome) {
254                         msg->causeType = controlFailure->protocolIEs.list.array[i]->value.choice.Cause.present;
255                         if (msg->causeType == Cause_PR_ricRequest) {
256                                 msg->causeValue = controlFailure->protocolIEs.list.array[i]->value.choice.Cause.choice.ricRequest;
257                         } else if (msg->causeType == Cause_PR_ricService) {
258                                 msg->causeValue = controlFailure->protocolIEs.list.array[i]->value.choice.Cause.choice.ricService;
259                         } else if (msg->causeType == Cause_PR_transport) {
260                                 msg->causeValue = controlFailure->protocolIEs.list.array[i]->value.choice.Cause.choice.transport;
261                         } else if (msg->causeType == Cause_PR_protocol) {
262                                 msg->causeValue = controlFailure->protocolIEs.list.array[i]->value.choice.Cause.choice.protocol;
263                         } else if (msg->causeType == Cause_PR_misc) {
264                                 msg->causeValue = controlFailure->protocolIEs.list.array[i]->value.choice.Cause.choice.misc;
265                         }else {
266                                 msg->causeType == Cause_PR_NOTHING;
267                         }
268                 }
269                 else if(controlFailure->protocolIEs.list.array[i]->id == ProtocolIE_ID_id_RICcontrolOutcome) {
270                     size_t ricControlOutComeSize = controlFailure->protocolIEs.list.array[i]->value.choice.RICcontrolOutcome.size;
271                     msg->ricControlOutCome = calloc(1, ricControlOutComeSize);
272                     if (!msg->ricControlOutCome) {
273                         fprintf(stderr, "alloc ricControlOutCome failed\n");
274                         e2ap_free_decoded_ric_control_failure(msg);
275                         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
276                         return NULL;
277                     }
278
279                     memcpy(msg->ricControlOutCome, controlFailure->protocolIEs.list.array[i]->value.choice.RICcontrolOutcome.buf, ricControlOutComeSize);
280                     msg->ricControlOutComeSize = ricControlOutComeSize;
281
282                 }
283         }
284                  if(pdu != NULL)
285                      ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
286
287                 return msg;
288         }
289         }
290
291         if(pdu != NULL)
292                 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
293
294         return NULL;
295 }
296
297 void e2ap_free_decoded_ric_control_failure(RICControlFailure* msg) {
298     if(msg == NULL) {
299         return;
300     }
301     if(msg->callProcessID != NULL) {
302         free(msg->callProcessID);
303         msg->callProcessID = NULL;
304     }
305     if(msg->ricControlOutCome != NULL) {
306         free(msg->ricControlOutCome);
307         msg->ricControlOutCome = NULL;
308     }
309         free(msg);
310     msg = NULL;
311 }