Merge "Using TAPA Task to generated PHY Stub's slot indication [Issue-Id: ODUHIGH...
[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       DU_LOG("\nINFO   -->  PHY_STUB: Slot Indication is stopped successfully");
1138       MAC_FREE(msg, msgLen);
1139    }
1140    else
1141    {
1142       DU_LOG("\nINFO  -->  PHY_STUB: Received Stop Req in PHY State %d", lwrMacCb.phyState);
1143       MAC_FREE(msg, msgLen);
1144       return RFAILED;
1145    }
1146 #endif
1147    return ROK;
1148 }
1149
1150 /*******************************************************************
1151  *
1152  * @brief Build And Send Rx Data Ind for Msg5
1153  *
1154  * @details
1155  *
1156  *    Function : l1BuildAndSendMsg5
1157  *
1158  *    Functionality: Build And Send Rx Data Ind for Msg5
1159  *
1160  * @params[in] SFN
1161  *             Slot
1162  * @return ROK     - success
1163  *         RFAILED - failure
1164  *
1165  * ****************************************************************/
1166 uint8_t l1BuildAndSendMsg5(uint16_t sfn, uint16_t slot)
1167 {   
1168 #ifdef INTEL_FAPI
1169    uint8_t idx = 0;
1170    fapi_rx_data_indication_t *rxDataInd;
1171    fapi_pdu_ind_info_t       *pduInfo;
1172    uint8_t  *pdu;
1173    uint16_t byteIdx = 0;
1174    uint32_t msgLen = 0;
1175
1176    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1177    if(!rxDataInd)
1178    {
1179       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication for msg5");
1180       return RFAILED;
1181    }
1182    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1183
1184    /* TODO: Fill the required values */
1185    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1186    rxDataInd->sfn = sfn;
1187    rxDataInd->slot = slot;
1188    rxDataInd->numPdus = 1;
1189
1190    pduInfo = &rxDataInd->pdus[idx];
1191    pduInfo->handle = 100;
1192    pduInfo->rnti = 100;
1193    pduInfo->harqId = 1;
1194    pduInfo->pdu_length = 25;
1195    pduInfo->ul_cqi = 0;
1196    pduInfo->timingAdvance = 0;
1197    pduInfo->rssi = 0;
1198
1199    /* Filling pdu with random values for testing */
1200    pduInfo->pduData = NULL;
1201    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1202    if(!pduInfo->pduData)
1203    {
1204       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1205       return RFAILED;
1206    }
1207
1208    /* Filling PDU */
1209    pdu = (uint8_t *)pduInfo->pduData;
1210
1211    uint8_t  msg5PduLen = 33;
1212    /* For RRC setup complete
1213       MAC subheader format is R/F/LCId/L (2/3 bytes)
1214       LCId is 1 for SRB1
1215       L is length of PDU i.e 6bytes here
1216       From 38.321 section 6.1.1
1217     */
1218    uint8_t msg5[] = {1, msg5PduLen, 0, 0, 16, 0, 5, 223, 128, 16, 94, \
1219       64, 3, 64, 89, 61, 138, 64, 0, 0, 0, 4, 0, 0, 4, 68, 11, 128, \
1220          184, 56, 0, 0, 0, 0, 0};
1221
1222    msg5PduLen += 2;  /* 2bytes of header */
1223    memcpy(pdu, &msg5, msg5PduLen);
1224    byteIdx += msg5PduLen; /* 2 bytes of header */
1225
1226    /* Filling MAC SDU for Padding bytes*/
1227    if(byteIdx < pduInfo->pdu_length)
1228    {
1229       /* For Padding
1230          MAC subheader format is R/R/LCId (1byte)
1231          LCId is 63 for padding
1232          From 38.321 section 6.1.1
1233        */
1234       pdu[byteIdx++] = 63;
1235
1236       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1237          pdu[byteIdx] = 0;
1238    }
1239    msgLen += pduInfo->pdu_length;
1240
1241    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1242
1243    /* Sending Rx data indication to MAC */
1244    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
1245    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1246
1247    if(pduInfo->pdu_length)
1248       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1249    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1250 #endif
1251    return ROK;
1252 }
1253
1254 /*******************************************************************
1255  *
1256  * @brief Handles Ul Dci request received from MAC
1257  *
1258  * @details
1259  *
1260  *    Function : l1HdlUlDciReq
1261  *
1262  *    Functionality:
1263  *          -Handles Ul Dci request received from MAC
1264  *
1265  * @params[in]   Message length
1266  *               Ul Dci request message pointer
1267  *
1268  * @return void
1269  *
1270  * ****************************************************************/
1271
1272 S16 l1HdlUlDciReq(uint16_t msgLen, void *msg)
1273 {
1274 #ifdef INTEL_FAPI
1275    p_fapi_api_queue_elem_t ulDciElem = (p_fapi_api_queue_elem_t)msg;
1276    fapi_ul_dci_req_t *ulDciReq = (fapi_ul_dci_req_t *)(ulDciElem +1);
1277    uint8_t numPdus = ulDciReq->numPdus;
1278
1279    while(numPdus)
1280    {
1281       if(ulDciReq->pdus[numPdus-1].pduType == 0)
1282       {
1283          DU_LOG("\nINFO   -->  PHY STUB: Received UL DCI Request for PDCCH PDU");
1284          //l1BuildAndSendMsg5(ulDciReq->sfn, ulDciReq->slot);
1285          //msg5Sent = true;
1286       }
1287       numPdus--;
1288    }
1289
1290    MAC_FREE(msg, msgLen);
1291 #endif
1292    return ROK;
1293 }
1294
1295 /*******************************************************************
1296  *
1297  * @brief Send UL user data to DU
1298  *
1299  * @details
1300  *
1301  *    Function : l1SendUlUserData
1302  *
1303  *    Functionality: Send UL user data to DU
1304  *
1305  * @params[in]
1306  * @return ROK     - success
1307  *         RFAILED - failure
1308  *
1309  * ****************************************************************/
1310 uint8_t l1SendUlUserData()
1311 {
1312    uint8_t cnt = 0;
1313    fapi_rx_data_indication_t *rxDataInd;
1314    fapi_pdu_ind_info_t       *pduInfo;
1315    uint8_t  *pdu = NULLP;
1316    uint16_t byteIdx = 0;
1317    uint32_t msgLen = 0;
1318    uint8_t idx = 0;
1319
1320    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1321    if(!rxDataInd)
1322    {
1323       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1324       return RFAILED;
1325    }
1326    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1327
1328    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1329    rxDataInd->sfn = 0;
1330    rxDataInd->slot = 0;
1331    rxDataInd->numPdus = 1;
1332
1333    /* TODO : Fill pduInfo using PUSCH PDU. Currently hardcoding */
1334    pduInfo = &rxDataInd->pdus[idx];
1335    pduInfo->handle = 100;
1336    pduInfo->rnti = 100;
1337    pduInfo->harqId = 1;
1338    /* Since user data size = 50bytes and 2 bytes of MAC header and 3 byte of RLC header, 
1339     * setting tbsize = 56 from Table 5.1.3.2-1 spec 38.214 */
1340    pduInfo->pdu_length = 56;
1341    pduInfo->ul_cqi = 0;
1342    pduInfo->timingAdvance = 0;
1343    pduInfo->rssi = 0;
1344
1345    /* Filling pdu with random values for testing */
1346    pduInfo->pduData = NULL;
1347    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1348    if(!pduInfo->pduData)
1349    {
1350       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1351       return RFAILED;
1352    }
1353
1354    /* Filling PDU */
1355    pdu = (uint8_t *)pduInfo->pduData;
1356    msgLen = 52;
1357
1358    /* For UL User data
1359       MAC subheader format is R/F/LCId/L (2/3 bytes)
1360       LCId is 4 for DRB1
1361       L is length of PDU i.e 50 bytes
1362       From 38.321 section 6.1.1
1363       From 38.322 section 6.2.2.4, 6.2.2.3 for AM, UM Header
1364     */
1365
1366     /* Below ulMsg supports 12bit SN for UM mode */
1367                                /*  SI  SN */
1368     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,
1369     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};
1370     msgLen += 2;  /* 2bytes of header */
1371     memcpy(pdu, &ulMsg, msgLen);
1372     byteIdx += msgLen; /* 2 bytes of header */
1373
1374    /* Filling MAC SDU for Padding bytes*/
1375    if(byteIdx < pduInfo->pdu_length)
1376    {
1377       /* For Padding
1378          MAC subheader format is R/R/LCId (1byte)
1379          LCId is 63 for padding
1380          From 38.321 section 6.1.1
1381        */
1382       pdu[byteIdx++] = 63;
1383
1384       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1385          pdu[byteIdx] = 0;
1386    }
1387    msgLen += pduInfo->pdu_length;
1388
1389    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1390
1391     /* Send Message to peer */
1392     while(cnt < 2)
1393     {
1394        DU_LOG("\nDEBUG  -->  PHY STUB : Sending UL User Data[%d] at sfn %d slot %d", cnt+1, sfnValue, slotValue);
1395        /* Sending Rx data indication to MAC */
1396        rxDataInd->sfn = sfnValue;
1397        rxDataInd->slot = slotValue;
1398        procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1399        cnt++;
1400     }
1401
1402     if(pduInfo->pdu_length)
1403        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1404     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1405     return ROK;
1406 }
1407
1408 /*******************************************************************
1409  *
1410  * @brief Sends RLC Status PDU to DU
1411  *
1412  * @details
1413  *
1414  *    Function : l1SendStatusPdu
1415  *
1416  *    Functionality: Send RLC Status PDU to DU
1417  *
1418  * @params[in]
1419  * @return ROK     - success
1420  *         RFAILED - failure
1421  *
1422  * ****************************************************************/
1423 uint8_t l1SendStatusPdu()
1424 {
1425    fapi_rx_data_indication_t *rxDataInd;
1426    fapi_pdu_ind_info_t       *pduInfo;
1427    uint8_t  *pdu = NULLP;
1428    uint16_t byteIdx = 0;
1429    uint32_t msgLen = 0;
1430    uint8_t idx = 0;
1431
1432    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1433    if(!rxDataInd)
1434    {
1435       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1436       return RFAILED;
1437    }
1438    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1439
1440    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1441    rxDataInd->sfn = 0;
1442    rxDataInd->slot = 0;
1443    rxDataInd->numPdus = 1;
1444
1445    /* TODO : Fill pduInfo using PUSCH PDU. Currently hardcoding */
1446    pduInfo = &rxDataInd->pdus[idx];
1447    pduInfo->handle = 100;
1448    pduInfo->rnti = 100;
1449    pduInfo->harqId = 1;
1450    /* Since status pdu size = 3bytes and 2 bytes of MAC header,
1451     * setting tbsize = 24 from Table 5.1.3.2-1 spec 38.214 */
1452    pduInfo->pdu_length = 24;
1453    pduInfo->ul_cqi = 0;
1454    pduInfo->timingAdvance = 0;
1455    pduInfo->rssi = 0;
1456
1457    /* Filling pdu with random values for testing */
1458    pduInfo->pduData = NULL;
1459    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1460    if(!pduInfo->pduData)
1461    {
1462       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1463       return RFAILED;
1464    }
1465
1466    /* Filling PDU */
1467    pdu = (uint8_t *)pduInfo->pduData;
1468    msgLen = 3;
1469
1470    /* For RLC Status PDU
1471       MAC subheader format is R/F/LCId/L (2/3 bytes)
1472       LCId is 4 for DRB1
1473       L is length of Status PDU i.e 3 bytes
1474       From 38.321 section 6.1.1
1475     */
1476    uint8_t statusPdu[] = {4, msgLen, 0, 0, 0};
1477    msgLen += 2;  /* 2bytes of header */
1478    memcpy(pdu, &statusPdu, msgLen);
1479    byteIdx += msgLen; /* 2 bytes of header */
1480
1481
1482    /* Filling MAC SDU for Padding bytes*/
1483    if(byteIdx < pduInfo->pdu_length)
1484    {
1485       /* For Padding
1486          MAC subheader format is R/R/LCId (1byte)
1487          LCId is 63 for padding
1488          From 38.321 section 6.1.1
1489        */
1490       pdu[byteIdx++] = 63;
1491
1492       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1493          pdu[byteIdx] = 0;
1494    }
1495    msgLen += pduInfo->pdu_length;
1496
1497    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1498
1499     /* Send Message to peer */
1500     DU_LOG("\nDEBUG  -->  PHY STUB : Sending RLC status pdu at sfn %d slot %d", sfnValue, slotValue);
1501     /* Sending Rx data indication to MAC */
1502     rxDataInd->sfn = sfnValue;
1503     rxDataInd->slot = slotValue;
1504     procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1505
1506     if(pduInfo->pdu_length)
1507        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1508     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1509     return ROK;
1510 }
1511
1512 /*******************************************************************
1513  *
1514  * @brief Receives message from MAC
1515  *
1516  * @details
1517  *
1518  *    Function :  l1ProcessFapiRequest
1519  *
1520  *    Functionality:
1521  *       - Receives message from MAC and calls handler
1522  *
1523  * @params[in] Message type
1524  *             Message length
1525  *             Message pointer
1526  *
1527  * @return void
1528  *
1529  * ****************************************************************/
1530
1531 void l1ProcessFapiRequest(uint8_t msgType, uint32_t msgLen, void *msg)
1532 {
1533    switch(msgType)
1534    {
1535 #ifdef INTEL_FAPI
1536       case FAPI_PARAM_REQUEST:
1537          l1HdlParamReq(msgLen, msg);
1538          break;
1539       case FAPI_CONFIG_REQUEST:
1540          l1HdlConfigReq(msgLen, msg);
1541          break;
1542       case FAPI_START_REQUEST:
1543          l1HdlStartReq(msgLen, msg);
1544          break;
1545       case FAPI_DL_TTI_REQUEST:
1546          l1HdlDlTtiReq(msgLen, msg);
1547          break;
1548       case FAPI_TX_DATA_REQUEST:
1549          l1HdlTxDataReq(msgLen, msg);
1550          break;
1551       case FAPI_UL_TTI_REQUEST:
1552          l1HdlUlTtiReq(msgLen, msg);
1553          break;
1554       case FAPI_STOP_REQUEST:
1555          l1HdlStopReq(msgLen, msg);
1556          break;
1557       case FAPI_UL_DCI_REQUEST:
1558          l1HdlUlDciReq(msgLen, msg);
1559          break;
1560       default:
1561          DU_LOG("\nERROR  -->  PHY_STUB: Invalid message type[%x] received at PHY", msgType);
1562          break;
1563 #endif
1564    }
1565 }
1566 /**********************************************************************
1567   End of file
1568  **********************************************************************/