Merge "Cleanup of message sib1"
[o-du/l2.git] / src / du_app / du_ue_mgr.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  /* This file contains UE management handling functionality for DU APP */
19 #include "du_ue_mgr.h"
20
21 #ifdef EGTP_TEST
22 U32 sduId = 0;
23 #endif
24
25 DuMacDlCcchInd packMacDlCcchIndOpts[] =
26 {
27    packMacDlCcchInd,   /* Loose coupling */
28    MacHdlDlCcchInd,    /* TIght coupling */
29    packMacDlCcchInd    /* Light weight-loose coupling */
30 };
31
32 /******************************************************************
33  *
34  * @brief Send UE configuration to RLC
35  *
36  * @details
37  *
38  *    Function : duSendUeCreateReqToRlc
39  *
40  *    Functionality: Send UeCreateReqToRlc
41  *
42  * @return ROK     - success
43  *         RFAILED - failure
44  *
45  * ****************************************************************/
46 S16 duSendUeCreateReqToRlc()
47 {
48    U8  idx;
49    Pst pst;
50    CkwCfgInfo *ueCfg;
51    
52    DU_SET_ZERO(&ueCfg, sizeof(ueCfg));
53    DU_SET_ZERO(&pst, sizeof(Pst));
54
55    DU_ALLOC(ueCfg, sizeof(CkwCfgInfo));
56
57 #ifdef EGTP_TEST
58    ueCfg->ueId = UE_ID;
59 #endif
60    ueCfg->cellId = NR_CELL_ID;
61    ueCfg->numEnt = 1;
62    
63    for(idx = 0; idx < ueCfg->numEnt; idx++)
64    {
65 #ifdef EGTP_TEST
66       ueCfg->entCfg[idx].rbId           = RB_ID;
67       ueCfg->entCfg[idx].rbType         = CM_LTE_DRB;
68       ueCfg->entCfg[idx].lCh[0].lChId   = LC_ID;
69       ueCfg->entCfg[idx].lCh[0].type    = CM_LTE_LCH_DTCH;
70 #endif
71       ueCfg->entCfg[idx].entMode        = CM_LTE_MODE_UM;
72       ueCfg->entCfg[idx].dir            = CKW_CFG_DIR_BOTH;
73       switch(ueCfg->entCfg[idx].entMode)
74       {
75          case CM_LTE_MODE_TM:
76          {
77             break;
78          }
79
80          case CM_LTE_MODE_UM:
81          {
82             ueCfg->entCfg[idx].m.umInfo.dl.snLen = 1;      /* For 12 bit SN */
83             ueCfg->entCfg[idx].m.umInfo.ul.snLen = 1;      /* For 12 bit SN */
84             ueCfg->entCfg[idx].m.umInfo.ul.reOrdTmr = 10;  /* in msec */
85             break;
86          }
87
88          case CM_LTE_MODE_AM:
89          {
90             break;
91          }
92          
93          default:
94             break;
95       } /* End of switch(entMode) */
96    } /* End of entity configuration for loop */
97
98    /* Fill Pst */
99    pst.selector  = DU_SELECTOR_LWLC;
100    pst.srcEnt    = ENTDUAPP;
101    pst.dstEnt    = ENTKW;
102    pst.dstInst   = RLC_UL_INST;
103    pst.dstProcId = DU_PROC;
104    pst.srcProcId = DU_PROC;
105    pst.region    = duCb.init.region;
106
107    /* Sending to RLC */
108    packUeCreateReq(&pst, ueCfg);
109
110    RETVALUE(ROK); 
111 } /* End of duSendUeCreateReqToRlc */
112
113 /*******************************************************************
114  *
115  * @brief Handles EGTP data from CU 
116  *
117  * @details
118  *
119  *    Function : duHdlEgtpData
120  *
121  *    Functionality: 
122  *      Processes EGTP header and sends data to RLC
123  *
124  * @params[in]  Pointer to EGTP Message 
125  * @return ROK     - success
126  *         RFAILED - failure
127  *
128  * ****************************************************************/
129 S16 duHdlEgtpDlData(EgtpMsg  *egtpMsg)
130 {
131    /* TODO : Extract RbId/UeID/CellID/SduId from database
132       using tunnel id in egtp header */
133    
134    DU_LOG("\nDU_APP : Processing DL data");
135    
136    Pst pst;
137    KwuDatReqInfo datReqInfo;
138
139 #ifdef EGTP_TEST
140    datReqInfo.rlcId.rbId = RB_ID;
141    datReqInfo.rlcId.rbType = CM_LTE_DRB;
142    datReqInfo.rlcId.ueId = UE_ID;
143    datReqInfo.rlcId.cellId = NR_CELL_ID;
144    
145    datReqInfo.sduId = ++sduId;
146    datReqInfo.lcType = CM_LTE_LCH_DTCH;
147 #endif
148    /* Filling pst and Sending to RLC DL */
149    pst.selector  = DU_SELECTOR_LWLC;
150    pst.srcEnt    = ENTDUAPP;
151    pst.dstEnt    = ENTKW;
152    pst.dstInst   = RLC_DL_INST;
153    pst.dstProcId = DU_PROC;
154    pst.srcProcId = DU_PROC;
155    pst.region    = duCb.init.region;
156
157    cmPkKwuDatReq(&pst, &datReqInfo, egtpMsg->msg);
158    return ROK;
159 }
160
161 /*******************************************************************
162  *
163  * @brief Handles UL data and send to CU
164  *
165  * @details
166  *
167  *    Function : duHdlRlcUlData
168  *
169  *    Functionality: 
170  *     Processes UL Data from RLC and sends to CU
171  * 
172  *  @params[in]  Pointer to EGTP Message 
173  *  @return ROK     - success
174  *          RFAILED - failure
175  * 
176  *****************************************************************/
177
178 PUBLIC S16 duHdlRlcUlData(Pst *pst, KwuDatIndInfo* datInd, Buffer *mBuf)
179 {
180    DU_LOG("\nDU_APP : Received UL Data at DU_APP");
181  
182    /* Send UL data to CU via EGTP */
183    duSendEgtpDatInd(mBuf);
184    SPutMsg(mBuf);
185
186    return ROK;
187 }
188
189 /******************************************************************
190 *
191 * @brief Builds and Sends DL CCCH Ind to MAC
192 *
193 * @details
194 *
195 *    Function : duBuildAndSendDlCcchInd 
196 *
197 *    Functionality: Builds and sends DL CCCH Ind Msg to MAC
198 *
199 * @params[in] dlCcchMsg - uint8_t*
200 * @return ROK     - success
201 *         RFAILED - failure
202 *
203 * ****************************************************************/
204 uint8_t duBuildAndSendDlCcchInd(uint8_t *dlCcchMsg, uint16_t crnti, uint16_t cellId, DlCcchMsgType msgType)
205 {
206    Pst pst;
207         uint8_t ret                  = ROK;
208    DlCcchIndInfo *dlCcchIndInfo = NULLP;
209
210    DU_LOG("\nDU APP : Building and Sending DL CCCH Ind to MAC");
211
212         DU_ALLOC_SHRABL_BUF(dlCcchIndInfo, sizeof(DlCcchIndInfo));
213
214    if(!dlCcchIndInfo)
215    {
216                 DU_LOG("\nDU APP : Memory alloc failed while building DL CCCH Ind");
217                 return RFAILED;
218         }
219
220         dlCcchIndInfo->cellId = cellId;
221         dlCcchIndInfo->crnti = crnti;
222         dlCcchIndInfo->msgType = msgType;
223         DU_ALLOC_SHRABL_BUF(dlCcchIndInfo->dlCcchMsg, strlen((const char*)dlCcchMsg));
224         if(!dlCcchIndInfo->dlCcchMsg)
225    {
226                 DU_LOG("\nDU APP : Memory alloc failed while building DL CCCH Ind");
227                 DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, sizeof(DlCcchIndInfo));
228                 return RFAILED;
229         }
230         memcpy(dlCcchIndInfo->dlCcchMsg, dlCcchMsg, strlen((const char*)dlCcchMsg));
231         DU_FREE(dlCcchMsg, strlen((const char*)dlCcchMsg));
232
233         /* Fill Pst */
234         pst.selector  = DU_MAC_LWLC;
235         pst.srcEnt    = ENTDUAPP;
236         pst.dstEnt    = ENTRG;
237         pst.dstInst   = 0;
238         pst.srcInst   = 0;
239         pst.dstProcId = DU_PROC;
240         pst.srcProcId = DU_PROC;
241         pst.region    = DU_APP_MEM_REGION;
242         pst.pool      = DU_POOL;
243         pst.event     = EVENT_MAC_DL_CCCH_IND;
244
245    ret = (*packMacDlCcchIndOpts[pst.selector])(&pst, dlCcchIndInfo);
246         if(ret != ROK)
247         {
248       DU_LOG("\nDU_APP : Failure in sending DL CCCH to MAC");
249                 DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo->dlCcchMsg, strlen((const char*)dlCcchMsg));
250                 DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, sizeof(DlCcchIndInfo));
251       ret = RFAILED; 
252         }
253
254         return ret;
255
256 }
257
258 /******************************************************************
259 *
260 * @brief Processes DL RRC Message Transfer  sent by CU
261 *
262 * @details
263 *
264 *    Function : procDlRrcMsgTrans 
265 *
266 *    Functionality: Processes DL RRC Message Transfer sent by CU
267 *
268 * @params[in] F1AP_PDU_t ASN decoded F1AP message
269 * @return ROK     - success
270 *         RFAILED - failure
271 *
272 * ****************************************************************/
273 uint8_t procDlRrcMsgTrans(F1AP_PDU_t *f1apMsg)
274 {
275         DLRRCMessageTransfer_t *dlRrcMsg = NULLP;
276         uint8_t                *dlCcchMsg = NULLP;
277         uint8_t                srbId, idx, ret;
278         uint16_t               crnti, cellId;
279         uint32_t               gnbCuUeF1apId, gnbDuUeF1apId;
280
281
282         DU_LOG("\nDU_APP : DL RRC message transfer Recevied");
283         dlRrcMsg = &f1apMsg->choice.initiatingMessage->value.choice.DLRRCMessageTransfer;
284
285         ret = ROK;
286
287         for(idx=0; idx<dlRrcMsg->protocolIEs.list.count; idx++)
288         {
289                 switch(dlRrcMsg->protocolIEs.list.array[idx]->id)
290                 {
291                         case ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID:
292                                 {
293                                         gnbCuUeF1apId = dlRrcMsg->protocolIEs.list.array[idx]->value.choice.GNB_CU_UE_F1AP_ID;
294                                         UNUSED(gnbCuUeF1apId); //This is currently not used
295                                         break;
296                                 }
297                         case ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID:
298                                 {
299                                         gnbDuUeF1apId = dlRrcMsg->protocolIEs.list.array[idx]->value.choice.GNB_DU_UE_F1AP_ID;
300                                         break;
301                                 }
302                         case ProtocolIE_ID_id_SRBID:
303                                 {
304                                         srbId = dlRrcMsg->protocolIEs.list.array[idx]->value.choice.SRBID;
305                                         break;
306                                 }
307                         case ProtocolIE_ID_id_RRCContainer:
308                                 {
309                                    DU_ALLOC(dlCcchMsg, dlRrcMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer.size);
310                                         memcpy(dlCcchMsg,
311                                                         dlRrcMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer.buf,
312                                                         dlRrcMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer.size);
313                                         break;
314                                 }
315
316                         default:
317                                 DU_LOG("\nDU_APP : Invalid IE received in DL RRC Msg Transfer:%ld",
318                                                 dlRrcMsg->protocolIEs.list.array[idx]->id);
319                 }
320         }
321    
322    for(idx=0; idx<duCb.numUe; idx++)
323         {
324       if(gnbDuUeF1apId == duCb.ueCcchCtxt[idx].gnbDuUeF1apId)
325                 {
326                    crnti  = duCb.ueCcchCtxt[idx].crnti;
327                         cellId = duCb.ueCcchCtxt[idx].cellId;
328                 }
329         }
330         if(srbId == 0) //RRC connection setup
331         {
332                 ret = duBuildAndSendDlCcchInd(dlCcchMsg, crnti, cellId, RRC_SETUP);
333         }
334         return ret;
335 }
336
337 /******************************************************************
338  *
339  * @brief Generates GNB DU Ue F1AP ID
340  *
341  * @details
342  *
343  *    Function : genGnbDuUeF1apId
344  *
345  *    Functionality: Generates GNB DU Ue F1AP ID
346  *
347  * @params[in] void
348  * @return gnbDuF1apId
349  *
350  * ****************************************************************/
351 uint32_t genGnbDuUeF1apId()
352 {
353         static uint32_t gnbDuUeF1apId = 0;
354
355         return ++gnbDuUeF1apId;
356 }
357 /******************************************************************
358  *
359  * @brief Processes UL CCCH Ind recvd from MAC
360  *
361  * @details
362  *
363  *    Function : duProcUlCcchInd
364  *
365  *    Functionality: Processes UL CCCH Ind recvd from MAC
366  *
367  * @params[in] UlCcchIndInfo *ulCcchIndInfo
368  * @return ROK     - success
369  *         RFAILED - failure
370  *
371  * ****************************************************************/
372 uint8_t duProcUlCcchInd(UlCcchIndInfo *ulCcchIndInfo)
373 {
374
375    uint8_t ret = ROK;
376         uint32_t gnbDuUeF1apId = 0;
377
378         gnbDuUeF1apId = genGnbDuUeF1apId();
379
380         /* Store Ue mapping */
381         duCb.ueCcchCtxt[duCb.numUe].gnbDuUeF1apId = gnbDuUeF1apId;
382         duCb.ueCcchCtxt[duCb.numUe].crnti         = ulCcchIndInfo->crnti;
383         duCb.ueCcchCtxt[duCb.numUe].cellId        = ulCcchIndInfo->cellId;
384
385         duCb.numUe++;
386
387    ret = (BuildAndSendInitialRrcMsgTransfer(gnbDuUeF1apId, ulCcchIndInfo->crnti,
388                                 ulCcchIndInfo->ulCcchMsg));
389         if(ret != ROK)
390         {
391       DU_LOG("\nDU_APP : BuildAndSendInitialRrcMsgTransfer failed");
392         }
393
394    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, RG_POOL, ulCcchIndInfo->ulCcchMsg, strlen((const char*)ulCcchIndInfo->ulCcchMsg));
395    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, RG_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
396
397         return ret;
398
399 }
400
401
402 /**********************************************************************
403          End of file
404 ***********************************************************************/