Merge "RAR and MSG3 scheduling in TDD [Issue-ID: ODUHIGH-342]"
[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    memset(&ueDb, 0, sizeof(UeDb));
249
250 #ifdef INTEL_FAPI
251    p_fapi_api_queue_elem_t configReqElem = (p_fapi_api_queue_elem_t)msg;
252    fapi_config_req_t *configReq = (fapi_config_req_t *)(configReqElem +1);
253
254    DU_LOG("\nINFO   -->  PHY_STUB: Received Config Request in PHY");
255
256    /* Handling CONFIG RESPONSE */
257    if(l1BldAndSndConfigRsp(configReq)!= ROK)
258    {
259       DU_LOG("\nERROR  -->  PHY_STUB: Failed Sending config Response");
260    }
261
262    MAC_FREE(msg, msgLen);
263 #endif
264
265 }
266
267 /*******************************************************************
268  *
269  * @brief Build and Send CRC Indication
270  *
271  * @details
272  *
273  *    Function : l1BuildAndSendCrcInd
274  *
275  *    Functionality:
276  *      Build and Send CRC Indication
277  *
278  * @params[in] Slot
279  *             SFN 
280  * @return ROK     - success
281  *         RFAILED - failure
282  *
283  * ****************************************************************/
284 uint16_t l1BuildAndSendCrcInd(uint16_t slot, uint16_t sfn)
285 {
286 #ifdef INTEL_FAPI
287    uint8_t idx = 0;
288    fapi_crc_ind_t  *crcInd;
289
290    MAC_ALLOC(crcInd, sizeof(fapi_crc_ind_t));
291    if(!crcInd)
292    {
293       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for CRC Indication Message");
294       return RFAILED;
295    }
296    memset(crcInd, 0, sizeof(fapi_crc_ind_t));
297
298    /* TODO: Fill the required values. As of now only 1 CRC status PASS is filled */
299    crcInd->sfn = sfn;
300    crcInd->slot = slot;
301    crcInd->numCrcs = 1;
302
303    crcInd->crc[idx].handle = 0;
304    crcInd->crc[idx].rnti = 0;
305    crcInd->crc[idx].harqId = 0;
306    crcInd->crc[idx].tbCrcStatus = 0;
307    crcInd->crc[idx].numCb = 1;
308    crcInd->crc[idx].cbCrcStatus[0] = 0;
309    crcInd->crc[idx].ul_cqi = 0;
310    crcInd->crc[idx].timingAdvance = 0;
311    crcInd->crc[idx].rssi = 0;
312
313    fillMsgHeader(&crcInd->header, FAPI_CRC_INDICATION, \
314          sizeof(fapi_crc_ind_t) - sizeof(fapi_msg_t));
315
316    /* Sending RACH indication to MAC */
317    DU_LOG("\nINFO   -->  PHY STUB: Sending CRC Indication to MAC");
318    procPhyMessages(crcInd->header.msg_id, sizeof(fapi_crc_ind_t), (void *)crcInd);
319    MAC_FREE(crcInd, sizeof(fapi_crc_ind_t));
320 #endif
321    return ROK;
322 } /* l1BuildAndSendCrcInd */
323
324 #ifdef INTEL_FAPI
325 /*******************************************************************
326  *
327  * @brief Build and send Rx data indication
328  *
329  * @details
330  *
331  *    Function : l1BuildAndSendRxDataInd
332  *
333  *    Functionality:
334  *       Build and send Rx data indication
335  *
336  * @params[in] SFN
337  *             Slot
338  * @return ROK     - success
339  *         RFAILED - failure
340  *
341  * ****************************************************************/
342 uint16_t l1BuildAndSendRxDataInd(uint16_t slot, uint16_t sfn, fapi_ul_pusch_pdu_t puschPdu)
343 {
344    uint8_t idx = 0, ueId = 0;
345    fapi_rx_data_indication_t *rxDataInd =NULLP;
346    fapi_pdu_ind_info_t       *pduInfo =NULLP;
347    uint8_t  *pdu = NULLP;
348    uint16_t byteIdx = 0;
349    uint32_t msgLen = 0;
350    MsgType type = 0;
351
352    GET_UE_IDX(puschPdu.rnti, ueId);
353    if(!ueDb.ueCb[ueId-1].msg3Sent)
354    {
355       ueDb.ueCb[ueId-1].ueId = ueId;
356       ueDb.ueCb[ueId-1].crnti = puschPdu.rnti;
357       ueDb.ueCb[ueId-1].msg3Sent = true;
358       type = MSG_TYPE_MSG3;
359       sleep(2);
360    }
361    else if(!ueDb.ueCb[ueId-1].msg5ShortBsrSent)
362    {
363       ueDb.ueCb[ueId-1].msg5ShortBsrSent = true;
364       type = MSG_TYPE_SHORT_BSR;
365    }
366    else if(!ueDb.ueCb[ueId-1].msg5Sent)
367    {
368       ueDb.ueCb[ueId-1].msg5Sent = true;
369       type = MSG_TYPE_MSG5;
370    }
371    else if(!ueDb.ueCb[ueId-1].msgRegistrationComp)
372    {
373       ueDb.ueCb[ueId-1].msgRegistrationComp = true;
374       type = MSG_TYPE_REGISTRATION_COMPLETE; 
375    }
376    else if(!ueDb.ueCb[ueId-1].msgSecurityModeComp)
377    {
378       ueDb.ueCb[ueId-1].msgSecurityModeComp = true;
379       type = MSG_TYPE_SECURITY_MODE_COMPLETE;
380    }
381    else if(!ueDb.ueCb[ueId-1].msgRrcReconfiguration)
382    {
383       ueDb.ueCb[ueId-1].msgRrcReconfiguration = true;
384       type = MSG_TYPE_RRC_RECONFIG_COMPLETE;
385    }
386    else
387       return RFAILED;
388
389    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
390    if(!rxDataInd)
391    {
392       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
393       return RFAILED;
394    }
395    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
396    
397    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
398    rxDataInd->sfn = sfn;
399    rxDataInd->slot = slot;
400    rxDataInd->numPdus = 1;
401
402    pduInfo = &rxDataInd->pdus[idx];
403    pduInfo->handle = puschPdu.handle;
404    pduInfo->rnti = puschPdu.rnti;
405    pduInfo->harqId = puschPdu.puschData.harqProcessId;
406    pduInfo->pdu_length = puschPdu.puschData.tbSize ;
407    pduInfo->ul_cqi = 0;
408    pduInfo->timingAdvance = 0;
409    pduInfo->rssi = 0;
410
411    /* Filling pdu with random values for testing */
412    pduInfo->pduData = NULL;
413    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
414    if(!pduInfo->pduData)
415    {
416       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
417       return RFAILED;
418    }
419    memset(pduInfo->pduData, 0, pduInfo->pdu_length);
420    /* Filling PDU */
421    pdu = (uint8_t *)pduInfo->pduData;
422
423    switch(type)
424    {
425       case MSG_TYPE_MSG3: 
426          {
427             DU_LOG("\nDEBUG  -->  PHY_STUB: Forming MSG3 PDU ");
428             /* For Initial RRC setup Request,
429                MAC subheader format is R/R/LCId (1byte)
430                LCId is CCCH(0)
431                From 38.321 section 6.1.1
432              */
433             pdu[byteIdx++] = 0;
434             /* Hardcoding MAC PDU */
435             pdu[byteIdx++] = 16;
436             pdu[byteIdx++] = 00;
437             pdu[byteIdx++] = 00;
438             pdu[byteIdx++] = 00;
439             pdu[byteIdx++] = 00;
440             pdu[byteIdx++] = 103;
441
442             break;
443          }
444       case MSG_TYPE_SHORT_BSR:
445       {
446          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming SHORT BSR PDU ");
447          uint8_t lcgId = 0;
448          uint8_t bufferSizeIdx = 6;
449
450          /* For Short BSR
451             MAC subheader format is R/R/LcId (1Byte)
452             LCId is 61
453             From 38.321 section 6.1.1
454           */
455          pdu[byteIdx++] = 61;    // LCID
456          pdu[byteIdx++] = (lcgId << 5) | bufferSizeIdx;
457
458          break;
459       }
460
461       case MSG_TYPE_MSG5:
462       {
463          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming MSG5 PDU");
464          uint8_t  msg5PduLen = 33;
465          /* For RRC setup complete
466             MAC subheader format is R/F/LCId/L (2/3 bytes)
467             LCId is 1 for SRB1
468             L is length of PDU i.e 6bytes here 
469             From 38.321 section 6.1.1
470           */
471          uint8_t msg5[] = {1, msg5PduLen, 0, 0, 16, 0, 5, 223, 128, 16, 94, \
472             64, 3, 64, 89, 61, 138, 64, 0, 0, 0, 4, 0, 0, 4, 68, 11, 128, \
473                184, 56, 0, 0, 0, 0, 0};
474
475          msg5PduLen += 2;  /* 2bytes of header */
476          memcpy(pdu, &msg5, msg5PduLen);
477          byteIdx += msg5PduLen; /* 2 bytes of header */
478          break;
479       }
480
481       case MSG_TYPE_SECURITY_MODE_COMPLETE:
482       {
483          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming SECURITY MODE COMPLETE PDU");
484          uint8_t  pduLen = 12;
485          /* For security mode complete where RRC Container is dummy
486             MAC subheader format is R/F/LCId/L (2/3 bytes)
487             LCId is 1 for SRB1
488             L is length of PDU i.e 6bytes here 
489             From 38.321 section 6.1.1
490           */
491          uint8_t msg[] = {1, pduLen, 0, 3, 0x2a, 0x40, 0, 0, 0, 0, 0, 0, 0, 0};
492
493          pduLen += 2;  /* 2bytes of header */
494          memcpy(pdu, &msg, pduLen);
495          byteIdx += pduLen; /* 2 bytes of header */
496          break;
497       }
498       case MSG_TYPE_REGISTRATION_COMPLETE:
499       {
500           
501           DU_LOG("\nDEBUG  -->  PHY_STUB: Forming RRC REGISTRATION COMPLETE PDU");
502           uint8_t  pduLen = 12;
503           /* For rrc reconfig complete where RRC Container is dummy
504           MAC subheader format is R/F/LCId/L (2/3 bytes)
505           LCId is 1 for SRB1
506           L is length of PDU i.e 6bytes here
507           From 38.321 section 6.1.1
508           */
509           uint8_t msg[] = {1, pduLen, 0, 0x04, 0x3a, 0x81, 0xbf, 0, 0x21, 0x80, 0, \
510           0, 0, 0};
511         
512           pduLen += 2;  /* 2bytes of header */
513           memcpy(pdu, &msg, pduLen);
514           byteIdx += pduLen; /* 2 bytes of header */
515           break;
516       }
517       case MSG_TYPE_RRC_RECONFIG_COMPLETE:
518       {
519          DU_LOG("\nDEBUG  -->  PHY_STUB: Forming RRC RECONFIGURATION COMPLETE PDU");
520          uint8_t  pduLen = 14;
521          /* For rrc reconfig complete where RRC Container is dummy
522          MAC subheader format is R/F/LCId/L (2/3 bytes)
523          LCId is 1 for SRB1
524          L is length of PDU i.e 6bytes here
525          From 38.321 section 6.1.1
526          */
527          uint8_t msg[] = {1, pduLen, 0, 6, 8, 64, 0, 0, 0, 0, \
528                           0, 0, 0, 0, 0};
529         
530          pduLen += 2;  /* 2bytes of header */
531          memcpy(pdu, &msg, pduLen);
532          byteIdx += pduLen; /* 2 bytes of header */
533          break;
534
535       }
536       
537       default:
538          break;
539    } /* End of switch(type) */
540    /* Filling MAC SDU for Padding bytes*/
541    if(byteIdx < pduInfo->pdu_length)
542    {
543       /* For Padding
544          MAC subheader format is R/R/LCId (1byte)
545          LCId is 63 for padding
546          From 38.321 section 6.1.1
547        */
548       pdu[byteIdx++] = 63;
549
550       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
551          pdu[byteIdx] = 0;
552    }
553    msgLen += pduInfo->pdu_length;
554
555    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
556
557    /* Sending Rx data indication to MAC */
558    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
559    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
560
561    if(pduInfo->pdu_length)
562       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
563    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
564    return ROK;
565 }
566 #endif
567
568 /*******************************************************************
569  *
570  * @brief Builds and Sends RACH indication to MAC 
571  *
572  * @details
573  *
574  *    Function : l1BuildAndSendRachInd 
575  *
576  *    Functionality:
577  *      Builds and Sends RACH indication to MAC
578  *
579  * @params[in] SFN value 
580  *             slot value
581  * @return ROK     - success
582  *         RFAILED - failure
583  *
584  * ****************************************************************/
585 uint16_t l1BuildAndSendRachInd(uint16_t slot, uint16_t sfn)
586 {
587 #ifdef INTEL_FAPI
588    uint8_t   rachPduIdx = 0; 
589    uint8_t   preamIdx = 0;
590    fapi_rach_pdu_t  *rachPdu;
591    fapi_rach_indication_t  *rachInd;
592
593    /* Building RACH indication */
594    MAC_ALLOC(rachInd, sizeof(fapi_rach_indication_t));
595    if(!rachInd)
596    {
597       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rach Indication Message");
598       return RFAILED;
599    }
600    memset(rachInd, 0, sizeof(fapi_rach_indication_t)); 
601    rachInd->sfn = sfn;
602    rachInd->slot = slot;
603    rachInd->numPdus = 1;
604
605    rachPdu = &rachInd->rachPdu[rachPduIdx];
606    rachPdu->phyCellId = NR_PCI;
607    rachPdu->symbolIndex = 0;
608    rachPdu->slotIndex = slot;
609    rachPdu->freqIndex = 0;
610    rachPdu->avgRssi = 0;
611    rachPdu->avgSnr = 0;
612    rachPdu->numPreamble = 1;
613
614    rachPdu->preambleInfo[preamIdx].preambleIndex = 3;
615    rachPdu->preambleInfo[preamIdx].timingAdvance = 0;
616    rachPdu->preambleInfo[preamIdx].preamblePwr = 0;
617
618    fillMsgHeader(&rachInd->header, FAPI_RACH_INDICATION, \
619          sizeof(fapi_rach_indication_t) - sizeof(fapi_msg_t));
620
621    /* Sending RACH indication to MAC */
622    DU_LOG("\nINFO   -->  PHY STUB: Sending RACH Indication to MAC");
623    procPhyMessages(rachInd->header.msg_id, sizeof(fapi_rach_indication_t), (void *)rachInd);
624    MAC_FREE(rachInd, sizeof(fapi_rach_indication_t));
625 #endif
626    return ROK;
627 }
628
629 /*******************************************************************
630  *
631  * @brief Builds and Send the Slot Indication message to MAC
632  *
633  * @details
634  *
635  *    Function : l1BuildAndSendSlotIndication
636  *
637  *    Functionality:
638  *          -Send the Slot indication Message to MAC
639  *
640  * @params[in]   Message length
641  *               config request message pointer
642  *
643  * @return void
644  *
645  * ****************************************************************/
646 uint16_t l1BuildAndSendSlotIndication()
647 {
648    Pst pst;
649    Buffer *mBuf;
650
651 #ifdef INTEL_FAPI
652    fapi_slot_ind_t *slotIndMsg;
653
654    MAC_ALLOC_SHRABL_BUF(slotIndMsg, sizeof(fapi_slot_ind_t));
655    if(!slotIndMsg)
656    {
657       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for slot Indication Message");
658       return RFAILED;
659    }
660    else
661    {
662       memset(slotIndMsg, 0, sizeof(fapi_slot_ind_t));
663       slotIndMsg->sfn = sfnValue;
664       slotIndMsg->slot = slotValue;
665
666 #ifdef ODU_SLOT_IND_DEBUG_LOG
667       DU_LOG("\n\nDEBUG  -->  PHY_STUB: Sending Slot Indication [%d : %d] to MAC", sfnValue, slotValue);
668 #endif
669
670       /* increment for the next TTI */
671       slotValue++;
672       if(sfnValue >= MAX_SFN_VALUE && slotValue > MAX_SLOT_VALUE)
673       {
674          sfnValue = 0;
675          slotValue = 0;
676       }
677       else if(slotValue > MAX_SLOT_VALUE)
678       {
679          sfnValue++;
680          slotValue = 0;
681       }
682       fillMsgHeader(&slotIndMsg->header, FAPI_SLOT_INDICATION, \
683             sizeof(fapi_slot_ind_t) - sizeof(fapi_msg_t));
684
685       memset(&pst, 0, sizeof(Pst));
686       FILL_PST_PHY_STUB_TO_LWR_MAC(pst, EVT_PHY_STUB_SLOT_IND);
687
688       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
689       if(!mBuf)
690       {
691          DU_LOG("\nERROR  --> PHY_STUB: Failed to allocate memory for slot indication buffer");
692          MAC_FREE_SHRABL_BUF(pst.region, pst.pool, slotIndMsg, sizeof(fapi_slot_ind_t));
693          return RFAILED;
694       }
695       CMCHKPK(oduPackPointer, (PTR)slotIndMsg, mBuf);
696       ODU_POST_TASK(&pst, mBuf);
697    }
698 #endif
699    return ROK;
700 }
701
702 /*******************************************************************
703  *
704  * @brief Handles start request received from MAC
705  *
706  * @details
707  *
708  *    Function : l1HdlStartReq
709  *
710  *    Functionality:
711  *          -Handles start request received from MAC
712  *
713  * @params[in]   Message length
714  *               config request message pointer
715  *
716  * @return void
717  *
718  * ****************************************************************/
719
720 S16 l1HdlStartReq(uint32_t msgLen, void *msg)
721 {
722 #ifdef INTEL_FAPI
723    if(lwrMacCb.phyState == PHY_STATE_CONFIGURED)
724    {
725       l1HdlSlotIndicaion(FALSE);
726       l1StartConsoleHandler();
727       MAC_FREE(msg, msgLen);
728    }
729    else
730    {
731       DU_LOG("\nINFO   -->  PHY_STUB: Received Start Req in PHY State %d", lwrMacCb.phyState);
732       MAC_FREE(msg, msgLen);
733       return RFAILED;
734    }
735 #endif
736    return ROK;
737 }
738
739 /*******************************************************************
740  *
741  * @brief Handles Dl Tti request received from MAC
742  *
743  * @details
744  *
745  *    Function : l1HdlDlTtiReq
746  *
747  *    Functionality:
748  *          -Handles Dl Tti request received from MAC
749  *
750  * @params[in]   Message length
751  *               Dl Tti request message pointer
752  *
753  * @return void
754  *
755  * ****************************************************************/
756
757 S16 l1HdlDlTtiReq(uint16_t msgLen, void *msg)
758 {
759 #ifdef INTEL_FAPI
760    p_fapi_api_queue_elem_t dlTtiElem = (p_fapi_api_queue_elem_t)msg;
761    fapi_dl_tti_req_t *dlTtiReq = (fapi_dl_tti_req_t *)(dlTtiElem +1);
762    
763    uint8_t pduCount = 0;
764
765 #ifdef ODU_SLOT_IND_DEBUG_LOG
766    if(dlTtiReq->nPdus == 0)
767    {
768       DU_LOG("\nDEBUG  -->  PHY_STUB: No PDU in DL TTI Request");
769    }
770    else
771    {  
772      DU_LOG("\nDEBUG  -->  PHY STUB: DL TTI Request at sfn=%d slot=%d",dlTtiReq->sfn,dlTtiReq->slot);
773    }
774 #endif 
775
776    for(pduCount=0; pduCount<dlTtiReq->nPdus; pduCount++)
777    {
778       if(dlTtiReq->pdus[pduCount].pduType == 3) //SSB_PDU_TYPE
779       {
780          DU_LOG("\nINFO   -->  PHY_STUB: SSB PDU");
781       }
782       else if(dlTtiReq->pdus[pduCount].pduType == 0)
783       {
784          DU_LOG("\nINFO   -->  PHY_STUB: PDCCH PDU");
785       }
786       else if(dlTtiReq->pdus[pduCount].pduType == 1)
787       {
788          DU_LOG("\nINFO   -->  PHY_STUB: PDSCH PDU");
789       }
790    }
791
792    /* Free FAPI message */
793    MAC_FREE(msg, msgLen);
794 #endif
795    return ROK;
796 }
797
798 /*******************************************************************
799  *
800  * @brief Handles tx_data request received from MAC
801  *
802  * @details
803  *
804  *    Function : l1HdlTxDataReq
805  *
806  *    Functionality:
807  *          -Handles tx_data request received from MAC
808  *
809  * @params[in]   Message length
810  *               tx_data request message pointer
811  *
812  * @return void
813  *
814  * ****************************************************************/
815
816 S16 l1HdlTxDataReq(uint16_t msgLen, void *msg)
817 {
818 #ifdef INTEL_FAPI
819    p_fapi_api_queue_elem_t txDataElem = (p_fapi_api_queue_elem_t)msg;
820    fapi_tx_data_req_t *txDataReq = (fapi_tx_data_req_t *)(txDataElem +1);
821
822    DU_LOG("\nINFO   -->  PHY STUB: TX DATA Request at sfn=%d slot=%d",txDataReq->sfn,txDataReq->slot);
823 /*
824    if(dlDedMsg)
825    {
826       DU_LOG("\nINFO   -->  PHY_STUB: TxDataPdu for DED MSG sent");
827       dlDedMsg = false;
828    }
829 */
830    MAC_FREE(msg, msgLen);
831 #endif
832    return ROK;
833 }
834
835 #ifdef INTEL_FAPI
836 /*******************************************************************
837  *
838  * @brief Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1
839  *
840  * @details
841  *
842  *    Function : fillPucchF0F1PduInfo
843  *
844  *    Functionality:
845  *       Fills Uci Ind Pdu Info carried on Pucch Format 0/Format 1 
846  *
847  * @params[in] fapi_uci_o_pucch_f0f1_t *
848  *             pucchPdu
849  * @return ROK     - success
850  *         RFAILED - failure
851  *
852  * ****************************************************************/
853 uint8_t fillPucchF0F1PduInfo(fapi_uci_o_pucch_f0f1_t *pduInfo, fapi_ul_pucch_pdu_t pucchPdu)
854 {
855    uint8_t idx = 0;
856
857    pduInfo->handle = pucchPdu.handle;
858    pduInfo->pduBitmap = 1;  //hardcoded for SR
859    pduInfo->pucchFormat = pucchPdu.formatType;
860    pduInfo->ul_cqi = 0;
861    pduInfo->rnti = pucchPdu.rnti;
862    pduInfo->timingAdvance = 0;
863    pduInfo->rssi = 0;
864    if(pduInfo->pduBitmap & SR_PDU_BITMASK)
865    {
866       pduInfo->srInfo.srIndication = SR_DETECTED;
867       pduInfo->srInfo.srConfidenceLevel = CONFDC_LEVEL_GOOD;
868    }
869    if(pduInfo->pduBitmap & HARQ_PDU_BITMASK)
870    {
871       pduInfo->harqInfo.numHarq++;
872       pduInfo->harqInfo.harqConfidenceLevel = CONFDC_LEVEL_GOOD;
873       for(idx = 0; idx < pduInfo->harqInfo.numHarq; idx++)
874       {
875          pduInfo->harqInfo.harqValue[idx] = HARQ_PASS;
876       }
877    }
878    return ROK;
879 }
880 /*******************************************************************
881  *
882  * @brief Fills UCI Pdu Information
883  *
884  * @details
885  *
886  *    Function : fillUciPduInfo
887  *
888  *    Functionality:
889  *       Fills UCI Pdu Information
890  *
891  * @params[in] Pointer to uciPdu
892  *             pucchPdu
893  * @return ROK     - success
894  *         RFAILED - failure
895  *
896  * ****************************************************************/
897 uint8_t fillUciPduInfo(fapi_uci_pdu_info_t *uciPdu, fapi_ul_pucch_pdu_t pucchPdu)
898 {
899    uint8_t ret = ROK;
900
901    /*TODO: The pduType is hardcoded here to support 
902      UCI Ind for PUCCH forat0/format1. This is to be
903      modified when we get SR form UE */
904    uciPdu->pduType = UCI_IND_PUCCH_F0F1;
905    switch(uciPdu->pduType)
906    {
907       case UCI_IND_PUSCH:
908          break;
909       case UCI_IND_PUCCH_F0F1:
910          {
911             fapi_uci_o_pucch_f0f1_t *pduInfo = NULLP;
912
913             pduInfo = &uciPdu->uci.uciPucchF0F1;
914             ret = fillPucchF0F1PduInfo(pduInfo, pucchPdu);
915             uciPdu->pduSize = sizeof(fapi_uci_o_pucch_f0f1_t);
916          }
917          break;
918       case UCI_IND_PUCCH_F2F3F4:
919          break;
920       default:
921          DU_LOG("\nERROR  -->  PHY_STUB: Invalid Pdu Type %d", uciPdu->pduType);
922          break;
923    }
924    return ret;
925 }
926
927 /*******************************************************************
928  *
929  * @brief Build and send Uci indication
930  *
931  * @details
932  *
933  *    Function : l1BuildAndSendUciInd
934  *
935  *    Functionality:
936  *       Build and send Uci indication
937  *
938  * @params[in] SFN
939  *             Slot
940  * @return ROK     - success
941  *         RFAILED - failure
942  *
943  * ****************************************************************/
944 uint8_t l1BuildAndSendUciInd(uint16_t slot, uint16_t sfn, fapi_ul_pucch_pdu_t pucchPdu)
945 {
946    uint8_t uciIdx = 0;
947    uint8_t nUciPdus = 0;
948    uint8_t ret = ROK;
949    uint32_t msgLen = 0;
950    fapi_uci_indication_t *uciInd =  NULLP;
951
952    MAC_ALLOC(uciInd, sizeof(fapi_uci_indication_t));
953    if(!uciInd)
954    {
955       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for UCI Indication");
956       return RFAILED;
957    }
958    memset(uciInd, 0, sizeof(fapi_uci_indication_t));
959    uciInd->sfn = sfn;
960    uciInd->slot = slot;
961    uciInd->numUcis = 1;   //consdering the UCI Ind for SR
962    nUciPdus = uciInd->numUcis;
963    while(nUciPdus)
964    {
965       ret = fillUciPduInfo(&uciInd->uciPdu[uciIdx], pucchPdu);
966       uciIdx++;
967       nUciPdus--;
968    }
969    if(ret == ROK)
970    {
971       msgLen = sizeof(fapi_uci_indication_t)- sizeof(fapi_msg_t);
972       fillMsgHeader(&uciInd->header, FAPI_UCI_INDICATION, msgLen);
973
974       /* Sending UCI indication to MAC */
975       DU_LOG("\nINFO   -->  PHY STUB: Sending UCI Indication to MAC");
976       procPhyMessages(uciInd->header.msg_id, sizeof(fapi_uci_indication_t), (void *)uciInd);
977    }
978    MAC_FREE(uciInd, sizeof(fapi_uci_indication_t));
979    return ret;
980 }
981 #endif
982
983 /*******************************************************************
984  *
985  * @brief Handles Ul Tti request received from MAC
986  *
987  * @details
988  *
989  *    Function : l1HdlUlTtiReq
990  *
991  *    Functionality:
992  *          -Handles Ul Tti request received from MAC
993  *
994  * @params[in]   Message length
995  *               Ul Tti request message pointer
996  *
997  * @return void
998  *
999  * ****************************************************************/
1000
1001 S16 l1HdlUlTtiReq(uint16_t msgLen, void *msg)
1002 {
1003 #ifdef INTEL_FAPI
1004    p_fapi_api_queue_elem_t ulTtiElem = (p_fapi_api_queue_elem_t)msg;
1005    fapi_ul_tti_req_t *ulTtiReq = (fapi_ul_tti_req_t *)(ulTtiElem +1);
1006    uint8_t numPdus = ulTtiReq->nPdus;
1007
1008 #ifdef ODU_SLOT_IND_DEBUG_LOG
1009    if(numPdus == 0)
1010    {
1011       DU_LOG("\nINFO   -->  PHY STUB: No PDU received in UL TTI Req");
1012    }
1013    else
1014    {
1015       DU_LOG("\nINFO   -->  PHY STUB: Received UL TTI Request");
1016    }
1017 #endif 
1018
1019    while(numPdus)
1020    {
1021       if(ulTtiReq->pdus[numPdus-1].pduType == 0)
1022       {
1023          DU_LOG("\nINFO   -->  PHY STUB: PRACH PDU");
1024       }
1025       if(ulTtiReq->pdus[numPdus-1].pduType == 1)
1026       {
1027          DU_LOG("\nINFO   -->  PHY STUB: PUSCH PDU");
1028          l1BuildAndSendRxDataInd(ulTtiReq->slot, ulTtiReq->sfn, \
1029                ulTtiReq->pdus[numPdus-1].pdu.pusch_pdu); 
1030       }
1031       if(ulTtiReq->pdus[numPdus-1].pduType == 2)
1032       {
1033          DU_LOG("\nINFO   -->  PHY STUB: PUCCH PDU");
1034          fapi_ul_tti_req_t ulTtiSlotInd;
1035          memset(&ulTtiSlotInd, 0, sizeof(fapi_ul_tti_req_t));
1036          ulTtiSlotInd.slot = ulTtiReq->slot;
1037          ulTtiSlotInd.sfn  = ulTtiReq->sfn;
1038          ADD_DELTA_TO_TIME(ulTtiSlotInd, ulTtiSlotInd, SLOT_DELAY);
1039          l1BuildAndSendUciInd(ulTtiSlotInd.slot, ulTtiSlotInd.sfn, \
1040                ulTtiReq->pdus[numPdus-1].pdu.pucch_pdu);
1041       }
1042       numPdus--;
1043    }
1044
1045    if(ueDb.ueCb[UE_IDX_0].rachIndSent == false && ulTtiReq->sfn == 16 && ulTtiReq->slot == 6)
1046    {
1047       ueDb.ueCb[UE_IDX_0].rachIndSent = true;
1048       l1BuildAndSendRachInd(ulTtiReq->slot, ulTtiReq->sfn);
1049       ueDb.numActvUe++;
1050    }
1051
1052    //Following can be enabled to test with a second UE
1053 #if 0
1054    if(ueDb.ueCb[UE_IDX_1].rachIndSent == false && ulTtiReq->sfn == 304 && ulTtiReq->slot == 0)
1055    {
1056       ueDb.ueCb[UE_IDX_1].rachIndSent = true;
1057       l1BuildAndSendRachInd(ulTtiReq->slot, ulTtiReq->sfn);
1058       ueDb.numActvUe++;
1059    }
1060 #endif
1061
1062    MAC_FREE(msg, msgLen);
1063 #endif
1064    return ROK;
1065 }
1066
1067 /*******************************************************************
1068  *
1069  * @brief Builds and Send the stop Indication message to MAC
1070  *
1071  * @details
1072  *
1073  *    Function : l1BuildAndSendStopInd
1074  *
1075  *    Functionality:
1076  *          -Send the Stop indication Message to MAC
1077  *
1078  *
1079  * @return void
1080  *
1081  * ****************************************************************/
1082 uint16_t l1BuildAndSendStopInd()
1083 {
1084 #ifdef INTEL_FAPI
1085    Pst pst;
1086    Buffer *mBuf = NULLP;
1087    fapi_stop_ind_t *stopIndMsg = NULLP;
1088    uint32_t msgLen = 0;
1089
1090    MAC_ALLOC_SHRABL_BUF(stopIndMsg, sizeof(fapi_stop_ind_t));
1091    if(!stopIndMsg)
1092    {
1093       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for stop Indication Message");
1094       return RFAILED;
1095    }
1096    else
1097    {
1098       fillMsgHeader(&stopIndMsg->header, FAPI_STOP_INDICATION, msgLen);
1099       DU_LOG("\n\nINFO   -->  PHY_STUB: Processing Stop indication to MAC");
1100
1101       memset(&pst, 0, sizeof(Pst));
1102       FILL_PST_PHY_STUB_TO_LWR_MAC(pst, EVT_PHY_STUB_STOP_IND);
1103       ODU_GET_MSG_BUF(pst.region, pst.pool, &mBuf);
1104       if(!mBuf)
1105       {
1106          DU_LOG("\nERROR  --> PHY_STUB: Failed to allocate memory for slot indication buffer");
1107          MAC_FREE_SHRABL_BUF(pst.region, pst.pool, stopIndMsg, sizeof(fapi_stop_ind_t));
1108          return RFAILED;
1109       }
1110       CMCHKPK(oduPackPointer, (PTR)stopIndMsg, mBuf);
1111       ODU_POST_TASK(&pst, mBuf);
1112    }
1113 #endif
1114    return ROK;
1115 }
1116
1117 /*******************************************************************
1118  *
1119  * @brief Handles stop request received from MAC
1120  *
1121  * @details
1122  *
1123  *    Function : l1HdlStopReq
1124  *
1125  *    Functionality:
1126  *          -Handles stop request received from MAC
1127  *
1128  * @params[in]   Message length
1129  *               stop request message pointer
1130  *
1131  * @return void
1132  *
1133  * ****************************************************************/
1134
1135 S16 l1HdlStopReq(uint32_t msgLen, void *msg)
1136 {
1137 #ifdef INTEL_FAPI
1138    if(lwrMacCb.phyState == PHY_STATE_RUNNING)
1139    {
1140       l1HdlSlotIndicaion(TRUE);
1141
1142       /* Initialize all global variables */
1143       sfnValue = 0;
1144       slotValue = 0;
1145       memset(&ueDb, 0, sizeof(UeDb));
1146
1147       DU_LOG("\nINFO   -->  PHY_STUB: Slot Indication is stopped successfully");
1148       MAC_FREE(msg, msgLen);
1149    }
1150    else
1151    {
1152       DU_LOG("\nINFO  -->  PHY_STUB: Received Stop Req in PHY State %d", lwrMacCb.phyState);
1153       MAC_FREE(msg, msgLen);
1154       return RFAILED;
1155    }
1156 #endif
1157    return ROK;
1158 }
1159
1160 #if 0
1161 /*******************************************************************
1162  *
1163  * @brief Build And Send Rx Data Ind for Msg5
1164  *
1165  * @details
1166  *
1167  *    Function : l1BuildAndSendMsg5
1168  *
1169  *    Functionality: Build And Send Rx Data Ind for Msg5
1170  *
1171  * @params[in] SFN
1172  *             Slot
1173  * @return ROK     - success
1174  *         RFAILED - failure
1175  *
1176  * ****************************************************************/
1177 uint8_t l1BuildAndSendMsg5(uint16_t sfn, uint16_t slot)
1178 {   
1179 #ifdef INTEL_FAPI
1180    uint8_t idx = 0;
1181    fapi_rx_data_indication_t *rxDataInd;
1182    fapi_pdu_ind_info_t       *pduInfo;
1183    uint8_t  *pdu;
1184    uint16_t byteIdx = 0;
1185    uint32_t msgLen = 0;
1186
1187    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1188    if(!rxDataInd)
1189    {
1190       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication for msg5");
1191       return RFAILED;
1192    }
1193    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1194
1195    /* TODO: Fill the required values */
1196    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1197    rxDataInd->sfn = sfn;
1198    rxDataInd->slot = slot;
1199    rxDataInd->numPdus = 1;
1200
1201    pduInfo = &rxDataInd->pdus[idx];
1202    pduInfo->handle = 100;
1203    pduInfo->rnti = 100;
1204    pduInfo->harqId = 1;
1205    pduInfo->pdu_length = 25;
1206    pduInfo->ul_cqi = 0;
1207    pduInfo->timingAdvance = 0;
1208    pduInfo->rssi = 0;
1209
1210    /* Filling pdu with random values for testing */
1211    pduInfo->pduData = NULL;
1212    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1213    if(!pduInfo->pduData)
1214    {
1215       DU_LOG("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1216       return RFAILED;
1217    }
1218
1219    /* Filling PDU */
1220    pdu = (uint8_t *)pduInfo->pduData;
1221
1222    uint8_t  msg5PduLen = 33;
1223    /* For RRC setup complete
1224       MAC subheader format is R/F/LCId/L (2/3 bytes)
1225       LCId is 1 for SRB1
1226       L is length of PDU i.e 6bytes here
1227       From 38.321 section 6.1.1
1228     */
1229    uint8_t msg5[] = {1, msg5PduLen, 0, 0, 16, 0, 5, 223, 128, 16, 94, \
1230       64, 3, 64, 89, 61, 138, 64, 0, 0, 0, 4, 0, 0, 4, 68, 11, 128, \
1231          184, 56, 0, 0, 0, 0, 0};
1232
1233    msg5PduLen += 2;  /* 2bytes of header */
1234    memcpy(pdu, &msg5, msg5PduLen);
1235    byteIdx += msg5PduLen; /* 2 bytes of header */
1236
1237    /* Filling MAC SDU for Padding bytes*/
1238    if(byteIdx < pduInfo->pdu_length)
1239    {
1240       /* For Padding
1241          MAC subheader format is R/R/LCId (1byte)
1242          LCId is 63 for padding
1243          From 38.321 section 6.1.1
1244        */
1245       pdu[byteIdx++] = 63;
1246
1247       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1248          pdu[byteIdx] = 0;
1249    }
1250    msgLen += pduInfo->pdu_length;
1251
1252    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1253
1254    /* Sending Rx data indication to MAC */
1255    DU_LOG("\nINFO   -->  PHY STUB: Sending Rx data Indication to MAC");
1256    procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1257
1258    if(pduInfo->pdu_length)
1259       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1260    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1261 #endif
1262    return ROK;
1263 }
1264 #endif
1265
1266 /*******************************************************************
1267  *
1268  * @brief Handles Ul Dci request received from MAC
1269  *
1270  * @details
1271  *
1272  *    Function : l1HdlUlDciReq
1273  *
1274  *    Functionality:
1275  *          -Handles Ul Dci request received from MAC
1276  *
1277  * @params[in]   Message length
1278  *               Ul Dci request message pointer
1279  *
1280  * @return void
1281  *
1282  * ****************************************************************/
1283
1284 S16 l1HdlUlDciReq(uint16_t msgLen, void *msg)
1285 {
1286 #ifdef INTEL_FAPI
1287    p_fapi_api_queue_elem_t ulDciElem = (p_fapi_api_queue_elem_t)msg;
1288    fapi_ul_dci_req_t *ulDciReq = (fapi_ul_dci_req_t *)(ulDciElem +1);
1289    uint8_t numPdus = ulDciReq->numPdus;
1290
1291    while(numPdus)
1292    {
1293       if(ulDciReq->pdus[numPdus-1].pduType == 0)
1294       {
1295          DU_LOG("\nINFO   -->  PHY STUB: Received UL DCI Request for PDCCH PDU");
1296          //l1BuildAndSendMsg5(ulDciReq->sfn, ulDciReq->slot);
1297          //msg5Sent = true;
1298       }
1299       numPdus--;
1300    }
1301
1302    MAC_FREE(msg, msgLen);
1303 #endif
1304    return ROK;
1305 }
1306
1307 /*******************************************************************
1308  *
1309  * @brief Send UL user data to DU
1310  *
1311  * @details
1312  *
1313  *    Function : l1SendUlUserData
1314  *
1315  *    Functionality: Send UL user data to DU
1316  *
1317  * @params[in]
1318  * @return ROK     - success
1319  *         RFAILED - failure
1320  *
1321  * ****************************************************************/
1322 uint8_t l1SendUlUserData(uint8_t drbId)
1323 {
1324    uint8_t cnt = 0;
1325    fapi_rx_data_indication_t *rxDataInd;
1326    fapi_pdu_ind_info_t       *pduInfo;
1327    uint8_t  *pdu = NULLP;
1328    uint16_t byteIdx = 0;
1329    uint32_t msgLen = 0;
1330    uint8_t idx = 0;
1331    uint8_t lcId = 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     lcId = MIN_DRB_LCID + drbId;
1382     uint8_t ulMsg[] = {lcId, msgLen,   0, 0, 0, 0, 0, 50, 0, 0, 0, 0, 0, 1, 0, 0, 192, 168, 130, 81, 192, 168, 130, 82, 84, 104,
1383     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};
1384     msgLen += 2;  /* 2bytes of header */
1385     memcpy(pdu, &ulMsg, msgLen);
1386     byteIdx += msgLen; /* 2 bytes of header */
1387
1388    /* Filling MAC SDU for Padding bytes*/
1389    if(byteIdx < pduInfo->pdu_length)
1390    {
1391       /* For Padding
1392          MAC subheader format is R/R/LCId (1byte)
1393          LCId is 63 for padding
1394          From 38.321 section 6.1.1
1395        */
1396       pdu[byteIdx++] = 63;
1397
1398       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1399          pdu[byteIdx] = 0;
1400    }
1401    msgLen += pduInfo->pdu_length;
1402
1403    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1404
1405     /* Send Message to peer */
1406     while(cnt < NUM_UL_PACKETS)
1407     {
1408        DU_LOG("\nDEBUG  -->  PHY STUB : Sending UL User Data[%d][LCID:%d] at sfn %d slot %d", cnt+1, lcId, sfnValue, slotValue);
1409        /* Sending Rx data indication to MAC */
1410        rxDataInd->sfn = sfnValue;
1411        rxDataInd->slot = slotValue;
1412        procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1413        cnt++;
1414     }
1415
1416     if(pduInfo->pdu_length)
1417        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1418     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1419     return ROK;
1420 }
1421
1422 /*******************************************************************
1423  *
1424  * @brief Sends RLC Status PDU to DU
1425  *
1426  * @details
1427  *
1428  *    Function : l1SendStatusPdu
1429  *
1430  *    Functionality: Send RLC Status PDU to DU
1431  *
1432  * @params[in]
1433  * @return ROK     - success
1434  *         RFAILED - failure
1435  *
1436  * ****************************************************************/
1437 uint8_t l1SendStatusPdu()
1438 {
1439    fapi_rx_data_indication_t *rxDataInd;
1440    fapi_pdu_ind_info_t       *pduInfo;
1441    uint8_t  *pdu = NULLP;
1442    uint16_t byteIdx = 0;
1443    uint32_t msgLen = 0;
1444    uint8_t idx = 0;
1445
1446    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
1447    if(!rxDataInd)
1448    {
1449       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Indication");
1450       return RFAILED;
1451    }
1452    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
1453
1454    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
1455    rxDataInd->sfn = 0;
1456    rxDataInd->slot = 0;
1457    rxDataInd->numPdus = 1;
1458
1459    /* TODO : Fill pduInfo using PUSCH PDU. Currently hardcoding */
1460    pduInfo = &rxDataInd->pdus[idx];
1461    pduInfo->handle = 100;
1462    pduInfo->rnti = 100;
1463    pduInfo->harqId = 1;
1464    /* Since status pdu size = 3bytes and 2 bytes of MAC header,
1465     * setting tbsize = 24 from Table 5.1.3.2-1 spec 38.214 */
1466    pduInfo->pdu_length = 24;
1467    pduInfo->ul_cqi = 0;
1468    pduInfo->timingAdvance = 0;
1469    pduInfo->rssi = 0;
1470
1471    /* Filling pdu with random values for testing */
1472    pduInfo->pduData = NULL;
1473    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
1474    if(!pduInfo->pduData)
1475    {
1476       printf("\nERROR  -->  PHY_STUB: Memory allocation failed for Rx Data Pdu");
1477       return RFAILED;
1478    }
1479
1480    /* Filling PDU */
1481    pdu = (uint8_t *)pduInfo->pduData;
1482    msgLen = 3;
1483
1484    /* For RLC Status PDU
1485       MAC subheader format is R/F/LCId/L (2/3 bytes)
1486       LCId is 4 for DRB1
1487       L is length of Status PDU i.e 3 bytes
1488       From 38.321 section 6.1.1
1489     */
1490    uint8_t statusPdu[] = {4, msgLen, 0, 0, 0};
1491    msgLen += 2;  /* 2bytes of header */
1492    memcpy(pdu, &statusPdu, msgLen);
1493    byteIdx += msgLen; /* 2 bytes of header */
1494
1495
1496    /* Filling MAC SDU for Padding bytes*/
1497    if(byteIdx < pduInfo->pdu_length)
1498    {
1499       /* For Padding
1500          MAC subheader format is R/R/LCId (1byte)
1501          LCId is 63 for padding
1502          From 38.321 section 6.1.1
1503        */
1504       pdu[byteIdx++] = 63;
1505
1506       for(; byteIdx < pduInfo->pdu_length; byteIdx++)
1507          pdu[byteIdx] = 0;
1508    }
1509    msgLen += pduInfo->pdu_length;
1510
1511    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
1512
1513     /* Send Message to peer */
1514     DU_LOG("\nDEBUG  -->  PHY STUB : Sending RLC status pdu at sfn %d slot %d", sfnValue, slotValue);
1515     /* Sending Rx data indication to MAC */
1516     rxDataInd->sfn = sfnValue;
1517     rxDataInd->slot = slotValue;
1518     procPhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
1519
1520     if(pduInfo->pdu_length)
1521        MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
1522     MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
1523     return ROK;
1524 }
1525
1526 /*******************************************************************
1527  *
1528  * @brief Receives message from MAC
1529  *
1530  * @details
1531  *
1532  *    Function :  l1ProcessFapiRequest
1533  *
1534  *    Functionality:
1535  *       - Receives message from MAC and calls handler
1536  *
1537  * @params[in] Message type
1538  *             Message length
1539  *             Message pointer
1540  *
1541  * @return void
1542  *
1543  * ****************************************************************/
1544
1545 void l1ProcessFapiRequest(uint8_t msgType, uint32_t msgLen, void *msg)
1546 {
1547    switch(msgType)
1548    {
1549 #ifdef INTEL_FAPI
1550       case FAPI_PARAM_REQUEST:
1551          l1HdlParamReq(msgLen, msg);
1552          break;
1553       case FAPI_CONFIG_REQUEST:
1554          l1HdlConfigReq(msgLen, msg);
1555          break;
1556       case FAPI_START_REQUEST:
1557          l1HdlStartReq(msgLen, msg);
1558          break;
1559       case FAPI_DL_TTI_REQUEST:
1560          l1HdlDlTtiReq(msgLen, msg);
1561          break;
1562       case FAPI_TX_DATA_REQUEST:
1563          l1HdlTxDataReq(msgLen, msg);
1564          break;
1565       case FAPI_UL_TTI_REQUEST:
1566          l1HdlUlTtiReq(msgLen, msg);
1567          break;
1568       case FAPI_STOP_REQUEST:
1569          l1HdlStopReq(msgLen, msg);
1570          break;
1571       case FAPI_UL_DCI_REQUEST:
1572          l1HdlUlDciReq(msgLen, msg);
1573          break;
1574       default:
1575          DU_LOG("\nERROR  -->  PHY_STUB: Invalid message type[%x] received at PHY", msgType);
1576          break;
1577 #endif
1578    }
1579 }
1580 /**********************************************************************
1581   End of file
1582  **********************************************************************/