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