324ca8e1ecbf5699c02b81c05469676e67375f4c
[o-du/l2.git] / src / phy_stub / phy_stub_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 /*This file contains stub for PHY to handle messages to/from MAC CL */
20
21 #include "common_def.h"
22 #include "lrg.h"
23 #include "lrg.x"
24 #include "du_app_mac_inf.h"
25 #include "mac_sch_interface.h"
26 #include "lwr_mac.h"
27 #include "lwr_mac_fsm.h"
28 #include "lwr_mac_phy.h"
29 #ifdef INTEL_FAPI
30 #include "fapi.h"
31 #include "fapi_vendor_extension.h"
32 #endif
33 #include "lwr_mac_upr_inf.h"
34 #include "mac_utils.h"
35 #include "phy_stub.h"
36 #include "phy_stub_utils.h"
37 #include "lwr_mac_phy_stub_inf.h"
38
39 /*******************************************************************
40  *
41  * @brief Builds and sends param response to MAC CL
42  *
43  * @details
44  *
45  *    Function : l1BldAndSndParamRsp
46  *
47  *    Functionality:
48  *          - Builds and sends param response to MAC
49  *
50  * @params[in] Config request message pointer
51  * @return ROK     - success
52  *         RFAILED - failure
53  *
54  * ****************************************************************/
55 S16 l1BldAndSndParamRsp(void *msg)
56 {
57 #ifdef INTEL_FAPI
58    uint8_t index = 0;
59    uint32_t msgLen = 0;
60    fapi_param_resp_t *fapiParamRsp;
61
62    MAC_ALLOC(fapiParamRsp, sizeof(fapi_param_resp_t));
63    if(!fapiParamRsp)
64    {
65       DU_LOG("\nERROR  -->  PHY STUB: Memory allocation failed");
66       return RFAILED;
67    }
68
69    /* Cell Params */
70    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_RELEASE_CAPABILITY_TAG,                         sizeof(uint16_t), 1, &msgLen);
71    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PHY_STATE_TAG,                                  sizeof(uint16_t), 0, &msgLen);
72    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SKIP_BLANK_DL_CONFIG_TAG,                       sizeof(uint8_t),  0, &msgLen);
73    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SKIP_BLANK_UL_CONFIG_TAG,                       sizeof(uint8_t),  0, &msgLen);
74    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_NUM_CONFIG_TLVS_TO_REPORT_TYPE_TAG,              sizeof(uint8_t),  0, &msgLen);
75
76    /* Carrier Params */
77    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CYCLIC_PREFIX_TAG,                               sizeof(uint8_t),  1, &msgLen);
78    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_SUBCARRIER_SPACING_DL_TAG,            sizeof(uint8_t),  1, &msgLen);
79    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_BANDWIDTH_DL_TAG,                     sizeof(uint16_t), 1, &msgLen);
80    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_SUBCARRIER_SPACING_UL_TAG,            sizeof(uint8_t),  0, &msgLen);
81    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_BANDWIDTH_UL_TAG,                     sizeof(uint16_t), 0, &msgLen);
82
83    /* PDCCH Param*/
84    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CCE_MAPPING_TYPE_TAG,                                   sizeof(uint8_t), 0, &msgLen);
85    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CORESET_OUTSIDE_FIRST_3_OFDM_SYMS_OF_SLOT_TAG,   sizeof(uint8_t), 0, &msgLen);
86    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRECODER_GRANULARITY_CORESET_TAG,               sizeof(uint8_t), 0, &msgLen);
87    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDCCH_MU_MIMO_TAG,                              sizeof(uint8_t), 0, &msgLen);
88    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDCCH_PRECODER_CYCLING_TAG,                     sizeof(uint8_t), 0, &msgLen);
89    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDCCHS_PER_SLOT_TAG,                                sizeof(uint8_t), 0, &msgLen);
90
91    /* PUCCH Param */
92    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUCCH_FORMATS_TAG,                              sizeof(uint8_t), 0, &msgLen);
93    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PUCCHS_PER_SLOT_TAG,                         sizeof(uint8_t), 0, &msgLen);
94
95    /* PDSCH Param */
96    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_MAPPING_TYPE_TAG,                         sizeof(uint8_t), 0, &msgLen);
97    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_ALLOCATION_TYPES_TAG,                      sizeof(uint8_t), 0, &msgLen);
98    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_VRB_TO_PRB_MAPPING_TAG,                   sizeof(uint8_t), 0, &msgLen);
99    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_CBG_TAG,                                  sizeof(uint8_t), 0, &msgLen);
100    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_CONFIG_TYPES_TAG,                    sizeof(uint8_t), 0, &msgLen);
101    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_MAX_LENGTH_TAG,                      sizeof(uint8_t), 0, &msgLen);
102    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_ADDITIONAL_POS_TAG,                  sizeof(uint8_t), 0, &msgLen);
103    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDSCHS_TBS_PER_SLOT_TAG,                    sizeof(uint8_t), 0, &msgLen);
104    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_NUMBER_MIMO_LAYERS_PDSCH_TAG,               sizeof(uint8_t), 0, &msgLen);
105    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_MAX_MODULATION_ORDER_DL_TAG,          sizeof(uint8_t), 0, &msgLen);
106    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_MU_MIMO_USERS_DL_TAG,                       sizeof(uint8_t), 0, &msgLen);
107    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DATA_IN_DMRS_SYMBOLS_TAG,                 sizeof(uint8_t), 0, &msgLen);
108    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PREMPTIONSUPPORT_TAG,                           sizeof(uint8_t), 0, &msgLen);
109    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_NON_SLOT_SUPPORT_TAG,                      sizeof(uint8_t), 0, &msgLen);
110
111    /* PUSCH Param */
112    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_UCI_MUX_ULSCH_IN_PUSCH_TAG,                     sizeof(uint8_t), 0, &msgLen);
113    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_UCI_ONLY_PUSCH_TAG,                             sizeof(uint8_t), 0, &msgLen);
114    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_FREQUENCY_HOPPING_TAG,                    sizeof(uint8_t), 0, &msgLen);
115    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_CONFIG_TYPES_TAG,                    sizeof(uint8_t), 0, &msgLen);
116    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_MAX_LEN_TAG,                         sizeof(uint8_t), 0, &msgLen);
117    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_ADDITIONAL_POS_TAG,                  sizeof(uint8_t), 0, &msgLen);
118    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_CBG_TAG,                                  sizeof(uint8_t), 0, &msgLen);
119    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_MAPPING_TYPE_TAG,                          sizeof(uint8_t), 0, &msgLen);
120    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_ALLOCATION_TYPES_TAG,                     sizeof(uint8_t), 0, &msgLen);
121    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_VRB_TO_PRB_MAPPING_TAG,                   sizeof(uint8_t), 0, &msgLen);
122    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_MAX_PTRS_PORTS_TAG,                        sizeof(uint8_t), 0, &msgLen);
123    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDUSCHS_TBS_PER_SLOT_TAG,                   sizeof(uint8_t), 0, &msgLen);
124    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_NUMBER_MIMO_LAYERS_NON_CB_PUSCH_TAG,         sizeof(uint8_t), 0, &msgLen);
125    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_MODULATION_ORDER_UL_TAG,               sizeof(uint8_t), 0, &msgLen);
126    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_MU_MIMO_USERS_UL_TAG,                        sizeof(uint8_t), 0, &msgLen);
127    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_DFTS_OFDM_SUPPORT_TAG,                           sizeof(uint8_t), 0, &msgLen);
128    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_AGGREGATION_FACTOR_TAG,                    sizeof(uint8_t), 0, &msgLen);
129
130    /* PRACH Params */
131    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_LONG_FORMATS_TAG,                         sizeof(uint8_t), 0, &msgLen);
132    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_SHORT_FORMATS_TAG,                         sizeof(uint8_t), 0, &msgLen);
133    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_RESTRICTED_SETS_TAG,                       sizeof(uint8_t), 0, &msgLen);
134    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PRACH_FD_OCCASIONS_IN_A_SLOT_TAG,            sizeof(uint8_t), 0, &msgLen);
135
136    /* MEASUREMENT TAG */
137    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_RSSI_MEASUREMENT_SUPPORT_TAG,                    sizeof(uint8_t), 0, &msgLen);
138
139    fapiParamRsp->number_of_tlvs = index;
140    msgLen += sizeof(fapi_param_resp_t) - sizeof(fapi_msg_t);
141    fillMsgHeader(&fapiParamRsp->header, FAPI_PARAM_RESPONSE, msgLen);
142    fapiParamRsp->error_code = MSG_OK;
143
144    DU_LOG("\nINFO   -->  PHY_STUB: Sending Param Request to Lower Mac");
145    procPhyMessages(fapiParamRsp->header.msg_id, sizeof(fapi_param_resp_t), (void *)fapiParamRsp);
146    MAC_FREE(fapiParamRsp, sizeof(fapi_param_resp_t));
147 #endif
148    return ROK;
149 }
150
151 /*******************************************************************
152  *
153  * @brief Builds and sends config response to lower mac
154  *
155  * @details
156  *
157  *    Function : l1BldAndSndConfigRsp
158  *
159  *    Functionality:
160  *          - Builds and sends config response to MAC
161  *
162  * @params[in] Config request message pointer
163  * @return ROK     - success
164  *         RFAILED - failure
165  *
166  * ****************************************************************/
167
168 S16 l1BldAndSndConfigRsp(void *msg)
169 {
170 #ifdef INTEL_FAPI
171    uint32_t msgLen = 0;
172    fapi_config_resp_t *fapiConfigRsp;
173
174    MAC_ALLOC(fapiConfigRsp, sizeof(fapi_config_resp_t));
175    if(!fapiConfigRsp)
176    {
177       DU_LOG("\nERROR  -->  PHY STUB: Memory allocation failed");
178       return RFAILED;
179    }
180    memset(fapiConfigRsp, 0, sizeof(fapi_config_resp_t));
181    fapiConfigRsp->number_of_invalid_tlvs = NULLP;
182    fapiConfigRsp->number_of_inv_tlvs_idle_only = NULLP;
183    fapiConfigRsp->number_of_missing_tlvs = NULLP;
184    fapiConfigRsp->error_code = MSG_OK;
185    msgLen = sizeof(fapi_config_resp_t) - sizeof(fapi_msg_t);
186    fillMsgHeader(&fapiConfigRsp->header, FAPI_CONFIG_RESPONSE, msgLen);
187
188    DU_LOG("\nINFO   -->  PHY_STUB: Sending Config Response to Lower Mac");
189
190    procPhyMessages(fapiConfigRsp->header.msg_id, \
191          sizeof(fapi_config_resp_t), (void *)fapiConfigRsp);
192    MAC_FREE(fapiConfigRsp, sizeof(fapi_config_resp_t));
193 #endif
194    return ROK;
195 }
196 /*******************************************************************
197  *
198  * @brief Handles param request received from MAC
199  *
200  * @details
201  *
202  *    Function : l1HdlParamReq
203  *
204  *    Functionality:
205  *          -Handles param request received from MAC
206  *
207  * @params[in]   Message length
208  *               Param request message pointer
209  *
210  * @return void
211  *
212  * ****************************************************************/
213
214 void l1HdlParamReq(uint32_t msgLen, void *msg)
215 {
216 #ifdef INTEL_FAPI
217    DU_LOG("\nINFO   -->  PHY_STUB: Received Param Request in PHY");
218
219    /* Build and send PARAM RESPONSE */
220    if(l1BldAndSndParamRsp(msg)!= ROK)
221    {
222       DU_LOG("\nERROR  -->  PHY_STUB: Failed Sending Param Response");
223    }
224    MAC_FREE(msg, sizeof(fapi_param_req_t));
225 #endif
226
227
228 /*******************************************************************
229  *
230  * @brief Handles config request received from MAC
231  *
232  * @details
233  *
234  *    Function : l1HdlConfigReq
235  *
236  *    Functionality:
237  *          -Handles config request received from MAC
238  *
239  * @params[in]   Message length
240  *               config request message pointer
241  *
242  * @return void
243  *
244  * ****************************************************************/
245
246 void l1HdlConfigReq(uint32_t msgLen, void *msg)
247 {
248    memset(&phyDb.ueDb, 0, sizeof(UeDb));
249    cmInetAddr((char *)SOURCE_DU_IPV4_ADDR, &phyDb.ipCfgInfo.sourceDu);
250    cmInetAddr((char *)DESTINATION_DU_IPV4_ADDR, &phyDb.ipCfgInfo.destinationDu);
251    phyDb.ipCfgInfo.portNumber = PORT_NUMBER;
252    phyDb.isServer = true;
253
254 #ifdef INTEL_FAPI
255    p_fapi_api_queue_elem_t configReqElem = (p_fapi_api_queue_elem_t)msg;
256    fapi_config_req_t *configReq = (fapi_config_req_t *)(configReqElem +1);
257
258    DU_LOG("\nINFO   -->  PHY_STUB: Received Config Request in PHY");
259
260    /* Handling CONFIG RESPONSE */
261    if(l1BldAndSndConfigRsp(configReq)!= ROK)
262    {
263       DU_LOG("\nERROR  -->  PHY_STUB: Failed Sending config Response");
264    }
265
266    MAC_FREE(msg, msgLen);
267 #endif
268
269 }
270
271 /*******************************************************************
272  *
273  * @brief Build and Send CRC Indication
274  *
275  * @details
276  *
277  *    Function : l1BuildAndSendCrcInd
278  *
279  *    Functionality:
280  *      Build and Send CRC Indication
281  *
282  * @params[in] Slot
283  *             SFN 
284  * @return ROK     - success
285  *         RFAILED - failure
286  *
287  * ****************************************************************/
288 uint16_t l1BuildAndSendCrcInd(uint16_t slot, uint16_t sfn)
289 {
290 #ifdef INTEL_FAPI
291    uint8_t idx = 0;
292    fapi_crc_ind_t  *crcInd;
293
294    MAC_ALLOC(crcInd, sizeof(fapi_crc_ind_t));
295    if(!crcInd)
296    {
297       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for CRC Indication Message");
298       return RFAILED;
299    }
300    memset(crcInd, 0, sizeof(fapi_crc_ind_t));
301
302    /* TODO: Fill the required values. As of now only 1 CRC status PASS is filled */
303    crcInd->sfn = sfn;
304    crcInd->slot = slot;
305    crcInd->numCrcs = 1;
306
307    crcInd->crc[idx].handle = 0;
308    crcInd->crc[idx].rnti = 0;
309    crcInd->crc[idx].harqId = 0;
310    crcInd->crc[idx].tbCrcStatus = 0;
311    crcInd->crc[idx].numCb = 1;
312    crcInd->crc[idx].cbCrcStatus[0] = 0;
313    crcInd->crc[idx].ul_cqi = 0;
314    crcInd->crc[idx].timingAdvance = 0;
315    crcInd->crc[idx].rssi = 0;
316
317    fillMsgHeader(&crcInd->header, FAPI_CRC_INDICATION, \
318          sizeof(fapi_crc_ind_t) - sizeof(fapi_msg_t));
319
320    /* Sending RACH indication to MAC */
321    DU_LOG("\nINFO   -->  PHY STUB: Sending CRC Indication to MAC");
322    procPhyMessages(crcInd->header.msg_id, sizeof(fapi_crc_ind_t), (void *)crcInd);
323    MAC_FREE(crcInd, sizeof(fapi_crc_ind_t));
324 #endif
325    return ROK;
326 } /* l1BuildAndSendCrcInd */
327
328 #ifdef INTEL_FAPI
329 /*******************************************************************
330  *
331  * @brief Build and send Rx data indication
332  *
333  * @details
334  *
335  *    Function : l1BuildAndSendRxDataInd
336  *
337  *    Functionality:
338  *       Build and send Rx data indication
339  *
340  * @params[in] SFN
341  *             Slot
342  * @return ROK     - success
343  *         RFAILED - failure
344  *
345  * ****************************************************************/
346 uint16_t l1BuildAndSendRxDataInd(uint16_t slot, uint16_t sfn, fapi_ul_pusch_pdu_t puschPdu)
347 {
348    uint8_t idx = 0, ueId = 0;
349    fapi_rx_data_indication_t *rxDataInd =NULLP;
350    fapi_pdu_ind_info_t       *pduInfo =NULLP;
351    uint8_t  *pdu = NULLP;
352    uint16_t byteIdx = 0;
353    uint32_t msgLen = 0;
354    MsgType type = 0;
355
356    GET_UE_ID(puschPdu.rnti, ueId);
357    if(!phyDb.ueDb.ueCb[ueId-1].msg3Sent)
358    {
359       phyDb.ueDb.ueCb[ueId-1].ueId = ueId;
360       phyDb.ueDb.ueCb[ueId-1].crnti = puschPdu.rnti;
361       phyDb.ueDb.ueCb[ueId-1].msg3Sent = true;
362       type = MSG_TYPE_MSG3;
363       sleep(2);
364    }
365    else if(!phyDb.ueDb.ueCb[ueId-1].msg5ShortBsrSent)
366    {
367       phyDb.ueDb.ueCb[ueId-1].msg5ShortBsrSent = true;
368       type = MSG_TYPE_SHORT_BSR;
369    }
370    else if(!phyDb.ueDb.ueCb[ueId-1].msg5Sent)
371    {
372       phyDb.ueDb.ueCb[ueId-1].msg5Sent = true;
373       type = MSG_TYPE_MSG5;
374    }
375    else if(!phyDb.ueDb.ueCb[ueId-1].msgRegistrationComp)
376    {
377       phyDb.ueDb.ueCb[ueId-1].msgRegistrationComp = true;
378       type = MSG_TYPE_REGISTRATION_COMPLETE; 
379    }
380    else if(!phyDb.ueDb.ueCb[ueId-1].msgSecurityModeComp)
381    {
382       phyDb.ueDb.ueCb[ueId-1].msgSecurityModeComp = true;
383       type = MSG_TYPE_SECURITY_MODE_COMPLETE;
384    }
385    else if(!phyDb.ueDb.ueCb[ueId-1].msgRrcReconfiguration)
386    {
387       phyDb.ueDb.ueCb[ueId-1].msgRrcReconfiguration = true;
388       type = MSG_TYPE_RRC_RECONFIG_COMPLETE;
389    }
390    else
391       return RFAILED;
392
393    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
394    if(!rxDataInd)
395    {
396       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
397       return RFAILED;
398    }
399    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
400    
401    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
402    rxDataInd->sfn = sfn;
403    rxDataInd->slot = slot;
404    rxDataInd->numPdus = 1;
405
406    pduInfo = &rxDataInd->pdus[idx];
407    pduInfo->handle = puschPdu.handle;
408    pduInfo->rnti = puschPdu.rnti;
409    pduInfo->harqId = puschPdu.puschData.harqProcessId;
410    pduInfo->pdu_length = puschPdu.puschData.tbSize ;
411    pduInfo->ul_cqi = 0;
412    pduInfo->timingAdvance = 0;
413    pduInfo->rssi = 0;
414
415    /* Filling pdu with random values for testing */
416    pduInfo->pduData = NULL;
417    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
418    if(!pduInfo->pduData)
419    {
420       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
421       return RFAILED;
422    }
423    memset(pduInfo->pduData, 0, pduInfo->pdu_length);
424    /* Filling PDU */
425    pdu = (uint8_t *)pduInfo->pduData;
426
427    switch(type)
428    {
429       case MSG_TYPE_MSG3: 
430          {
431             DU_LOG("\nDEBUG  -->  PHY_STUB: Forming MSG3 PDU ");
432             /* For Initial RRC setup Request,
433                MAC subheader format is R/R/LCId (1byte)
434                LCId is CCCH(0)
435                From 38.321 section 6.1.1
436                */
437             pdu[byteIdx++] = 0;
438             /* Hardcoding MAC PDU */
439             pdu[byteIdx++] = 16;
440             pdu[byteIdx++] = 00;
441             pdu[byteIdx++] = 00;
442             pdu[byteIdx++] = 00;
443             pdu[byteIdx++] = 00;
444             pdu[byteIdx++] = 103;
445
446             break;
447          }
448       case MSG_TYPE_SHORT_BSR:
449          {
450             DU_LOG("\nDEBUG  -->  PHY_STUB: Forming SHORT BSR PDU ");
451             uint8_t lcgId = 0;
452             uint8_t bufferSizeIdx = 6;
453
454             /* For Short BSR
455                MAC subheader format is R/R/LcId (1Byte)
456                LCId is 61
457                From 38.321 section 6.1.1
458                */
459             pdu[byteIdx++] = 61;    // LCID
460             pdu[byteIdx++] = (lcgId << 5) | bufferSizeIdx;
461
462             break;
463          }
464
465       case MSG_TYPE_MSG5:
466       {
467          /* For RRC setup complete
468           *
469           * MAC subheader format is R/F/LCId/L (2/3 bytes)
470           * LCId is 1 for SRB1
471           * L is length of PDU i.e 6bytes here 
472           * From 38.321 section 6.1.1
473           *
474           * RLC subheader for AM PDU is D/C/P/SI/SN (2 bytes for 12-bit SN)
475           * From 38.322, section 6.2.2.4
476           */
477          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming MSG5 PDU");
478          uint8_t  msg5PduLen = 33; /* Length of MSG5 */
479          msg5PduLen += 2; /* RLC subheader */
480          uint8_t msg5[] = {1, msg5PduLen, 128, phyDb.ueDb.ueCb[ueId-1].rlcSnForSrb1++, 0, phyDb.ueDb.ueCb[ueId-1].pdcpSn++, 16, 0, \
481             5, 223, 128, 16, 94, 64, 3, 64, 68, 252, 97, 0, 0, 0, 0, 4, 0, 0, 4, 68, 11, 128, 184, 56, 0, 0, 0, 0, 0};
482
483          msg5PduLen += 2;  /* 2 bytes of MAC header */
484          memcpy(pdu, &msg5, msg5PduLen);
485          byteIdx += msg5PduLen; /* 4 bytes of header : MAC+RLC */
486          break;
487       }
488
489       case MSG_TYPE_SECURITY_MODE_COMPLETE:
490       {
491          /* For security mode complete where RRC Container is dummy
492           *
493           * MAC subheader format is R/F/LCId/L (2/3 bytes)
494           * LCId is 1 for SRB1
495           * L is length of PDU i.e 6bytes here 
496           * From 38.321 section 6.1.1
497           *
498           * RLC subheader for AM PDU is D/C/P/SI/SN (2 bytes for 12-bit SN)
499           * From 38.322, section 6.2.2.4
500           */
501          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming SECURITY MODE COMPLETE PDU");
502          uint8_t  pduLen = 12; /* Length of PDU */
503          pduLen += 2; /* RLC subheader */
504          uint8_t msg[] = {1, pduLen, 128, phyDb.ueDb.ueCb[ueId-1].rlcSnForSrb1++, 0, phyDb.ueDb.ueCb[ueId-1].pdcpSn++, 0x2a, 0x40, \
505             0, 0, 0, 0, 0, 0, 0, 0};
506
507          pduLen += 2;  /* 2 bytes of MAC header */
508          memcpy(pdu, &msg, pduLen);
509          byteIdx += pduLen; /* 4 bytes of header : MAC+RLC */
510          break;
511       }
512
513       case MSG_TYPE_REGISTRATION_COMPLETE:
514       {
515          /* For rrc reconfig complete where RRC Container is dummy
516           *
517           * MAC subheader format is R/F/LCId/L (2/3 bytes)
518           * LCId is 1 for SRB1
519           * L is length of PDU i.e 6bytes here
520           * From 38.321 section 6.1.1
521           * 
522           * RLC subheader for AM PDU is D/C/P/SI/SN (2 bytes for 12-bit SN)
523           * From 38.322, section 6.2.2.4
524           */
525          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming RRC REGISTRATION COMPLETE PDU");
526          uint8_t  pduLen = 12; /* Length of PDU */
527          pduLen += 2; /* RLC subheader */
528          uint8_t msg[] = {1, pduLen, 128, phyDb.ueDb.ueCb[ueId-1].rlcSnForSrb1++, 0, phyDb.ueDb.ueCb[ueId-1].pdcpSn++, 0x3a, 0x81, \
529             0xbf, 0, 0x21, 0x80, 0, 0, 0, 0};
530
531          pduLen += 2;  /* 2 bytes of MAC header */
532          memcpy(pdu, &msg, pduLen);
533          byteIdx += pduLen; /* 4 bytes of header : MAC+RLC */
534          break;
535       }
536
537       case MSG_TYPE_RRC_RECONFIG_COMPLETE:
538       {
539          /* For rrc reconfig complete where RRC Container is dummy
540           *
541           * MAC subheader format is R/F/LCId/L (2/3 bytes)
542           * LCId is 1 for SRB1
543           * L is length of PDU i.e 6bytes here
544           * From 38.321 section 6.1.1
545           *
546           * RLC subheader for AM PDU is D/C/P/SI/SN (2 bytes for 12-bit SN)
547           * From 38.322, section 6.2.2.4
548           */
549          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming RRC RECONFIGURATION COMPLETE PDU");
550          uint8_t  pduLen = 13; /* PDU length */
551          pduLen += 2; /* RLC sub header */
552          uint8_t msg[] = {1, pduLen, 128, phyDb.ueDb.ueCb[ueId-1].rlcSnForSrb1++, 0, phyDb.ueDb.ueCb[ueId-1].pdcpSn++, 8, 64, 0, 0,\
553             0, 0, 0, 0, 0, 0, 0};
554
555          pduLen += 2;  /* 2bytes of MAC header */
556          memcpy(pdu, &msg, pduLen);
557          byteIdx += pduLen; /* 4 bytes of header : MAC+RLC*/
558          break;
559
560       }
561
562       default:
563       break;
564    } /* End of switch(type) */
565
566    /* Filling MAC SDU for Padding bytes*/
567    if(byteIdx < pduInfo->pdu_length)
568    {
569       /* For Padding
570          MAC subheader format is R/R/LCId (1byte)
571          LCId is 63 for padding
572          From 38.321 section 6.1.1
573          */
574       pdu[byteIdx++] = 63;
575
576       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
577          pdu[byteIdx] = 0;
578    }
579    msgLen += pduInfo->pdu_length;
580
581    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
582
583    /* Sending Rx data indication to MAC */
584    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
585    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
586
587    if(pduInfo->pdu_length)
588       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
589    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
590    return ROK;
591 }
592 #endif
593
594 /*******************************************************************
595  *
596  * @brief Builds and Sends RACH indication to MAC 
597  *
598  * @details
599  *
600  *    Function : l1BuildAndSendRachInd 
601  *
602  *    Functionality:
603  *      Builds and Sends RACH indication to MAC
604  *
605  * @params[in] SFN value 
606  *             slot value
607  * @return ROK     - success
608  *         RFAILED - failure
609  *
610  * ****************************************************************/
611 uint16_t l1BuildAndSendRachInd(uint16_t slot, uint16_t sfn)
612 {
613 #ifdef INTEL_FAPI
614    uint8_t   rachPduIdx = 0; 
615    uint8_t   preamIdx = 0;
616    fapi_rach_pdu_t  *rachPdu;
617    fapi_rach_indication_t  *rachInd;
618
619    /* Building RACH indication */
620    MAC_ALLOC(rachInd, sizeof(fapi_rach_indication_t));
621    if(!rachInd)
622    {
623       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rach Indication Message");
624       return RFAILED;
625    }
626    memset(rachInd, 0, sizeof(fapi_rach_indication_t)); 
627    rachInd->sfn = sfn;
628    rachInd->slot = slot;
629    rachInd->numPdus = 1;
630
631    rachPdu = &rachInd->rachPdu[rachPduIdx];
632    rachPdu->phyCellId = NR_PCI;
633    rachPdu->symbolIndex = 0;
634    rachPdu->slotIndex = slot;
635    rachPdu->freqIndex = 0;
636    rachPdu->avgRssi = 0;
637    rachPdu->avgSnr = 0;
638    rachPdu->numPreamble = 1;
639
640    rachPdu->preambleInfo[preamIdx].preambleIndex = 3;
641    rachPdu->preambleInfo[preamIdx].timingAdvance = 0;
642    rachPdu->preambleInfo[preamIdx].preamblePwr = 0;
643
644    fillMsgHeader(&rachInd->header, FAPI_RACH_INDICATION, \
645          sizeof(fapi_rach_indication_t) - sizeof(fapi_msg_t));
646
647    /* Sending RACH indication to MAC */
648    DU_LOG("\nINFO   -->  PHY STUB: Sending RACH Indication to MAC");
649    procPhyMessages(rachInd->header.msg_id, sizeof(fapi_rach_indication_t), (void *)rachInd);
650    MAC_FREE(rachInd, sizeof(fapi_rach_indication_t));
651 #endif
652    return ROK;
653 }
654
655 /*******************************************************************
656  *
657  * @brief Builds and Send the Slot Indication message to MAC
658  *
659  * @details
660  *
661  *    Function : l1BuildAndSendSlotIndication
662  *
663  *    Functionality:
664  *          -Send the Slot indication Message to MAC
665  *
666  * @params[in]   Message length
667  *               config request message pointer
668  *
669  * @return void
670  *
671  * ****************************************************************/
672 uint16_t l1BuildAndSendSlotIndication()
673 {
674    Pst pst;
675    Buffer *mBuf;
676
677 #ifdef INTEL_FAPI
678    fapi_slot_ind_t *slotIndMsg;
679
680    MAC_ALLOC_SHRABL_BUF(slotIndMsg, sizeof(fapi_slot_ind_t));
681    if(!slotIndMsg)
682    {
683       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for slot Indication Message");
684       return RFAILED;
685    }
686    else
687    {
688       memset(slotIndMsg, 0, sizeof(fapi_slot_ind_t));
689       slotIndMsg->sfn = sfnValue;
690       slotIndMsg->slot = slotValue;
691
692 #ifdef ODU_SLOT_IND_DEBUG_LOG
693       DU_LOG("\n\nDEBUG  -->  PHY_STUB: Sending Slot Indication [%d : %d] to MAC", sfnValue, slotValue);
694 #endif
695
696       /* increment for the next TTI */
697       slotValue++;
698       if(sfnValue >= MAX_SFN_VALUE && slotValue > MAX_SLOT_VALUE)
699       {
700          sfnValue = 0;
701          slotValue = 0;
702       }
703       else if(slotValue > MAX_SLOT_VALUE)
704       {
705          sfnValue++;
706          slotValue = 0;
707       }
708       fillMsgHeader(&slotIndMsg->header, FAPI_SLOT_INDICATION, \
709             sizeof(fapi_slot_ind_t) - sizeof(fapi_msg_t));
710
711       memset(&pst, 0, sizeof(Pst));
712       FILL_PST_PHY_STUB_TO_LWR_MAC(pst, EVT_PHY_STUB_SLOT_IND);
713       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
714       if(!mBuf)
715       {
716          DU_LOG("\nERROR  --> PHY_STUB: Failed to allocate memory for slot indication buffer");
717          MAC_FREE_SHRABL_BUF(pst.region, pst.pool, slotIndMsg, sizeof(fapi_slot_ind_t));
718          return RFAILED;
719       }
720       CMCHKPK(oduPackPointer, (PTR)slotIndMsg, mBuf);
721       ODU_POST_TASK(&pst, mBuf);
722    }
723 #endif
724    return ROK;
725 }
726
727 /*******************************************************************
728  *
729  * @brief Handles start request received from MAC
730  *
731  * @details
732  *
733  *    Function : l1HdlStartReq
734  *
735  *    Functionality:
736  *          -Handles start request received from MAC
737  *
738  * @params[in]   Message length
739  *               config request message pointer
740  *
741  * @return void
742  *
743  * ****************************************************************/
744
745 S16 l1HdlStartReq(uint32_t msgLen, void *msg)
746 {
747 #ifdef INTEL_FAPI
748    if(lwrMacCb.phyState == PHY_STATE_CONFIGURED)
749    {
750       l1HdlSlotIndicaion(FALSE);
751       l1StartConsoleHandler();
752       MAC_FREE(msg, msgLen);
753    }
754    else
755    {
756       DU_LOG("\nINFO   -->  PHY_STUB: Received Start Req in PHY State %d", lwrMacCb.phyState);
757       MAC_FREE(msg, msgLen);
758       return RFAILED;
759    }
760 #endif
761    return ROK;
762 }
763
764 /*******************************************************************
765  *
766  * @brief Handles Dl Tti request received from MAC
767  *
768  * @details
769  *
770  *    Function : l1HdlDlTtiReq
771  *
772  *    Functionality:
773  *          -Handles Dl Tti request received from MAC
774  *
775  * @params[in]   Message length
776  *               Dl Tti request message pointer
777  *
778  * @return void
779  *
780  * ****************************************************************/
781
782 S16 l1HdlDlTtiReq(uint16_t msgLen, void *msg)
783 {
784 #ifdef INTEL_FAPI
785    p_fapi_api_queue_elem_t dlTtiElem = (p_fapi_api_queue_elem_t)msg;
786    fapi_dl_tti_req_t *dlTtiReq = (fapi_dl_tti_req_t *)(dlTtiElem +1);
787    
788    uint8_t pduCount = 0;
789
790 #ifdef ODU_SLOT_IND_DEBUG_LOG
791    if(dlTtiReq->nPdus == 0)
792    {
793       DU_LOG("\nDEBUG  -->  PHY_STUB: No PDU in DL TTI Request");
794    }
795    else
796    {  
797      DU_LOG("\nDEBUG  -->  PHY STUB: DL TTI Request at sfn=%d slot=%d",dlTtiReq->sfn,dlTtiReq->slot);
798    }
799 #endif 
800
801    for(pduCount=0; pduCount<dlTtiReq->nPdus; pduCount++)
802    {
803       if(dlTtiReq->pdus[pduCount].pduType == 3) //SSB_PDU_TYPE
804       {
805          DU_LOG("\nINFO   -->  PHY_STUB: SSB PDU");
806       }
807       else if(dlTtiReq->pdus[pduCount].pduType == 0)
808       {
809          DU_LOG("\nINFO   -->  PHY_STUB: PDCCH PDU");
810       }
811       else if(dlTtiReq->pdus[pduCount].pduType == 1)
812       {
813          DU_LOG("\nINFO   -->  PHY_STUB: PDSCH PDU");
814       }
815    }
816
817    /* Free FAPI message */
818    MAC_FREE(msg, msgLen);
819 #endif
820    return ROK;
821 }
822
823 /*******************************************************************
824  *
825  * @brief Handles tx_data request received from MAC
826  *
827  * @details
828  *
829  *    Function : l1HdlTxDataReq
830  *
831  *    Functionality:
832  *          -Handles tx_data request received from MAC
833  *
834  * @params[in]   Message length
835  *               tx_data request message pointer
836  *
837  * @return void
838  *
839  * ****************************************************************/
840
841 S16 l1HdlTxDataReq(uint16_t msgLen, void *msg)
842 {
843 #ifdef INTEL_FAPI
844    p_fapi_api_queue_elem_t txDataElem = (p_fapi_api_queue_elem_t)msg;
845    fapi_tx_data_req_t *txDataReq = (fapi_tx_data_req_t *)(txDataElem +1);
846
847    DU_LOG("\nINFO   -->  PHY STUB: TX DATA Request at sfn=%d slot=%d",txDataReq->sfn,txDataReq->slot);
848 /*
849    if(dlDedMsg)
850    {
851       DU_LOG("\nINFO   -->  PHY_STUB: TxDataPdu for DED MSG sent");
852       dlDedMsg = false;
853    }
854 */
855    MAC_FREE(msg, msgLen);
856 #endif
857    return ROK;
858 }
859
860 #ifdef INTEL_FAPI
861 /*******************************************************************
862  *
863  * @brief Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1
864  *
865  * @details
866  *
867  *    Function : fillPucchF0F1PduInfo
868  *
869  *    Functionality:
870  *       Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1 
871  *
872  * @params[in] fapi_uci_o_pucch_f0f1_t *
873  *             pucchPdu
874  * @return ROK     - success
875  *         RFAILED - failure
876  *
877  * ****************************************************************/
878 uint8_t fillPucchF0F1PduInfo(fapi_uci_o_pucch_f0f1_t *pduInfo, fapi_ul_pucch_pdu_t pucchPdu)
879 {
880    uint8_t idx = 0;
881
882    pduInfo->handle = pucchPdu.handle;
883    pduInfo->pduBitmap = 1;  //hardcoded for SR
884    pduInfo->pucchFormat = pucchPdu.formatType;
885    pduInfo->ul_cqi = 0;
886    pduInfo->rnti = pucchPdu.rnti;
887    pduInfo->timingAdvance = 0;
888    pduInfo->rssi = 0;
889    if(pduInfo->pduBitmap & SR_PDU_BITMASK)
890    {
891       pduInfo->srInfo.srIndication = SR_DETECTED;
892       pduInfo->srInfo.srConfidenceLevel = CONFDC_LEVEL_GOOD;
893    }
894    if(pduInfo->pduBitmap & HARQ_PDU_BITMASK)
895    {
896       pduInfo->harqInfo.numHarq++;
897       pduInfo->harqInfo.harqConfidenceLevel = CONFDC_LEVEL_GOOD;
898       for(idx = 0; idx < pduInfo->harqInfo.numHarq; idx++)
899       {
900          pduInfo->harqInfo.harqValue[idx] = HARQ_PASS;
901       }
902    }
903    return ROK;
904 }
905 /*******************************************************************
906  *
907  * @brief Fills UCI Pdu Information
908  *
909  * @details
910  *
911  *    Function : fillUciPduInfo
912  *
913  *    Functionality:
914  *       Fills UCI Pdu Information
915  *
916  * @params[in] Pointer to uciPdu
917  *             pucchPdu
918  * @return ROK     - success
919  *         RFAILED - failure
920  *
921  * ****************************************************************/
922 uint8_t fillUciPduInfo(fapi_uci_pdu_info_t *uciPdu, fapi_ul_pucch_pdu_t pucchPdu)
923 {
924    uint8_t ret = ROK;
925
926    /*TODO: The pduType is hardcoded here to support 
927      UCI Ind for PUCCH forat0/format1. This is to be
928      modified when we get SR form UE */
929    uciPdu->pduType = UCI_IND_PUCCH_F0F1;
930    switch(uciPdu->pduType)
931    {
932       case UCI_IND_PUSCH:
933          break;
934       case UCI_IND_PUCCH_F0F1:
935          {
936             fapi_uci_o_pucch_f0f1_t *pduInfo = NULLP;
937
938             pduInfo = &uciPdu->uci.uciPucchF0F1;
939             ret = fillPucchF0F1PduInfo(pduInfo, pucchPdu);
940             uciPdu->pduSize = sizeof(fapi_uci_o_pucch_f0f1_t);
941          }
942          break;
943       case UCI_IND_PUCCH_F2F3F4:
944          break;
945       default:
946          DU_LOG("\nERROR  -->  PHY_STUB: Invalid Pdu Type %d", uciPdu->pduType);
947          break;
948    }
949    return ret;
950 }
951
952 /*******************************************************************
953  *
954  * @brief Build and send Uci indication
955  *
956  * @details
957  *
958  *    Function : l1BuildAndSendUciInd
959  *
960  *    Functionality:
961  *       Build and send Uci indication
962  *
963  * @params[in] SFN
964  *             Slot
965  * @return ROK     - success
966  *         RFAILED - failure
967  *
968  * ****************************************************************/
969 uint8_t l1BuildAndSendUciInd(uint16_t slot, uint16_t sfn, fapi_ul_pucch_pdu_t pucchPdu)
970 {
971    uint8_t uciIdx = 0;
972    uint8_t nUciPdus = 0;
973    uint8_t ret = ROK;
974    uint32_t msgLen = 0;
975    fapi_uci_indication_t *uciInd =  NULLP;
976
977    MAC_ALLOC(uciInd, sizeof(fapi_uci_indication_t));
978    if(!uciInd)
979    {
980       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for UCI Indication");
981       return RFAILED;
982    }
983    memset(uciInd, 0, sizeof(fapi_uci_indication_t));
984    uciInd->sfn = sfn;
985    uciInd->slot = slot;
986    uciInd->numUcis = 1;   //consdering the UCI Ind for SR
987    nUciPdus = uciInd->numUcis;
988    while(nUciPdus)
989    {
990       ret = fillUciPduInfo(&uciInd->uciPdu[uciIdx], pucchPdu);
991       uciIdx++;
992       nUciPdus--;
993    }
994    if(ret == ROK)
995    {
996       msgLen = sizeof(fapi_uci_indication_t)- sizeof(fapi_msg_t);
997       fillMsgHeader(&uciInd->header, FAPI_UCI_INDICATION, msgLen);
998
999       /* Sending UCI indication to MAC */
1000       DU_LOG("\nINFO   -->  PHY STUB: Sending UCI Indication to MAC");
1001       procPhyMessages(uciInd->header.msg_id, sizeof(fapi_uci_indication_t), (void *)uciInd);
1002    }
1003    MAC_FREE(uciInd, sizeof(fapi_uci_indication_t));
1004    return ret;
1005 }
1006 #endif
1007
1008 /*******************************************************************
1009  *
1010  * @brief Handles Ul Tti request received from MAC
1011  *
1012  * @details
1013  *
1014  *    Function : l1HdlUlTtiReq
1015  *
1016  *    Functionality:
1017  *          -Handles Ul Tti request received from MAC
1018  *
1019  * @params[in]   Message length
1020  *               Ul Tti request message pointer
1021  *
1022  * @return void
1023  *
1024  * ****************************************************************/
1025
1026 S16 l1HdlUlTtiReq(uint16_t msgLen, void *msg)
1027 {
1028 #ifdef INTEL_FAPI
1029    p_fapi_api_queue_elem_t ulTtiElem = (p_fapi_api_queue_elem_t)msg;
1030    fapi_ul_tti_req_t *ulTtiReq = (fapi_ul_tti_req_t *)(ulTtiElem +1);
1031    uint8_t numPdus = ulTtiReq->nPdus;
1032
1033 #ifdef ODU_SLOT_IND_DEBUG_LOG
1034    if(numPdus == 0)
1035    {
1036       DU_LOG("\nINFO   -->  PHY STUB: No PDU received in UL TTI Req");
1037    }
1038    else
1039    {
1040       DU_LOG("\nINFO   -->  PHY STUB: Received UL TTI Request");
1041    }
1042 #endif 
1043
1044    while(numPdus)
1045    {
1046       if(ulTtiReq->pdus[numPdus-1].pduType == 0)
1047       {
1048          DU_LOG("\nINFO   -->  PHY STUB: PRACH PDU");
1049       }
1050       if(ulTtiReq->pdus[numPdus-1].pduType == 1)
1051       {
1052          DU_LOG("\nINFO   -->  PHY STUB: PUSCH PDU");
1053          l1BuildAndSendRxDataInd(ulTtiReq->slot, ulTtiReq->sfn, \
1054                ulTtiReq->pdus[numPdus-1].pdu.pusch_pdu); 
1055       }
1056       if(ulTtiReq->pdus[numPdus-1].pduType == 2)
1057       {
1058          DU_LOG("\nINFO   -->  PHY STUB: PUCCH PDU");
1059          fapi_ul_tti_req_t ulTtiSlotInd;
1060          memset(&ulTtiSlotInd, 0, sizeof(fapi_ul_tti_req_t));
1061          ulTtiSlotInd.slot = ulTtiReq->slot;
1062          ulTtiSlotInd.sfn  = ulTtiReq->sfn;
1063          ADD_DELTA_TO_TIME(ulTtiSlotInd, ulTtiSlotInd, SLOT_DELAY);
1064          l1BuildAndSendUciInd(ulTtiSlotInd.slot, ulTtiSlotInd.sfn, \
1065                ulTtiReq->pdus[numPdus-1].pdu.pucch_pdu);
1066       }
1067       numPdus--;
1068    }
1069
1070    /* TODO: [SFN:SLOT] at which RACH Indication is sent should be calculated
1071     * based on PRACH cfg index */
1072    /* Send RACH Ind to L2 for first UE */
1073    if(phyDb.ueDb.ueCb[UE_IDX_0].rachIndSent == false && ulTtiReq->sfn == 16 && ulTtiReq->slot == 6)
1074    {
1075       phyDb.ueDb.ueCb[UE_IDX_0].rachIndSent = true;
1076       l1BuildAndSendRachInd(ulTtiReq->slot, ulTtiReq->sfn);
1077       phyDb.ueDb.numActvUe++;
1078    }
1079 #if 0
1080    /* Send RACH Ind to L2 for second UE */
1081    if(phyDb.ueDb.ueCb[UE_IDX_1].rachIndSent == false && ulTtiReq->sfn == 304 && ulTtiReq->slot == 0)
1082    {
1083       phyDb.ueDb.ueCb[UE_IDX_1].rachIndSent = true;
1084       l1BuildAndSendRachInd(ulTtiReq->slot, ulTtiReq->sfn);
1085       phyDb.ueDb.numActvUe++;
1086    }
1087
1088    /* Send RACH Ind to L2 for third UE */
1089    if(phyDb.ueDb.ueCb[UE_IDX_2].rachIndSent == false && ulTtiReq->sfn == 526 && ulTtiReq->slot == 0)
1090    {
1091       phyDb.ueDb.ueCb[UE_IDX_2].rachIndSent = true;
1092       l1BuildAndSendRachInd(ulTtiReq->slot, ulTtiReq->sfn);
1093       phyDb.ueDb.numActvUe++;
1094    }
1095 #endif
1096    MAC_FREE(msg, msgLen);
1097 #endif
1098    return ROK;
1099 }
1100
1101 /*******************************************************************
1102  *
1103  * @brief Builds and Send the stop Indication message to MAC
1104  *
1105  * @details
1106  *
1107  *    Function : l1BuildAndSendStopInd
1108  *
1109  *    Functionality:
1110  *          -Send the Stop indication Message to MAC
1111  *
1112  *
1113  * @return void
1114  *
1115  * ****************************************************************/
1116 uint16_t l1BuildAndSendStopInd()
1117 {
1118 #ifdef INTEL_FAPI
1119    Pst pst;
1120    Buffer *mBuf = NULLP;
1121    fapi_stop_ind_t *stopIndMsg = NULLP;
1122    uint32_t msgLen = 0;
1123
1124    MAC_ALLOC_SHRABL_BUF(stopIndMsg, sizeof(fapi_stop_ind_t));
1125    if(!stopIndMsg)
1126    {
1127       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for stop Indication Message");
1128       return RFAILED;
1129    }
1130    else
1131    {
1132       fillMsgHeader(&stopIndMsg->header, FAPI_STOP_INDICATION, msgLen);
1133       DU_LOG("\n\nINFO   -->  PHY_STUB: Processing Stop indication to MAC");
1134
1135       memset(&pst, 0, sizeof(Pst));
1136       FILL_PST_PHY_STUB_TO_LWR_MAC(pst, EVT_PHY_STUB_STOP_IND);
1137       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
1138       if(!mBuf)
1139       {
1140          DU_LOG("\nERROR  --> PHY_STUB: Failed to allocate memory for slot indication buffer");
1141          MAC_FREE_SHRABL_BUF(pst.region, pst.pool, stopIndMsg, sizeof(fapi_stop_ind_t));
1142          return RFAILED;
1143       }
1144       CMCHKPK(oduPackPointer, (PTR)stopIndMsg, mBuf);
1145       ODU_POST_TASK(&pst, mBuf);
1146    }
1147 #endif
1148    return ROK;
1149 }
1150
1151 /*******************************************************************
1152  *
1153  * @brief Handles stop request received from MAC
1154  *
1155  * @details
1156  *
1157  *    Function : l1HdlStopReq
1158  *
1159  *    Functionality:
1160  *          -Handles stop request received from MAC
1161  *
1162  * @params[in]   Message length
1163  *               stop request message pointer
1164  *
1165  * @return void
1166  *
1167  * ****************************************************************/
1168
1169 S16 l1HdlStopReq(uint32_t msgLen, void *msg)
1170 {
1171 #ifdef INTEL_FAPI
1172    if(lwrMacCb.phyState == PHY_STATE_RUNNING)
1173    {
1174       l1HdlSlotIndicaion(TRUE);
1175
1176       /* Initialize all global variables */
1177       sfnValue = 0;
1178       slotValue = 0;
1179       memset(&phyDb.ueDb, 0, sizeof(UeDb));
1180
1181       DU_LOG("\nINFO   -->  PHY_STUB: Slot Indication is stopped successfully");
1182       MAC_FREE(msg, msgLen);
1183    }
1184    else
1185    {
1186       DU_LOG("\nINFO  -->  PHY_STUB: Received Stop Req in PHY State %d", lwrMacCb.phyState);
1187       MAC_FREE(msg, msgLen);
1188       return RFAILED;
1189    }
1190 #endif
1191    return ROK;
1192 }
1193
1194 #if 0
1195 /*******************************************************************
1196  *
1197  * @brief Build And Send Rx Data Ind for Msg5
1198  *
1199  * @details
1200  *
1201  *    Function : l1BuildAndSendMsg5
1202  *
1203  *    Functionality: Build And Send Rx Data Ind for Msg5
1204  *
1205  * @params[in] SFN
1206  *             Slot
1207  * @return ROK     - success
1208  *         RFAILED - failure
1209  *
1210  * ****************************************************************/
1211 uint8_t l1BuildAndSendMsg5(uint16_t sfn, uint16_t slot)
1212 {   
1213 #ifdef INTEL_FAPI
1214    uint8_t idx = 0;
1215    fapi_rx_data_indication_t *rxDataInd;
1216    fapi_pdu_ind_info_t       *pduInfo;
1217    uint8_t  *pdu;
1218    uint16_t byteIdx = 0;
1219    uint32_t msgLen = 0;
1220
1221    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1222    if(!rxDataInd)
1223    {
1224       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication for msg5");
1225       return RFAILED;
1226    }
1227    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1228
1229    /* TODO: Fill the required values */
1230    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1231    rxDataInd->sfn = sfn;
1232    rxDataInd->slot = slot;
1233    rxDataInd->numPdus = 1;
1234
1235    pduInfo = &rxDataInd->pdus[idx];
1236    pduInfo->handle = 100;
1237    pduInfo->rnti = 100;
1238    pduInfo->harqId = 1;
1239    pduInfo->pdu_length = 25;
1240    pduInfo->ul_cqi = 0;
1241    pduInfo->timingAdvance = 0;
1242    pduInfo->rssi = 0;
1243
1244    /* Filling pdu with random values for testing */
1245    pduInfo->pduData = NULL;
1246    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1247    if(!pduInfo->pduData)
1248    {
1249       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1250       return RFAILED;
1251    }
1252
1253    /* Filling PDU */
1254    pdu = (uint8_t *)pduInfo->pduData;
1255
1256    uint8_t  msg5PduLen = 33;
1257    /* For RRC setup complete
1258       MAC subheader format is R/F/LCId/L (2/3 bytes)
1259       LCId is 1 for SRB1
1260       L is length of PDU i.e 6bytes here
1261       From 38.321 section 6.1.1
1262     */
1263    uint8_t msg5[] = {1, msg5PduLen, 0, 0, 16, 0, 5, 223, 128, 16, 94, \
1264       64, 3, 64, 89, 61, 138, 64, 0, 0, 0, 4, 0, 0, 4, 68, 11, 128, \
1265          184, 56, 0, 0, 0, 0, 0};
1266
1267    msg5PduLen += 2;  /* 2bytes of header */
1268    memcpy(pdu, &msg5, msg5PduLen);
1269    byteIdx += msg5PduLen; /* 2 bytes of header */
1270
1271    /* Filling MAC SDU for Padding bytes*/
1272    if(byteIdx < pduInfo->pdu_length)
1273    {
1274       /* For Padding
1275          MAC subheader format is R/R/LCId (1byte)
1276          LCId is 63 for padding
1277          From 38.321 section 6.1.1
1278        */
1279       pdu[byteIdx++] = 63;
1280
1281       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1282          pdu[byteIdx] = 0;
1283    }
1284    msgLen += pduInfo->pdu_length;
1285
1286    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1287
1288    /* Sending Rx data indication to MAC */
1289    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
1290    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1291
1292    if(pduInfo->pdu_length)
1293       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1294    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1295 #endif
1296    return ROK;
1297 }
1298 #endif
1299
1300 /*******************************************************************
1301  *
1302  * @brief Handles Ul Dci request received from MAC
1303  *
1304  * @details
1305  *
1306  *    Function : l1HdlUlDciReq
1307  *
1308  *    Functionality:
1309  *          -Handles Ul Dci request received from MAC
1310  *
1311  * @params[in]   Message length
1312  *               Ul Dci request message pointer
1313  *
1314  * @return void
1315  *
1316  * ****************************************************************/
1317
1318 S16 l1HdlUlDciReq(uint16_t msgLen, void *msg)
1319 {
1320 #ifdef INTEL_FAPI
1321    p_fapi_api_queue_elem_t ulDciElem = (p_fapi_api_queue_elem_t)msg;
1322    fapi_ul_dci_req_t *ulDciReq = (fapi_ul_dci_req_t *)(ulDciElem +1);
1323    uint8_t numPdus = ulDciReq->numPdus;
1324
1325    while(numPdus)
1326    {
1327       if(ulDciReq->pdus[numPdus-1].pduType == 0)
1328       {
1329          DU_LOG("\nINFO   -->  PHY STUB: Received UL DCI Request for PDCCH PDU");
1330          //l1BuildAndSendMsg5(ulDciReq->sfn, ulDciReq->slot);
1331          //msg5Sent = true;
1332       }
1333       numPdus--;
1334    }
1335
1336    MAC_FREE(msg, msgLen);
1337 #endif
1338    return ROK;
1339 }
1340
1341 /*******************************************************************
1342  *
1343  * @brief Send UL user data to DU
1344  *
1345  * @details
1346  *
1347  *    Function : l1SendUlUserData
1348  *
1349  *    Functionality: Send UL user data to DU
1350  *
1351  * @params[in]
1352  * @return ROK     - success
1353  *         RFAILED - failure
1354  *
1355  * ****************************************************************/
1356 uint8_t l1SendUlUserData(uint8_t drbId, uint8_t ueIdx)
1357 {
1358    uint8_t cnt = 0;
1359    fapi_rx_data_indication_t *rxDataInd;
1360    fapi_pdu_ind_info_t       *pduInfo;
1361    uint8_t  *pdu = NULLP;
1362    uint16_t byteIdx = 0;
1363    uint32_t msgLen = 0;
1364    uint8_t idx = 0;
1365    uint8_t lcId = 0;
1366
1367    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1368    if(!rxDataInd)
1369    {
1370       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1371       return RFAILED;
1372    }
1373    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1374
1375    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1376    rxDataInd->sfn = 0;
1377    rxDataInd->slot = 0;
1378    rxDataInd->numPdus = 1;
1379
1380    /* TODO : Fill pduInfo using PUSCH PDU. Currently hardcoding */
1381    pduInfo = &rxDataInd->pdus[idx];
1382    pduInfo->handle = ueIdx + ODU_START_CRNTI;
1383    pduInfo->rnti = ueIdx + ODU_START_CRNTI;
1384    pduInfo->harqId = 1;
1385    /* Since user data size = 50bytes and 2 bytes of MAC header and 3 byte of RLC header, 
1386     * setting tbsize = 56 from Table 5.1.3.2-1 spec 38.214 */
1387    pduInfo->pdu_length = 56;
1388    pduInfo->ul_cqi = 0;
1389    pduInfo->timingAdvance = 0;
1390    pduInfo->rssi = 0;
1391
1392    /* Filling pdu with random values for testing */
1393    pduInfo->pduData = NULL;
1394    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1395    if(!pduInfo->pduData)
1396    {
1397       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1398       return RFAILED;
1399    }
1400
1401    /* Filling PDU */
1402    pdu = (uint8_t *)pduInfo->pduData;
1403    msgLen = 52;
1404
1405    /* For UL User data
1406       MAC subheader format is R/F/LCId/L (2/3 bytes)
1407       LCId is 4 for DRB1
1408       L is length of PDU i.e 50 bytes
1409       From 38.321 section 6.1.1
1410       From 38.322 section 6.2.2.4, 6.2.2.3 for AM, UM Header
1411     */
1412
1413     /* Below ulMsg supports 12bit SN for UM mode */
1414                                /*  SI  SN */
1415     lcId = MIN_DRB_LCID + drbId;
1416     uint8_t ulMsg[] = {lcId, msgLen,   0, 0, 0, 0, 0, 50, 0, 0, 0, 0, 0, 1, 0, 0, 192, 168, 130, 81, 192, 168, 130, 82, 84, 104,
1417     105, 115, 32, 105, 115, 32, 69, 71, 84, 80, 32, 100, 97, 116, 97, 32, 102, 114, 111, 109, 32, 68, 85, 0, 0, 0, 0, 0};
1418     msgLen += 2;  /* 2bytes of header */
1419     memcpy(pdu, &ulMsg, msgLen);
1420     byteIdx += msgLen; /* 2 bytes of header */
1421
1422    /* Filling MAC SDU for Padding bytes*/
1423    if(byteIdx < pduInfo->pdu_length)
1424    {
1425       /* For Padding
1426          MAC subheader format is R/R/LCId (1byte)
1427          LCId is 63 for padding
1428          From 38.321 section 6.1.1
1429        */
1430       pdu[byteIdx++] = 63;
1431
1432       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1433          pdu[byteIdx] = 0;
1434    }
1435    msgLen += pduInfo->pdu_length;
1436
1437    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1438
1439     /* Send Message to peer */
1440     while(cnt < NUM_UL_PACKETS)
1441     {
1442        DU_LOG("\nDEBUG  -->  PHY STUB : Sending UL User Data[%d][LCID:%d] at sfn %d slot %d", cnt+1, lcId, sfnValue, slotValue);
1443        /* Sending Rx data indication to MAC */
1444        rxDataInd->sfn = sfnValue;
1445        rxDataInd->slot = slotValue;
1446        procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1447        cnt++;
1448     }
1449
1450     if(pduInfo->pdu_length)
1451        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1452     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1453     return ROK;
1454 }
1455
1456 /*******************************************************************
1457  *
1458  * @brief Sends RLC Status PDU to DU
1459  *
1460  * @details
1461  *
1462  *    Function : l1SendStatusPdu
1463  *
1464  *    Functionality: Send RLC Status PDU to DU
1465  *
1466  * @params[in]
1467  * @return ROK     - success
1468  *         RFAILED - failure
1469  *
1470  * ****************************************************************/
1471 uint8_t l1SendStatusPdu()
1472 {
1473    fapi_rx_data_indication_t *rxDataInd;
1474    fapi_pdu_ind_info_t       *pduInfo;
1475    uint8_t  *pdu = NULLP;
1476    uint16_t byteIdx = 0;
1477    uint32_t msgLen = 0;
1478    uint8_t idx = 0;
1479
1480    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1481    if(!rxDataInd)
1482    {
1483       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1484       return RFAILED;
1485    }
1486    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1487
1488    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1489    rxDataInd->sfn = 0;
1490    rxDataInd->slot = 0;
1491    rxDataInd->numPdus = 1;
1492
1493    /* TODO : Fill pduInfo using PUSCH PDU. Currently hardcoding */
1494    pduInfo = &rxDataInd->pdus[idx];
1495    pduInfo->handle = 100;
1496    pduInfo->rnti = 100;
1497    pduInfo->harqId = 1;
1498    /* Since status pdu size = 3bytes and 2 bytes of MAC header,
1499     * setting tbsize = 24 from Table 5.1.3.2-1 spec 38.214 */
1500    pduInfo->pdu_length = 24;
1501    pduInfo->ul_cqi = 0;
1502    pduInfo->timingAdvance = 0;
1503    pduInfo->rssi = 0;
1504
1505    /* Filling pdu with random values for testing */
1506    pduInfo->pduData = NULL;
1507    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1508    if(!pduInfo->pduData)
1509    {
1510       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1511       return RFAILED;
1512    }
1513
1514    /* Filling PDU */
1515    pdu = (uint8_t *)pduInfo->pduData;
1516    msgLen = 3;
1517
1518    /* For RLC Status PDU
1519     * MAC subheader format is R/F/LCId/L (2/3 bytes)
1520     * LCId is 4 for DRB1
1521     * L is length of Status PDU i.e 3 bytes
1522     * From 38.321 section 6.1.1
1523     *
1524     * RLC Status PDU format :
1525     * ---------------------------------
1526     * |D/C|CPT|    ACK_SN             |
1527     * ---------------------------------
1528     * |            ACK_SN             |
1529     * ---------------------------------
1530     * |E1 | R | R | R | R | R | R | R |
1531     * ---------------------------------
1532     *  
1533     * Here, D/C bit = 0 for Control PDU
1534     * CPT bit = 0 since control pdu type is Status PDU
1535     * ACK_SN = 0 as of now, this can be changed based on the 
1536     * scenario being tested
1537     * E1 = 0 because no NACK_SN, E1, E2 and E3 follows it
1538     * R = 0 i.e. reserved bits
1539     * From Spec 38.322 section 6.2.2.5
1540     */
1541    uint8_t statusPdu[] = {4, msgLen, 0, 0, 0};
1542    msgLen += 2;  /* 2bytes of header */
1543    memcpy(pdu, &statusPdu, msgLen);
1544    byteIdx += msgLen; /* 2 bytes of header */
1545
1546
1547    /* Filling MAC SDU for Padding bytes*/
1548    if(byteIdx < pduInfo->pdu_length)
1549    {
1550       /* For Padding
1551          MAC subheader format is R/R/LCId (1byte)
1552          LCId is 63 for padding
1553          From 38.321 section 6.1.1
1554        */
1555       pdu[byteIdx++] = 63;
1556
1557       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1558          pdu[byteIdx] = 0;
1559    }
1560    msgLen += pduInfo->pdu_length;
1561
1562    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1563
1564     /* Send Message to peer */
1565     DU_LOG("\nDEBUG  -->  PHY STUB : Sending RLC status pdu at sfn %d slot %d", sfnValue, slotValue);
1566     /* Sending Rx data indication to MAC */
1567     rxDataInd->sfn = sfnValue;
1568     rxDataInd->slot = slotValue;
1569     procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1570
1571     if(pduInfo->pdu_length)
1572        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1573     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1574     return ROK;
1575 }
1576
1577 /*******************************************************************
1578  *
1579  * @brief Receives message from MAC
1580  *
1581  * @details
1582  *
1583  *    Function :  l1ProcessFapiRequest
1584  *
1585  *    Functionality:
1586  *       - Receives message from MAC and calls handler
1587  *
1588  * @params[in] Message type
1589  *             Message length
1590  *             Message pointer
1591  *
1592  * @return void
1593  *
1594  * ****************************************************************/
1595
1596 void l1ProcessFapiRequest(uint8_t msgType, uint32_t msgLen, void *msg)
1597 {
1598    switch(msgType)
1599    {
1600 #ifdef INTEL_FAPI
1601       case FAPI_PARAM_REQUEST:
1602          l1HdlParamReq(msgLen, msg);
1603          break;
1604       case FAPI_CONFIG_REQUEST:
1605          l1HdlConfigReq(msgLen, msg);
1606          break;
1607       case FAPI_START_REQUEST:
1608          l1HdlStartReq(msgLen, msg);
1609          break;
1610       case FAPI_DL_TTI_REQUEST:
1611          l1HdlDlTtiReq(msgLen, msg);
1612          break;
1613       case FAPI_TX_DATA_REQUEST:
1614          l1HdlTxDataReq(msgLen, msg);
1615          break;
1616       case FAPI_UL_TTI_REQUEST:
1617          l1HdlUlTtiReq(msgLen, msg);
1618          break;
1619       case FAPI_STOP_REQUEST:
1620          l1HdlStopReq(msgLen, msg);
1621          break;
1622       case FAPI_UL_DCI_REQUEST:
1623          l1HdlUlDciReq(msgLen, msg);
1624          break;
1625       default:
1626          DU_LOG("\nERROR  -->  PHY_STUB: Invalid message type[%x] received at PHY", msgType);
1627          break;
1628 #endif
1629    }
1630 }
1631
1632 #ifdef INTEL_FAPI
1633 /*******************************************************************
1634  *
1635  * @brief Builds and Send the BSR message to MAC
1636  *
1637  * @details
1638  *
1639  *    Function : l1BuildAndSendBSR
1640  *
1641  *   Functionality:
1642  *          -Send the BSR Message to MAC
1643  *
1644  * @params[in]  BSR type 
1645  *              array of LCGID and BSIdx
1646  * @return void
1647  *
1648  *****************************************************************/
1649 uint16_t l1BuildAndSendBSR(uint8_t ueIdx, BsrType bsrType,\
1650              LcgBufferSize lcgBsIdx[MAX_NUM_LOGICAL_CHANNEL_GROUPS])
1651 {
1652    fapi_rx_data_indication_t *rxDataInd;
1653    fapi_pdu_ind_info_t       *pduInfo;
1654    uint8_t  *pdu = NULLP;
1655    uint16_t byteIdx = 0;
1656    uint32_t msgLen = 0;
1657    uint8_t pduIdx = 0, lcgIdx = 0, lcgIdxPos = 0;
1658
1659    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1660    if(!rxDataInd)
1661    {
1662       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1663       return RFAILED;
1664    }
1665    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1666
1667    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1668    rxDataInd->sfn = 0;
1669    rxDataInd->slot = 0;
1670    rxDataInd->numPdus = 1;
1671
1672    pduInfo = &rxDataInd->pdus[pduIdx];
1673    pduInfo->handle = (ODU_START_CRNTI + ueIdx);
1674    pduInfo->rnti = (ODU_START_CRNTI + ueIdx);
1675    pduInfo->harqId = 1;
1676
1677    /* Since status pdu size = 3bytes and 2 bytes of MAC header,
1678     * setting tbsize = 24 from Table 5.1.3.2-1 spec 38.214 */
1679    pduInfo->pdu_length = 24;
1680    pduInfo->ul_cqi = 0;
1681    pduInfo->timingAdvance = 0;
1682    pduInfo->rssi = 0;
1683
1684    /* Filling pdu with random values for testing */
1685    pduInfo->pduData = NULL;
1686    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1687    if(!pduInfo->pduData)
1688    {
1689       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1690       MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1691       return RFAILED;
1692    }
1693
1694    /* Filling PDU */
1695    pdu = (uint8_t *)pduInfo->pduData;
1696
1697    switch(bsrType)
1698    {
1699       case SHORT_BSR:
1700          {
1701             DU_LOG("\nDEBUG  -->  PHY_STUB: Forming SHORT BSR PDU ");
1702
1703             /* For Short BSR
1704              * MAC subheader format is R/R/LcId (1Byte)
1705              * LCId is 61
1706              * From 38.321 section 6.1.1
1707              */
1708             pdu[byteIdx++] = 61;    // LCID
1709             pdu[byteIdx++] = (lcgBsIdx[0].lcgId << 5) | lcgBsIdx[0].bsIdx;
1710             break;
1711          }
1712
1713       case LONG_BSR:
1714          {
1715             DU_LOG("\nDEBUG  -->  PHY_STUB: Forming LONG BSR PDU ");
1716
1717             /* For Long BSR
1718              * MAC subheader format is R/R/LcId (1Byte)
1719              * LCId is 62
1720              * From 38.321 section 6.1.1
1721              */
1722             pdu[byteIdx++] = 62;    // LCID
1723
1724             /*Octet where lcgId bitmap will be present*/
1725             lcgIdxPos = byteIdx;
1726             byteIdx++;
1727             for(lcgIdx = 0; lcgIdx < MAX_NUM_LOGICAL_CHANNEL_GROUPS; lcgIdx++)
1728             {
1729                if(lcgBsIdx[lcgIdx].bsIdx > 0)
1730                {
1731                   pdu[lcgIdxPos] |= 1 << lcgBsIdx[lcgIdx].lcgId;
1732                   pdu[byteIdx++] = lcgBsIdx[lcgIdx].bsIdx;
1733                }
1734             }
1735
1736             break;
1737          }
1738
1739       default:
1740          {
1741             DU_LOG("\nERROR  -->  PHY_STUB: Incorrect BSR type:%d!", bsrType);
1742             if(pduInfo->pdu_length)
1743                MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1744             MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1745             return RFAILED;
1746          }
1747    }
1748    /* Filling MAC SDU for Padding bytes*/
1749    if(byteIdx < pduInfo->pdu_length)
1750    {
1751       /* For Padding
1752        * MAC subheader format is R/R/LCId (1byte)
1753        * LCId is 63 for padding
1754        * From 38.321 section 6.1.1
1755        */
1756       pdu[byteIdx++] = 63;
1757
1758       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1759          pdu[byteIdx] = 0;
1760    }
1761    msgLen += pduInfo->pdu_length;
1762    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1763
1764    /* Sending Rx data indication to MAC */
1765    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
1766    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1767
1768    if(pduInfo->pdu_length)
1769       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1770    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1771    return ROK;
1772 }
1773 #endif
1774
1775 /**********************************************************************
1776   End of file
1777  **********************************************************************/