Cu ASN changes for DlRrcMessageTransfer
[o-du/l2.git] / src / 5gnrmac / mac_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 /* header include files -- defines (.h)  */
20 #include <stdbool.h>
21 #include "envopt.h"        /* environment options */
22 #include "envdep.h"        /* environment dependent */
23 #include "envind.h"        /* environment independent */
24 #include "gen.h"           /* general layer */
25 #include "ssi.h"           /* system services */
26 #include "cm5.h"           /* common timers defines */
27 #include "cm_hash.h"       /* common hash list defines */
28 #include "cm_llist.h"      /* common linked list defines */
29 #include "cm_mblk.h"       /* memory management */
30 #include "cm_tkns.h"       /* common tokens */
31 #include "cm_lte.h"        /* common tokens */
32 #include "rgu.h"
33 #include "tfu.h"
34 #include "rg_sch_inf.h"
35 #include "rg_env.h"
36 #include "lrg.h"
37 #include "crg.h"
38 #include "rg.h"
39 #include "du_log.h"
40 #include "lwr_mac.h"
41
42 /* header/extern include files (.x) */
43 #include "gen.x"           /* general layer typedefs */
44 #include "ssi.x"           /* system services typedefs */
45 #include "cm5.x"           /* common timers */
46 #include "cm_hash.x"       /* common hash list */
47 #include "cm_lib.x"        /* common library */
48 #include "cm_llist.x"      /* common linked list */
49 #include "cm_mblk.x"       /* memory management */
50 #include "cm_tkns.x"       /* common tokens */
51 #include "cm_lte.x"        /* common tokens */
52 #include "rgu.x"
53 #include "tfu.x"
54 #include "rg_sch_inf.x"
55 #include "lrg.x"
56 #include "crg.x"
57 #include "rg_prg.x"
58 #include "du_app_mac_inf.h"
59 #include "mac_upr_inf_api.h"
60 #include "rg.x"
61 #include "lwr_mac_fsm.h"
62 #include "mac.h"
63
64 /* This file contains message handling functionality for MAC */
65
66 extern MacCb  macCb;
67
68 extern void sendToLowerMac(uint16_t msgType, uint32_t msgLen, void *msg);
69 uint16_t buildMacPdu(RlcMacData *dlData);
70
71 /* Function pointer for sending crc ind from MAC to SCH */
72 MacSchCrcIndFunc macSchCrcIndOpts[]=
73 {
74    packMacSchCrcInd,
75    macSchCrcInd,
76    packMacSchCrcInd
77 };
78
79 /* Function pointer for sending DL RLC BO Info from MAC to SCH */
80 MacSchDlRlcBoInfoFunc macSchDlRlcBoInfoOpts[]=
81 {
82    packMacSchDlRlcBoInfo,
83    macSchDlRlcBoInfo,
84    packMacSchDlRlcBoInfo
85 };
86
87 /*******************************************************************
88  *
89  * @brief Sends DL BO Info to SCH
90  *
91  * @details
92  *
93  *    Function : sendDlRlcBoInfoMacToSch
94  *
95  *    Functionality:
96  *       Sends DL BO Info to SCH
97  *
98  * @params[in] 
99  * @return ROK     - success
100  *         RFAILED - failure
101  *
102  ****************************************************************/
103 int sendDlRlcBoInfoMacToSch(DlRlcBOInfo *dlBoInfo)
104 {
105    Pst pst;
106  
107    fillMacToSchPst(&pst);
108    pst.event = EVENT_DL_RLC_BO_INFO_TO_SCH;
109  
110    return(*macSchDlRlcBoInfoOpts[pst.selector])(&pst, dlBoInfo);
111 }
112
113 /*******************************************************************
114  *
115  * @brief Sends CRC Indication to SCH
116  *
117  * @details
118  *
119  *    Function : sendCrcIndMacToSch 
120  *
121  *    Functionality:
122  *       Sends CRC Indication to SCH
123  *
124  * @params[in] 
125  * @return ROK     - success
126  *         RFAILED - failure
127  *
128   ****************************************************************/
129 int sendCrcIndMacToSch(CrcIndInfo *crcInd)
130 {
131    Pst pst;
132  
133    fillMacToSchPst(&pst);
134    pst.event = EVENT_CRC_IND_TO_SCH;
135  
136    return(*macSchCrcIndOpts[pst.selector])(&pst, crcInd);
137 }
138  
139 /*******************************************************************
140  *
141  * @brief Processes CRC Indication from PHY
142  *
143  * @details
144  *
145  *    Function : fapiMacCrcInd
146  *
147  *    Functionality:
148  *       Processes CRC Indication from PHY
149  *
150  * @params[in] Post Structure Pointer
151  *             Crc Indication Pointer
152  * @return ROK     - success
153  *         RFAILED - failure
154  *
155  * ****************************************************************/
156 uint16_t fapiMacCrcInd(Pst *pst, CrcInd *crcInd)
157 {
158    CrcIndInfo   crcIndInfo;
159  
160    DU_LOG("\nMAC : Received CRC indication");
161
162    /* Considering one pdu and one preamble */ 
163    crcIndInfo.cellId = macCb.macCell->cellId;;
164    crcIndInfo.crnti = crcInd->crcInfo[0].rnti;
165    crcIndInfo.timingInfo.sfn = crcInd->timingInfo.sfn;
166    crcIndInfo.timingInfo.slot = crcInd->timingInfo.slot;
167    crcIndInfo.numCrcInd = crcInd->crcInfo[0].numCb;
168    crcIndInfo.crcInd[0] = crcInd->crcInfo[0].cbCrcStatus[0];
169  
170    return(sendCrcIndMacToSch(&crcIndInfo));
171 }
172  
173 /*******************************************************************
174  *
175  * @brief Process Rx Data Ind at MAC
176  *
177  * @details
178  *
179  *    Function : fapiMacRxDataInd
180  *
181  *    Functionality:
182  *       Process Rx Data Ind at MAC
183  *
184  * @params[in] Post structure
185  *             Rx Data Indication
186  * @return ROK     - success
187  *         RFAILED - failure
188  *
189  * ****************************************************************/
190 uint16_t fapiMacRxDataInd(Pst *pst, RxDataInd *rxDataInd)
191 {
192    uint16_t pduIdx;
193  
194    DU_LOG("\nMAC : Received Rx Data indication");
195   
196    /* TODO : compare the handle received in RxDataInd with handle send in PUSCH
197          * PDU, which is stored in raCb */
198
199    for(pduIdx = 0; pduIdx < rxDataInd->numPdus; pduIdx++)
200    {
201       unpackRxData(&rxDataInd->pdus[pduIdx]);
202    }
203    return ROK;
204 }
205
206 /*******************************************************************
207  *
208  * @brief Processes DL data from RLC
209  *
210  * @details
211  *
212  *    Function : MacRlcProcDlData 
213  *
214  *    Functionality:
215  *      Processes DL data from RLC
216  *
217  * @params[in] Post structure
218  *             DL data
219  * @return ROK     - success
220  *         RFAILED - failure
221  *
222  * ****************************************************************/
223 uint16_t MacRlcProcDlData(Pst* pst, SpId spId, RlcMacData *dlData)
224 {
225    return ROK;
226 }
227
228 /*******************************************************************
229  *
230  * @brief Processes BO status from RLC
231  *
232  * @details
233  *
234  *    Function : MacRlcProcBOStatus
235  *
236  *    Functionality:
237  *      Processes BO status from RLC
238  *
239  * @params[in] Post structure
240  *             BO status
241  * @return ROK     - success
242  *         RFAILED - failure
243  *
244  * ****************************************************************/
245 uint16_t MacRlcProcBOStatus(Pst* pst, SpId spId, RlcMacBOStatus*      boStatus)
246 {
247    return ROK;
248 }
249
250
251 /*******************************************************************
252  *
253  * @brief Handles cell start reuqest from DU APP
254  *
255  * @details
256  *
257  *    Function : MacHdlCellStartReq
258  *
259  *    Functionality:
260  *      Handles cell start reuqest from DU APP
261  *
262  * @params[in] Post structure pointer
263  *             Cell Start Request info pointer 
264  * @return ROK     - success
265  *         RFAILED - failure
266  *
267  * ****************************************************************/
268 uint16_t MacHdlCellStartReq(Pst *pst, MacCellStartInfo  *cellStartInfo)
269 {
270    DU_LOG("\nMAC : Handling cell start request");
271    sendToLowerMac(START_REQUEST, 0, cellStartInfo);
272
273    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStartInfo, \
274            sizeof(MacCellStartInfo));
275
276    return ROK;
277 }
278
279 /*******************************************************************
280  *
281  * @brief Handles cell stop request from DU APP
282  *
283  * @details
284  *
285  *    Function : MacHdlCellStartReq
286  *
287  *    Functionality:
288  *        Handles cell stop request from DU APP
289  *
290  * @params[in] Post structure pointer
291  *             Mac Cell stop information
292  * @return ROK     - success
293  *         RFAILED - failure
294  *
295  * ****************************************************************/
296 uint16_t MacHdlCellStopReq(Pst *pst, MacCellStopInfo  *cellStopInfo)
297 {
298    #ifdef FAPI
299    DU_LOG("\nMAC : Sending cell stop request to Lower Mac");
300    sendToLowerMac(FAPI_STOP_REQUEST, 0, cellStopInfo);
301    #endif
302
303    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellStopInfo, \
304            sizeof(MacCellStopInfo));
305  
306    return ROK;
307 }
308
309 /*******************************************************************
310  *
311  * @brief Handles DL CCCH Ind from DU APP
312  *
313  * @details
314  *
315  *    Function : MacHdlDlCcchInd 
316  *
317  *    Functionality:
318  *      Handles DL CCCH Ind from DU APP
319  *
320  * @params[in] Post structure pointer
321  *             DL CCCH Ind pointer 
322  * @return ROK     - success
323  *         RFAILED - failure
324  *
325  * ****************************************************************/
326 uint16_t MacHdlDlCcchInd(Pst *pst, DlCcchIndInfo *dlCcchIndInfo)
327 {
328    uint16_t idx;
329    DlRlcBOInfo  dlBoInfo;
330         memset(&dlBoInfo, 0, sizeof(DlRlcBOInfo));
331
332    DU_LOG("\nMAC : Handling DL CCCH IND");
333    
334    /* TODO : Fill DL RLC Buffer status info */
335    dlBoInfo.cellId = dlCcchIndInfo->cellId;
336    dlBoInfo.crnti = dlCcchIndInfo->crnti;
337    dlBoInfo.numLc = 0;
338    
339    if(dlCcchIndInfo->msgType == RRC_SETUP)
340    {
341       dlBoInfo.boInfo[dlBoInfo.numLc].lcId = SRB_ID_0;    // SRB ID 0 for msg4
342       dlBoInfo.boInfo[SRB_ID_0].dataVolume = \
343         dlCcchIndInfo->dlCcchMsgLen;
344       dlBoInfo.numLc++;
345
346       /* storing Msg4 Pdu in raCb */
347       if(macCb.macCell->macRaCb[0].crnti == dlCcchIndInfo->crnti)
348       {
349          macCb.macCell->macRaCb[0].msg4PduLen = dlCcchIndInfo->dlCcchMsgLen;
350          MAC_ALLOC(macCb.macCell->macRaCb[0].msg4Pdu, macCb.macCell->macRaCb[0]\
351          .msg4PduLen);
352          if(macCb.macCell->macRaCb[0].msg4Pdu)
353          {
354                            for(idx = 0; idx < dlCcchIndInfo->dlCcchMsgLen; idx++)
355                                 {
356                macCb.macCell->macRaCb[0].msg4Pdu[idx] =\
357                                           dlCcchIndInfo->dlCcchMsg[idx];
358                            }
359          }
360       }
361    }
362    sendDlRlcBoInfoMacToSch(&dlBoInfo);
363    
364    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
365       dlCcchIndInfo->dlCcchMsgLen);
366    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
367    return ROK;
368
369 }
370
371 /*******************************************************************
372  *
373  * @brief Sends UL CCCH Ind to DU APP
374  *
375  * @details
376  *
377  *    Function : macSendUlCcchInd
378  *
379  *    Functionality:
380  *        MAC sends UL CCCH Ind to DU APP
381  *
382  * @params[in] Post structure pointer
383  *            
384  * @return ROK     - success
385  *         RFAILED - failure
386  *
387  * ****************************************************************/
388 uint16_t macSendUlCcchInd(uint8_t *rrcContainer, uint16_t cellId, uint16_t crnti)
389 {
390    Pst pst;
391         uint8_t ret = ROK;
392    UlCcchIndInfo *ulCcchIndInfo = NULLP;
393
394    MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
395    if(!ulCcchIndInfo)
396    {
397                 DU_LOG("\nMAC: Memory failed in macSendUlCcchInd");
398                 return RFAILED;
399         }
400
401         ulCcchIndInfo->cellId = cellId;
402         ulCcchIndInfo->crnti  = crnti;
403         ulCcchIndInfo->ulCcchMsg = rrcContainer;
404
405         /* Fill Pst */
406         pst.selector  = DU_MAC_LWLC;
407         pst.srcEnt    = ENTRG;
408         pst.dstEnt    = ENTDUAPP;
409         pst.dstInst   = 0;
410         pst.srcInst   = macCb.macInst;
411         pst.dstProcId = rgCb[pst.srcInst].rgInit.procId;
412         pst.srcProcId = rgCb[pst.srcInst].rgInit.procId;
413         pst.region    = MAC_MEM_REGION;
414         pst.pool      = MAC_POOL;
415         pst.event     = EVENT_MAC_UL_CCCH_IND;
416         pst.route     = 0;
417         pst.prior     = 0;
418         pst.intfVer   = 0;
419
420         if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
421         {
422                 DU_LOG("\nMAC: Failed to send UL CCCH Ind to DU APP");
423                 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg,
424                                 strlen((const char*)ulCcchIndInfo->ulCcchMsg));
425                 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
426                 ret = RFAILED;
427         }
428         return ret;
429 }
430
431 /**********************************************************************
432          End of file
433 **********************************************************************/
434