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