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