c9345691feac0f6fdfd46935b3e7fa567e9eda48
[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,
99                 p_phy_crc_ind->sSFN_Slot.nSlot));
100
101     return SUCCESS;
102 }
103
104  /** @ingroup group_source_api_p7_fapi2mac_proc
105  *
106  *  @param[in]  p_phy_instance Pointer to PHY instance.
107  *  @param[in]   p_phy_crc_ind Pointer to IAPI CRC.indication structure.
108  *  @param[out]  p_fapi_crc_ind Pointer to FAPI CRC.indication structure.
109  *  
110  *  @return     Returns ::SUCCESS and ::FAILURE.
111  *
112  *  @description
113  *  This function converts IAPI CRC.indication to FAPI CRC.indication
114  *  structure.
115  *
116 **/
117 uint8_t nr5g_fapi_crc_indication_to_fapi_translation(
118     p_nr5g_fapi_phy_instance_t p_phy_instance,
119     PCRCIndicationStruct p_phy_crc_ind,
120     fapi_crc_ind_t * p_fapi_crc_ind)
121 {
122     uint8_t num_crc, i;
123     uint8_t slot_no;
124     uint16_t frame_no;
125
126     nr5g_fapi_pusch_info_t *p_pusch_info;
127     fapi_crc_ind_info_t *p_fapi_crc_ind_info;
128     nr5g_fapi_ul_slot_info_t *p_ul_slot_info;
129     nr5g_fapi_stats_t *p_stats;
130     ULCRCStruct *p_ul_crc_struct;
131
132     p_stats = &p_phy_instance->stats;
133
134     frame_no = p_fapi_crc_ind->sfn = p_phy_crc_ind->sSFN_Slot.nSFN;
135     slot_no = p_fapi_crc_ind->slot = p_phy_crc_ind->sSFN_Slot.nSlot;
136
137     p_ul_slot_info =
138         nr5g_fapi_get_ul_slot_info(frame_no, slot_no, p_phy_instance);
139
140     if (p_ul_slot_info == NULL) {
141         NR5G_FAPI_LOG(ERROR_LOG, (" [CRC.indication] No Valid data available "
142                 "for frame :%d and slot: %d", frame_no, slot_no));
143         return FAILURE;
144     }
145
146     num_crc = p_fapi_crc_ind->numCrcs = p_phy_crc_ind->nCrc;
147     for (i = 0; i < num_crc; i++) {
148         p_stats->iapi_stats.iapi_crc_ind_pdus++;
149
150         p_fapi_crc_ind_info = &p_fapi_crc_ind->crc[i];
151         p_ul_crc_struct = &p_phy_crc_ind->sULCRCStruct[i];
152         p_pusch_info =
153             nr5g_fapi_get_pusch_info(p_ul_crc_struct->nUEId, p_ul_slot_info);
154
155         if (p_pusch_info == NULL) {
156             NR5G_FAPI_LOG(ERROR_LOG,
157                 (" [CRC.indication] No Valid data available "
158                     "nUEId:%d, frame_no:%d, slot_no:%d", p_ul_crc_struct->nUEId,
159                     frame_no, slot_no));
160             return FAILURE;
161         }
162
163         p_fapi_crc_ind_info->handle = p_pusch_info->handle;
164         p_fapi_crc_ind_info->rnti = p_ul_crc_struct->nRNTI;
165         p_fapi_crc_ind_info->harqId = p_pusch_info->harq_process_id;
166         p_fapi_crc_ind_info->tbCrcStatus = !(p_ul_crc_struct->nCrcFlag);
167         p_fapi_crc_ind_info->ul_cqi = (p_ul_crc_struct->nSNR + 64) * 2;
168         p_pusch_info->ul_cqi = p_fapi_crc_ind_info->ul_cqi;
169
170         p_fapi_crc_ind_info->numCb = 0;
171         p_pusch_info->timing_advance = p_fapi_crc_ind_info->timingAdvance = 31;
172 #ifdef DEBUG_MODE
173         p_pusch_info->timing_advance = p_fapi_crc_ind_info->timingAdvance =
174             p_ul_crc_struct->nTA;
175 #endif
176         p_fapi_crc_ind_info->rssi = 880;
177
178         p_stats->fapi_stats.fapi_crc_ind_pdus++;
179     }
180     return SUCCESS;
181 }