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