fix ODUHIGH-182 updating code to be compatible with latest available fapi_inetrface...
[o-du/l2.git] / src / phy_stub / l1_bdy1.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 "lwr_mac.h"
23 #include "lwr_mac_phy.h"
24 #ifdef INTEL_FAPI
25 #include "fapi.h"
26 #endif
27 #include "lphy_stub.h"
28 #include "stdbool.h"
29 #include "du_log.h"
30 #include "rg.h"
31
32 #define MAX_SLOT_VALUE   9
33 #define MAX_SFN_VALUE    1023
34 #define NR_PCI            1
35
36 uint16_t sfnValue = 0;
37 uint16_t slotValue = 0;
38 bool     rachIndSent = false;
39
40 EXTERN void phyToMac ARGS((uint16_t msgType, uint32_t msgLen,void *msg));
41 #ifdef INTEL_FAPI
42 EXTERN void fillTlvs ARGS((fapi_uint16_tlv_t *tlv, uint16_t tag, uint16_t
43 length, uint16_t value, uint32_t *msgLen));
44 EXTERN void fillMsgHeader ARGS((fapi_msg_t *hdr, uint16_t msgType, uint16_t msgLen));
45 #endif
46 EXTERN void handlePhyMessages(uint16_t msgType, uint32_t msgSize, void *msg);
47
48 /*******************************************************************
49  *
50  * @brief Builds and sends param response to MAC CL
51  *
52  * @details
53  *
54  *    Function : l1BldAndSndParamRsp
55  *
56  *    Functionality:
57  *          - Builds and sends param response to MAC
58  *
59  * @params[in] Config request message pointer
60  * @return ROK     - success
61  *         RFAILED - failure
62  *
63  * ****************************************************************/
64 S16 l1BldAndSndParamRsp(void *msg)
65 {
66 #ifdef INTEL_FAPI
67    uint8_t index = 0;
68    uint32_t msgLen = 0;
69         fapi_param_resp_t *fapiParamRsp;
70         
71         MAC_ALLOC(fapiParamRsp, sizeof(fapi_param_resp_t));
72         if(!fapiParamRsp)
73         {
74            DU_LOG("PHY STUB: Memory allocation failed");
75                 return RFAILED;
76         }
77
78   /* Cell Params */
79   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_RELEASE_CAPABILITY_TAG,                          sizeof(uint16_t), 1, &msgLen);
80   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PHY_STATE_TAG,                                   sizeof(uint16_t), 0, &msgLen);
81   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SKIP_BLANK_DL_CONFIG_TAG,                        sizeof(uint8_t),  0, &msgLen);
82   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SKIP_BLANK_UL_CONFIG_TAG,                        sizeof(uint8_t),  0, &msgLen);
83   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_NUM_CONFIG_TLVS_TO_REPORT_TYPE_TAG,              sizeof(uint8_t),  0, &msgLen);
84
85   /* Carrier Params */
86   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CYCLIC_PREFIX_TAG,                               sizeof(uint8_t),  1, &msgLen);
87   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_SUBCARRIER_SPACING_DL_TAG,             sizeof(uint8_t),  1, &msgLen);
88   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_BANDWIDTH_DL_TAG,                      sizeof(uint16_t), 1, &msgLen);
89   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_SUBCARRIER_SPACING_UL_TAG,             sizeof(uint8_t),  0, &msgLen);
90   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_BANDWIDTH_UL_TAG,                      sizeof(uint16_t), 0, &msgLen);
91
92   /* PDCCH Param*/
93   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CCE_MAPPING_TYPE_TAG,                            sizeof(uint8_t), 0, &msgLen);
94   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_CORESET_OUTSIDE_FIRST_3_OFDM_SYMS_OF_SLOT_TAG,   sizeof(uint8_t), 0, &msgLen);
95   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRECODER_GRANULARITY_CORESET_TAG,                sizeof(uint8_t), 0, &msgLen);
96   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDCCH_MU_MIMO_TAG,                               sizeof(uint8_t), 0, &msgLen);
97   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDCCH_PRECODER_CYCLING_TAG,                      sizeof(uint8_t), 0, &msgLen);
98   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDCCHS_PER_SLOT_TAG,                         sizeof(uint8_t), 0, &msgLen);
99
100   /* PUCCH Param */
101   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUCCH_FORMATS_TAG,                               sizeof(uint8_t), 0, &msgLen);
102   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PUCCHS_PER_SLOT_TAG,                         sizeof(uint8_t), 0, &msgLen);
103
104   /* PDSCH Param */
105   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_MAPPING_TYPE_TAG,                          sizeof(uint8_t), 0, &msgLen);
106   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_ALLOCATION_TYPES_TAG,                      sizeof(uint8_t), 0, &msgLen);
107   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_VRB_TO_PRB_MAPPING_TAG,                    sizeof(uint8_t), 0, &msgLen);
108   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_CBG_TAG,                                   sizeof(uint8_t), 0, &msgLen);
109   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_CONFIG_TYPES_TAG,                     sizeof(uint8_t), 0, &msgLen);
110   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_MAX_LENGTH_TAG,                       sizeof(uint8_t), 0, &msgLen);
111   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DMRS_ADDITIONAL_POS_TAG,                   sizeof(uint8_t), 0, &msgLen);
112   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDSCHS_TBS_PER_SLOT_TAG,                     sizeof(uint8_t), 0, &msgLen);
113   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_NUMBER_MIMO_LAYERS_PDSCH_TAG,                sizeof(uint8_t), 0, &msgLen);
114   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_MAX_MODULATION_ORDER_DL_TAG,           sizeof(uint8_t), 0, &msgLen);
115   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_MU_MIMO_USERS_DL_TAG,                        sizeof(uint8_t), 0, &msgLen);
116   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_DATA_IN_DMRS_SYMBOLS_TAG,                  sizeof(uint8_t), 0, &msgLen);
117   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PREMPTIONSUPPORT_TAG,                            sizeof(uint8_t), 0, &msgLen);
118   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PDSCH_NON_SLOT_SUPPORT_TAG,                      sizeof(uint8_t), 0, &msgLen);
119
120   /* PUSCH Param */
121   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_UCI_MUX_ULSCH_IN_PUSCH_TAG,                      sizeof(uint8_t), 0, &msgLen);
122   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_UCI_ONLY_PUSCH_TAG,                              sizeof(uint8_t), 0, &msgLen);
123   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_FREQUENCY_HOPPING_TAG,                     sizeof(uint8_t), 0, &msgLen);
124   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_CONFIG_TYPES_TAG,                     sizeof(uint8_t), 0, &msgLen);
125   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_MAX_LEN_TAG,                          sizeof(uint8_t), 0, &msgLen);
126   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_DMRS_ADDITIONAL_POS_TAG,                   sizeof(uint8_t), 0, &msgLen);
127   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_CBG_TAG,                                   sizeof(uint8_t), 0, &msgLen);
128   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_MAPPING_TYPE_TAG,                          sizeof(uint8_t), 0, &msgLen);
129   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_ALLOCATION_TYPES_TAG,                      sizeof(uint8_t), 0, &msgLen);
130   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_VRB_TO_PRB_MAPPING_TAG,                    sizeof(uint8_t), 0, &msgLen);
131   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_MAX_PTRS_PORTS_TAG,                        sizeof(uint8_t), 0, &msgLen);
132   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PDUSCHS_TBS_PER_SLOT_TAG,                    sizeof(uint8_t), 0, &msgLen);
133   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_NUMBER_MIMO_LAYERS_NON_CB_PUSCH_TAG,         sizeof(uint8_t), 0, &msgLen);
134   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_SUPPORTED_MODULATION_ORDER_UL_TAG,               sizeof(uint8_t), 0, &msgLen);
135   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_MU_MIMO_USERS_UL_TAG,                        sizeof(uint8_t), 0, &msgLen);
136   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_DFTS_OFDM_SUPPORT_TAG,                           sizeof(uint8_t), 0, &msgLen);
137   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PUSCH_AGGREGATION_FACTOR_TAG,                    sizeof(uint8_t), 0, &msgLen);
138
139   /* PRACH Params */
140   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_LONG_FORMATS_TAG,                          sizeof(uint8_t), 0, &msgLen);
141   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_SHORT_FORMATS_TAG,                         sizeof(uint8_t), 0, &msgLen);
142   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_PRACH_RESTRICTED_SETS_TAG,                       sizeof(uint8_t), 0, &msgLen);
143   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_MAX_PRACH_FD_OCCASIONS_IN_A_SLOT_TAG,            sizeof(uint8_t), 0, &msgLen);
144
145   /* MEASUREMENT TAG */
146   fillTlvs(&fapiParamRsp->tlvs[index++],  FAPI_RSSI_MEASUREMENT_SUPPORT_TAG,                    sizeof(uint8_t), 0, &msgLen);
147
148   fapiParamRsp->number_of_tlvs = index;
149   msgLen += sizeof(fapi_param_resp_t) - sizeof(fapi_msg_t);
150   fillMsgHeader(&fapiParamRsp->header, FAPI_PARAM_RESPONSE, msgLen);
151   fapiParamRsp->error_code = MSG_OK;
152
153   DU_LOG("\nPHY_STUB: Sending Param Request to Lower Mac");
154   handlePhyMessages(fapiParamRsp->header.msg_id, sizeof(fapi_param_resp_t), (void *)fapiParamRsp);
155   MAC_FREE(fapiParamRsp, sizeof(fapi_param_resp_t));
156 #endif
157   return ROK;
158 }
159
160 /*******************************************************************
161  *
162  * @brief Builds and sends config response to lower mac
163  *
164  * @details
165  *
166  *    Function : l1BldAndSndConfigRsp
167  *
168  *    Functionality:
169  *          - Builds and sends config response to MAC
170  *
171  * @params[in] Config request message pointer
172  * @return ROK     - success
173  *         RFAILED - failure
174  *
175  * ****************************************************************/
176
177 S16 l1BldAndSndConfigRsp(void *msg)
178 {
179 #ifdef INTEL_FAPI
180    uint32_t msgLen = 0;
181    fapi_config_resp_t *fapiConfigRsp;
182
183    MAC_ALLOC(fapiConfigRsp, sizeof(fapi_config_resp_t));
184    if(!fapiConfigRsp)
185    {
186       DU_LOG("PHY STUB: Memory allocation failed");
187       return RFAILED;
188    }
189    memset(fapiConfigRsp, 0, sizeof(fapi_config_resp_t));
190    fapiConfigRsp->number_of_invalid_tlvs = NULLP;
191    fapiConfigRsp->number_of_inv_tlvs_idle_only = NULLP;
192    fapiConfigRsp->number_of_missing_tlvs = NULLP;
193    fapiConfigRsp->error_code = MSG_OK;
194         msgLen = sizeof(fapi_config_resp_t) - sizeof(fapi_msg_t);
195    fillMsgHeader(&fapiConfigRsp->header, FAPI_CONFIG_RESPONSE, msgLen);
196
197    DU_LOG("\nPHY_STUB: Sending Config Response to Lower Mac");
198    handlePhyMessages(fapiConfigRsp->header.msg_id, \
199            sizeof(fapi_config_resp_t), (void *)fapiConfigRsp);
200    MAC_FREE(fapiConfigRsp, sizeof(fapi_config_resp_t));
201 #endif
202    return ROK;
203 }
204 /*******************************************************************
205  *
206  * @brief Handles param request received from MAC
207  *
208  * @details
209  *
210  *    Function : l1HdlParamReq
211  *
212  *    Functionality:
213  *          -Handles param request received from MAC
214  *
215  * @params[in]   Message length
216  *               Param request message pointer
217  *
218  * @return void
219  *
220  * ****************************************************************/
221
222 PUBLIC void l1HdlParamReq(uint32_t msgLen, void *msg)
223 {
224 #ifdef INTEL_FAPI
225    DU_LOG("\nPHY_STUB: Received Param Request in PHY");
226  
227    /* Build and send PARAM RESPONSE */
228    if(l1BldAndSndParamRsp(msg)!= ROK)
229    {
230       DU_LOG("\nPHY_STUB: Failed Sending Param Response");
231    }
232         MAC_FREE(msg, sizeof(fapi_param_req_t));
233 #endif
234
235
236 /*******************************************************************
237  *
238  * @brief Handles config request received from MAC
239  *
240  * @details
241  *
242  *    Function : l1HdlConfigReq
243  *
244  *    Functionality:
245  *          -Handles config request received from MAC
246  *
247  * @params[in]   Message length
248  *               config request message pointer
249  *
250  * @return void
251  *
252  * ****************************************************************/
253
254 PUBLIC void l1HdlConfigReq(uint32_t msgLen, void *msg)
255 {
256 #ifdef INTEL_FAPI
257         fapi_config_req_t *configReq = (fapi_config_req_t *)msg;
258
259    DU_LOG("\nPHY_STUB: Received Config Request in PHY");
260
261    /* Handling CONFIG RESPONSE */
262    if(l1BldAndSndConfigRsp(msg)!= ROK)
263    {
264       printf("\nPHY_STUB: Failed Sending config Response");
265    }
266    
267         MAC_FREE(configReq, msgLen);
268 #endif
269
270 }
271
272 /*******************************************************************
273  *
274  * @brief Build and Send CRC Indication
275  *
276  * @details
277  *
278  *    Function : l1BuildAndSendCrcInd
279  *
280  *    Functionality:
281  *      Build and Send CRC Indication
282  *
283  * @params[in] Slot
284  *             SFN 
285  * @return ROK     - success
286  *         RFAILED - failure
287  *
288  * ****************************************************************/
289 uint16_t l1BuildAndSendCrcInd(uint16_t slot, uint16_t sfn)
290 {
291 #ifdef INTEL_FAPI
292    uint8_t idx = 0;
293    fapi_crc_ind_t  *crcInd;
294    
295    MAC_ALLOC(crcInd, sizeof(fapi_crc_ind_t));
296    if(!crcInd)
297    {
298       printf("\nPHY_STUB: Memory allocation failed for CRC Indication Message");
299       return RFAILED;
300    }
301         memset(crcInd, 0, sizeof(fapi_crc_ind_t));
302
303    /* TODO: Fill the required values. As of now only 1 CRC status PASS is filled */
304    crcInd->sfn = sfn;
305    crcInd->slot = slot;
306    crcInd->numCrcs = 1;
307
308    crcInd->crc[idx].handle = 0;
309    crcInd->crc[idx].rnti = 0;
310    crcInd->crc[idx].harqId = 0;
311    crcInd->crc[idx].tbCrcStatus = 0;
312    crcInd->crc[idx].numCb = 1;
313    crcInd->crc[idx].cbCrcStatus[0] = 0;
314    crcInd->crc[idx].ul_cqi = 0;
315    crcInd->crc[idx].timingAdvance = 0;
316    crcInd->crc[idx].rssi = 0;
317
318    fillMsgHeader(&crcInd->header, FAPI_CRC_INDICATION, \
319       sizeof(fapi_crc_ind_t) - sizeof(fapi_msg_t));
320
321    /* Sending RACH indication to MAC */
322    DU_LOG("\nPHY STUB: Sending CRC Indication to MAC");
323    handlePhyMessages(crcInd->header.msg_id, sizeof(fapi_crc_ind_t), (void *)crcInd);
324    MAC_FREE(crcInd, sizeof(fapi_crc_ind_t));
325 #endif
326    return ROK;
327 } /* l1BuildAndSendCrcInd */
328
329 #ifdef INTEL_FAPI
330 /*******************************************************************
331  *
332  * @brief Build and send Rx data indication
333  *
334  * @details
335  *
336  *    Function : l1BuildAndSendRxDataInd
337  *
338  *    Functionality:
339  *       Build and send Rx data indication
340  *
341  * @params[in] SFN
342  *             Slot
343  * @return ROK     - success
344  *         RFAILED - failure
345  *
346  * ****************************************************************/
347 uint16_t l1BuildAndSendRxDataInd(uint16_t slot, uint16_t sfn, fapi_ul_pusch_pdu_t puschPdu)
348 {
349    uint8_t idx = 0;
350    fapi_rx_data_indication_t *rxDataInd;
351    fapi_pdu_ind_info_t       *pduInfo;
352    uint8_t  *pdu;
353         uint16_t byteIdx = 0;
354         uint32_t msgLen = 0;
355  
356    MAC_ALLOC(rxDataInd, sizeof(fapi_rx_data_indication_t));
357    if(!rxDataInd)
358    {
359       printf("\nPHY_STUB: Memory allocation failed for Rx Data Indication");
360       return RFAILED;
361    }
362    memset(rxDataInd, 0, sizeof(fapi_rx_data_indication_t));
363
364    /* TODO: Fill the required values */
365    msgLen = sizeof(fapi_rx_data_indication_t) - sizeof(fapi_msg_t);
366    rxDataInd->sfn = sfn;
367    rxDataInd->slot = slot;
368    rxDataInd->numPdus = 1;
369
370    pduInfo = &rxDataInd->pdus[idx];
371    pduInfo->handle = puschPdu.handle;
372    pduInfo->rnti = puschPdu.rnti;
373    pduInfo->harqId = puschPdu.puschData.harqProcessId;
374    pduInfo->pdu_length = puschPdu.puschData.tbSize;
375    pduInfo->ul_cqi = 0;
376    pduInfo->timingAdvance = 0;
377    pduInfo->rssi = 0;
378
379    /* Filling pdu with random values for testing */
380    pduInfo->pduData = NULL;
381    MAC_ALLOC(pduInfo->pduData, pduInfo->pdu_length);
382    if(!pduInfo->pduData)
383    {
384       printf("\nPHY_STUB: Memory allocation failed for Rx Data Pdu");
385       return RFAILED;
386    }
387  
388    /* Harcoded Initial RRC setup Request */
389    pdu = (uint8_t *)pduInfo->pduData;
390    pdu[byteIdx++] = 0;
391    pdu[byteIdx++] = 181;
392    pdu[byteIdx++] = 99;
393    pdu[byteIdx++] = 20;
394    pdu[byteIdx++] = 170;
395    pdu[byteIdx++] = 132;
396    pdu[byteIdx++] = 96;
397
398         /* Harcoding the pad bytes */
399         pdu[byteIdx++] = 63;
400
401         for(; byteIdx < pduInfo->pdu_length; byteIdx++)
402            pdu[byteIdx] = 0;
403    msgLen += pduInfo->pdu_length;
404
405    fillMsgHeader(&rxDataInd->header, FAPI_RX_DATA_INDICATION, msgLen);
406
407    /* Sending Rx data indication to MAC */
408    DU_LOG("\nPHY STUB: Sending Rx data Indication to MAC");
409    handlePhyMessages(rxDataInd->header.msg_id, sizeof(fapi_rx_data_indication_t), (void *)rxDataInd);
410
411    if(pduInfo->pdu_length)
412       MAC_FREE(pduInfo->pduData, pduInfo->pdu_length);
413    MAC_FREE(rxDataInd, sizeof(fapi_rx_data_indication_t));
414    return ROK;
415 }
416 #endif
417
418 /*******************************************************************
419  *
420  * @brief Builds and Sends RACH indication to MAC 
421  *
422  * @details
423  *
424  *    Function : l1BuildAndSendRachInd 
425  *
426  *    Functionality:
427  *      Builds and Sends RACH indication to MAC
428  *
429  * @params[in] SFN value 
430  *             slot value
431  * @return ROK     - success
432  *         RFAILED - failure
433  *
434  * ****************************************************************/
435 uint16_t l1BuildAndSendRachInd(uint16_t slot, uint16_t sfn)
436 {
437 #ifdef INTEL_FAPI
438    uint8_t   rachPduIdx = 0; 
439    uint8_t   preamIdx = 0;
440    fapi_rach_pdu_t  *rachPdu;
441    fapi_rach_indication_t  *rachInd;
442
443    /* Building RACH indication */
444         MAC_ALLOC(rachInd, sizeof(fapi_rach_indication_t));
445    if(!rachInd)
446    {
447       printf("\nPHY_STUB: Memory allocation failed for Rach Indication Message");
448       return RFAILED;
449    }
450    memset(rachInd, 0, sizeof(fapi_rach_indication_t)); 
451    rachInd->sfn = sfn;
452    rachInd->slot = slot;
453    rachInd->numPdus = 1;
454
455    rachPdu = &rachInd->rachPdu[rachPduIdx];
456    rachPdu->phyCellId = NR_PCI;
457    rachPdu->symbolIndex = 0;
458    rachPdu->slotIndex = slot;
459    rachPdu->freqIndex = 0;
460    rachPdu->avgRssi = 0;
461    rachPdu->avgSnr = 0;
462    rachPdu->numPreamble = 1;
463
464    rachPdu->preambleInfo[preamIdx].preambleIndex = 3;
465    rachPdu->preambleInfo[preamIdx].timingAdvance = 0;
466    rachPdu->preambleInfo[preamIdx].preamblePwr = 0;
467    
468    fillMsgHeader(&rachInd->header, FAPI_RACH_INDICATION, \
469       sizeof(fapi_rach_indication_t) - sizeof(fapi_msg_t));
470
471    /* Sending RACH indication to MAC */
472    DU_LOG("\nPHY STUB: Sending RACH Indication to MAC");
473    handlePhyMessages(rachInd->header.msg_id, sizeof(fapi_rach_indication_t), (void *)rachInd);
474    MAC_FREE(rachInd, sizeof(fapi_rach_indication_t));
475 #endif
476    return ROK;
477 }
478
479 /*******************************************************************
480  *
481  * @brief Builds and Send the Slot Indication message to MAC
482  *
483  * @details
484  *
485  *    Function : l1BuildAndSendSlotIndication
486  *
487  *    Functionality:
488  *          -Send the Slot indication Message to MAC
489  *
490  * @params[in]   Message length
491  *               config request message pointer
492  *
493  * @return void
494  *
495  * ****************************************************************/
496 PUBLIC uint16_t l1BuildAndSendSlotIndication()
497 {
498 #ifdef INTEL_FAPI
499    fapi_slot_ind_t *slotIndMsg;
500
501    MAC_ALLOC(slotIndMsg, sizeof(fapi_slot_ind_t));
502         if(!slotIndMsg)
503    {
504        DU_LOG("\nPHY_STUB: Memory allocation failed for slot Indication Message");
505        return RFAILED;
506    }
507    else
508    {
509            memset(slotIndMsg, 0, sizeof(fapi_slot_ind_t));
510       slotIndMsg->sfn = sfnValue;
511       slotIndMsg->slot = slotValue;
512       DU_LOG("\n\nPHY_STUB: SLOT indication [%d:%d]",sfnValue,slotValue);
513
514                 /* increment for the next TTI */
515       slotValue++;
516       if(sfnValue >= MAX_SFN_VALUE && slotValue > MAX_SLOT_VALUE)
517       {
518          sfnValue = 0;
519          slotValue = 0;
520       }
521       else if(slotValue > MAX_SLOT_VALUE)
522       {
523          sfnValue++;
524          slotValue = 0;
525       }
526       fillMsgHeader(&slotIndMsg->header, FAPI_SLOT_INDICATION, \
527                    sizeof(fapi_slot_ind_t) - sizeof(fapi_msg_t));
528       handlePhyMessages(slotIndMsg->header.msg_id, sizeof(fapi_slot_ind_t), (void*)slotIndMsg);
529       MAC_FREE(slotIndMsg, sizeof(fapi_slot_ind_t));
530    }
531 #endif
532    return ROK;
533 }
534
535 /*******************************************************************
536  *
537  * @brief Handles start request received from MAC
538  *
539  * @details
540  *
541  *    Function : l1HdlStartReq
542  *
543  *    Functionality:
544  *          -Handles start request received from MAC
545  *
546  * @params[in]   Message length
547  *               config request message pointer
548  *
549  * @return void
550  *
551  * ****************************************************************/
552
553 PUBLIC S16 l1HdlStartReq(uint32_t msgLen, void *msg)
554 {
555 #ifdef INTEL_FAPI
556    fapi_start_req_t *startReq = (fapi_start_req_t *)msg;
557
558    if(clGlobalCp.phyState == PHY_STATE_CONFIGURED)
559    {
560       l1HdlSlotIndicaion(FALSE);
561                 MAC_FREE(startReq, sizeof(fapi_start_req_t));
562    }
563    else
564    {
565       DU_LOG("\nPHY_STUB: Received Start Req in PHY State %d", clGlobalCp.phyState);
566       return RFAILED;
567    }
568 #endif
569    return ROK;
570 }
571
572 /*******************************************************************
573 *
574 * @brief Handles Dl Tti request received from MAC
575 *
576 * @details
577 *
578 *    Function : l1HdlDlTtiReq
579 *
580 *    Functionality:
581 *          -Handles Dl Tti request received from MAC
582 *
583 * @params[in]   Message length
584 *               Dl Tti request message pointer
585 *
586 * @return void
587 *
588 * ****************************************************************/
589
590 PUBLIC S16 l1HdlDlTtiReq(uint16_t msgLen, void *msg)
591 {
592 #ifdef INTEL_FAPI
593    fapi_dl_tti_req_t *dlTtiReq;
594    dlTtiReq = (fapi_dl_tti_req_t *)msg;
595
596    printf("\nPHY STUB: DL TTI Request at sfn=%d slot=%d",dlTtiReq->sfn,dlTtiReq->slot);
597 #if 0
598    printf("\nPHY_STUB:  SFN     %d", dlTtiReq->sfn);
599    printf("\nPHY_STUB:  SLOT    %d", dlTtiReq->slot);
600    printf("\nPHY_STUB:  nPdus   %d", dlTtiReq->nPdus);
601    printf("\nPHY_STUB:  nGroup  %d", dlTtiReq->nGroup);
602    /* Printing SSB CONFIGURED VALUES */
603    printf("\nPHY_STUB: physCellId   %d", dlTtiReq->pdus->u.ssb_pdu.physCellId);
604    printf("\nPHY_STUB: betaPss      %d", dlTtiReq->pdus->u.ssb_pdu.betaPss);
605    printf("\nPHY_STUB: ssbBlockIndex %d",       dlTtiReq->pdus->u.ssb_pdu.ssbBlockIndex);
606    printf("\nPHY_STUB: ssbSubCarrierOffset %d", dlTtiReq->pdus->u.ssb_pdu.ssbSubCarrierOffset);
607    printf("\nPHY_STUB: ssbOffsetPointA     %d", dlTtiReq->pdus->u.ssb_pdu.ssbOffsetPointA);
608    printf("\nPHY_STUB: bchPayloadFlag      %d", dlTtiReq->pdus->u.ssb_pdu.bchPayloadFlag);
609    printf("\nPHY_STUB: bchPayload          %x", dlTtiReq->pdus->u.ssb_pdu.bchPayload);
610 #endif
611         uint8_t pduCount = 0;
612         if(dlTtiReq->nPdus == 0)
613         {
614                 DU_LOG("\nPHY_STUB: No PDU in DL TTI Request");
615    }
616         for(pduCount=0; pduCount<dlTtiReq->nPdus; pduCount++)
617         {
618                 if(dlTtiReq->pdus[pduCount].pduType == 3) //SSB_PDU_TYPE
619                         DU_LOG("\nPHY_STUB: SSB PDU");
620                 else if(dlTtiReq->pdus[pduCount].pduType == 0)
621                         DU_LOG("\nPHY_STUB: PDCCH PDU");
622                 else if(dlTtiReq->pdus[pduCount].pduType == 1)
623                    DU_LOG("\nPHY_STUB: PDSCH PDU");
624         }
625
626         /* Free FAPI message */
627    MAC_FREE(dlTtiReq, msgLen);
628
629 #endif
630    return ROK;
631 }
632
633 /*******************************************************************
634 *
635 * @brief Handles tx_data request received from MAC
636 *
637 * @details
638 *
639 *    Function : l1HdlTxDataReq
640 *
641 *    Functionality:
642 *          -Handles tx_data request received from MAC
643 *
644 * @params[in]   Message length
645 *               tx_data request message pointer
646 *
647 * @return void
648 *
649 * ****************************************************************/
650
651 PUBLIC S16 l1HdlTxDataReq(uint16_t msgLen, void *msg)
652 {
653 #ifdef INTEL_FAPI
654    fapi_tx_data_req_t *txDataReq;
655    txDataReq = (fapi_tx_data_req_t *)msg;
656
657    DU_LOG("\nPHY STUB: TX DATA Request at sfn=%d slot=%d",txDataReq->sfn,txDataReq->slot);
658
659         MAC_FREE(txDataReq, msgLen);
660 #endif
661    return ROK;
662 }
663 /*******************************************************************
664 *
665 * @brief Handles Ul Tti request received from MAC
666 *
667 * @details
668 *
669 *    Function : l1HdlUlTtiReq
670 *
671 *    Functionality:
672 *          -Handles Ul Tti request received from MAC
673 *
674 * @params[in]   Message length
675 *               Ul Tti request message pointer
676 *
677 * @return void
678 *
679 * ****************************************************************/
680
681 PUBLIC S16 l1HdlUlTtiReq(uint16_t msgLen, void *msg)
682 {
683 #ifdef INTEL_FAPI
684    fapi_ul_tti_req_t *ulTtiReq;
685
686    DU_LOG("\nPHY STUB: Received UL TTI Request");
687
688    ulTtiReq = (fapi_ul_tti_req_t *)msg;
689    uint8_t numPdus = ulTtiReq->nPdus;
690     
691         if(numPdus == 0)
692         {
693                 DU_LOG("\nPHY STUB: No PDU in UL TTI");
694    }
695         while(numPdus)
696         {
697                 if(ulTtiReq->pdus[numPdus-1].pduType == 0)
698                 {
699                         DU_LOG("\nPHY STUB: PRACH PDU");
700                 }
701       if(ulTtiReq->pdus[numPdus-1].pduType == 1)
702       {
703          DU_LOG("\nPHY STUB: PUSCH PDU");                       
704          l1BuildAndSendRxDataInd(ulTtiReq->slot, ulTtiReq->sfn, \
705                            ulTtiReq->pdus[numPdus-1].pdu.pusch_pdu); 
706       }
707                 if(ulTtiReq->pdus[numPdus-1].pduType == 2)
708            {
709                    DU_LOG("\nPHY STUB: PUCCH PDU");
710                 }
711                 numPdus--;
712         }
713
714    if(rachIndSent == false && ulTtiReq->sfn == 2 && ulTtiReq->slot == 6)
715    {
716       rachIndSent = true;
717       l1BuildAndSendRachInd(ulTtiReq->slot, ulTtiReq->sfn);
718    }
719    
720    MAC_FREE(ulTtiReq, msgLen);
721 #endif
722    return ROK;
723 }
724
725 /*******************************************************************
726  *
727  * @brief Builds and Send the stop Indication message to MAC
728  *
729  * @details
730  *
731  *    Function : l1BuildAndSendStopInd
732  *
733  *    Functionality:
734  *          -Send the Stop indication Message to MAC
735  *
736  *
737  * @return void
738  *
739  * ****************************************************************/
740 PUBLIC uint16_t l1BuildAndSendStopInd()
741 {
742 #ifdef INTEL_FAPI
743    fapi_stop_ind_t *stopIndMsg = NULLP;
744    uint32_t msgLen = 0;
745
746    MAC_ALLOC(stopIndMsg, sizeof(fapi_stop_ind_t));
747    if(!stopIndMsg)
748    {
749        DU_LOG("\nPHY_STUB: Memory allocation failed for stop Indication Message");
750        return RFAILED;
751    }
752    else
753    {
754       fillMsgHeader(&stopIndMsg->header, FAPI_STOP_INDICATION, msgLen);
755       DU_LOG("\n\nPHY_STUB: Processing Stop indication to MAC");
756       handlePhyMessages(stopIndMsg->header.msg_id,\
757                   sizeof(fapi_stop_ind_t), (void*)stopIndMsg);
758       MAC_FREE(stopIndMsg, sizeof(fapi_stop_ind_t));
759    }
760 #endif
761    return ROK;
762 }
763
764 /*******************************************************************
765  *
766  * @brief Handles stop request received from MAC
767  *
768  * @details
769  *
770  *    Function : l1HdlStopReq
771  *
772  *    Functionality:
773  *          -Handles stop request received from MAC
774  *
775  * @params[in]   Message length
776  *               stop request message pointer
777  *
778  * @return void
779  *
780  * ****************************************************************/
781
782 PUBLIC S16 l1HdlStopReq(uint32_t msgLen, void *msg)
783 {
784 #ifdef INTEL_FAPI
785    fapi_stop_req_t *stopReq = (fapi_stop_req_t *)msg;
786
787    if(clGlobalCp.phyState == PHY_STATE_RUNNING)
788    {
789       l1HdlSlotIndicaion(TRUE);
790       DU_LOG("\nPHY_STUB: Slot Indication is stopped successfully");
791       l1BuildAndSendStopInd();
792       MAC_FREE(stopReq, msgLen);
793    }
794    else
795    {
796       DU_LOG("\nPHY_STUB: Received Stop Req in PHY State %d", clGlobalCp.phyState);
797       return RFAILED;
798    }
799 #endif
800    return ROK;
801 }
802
803 /*******************************************************************
804  *
805  * @brief Receives message from MAC
806  *
807  * @details
808  *
809  *    Function :  l1ProcessFapiRequest
810  *
811  *    Functionality:
812  *       - Receives message from MAC and calls handler
813  *
814  * @params[in] Message type
815  *             Message length
816  *             Message pointer
817  *
818  * @return void
819  *
820  * ****************************************************************/
821
822 void l1ProcessFapiRequest(uint8_t msgType, uint32_t msgLen, void *msg)
823 {
824    switch(msgType)
825    {
826 #ifdef INTEL_FAPI
827       case FAPI_PARAM_REQUEST:
828          l1HdlParamReq(msgLen, msg);
829          break;
830       case FAPI_CONFIG_REQUEST:
831          l1HdlConfigReq(msgLen, msg);
832          break;
833       case FAPI_START_REQUEST:
834          l1HdlStartReq(msgLen, msg);
835          break;
836       case FAPI_DL_TTI_REQUEST:
837          l1HdlDlTtiReq(msgLen, msg);
838          break;
839       case FAPI_TX_DATA_REQUEST:
840          l1HdlTxDataReq(msgLen, msg);
841          break;
842       case FAPI_UL_TTI_REQUEST:
843          l1HdlUlTtiReq(msgLen, msg);
844          break;
845       case FAPI_STOP_REQUEST:
846          l1HdlStopReq(msgLen, msg);
847          break;
848       default:
849          DU_LOG("\nPHY_STUB: Invalid message type[%x] received at PHY", msgType);
850          break;
851 #endif
852    }
853 }
854 /**********************************************************************
855          End of file
856 **********************************************************************/