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