Merge "MAC Clean-up [Issue-ID: ODUHIGH-212]"
[o-du/l2.git] / src / cu_stub / cu_f1ap_msg_hdl.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains F1AP message handler functions */
20 #include "common_def.h"
21 #include "OCTET_STRING.h"
22 #include "BIT_STRING.h"
23 #include "odu_common_codec.h"
24 #include "cu_stub_sctp.h"
25 #include "cu_f1ap_msg_hdl.h"
26
27 /*******************************************************************
28 *
29 * @brief Sends F1 msg over SCTP
30 *
31 * @details
32 *
33 *    Function : SendF1APMsg
34 *
35 *    Functionality: Sends F1 msg over SCTP
36 *
37 * @params[in] Region region
38 *             Pool pool
39 * @return ROK     - success
40 *         RFAILED - failure
41 *
42 * ****************************************************************/
43 S16 SendF1APMsg(Region region, Pool pool)
44 {
45    Buffer *mBuf = NULLP;
46
47    if(SGetMsg(region, pool, &mBuf) == ROK)
48    {
49       if(SAddPstMsgMult((Data *)encBuf, encBufSize, mBuf) == ROK)
50       {
51          SPrntMsg(mBuf, 0,0);
52  
53          if(sctpSend(mBuf) != ROK)
54          {
55             DU_LOG("\nF1AP : SCTP Send failed");
56             SPutMsg(mBuf);
57             RETVALUE(RFAILED);
58          }
59       }
60       else
61       {
62          DU_LOG("\nF1AP : SAddPstMsgMult failed");
63          SPutMsg(mBuf);
64          RETVALUE(RFAILED);
65       }
66       SPutMsg(mBuf);
67    }
68    else
69    {
70       DU_LOG("\nF1AP : Failed to allocate memory");
71       RETVALUE(RFAILED);
72    }
73  
74    RETVALUE(ROK);
75 } /* SendF1APMsg */
76
77 /*******************************************************************
78 *
79 * @brief Builds NRCell ID 
80 *
81 * @details
82 *
83 *    Function : BuildNrCellId
84 *
85 *    Functionality: Building the NR Cell ID
86 *
87 * @params[in] BIT_STRING_t *nrcell
88 * @return ROK     - success
89 *         RFAILED - failure
90 *
91 * ****************************************************************/
92  
93 S16 BuildNrCellId(BIT_STRING_t *nrcell)
94 {
95    U8 tmp;
96    for (tmp = 0 ; tmp < nrcell->size-1; tmp++)
97    {
98       nrcell->buf[tmp] = 0;
99    }
100    nrcell->buf[4]   = 16; 
101    nrcell->bits_unused = 4;
102    nrcell->size = 5 * sizeof(uint8_t);
103    RETVALUE(ROK);
104 }
105
106 /********************************************************************
107  *
108  * @brief Builds and sends the F1SetupResponse
109  *
110  * @details
111  *
112  *    Function : BuildAndSendF1SetupRsp
113  *
114  *    Functionality: Constructs the F1SetupResponse message and sends
115  *                   it back to the DU through SCTP.
116  *
117  * @params[in] void **buf,Buffer to which encoded pattern is written into
118  * @params[in] int *size,size of buffer
119  *
120  * @return ROK     - success
121  *         RFAILED - failure
122  *
123  * ****************************************************************/
124 S16 BuildAndSendF1SetupRsp()
125 {
126    U8    idx,idy;
127    U8    elementCnt,cellCnt;
128    F1AP_PDU_t         *f1apMsg = NULL;
129    F1SetupResponse_t  *f1SetupRsp;
130    GNB_CU_Name_t      *cuName;
131    Cells_to_be_Activated_List_t *cellToActivate;
132         RRC_Version_t      *rrcVer;
133         asn_enc_rval_t     encRetVal; 
134         DU_LOG("\nF1AP : Building F1 Setup Response\n");
135
136    /* Allocate the memory for F1SetupRequest_t */
137    CU_ALLOC(f1apMsg, sizeof(F1AP_PDU_t)); 
138    if(f1apMsg == NULLP)
139    {
140       DU_LOG("\nF1AP : Memory allocation for F1AP-PDU failed");
141       RETVALUE(RFAILED);
142    }
143    f1apMsg->present =  F1AP_PDU_PR_successfulOutcome;
144
145    CU_ALLOC(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
146    if(f1apMsg->choice.successfulOutcome == NULLP)
147    {
148       DU_LOG("\nF1AP : Memory allocation for F1AP-PDU failed");
149       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
150       RETVALUE(RFAILED);  
151    }
152
153    f1apMsg->choice.successfulOutcome->procedureCode = ProcedureCode_id_F1Setup;
154    f1apMsg->choice.successfulOutcome->criticality = Criticality_reject;
155    f1apMsg->choice.successfulOutcome->value.present = \
156          SuccessfulOutcome__value_PR_F1SetupResponse;
157    f1SetupRsp = &f1apMsg->choice.successfulOutcome->value.choice.F1SetupResponse;
158
159    elementCnt = 4;
160    f1SetupRsp->protocolIEs.list.count = elementCnt;
161    f1SetupRsp->protocolIEs.list.size = elementCnt*sizeof(F1SetupResponseIEs_t *);
162
163    CU_ALLOC(f1SetupRsp->protocolIEs.list.array, \
164          elementCnt * sizeof(F1SetupResponseIEs_t *));
165    if(f1SetupRsp->protocolIEs.list.array == NULLP)
166    {
167       DU_LOG("\nF1AP : Memory allocation for F1ResponseIEs failed");
168       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
169       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
170       RETVALUE(RFAILED);
171    }
172
173    for(idx=0; idx<elementCnt; idx++)
174    {
175       CU_ALLOC(f1SetupRsp->protocolIEs.list.array[idx], \
176             sizeof(F1SetupResponseIEs_t)); 
177       if(f1SetupRsp->protocolIEs.list.array[idx] == NULLP)
178       {  
179          CU_FREE(f1SetupRsp->protocolIEs.list.array,\
180                elementCnt * sizeof(F1SetupResponseIEs_t *));
181          CU_FREE(f1apMsg->choice.successfulOutcome, \
182                sizeof(SuccessfulOutcome_t));
183          CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
184          RETVALUE(RFAILED);
185       }    
186    }
187
188    /*TransactionID*/
189    idx = 0;
190    f1SetupRsp->protocolIEs.list.array[idx]->id = ProtocolIE_ID_id_TransactionID;
191    f1SetupRsp->protocolIEs.list.array[idx]->criticality = Criticality_reject;
192    f1SetupRsp->protocolIEs.list.array[idx]->value.present = \
193                                      F1SetupResponseIEs__value_PR_TransactionID;
194    f1SetupRsp->protocolIEs.list.array[idx]->value.choice.TransactionID =\
195                                                                        TRANS_ID;
196
197    /*CU Name*/
198    idx++;
199    f1SetupRsp->protocolIEs.list.array[idx]->id = ProtocolIE_ID_id_gNB_CU_Name;
200    f1SetupRsp->protocolIEs.list.array[idx]->criticality = Criticality_ignore;
201    f1SetupRsp->protocolIEs.list.array[idx]->value.present = \
202                                        F1SetupResponseIEs__value_PR_GNB_CU_Name;
203    cuName = &f1SetupRsp->protocolIEs.list.array[idx]->value.choice.GNB_CU_Name;
204    cuName->size = sizeof(cuCfgParams.cuName);
205
206    CU_ALLOC(cuName->buf, sizeof(cuName->size)); 
207    if(cuName->buf == NULLP)
208       {
209          for(idy=0; idy<elementCnt; idy++)
210          {
211             CU_FREE(f1SetupRsp->protocolIEs.list.array[idy],\
212                   sizeof(F1SetupResponseIEs_t));
213          }
214          CU_FREE(f1SetupRsp->protocolIEs.list.array,\
215                elementCnt * sizeof(F1SetupResponseIEs_t *));
216          CU_FREE(f1apMsg->choice.successfulOutcome,\
217                sizeof(SuccessfulOutcome_t));
218          CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
219          RETVALUE(RFAILED);
220       }
221    strcpy((char*)cuName->buf, (char*)cuCfgParams.cuName);
222  
223    /*Cells to be activated list*/
224    idx++;
225    f1SetupRsp->protocolIEs.list.array[idx]->id = \
226                               ProtocolIE_ID_id_Cells_to_be_Activated_List ;
227    f1SetupRsp->protocolIEs.list.array[idx]->criticality = Criticality_reject;
228    f1SetupRsp->protocolIEs.list.array[idx]->value.present = \
229                      F1SetupResponseIEs__value_PR_Cells_to_be_Activated_List;
230    cellToActivate = &f1SetupRsp->protocolIEs.list.array[idx]->value.choice.\
231                     Cells_to_be_Activated_List;
232    cellCnt=1;
233    cellToActivate->list.count = cellCnt;
234    cellToActivate->list.size = \
235                cellCnt*sizeof(struct Cells_to_be_Activated_List_ItemIEs  *);
236    CU_ALLOC(cellToActivate->list.array,\
237          sizeof(struct Cells_to_be_Activated_List_ItemIEs  *));
238    if(cellToActivate->list.array == NULLP)
239    {
240       CU_FREE(cuName->buf, sizeof(cuName->size));
241       for(idy=0; idy<elementCnt; idy++)
242       {
243          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy],\
244                sizeof(F1SetupResponseIEs_t));
245       }
246       CU_FREE(f1SetupRsp->protocolIEs.list.array,\
247             elementCnt * sizeof(F1SetupResponseIEs_t *));
248       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
249       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
250       RETVALUE(RFAILED);
251    }
252    for(idy=0; idy<cellCnt; idy++)
253    {
254       CU_ALLOC(cellToActivate->list.array[idy],\
255             sizeof(struct Cells_to_be_Activated_List_ItemIEs ));
256       if(cellToActivate->list.array[idy] == NULLP)
257       {
258          CU_FREE(cellToActivate->list.array,\
259                sizeof(struct Cells_to_be_Activated_List_ItemIEs  *));
260          CU_FREE(cuName->buf, sizeof(cuName->size));
261          for(idy=0; idy<elementCnt; idy++)
262          {
263             CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
264                   sizeof(F1SetupResponseIEs_t));
265          }
266          CU_FREE(f1SetupRsp->protocolIEs.list.array, \
267                elementCnt * sizeof(F1SetupResponseIEs_t *));
268          CU_FREE(f1apMsg->choice.successfulOutcome, \
269                sizeof(SuccessfulOutcome_t));
270          CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
271          RETVALUE(RFAILED);
272       }
273    }
274    cellToActivate->list.array[0]->id = \
275                               ProtocolIE_ID_id_Cells_to_be_Activated_List_Item;
276    cellToActivate->list.array[0]->criticality = Criticality_ignore;
277    cellToActivate->list.array[0]->value.present = \
278        Cells_to_be_Activated_List_ItemIEs__value_PR_Cells_to_be_Activated_List_Item;
279    cellToActivate->list.array[0]->value.choice.Cells_to_be_Activated_List_Item.\
280       nRCGI.pLMN_Identity.size = 3*sizeof(uint8_t);
281    CU_ALLOC(cellToActivate->list.array[0]->\
282          value.choice.Cells_to_be_Activated_List_Item.nRCGI.pLMN_Identity.buf,\
283          3*sizeof(uint8_t));
284    if(cellToActivate->list.array[0]->value.choice.\
285          Cells_to_be_Activated_List_Item.nRCGI.pLMN_Identity.buf == NULLP)
286    {
287
288       for(idy=0; idy<cellCnt; idy++)
289       {
290          CU_FREE(cellToActivate->list.array[idy],\
291                sizeof(struct Cells_to_be_Activated_List_ItemIEs ));
292       }
293
294       CU_FREE(cellToActivate->list.array,\
295             sizeof(struct Cells_to_be_Activated_List_ItemIEs  *));
296       CU_FREE(cuName->buf, sizeof(cuName->size));
297       for(idy=0; idy<elementCnt; idy++)
298       {
299          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
300                sizeof(F1SetupResponseIEs_t));
301       }
302       CU_FREE(f1SetupRsp->protocolIEs.list.array, \
303             elementCnt * sizeof(F1SetupResponseIEs_t *));
304       CU_FREE(f1apMsg->choice.successfulOutcome, \
305             sizeof(SuccessfulOutcome_t));
306       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
307       RETVALUE(RFAILED);
308    }
309     buildPlmnId(cuCfgParams.plmn , &cellToActivate->list.array[0]->value.choice.\
310          Cells_to_be_Activated_List_Item.nRCGI.pLMN_Identity);
311    cellToActivate->list.array[0]->value.choice.Cells_to_be_Activated_List_Item.\
312       nRCGI.nRCellIdentity.size = 5;
313    CU_ALLOC(cellToActivate->list.array[0]->value.choice.\
314          Cells_to_be_Activated_List_Item.nRCGI.nRCellIdentity.buf,\
315          5*sizeof(uint8_t));
316    if(cellToActivate->list.array[0]->value.choice.\
317        Cells_to_be_Activated_List_Item.nRCGI.nRCellIdentity.buf == NULLP)
318    {
319       CU_FREE(cellToActivate->list.array[0]->\
320           value.choice.Cells_to_be_Activated_List_Item.nRCGI.pLMN_Identity.buf,\
321           3*sizeof(uint8_t));
322       for(idy=0; idy<cellCnt; idy++)
323       {
324          CU_FREE(cellToActivate->list.array[idy],\
325                sizeof(struct Cells_to_be_Activated_List_ItemIEs ));
326       }
327
328       CU_FREE(cellToActivate->list.array,\
329             sizeof(struct Cells_to_be_Activated_List_ItemIEs  *));
330       CU_FREE(cuName->buf, sizeof(cuName->size));
331       for(idy=0; idy<elementCnt; idy++)
332       {
333          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
334                sizeof(F1SetupResponseIEs_t));
335       }
336       CU_FREE(f1SetupRsp->protocolIEs.list.array, \
337             elementCnt * sizeof(F1SetupResponseIEs_t *));
338       CU_FREE(f1apMsg->choice.successfulOutcome, \
339             sizeof(SuccessfulOutcome_t));
340       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
341       RETVALUE(RFAILED);
342    }
343    BuildNrCellId(&(cellToActivate->list.array[0]->value.choice.Cells_to_be_Activated_List_Item.nRCGI.nRCellIdentity));
344    /* RRC Version */
345    idx++;
346    f1SetupRsp->protocolIEs.list.array[idx]->id = \
347                                         ProtocolIE_ID_id_GNB_CU_RRC_Version;
348    f1SetupRsp->protocolIEs.list.array[idx]->criticality = Criticality_reject;
349    f1SetupRsp->protocolIEs.list.array[idx]->value.present = \
350                                     F1SetupResponseIEs__value_PR_RRC_Version;
351    rrcVer = &f1SetupRsp->protocolIEs.list.array[idx]->value.choice.RRC_Version;
352    rrcVer->latest_RRC_Version.size = RRC_SIZE; 
353
354    CU_ALLOC(rrcVer->latest_RRC_Version.buf, sizeof(U8));
355    if(rrcVer->latest_RRC_Version.buf == NULLP)
356    {  
357       CU_FREE(cuName->buf, sizeof(cuName->size));
358       for(idy=0; idy<elementCnt; idx++)
359       {
360          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
361                sizeof(F1SetupResponseIEs_t));
362       } 
363       CU_FREE(f1SetupRsp->protocolIEs.list.array,\
364             elementCnt * sizeof(F1SetupResponseIEs_t *));
365       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
366       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
367       RETVALUE(RFAILED);
368    }
369
370   /* Need to check RRC Version */
371    rrcVer->latest_RRC_Version.buf[0] = cuCfgParams.rrcVersion.rrcVer; 
372    rrcVer->latest_RRC_Version.bits_unused = 5; //TODO: pick from cuCfgParam. If not present, add it
373    CU_ALLOC(rrcVer->iE_Extensions,sizeof(ProtocolExtensionContainer_4624P81_t));
374    if(rrcVer->iE_Extensions == NULLP)
375    {
376       CU_FREE(rrcVer->latest_RRC_Version.buf, sizeof(U8));
377       CU_FREE(cuName->buf, sizeof(cuName->size));
378       for(idy=0; idy<elementCnt; idy++)
379       {
380          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
381                sizeof(F1SetupResponseIEs_t));
382       } 
383       CU_FREE(f1SetupRsp->protocolIEs.list.array,\
384             elementCnt * sizeof(F1SetupResponseIEs_t *));
385       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
386       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
387       RETVALUE(RFAILED);
388    }
389    rrcVer->iE_Extensions->list.count = 1;
390    rrcVer->iE_Extensions->list.size = sizeof(struct RRC_Version_ExtIEs *);
391    CU_ALLOC(rrcVer->iE_Extensions->list.array,\
392          sizeof(struct RRC_Version_ExtIEs *));
393    if(rrcVer->iE_Extensions->list.array == NULLP)
394    {
395       CU_FREE(rrcVer->iE_Extensions,\
396             sizeof(ProtocolExtensionContainer_4624P81_t));
397       CU_FREE(rrcVer->latest_RRC_Version.buf, sizeof(U8));
398       CU_FREE(cuName->buf, sizeof(cuName->size));
399       for(idy=0; idy<elementCnt; idy++)
400       {
401          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
402                sizeof(F1SetupResponseIEs_t));
403       } 
404       CU_FREE(f1SetupRsp->protocolIEs.list.array,\
405             elementCnt * sizeof(F1SetupResponseIEs_t *));
406       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
407       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
408       RETVALUE(RFAILED);
409    }
410    CU_ALLOC(rrcVer->iE_Extensions->list.array[0],\
411          sizeof(struct RRC_Version_ExtIEs));
412    if(rrcVer->iE_Extensions->list.array[0] == NULLP)
413    {
414       CU_FREE(rrcVer->iE_Extensions->list.array,\
415             sizeof(struct RRC_Version_ExtIEs *));
416       CU_FREE(rrcVer->iE_Extensions,\
417             sizeof(ProtocolExtensionContainer_4624P81_t));
418       CU_FREE(rrcVer->latest_RRC_Version.buf, sizeof(U8));
419       CU_FREE(cuName->buf, sizeof(cuName->size));
420       for(idy=0; idy<elementCnt; idy++)
421       {
422          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
423                sizeof(F1SetupResponseIEs_t));
424       } 
425       CU_FREE(f1SetupRsp->protocolIEs.list.array,\
426             elementCnt * sizeof(F1SetupResponseIEs_t *));
427       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
428       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
429       RETVALUE(RFAILED);
430    }
431    rrcVer->iE_Extensions->list.array[0]->id = \
432                                 ProtocolIE_ID_id_latest_RRC_Version_Enhanced;
433    rrcVer->iE_Extensions->list.array[0]->criticality = Criticality_reject;
434    rrcVer->iE_Extensions->list.array[0]->extensionValue.present = \
435              RRC_Version_ExtIEs__extensionValue_PR_Latest_RRC_Version_Enhanced;
436    rrcVer->iE_Extensions->list.array[0]->extensionValue.choice.\
437       Latest_RRC_Version_Enhanced.size = 3*sizeof(U8);
438    CU_ALLOC(rrcVer->iE_Extensions->list.\
439          array[0]->extensionValue.choice.Latest_RRC_Version_Enhanced.buf,\
440          3*sizeof(U8));
441    if(rrcVer->iE_Extensions->list.\
442       array[0]->extensionValue.choice.Latest_RRC_Version_Enhanced.buf == NULLP)
443    {
444       CU_FREE(rrcVer->iE_Extensions->list.array[0],\
445              sizeof(struct RRC_Version_ExtIEs));
446       CU_FREE(rrcVer->iE_Extensions->list.array,\
447             sizeof(struct RRC_Version_ExtIEs *));
448       CU_FREE(rrcVer->iE_Extensions,\
449             sizeof(ProtocolExtensionContainer_4624P81_t));
450       CU_FREE(rrcVer->latest_RRC_Version.buf, sizeof(U8));
451       CU_FREE(cuName->buf, sizeof(cuName->size));
452       for(idy=0; idy<elementCnt; idy++)
453       {
454          CU_FREE(f1SetupRsp->protocolIEs.list.array[idy], \
455                sizeof(F1SetupResponseIEs_t));
456       } 
457       CU_FREE(f1SetupRsp->protocolIEs.list.array,\
458             elementCnt * sizeof(F1SetupResponseIEs_t *));
459       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
460       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
461       RETVALUE(RFAILED);
462    }
463    rrcVer->iE_Extensions->list.array[0]->extensionValue.choice.\
464       Latest_RRC_Version_Enhanced.buf[0] = 0;
465    rrcVer->iE_Extensions->list.array[0]->extensionValue.choice.\
466       Latest_RRC_Version_Enhanced.buf[1] = 5;
467    rrcVer->iE_Extensions->list.array[0]->extensionValue.choice.\
468       Latest_RRC_Version_Enhanced.buf[2] = 15;
469
470    xer_fprint(stdout, &asn_DEF_F1AP_PDU, f1apMsg);
471
472    /* Encode the F1SetupRequest type as UPER */
473    cmMemset((U8 *)encBuf, 0, ENC_BUF_MAX_LEN);
474    encBufSize = 0;
475    encRetVal = aper_encode(&asn_DEF_F1AP_PDU, 0, f1apMsg, PrepFinalEncBuf, encBuf);
476
477    /* Clean up */
478    CU_FREE(rrcVer->latest_RRC_Version.buf, sizeof(U8));
479    CU_FREE(cuName->buf, sizeof(cuName->size));
480    for(idx=0; idx<elementCnt; idx++)
481    {
482       CU_FREE(f1SetupRsp->protocolIEs.list.array[idx], sizeof(F1SetupResponseIEs_t));
483    }             
484    CU_FREE(f1SetupRsp->protocolIEs.list.array, elementCnt * sizeof(F1SetupResponseIEs_t *));
485    CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
486    CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
487
488    /* Check encode results */
489    if(encRetVal.encoded == ENCODE_FAIL)
490    {
491            DU_LOG("\nF1AP : Could not encode F1SetupResponse structure (at %s)\n",\
492                            encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
493            RETVALUE(RFAILED);   
494    } 
495    else 
496    {
497            DU_LOG("\nF1AP : Created APER encoded buffer for F1SetupResponse\n");
498            for(int i=0; i< encBufSize; i++)
499            {
500                    printf("%x",encBuf[i]);
501            } 
502    }
503
504    /* Sending msg */
505    if(SendF1APMsg(CU_APP_MEM_REG, CU_POOL) != ROK)
506    {
507            DU_LOG("\nF1AP : Sending F1 Setup Response failed");      
508            RETVALUE(RFAILED);
509    }
510
511    RETVALUE(ROK);
512 }/* End of BuildAndSendF1SetupRsp */
513
514 /*******************************************************************
515  *
516  * @brief Builds and sends the DUUpdateAcknowledge
517  *
518  * @details
519  *
520  *    Function : BuildAndSendDUUpdateAck
521  *
522  *    Functionality: Constructs the DU Update Acknowledge message and sends
523  *                   it to the DU through SCTP.
524  *
525  * @params[in] void **buf,Buffer to which encoded pattern is written into
526  * @params[in] int *size,size of buffer
527  *
528  * @return ROK     - success
529  *         RFAILED - failure
530  *
531  * ****************************************************************/
532
533 S16 BuildAndSendDUUpdateAck()
534 {
535    U8   idx;
536    U8   elementCnt;
537    F1AP_PDU_t *f1apMsg = NULL;
538    GNBDUConfigurationUpdateAcknowledge_t *gNBDuCfgAck;
539    asn_enc_rval_t enRetVal; /* Encoder return value */
540
541    DU_LOG("\nF1AP : Building GNB-DU Config Update Ack\n");
542
543    /* Allocate the memory for F1SetupRequest_t */
544    CU_ALLOC(f1apMsg, (Size)sizeof(F1AP_PDU_t));
545    if(f1apMsg == NULLP)
546    {
547       DU_LOG("\nF1AP : Memory allocation for F1AP-PDU failed");
548       RETVALUE(RFAILED);
549    }
550
551    f1apMsg->present =  F1AP_PDU_PR_successfulOutcome;
552
553    CU_ALLOC(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
554    if(f1apMsg->choice.successfulOutcome == NULLP)
555    {
556       DU_LOG("\nF1AP : Memory allocation for F1AP-PDU failed");
557       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
558       RETVALUE(RFAILED);
559    }
560
561    f1apMsg->choice.successfulOutcome->procedureCode = ProcedureCode_id_gNBDUConfigurationUpdate;
562    f1apMsg->choice.successfulOutcome->criticality = Criticality_reject;
563    f1apMsg->choice.successfulOutcome->value.present = SuccessfulOutcome__value_PR_GNBDUConfigurationUpdateAcknowledge;
564    gNBDuCfgAck = &f1apMsg->choice.successfulOutcome->value.choice.GNBDUConfigurationUpdateAcknowledge;
565
566    elementCnt = 1;
567    gNBDuCfgAck->protocolIEs.list.count = elementCnt;
568    gNBDuCfgAck->protocolIEs.list.size = elementCnt*sizeof(GNBDUConfigurationUpdateAcknowledgeIEs_t);
569
570    /* Initialize the F1Setup members */
571    CU_ALLOC(gNBDuCfgAck->protocolIEs.list.array, elementCnt * sizeof(GNBDUConfigurationUpdateAcknowledgeIEs_t *));
572    if(gNBDuCfgAck->protocolIEs.list.array == NULLP)
573    {
574       DU_LOG("\nF1AP : Memory allocation for DuUpdateAcknowledgeIEs failed");
575       CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
576       CU_FREE(f1apMsg,(Size)sizeof(F1AP_PDU_t));
577       RETVALUE(RFAILED);
578    }
579
580    for(idx=0; idx<elementCnt; idx++)
581    {
582       CU_ALLOC(gNBDuCfgAck->protocolIEs.list.array[idx], sizeof(GNBDUConfigurationUpdateAcknowledgeIEs_t));
583       if(gNBDuCfgAck->protocolIEs.list.array[idx] == NULLP)
584       {
585          CU_FREE(gNBDuCfgAck->protocolIEs.list.array, elementCnt * sizeof(GNBDUConfigurationUpdateAcknowledgeIEs_t *));
586          CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
587          CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
588          RETVALUE(RFAILED);
589       }
590    }
591
592    /*TransactionID*/ 
593    idx = 0;
594    gNBDuCfgAck->protocolIEs.list.array[idx]->id = ProtocolIE_ID_id_TransactionID ;
595    gNBDuCfgAck->protocolIEs.list.array[idx]->criticality = Criticality_reject;
596    gNBDuCfgAck->protocolIEs.list.array[idx]->value.present = GNBDUConfigurationUpdateAcknowledgeIEs__value_PR_TransactionID;
597    gNBDuCfgAck->protocolIEs.list.array[idx]->value.choice.TransactionID = TRANS_ID;
598
599    xer_fprint(stdout, &asn_DEF_F1AP_PDU, f1apMsg);
600  
601    /* Encode the F1SetupRequest type as UPER */
602    cmMemset((U8 *)encBuf, 0, ENC_BUF_MAX_LEN);
603    encBufSize = 0;
604    enRetVal = aper_encode(&asn_DEF_F1AP_PDU, 0, f1apMsg, PrepFinalEncBuf, encBuf);
605
606    /* Clean up */
607    for(idx=0; idx<elementCnt; idx++)
608    {
609       CU_FREE(gNBDuCfgAck->protocolIEs.list.array[idx], sizeof(GNBDUConfigurationUpdateAcknowledgeIEs_t));
610    }
611    CU_FREE(gNBDuCfgAck->protocolIEs.list.array, elementCnt * sizeof(GNBDUConfigurationUpdateAcknowledgeIEs_t *));
612    CU_FREE(f1apMsg->choice.successfulOutcome, sizeof(SuccessfulOutcome_t));
613    CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
614
615    /* Checking encode results */
616    if(enRetVal.encoded == ENCODE_FAIL) 
617    {
618       DU_LOG("\nF1AP : Could not encode DUConfigUpdateAcknowledge structure (at %s)",enRetVal.failed_type ? enRetVal.failed_type->name : "unknown");
619       RETVALUE(RFAILED); 
620    } 
621    else 
622    {
623     DU_LOG("\nF1AP : Created APER encoded buffer for DuConfigUpdateAcknowledge\n");  
624       for(int i=0; i< encBufSize; i++)
625       {
626          printf("%x",encBuf[i]);
627       } 
628    }
629
630    /* Sending msg */
631    if(SendF1APMsg(CU_APP_MEM_REG, CU_POOL) != ROK)
632    {
633       DU_LOG("\nF1AP : Sending GNB-DU Config Update Ack failed");
634       RETVALUE(RFAILED);
635    }
636
637    RETVALUE(ROK);
638
639 }/* End of BuildAndSendDUUpdateAck*/
640
641 /*******************************************************************
642  *
643  * @brief Fills Radio Bearer Config 
644  *
645  * @details
646  *
647  *    Function : fillRadioBearerConfig
648  *
649  *    Functionality: Fills Radio Bearer Config
650  *
651  * @params[in] SRB_ToAddModList *
652  *
653  * @return ROK     - success
654  *         RFAILED - failure
655  *
656  * ****************************************************************/
657 uint8_t fillRadioBearerConfig(SRB_ToAddModList_t *bearerCfg)
658 {
659    uint8_t elementCnt;
660    uint8_t idx;
661    uint8_t ied;
662    if(bearerCfg != NULLP)
663    {
664       elementCnt = 1;
665       bearerCfg->list.count = elementCnt;
666       bearerCfg->list.size =\
667            elementCnt * sizeof(SRB_ToAddMod_t *);
668       CU_ALLOC(bearerCfg->list.array, bearerCfg->list.size);
669       if(bearerCfg->list.array != NULLP)
670       {
671          for(idx = 0; idx < elementCnt; idx++)
672          {
673             CU_ALLOC(bearerCfg->list.array[idx], sizeof(SRB_ToAddMod_t));
674             if(bearerCfg->list.array[idx] == NULLP)
675             {
676                for(ied = 0; ied < idx; ied++)
677                {
678                   CU_FREE(bearerCfg->list.array[idx], sizeof(SRB_ToAddMod_t));
679                }
680                CU_FREE(bearerCfg->list.array, bearerCfg->list.size);
681                return RFAILED;
682             }
683          }
684       }
685                 else
686                 {
687                    return RFAILED;
688                 }
689       idx = 0;
690       bearerCfg->list.array[idx]->srb_Identity = SRB1;
691    }
692    return ROK;
693 }
694
695 /*******************************************************************
696  *
697  * @brief Fills Master CellGroup Info 
698  *
699  * @details
700  *
701  *    Function : fillMasterCellGroup
702  *
703  *    Functionality: Fills Master Cell Group IE
704  *
705  * @params[in] RRCSetup_IEs_t *
706  *
707  * @return ROK     - success
708  *         RFAILED - failure
709  *
710  * ****************************************************************/
711
712 uint8_t fillMasterCellGroup(OCTET_STRING_t *masterCellGroup)
713 {
714    uint8_t ret = ROK;
715    masterCellGroup->buf = NULLP;
716         if(f1apMsgDb.duToCuContainer.buf)
717         {
718       masterCellGroup->size = f1apMsgDb.duToCuContainer.size;
719       CU_ALLOC(masterCellGroup->buf, masterCellGroup->size);
720       if(masterCellGroup->buf != NULLP)
721       {
722          memcpy(masterCellGroup->buf, f1apMsgDb.duToCuContainer.buf,\
723                           masterCellGroup->size);
724       }
725            else
726            {
727               ret = RFAILED;
728            }
729         }
730         else
731         {
732            ret =  RFAILED;
733    }
734    return ret;
735 }
736
737 /*******************************************************************
738  *
739  * @brief Fills RRC setup IE 
740  *
741  * @details
742  *
743  *    Function : fillRRCSetupIE
744  *
745  *    Functionality: Fills RRC Setup IE
746  *
747  * @params[in] RRCSetup_IEs_t *
748  *
749  * @return ROK     - success
750  *         RFAILED - failure
751  *
752  * ****************************************************************/
753
754 uint8_t fillRRCSetupIE(RRCSetup_IEs_t *rrcSetupIE)
755 {
756    uint8_t ret = ROK;
757    if(rrcSetupIE)
758    {
759       CU_ALLOC(rrcSetupIE->radioBearerConfig.srb_ToAddModList, sizeof(SRB_ToAddModList_t));
760                 if(rrcSetupIE->radioBearerConfig.srb_ToAddModList != NULLP)
761                 {
762          ret = fillRadioBearerConfig(rrcSetupIE->radioBearerConfig.srb_ToAddModList);
763            }            
764       if(!ret)
765       {
766          ret = fillMasterCellGroup(&rrcSetupIE->masterCellGroup);
767       }
768                 else
769                 {
770                    CU_FREE(rrcSetupIE->radioBearerConfig.srb_ToAddModList, sizeof(SRB_ToAddModList_t));
771                         ret = RFAILED;
772            }
773    }
774    return ret;
775 }
776 /*******************************************************************
777  *
778  * @brief Builds RRC Container IE required for DLRRCMessageTransfer
779  *
780  * @details
781  *
782  *    Function : BuildDLRRCContainer
783  *
784  *    Functionality: Builds RRC Container IE required for 
785  *                   DLRRCMessageTransfer
786  *
787  * @params[in] 
788  *
789  * @return ROK     - success
790  *         RFAILED - failure
791  *
792  * ****************************************************************/
793
794 uint8_t BuildDLRRCContainer(RRCContainer_t *rrcContainer)
795 {
796    uint8_t ret = ROK;
797         uint16_t idx2;
798    DL_CCCH_Message_t dl_CCCH_Msg;
799    asn_enc_rval_t        encRetVal;
800
801    if(rrcContainer != NULLP)
802    {
803       dl_CCCH_Msg.message.present = DL_CCCH_MessageType_PR_c1;
804
805       CU_ALLOC(dl_CCCH_Msg.message.choice.c1 , sizeof(DL_CCCH_MessageType_t));
806       if(dl_CCCH_Msg.message.choice.c1 != NULLP)
807       {
808          dl_CCCH_Msg.message.choice.c1->present = DL_CCCH_MessageType__c1_PR_rrcSetup;
809          CU_ALLOC(dl_CCCH_Msg.message.choice.c1->choice.rrcSetup, sizeof(RRCSetup_t));
810          if(dl_CCCH_Msg.message.choice.c1->choice.rrcSetup != NULLP)
811          {
812             dl_CCCH_Msg.message.choice.c1->choice.rrcSetup->rrc_TransactionIdentifier = 0;
813             dl_CCCH_Msg.message.choice.c1->choice.rrcSetup->criticalExtensions.\
814               present = RRCSetup__criticalExtensions_PR_rrcSetup;
815                                 /* Fill RRC Setup IE */
816             CU_ALLOC(dl_CCCH_Msg.message.choice.c1->choice.rrcSetup->\
817               criticalExtensions.choice.rrcSetup, sizeof(RRCSetup_IEs_t));
818             if(dl_CCCH_Msg.message.choice.c1->choice.rrcSetup->\
819                 criticalExtensions.choice.rrcSetup != NULLP)
820             {
821                ret = fillRRCSetupIE(dl_CCCH_Msg.message.choice.c1->choice.rrcSetup->\
822                criticalExtensions.choice.rrcSetup);
823
824                if(!ret)
825                                         {
826                   /* encode DL-CCCH message into RRC Container */
827                   xer_fprint(stdout, &asn_DEF_DL_CCCH_MessageType, &dl_CCCH_Msg);
828                   cmMemset((U8 *)encBuf, 0, ENC_BUF_MAX_LEN);
829                   encBufSize = 0;
830                   encRetVal = aper_encode(&asn_DEF_DL_CCCH_MessageType, 0, &dl_CCCH_Msg, PrepFinalEncBuf, encBuf);
831                   /* Encode results */
832                   if(encRetVal.encoded == ENCODE_FAIL)
833                   {
834                      DU_LOG( "\n F1AP : Could not encode RRCContainer (at %s)\n",\
835                           encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
836                           return RFAILED;
837                   }
838                   else
839                   {
840                      DU_LOG("\n F1AP : Created APER encoded buffer for RRCContainer\n");
841                         for(int i = 0; i< encBufSize; i++)
842                         {
843                            printf("%x",encBuf[i]);
844                         }
845                      rrcContainer->size = encBufSize;
846                      CU_ALLOC(rrcContainer->buf, rrcContainer->size);
847                      if(rrcContainer->buf != NULLP)
848                      {
849                         memset(rrcContainer->buf, 0, encBufSize);
850                              for(idx2 = 0; idx2 < encBufSize; idx2++)
851                              {
852                                 rrcContainer->buf[idx2] =       encBuf[idx2];
853                         }
854                      }
855                                                 }
856                                         }
857                                         else
858                                         {
859                                            ret = RFAILED;
860                                         }
861             }
862                                 else
863                                 {
864                     DU_LOG("\nF1AP: Memory Alloc failed for RRC Setup Msg");
865                ret = RFAILED;
866                                 }
867          }
868                         else
869                         {
870                   DU_LOG("\nF1AP: Memory Alloc failed for RRC Msg");
871              ret = RFAILED;
872                         }
873       }
874                 else
875                 {
876               DU_LOG("\nF1AP: Memory Alloc failed for DL Ccch Msg choice");
877          ret = RFAILED;
878                 }
879    }
880         else
881         {
882            DU_LOG("\nF1AP: Memory Alloc failed for DlCcch Msg");
883       ret = RFAILED;
884         }
885    return ret;
886 }
887
888 /*******************************************************************
889  *
890  * @brief Builds and sends the DLRRCMessageTransfer 
891  *
892  * @details
893  *
894  *    Function : BuildAndSendDLRRCMessageTransfer
895  *
896  *    Functionality: Constructs the DL RRC Message Transfer and sends
897  *                   it to the CU through SCTP.
898  *
899  * @params[in] 
900  *
901  * @return ROK     - success
902  *         RFAILED - failure
903  *
904  * ****************************************************************/
905 S16 BuildAndSendDLRRCMessageTransfer()
906 {
907    uint8_t ret = ROK ;
908    uint8_t   elementCnt = 0;
909    uint8_t  ieId;
910    uint8_t  idx;
911         uint16_t idx2;
912    F1AP_PDU_t  *f1apMsg = NULLP;
913    DLRRCMessageTransfer_t  *dlRRCMsg = NULLP;
914    asn_enc_rval_t   encRetVal;        /* Encoder return value */
915
916    DU_LOG("\n F1AP : Building DL RRC Message Transfer Message\n");
917
918    CU_ALLOC(f1apMsg, sizeof(F1AP_PDU_t));
919    if(f1apMsg == NULLP)
920    {
921       DU_LOG(" F1AP : Memory allocation for F1AP-PDU failed");
922       return RFAILED;
923    }
924
925    f1apMsg->present = F1AP_PDU_PR_initiatingMessage;
926    CU_ALLOC(f1apMsg->choice.initiatingMessage,
927       sizeof(InitiatingMessage_t));
928    if(f1apMsg->choice.initiatingMessage == NULLP)
929    {
930       DU_LOG(" F1AP : Memory allocation for  F1AP-PDU failed");
931       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
932       return RFAILED;
933    }
934
935    f1apMsg->choice.initiatingMessage->procedureCode = ProcedureCode_id_DLRRCMessageTransfer;
936    f1apMsg->choice.initiatingMessage->criticality = Criticality_ignore;
937    f1apMsg->choice.initiatingMessage->value.present = InitiatingMessage__value_PR_DLRRCMessageTransfer;
938    dlRRCMsg = &f1apMsg->choice.initiatingMessage->value.choice.DLRRCMessageTransfer;
939
940    elementCnt = 4;
941    dlRRCMsg->protocolIEs.list.count = elementCnt;
942    dlRRCMsg->protocolIEs.list.size = elementCnt * sizeof(DLRRCMessageTransferIEs_t *);
943
944    /* Initialize the F1Setup members */
945    CU_ALLOC(dlRRCMsg->protocolIEs.list.array, dlRRCMsg->protocolIEs.list.size);
946    if(dlRRCMsg->protocolIEs.list.array == NULLP)
947    {
948       DU_LOG(" F1AP : Memory allocation for DL RRC MessageTransferIEs failed");
949       CU_FREE(f1apMsg->choice.initiatingMessage, sizeof(InitiatingMessage_t));
950       CU_FREE(f1apMsg, sizeof(F1AP_PDU_t));
951       return RFAILED;
952    }
953
954    for(idx=0; idx<elementCnt; idx++)
955    {
956       CU_ALLOC(dlRRCMsg->protocolIEs.list.array[idx], sizeof(DLRRCMessageTransferIEs_t));
957       if(dlRRCMsg->protocolIEs.list.array[idx] == NULLP)
958       {
959          for(ieId=0; ieId<idx; ieId++)
960          {
961              CU_FREE(dlRRCMsg->protocolIEs.list.array[ieId],\
962                        sizeof(DLRRCMessageTransferIEs_t));
963          }
964          CU_FREE(dlRRCMsg->protocolIEs.list.array,\
965            dlRRCMsg->protocolIEs.list.size);
966          CU_FREE(f1apMsg->choice.initiatingMessage,\
967            sizeof(InitiatingMessage_t));
968          CU_FREE(f1apMsg,sizeof(F1AP_PDU_t));
969          return RFAILED;
970       }
971    }
972
973    /* GNB CU UE F1AP ID */
974    idx = 0;
975    dlRRCMsg->protocolIEs.list.array[idx]->id  = ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID;
976    dlRRCMsg->protocolIEs.list.array[idx]->criticality  =   Criticality_reject;
977    dlRRCMsg->protocolIEs.list.array[idx]->value.present = \
978                          DLRRCMessageTransferIEs__value_PR_GNB_CU_UE_F1AP_ID;
979    dlRRCMsg->protocolIEs.list.array[idx]->value.choice.GNB_CU_UE_F1AP_ID = CU_ID;
980  
981    /* GNB DU UE F1AP ID */
982    idx++;
983    dlRRCMsg->protocolIEs.list.array[idx]->id  = \
984                                    ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID;
985    dlRRCMsg->protocolIEs.list.array[idx]->criticality  =   Criticality_reject;
986    dlRRCMsg->protocolIEs.list.array[idx]->value.present = \
987                          DLRRCMessageTransferIEs__value_PR_GNB_DU_UE_F1AP_ID;
988    dlRRCMsg->protocolIEs.list.array[idx]->value.choice.GNB_DU_UE_F1AP_ID = DU_ID;
989  
990    /* SRBID */
991    idx++;
992    dlRRCMsg->protocolIEs.list.array[idx]->id  = ProtocolIE_ID_id_SRBID;
993    dlRRCMsg->protocolIEs.list.array[idx]->criticality  =   Criticality_reject;
994    dlRRCMsg->protocolIEs.list.array[idx]->value.present = \
995                                  DLRRCMessageTransferIEs__value_PR_SRBID;
996    dlRRCMsg->protocolIEs.list.array[idx]->value.choice.SRBID = SRB1;
997          
998    /* RRCContainer */
999    idx++;
1000    dlRRCMsg->protocolIEs.list.array[idx]->id  = ProtocolIE_ID_id_RRCContainer;
1001    dlRRCMsg->protocolIEs.list.array[idx]->criticality   = Criticality_reject;
1002    dlRRCMsg->protocolIEs.list.array[idx]->value.present = \
1003                                      DLRRCMessageTransferIEs__value_PR_RRCContainer;
1004         BuildDLRRCContainer(&dlRRCMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer);
1005  
1006    xer_fprint(stdout, &asn_DEF_F1AP_PDU, f1apMsg);
1007  
1008    /* Encode the F1SetupRequest type as APER */
1009    cmMemset((U8 *)encBuf, 0, ENC_BUF_MAX_LEN);
1010    encBufSize = 0;
1011    encRetVal = aper_encode(&asn_DEF_F1AP_PDU, 0, f1apMsg, PrepFinalEncBuf,\
1012        encBuf);
1013    /* Encode results */
1014    if(encRetVal.encoded == ENCODE_FAIL)
1015    {
1016      DU_LOG( "\n F1AP : Could not encode DL RRC Message Transfer structure (at %s)\n",\
1017          encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
1018      RETVALUE(RFAILED);
1019    }
1020    else
1021    {
1022      DU_LOG("\n F1AP : Created APER encoded buffer for DL RRC Message transfer\n");
1023      for(int i=0; i< encBufSize; i++)
1024      {
1025        printf("%x",encBuf[i]);
1026      }
1027    }
1028  
1029    /* Sending  msg  */
1030    if(SendF1APMsg(CU_APP_MEM_REG,CU_POOL)  !=  ROK)
1031    {
1032      DU_LOG("\n F1AP : Sending  DL RRC Message Transfer Failed");
1033      return RFAILED;
1034    }
1035  
1036    return ROK;
1037 }/* End of BuildAndSendDLRRCMessageTransfer */
1038
1039
1040 /*******************************************************************
1041  *
1042  * @brief Builds and sends the UE Setup Response
1043  *
1044  * @details
1045  *
1046  *    Function : BuildAndSendUESetRsp
1047  *
1048  *    Functionality: Constructs the UE Setup Response and sends
1049  *                   it to the DU through SCTP.
1050  *
1051  * @params[in] 
1052  *
1053  * @return ROK     - success
1054  *         RFAILED - failure
1055  *
1056  * ****************************************************************/
1057 S16 BuildAndSendUESetRsp()
1058 {
1059         S16  ret;
1060         U8   elementCnt;
1061         U8   cellCnt;
1062         U8   ieId;
1063         U8   idx;
1064         U8   drbCnt;
1065         U8   drbId;
1066         F1AP_PDU_t                                                      *f1apMsg = NULL;
1067    UEContextSetupResponse_t                     *ueSetRsp;
1068         asn_enc_rval_t                                                  encRetVal;        /* Encoder return value */
1069
1070         DU_LOG("\n F1AP : Building UE Context Setup Response\n");
1071
1072         CU_ALLOC(f1apMsg, sizeof(F1AP_PDU_t));
1073         if(f1apMsg == NULLP)
1074         {
1075                 DU_LOG(" F1AP : Memory allocation for F1AP-PDU failed");
1076                 RETVALUE(RFAILED);
1077         }
1078
1079         f1apMsg->present = F1AP_PDU_PR_successfulOutcome;
1080         CU_ALLOC(f1apMsg->choice.successfulOutcome,
1081                         sizeof(SuccessfulOutcome_t));
1082         if(f1apMsg->choice.successfulOutcome == NULLP)
1083         {
1084                 DU_LOG(" F1AP : Memory allocation for   F1AP-PDU failed");
1085                 CU_FREE(f1apMsg,sizeof(F1AP_PDU_t));
1086                 RETVALUE(RFAILED);
1087         }
1088
1089         f1apMsg->choice.successfulOutcome->procedureCode = \
1090                                                                                                         ProcedureCode_id_UEContextSetup;
1091         f1apMsg->choice.successfulOutcome->criticality = Criticality_reject;
1092         f1apMsg->choice.successfulOutcome->value.present = \
1093                                    SuccessfulOutcome__value_PR_UEContextSetupResponse;
1094         ueSetRsp =
1095                 &f1apMsg->choice.successfulOutcome->value.choice.UEContextSetupResponse;
1096         elementCnt = 2;
1097         ueSetRsp->protocolIEs.list.count = elementCnt;
1098         ueSetRsp->protocolIEs.list.size = \
1099                                                                         elementCnt * sizeof(UEContextSetupResponse_t *);
1100
1101         /* Initialize the UESetup members */
1102         CU_ALLOC(ueSetRsp->protocolIEs.list.array, \
1103                                 ueSetRsp->protocolIEs.list.size);
1104         if(ueSetRsp->protocolIEs.list.array == NULLP)
1105         {
1106                 DU_LOG(" F1AP : Memory allocation for UE Setup Response failed");
1107                 CU_FREE(f1apMsg->choice.successfulOutcome,
1108                                 sizeof(SuccessfulOutcome_t));
1109                 CU_FREE(f1apMsg,(Size)sizeof(F1AP_PDU_t));
1110                 RETVALUE(RFAILED);
1111         }
1112
1113         for(idx=0; idx<elementCnt; idx++)
1114         {
1115                 CU_ALLOC(ueSetRsp->protocolIEs.list.array[idx],\
1116                                                                                 sizeof(UEContextSetupResponseIEs_t));
1117                 if(ueSetRsp->protocolIEs.list.array[idx] == NULLP)
1118                 {
1119                         for(ieId=0; ieId<idx; ieId++)
1120                         {
1121                                 CU_FREE(ueSetRsp->protocolIEs.list.array[ieId],\
1122                                                 sizeof(UEContextSetupResponseIEs_t));
1123                         }
1124                         CU_FREE(ueSetRsp->protocolIEs.list.array,\
1125                                                  ueSetRsp->protocolIEs.list.size);
1126                         CU_FREE(f1apMsg->choice.successfulOutcome,\
1127                                                                                                 sizeof(SuccessfulOutcome_t));
1128                         CU_FREE(f1apMsg,sizeof(F1AP_PDU_t));
1129                         RETVALUE(RFAILED);
1130                 }
1131         }
1132
1133         idx = 0;
1134
1135         /*GNB CU UE F1AP ID*/
1136         ueSetRsp->protocolIEs.list.array[idx]->id       = \
1137                                                                                  ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID;
1138         ueSetRsp->protocolIEs.list.array[idx]->criticality      =       Criticality_reject;
1139         ueSetRsp->protocolIEs.list.array[idx]->value.present = \
1140                                                 UEContextSetupResponseIEs__value_PR_GNB_CU_UE_F1AP_ID;
1141         ueSetRsp->protocolIEs.list.array[idx]->value.choice.GNB_CU_UE_F1AP_ID = CU_ID;
1142         
1143         /*GNB DU UE F1AP ID*/
1144         idx++;
1145         ueSetRsp->protocolIEs.list.array[idx]->id       = \
1146                                                                                  ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID;
1147         ueSetRsp->protocolIEs.list.array[idx]->criticality      =       Criticality_reject;
1148         ueSetRsp->protocolIEs.list.array[idx]->value.present = \
1149                                                 UEContextSetupResponseIEs__value_PR_GNB_DU_UE_F1AP_ID;
1150         ueSetRsp->protocolIEs.list.array[idx]->value.choice.GNB_DU_UE_F1AP_ID = DU_ID;
1151         
1152
1153         xer_fprint(stdout, &asn_DEF_F1AP_PDU, f1apMsg);
1154
1155         /* Encode the F1SetupRequest type as APER */
1156         cmMemset((U8 *)encBuf, 0, ENC_BUF_MAX_LEN);
1157         encBufSize = 0;
1158         encRetVal = aper_encode(&asn_DEF_F1AP_PDU, 0, f1apMsg, PrepFinalEncBuf,\
1159                         encBuf);
1160         /* Encode results */
1161         if(encRetVal.encoded == ENCODE_FAIL)
1162         {
1163                 DU_LOG( "\n F1AP : Could not encode UE Context Setup Request structure (at %s)\n",\
1164                                 encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
1165                 RETVALUE(RFAILED);
1166         }
1167         else
1168         {
1169                 DU_LOG("\n F1AP : Created APER encoded buffer for UE Context Setup Request\n");
1170                 for(int i=0; i< encBufSize; i++)
1171                 {
1172                         printf("%x",encBuf[i]);
1173                 }
1174         }
1175
1176         /* Sending  msg  */
1177         if(SendF1APMsg(CU_APP_MEM_REG,CU_POOL)  !=      ROK)
1178         {
1179                 DU_LOG("\n F1AP : Sending       UE Context Setup Request Failed");
1180                 RETVALUE(RFAILED);
1181         }
1182
1183         RETVALUE(ROK);
1184 }/* End of BuildAndSendUESetRsp */
1185
1186
1187 uint8_t procInitULRRCMsg(F1AP_PDU_t *f1apMsg)
1188 {
1189    uint8_t idx;
1190    uint8_t ret =ROK;
1191    InitialULRRCMessageTransfer_t *initULRRCMsg = NULLP;
1192    DU_LOG("\n filling the required values in DB in procInitULRRCMsg");
1193
1194    initULRRCMsg = &f1apMsg->choice.initiatingMessage->value.choice.InitialULRRCMessageTransfer;
1195
1196    for(idx=0; idx < initULRRCMsg->protocolIEs.list.count; idx++)
1197    {
1198       switch(initULRRCMsg->protocolIEs.list.array[idx]->id)
1199       {
1200          case ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID:
1201          break;
1202          case ProtocolIE_ID_id_NRCGI:
1203          break;
1204          case ProtocolIE_ID_id_C_RNTI:
1205          break;
1206          case ProtocolIE_ID_id_RRCContainer:
1207          break;
1208          case ProtocolIE_ID_id_DUtoCURRCContainer:
1209                            {
1210                                    if((initULRRCMsg->protocolIEs.list.array[idx]->value.choice\
1211                                            .DUtoCURRCContainer.size > 0) && (initULRRCMsg->protocolIEs\
1212                                                 .list.array[idx]->value.choice.DUtoCURRCContainer.buf != NULLP))
1213                {
1214                   DU_LOG("\n Received Du to Cu RRC Container ");
1215                   f1apMsgDb.duToCuContainer.size = initULRRCMsg->protocolIEs\
1216                    .list.array[idx]->value.choice.DUtoCURRCContainer.size;
1217                   CU_ALLOC(f1apMsgDb.duToCuContainer.buf, \
1218                                                  f1apMsgDb.duToCuContainer.size);
1219                   if(f1apMsgDb.duToCuContainer.buf != NULLP)
1220                   { 
1221                      memcpy(f1apMsgDb.duToCuContainer.buf, initULRRCMsg->protocolIEs\
1222                       .list.array[idx]->value.choice.DUtoCURRCContainer.buf, f1apMsgDb\
1223                       .duToCuContainer.size);
1224                   }
1225                }
1226                                    else
1227                                    {
1228                   DU_LOG("\n Failed to receive Du to Cu RRC Container ");
1229                                         ret = RFAILED;
1230                               }
1231                break;
1232                            }
1233          default:
1234             DU_LOG("\n Invalid Event %ld", initULRRCMsg->protocolIEs.list.array[idx]->id);
1235             break;
1236       }
1237    }
1238         if(!ret)
1239            ret = BuildAndSendDLRRCMessageTransfer();
1240    return ret;
1241 }
1242 /*******************************************************************
1243 *
1244 * @brief Handles received F1AP message and sends back response  
1245 *
1246 * @details
1247 *
1248 *    Function : F1APMsgHdlr
1249 *
1250 *    Functionality:
1251 *         - Decodes received F1AP control message
1252 *         - Prepares response message, encodes and sends to SCTP
1253 *
1254 * @params[in] 
1255 * @return ROK     - success
1256 *         RFAILED - failure
1257 *
1258 * ****************************************************************/
1259 void F1APMsgHdlr(Buffer *mBuf)
1260 {
1261    int i;
1262    char *recvBuf;
1263    MsgLen copyCnt;
1264    MsgLen recvBufLen;
1265    F1AP_PDU_t *f1apMsg = NULLP;
1266    asn_dec_rval_t rval; /* Decoder return value */
1267    F1AP_PDU_t f1apasnmsg ;
1268  
1269    DU_LOG("\nF1AP : Received F1AP message buffer");
1270    SPrntMsg(mBuf, 0,0);
1271  
1272    /* Copy mBuf into char array to decode it */
1273    SFndLenMsg(mBuf, &recvBufLen);
1274    if(SGetSBuf(DFLT_REGION, DFLT_POOL, (Data **)&recvBuf, (Size)recvBufLen) != ROK)
1275    {
1276       DU_LOG("\nF1AP : Memory allocation failed");
1277       return;
1278    }
1279    if(SCpyMsgFix(mBuf, 0, recvBufLen, (Data *)recvBuf, &copyCnt) != ROK)
1280    {
1281       DU_LOG("\nF1AP : Failed while copying %d", copyCnt);
1282       return;
1283    }
1284
1285    printf("\nF1AP : Received flat buffer to be decoded : ");
1286    for(i=0; i< recvBufLen; i++)
1287    {
1288         printf("%x",recvBuf[i]);
1289    }
1290
1291    /* Decoding flat buffer into F1AP messsage */
1292    f1apMsg = &f1apasnmsg;
1293    memset(f1apMsg, 0, sizeof(F1AP_PDU_t));
1294  
1295    rval = aper_decode(0, &asn_DEF_F1AP_PDU, (void **)&f1apMsg, recvBuf, recvBufLen, 0, 0);
1296    SPutSBuf(DFLT_REGION, DFLT_POOL, (Data *)recvBuf, (Size)recvBufLen);
1297    if(rval.code == RC_FAIL || rval.code == RC_WMORE)
1298    {
1299       DU_LOG("\nF1AP : ASN decode failed");
1300       return;
1301    }
1302    printf("\n");
1303    xer_fprint(stdout, &asn_DEF_F1AP_PDU, f1apMsg);
1304
1305    switch(f1apMsg->present)
1306    {
1307       case F1AP_PDU_PR_initiatingMessage:
1308       {
1309          switch(f1apMsg->choice.initiatingMessage->value.present)
1310          {
1311             case InitiatingMessage__value_PR_F1SetupRequest:
1312             {
1313                DU_LOG("\nF1AP : F1 setup request received");
1314                BuildAndSendF1SetupRsp();
1315                break;
1316             }
1317
1318             case InitiatingMessage__value_PR_GNBDUConfigurationUpdate:
1319             {
1320                DU_LOG("\nF1AP : GNB-DU config update received");
1321                BuildAndSendDUUpdateAck();
1322                break;
1323             }
1324             case InitiatingMessage__value_PR_InitialULRRCMessageTransfer:
1325             {
1326                DU_LOG("\nF1AP : Received InitialULRRCMessageTransfer");
1327                procInitULRRCMsg(f1apMsg);
1328                break;
1329             }
1330             default:
1331             {
1332                DU_LOG("\nF1AP : Invalid type of intiating message [%d]",f1apMsg->choice.initiatingMessage->value.present);
1333                return;
1334             }
1335          }/* End of switch(initiatingMessage) */
1336          break;
1337       }
1338
1339    }/* End of switch(f1apMsg->present) */
1340  
1341 } /* End of F1APMsgHdlr */
1342  
1343 /**********************************************************************
1344   End of file
1345  **********************************************************************/