ebe33a3a92f7eb08a921c619b1d1966743befeb7
[o-du/l2.git] / src / phy_stub / phy_stub_msg_hdl.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /*This file contains stub for PHY to handle messages to/from MAC CL */
20
21 #include "common_def.h"
22 #include "lrg.h"
23 #include "lrg.x"
24 #include "du_app_mac_inf.h"
25 #include "mac_sch_interface.h"
26 #include "lwr_mac.h"
27 #include "lwr_mac_fsm.h"
28 #include "lwr_mac_phy.h"
29 #ifdef INTEL_FAPI
30 #include "fapi.h"
31 #include "fapi_vendor_extension.h"
32 #endif
33 #include "lwr_mac_upr_inf.h"
34 #include "mac_utils.h"
35 #include "phy_stub.h"
36 #include "phy_stub_utils.h"
37 #include "lwr_mac_phy_stub_inf.h"
38
39 /*******************************************************************
40  *
41  * @brief Builds and sends param response to MAC CL
42  *
43  * @details
44  *
45  *    Function : l1BldAndSndParamRsp
46  *
47  *    Functionality:
48  *          - Builds and sends param response to MAC
49  *
50  * @params[in] Config request message pointer
51  * @return ROK     - success
52  *         RFAILED - failure
53  *
54  * ****************************************************************/
55 S16 l1BldAndSndParamRsp(void *msg)
56 {
57 #ifdef INTEL_FAPI
58    uint8_t index = 0;
59    uint32_t msgLen = 0;
60    fapi_param_resp_t *fapiParamRsp;
61
62    MAC_ALLOC(fapiParamRsp, sizeof(fapi_param_resp_t));
63    if(!fapiParamRsp)
64    {
65       DU_LOG("\nERROR  -->  PHY STUB: Memory allocation failed");
66       return RFAILED;
67    }
68
69    /* Cell Params */
70    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_RELEASE_CAPABILITY_TAG,                         sizeof(uint16_t), 1, &msgLen);
71    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PHY_STATE_TAG,                                  sizeof(uint16_t), 0, &msgLen);
72    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SKIP_BLANK_DL_CONFIG_TAG,                       sizeof(uint8_t),  0, &msgLen);
73    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SKIP_BLANK_UL_CONFIG_TAG,                       sizeof(uint8_t),  0, &msgLen);
74    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_NUM_CONFIG_TLVS_TO_REPORT_TYPE_TAG,              sizeof(uint8_t),  0, &msgLen);
75
76    /* Carrier Params */
77    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CYCLIC_PREFIX_TAG,                               sizeof(uint8_t),  1, &msgLen);
78    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_SUBCARRIER_SPACING_DL_TAG,            sizeof(uint8_t),  1, &msgLen);
79    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_BANDWIDTH_DL_TAG,                     sizeof(uint16_t), 1, &msgLen);
80    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_SUBCARRIER_SPACING_UL_TAG,            sizeof(uint8_t),  0, &msgLen);
81    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_BANDWIDTH_UL_TAG,                     sizeof(uint16_t), 0, &msgLen);
82
83    /* PDCCH Param*/
84    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CCE_MAPPING_TYPE_TAG,                                   sizeof(uint8_t), 0, &msgLen);
85    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CORESET_OUTSIDE_FIRST_3_OFDM_SYMS_OF_SLOT_TAG,   sizeof(uint8_t), 0, &msgLen);
86    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRECODER_GRANULARITY_CORESET_TAG,               sizeof(uint8_t), 0, &msgLen);
87    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDCCH_MU_MIMO_TAG,                              sizeof(uint8_t), 0, &msgLen);
88    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDCCH_PRECODER_CYCLING_TAG,                     sizeof(uint8_t), 0, &msgLen);
89    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDCCHS_PER_SLOT_TAG,                                sizeof(uint8_t), 0, &msgLen);
90
91    /* PUCCH Param */
92    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUCCH_FORMATS_TAG,                              sizeof(uint8_t), 0, &msgLen);
93    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PUCCHS_PER_SLOT_TAG,                         sizeof(uint8_t), 0, &msgLen);
94
95    /* PDSCH Param */
96    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_MAPPING_TYPE_TAG,                         sizeof(uint8_t), 0, &msgLen);
97    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_ALLOCATION_TYPES_TAG,                      sizeof(uint8_t), 0, &msgLen);
98    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_VRB_TO_PRB_MAPPING_TAG,                   sizeof(uint8_t), 0, &msgLen);
99    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_CBG_TAG,                                  sizeof(uint8_t), 0, &msgLen);
100    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_CONFIG_TYPES_TAG,                    sizeof(uint8_t), 0, &msgLen);
101    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_MAX_LENGTH_TAG,                      sizeof(uint8_t), 0, &msgLen);
102    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_ADDITIONAL_POS_TAG,                  sizeof(uint8_t), 0, &msgLen);
103    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDSCHS_TBS_PER_SLOT_TAG,                    sizeof(uint8_t), 0, &msgLen);
104    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_NUMBER_MIMO_LAYERS_PDSCH_TAG,               sizeof(uint8_t), 0, &msgLen);
105    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_MAX_MODULATION_ORDER_DL_TAG,          sizeof(uint8_t), 0, &msgLen);
106    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_MU_MIMO_USERS_DL_TAG,                       sizeof(uint8_t), 0, &msgLen);
107    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DATA_IN_DMRS_SYMBOLS_TAG,                 sizeof(uint8_t), 0, &msgLen);
108    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PREMPTIONSUPPORT_TAG,                           sizeof(uint8_t), 0, &msgLen);
109    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_NON_SLOT_SUPPORT_TAG,                      sizeof(uint8_t), 0, &msgLen);
110
111    /* PUSCH Param */
112    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_UCI_MUX_ULSCH_IN_PUSCH_TAG,                     sizeof(uint8_t), 0, &msgLen);
113    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_UCI_ONLY_PUSCH_TAG,                             sizeof(uint8_t), 0, &msgLen);
114    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_FREQUENCY_HOPPING_TAG,                    sizeof(uint8_t), 0, &msgLen);
115    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_CONFIG_TYPES_TAG,                    sizeof(uint8_t), 0, &msgLen);
116    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_MAX_LEN_TAG,                         sizeof(uint8_t), 0, &msgLen);
117    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_ADDITIONAL_POS_TAG,                  sizeof(uint8_t), 0, &msgLen);
118    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_CBG_TAG,                                  sizeof(uint8_t), 0, &msgLen);
119    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_MAPPING_TYPE_TAG,                          sizeof(uint8_t), 0, &msgLen);
120    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_ALLOCATION_TYPES_TAG,                     sizeof(uint8_t), 0, &msgLen);
121    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_VRB_TO_PRB_MAPPING_TAG,                   sizeof(uint8_t), 0, &msgLen);
122    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_MAX_PTRS_PORTS_TAG,                        sizeof(uint8_t), 0, &msgLen);
123    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDUSCHS_TBS_PER_SLOT_TAG,                   sizeof(uint8_t), 0, &msgLen);
124    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_NUMBER_MIMO_LAYERS_NON_CB_PUSCH_TAG,         sizeof(uint8_t), 0, &msgLen);
125    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_MODULATION_ORDER_UL_TAG,               sizeof(uint8_t), 0, &msgLen);
126    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_MU_MIMO_USERS_UL_TAG,                        sizeof(uint8_t), 0, &msgLen);
127    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_DFTS_OFDM_SUPPORT_TAG,                           sizeof(uint8_t), 0, &msgLen);
128    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_AGGREGATION_FACTOR_TAG,                    sizeof(uint8_t), 0, &msgLen);
129
130    /* PRACH Params */
131    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_LONG_FORMATS_TAG,                         sizeof(uint8_t), 0, &msgLen);
132    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_SHORT_FORMATS_TAG,                         sizeof(uint8_t), 0, &msgLen);
133    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_RESTRICTED_SETS_TAG,                       sizeof(uint8_t), 0, &msgLen);
134    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PRACH_FD_OCCASIONS_IN_A_SLOT_TAG,            sizeof(uint8_t), 0, &msgLen);
135
136    /* MEASUREMENT TAG */
137    fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_RSSI_MEASUREMENT_SUPPORT_TAG,                    sizeof(uint8_t), 0, &msgLen);
138
139    fapiParamRsp->number_of_tlvs = index;
140    msgLen += sizeof(fapi_param_resp_t) - sizeof(fapi_msg_t);
141    fillMsgHeader(&fapiParamRsp->header, FAPI_PARAM_RESPONSE, msgLen);
142    fapiParamRsp->error_code = MSG_OK;
143
144    DU_LOG("\nINFO   -->  PHY_STUB: Sending Param Request to Lower Mac");
145    procPhyMessages(fapiParamRsp->header.msg_id, sizeof(fapi_param_resp_t), (void *)fapiParamRsp);
146    MAC_FREE(fapiParamRsp, sizeof(fapi_param_resp_t));
147 #endif
148    return ROK;
149 }
150
151 /*******************************************************************
152  *
153  * @brief Builds and sends config response to lower mac
154  *
155  * @details
156  *
157  *    Function : l1BldAndSndConfigRsp
158  *
159  *    Functionality:
160  *          - Builds and sends config response to MAC
161  *
162  * @params[in] Config request message pointer
163  * @return ROK     - success
164  *         RFAILED - failure
165  *
166  * ****************************************************************/
167
168 S16 l1BldAndSndConfigRsp(void *msg)
169 {
170 #ifdef INTEL_FAPI
171    uint32_t msgLen = 0;
172    fapi_config_resp_t *fapiConfigRsp;
173
174    MAC_ALLOC(fapiConfigRsp, sizeof(fapi_config_resp_t));
175    if(!fapiConfigRsp)
176    {
177       DU_LOG("\nERROR  -->  PHY STUB: Memory allocation failed");
178       return RFAILED;
179    }
180    memset(fapiConfigRsp, 0, sizeof(fapi_config_resp_t));
181    fapiConfigRsp->number_of_invalid_tlvs = NULLP;
182    fapiConfigRsp->number_of_inv_tlvs_idle_only = NULLP;
183    fapiConfigRsp->number_of_missing_tlvs = NULLP;
184    fapiConfigRsp->error_code = MSG_OK;
185    msgLen = sizeof(fapi_config_resp_t) - sizeof(fapi_msg_t);
186    fillMsgHeader(&fapiConfigRsp->header, FAPI_CONFIG_RESPONSE, msgLen);
187
188    DU_LOG("\nINFO   -->  PHY_STUB: Sending Config Response to Lower Mac");
189
190    procPhyMessages(fapiConfigRsp->header.msg_id, \
191          sizeof(fapi_config_resp_t), (void *)fapiConfigRsp);
192    MAC_FREE(fapiConfigRsp, sizeof(fapi_config_resp_t));
193 #endif
194    return ROK;
195 }
196 /*******************************************************************
197  *
198  * @brief Handles param request received from MAC
199  *
200  * @details
201  *
202  *    Function : l1HdlParamReq
203  *
204  *    Functionality:
205  *          -Handles param request received from MAC
206  *
207  * @params[in]   Message length
208  *               Param request message pointer
209  *
210  * @return void
211  *
212  * ****************************************************************/
213
214 void l1HdlParamReq(uint32_t msgLen, void *msg)
215 {
216 #ifdef INTEL_FAPI
217    DU_LOG("\nINFO   -->  PHY_STUB: Received Param Request in PHY");
218
219    /* Build and send PARAM RESPONSE */
220    if(l1BldAndSndParamRsp(msg)!= ROK)
221    {
222       DU_LOG("\nERROR  -->  PHY_STUB: Failed Sending Param Response");
223    }
224    MAC_FREE(msg, sizeof(fapi_param_req_t));
225 #endif
226
227
228 /*******************************************************************
229  *
230  * @brief Handles config request received from MAC
231  *
232  * @details
233  *
234  *    Function : l1HdlConfigReq
235  *
236  *    Functionality:
237  *          -Handles config request received from MAC
238  *
239  * @params[in]   Message length
240  *               config request message pointer
241  *
242  * @return void
243  *
244  * ****************************************************************/
245
246 void l1HdlConfigReq(uint32_t msgLen, void *msg)
247 {
248    rachIndSent = false;
249    msg3Sent = false;
250    msg5ShortBsrSent = false;
251    msg5Sent = false;
252    dlDedMsg = false;
253    msgSecurityModeComp =  false;
254    msgRrcReconfiguration  =  false;
255    msgRegistrationComp    = false;
256
257 #ifdef INTEL_FAPI
258    p_fapi_api_queue_elem_t configReqElem = (p_fapi_api_queue_elem_t)msg;
259    fapi_config_req_t *configReq = (fapi_config_req_t *)(configReqElem +1);
260
261    DU_LOG("\nINFO   -->  PHY_STUB: Received Config Request in PHY");
262
263    /* Handling CONFIG RESPONSE */
264    if(l1BldAndSndConfigRsp(configReq)!= ROK)
265    {
266       DU_LOG("\nERROR  -->  PHY_STUB: Failed Sending config Response");
267    }
268
269    MAC_FREE(msg, msgLen);
270 #endif
271
272 }
273
274 /*******************************************************************
275  *
276  * @brief Build and Send CRC Indication
277  *
278  * @details
279  *
280  *    Function : l1BuildAndSendCrcInd
281  *
282  *    Functionality:
283  *      Build and Send CRC Indication
284  *
285  * @params[in] Slot
286  *             SFN 
287  * @return ROK     - success
288  *         RFAILED - failure
289  *
290  * ****************************************************************/
291 uint16_t l1BuildAndSendCrcInd(uint16_t slot, uint16_t sfn)
292 {
293 #ifdef INTEL_FAPI
294    uint8_t idx = 0;
295    fapi_crc_ind_t  *crcInd;
296
297    MAC_ALLOC(crcInd, sizeof(fapi_crc_ind_t));
298    if(!crcInd)
299    {
300       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for CRC Indication Message");
301       return RFAILED;
302    }
303    memset(crcInd, 0, sizeof(fapi_crc_ind_t));
304
305    /* TODO: Fill the required values. As of now only 1 CRC status PASS is filled */
306    crcInd->sfn = sfn;
307    crcInd->slot = slot;
308    crcInd->numCrcs = 1;
309
310    crcInd->crc[idx].handle = 0;
311    crcInd->crc[idx].rnti = 0;
312    crcInd->crc[idx].harqId = 0;
313    crcInd->crc[idx].tbCrcStatus = 0;
314    crcInd->crc[idx].numCb = 1;
315    crcInd->crc[idx].cbCrcStatus[0] = 0;
316    crcInd->crc[idx].ul_cqi = 0;
317    crcInd->crc[idx].timingAdvance = 0;
318    crcInd->crc[idx].rssi = 0;
319
320    fillMsgHeader(&crcInd->header, FAPI_CRC_INDICATION, \
321          sizeof(fapi_crc_ind_t) - sizeof(fapi_msg_t));
322
323    /* Sending RACH indication to MAC */
324    DU_LOG("\nINFO   -->  PHY STUB: Sending CRC Indication to MAC");
325    procPhyMessages(crcInd->header.msg_id, sizeof(fapi_crc_ind_t), (void *)crcInd);
326    MAC_FREE(crcInd, sizeof(fapi_crc_ind_t));
327 #endif
328    return ROK;
329 } /* l1BuildAndSendCrcInd */
330
331 #ifdef INTEL_FAPI
332 /*******************************************************************
333  *
334  * @brief Build and send Rx data indication
335  *
336  * @details
337  *
338  *    Function : l1BuildAndSendRxDataInd
339  *
340  *    Functionality:
341  *       Build and send Rx data indication
342  *
343  * @params[in] SFN
344  *             Slot
345  * @return ROK     - success
346  *         RFAILED - failure
347  *
348  * ****************************************************************/
349 uint16_t l1BuildAndSendRxDataInd(uint16_t slot, uint16_t sfn, fapi_ul_pusch_pdu_t puschPdu)
350 {
351    uint8_t idx = 0;
352    fapi_rx_data_indication_t *rxDataInd =NULLP;
353    fapi_pdu_ind_info_t       *pduInfo =NULLP;
354    uint8_t  *pdu = NULLP;
355    uint16_t byteIdx = 0;
356    uint32_t msgLen = 0;
357    MsgType type = 0;
358
359    if(!msg3Sent)
360    {
361       msg3Sent = true;
362       type = MSG_TYPE_MSG3;
363       sleep(2);
364    }
365    else if(!msg5ShortBsrSent)
366    {
367       msg5ShortBsrSent = true;
368       type = MSG_TYPE_SHORT_BSR;
369    }
370    else if(!msg5Sent)
371    {
372       msg5Sent = true;
373       type = MSG_TYPE_MSG5;
374    }
375    else if(!msgRegistrationComp)
376    {
377       msgRegistrationComp = true;
378       type = MSG_TYPE_REGISTRATION_COMPLETE; 
379    }
380    else if(!msgSecurityModeComp)
381    {
382       msgSecurityModeComp = true;
383       type = MSG_TYPE_SECURITY_MODE_COMPLETE;
384    }
385    else if(!msgRrcReconfiguration)
386    {
387       msgRrcReconfiguration = true;
388       type = MSG_TYPE_RRC_RECONFIG_COMPLETE;
389    }
390    else
391       return RFAILED;
392
393    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
394    if(!rxDataInd)
395    {
396       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
397       return RFAILED;
398    }
399    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
400    
401    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
402    rxDataInd->sfn = sfn;
403    rxDataInd->slot = slot;
404    rxDataInd->numPdus = 1;
405
406    pduInfo = &rxDataInd->pdus[idx];
407    pduInfo->handle = puschPdu.handle;
408    pduInfo->rnti = puschPdu.rnti;
409    pduInfo->harqId = puschPdu.puschData.harqProcessId;
410    pduInfo->pdu_length = puschPdu.puschData.tbSize ;
411    pduInfo->ul_cqi = 0;
412    pduInfo->timingAdvance = 0;
413    pduInfo->rssi = 0;
414
415    /* Filling pdu with random values for testing */
416    pduInfo->pduData = NULL;
417    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
418    if(!pduInfo->pduData)
419    {
420       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
421       return RFAILED;
422    }
423    memset(pduInfo->pduData, 0, pduInfo->pdu_length);
424    /* Filling PDU */
425    pdu = (uint8_t *)pduInfo->pduData;
426
427    switch(type)
428    {
429       case MSG_TYPE_MSG3: 
430          {
431             DU_LOG("\nDEBUG  -->  PHY_STUB: Forming MSG3 PDU ");
432             /* For Initial RRC setup Request,
433                MAC subheader format is R/R/LCId (1byte)
434                LCId is CCCH(0)
435                From 38.321 section 6.1.1
436              */
437             pdu[byteIdx++] = 0;
438             /* Hardcoding MAC PDU */
439             pdu[byteIdx++] = 16;
440             pdu[byteIdx++] = 00;
441             pdu[byteIdx++] = 00;
442             pdu[byteIdx++] = 00;
443             pdu[byteIdx++] = 00;
444             pdu[byteIdx++] = 103;
445
446             break;
447          }
448       case MSG_TYPE_SHORT_BSR:
449       {
450          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming SHORT BSR PDU ");
451          uint8_t lcgId = 0;
452          uint8_t bufferSizeIdx = 6;
453
454          /* For Short BSR
455             MAC subheader format is R/R/LcId (1Byte)
456             LCId is 61
457             From 38.321 section 6.1.1
458           */
459          pdu[byteIdx++] = 61;    // LCID
460          pdu[byteIdx++] = (lcgId << 5) | bufferSizeIdx;
461
462          break;
463       }
464
465       case MSG_TYPE_MSG5:
466       {
467          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming MSG5 PDU");
468          uint8_t  msg5PduLen = 33;
469          /* For RRC setup complete
470             MAC subheader format is R/F/LCId/L (2/3 bytes)
471             LCId is 1 for SRB1
472             L is length of PDU i.e 6bytes here 
473             From 38.321 section 6.1.1
474           */
475          uint8_t msg5[] = {1, msg5PduLen, 0, 0, 16, 0, 5, 223, 128, 16, 94, \
476             64, 3, 64, 89, 61, 138, 64, 0, 0, 0, 4, 0, 0, 4, 68, 11, 128, \
477                184, 56, 0, 0, 0, 0, 0};
478
479          msg5PduLen += 2;  /* 2bytes of header */
480          memcpy(pdu, &msg5, msg5PduLen);
481          byteIdx += msg5PduLen; /* 2 bytes of header */
482          break;
483       }
484
485       case MSG_TYPE_SECURITY_MODE_COMPLETE:
486       {
487          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming SECURITY MODE COMPLETE PDU");
488          uint8_t  pduLen = 12;
489          /* For security mode complete where RRC Container is dummy
490             MAC subheader format is R/F/LCId/L (2/3 bytes)
491             LCId is 1 for SRB1
492             L is length of PDU i.e 6bytes here 
493             From 38.321 section 6.1.1
494           */
495          uint8_t msg[] = {1, pduLen, 0, 3, 0x2a, 0x40, 0, 0, 0, 0, 0, 0, 0, 0};
496
497          pduLen += 2;  /* 2bytes of header */
498          memcpy(pdu, &msg, pduLen);
499          byteIdx += pduLen; /* 2 bytes of header */
500          break;
501       }
502       case MSG_TYPE_REGISTRATION_COMPLETE:
503       {
504           
505           DU_LOG("\nDEBUG  -->  PHY_STUB: Forming RRC REGISTRATION COMPLETE PDU");
506           uint8_t  pduLen = 12;
507           /* For rrc reconfig complete where RRC Container is dummy
508           MAC subheader format is R/F/LCId/L (2/3 bytes)
509           LCId is 1 for SRB1
510           L is length of PDU i.e 6bytes here
511           From 38.321 section 6.1.1
512           */
513           uint8_t msg[] = {1, pduLen, 0, 0x04, 0x3a, 0x81, 0xbf, 0, 0x21, 0x80, 0, \
514           0, 0, 0};
515         
516           pduLen += 2;  /* 2bytes of header */
517           memcpy(pdu, &msg, pduLen);
518           byteIdx += pduLen; /* 2 bytes of header */
519           break;
520       }
521       case MSG_TYPE_RRC_RECONFIG_COMPLETE:
522       {
523          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming RRC RECONFIGURATION COMPLETE PDU");
524          uint8_t  pduLen = 14;
525          /* For rrc reconfig complete where RRC Container is dummy
526          MAC subheader format is R/F/LCId/L (2/3 bytes)
527          LCId is 1 for SRB1
528          L is length of PDU i.e 6bytes here
529          From 38.321 section 6.1.1
530          */
531          uint8_t msg[] = {1, pduLen, 0, 6, 8, 64, 0, 0, 0, 0, \
532                           0, 0, 0, 0, 0};
533         
534          pduLen += 2;  /* 2bytes of header */
535          memcpy(pdu, &msg, pduLen);
536          byteIdx += pduLen; /* 2 bytes of header */
537          break;
538
539       }
540       
541       default:
542          break;
543    } /* End of switch(type) */
544    /* Filling MAC SDU for Padding bytes*/
545    if(byteIdx < pduInfo->pdu_length)
546    {
547       /* For Padding
548          MAC subheader format is R/R/LCId (1byte)
549          LCId is 63 for padding
550          From 38.321 section 6.1.1
551        */
552       pdu[byteIdx++] = 63;
553
554       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
555          pdu[byteIdx] = 0;
556    }
557    msgLen += pduInfo->pdu_length;
558
559    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
560
561    /* Sending Rx data indication to MAC */
562    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
563    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
564
565    if(pduInfo->pdu_length)
566       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
567    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
568    return ROK;
569 }
570 #endif
571
572 /*******************************************************************
573  *
574  * @brief Builds and Sends RACH indication to MAC 
575  *
576  * @details
577  *
578  *    Function : l1BuildAndSendRachInd 
579  *
580  *    Functionality:
581  *      Builds and Sends RACH indication to MAC
582  *
583  * @params[in] SFN value 
584  *             slot value
585  * @return ROK     - success
586  *         RFAILED - failure
587  *
588  * ****************************************************************/
589 uint16_t l1BuildAndSendRachInd(uint16_t slot, uint16_t sfn)
590 {
591 #ifdef INTEL_FAPI
592    uint8_t   rachPduIdx = 0; 
593    uint8_t   preamIdx = 0;
594    fapi_rach_pdu_t  *rachPdu;
595    fapi_rach_indication_t  *rachInd;
596
597    /* Building RACH indication */
598    MAC_ALLOC(rachInd, sizeof(fapi_rach_indication_t));
599    if(!rachInd)
600    {
601       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rach Indication Message");
602       return RFAILED;
603    }
604    memset(rachInd, 0, sizeof(fapi_rach_indication_t)); 
605    rachInd->sfn = sfn;
606    rachInd->slot = slot;
607    rachInd->numPdus = 1;
608
609    rachPdu = &rachInd->rachPdu[rachPduIdx];
610    rachPdu->phyCellId = NR_PCI;
611    rachPdu->symbolIndex = 0;
612    rachPdu->slotIndex = slot;
613    rachPdu->freqIndex = 0;
614    rachPdu->avgRssi = 0;
615    rachPdu->avgSnr = 0;
616    rachPdu->numPreamble = 1;
617
618    rachPdu->preambleInfo[preamIdx].preambleIndex = 3;
619    rachPdu->preambleInfo[preamIdx].timingAdvance = 0;
620    rachPdu->preambleInfo[preamIdx].preamblePwr = 0;
621
622    fillMsgHeader(&rachInd->header, FAPI_RACH_INDICATION, \
623          sizeof(fapi_rach_indication_t) - sizeof(fapi_msg_t));
624
625    /* Sending RACH indication to MAC */
626    DU_LOG("\nINFO   -->  PHY STUB: Sending RACH Indication to MAC");
627    procPhyMessages(rachInd->header.msg_id, sizeof(fapi_rach_indication_t), (void *)rachInd);
628    MAC_FREE(rachInd, sizeof(fapi_rach_indication_t));
629 #endif
630    return ROK;
631 }
632
633 /*******************************************************************
634  *
635  * @brief Builds and Send the Slot Indication message to MAC
636  *
637  * @details
638  *
639  *    Function : l1BuildAndSendSlotIndication
640  *
641  *    Functionality:
642  *          -Send the Slot indication Message to MAC
643  *
644  * @params[in]   Message length
645  *               config request message pointer
646  *
647  * @return void
648  *
649  * ****************************************************************/
650 uint16_t l1BuildAndSendSlotIndication()
651 {
652    Pst pst;
653    Buffer *mBuf;
654
655 #ifdef INTEL_FAPI
656    fapi_slot_ind_t *slotIndMsg;
657
658    MAC_ALLOC_SHRABL_BUF(slotIndMsg, sizeof(fapi_slot_ind_t));
659    if(!slotIndMsg)
660    {
661       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for slot Indication Message");
662       return RFAILED;
663    }
664    else
665    {
666       memset(slotIndMsg, 0, sizeof(fapi_slot_ind_t));
667       slotIndMsg->sfn = sfnValue;
668       slotIndMsg->slot = slotValue;
669
670 #ifdef ODU_SLOT_IND_DEBUG_LOG
671       DU_LOG("\n\nDEBUG  -->  PHY_STUB: Sending Slot Indication [%d : %d] to MAC", sfnValue, slotValue);
672 #endif
673
674       /* increment for the next TTI */
675       slotValue++;
676       if(sfnValue >= MAX_SFN_VALUE && slotValue > MAX_SLOT_VALUE)
677       {
678          sfnValue = 0;
679          slotValue = 0;
680       }
681       else if(slotValue > MAX_SLOT_VALUE)
682       {
683          sfnValue++;
684          slotValue = 0;
685       }
686       fillMsgHeader(&slotIndMsg->header, FAPI_SLOT_INDICATION, \
687             sizeof(fapi_slot_ind_t) - sizeof(fapi_msg_t));
688
689       memset(&pst, 0, sizeof(Pst));
690       FILL_PST_PHY_STUB_TO_LWR_MAC(pst, EVT_PHY_STUB_SLOT_IND);
691
692       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
693       if(!mBuf)
694       {
695          DU_LOG("\nERROR  --> PHY_STUB: Failed to allocate memory for slot indication buffer");
696          MAC_FREE_SHRABL_BUF(pst.region, pst.pool, slotIndMsg, sizeof(fapi_slot_ind_t));
697          return RFAILED;
698       }
699       CMCHKPK(oduPackPointer, (PTR)slotIndMsg, mBuf);
700       ODU_POST_TASK(&pst, mBuf);
701    }
702 #endif
703    return ROK;
704 }
705
706 /*******************************************************************
707  *
708  * @brief Handles start request received from MAC
709  *
710  * @details
711  *
712  *    Function : l1HdlStartReq
713  *
714  *    Functionality:
715  *          -Handles start request received from MAC
716  *
717  * @params[in]   Message length
718  *               config request message pointer
719  *
720  * @return void
721  *
722  * ****************************************************************/
723
724 S16 l1HdlStartReq(uint32_t msgLen, void *msg)
725 {
726 #ifdef INTEL_FAPI
727    if(lwrMacCb.phyState == PHY_STATE_CONFIGURED)
728    {
729       l1HdlSlotIndicaion(FALSE);
730       l1StartConsoleHandler();
731       MAC_FREE(msg, msgLen);
732    }
733    else
734    {
735       DU_LOG("\nINFO   -->  PHY_STUB: Received Start Req in PHY State %d", lwrMacCb.phyState);
736       MAC_FREE(msg, msgLen);
737       return RFAILED;
738    }
739 #endif
740    return ROK;
741 }
742
743 /*******************************************************************
744  *
745  * @brief Handles Dl Tti request received from MAC
746  *
747  * @details
748  *
749  *    Function : l1HdlDlTtiReq
750  *
751  *    Functionality:
752  *          -Handles Dl Tti request received from MAC
753  *
754  * @params[in]   Message length
755  *               Dl Tti request message pointer
756  *
757  * @return void
758  *
759  * ****************************************************************/
760
761 S16 l1HdlDlTtiReq(uint16_t msgLen, void *msg)
762 {
763 #ifdef INTEL_FAPI
764    p_fapi_api_queue_elem_t dlTtiElem = (p_fapi_api_queue_elem_t)msg;
765    fapi_dl_tti_req_t *dlTtiReq = (fapi_dl_tti_req_t *)(dlTtiElem +1);
766    
767    uint8_t pduCount = 0;
768
769 #ifdef ODU_SLOT_IND_DEBUG_LOG
770    if(dlTtiReq->nPdus == 0)
771    {
772       DU_LOG("\nDEBUG  -->  PHY_STUB: No PDU in DL TTI Request");
773    }
774    else
775    {  
776      DU_LOG("\nDEBUG  -->  PHY STUB: DL TTI Request at sfn=%d slot=%d",dlTtiReq->sfn,dlTtiReq->slot);
777    }
778 #endif 
779
780    for(pduCount=0; pduCount<dlTtiReq->nPdus; pduCount++)
781    {
782       if(dlTtiReq->pdus[pduCount].pduType == 3) //SSB_PDU_TYPE
783       {
784          DU_LOG("\nINFO   -->  PHY_STUB: SSB PDU");
785       }
786       else if(dlTtiReq->pdus[pduCount].pduType == 0)
787       {
788          DU_LOG("\nINFO   -->  PHY_STUB: PDCCH PDU");
789          if(dlTtiReq->pdus[pduCount].pdu.pdcch_pdu.\
790             coreSetType == 1)
791          {
792             dlDedMsg = true;
793          }
794       }
795       else if(dlTtiReq->pdus[pduCount].pduType == 1)
796       {
797          DU_LOG("\nINFO   -->  PHY_STUB: PDSCH PDU");
798       }
799    }
800
801    /* Free FAPI message */
802    MAC_FREE(msg, msgLen);
803 #endif
804    return ROK;
805 }
806
807 /*******************************************************************
808  *
809  * @brief Handles tx_data request received from MAC
810  *
811  * @details
812  *
813  *    Function : l1HdlTxDataReq
814  *
815  *    Functionality:
816  *          -Handles tx_data request received from MAC
817  *
818  * @params[in]   Message length
819  *               tx_data request message pointer
820  *
821  * @return void
822  *
823  * ****************************************************************/
824
825 S16 l1HdlTxDataReq(uint16_t msgLen, void *msg)
826 {
827 #ifdef INTEL_FAPI
828    p_fapi_api_queue_elem_t txDataElem = (p_fapi_api_queue_elem_t)msg;
829    fapi_tx_data_req_t *txDataReq = (fapi_tx_data_req_t *)(txDataElem +1);
830
831    DU_LOG("\nINFO   -->  PHY STUB: TX DATA Request at sfn=%d slot=%d",txDataReq->sfn,txDataReq->slot);
832    if(dlDedMsg)
833    {
834       DU_LOG("\nINFO   -->  PHY_STUB: TxDataPdu for DED MSG sent");
835       dlDedMsg = false;
836    }
837    MAC_FREE(msg, msgLen);
838 #endif
839    return ROK;
840 }
841
842 #ifdef INTEL_FAPI
843 /*******************************************************************
844  *
845  * @brief Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1
846  *
847  * @details
848  *
849  *    Function : fillPucchF0F1PduInfo
850  *
851  *    Functionality:
852  *       Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1 
853  *
854  * @params[in] fapi_uci_o_pucch_f0f1_t *
855  *             pucchPdu
856  * @return ROK     - success
857  *         RFAILED - failure
858  *
859  * ****************************************************************/
860 uint8_t fillPucchF0F1PduInfo(fapi_uci_o_pucch_f0f1_t *pduInfo, fapi_ul_pucch_pdu_t pucchPdu)
861 {
862    uint8_t idx = 0;
863
864    pduInfo->handle = pucchPdu.handle;
865    pduInfo->pduBitmap = 1;  //hardcoded for SR
866    pduInfo->pucchFormat = pucchPdu.formatType;
867    pduInfo->ul_cqi = 0;
868    pduInfo->rnti = pucchPdu.rnti;
869    pduInfo->timingAdvance = 0;
870    pduInfo->rssi = 0;
871    if(pduInfo->pduBitmap & SR_PDU_BITMASK)
872    {
873       pduInfo->srInfo.srIndication = SR_DETECTED;
874       pduInfo->srInfo.srConfidenceLevel = CONFDC_LEVEL_GOOD;
875    }
876    if(pduInfo->pduBitmap & HARQ_PDU_BITMASK)
877    {
878       pduInfo->harqInfo.numHarq++;
879       pduInfo->harqInfo.harqConfidenceLevel = CONFDC_LEVEL_GOOD;
880       for(idx = 0; idx < pduInfo->harqInfo.numHarq; idx++)
881       {
882          pduInfo->harqInfo.harqValue[idx] = HARQ_PASS;
883       }
884    }
885    return ROK;
886 }
887 /*******************************************************************
888  *
889  * @brief Fills UCI Pdu Information
890  *
891  * @details
892  *
893  *    Function : fillUciPduInfo
894  *
895  *    Functionality:
896  *       Fills UCI Pdu Information
897  *
898  * @params[in] Pointer to uciPdu
899  *             pucchPdu
900  * @return ROK     - success
901  *         RFAILED - failure
902  *
903  * ****************************************************************/
904 uint8_t fillUciPduInfo(fapi_uci_pdu_info_t *uciPdu, fapi_ul_pucch_pdu_t pucchPdu)
905 {
906    uint8_t ret = ROK;
907
908    /*TODO: The pduType is hardcoded here to support 
909      UCI Ind for PUCCH forat0/format1. This is to be
910      modified when we get SR form UE */
911    uciPdu->pduType = UCI_IND_PUCCH_F0F1;
912    switch(uciPdu->pduType)
913    {
914       case UCI_IND_PUSCH:
915          break;
916       case UCI_IND_PUCCH_F0F1:
917          {
918             fapi_uci_o_pucch_f0f1_t *pduInfo = NULLP;
919
920             pduInfo = &uciPdu->uci.uciPucchF0F1;
921             ret = fillPucchF0F1PduInfo(pduInfo, pucchPdu);
922             uciPdu->pduSize = sizeof(fapi_uci_o_pucch_f0f1_t);
923          }
924          break;
925       case UCI_IND_PUCCH_F2F3F4:
926          break;
927       default:
928          DU_LOG("\nERROR  -->  PHY_STUB: Invalid Pdu Type %d", uciPdu->pduType);
929          break;
930    }
931    return ret;
932 }
933
934 /*******************************************************************
935  *
936  * @brief Build and send Uci indication
937  *
938  * @details
939  *
940  *    Function : l1BuildAndSendUciInd
941  *
942  *    Functionality:
943  *       Build and send Uci indication
944  *
945  * @params[in] SFN
946  *             Slot
947  * @return ROK     - success
948  *         RFAILED - failure
949  *
950  * ****************************************************************/
951 uint8_t l1BuildAndSendUciInd(uint16_t slot, uint16_t sfn, fapi_ul_pucch_pdu_t pucchPdu)
952 {
953    uint8_t uciIdx = 0;
954    uint8_t nUciPdus = 0;
955    uint8_t ret = ROK;
956    uint32_t msgLen = 0;
957    fapi_uci_indication_t *uciInd =  NULLP;
958
959    MAC_ALLOC(uciInd, sizeof(fapi_uci_indication_t));
960    if(!uciInd)
961    {
962       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for UCI Indication");
963       return RFAILED;
964    }
965    memset(uciInd, 0, sizeof(fapi_uci_indication_t));
966    uciInd->sfn = sfn;
967    uciInd->slot = slot;
968    uciInd->numUcis = 1;   //consdering the UCI Ind for SR
969    nUciPdus = uciInd->numUcis;
970    while(nUciPdus)
971    {
972       ret = fillUciPduInfo(&uciInd->uciPdu[uciIdx], pucchPdu);
973       uciIdx++;
974       nUciPdus--;
975    }
976    if(ret == ROK)
977    {
978       msgLen = sizeof(fapi_uci_indication_t)- sizeof(fapi_msg_t);
979       fillMsgHeader(&uciInd->header, FAPI_UCI_INDICATION, msgLen);
980
981       /* Sending UCI indication to MAC */
982       DU_LOG("\nINFO   -->  PHY STUB: Sending UCI Indication to MAC");
983       procPhyMessages(uciInd->header.msg_id, sizeof(fapi_uci_indication_t), (void *)uciInd);
984    }
985    MAC_FREE(uciInd, sizeof(fapi_uci_indication_t));
986    return ret;
987 }
988 #endif
989
990 /*******************************************************************
991  *
992  * @brief Handles Ul Tti request received from MAC
993  *
994  * @details
995  *
996  *    Function : l1HdlUlTtiReq
997  *
998  *    Functionality:
999  *          -Handles Ul Tti request received from MAC
1000  *
1001  * @params[in]   Message length
1002  *               Ul Tti request message pointer
1003  *
1004  * @return void
1005  *
1006  * ****************************************************************/
1007
1008 S16 l1HdlUlTtiReq(uint16_t msgLen, void *msg)
1009 {
1010 #ifdef INTEL_FAPI
1011    p_fapi_api_queue_elem_t ulTtiElem = (p_fapi_api_queue_elem_t)msg;
1012    fapi_ul_tti_req_t *ulTtiReq = (fapi_ul_tti_req_t *)(ulTtiElem +1);
1013    uint8_t numPdus = ulTtiReq->nPdus;
1014
1015 #ifdef ODU_SLOT_IND_DEBUG_LOG
1016    if(numPdus == 0)
1017    {
1018       DU_LOG("\nINFO   -->  PHY STUB: No PDU received in UL TTI Req");
1019    }
1020    else
1021    {
1022       DU_LOG("\nINFO   -->  PHY STUB: Received UL TTI Request");
1023    }
1024 #endif 
1025
1026    while(numPdus)
1027    {
1028       if(ulTtiReq->pdus[numPdus-1].pduType == 0)
1029       {
1030          DU_LOG("\nINFO   -->  PHY STUB: PRACH PDU");
1031       }
1032       if(ulTtiReq->pdus[numPdus-1].pduType == 1)
1033       {
1034          DU_LOG("\nINFO   -->  PHY STUB: PUSCH PDU");
1035          l1BuildAndSendRxDataInd(ulTtiReq->slot, ulTtiReq->sfn, \
1036                ulTtiReq->pdus[numPdus-1].pdu.pusch_pdu); 
1037       }
1038       if(ulTtiReq->pdus[numPdus-1].pduType == 2)
1039       {
1040          DU_LOG("\nINFO   -->  PHY STUB: PUCCH PDU");
1041          fapi_ul_tti_req_t ulTtiSlotInd;
1042          memset(&ulTtiSlotInd, 0, sizeof(fapi_ul_tti_req_t));
1043          ulTtiSlotInd.slot = ulTtiReq->slot;
1044          ulTtiSlotInd.sfn  = ulTtiReq->sfn;
1045          ADD_DELTA_TO_TIME(ulTtiSlotInd, ulTtiSlotInd, SLOT_DELAY);
1046          l1BuildAndSendUciInd(ulTtiSlotInd.slot, ulTtiSlotInd.sfn, \
1047                ulTtiReq->pdus[numPdus-1].pdu.pucch_pdu);
1048       }
1049       numPdus--;
1050    }
1051
1052    if(rachIndSent == false && ulTtiReq->sfn == 16 && ulTtiReq->slot == 6)
1053    {
1054       rachIndSent = true;
1055       l1BuildAndSendRachInd(ulTtiReq->slot, ulTtiReq->sfn);
1056    }
1057
1058    MAC_FREE(msg, msgLen);
1059 #endif
1060    return ROK;
1061 }
1062
1063 /*******************************************************************
1064  *
1065  * @brief Builds and Send the stop Indication message to MAC
1066  *
1067  * @details
1068  *
1069  *    Function : l1BuildAndSendStopInd
1070  *
1071  *    Functionality:
1072  *          -Send the Stop indication Message to MAC
1073  *
1074  *
1075  * @return void
1076  *
1077  * ****************************************************************/
1078 uint16_t l1BuildAndSendStopInd()
1079 {
1080 #ifdef INTEL_FAPI
1081    Pst pst;
1082    Buffer *mBuf = NULLP;
1083    fapi_stop_ind_t *stopIndMsg = NULLP;
1084    uint32_t msgLen = 0;
1085
1086    MAC_ALLOC_SHRABL_BUF(stopIndMsg, sizeof(fapi_stop_ind_t));
1087    if(!stopIndMsg)
1088    {
1089       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for stop Indication Message");
1090       return RFAILED;
1091    }
1092    else
1093    {
1094       fillMsgHeader(&stopIndMsg->header, FAPI_STOP_INDICATION, msgLen);
1095       DU_LOG("\n\nINFO   -->  PHY_STUB: Processing Stop indication to MAC");
1096
1097       memset(&pst, 0, sizeof(Pst));
1098       FILL_PST_PHY_STUB_TO_LWR_MAC(pst, EVT_PHY_STUB_STOP_IND);
1099       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
1100       if(!mBuf)
1101       {
1102          DU_LOG("\nERROR  --> PHY_STUB: Failed to allocate memory for slot indication buffer");
1103          MAC_FREE_SHRABL_BUF(pst.region, pst.pool, stopIndMsg, sizeof(fapi_stop_ind_t));
1104          return RFAILED;
1105       }
1106       CMCHKPK(oduPackPointer, (PTR)stopIndMsg, mBuf);
1107       ODU_POST_TASK(&pst, mBuf);
1108    }
1109 #endif
1110    return ROK;
1111 }
1112
1113 /*******************************************************************
1114  *
1115  * @brief Handles stop request received from MAC
1116  *
1117  * @details
1118  *
1119  *    Function : l1HdlStopReq
1120  *
1121  *    Functionality:
1122  *          -Handles stop request received from MAC
1123  *
1124  * @params[in]   Message length
1125  *               stop request message pointer
1126  *
1127  * @return void
1128  *
1129  * ****************************************************************/
1130
1131 S16 l1HdlStopReq(uint32_t msgLen, void *msg)
1132 {
1133 #ifdef INTEL_FAPI
1134    if(lwrMacCb.phyState == PHY_STATE_RUNNING)
1135    {
1136       l1HdlSlotIndicaion(TRUE);
1137
1138       /* Initialize all global variables */
1139       sfnValue = 0;
1140       slotValue = 0;
1141       rachIndSent = false;
1142       msg3Sent = false;
1143       msg5ShortBsrSent = false;
1144       msg5Sent = false;
1145       dlDedMsg = false;
1146       msgSecurityModeComp =  false;
1147       msgRrcReconfiguration  =  false;
1148       msgRegistrationComp    = false;
1149
1150       DU_LOG("\nINFO   -->  PHY_STUB: Slot Indication is stopped successfully");
1151       MAC_FREE(msg, msgLen);
1152    }
1153    else
1154    {
1155       DU_LOG("\nINFO  -->  PHY_STUB: Received Stop Req in PHY State %d", lwrMacCb.phyState);
1156       MAC_FREE(msg, msgLen);
1157       return RFAILED;
1158    }
1159 #endif
1160    return ROK;
1161 }
1162
1163 /*******************************************************************
1164  *
1165  * @brief Build And Send Rx Data Ind for Msg5
1166  *
1167  * @details
1168  *
1169  *    Function : l1BuildAndSendMsg5
1170  *
1171  *    Functionality: Build And Send Rx Data Ind for Msg5
1172  *
1173  * @params[in] SFN
1174  *             Slot
1175  * @return ROK     - success
1176  *         RFAILED - failure
1177  *
1178  * ****************************************************************/
1179 uint8_t l1BuildAndSendMsg5(uint16_t sfn, uint16_t slot)
1180 {   
1181 #ifdef INTEL_FAPI
1182    uint8_t idx = 0;
1183    fapi_rx_data_indication_t *rxDataInd;
1184    fapi_pdu_ind_info_t       *pduInfo;
1185    uint8_t  *pdu;
1186    uint16_t byteIdx = 0;
1187    uint32_t msgLen = 0;
1188
1189    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1190    if(!rxDataInd)
1191    {
1192       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication for msg5");
1193       return RFAILED;
1194    }
1195    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1196
1197    /* TODO: Fill the required values */
1198    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1199    rxDataInd->sfn = sfn;
1200    rxDataInd->slot = slot;
1201    rxDataInd->numPdus = 1;
1202
1203    pduInfo = &rxDataInd->pdus[idx];
1204    pduInfo->handle = 100;
1205    pduInfo->rnti = 100;
1206    pduInfo->harqId = 1;
1207    pduInfo->pdu_length = 25;
1208    pduInfo->ul_cqi = 0;
1209    pduInfo->timingAdvance = 0;
1210    pduInfo->rssi = 0;
1211
1212    /* Filling pdu with random values for testing */
1213    pduInfo->pduData = NULL;
1214    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1215    if(!pduInfo->pduData)
1216    {
1217       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1218       return RFAILED;
1219    }
1220
1221    /* Filling PDU */
1222    pdu = (uint8_t *)pduInfo->pduData;
1223
1224    uint8_t  msg5PduLen = 33;
1225    /* For RRC setup complete
1226       MAC subheader format is R/F/LCId/L (2/3 bytes)
1227       LCId is 1 for SRB1
1228       L is length of PDU i.e 6bytes here
1229       From 38.321 section 6.1.1
1230     */
1231    uint8_t msg5[] = {1, msg5PduLen, 0, 0, 16, 0, 5, 223, 128, 16, 94, \
1232       64, 3, 64, 89, 61, 138, 64, 0, 0, 0, 4, 0, 0, 4, 68, 11, 128, \
1233          184, 56, 0, 0, 0, 0, 0};
1234
1235    msg5PduLen += 2;  /* 2bytes of header */
1236    memcpy(pdu, &msg5, msg5PduLen);
1237    byteIdx += msg5PduLen; /* 2 bytes of header */
1238
1239    /* Filling MAC SDU for Padding bytes*/
1240    if(byteIdx < pduInfo->pdu_length)
1241    {
1242       /* For Padding
1243          MAC subheader format is R/R/LCId (1byte)
1244          LCId is 63 for padding
1245          From 38.321 section 6.1.1
1246        */
1247       pdu[byteIdx++] = 63;
1248
1249       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1250          pdu[byteIdx] = 0;
1251    }
1252    msgLen += pduInfo->pdu_length;
1253
1254    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1255
1256    /* Sending Rx data indication to MAC */
1257    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
1258    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1259
1260    if(pduInfo->pdu_length)
1261       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1262    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1263 #endif
1264    return ROK;
1265 }
1266
1267 /*******************************************************************
1268  *
1269  * @brief Handles Ul Dci request received from MAC
1270  *
1271  * @details
1272  *
1273  *    Function : l1HdlUlDciReq
1274  *
1275  *    Functionality:
1276  *          -Handles Ul Dci request received from MAC
1277  *
1278  * @params[in]   Message length
1279  *               Ul Dci request message pointer
1280  *
1281  * @return void
1282  *
1283  * ****************************************************************/
1284
1285 S16 l1HdlUlDciReq(uint16_t msgLen, void *msg)
1286 {
1287 #ifdef INTEL_FAPI
1288    p_fapi_api_queue_elem_t ulDciElem = (p_fapi_api_queue_elem_t)msg;
1289    fapi_ul_dci_req_t *ulDciReq = (fapi_ul_dci_req_t *)(ulDciElem +1);
1290    uint8_t numPdus = ulDciReq->numPdus;
1291
1292    while(numPdus)
1293    {
1294       if(ulDciReq->pdus[numPdus-1].pduType == 0)
1295       {
1296          DU_LOG("\nINFO   -->  PHY STUB: Received UL DCI Request for PDCCH PDU");
1297          //l1BuildAndSendMsg5(ulDciReq->sfn, ulDciReq->slot);
1298          //msg5Sent = true;
1299       }
1300       numPdus--;
1301    }
1302
1303    MAC_FREE(msg, msgLen);
1304 #endif
1305    return ROK;
1306 }
1307
1308 /*******************************************************************
1309  *
1310  * @brief Send UL user data to DU
1311  *
1312  * @details
1313  *
1314  *    Function : l1SendUlUserData
1315  *
1316  *    Functionality: Send UL user data to DU
1317  *
1318  * @params[in]
1319  * @return ROK     - success
1320  *         RFAILED - failure
1321  *
1322  * ****************************************************************/
1323 uint8_t l1SendUlUserData()
1324 {
1325    uint8_t cnt = 0;
1326    fapi_rx_data_indication_t *rxDataInd;
1327    fapi_pdu_ind_info_t       *pduInfo;
1328    uint8_t  *pdu = NULLP;
1329    uint16_t byteIdx = 0;
1330    uint32_t msgLen = 0;
1331    uint8_t idx = 0;
1332
1333    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1334    if(!rxDataInd)
1335    {
1336       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1337       return RFAILED;
1338    }
1339    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1340
1341    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1342    rxDataInd->sfn = 0;
1343    rxDataInd->slot = 0;
1344    rxDataInd->numPdus = 1;
1345
1346    /* TODO : Fill pduInfo using PUSCH PDU. Currently hardcoding */
1347    pduInfo = &rxDataInd->pdus[idx];
1348    pduInfo->handle = 100;
1349    pduInfo->rnti = 100;
1350    pduInfo->harqId = 1;
1351    /* Since user data size = 50bytes and 2 bytes of MAC header and 3 byte of RLC header, 
1352     * setting tbsize = 56 from Table 5.1.3.2-1 spec 38.214 */
1353    pduInfo->pdu_length = 56;
1354    pduInfo->ul_cqi = 0;
1355    pduInfo->timingAdvance = 0;
1356    pduInfo->rssi = 0;
1357
1358    /* Filling pdu with random values for testing */
1359    pduInfo->pduData = NULL;
1360    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1361    if(!pduInfo->pduData)
1362    {
1363       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1364       return RFAILED;
1365    }
1366
1367    /* Filling PDU */
1368    pdu = (uint8_t *)pduInfo->pduData;
1369    msgLen = 52;
1370
1371    /* For UL User data
1372       MAC subheader format is R/F/LCId/L (2/3 bytes)
1373       LCId is 4 for DRB1
1374       L is length of PDU i.e 50 bytes
1375       From 38.321 section 6.1.1
1376       From 38.322 section 6.2.2.4, 6.2.2.3 for AM, UM Header
1377     */
1378
1379     /* Below ulMsg supports 12bit SN for UM mode */
1380                                /*  SI  SN */
1381     uint8_t ulMsg[] = {4, 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,
1382     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};
1383     msgLen += 2;  /* 2bytes of header */
1384     memcpy(pdu, &ulMsg, msgLen);
1385     byteIdx += msgLen; /* 2 bytes of header */
1386
1387    /* Filling MAC SDU for Padding bytes*/
1388    if(byteIdx < pduInfo->pdu_length)
1389    {
1390       /* For Padding
1391          MAC subheader format is R/R/LCId (1byte)
1392          LCId is 63 for padding
1393          From 38.321 section 6.1.1
1394        */
1395       pdu[byteIdx++] = 63;
1396
1397       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1398          pdu[byteIdx] = 0;
1399    }
1400    msgLen += pduInfo->pdu_length;
1401
1402    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1403
1404     /* Send Message to peer */
1405     while(cnt < 2)
1406     {
1407        DU_LOG("\nDEBUG  -->  PHY STUB : Sending UL User Data[%d] at sfn %d slot %d", cnt+1, sfnValue, slotValue);
1408        /* Sending Rx data indication to MAC */
1409        rxDataInd->sfn = sfnValue;
1410        rxDataInd->slot = slotValue;
1411        procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1412        cnt++;
1413     }
1414
1415     if(pduInfo->pdu_length)
1416        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1417     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1418     return ROK;
1419 }
1420
1421 /*******************************************************************
1422  *
1423  * @brief Sends RLC Status PDU to DU
1424  *
1425  * @details
1426  *
1427  *    Function : l1SendStatusPdu
1428  *
1429  *    Functionality: Send RLC Status PDU to DU
1430  *
1431  * @params[in]
1432  * @return ROK     - success
1433  *         RFAILED - failure
1434  *
1435  * ****************************************************************/
1436 uint8_t l1SendStatusPdu()
1437 {
1438    fapi_rx_data_indication_t *rxDataInd;
1439    fapi_pdu_ind_info_t       *pduInfo;
1440    uint8_t  *pdu = NULLP;
1441    uint16_t byteIdx = 0;
1442    uint32_t msgLen = 0;
1443    uint8_t idx = 0;
1444
1445    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1446    if(!rxDataInd)
1447    {
1448       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1449       return RFAILED;
1450    }
1451    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1452
1453    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1454    rxDataInd->sfn = 0;
1455    rxDataInd->slot = 0;
1456    rxDataInd->numPdus = 1;
1457
1458    /* TODO : Fill pduInfo using PUSCH PDU. Currently hardcoding */
1459    pduInfo = &rxDataInd->pdus[idx];
1460    pduInfo->handle = 100;
1461    pduInfo->rnti = 100;
1462    pduInfo->harqId = 1;
1463    /* Since status pdu size = 3bytes and 2 bytes of MAC header,
1464     * setting tbsize = 24 from Table 5.1.3.2-1 spec 38.214 */
1465    pduInfo->pdu_length = 24;
1466    pduInfo->ul_cqi = 0;
1467    pduInfo->timingAdvance = 0;
1468    pduInfo->rssi = 0;
1469
1470    /* Filling pdu with random values for testing */
1471    pduInfo->pduData = NULL;
1472    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1473    if(!pduInfo->pduData)
1474    {
1475       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1476       return RFAILED;
1477    }
1478
1479    /* Filling PDU */
1480    pdu = (uint8_t *)pduInfo->pduData;
1481    msgLen = 3;
1482
1483    /* For RLC Status PDU
1484       MAC subheader format is R/F/LCId/L (2/3 bytes)
1485       LCId is 4 for DRB1
1486       L is length of Status PDU i.e 3 bytes
1487       From 38.321 section 6.1.1
1488     */
1489    uint8_t statusPdu[] = {4, msgLen, 0, 0, 0};
1490    msgLen += 2;  /* 2bytes of header */
1491    memcpy(pdu, &statusPdu, msgLen);
1492    byteIdx += msgLen; /* 2 bytes of header */
1493
1494
1495    /* Filling MAC SDU for Padding bytes*/
1496    if(byteIdx < pduInfo->pdu_length)
1497    {
1498       /* For Padding
1499          MAC subheader format is R/R/LCId (1byte)
1500          LCId is 63 for padding
1501          From 38.321 section 6.1.1
1502        */
1503       pdu[byteIdx++] = 63;
1504
1505       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1506          pdu[byteIdx] = 0;
1507    }
1508    msgLen += pduInfo->pdu_length;
1509
1510    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1511
1512     /* Send Message to peer */
1513     DU_LOG("\nDEBUG  -->  PHY STUB : Sending RLC status pdu at sfn %d slot %d", sfnValue, slotValue);
1514     /* Sending Rx data indication to MAC */
1515     rxDataInd->sfn = sfnValue;
1516     rxDataInd->slot = slotValue;
1517     procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1518
1519     if(pduInfo->pdu_length)
1520        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1521     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1522     return ROK;
1523 }
1524
1525 /*******************************************************************
1526  *
1527  * @brief Receives message from MAC
1528  *
1529  * @details
1530  *
1531  *    Function :  l1ProcessFapiRequest
1532  *
1533  *    Functionality:
1534  *       - Receives message from MAC and calls handler
1535  *
1536  * @params[in] Message type
1537  *             Message length
1538  *             Message pointer
1539  *
1540  * @return void
1541  *
1542  * ****************************************************************/
1543
1544 void l1ProcessFapiRequest(uint8_t msgType, uint32_t msgLen, void *msg)
1545 {
1546    switch(msgType)
1547    {
1548 #ifdef INTEL_FAPI
1549       case FAPI_PARAM_REQUEST:
1550          l1HdlParamReq(msgLen, msg);
1551          break;
1552       case FAPI_CONFIG_REQUEST:
1553          l1HdlConfigReq(msgLen, msg);
1554          break;
1555       case FAPI_START_REQUEST:
1556          l1HdlStartReq(msgLen, msg);
1557          break;
1558       case FAPI_DL_TTI_REQUEST:
1559          l1HdlDlTtiReq(msgLen, msg);
1560          break;
1561       case FAPI_TX_DATA_REQUEST:
1562          l1HdlTxDataReq(msgLen, msg);
1563          break;
1564       case FAPI_UL_TTI_REQUEST:
1565          l1HdlUlTtiReq(msgLen, msg);
1566          break;
1567       case FAPI_STOP_REQUEST:
1568          l1HdlStopReq(msgLen, msg);
1569          break;
1570       case FAPI_UL_DCI_REQUEST:
1571          l1HdlUlDciReq(msgLen, msg);
1572          break;
1573       default:
1574          DU_LOG("\nERROR  -->  PHY_STUB: Invalid message type[%x] received at PHY", msgType);
1575          break;
1576 #endif
1577    }
1578 }
1579 /**********************************************************************
1580   End of file
1581  **********************************************************************/