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