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