PHY_DELTA changes
[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    DlRlcBOInfo  dlBoInfo;
329
330    DU_LOG("\nMAC : Handling DL CCCH IND");
331    
332    /* TODO : Fill DL RLC Buffer status info */
333    dlBoInfo.cellId = dlCcchIndInfo->cellId;
334    dlBoInfo.crnti = dlCcchIndInfo->crnti;
335    dlBoInfo.numLc = 0;
336    
337    if(dlCcchIndInfo->msgType == RRC_SETUP)
338    {
339       dlBoInfo.numLc++;
340       dlBoInfo.boInfo[dlBoInfo.numLc].lcId = 0;    // SRB 0 for msg4
341       dlBoInfo.boInfo[dlBoInfo.numLc].dataVolume = \
342       strlen((const char*)dlCcchIndInfo->dlCcchMsg);
343
344       /* storing Msg4 Pdu in raCb */
345       if(macCb.macCell->macRaCb[0].crnti == dlCcchIndInfo->crnti)
346       {
347          macCb.macCell->macRaCb[0].msg4PduLen = strlen((const char*)dlCcchIndInfo\
348          ->dlCcchMsg);
349          MAC_ALLOC(macCb.macCell->macRaCb[0].msg4Pdu, macCb.macCell->macRaCb[0]\
350          .msg4PduLen);
351          if(macCb.macCell->macRaCb[0].msg4Pdu)
352          {
353             memcpy(macCb.macCell->macRaCb[0].msg4Pdu, dlCcchIndInfo->dlCcchMsg,\
354             macCb.macCell->macRaCb[0].msg4PduLen);
355          }
356       }
357    }
358    sendDlRlcBoInfoMacToSch(&dlBoInfo);
359    
360    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo->dlCcchMsg, \
361       strlen((const char*)dlCcchIndInfo->dlCcchMsg));
362    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlCcchIndInfo, sizeof(DlCcchIndInfo));
363    return ROK;
364
365 }
366
367 /*******************************************************************
368  *
369  * @brief Sends UL CCCH Ind to DU APP
370  *
371  * @details
372  *
373  *    Function : macSendUlCcchInd
374  *
375  *    Functionality:
376  *        MAC sends UL CCCH Ind to DU APP
377  *
378  * @params[in] Post structure pointer
379  *            
380  * @return ROK     - success
381  *         RFAILED - failure
382  *
383  * ****************************************************************/
384 uint16_t macSendUlCcchInd(uint8_t *rrcContainer, uint16_t cellId, uint16_t crnti)
385 {
386    Pst pst;
387         uint8_t ret = ROK;
388    UlCcchIndInfo *ulCcchIndInfo = NULLP;
389
390    MAC_ALLOC_SHRABL_BUF(ulCcchIndInfo, sizeof(UlCcchIndInfo));
391    if(!ulCcchIndInfo)
392    {
393                 DU_LOG("\nMAC: Memory failed in macSendUlCcchInd");
394                 return RFAILED;
395         }
396
397         ulCcchIndInfo->cellId = cellId;
398         ulCcchIndInfo->crnti  = crnti;
399         ulCcchIndInfo->ulCcchMsg = rrcContainer;
400
401         /* Fill Pst */
402         pst.selector  = DU_MAC_LWLC;
403         pst.srcEnt    = ENTRG;
404         pst.dstEnt    = ENTDUAPP;
405         pst.dstInst   = 0;
406         pst.srcInst   = macCb.macInst;
407         pst.dstProcId = rgCb[pst.srcInst].rgInit.procId;
408         pst.srcProcId = rgCb[pst.srcInst].rgInit.procId;
409         pst.region    = MAC_MEM_REGION;
410         pst.pool      = MAC_POOL;
411         pst.event     = EVENT_MAC_UL_CCCH_IND;
412         pst.route     = 0;
413         pst.prior     = 0;
414         pst.intfVer   = 0;
415
416         if(MacDuAppUlCcchInd(&pst, ulCcchIndInfo) != ROK)
417         {
418                 DU_LOG("\nMAC: Failed to send UL CCCH Ind to DU APP");
419                 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg,
420                                 strlen((const char*)ulCcchIndInfo->ulCcchMsg));
421                 MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
422                 ret = RFAILED;
423         }
424         return ret;
425 }
426
427 /**********************************************************************
428          End of file
429 **********************************************************************/
430