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