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