Update to odulow per maintenance bronze
[o-du/phy.git] / fapi_5g / source / api / fapi2mac / p7 / nr5g_fapi_proc_crc_ind.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
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  * @file
20  * This file consist of implementation of FAPI CRC.indication message.
21  *
22  **/
23
24 #include "nr5g_fapi_framework.h"
25 #include "gnb_l1_l2_api.h"
26 #include "nr5g_fapi_fapi2mac_api.h"
27 #include "nr5g_fapi_fapi2mac_p7_proc.h"
28 #include "nr5g_fapi_fapi2mac_p7_pvt_proc.h"
29
30  /** @ingroup group_source_api_p7_fapi2mac_proc
31  *
32  *  @param[in]  p_phy_ctx Pointer to PHY context.
33  *  @param[in]  p_phy_crc_ind Pointer to FAPI CRC.indication message structure.
34  *  
35  *  @return     Returns ::SUCCESS and ::FAILURE.
36  *
37  *  @description
38  *  This message includes CRC PDU.
39  *
40 **/
41 uint8_t nr5g_fapi_crc_indication(
42     p_nr5g_fapi_phy_ctx_t p_phy_ctx,
43     PCRCIndicationStruct p_phy_crc_ind)
44 {
45     uint8_t phy_id;
46     fapi_crc_ind_t *p_fapi_crc_ind;
47     p_fapi_api_queue_elem_t p_list_elem;
48     p_nr5g_fapi_phy_instance_t p_phy_instance = NULL;
49     nr5g_fapi_stats_t *p_stats;
50
51     if (NULL == p_phy_ctx) {
52         NR5G_FAPI_LOG(ERROR_LOG, ("[CRC.indication] Invalid " "phy context"));
53         return FAILURE;
54     }
55
56     if (NULL == p_phy_crc_ind) {
57         NR5G_FAPI_LOG(ERROR_LOG, ("[NR5G_FAPI [CRC.indication] Invalid Phy "
58                 "CRC indication"));
59         return FAILURE;
60     }
61
62     phy_id = p_phy_crc_ind->sSFN_Slot.nCarrierIdx;
63     p_phy_instance = &p_phy_ctx->phy_instance[phy_id];
64     if (p_phy_instance->phy_id != phy_id) {
65         NR5G_FAPI_LOG(ERROR_LOG, ("[CRC.indication] Invalid " "phy instance"));
66         return FAILURE;
67     }
68
69     p_stats = &p_phy_instance->stats;
70     p_stats->iapi_stats.iapi_crc_ind++;
71
72     p_list_elem =
73         nr5g_fapi_fapi2mac_create_api_list_elem(FAPI_CRC_INDICATION, 1,
74         sizeof(fapi_crc_ind_t));
75     if (!p_list_elem) {
76         NR5G_FAPI_LOG(ERROR_LOG, ("[CRC.indication] Unable to create "
77                 "list element. Out of memory!!!"));
78         return FAILURE;
79     }
80
81     p_fapi_crc_ind = (fapi_crc_ind_t *) (p_list_elem + 1);
82     p_fapi_crc_ind->header.msg_id = FAPI_CRC_INDICATION;
83     p_fapi_crc_ind->header.length = (uint16_t) sizeof(fapi_crc_ind_t);
84
85     if (nr5g_fapi_crc_indication_to_fapi_translation(p_phy_instance,
86             p_phy_crc_ind, p_fapi_crc_ind)) {
87         NR5G_FAPI_LOG(ERROR_LOG, ("[CRC.indication] L1 to FAPI "
88                 "translation failed"));
89         return FAILURE;
90     }
91
92     nr5g_fapi_fapi2mac_add_api_to_list(phy_id, p_list_elem);
93
94     p_stats->fapi_stats.fapi_crc_ind++;
95
96     NR5G_FAPI_LOG(DEBUG_LOG, ("[CRC.indication][%d][%d,%d]",
97             p_phy_instance->phy_id,
98             p_phy_crc_ind->sSFN_Slot.nSFN, p_phy_crc_ind->sSFN_Slot.nSlot));
99
100     return SUCCESS;
101 }
102
103  /** @ingroup group_source_api_p7_fapi2mac_proc
104  *
105  *  @param[in]  p_phy_instance Pointer to PHY instance.
106  *  @param[in]   p_phy_crc_ind Pointer to IAPI CRC.indication structure.
107  *  @param[out]  p_fapi_crc_ind Pointer to FAPI CRC.indication structure.
108  *  
109  *  @return     Returns ::SUCCESS and ::FAILURE.
110  *
111  *  @description
112  *  This function converts IAPI CRC.indication to FAPI CRC.indication
113  *  structure.
114  *
115 **/
116 uint8_t nr5g_fapi_crc_indication_to_fapi_translation(
117     p_nr5g_fapi_phy_instance_t p_phy_instance,
118     PCRCIndicationStruct p_phy_crc_ind,
119     fapi_crc_ind_t * p_fapi_crc_ind)
120 {
121     uint8_t num_crc, i;
122     uint8_t slot_no;
123     uint16_t frame_no;
124
125     nr5g_fapi_pusch_info_t *p_pusch_info;
126     fapi_crc_ind_info_t *p_fapi_crc_ind_info;
127     nr5g_fapi_ul_slot_info_t *p_ul_slot_info;
128     nr5g_fapi_stats_t *p_stats;
129     ULCRCStruct *p_ul_crc_struct;
130
131     p_stats = &p_phy_instance->stats;
132
133     frame_no = p_fapi_crc_ind->sfn = p_phy_crc_ind->sSFN_Slot.nSFN;
134     slot_no = p_fapi_crc_ind->slot = p_phy_crc_ind->sSFN_Slot.nSlot;
135
136     p_ul_slot_info =
137         nr5g_fapi_get_ul_slot_info(frame_no, slot_no, p_phy_instance);
138
139     if (p_ul_slot_info == NULL) {
140         NR5G_FAPI_LOG(ERROR_LOG, (" [CRC.indication] No Valid data available "
141                 "for frame :%d and slot: %d", frame_no, slot_no));
142         return FAILURE;
143     }
144
145     num_crc = p_fapi_crc_ind->numCrcs = p_phy_crc_ind->nCrc;
146     for (i = 0; i < num_crc; i++) {
147         p_stats->iapi_stats.iapi_crc_ind_pdus++;
148
149         p_fapi_crc_ind_info = &p_fapi_crc_ind->crc[i];
150         p_ul_crc_struct = &p_phy_crc_ind->sULCRCStruct[i];
151         p_pusch_info =
152             nr5g_fapi_get_pusch_info(p_ul_crc_struct->nUEId, p_ul_slot_info);
153
154         if (p_pusch_info == NULL) {
155             NR5G_FAPI_LOG(ERROR_LOG,
156                 (" [CRC.indication] No Valid data available "
157                     "nUEId:%d, frame_no:%d, slot_no:%d", p_ul_crc_struct->nUEId,
158                     frame_no, slot_no));
159             return FAILURE;
160         }
161
162         p_fapi_crc_ind_info->handle = p_pusch_info->handle;
163         p_fapi_crc_ind_info->rnti = p_ul_crc_struct->nRNTI;
164         p_fapi_crc_ind_info->harqId = p_pusch_info->harq_process_id;
165         p_fapi_crc_ind_info->tbCrcStatus = !(p_ul_crc_struct->nCrcFlag);
166         p_fapi_crc_ind_info->ul_cqi = (p_ul_crc_struct->nSNR + 64) * 2;
167         p_pusch_info->ul_cqi = p_fapi_crc_ind_info->ul_cqi;
168
169         p_fapi_crc_ind_info->numCb = 0;
170         p_pusch_info->timing_advance = p_fapi_crc_ind_info->timingAdvance = 31;
171 #ifdef DEBUG_MODE
172         p_pusch_info->timing_advance = p_fapi_crc_ind_info->timingAdvance =
173             p_ul_crc_struct->nTA;
174 #endif
175         p_fapi_crc_ind_info->rssi = 880;
176
177         p_stats->fapi_stats.fapi_crc_ind_pdus++;
178     }
179     return SUCCESS;
180 }