MAC Clean-up [Issue-ID: ODUHIGH-212]
[o-du/l2.git] / src / 5gnrsch / sch_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 SCH */
19
20 #include "common_def.h"
21 #include "tfu.h"
22 #include "lrg.h"
23
24 #include "tfu.x"
25 #include "lrg.x"
26 #include "du_log.h"
27 #include "du_app_mac_inf.h"
28 #include "mac_sch_interface.h"
29 #include "sch.h"
30 #include "sch_utils.h"
31
32 /* local defines */
33 SchUeCfgRspFunc SchUeCfgRspOpts[] =
34 {
35    packSchUeCfgRsp,      /* LC */
36    MacProcSchUeCfgRsp,   /* TC */
37    packSchUeCfgRsp       /* LWLC */
38 };
39
40
41 /*******************************************************************
42  *
43  * @brief Fill and send UE cfg response to MAC
44  *
45  * @details
46  *
47  *    Function : SchSendUeCfgRspToMac
48  *
49  *    Functionality: Fill and send UE cfg response to MAC
50  *
51  * @params[in] 
52  * @return ROK     - success
53  *         RFAILED - failure
54  *
55  * ****************************************************************/
56 void SchSendUeCfgRspToMac(SchUeCfg *ueCfg, Inst inst,\
57       SchMacRsp result, SchUeCfgRsp *cfgRsp)
58 {
59    Pst rspPst;
60
61    DU_LOG("\nSCH: Sending UE Create response to MAC");
62
63    cfgRsp->cellId = ueCfg->cellId;
64    cfgRsp->crnti = ueCfg->crnti;
65    GET_UE_IDX(ueCfg->crnti, cfgRsp->ueIdx);
66    cfgRsp->rsp = result;   
67
68    /* Filling response post */
69    memset(&rspPst, 0, sizeof(Pst));
70    FILL_PST_SCH_TO_MAC(rspPst, inst);
71    rspPst.event = EVENT_UE_CREATE_RSP_TO_MAC;
72
73    SchUeCfgRspOpts[rspPst.selector](&rspPst, cfgRsp);
74 }
75
76 /*******************************************************************
77  *
78  * @brief Hanles Ue create request from MAC
79  *
80  * @details
81  *
82  *    Function : macSchUeCreateReq
83  *
84  *    Functionality: Hanles Ue create request from MAC
85  *
86  * @params[in] 
87  * @return ROK     - success
88  *         RFAILED - failure
89  *
90  * ****************************************************************/
91 uint8_t macSchUeCreateReq(Pst *pst, SchUeCfg *ueCfg)
92 {
93    uint8_t      idx;
94    uint16_t     ueIdx;
95    SchCellCb    *cellCb;
96    SchUeCb      *ueCb;
97    SchUeCfgRsp  cfgRsp;
98    Inst         inst = pst->dstInst - 1;
99
100    DU_LOG("\nSCH : UE Create Request for CRNTI[%d]", ueCfg->crnti);
101
102    memset(&cfgRsp, 0, sizeof(SchUeCfgRsp));
103
104    if(!ueCfg)
105    {
106       DU_LOG("\nSCH : UE create request failed");
107       return RFAILED;
108    }
109
110    /* Search of cell cb */
111    for(idx = 0; idx < MAX_NUM_CELL; idx++)
112    {
113       cellCb = schCb[inst].cells[idx];
114       if(cellCb->cellId == ueCfg->cellId)
115          break;
116    }
117    if(idx == MAX_NUM_CELL)
118    {
119       DU_LOG("\nSCH : Ue create request failed. Invalid cell id %d", ueCfg->cellId);
120       SchSendUeCfgRspToMac(ueCfg, inst, RSP_NOK, &cfgRsp);
121       return ROK;
122    }
123
124    /* Check if max number of UE configured */
125    if(cellCb->numActvUe > MAX_NUM_UE)
126    {
127       DU_LOG("SCH : Max number of UE [%d] already configured", MAX_NUM_UE);
128       SchSendUeCfgRspToMac(ueCfg, inst, RSP_NOK, &cfgRsp);
129       return ROK;
130    }
131
132    /* Search if UE already configured */
133    GET_UE_IDX(ueCfg->crnti, ueIdx);
134    ueCb = &cellCb->ueCb[ueIdx];
135    if(ueCb)
136    {
137       if((ueCb->crnti == ueCfg->crnti) && (ueCb->state == SCH_UE_STATE_ACTIVE))
138       {
139          DU_LOG("\n SCH : CRNTI %d already configured ", ueCfg->crnti);
140          SchSendUeCfgRspToMac(ueCfg, inst, RSP_OK, &cfgRsp);
141          return ROK;
142       }
143    }
144
145    /* Fill received Ue Configuration in UeCb */
146    memset(ueCb, 0, sizeof(SchUeCb));
147
148    GET_UE_IDX(ueCfg->crnti, ueCb->ueIdx);
149    ueCb->crnti = ueCfg->crnti;
150    memcpy(&ueCb->ueCfg, ueCfg, sizeof(SchUeCfg));
151    ueCb->state = SCH_UE_STATE_ACTIVE;
152    cellCb->numActvUe++;
153
154    SchSendUeCfgRspToMac(ueCfg, inst, RSP_OK, &cfgRsp);
155    return ROK;
156 }
157
158 /**********************************************************************
159   End of file
160  **********************************************************************/