75abd13567e9c8f75cddaa2dc16d660fdee92b4a
[ric-app/admin.git] / src / E2AP-c / e2ap_indication.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
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 /*
20  * ric_indication.c
21  *
22  *  Created on: Jul 11, 2019
23  *      Author: sjana, Ashwin Sridharan
24  */
25
26 #include <e2ap_indication.hpp>
27
28 // Set up the initiating message and also allocate protocolIEs in container
29 // Note : this bypasses requirement to use ASN_SEQUENCE_ADD. We can directly
30 // assign pointers to the array in ProtocolIE. However, this also leaves us on the
31 // hook to manually clear the memory
32
33 ric_indication::ric_indication(void){
34
35   e2ap_pdu_obj = 0;
36   e2ap_pdu_obj = (E2AP_PDU_t * )calloc(1, sizeof(E2AP_PDU_t));
37   assert(e2ap_pdu_obj != 0);
38
39   initMsg = 0;
40   initMsg = (InitiatingMessage_t * )calloc(1, sizeof(InitiatingMessage_t));
41   assert(initMsg != 0);
42
43   IE_array = 0;
44   IE_array = (RICindication_IEs_t *)calloc(NUM_INDICATION_IES, sizeof(RICindication_IEs_t));
45   assert(IE_array != 0);
46
47   e2ap_pdu_obj->present = E2AP_PDU_PR_initiatingMessage;
48   e2ap_pdu_obj->choice.initiatingMessage = initMsg;
49
50   RICindication_t * ric_indication = &(initMsg->value.choice.RICindication);
51   for(int i = 0; i < NUM_INDICATION_IES; i++){
52     ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[i]));
53   }
54                        
55   
56     
57 };
58
59
60
61 // Clear assigned protocolIE list from RIC indication IE container
62 ric_indication::~ric_indication(void){
63
64   mdclog_write(MDCLOG_INFO, "Freeing E2AP Indication object memory");
65   RICindication_t *ricIndication  = &(initMsg->value.choice.RICindication);
66   for(int i = 0; i < ricIndication->protocolIEs.list.size; i++){
67     ricIndication->protocolIEs.list.array[i] = 0;
68   }
69   if (ricIndication->protocolIEs.list.size > 0){
70     free(ricIndication->protocolIEs.list.array);
71     ricIndication->protocolIEs.list.array = 0;
72     ricIndication->protocolIEs.list.count = 0;
73     ricIndication->protocolIEs.list.size = 0;
74   }
75   
76   free(IE_array);
77   ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, e2ap_pdu_obj);
78   mdclog_write(MDCLOG_INFO, "Freed E2AP Indication object mempory");
79 }
80
81
82 bool ric_indication::encode_e2ap_indication(unsigned char *buf, size_t *size, E2APRicIndication & dinput){
83
84   initMsg->procedureCode = ProcedureCode_id_ricIndication;
85   initMsg->criticality = Criticality_ignore;
86   initMsg->value.present = InitiatingMessage__value_PR_RICindication;
87
88   bool res;
89   res = set_fields(initMsg, dinput);
90   if (!res){
91     return false;
92   }
93
94   int ret_constr = asn_check_constraints(&asn_DEF_E2AP_PDU, e2ap_pdu_obj, errbuf, &errbuf_len);
95   if(ret_constr){
96     error_string.assign(&errbuf[0], errbuf_len);
97     error_string = "Error encoding E2AP Indication message. Reason = " + error_string;
98     return false;
99   }
100
101   //xer_fprint(stdout, &asn_DEF_E2AP_PDU, e2ap_pdu_obj);
102   
103   asn_enc_rval_t retval = asn_encode_to_buffer(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, e2ap_pdu_obj, buf, *size);
104   
105   if(retval.encoded == -1){
106     error_string.assign(strerror(errno));
107     return false;
108   }
109
110   else {
111     if(*size < retval.encoded){
112       std::stringstream ss;
113       ss  <<"Error encoding event trigger definition. Reason =  encoded pdu size " << retval.encoded << " exceeds buffer size " << *size << std::endl;
114       error_string = ss.str();
115       return false;
116     }
117   }
118
119   *size = retval.encoded;
120   return true;
121   
122 }
123
124 bool ric_indication::set_fields(InitiatingMessage_t *initMsg, E2APRicIndication &dinput){
125   unsigned int i;
126
127   if (initMsg == 0){
128     error_string = "Invalid reference for E2AP Indication message in set_fields";
129     return false;
130   }
131   
132   for(i = 0; i < NUM_INDICATION_IES;i++){
133     memset(&(IE_array[i]), 0, sizeof(RICindication_IEs_t));
134   }
135  
136  
137   i = 0;
138   RICindication_IEs_t *ies_ricreq = &IE_array[i];
139   
140   ies_ricreq->criticality = Criticality_reject;
141   ies_ricreq->id = ProtocolIE_ID_id_RICrequestID;
142   ies_ricreq->value.present = RICindication_IEs__value_PR_RICrequestID;
143   RICrequestID_t *ricrequest_ie = &ies_ricreq->value.choice.RICrequestID;
144   ricrequest_ie->ricRequestorID = dinput.req_id;
145   ricrequest_ie->ricRequestSequenceNumber = dinput.req_seq_no;
146
147   i = 1;
148   RICindication_IEs_t *ies_ranfunc = &IE_array[i];
149   ies_ranfunc->criticality = Criticality_reject;
150   ies_ranfunc->id = ProtocolIE_ID_id_RANfunctionID;
151   ies_ranfunc->value.present = RICindication_IEs__value_PR_RANfunctionID;
152   RANfunctionID_t *ranfunction_ie = &ies_ranfunc->value.choice.RANfunctionID;
153   *ranfunction_ie = dinput.func_id;
154
155   i = 2;
156   RICindication_IEs_t *ies_actid = &IE_array[i];
157   ies_actid->criticality = Criticality_reject;
158   ies_actid->id = ProtocolIE_ID_id_RICactionID;
159   ies_actid->value.present = RICindication_IEs__value_PR_RICactionID;
160   RICactionID_t *ricaction_ie = &ies_actid->value.choice.RICactionID;
161   *ricaction_ie = dinput.action_id;
162
163   i = 3;
164   RICindication_IEs_t *ies_ricsn = &IE_array[i];
165   ies_ricsn->criticality = Criticality_reject;
166   ies_ricsn->id = ProtocolIE_ID_id_RICindicationSN;
167   ies_ricsn->value.present = RICindication_IEs__value_PR_RICindicationSN;
168   RICindicationSN_t *ricsn_ie = &ies_ricsn->value.choice.RICindicationSN;
169   *ricsn_ie = dinput.indication_sn;
170
171
172   i = 4;
173   RICindication_IEs_t *ies_indtyp = &IE_array[i];
174   ies_indtyp->criticality = Criticality_reject;
175   ies_indtyp->id = ProtocolIE_ID_id_RICindicationType;
176   ies_indtyp->value.present = RICindication_IEs__value_PR_RICindicationType;
177   RICindicationType_t *rictype_ie = &ies_indtyp->value.choice.RICindicationType;
178   *rictype_ie = dinput.indication_type;
179
180   i = 5;
181   RICindication_IEs_t *ies_richead = &IE_array[i];
182   ies_richead->criticality = Criticality_reject;
183   ies_richead->id = ProtocolIE_ID_id_RICindicationHeader;
184   ies_richead->value.present = RICindication_IEs__value_PR_RICindicationHeader;
185   RICindicationHeader_t *richeader_ie = &ies_richead->value.choice.RICindicationHeader;
186   richeader_ie->buf = dinput.indication_header;
187   richeader_ie->size = dinput.indication_header_size;
188   
189   i = 6;
190   RICindication_IEs_t *ies_indmsg = &IE_array[i];
191   ies_indmsg->criticality = Criticality_reject;
192   ies_indmsg->id = ProtocolIE_ID_id_RICindicationMessage;
193   ies_indmsg->value.present = RICindication_IEs__value_PR_RICindicationMessage;
194   RICindicationMessage_t *ricmsg_ie = &ies_indmsg->value.choice.RICindicationMessage;
195   ricmsg_ie->buf = dinput.indication_msg;
196   ricmsg_ie->size = dinput.indication_msg_size;
197
198   return true;
199
200 };
201
202   
203
204
205 bool ric_indication:: get_fields(InitiatingMessage_t * init_msg,  E2APRicIndication &dout)
206 {
207   if (init_msg == 0){
208     error_string = "Invalid reference for E2AP Indication message in get_fields";
209     return false;
210   }
211   
212  
213   for(int edx = 0; edx < init_msg->value.choice.RICindication.protocolIEs.list.count; edx++) {
214     RICindication_IEs_t *memb_ptr = init_msg->value.choice.RICindication.protocolIEs.list.array[edx];
215     
216     switch(memb_ptr->id)
217       {
218       case (ProtocolIE_ID_id_RICindicationHeader):
219         dout.indication_header = memb_ptr->value.choice.RICindicationHeader.buf;
220         dout.indication_header_size = memb_ptr->value.choice.RICindicationHeader.size;
221         break;
222         
223       case (ProtocolIE_ID_id_RICindicationMessage):
224         dout.indication_msg =  memb_ptr->value.choice.RICindicationMessage.buf;
225         dout.indication_msg_size = memb_ptr->value.choice.RICindicationMessage.size;
226         break;
227             
228       case (ProtocolIE_ID_id_RICrequestID):
229         dout.req_id = memb_ptr->value.choice.RICrequestID.ricRequestorID;
230         dout.req_seq_no = memb_ptr->value.choice.RICrequestID.ricRequestSequenceNumber;
231         break;
232         
233       case (ProtocolIE_ID_id_RANfunctionID):
234         dout.func_id = memb_ptr->value.choice.RANfunctionID;
235         break;
236         
237       case (ProtocolIE_ID_id_RICindicationSN):
238         dout.indication_sn = memb_ptr->value.choice.RICindicationSN;
239         break;
240         
241       case (ProtocolIE_ID_id_RICindicationType):
242         dout.indication_type = memb_ptr->value.choice.RICindicationType;
243         break;
244         
245       case (ProtocolIE_ID_id_RICactionID):
246         dout.action_id = memb_ptr->value.choice.RICactionID;
247         break;
248         
249       default:
250         break;
251       }
252     
253   }
254   
255   return true;
256
257 }
258
259 InitiatingMessage_t * ric_indication::get_message(void)  {
260     return initMsg;
261 }