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