phy_stub_UE.c 71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21

22 23 24 25
#include "openair2/LAYER2/MAC/mac_extern.h"
#include "openair2/LAYER2/MAC/mac.h"
#include "openair2/LAYER2/MAC/mac_proto.h"
#include "openair1/SCHED_UE/sched_UE.h"
26 27 28 29 30 31 32
#include "nfapi/open-nFAPI/nfapi/public_inc/nfapi_interface.h"
#include "openair2/PHY_INTERFACE/phy_stub_UE.h"
#include "openair2/ENB_APP/L1_paramdef.h"
#include "openair2/ENB_APP/enb_paramdef.h"
#include "targets/ARCH/ETHERNET/USERSPACE/LIB/if_defs.h"
#include "common/config/config_load_configmodule.h"
#include "common/config/config_userapi.h"
Michael Cook's avatar
Michael Cook committed
33
#include <arpa/inet.h>
34 35
#include <string.h>
#include <errno.h>
36 37

extern int oai_nfapi_rach_ind(nfapi_rach_indication_t *rach_ind);
38 39 40 41 42
void configure_nfapi_pnf(char *vnf_ip_addr,
                         int vnf_p5_port,
                         char *pnf_ip_addr,
                         int pnf_p7_port,
                         int vnf_p7_port);
Michael Cook's avatar
Michael Cook committed
43

44
UL_IND_t *UL_INFO = NULL;
45

46
queue_t dl_config_req_tx_req_queue;
47 48 49
queue_t ul_config_req_queue;
queue_t hi_dci0_req_queue;

Andrew Burger's avatar
Andrew Burger committed
50
int current_sfn_sf;
51
sem_t sfn_semaphore;
Andrew Burger's avatar
Andrew Burger committed
52

Andrew Burger's avatar
Andrew Burger committed
53 54
static int ue_tx_sock_descriptor = -1;
static int ue_rx_sock_descriptor = -1;
55

56
extern nfapi_tx_request_pdu_t* tx_request_pdu[1023][10][10];
Michael Cook's avatar
Michael Cook committed
57 58
//extern int timer_subframe;
//extern int timer_frame;
59 60 61 62 63 64

extern uint16_t sf_ahead;

void Msg1_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id);
void Msg3_transmitted(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id);

65 66 67 68 69 70 71
void fill_rx_indication_UE_MAC(module_id_t Mod_id,
                               int frame,
                               int subframe,
                               UL_IND_t *UL_INFO,
                               uint8_t *ulsch_buffer,
                               uint16_t buflen,
                               uint16_t rnti,
72 73
                               int index,
                               nfapi_ul_config_request_t *ul_config_req) {
74 75 76 77 78
  nfapi_rx_indication_pdu_t *pdu;
  int timing_advance_update;

  pthread_mutex_lock(&fill_ul_mutex.rx_mutex);

79
  UL_INFO->rx_ind.header.message_id = NFAPI_RX_ULSCH_INDICATION;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  UL_INFO->rx_ind.sfn_sf = frame << 4 | subframe;
  UL_INFO->rx_ind.rx_indication_body.tl.tag = NFAPI_RX_INDICATION_BODY_TAG;
  UL_INFO->rx_ind.vendor_extension = ul_config_req->vendor_extension;

  pdu = &UL_INFO->rx_ind.rx_indication_body
             .rx_pdu_list[UL_INFO->rx_ind.rx_indication_body.number_of_pdus];
  // pdu = &UL_INFO->rx_ind.rx_indication_body.rx_pdu_list[index];

  // pdu->rx_ue_information.handle          = eNB->ulsch[UE_id]->handle;
  pdu->rx_ue_information.tl.tag = NFAPI_RX_UE_INFORMATION_TAG;
  pdu->rx_ue_information.rnti = rnti;
  pdu->rx_indication_rel8.tl.tag = NFAPI_RX_INDICATION_REL8_TAG;
  pdu->rx_indication_rel8.length = buflen;
  pdu->rx_indication_rel8.offset = 1;
  pdu->rx_indication_rel9.tl.tag = NFAPI_RX_INDICATION_REL9_TAG;
  pdu->rx_indication_rel9.timing_advance_r9 = 0;

  pdu->data = malloc(buflen);
  memcpy(pdu->data, ulsch_buffer, buflen);
Andrew Burger's avatar
Andrew Burger committed
99 100
  LOG_I(MAC, "buflen of rx_ind pdu_data = %u SFN.SF: %d.%d\n", buflen,
        frame, subframe);
101 102 103
  // estimate timing advance for MAC
  timing_advance_update = 0; // Don't know what to put here
  pdu->rx_indication_rel8.timing_advance = timing_advance_update;
104

105
  int SNRtimes10 = 640;
106

107 108 109 110 111 112
  if (SNRtimes10 < -640)
    pdu->rx_indication_rel8.ul_cqi = 0;
  else if (SNRtimes10 > 635)
    pdu->rx_indication_rel8.ul_cqi = 255;
  else
    pdu->rx_indication_rel8.ul_cqi = (640 + SNRtimes10) / 5;
113

114 115 116
  UL_INFO->rx_ind.rx_indication_body.number_of_pdus++;
  UL_INFO->rx_ind.sfn_sf = frame << 4 | subframe;
  pthread_mutex_unlock(&fill_ul_mutex.rx_mutex);
117 118
}

119 120 121 122
void fill_sr_indication_UE_MAC(int Mod_id,
                               int frame,
                               int subframe,
                               UL_IND_t *UL_INFO,
123 124
                               uint16_t rnti,
                               nfapi_ul_config_request_t *ul_config_req) {
125
  pthread_mutex_lock(&fill_ul_mutex.sr_mutex);
126

127 128 129 130
  nfapi_sr_indication_t *sr_ind = &UL_INFO->sr_ind;
  nfapi_sr_indication_body_t *sr_ind_body = &sr_ind->sr_indication_body;
  nfapi_sr_indication_pdu_t *pdu = &sr_ind_body->sr_pdu_list[sr_ind_body->number_of_srs];
  UL_INFO->sr_ind.vendor_extension = ul_config_req->vendor_extension;
131

132
  sr_ind->sfn_sf = frame << 4 | subframe;
133 134 135 136
  sr_ind->header.message_id = NFAPI_RX_SR_INDICATION;

  sr_ind_body->tl.tag = NFAPI_SR_INDICATION_BODY_TAG;

137
  pdu->instance_length = 0; // don't know what to do with this
138
  //  pdu->rx_ue_information.handle                       = handle;
139 140
  pdu->rx_ue_information.tl.tag = NFAPI_RX_UE_INFORMATION_TAG;
  pdu->rx_ue_information.rnti = rnti; // UE_mac_inst[Mod_id].crnti
141 142 143 144

  pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;
  pdu->ul_cqi_information.channel = 0;

145 146 147 148 149 150 151 152 153
  int SNRtimes10 = 640;
  if (SNRtimes10 < -640)
    pdu->ul_cqi_information.ul_cqi = 0;
  else if (SNRtimes10 > 635)
    pdu->ul_cqi_information.ul_cqi = 255;
  else
    pdu->ul_cqi_information.ul_cqi = (640 + SNRtimes10) / 5;

  // UL_INFO->rx_ind.rx_indication_body.number_of_pdus++;
154
  sr_ind_body->number_of_srs++;
155
  pthread_mutex_unlock(&fill_ul_mutex.sr_mutex);
156 157
}

158 159 160 161 162 163
void fill_crc_indication_UE_MAC(int Mod_id,
                                int frame,
                                int subframe,
                                UL_IND_t *UL_INFO,
                                uint8_t crc_flag,
                                int index,
164 165
                                uint16_t rnti,
                                nfapi_ul_config_request_t *ul_config_req) {
166
  pthread_mutex_lock(&fill_ul_mutex.crc_mutex);
Andrew Burger's avatar
Andrew Burger committed
167 168
  LOG_D(MAC, "fill crc_indication num_crcs: %u\n",
        UL_INFO->crc_ind.crc_indication_body.number_of_crcs);
169 170 171
  nfapi_crc_indication_pdu_t *pdu =
      &UL_INFO->crc_ind.crc_indication_body
           .crc_pdu_list[UL_INFO->crc_ind.crc_indication_body.number_of_crcs];
172

173 174 175
  UL_INFO->crc_ind.sfn_sf = frame << 4 | subframe;
  UL_INFO->crc_ind.vendor_extension = ul_config_req->vendor_extension;
  UL_INFO->crc_ind.header.message_id = NFAPI_CRC_INDICATION;
176 177
  UL_INFO->crc_ind.crc_indication_body.tl.tag = NFAPI_CRC_INDICATION_BODY_TAG;

178 179
  pdu->instance_length = 0;
  pdu->rx_ue_information.tl.tag = NFAPI_RX_UE_INFORMATION_TAG;
180

181 182 183
  pdu->rx_ue_information.rnti = rnti;
  pdu->crc_indication_rel8.tl.tag = NFAPI_CRC_INDICATION_REL8_TAG;
  pdu->crc_indication_rel8.crc_flag = crc_flag;
184 185 186

  UL_INFO->crc_ind.crc_indication_body.number_of_crcs++;

187 188 189 190 191
  LOG_D(PHY,
        "%s() rnti:%04x pdus:%d\n",
        __FUNCTION__,
        pdu->rx_ue_information.rnti,
        UL_INFO->crc_ind.crc_indication_body.number_of_crcs);
192

193
  pthread_mutex_unlock(&fill_ul_mutex.crc_mutex);
194 195
}

196 197 198 199 200 201
void fill_rach_indication_UE_MAC(int Mod_id,
                                 int frame,
                                 int subframe,
                                 UL_IND_t *UL_INFO,
                                 uint8_t ra_PreambleIndex,
                                 uint16_t ra_RNTI) {
Michael Cook's avatar
Michael Cook committed
202
  LOG_D(MAC, "fill_rach_indication_UE_MAC 1 \n");
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

  pthread_mutex_lock(&fill_ul_mutex.rach_mutex);

  UL_INFO->rach_ind.rach_indication_body.number_of_preambles = 1;

  UL_INFO->rach_ind.header.message_id = NFAPI_RACH_INDICATION;
  UL_INFO->rach_ind.sfn_sf = frame << 4 | subframe;
  UL_INFO->rach_ind.vendor_extension = NULL;

  UL_INFO->rach_ind.rach_indication_body.tl.tag = NFAPI_RACH_INDICATION_BODY_TAG;

  const int np = UL_INFO->rach_ind.rach_indication_body.number_of_preambles;
  UL_INFO->rach_ind.rach_indication_body.preamble_list =
      calloc(np, sizeof(nfapi_preamble_pdu_t));
  UL_INFO->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.tl.tag =
      NFAPI_PREAMBLE_REL8_TAG;
  UL_INFO->rach_ind.rach_indication_body.preamble_list[0]
      .preamble_rel8.timing_advance = 0; // Not sure about that

  // The two following should get extracted from the call to
  // get_prach_resources().
  UL_INFO->rach_ind.rach_indication_body.preamble_list[0]
      .preamble_rel8.preamble = ra_PreambleIndex;
  UL_INFO->rach_ind.rach_indication_body.preamble_list[0].preamble_rel8.rnti =
      ra_RNTI;
  // UL_INFO->rach_ind.rach_indication_body.number_of_preambles++;

  UL_INFO->rach_ind.rach_indication_body.preamble_list[0]
      .preamble_rel13.rach_resource_type = 0;
  UL_INFO->rach_ind.rach_indication_body.preamble_list[0].instance_length = 0;

Michael Cook's avatar
Michael Cook committed
234
  LOG_I(PHY,
235 236 237 238 239 240 241 242 243 244 245 246 247
        "UE Filling NFAPI indication for RACH : TA %d, Preamble %d, rnti %x, "
        "rach_resource_type %d\n",
        UL_INFO->rach_ind.rach_indication_body.preamble_list[0]
            .preamble_rel8.timing_advance,
        UL_INFO->rach_ind.rach_indication_body.preamble_list[0]
            .preamble_rel8.preamble,
        UL_INFO->rach_ind.rach_indication_body.preamble_list[0]
            .preamble_rel8.rnti,
        UL_INFO->rach_ind.rach_indication_body.preamble_list[0]
            .preamble_rel13.rach_resource_type);

  // This function is currently defined only in the nfapi-RU-RAU-split so we
  // should call it when we merge with that branch.
248
  if (NFAPI_MODE == NFAPI_MODE_STANDALONE_PNF) {
249
    send_standalone_msg(UL_INFO, UL_INFO->rach_ind.header.message_id);
250 251 252 253
  } else {
    oai_nfapi_rach_ind(&UL_INFO->rach_ind);
  }

254 255 256
  free(UL_INFO->rach_ind.rach_indication_body.preamble_list);

  pthread_mutex_unlock(&fill_ul_mutex.rach_mutex);
257 258
}

259 260 261 262 263 264
void fill_ulsch_cqi_indication_UE_MAC(int Mod_id,
                                      uint16_t frame,
                                      uint8_t subframe,
                                      UL_IND_t *UL_INFO,
                                      uint16_t rnti) {
  pthread_mutex_lock(&fill_ul_mutex.cqi_mutex);
Andrew Burger's avatar
Andrew Burger committed
265 266
  LOG_D(MAC, "num_cqis: %u in fill_ulsch_cqi_indication_UE_MAC\n",
        UL_INFO->cqi_ind.cqi_indication_body.number_of_cqis);
267 268 269 270 271 272 273 274 275 276
  nfapi_cqi_indication_pdu_t *pdu =
      &UL_INFO->cqi_ind.cqi_indication_body
           .cqi_pdu_list[UL_INFO->cqi_ind.cqi_indication_body.number_of_cqis];
  nfapi_cqi_indication_raw_pdu_t *raw_pdu =
      &UL_INFO->cqi_ind.cqi_indication_body.cqi_raw_pdu_list
           [UL_INFO->cqi_ind.cqi_indication_body.number_of_cqis];

  UL_INFO->cqi_ind.sfn_sf = frame << 4 | subframe;
  // because of nfapi_vnf.c:733, set message id to 0, not
  // NFAPI_RX_CQI_INDICATION;
277
  UL_INFO->cqi_ind.header.message_id = NFAPI_RX_CQI_INDICATION;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  UL_INFO->cqi_ind.cqi_indication_body.tl.tag = NFAPI_CQI_INDICATION_BODY_TAG;

  pdu->rx_ue_information.tl.tag = NFAPI_RX_UE_INFORMATION_TAG;
  pdu->rx_ue_information.rnti = rnti;
  // Since we assume that CRC flag is always 0 (ACK) I guess that data_offset
  // should always be 0.
  pdu->cqi_indication_rel8.data_offset = 0;

  pdu->cqi_indication_rel8.tl.tag = NFAPI_CQI_INDICATION_REL8_TAG;
  pdu->cqi_indication_rel8.length = 1;
  pdu->cqi_indication_rel8.ri = 0;

  pdu->cqi_indication_rel8.timing_advance = 0;
  // pdu->cqi_indication_rel8.number_of_cc_reported = 1;
  pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;
Andrew Burger's avatar
Andrew Burger committed
293
  pdu->ul_cqi_information.channel = 1;
294

295 296 297
  // eNB_scheduler_primitives.c:4839: the upper four bits seem to be the CQI
  const int cqi = 15;
  raw_pdu->pdu[0] = cqi << 4;
298

299
  UL_INFO->cqi_ind.cqi_indication_body.number_of_cqis++;
Andrew Burger's avatar
Andrew Burger committed
300

301
  pthread_mutex_unlock(&fill_ul_mutex.cqi_mutex);
302 303
}

304 305 306 307 308 309
void fill_ulsch_harq_indication_UE_MAC(
    int Mod_id,
    int frame,
    int subframe,
    UL_IND_t *UL_INFO,
    nfapi_ul_config_ulsch_harq_information *harq_information,
310 311
    uint16_t rnti,
    nfapi_ul_config_request_t *ul_config_req) {
312 313
  pthread_mutex_lock(&fill_ul_mutex.harq_mutex);

314 315 316
  nfapi_harq_indication_pdu_t *pdu =
      &UL_INFO->harq_ind.harq_indication_body.harq_pdu_list
           [UL_INFO->harq_ind.harq_indication_body.number_of_harqs];
317 318

  UL_INFO->harq_ind.header.message_id = NFAPI_HARQ_INDICATION;
319 320
  UL_INFO->harq_ind.sfn_sf = frame << 4 | subframe;
  UL_INFO->harq_ind.vendor_extension = ul_config_req->vendor_extension;
321

322 323
  UL_INFO->harq_ind.harq_indication_body.tl.tag =
      NFAPI_HARQ_INDICATION_BODY_TAG;
324

325
  pdu->instance_length = 0; // don't know what to do with this
326
  //  pdu->rx_ue_information.handle                       = handle;
327 328
  pdu->rx_ue_information.tl.tag = NFAPI_RX_UE_INFORMATION_TAG;
  pdu->rx_ue_information.rnti = rnti;
329

330
  // For now we consider only FDD
331 332 333 334 335
  // if (eNB->frame_parms.frame_type == FDD) {
  pdu->harq_indication_fdd_rel13.tl.tag = NFAPI_HARQ_INDICATION_FDD_REL13_TAG;
  pdu->harq_indication_fdd_rel13.mode = 0;
  pdu->harq_indication_fdd_rel13.number_of_ack_nack =
      harq_information->harq_information_rel10.harq_size;
336

337 338
  // Could this be wrong? Is the number_of_ack_nack field equivalent to O_ACK?
  // pdu->harq_indication_fdd_rel13.number_of_ack_nack = ulsch_harq->O_ACK;
339

340 341 342
  for (int i = 0; i < harq_information->harq_information_rel10.harq_size; i++) {
    pdu->harq_indication_fdd_rel13.harq_tb_n[i] = 1; // Assume always ACK (No NACK or DTX)
  }
343 344

  UL_INFO->harq_ind.harq_indication_body.number_of_harqs++;
345 346
  pthread_mutex_unlock(&fill_ul_mutex.harq_mutex);
}
347 348 349 350 351 352

void fill_uci_harq_indication_UE_MAC(int Mod_id,
			      int frame,
			      int subframe,
			      UL_IND_t *UL_INFO,
			      nfapi_ul_config_harq_information *harq_information,
353 354
			      uint16_t rnti,
            nfapi_ul_config_request_t *ul_config_req) {
355 356
  pthread_mutex_lock(&fill_ul_mutex.harq_mutex);

357
  nfapi_harq_indication_t *ind = &UL_INFO->harq_ind;
358
  nfapi_harq_indication_body_t *body = &ind->harq_indication_body;
359 360 361
  nfapi_harq_indication_pdu_t *pdu =
      &body->harq_pdu_list[UL_INFO->harq_ind.harq_indication_body
                               .number_of_harqs];
362

363
  UL_INFO->harq_ind.vendor_extension = ul_config_req->vendor_extension;
364

365
  ind->sfn_sf = frame << 4 | subframe;
366 367 368
  ind->header.message_id = NFAPI_HARQ_INDICATION;

  body->tl.tag = NFAPI_HARQ_INDICATION_BODY_TAG;
369
  pdu->rx_ue_information.tl.tag = NFAPI_RX_UE_INFORMATION_TAG;
370

371 372
  pdu->instance_length = 0; // don't know what to do with this
  pdu->rx_ue_information.rnti = rnti;
373 374 375 376 377

  pdu->ul_cqi_information.tl.tag = NFAPI_UL_CQI_INFORMATION_TAG;

  int SNRtimes10 = 640;

378 379 380 381 382 383
  if (SNRtimes10 < -640)
    pdu->ul_cqi_information.ul_cqi = 0;
  else if (SNRtimes10 > 635)
    pdu->ul_cqi_information.ul_cqi = 255;
  else
    pdu->ul_cqi_information.ul_cqi = (640 + SNRtimes10) / 5;
384
  pdu->ul_cqi_information.channel = 0;
385 386 387 388 389 390
  if (harq_information->harq_information_rel9_fdd.tl.tag
      == NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL9_FDD_TAG) {
    if ((harq_information->harq_information_rel9_fdd.ack_nack_mode == 0)
        && (harq_information->harq_information_rel9_fdd.harq_size == 1)) {
      pdu->harq_indication_fdd_rel13.tl.tag =
          NFAPI_HARQ_INDICATION_FDD_REL13_TAG;
391 392 393
      pdu->harq_indication_fdd_rel13.mode = 0;
      pdu->harq_indication_fdd_rel13.number_of_ack_nack = 1;

394 395 396 397
      // AssertFatal(harq_ack[0] == 1 || harq_ack[0] == 2 || harq_ack[0] == 4,
      // "harq_ack[0] is %d, should be 1,2 or 4\n",harq_ack[0]);
      pdu->harq_indication_fdd_rel13.harq_tb_n[0] =
          1; // Assuming always an ACK (No NACK or DTX)
398

399 400 401 402 403 404
      // TODO: Fix ack/dtx -- needed for 5G
      // 1.) if received dl_config_req (with c-rnti) store this info and corresponding subframe.
      // 2.) if receiving ul_config_req for uci ack/nack or ulsch ack/nak in subframe n
      //     go look to see if dl_config_req (with c-rnti) was received in subframe (n - 4)
      // 3.) if the answer to #2 is yes then send ACK IF NOT send DTX

405 406 407 408 409
    } else if ((harq_information->harq_information_rel9_fdd.ack_nack_mode == 0)
               && (harq_information->harq_information_rel9_fdd.harq_size
                   == 2)) {
      pdu->harq_indication_fdd_rel13.tl.tag =
          NFAPI_HARQ_INDICATION_FDD_REL13_TAG;
410 411
      pdu->harq_indication_fdd_rel13.mode = 0;
      pdu->harq_indication_fdd_rel13.number_of_ack_nack = 2;
412 413 414 415
      pdu->harq_indication_fdd_rel13.harq_tb_n[0] =
          1; // Assuming always an ACK (No NACK or DTX)
      pdu->harq_indication_fdd_rel13.harq_tb_n[1] =
          1; // Assuming always an ACK (No NACK or DTX)
416
    }
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  } else if (harq_information->harq_information_rel10_tdd.tl.tag
             == NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL10_TDD_TAG) {
    if ((harq_information->harq_information_rel10_tdd.ack_nack_mode == 0)
        && (harq_information->harq_information_rel10_tdd.harq_size == 1)) {
      pdu->harq_indication_tdd_rel13.tl.tag =
          NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
      pdu->harq_indication_tdd_rel13.mode = 0;
      pdu->harq_indication_tdd_rel13.number_of_ack_nack = 1;
      pdu->harq_indication_tdd_rel13.harq_data[0].bundling.value_0 = 1;

    } else if ((harq_information->harq_information_rel10_tdd.ack_nack_mode == 1)
               && (harq_information->harq_information_rel10_tdd.harq_size
                   == 2)) {
      pdu->harq_indication_tdd_rel13.tl.tag =
          NFAPI_HARQ_INDICATION_TDD_REL13_TAG;
      pdu->harq_indication_tdd_rel13.mode = 0;
      pdu->harq_indication_tdd_rel13.number_of_ack_nack = 1;
      pdu->harq_indication_tdd_rel13.harq_data[0].bundling.value_0 = 1;
      pdu->harq_indication_tdd_rel13.harq_data[1].bundling.value_0 = 1;
    }
  } else
    AssertFatal(1 == 0, "only format 1a/b for now, received \n");
439 440

  UL_INFO->harq_ind.harq_indication_body.number_of_harqs++;
441 442 443
  LOG_D(PHY,
        "Incremented eNB->UL_INFO.harq_ind.number_of_harqs:%d\n",
        UL_INFO->harq_ind.harq_indication_body.number_of_harqs);
444
  pthread_mutex_unlock(&fill_ul_mutex.harq_mutex);
445 446 447
}

void handle_nfapi_ul_pdu_UE_MAC(module_id_t Mod_id,
448 449 450 451
                                nfapi_ul_config_request_pdu_t *ul_config_pdu,
                                uint16_t frame,
                                uint8_t subframe,
                                uint8_t srs_present,
452 453
                                int index,
                                nfapi_ul_config_request_t *ul_config_req) {
454
  if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE) {
455 456 457 458 459 460
    LOG_D(PHY,
          "Applying UL config for UE, rnti %x for frame %d, subframe %d\n",
          (ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8).rnti,
          frame,
          subframe);
    uint8_t ulsch_buffer[5477] __attribute__((aligned(32)));
461 462 463
    uint16_t buflen = ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size;

    uint16_t rnti = ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti;
464 465 466 467 468 469 470 471
    uint8_t access_mode = SCHEDULED_ACCESS;
    if (buflen > 0) {
      if (UE_mac_inst[Mod_id].first_ULSCH_Tx == 1) { // Msg3 case
        LOG_D(MAC,
              "handle_nfapi_ul_pdu_UE_MAC 2.2, Mod_id:%d, SFN/SF: %d/%d \n",
              Mod_id,
              frame,
              subframe);
472 473
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti,
                                  ul_config_req);
474 475 476 477 478 479 480
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  UE_mac_inst[Mod_id].RA_prach_resources.Msg3,
                                  buflen,
                                  rnti,
Michael Cook's avatar
Michael Cook committed
481 482
                                  index,
                                  ul_config_req);
483 484 485 486 487 488 489 490 491
        Msg3_transmitted(Mod_id, 0, frame, 0);
        //  Modification
        UE_mac_inst[Mod_id].UE_mode[0] = PUSCH;
        UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;

        // This should be done after the reception of the respective hi_dci0
        // UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
      } else {
        ue_get_sdu(Mod_id, 0, frame, subframe, 0, ulsch_buffer, buflen, &access_mode);
Michael Cook's avatar
Michael Cook committed
492
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti, ul_config_req);
493 494 495 496 497 498 499
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  ulsch_buffer,
                                  buflen,
                                  rnti,
500 501
                                  index,
                                  ul_config_req);
502
      }
503
    }
Andrew Burger's avatar
Andrew Burger committed
504
  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE) {
505 506 507 508 509 510 511 512 513 514 515 516
    // AssertFatal((UE_id =
    // find_ulsch(ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE))>=0,
    //            "No available UE ULSCH for rnti
    //            %x\n",ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti);
    uint8_t ulsch_buffer[5477] __attribute__((aligned(32)));
    uint16_t buflen =
        ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.size;
    nfapi_ul_config_ulsch_harq_information *ulsch_harq_information =
        &ul_config_pdu->ulsch_harq_pdu.harq_information;
    uint16_t rnti = ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti;
    uint8_t access_mode = SCHEDULED_ACCESS;
    if (buflen > 0) {
Andrew Burger's avatar
Andrew Burger committed
517
      if (UE_mac_inst[Mod_id].first_ULSCH_Tx == 1) {
Michael Cook's avatar
Michael Cook committed
518
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti, ul_config_req);
519 520 521 522 523 524 525
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  UE_mac_inst[Mod_id].RA_prach_resources.Msg3,
                                  buflen,
                                  rnti,
526 527
                                  index,
                                  ul_config_req);
528 529 530 531 532 533 534
        Msg3_transmitted(Mod_id, 0, frame, 0);
        // UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
        // Modification
        UE_mac_inst[Mod_id].UE_mode[0] = PUSCH;
        UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
      } else {
        ue_get_sdu(Mod_id, 0, frame, subframe, 0, ulsch_buffer, buflen, &access_mode);
Michael Cook's avatar
Michael Cook committed
535
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti, ul_config_req);
536 537 538 539 540 541 542
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  ulsch_buffer,
                                  buflen,
                                  rnti,
543 544
                                  index,
                                  ul_config_req);
545 546 547 548
      }
    }
    if (ulsch_harq_information != NULL)
      fill_ulsch_harq_indication_UE_MAC(
Michael Cook's avatar
Michael Cook committed
549
          Mod_id, frame, subframe, UL_INFO, ulsch_harq_information, rnti, ul_config_req);
550 551 552 553 554 555 556 557 558

  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE) {
    uint8_t ulsch_buffer[5477] __attribute__((aligned(32)));
    uint16_t buflen = ul_config_pdu->ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.size;

    uint16_t rnti = ul_config_pdu->ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti;
    uint8_t access_mode = SCHEDULED_ACCESS;
    if (buflen > 0) {
      if (UE_mac_inst[Mod_id].first_ULSCH_Tx == 1) { // Msg3 case
Michael Cook's avatar
Michael Cook committed
559
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti, ul_config_req);
560 561 562 563 564 565 566
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  UE_mac_inst[Mod_id].RA_prach_resources.Msg3,
                                  buflen,
                                  rnti,
567 568
                                  index,
                                  ul_config_req);
569 570 571 572 573 574 575
        Msg3_transmitted(Mod_id, 0, frame, 0);
        // UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
        // Modification
        UE_mac_inst[Mod_id].UE_mode[0] = PUSCH;
        UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
      } else {
        ue_get_sdu(Mod_id, 0, frame, subframe, 0, ulsch_buffer, buflen, &access_mode);
Michael Cook's avatar
Michael Cook committed
576
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti, ul_config_req);
577 578 579 580 581 582 583
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  ulsch_buffer,
                                  buflen,
                                  rnti,
584 585
                                  index,
                                  ul_config_req);
586
        fill_ulsch_cqi_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, rnti);
587 588 589 590 591 592 593 594 595 596 597 598
      }
    }
  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE) {
    uint8_t ulsch_buffer[5477] __attribute__((aligned(32)));
    uint16_t buflen = ul_config_pdu->ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.size;
    nfapi_ul_config_ulsch_harq_information *ulsch_harq_information =
        &ul_config_pdu->ulsch_cqi_harq_ri_pdu.harq_information;

    uint16_t rnti = ul_config_pdu->ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti;
    uint8_t access_mode = SCHEDULED_ACCESS;
    if (buflen > 0) {
      if (UE_mac_inst[Mod_id].first_ULSCH_Tx == 1) { // Msg3 case
Michael Cook's avatar
Michael Cook committed
599
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti, ul_config_req);
600 601 602 603 604 605 606
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  UE_mac_inst[Mod_id].RA_prach_resources.Msg3,
                                  buflen,
                                  rnti,
607 608
                                  index,
                                  ul_config_req);
609 610 611 612 613 614 615
        Msg3_transmitted(Mod_id, 0, frame, 0);
        // UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
        // Modification
        UE_mac_inst[Mod_id].UE_mode[0] = PUSCH;
        UE_mac_inst[Mod_id].first_ULSCH_Tx = 0;
      } else {
        ue_get_sdu(Mod_id, 0, frame, subframe, 0, ulsch_buffer, buflen, &access_mode);
Michael Cook's avatar
Michael Cook committed
616
        fill_crc_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, 0, index, rnti, ul_config_req);
617 618 619 620 621 622 623
        fill_rx_indication_UE_MAC(Mod_id,
                                  frame,
                                  subframe,
                                  UL_INFO,
                                  ulsch_buffer,
                                  buflen,
                                  rnti,
624 625
                                  index,
                                  ul_config_req);
626
        fill_ulsch_cqi_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, rnti);
627 628
      }
    }
629

630
    if (ulsch_harq_information != NULL)
Michael Cook's avatar
Michael Cook committed
631
      fill_ulsch_harq_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, ulsch_harq_information, rnti, ul_config_req);
632 633 634 635 636
  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE) {
    uint16_t rnti = ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti;

    nfapi_ul_config_harq_information *ulsch_harq_information = &ul_config_pdu->uci_harq_pdu.harq_information;
    if (ulsch_harq_information != NULL)
Michael Cook's avatar
Michael Cook committed
637
      fill_uci_harq_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, ulsch_harq_information, rnti, ul_config_req);
638 639 640 641 642 643 644 645 646 647 648 649 650
  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE) {
    AssertFatal(1 == 0, "NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE not handled yet\n");
  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE) {
    AssertFatal(1 == 0,
                "NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE not handled yet\n");
  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE) {
    AssertFatal(1 == 0,
                "NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE not handled yet\n");
  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE) {
    uint16_t rnti =
        ul_config_pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti;

    if (ue_get_SR(Mod_id, 0, frame, 0, rnti, subframe))
Michael Cook's avatar
Michael Cook committed
651
      fill_sr_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, rnti, ul_config_req);
652 653 654 655 656 657 658 659 660 661 662 663 664

  } else if (ul_config_pdu->pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE) {
    // AssertFatal((UE_id =
    // find_uci(rel8->rnti,proc->frame_tx,proc->subframe_tx,eNB,SEARCH_EXIST_OR_FREE))>=0,
    //            "No available UE UCI for rnti
    //            %x\n",ul_config_pdu->uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti);

    uint16_t rnti =
        ul_config_pdu->uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti;

    // We fill the sr_indication only if ue_get_sr() would normally instruct PHY
    // to send a SR.
    if (ue_get_SR(Mod_id, 0, frame, 0, rnti, subframe))
Michael Cook's avatar
Michael Cook committed
665
      fill_sr_indication_UE_MAC(Mod_id, frame, subframe, UL_INFO, rnti, ul_config_req);
666 667 668 669 670

    nfapi_ul_config_harq_information *ulsch_harq_information =
        &ul_config_pdu->uci_sr_harq_pdu.harq_information;
    if (ulsch_harq_information != NULL)
      fill_uci_harq_indication_UE_MAC(
Michael Cook's avatar
Michael Cook committed
671
          Mod_id, frame, subframe, UL_INFO, ulsch_harq_information, rnti, ul_config_req);
672 673 674
  }
}

675 676 677 678 679 680 681 682 683 684 685 686
int ul_config_req_UE_MAC(nfapi_ul_config_request_t *req,
                         int timer_frame,
                         int timer_subframe,
                         module_id_t Mod_id) {
  LOG_D(PHY,
        "[PNF] UL_CONFIG_REQ %s() sfn_sf:%d pdu:%d "
        "rach_prach_frequency_resources:%d srs_present:%u\n",
        __FUNCTION__,
        NFAPI_SFNSF2DEC(req->sfn_sf),
        req->ul_config_request_body.number_of_pdus,
        req->ul_config_request_body.rach_prach_frequency_resources,
        req->ul_config_request_body.srs_present);
687

Andrew Burger's avatar
Andrew Burger committed
688 689 690
  LOG_D(MAC, "ul_config_req Frame: %d Subframe: %d Proxy Frame: %u Subframe: %u\n",
        NFAPI_SFNSF2SFN(req->sfn_sf), NFAPI_SFNSF2SF(req->sfn_sf),
        timer_frame, timer_subframe);
Andrew Burger's avatar
Andrew Burger committed
691
  int sfn = NFAPI_SFNSF2SFN(req->sfn_sf);
Andrew Burger's avatar
Andrew Burger committed
692
  int sf = NFAPI_SFNSF2SF(req->sfn_sf);
693

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
  LOG_D(MAC,
        "ul_config_req_UE_MAC() TOTAL NUMBER OF UL_CONFIG PDUs: %d, SFN/SF: "
        "%d/%d \n",
        req->ul_config_request_body.number_of_pdus,
        timer_frame,
        timer_subframe);

  const rnti_t rnti = UE_mac_inst[Mod_id].crnti;
  for (int i = 0; i < req->ul_config_request_body.number_of_pdus; i++) {
    nfapi_ul_config_request_pdu_t* pdu = &req->ul_config_request_body.ul_config_pdu_list[i];
    const int pdu_type = pdu->pdu_type;
    if (   (pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE
            && pdu->ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
        || (pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE
            && pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
        || (pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE
            && pdu->ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
        || (pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE
            && pdu->ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
        || (pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE
            && pdu->uci_cqi_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)
        || (pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE
            && pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti == rnti)
        || (pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE
            && pdu->uci_cqi_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)) {
      handle_nfapi_ul_pdu_UE_MAC(
720
          Mod_id, pdu, sfn, sf, req->ul_config_request_body.srs_present, i, req);
721
    } else {
722
      LOG_D(MAC, "UNKNOWN UL_CONFIG_REQ PDU_TYPE or RNTI not matching pdu type: %d\n", pdu_type);
723 724 725 726 727 728
    }
  }

  return 0;
}

729 730 731 732 733 734 735 736 737 738 739 740
int tx_req_UE_MAC(nfapi_tx_request_t *req) {
  LOG_D(PHY,
        "%s() SFN/SF:%d/%d PDUs:%d\n",
        __FUNCTION__,
        NFAPI_SFNSF2SFN(req->sfn_sf),
        NFAPI_SFNSF2SF(req->sfn_sf),
        req->tx_request_body.number_of_pdus);

  for (int i = 0; i < req->tx_request_body.number_of_pdus; i++) {
    LOG_D(PHY,
          "%s() SFN/SF:%d/%d number_of_pdus:%d [PDU:%d] pdu_length:%d "
          "pdu_index:%d num_segments:%d\n",
741
          __FUNCTION__,
742 743
          NFAPI_SFNSF2SFN(req->sfn_sf),
          NFAPI_SFNSF2SF(req->sfn_sf),
744 745 746 747
          req->tx_request_body.number_of_pdus,
          i,
          req->tx_request_body.tx_pdu_list[i].pdu_length,
          req->tx_request_body.tx_pdu_list[i].pdu_index,
748 749
          req->tx_request_body.tx_pdu_list[i].num_segments);
  }
750 751 752 753

  return 0;
}

754 755 756 757
void dl_config_req_UE_MAC_dci(int sfn,
                              int sf,
                              nfapi_dl_config_request_pdu_t *dci,
                              nfapi_dl_config_request_pdu_t *dlsch,
758
                              int num_ue,
Andrew Burger's avatar
Andrew Burger committed
759
                              nfapi_tx_req_pdu_list_t *tx_req_pdu_list) {
760 761 762 763 764 765 766 767 768 769 770
  DevAssert(dci->pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE);
  DevAssert(dlsch->pdu_type == NFAPI_DL_CONFIG_DLSCH_PDU_TYPE);

  const rnti_t rnti = dci->dci_dl_pdu.dci_dl_pdu_rel8.rnti;
  const int rnti_type = dci->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type;
  if (rnti != dlsch->dlsch_pdu.dlsch_pdu_rel8.rnti) {
    LOG_E(MAC,
          "%s(): sfn/sf %d.%d DLSCH PDU RNTI %x does not match DCI RNTI %x\n",
          __func__, sfn, sf, rnti, dlsch->dlsch_pdu.dlsch_pdu_rel8.rnti);
    return;
  }
771

772
  const int pdu_index = dlsch->dlsch_pdu.dlsch_pdu_rel8.pdu_index;
Andrew Burger's avatar
Andrew Burger committed
773
  if (pdu_index < 0 || pdu_index >= tx_req_pdu_list->num_pdus) {
Michael Cook's avatar
Michael Cook committed
774 775 776 777 778
    LOG_E(MAC,
          "%s(): Problem with receiving data: "
          "sfn/sf:%d.%d PDU size:%d, TX_PDU index: %d\n",
          __func__,
          sfn, sf, dci->pdu_size, dlsch->dlsch_pdu.dlsch_pdu_rel8.pdu_index);
779 780
    return;
  }
781

Andrew Burger's avatar
Andrew Burger committed
782
  LOG_I(MAC, "%s() rnti value: 0x%x rnti type: %d\n", __func__,
783
        rnti, rnti_type);
784 785 786
  if (rnti_type == 1) { // C-RNTI (Normal DLSCH case)
    for (int ue_id = 0; ue_id < num_ue; ue_id++) {
      if (UE_mac_inst[ue_id].crnti == rnti) {
Andrew Burger's avatar
Andrew Burger committed
787
        LOG_I(MAC,
788
              "%s() Received data: sfn/sf:%d.%d "
789 790
              "size:%d, TX_PDU index: %d, tx_req_num_elems: %d \n",
              __func__,
791 792
              sfn, sf, dci->pdu_size,
              dlsch->dlsch_pdu.dlsch_pdu_rel8.pdu_index,
Andrew Burger's avatar
Andrew Burger committed
793
              tx_req_pdu_list->num_pdus);
794
        ue_send_sdu(ue_id, 0, sfn, sf,
Andrew Burger's avatar
Andrew Burger committed
795 796
            tx_req_pdu_list->pdus[pdu_index].segments[0].segment_data,
            tx_req_pdu_list->pdus[pdu_index].segments[0].segment_length,
797
            0);
798 799 800 801 802 803 804 805 806 807
        return;
      }
    }
  } else if (rnti_type == 2) {
    if (rnti == 0xFFFF) { /* SI-RNTI */
      for (int ue_id = 0; ue_id < num_ue; ue_id++) {
        if (UE_mac_inst[ue_id].UE_mode[0] == NOT_SYNCHED)
          continue;

        ue_decode_si(ue_id, 0, sfn, 0,
Andrew Burger's avatar
Andrew Burger committed
808 809
            tx_req_pdu_list->pdus[pdu_index].segments[0].segment_data,
            tx_req_pdu_list->pdus[pdu_index].segments[0].segment_length);
810 811 812 813 814 815
      }
    } else if (rnti == 0xFFFE) { /* PI-RNTI */
      for (int ue_id = 0; ue_id < num_ue; ue_id++) {
        LOG_I(MAC, "%s() Received paging message: sfn/sf:%d.%d\n",
              __func__, sfn, sf);
        ue_decode_p(ue_id, 0, sfn, 0,
Andrew Burger's avatar
Andrew Burger committed
816 817
                    tx_req_pdu_list->pdus[pdu_index].segments[0].segment_data,
                    tx_req_pdu_list->pdus[pdu_index].segments[0].segment_length);
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
      }
    } else if (rnti == 0x0002) { /* RA-RNTI */
      for (int ue_id = 0; ue_id < num_ue; ue_id++) {
        if (UE_mac_inst[ue_id].UE_mode[0] != RA_RESPONSE) {
          LOG_D(MAC, "UE %d not awaiting RAR, is in mode %d\n",
                ue_id, UE_mac_inst[ue_id].UE_mode[0]);
          continue;
        }
        // RNTI parameter not actually used. Provided only to comply with
        // existing function definition.  Not sure about parameters to fill
        // the preamble index.
        const rnti_t ra_rnti = UE_mac_inst[ue_id].RA_prach_resources.ra_RNTI;
        DevAssert(ra_rnti == 0x0002);
        if (UE_mac_inst[ue_id].UE_mode[0] != PUSCH
            && UE_mac_inst[ue_id].RA_prach_resources.Msg3 != NULL
            && ra_rnti == dlsch->dlsch_pdu.dlsch_pdu_rel8.rnti) {
          LOG_E(MAC,
                "%s(): Received RAR, PreambleIndex: %d\n",
                __func__, UE_mac_inst[ue_id].RA_prach_resources.ra_PreambleIndex);
          ue_process_rar(ue_id, 0, sfn,
              ra_rnti, //RA-RNTI
Andrew Burger's avatar
Andrew Burger committed
839
              tx_req_pdu_list->pdus[pdu_index].segments[0].segment_data,
840 841
              &UE_mac_inst[ue_id].crnti, //t-crnti
              UE_mac_inst[ue_id].RA_prach_resources.ra_PreambleIndex,
Andrew Burger's avatar
Andrew Burger committed
842
              tx_req_pdu_list->pdus[pdu_index].segments[0].segment_data);
Andrew Burger's avatar
Andrew Burger committed
843 844
          // UE_mac_inst[ue_id].UE_mode[0] = RA_RESPONSE;
          LOG_I(MAC, "setting UE_MODE now: %d\n", UE_mac_inst[ue_id].UE_mode[0]);
845 846 847
          // Expecting an UL_CONFIG_ULSCH_PDU to enable Msg3 Txon (first
          // ULSCH Txon for the UE)
          UE_mac_inst[ue_id].first_ULSCH_Tx = 1;
848 849
        }
      }
850 851
    } else {
      LOG_W(MAC, "can not handle special RNTI %x\n", rnti);
852 853 854 855
    }
  }
}

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
void dl_config_req_UE_MAC_bch(int sfn,
                              int sf,
                              nfapi_dl_config_request_pdu_t *bch,
                              int num_ue) {
  DevAssert(bch->pdu_type == NFAPI_DL_CONFIG_BCH_PDU_TYPE);

  for (int ue_id = 0; ue_id < num_ue; ue_id++) {
    if (UE_mac_inst[ue_id].UE_mode[0] == NOT_SYNCHED){
      dl_phy_sync_success(ue_id, sfn, 0, 1);
      LOG_E(MAC,
            "%s(): Received MIB: UE_mode: %d, sfn/sf: %d.%d\n",
            __func__,
            UE_mac_inst[ue_id].UE_mode[0],
            sfn,
            sf);
      UE_mac_inst[ue_id].UE_mode[0] = PRACH;
    } else {
      dl_phy_sync_success(ue_id, sfn, 0, 0);
874 875
    }
  }
876
}
877

Javier Morgade's avatar
Javier Morgade committed
878 879 880
void dl_config_req_UE_MAC_mch(int sfn,
                              int sf,
                              nfapi_dl_config_request_pdu_t *mch,
881
                              int num_ue,
Andrew Burger's avatar
Andrew Burger committed
882
                              nfapi_tx_req_pdu_list_t *tx_req_pdu_list) {
Javier Morgade's avatar
Javier Morgade committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896
  DevAssert(mch->pdu_type == NFAPI_DL_CONFIG_MCH_PDU_TYPE);

  for (int ue_id = 0; ue_id < num_ue; ue_id++) {
    if (UE_mac_inst[ue_id].UE_mode[0] == NOT_SYNCHED){
	 LOG_D(MAC,
            "%s(): Received MCH in NOT_SYNCHED: UE_mode: %d, sfn/sf: %d.%d\n",
            __func__,
            UE_mac_inst[ue_id].UE_mode[0],
            sfn,
            sf);
	return;

    } else {
	 const int pdu_index = mch->mch_pdu.mch_pdu_rel8.pdu_index;
Andrew Burger's avatar
Andrew Burger committed
897
	if (pdu_index < 0 || pdu_index >= tx_req_pdu_list->num_pdus) {
Michael Cook's avatar
Michael Cook committed
898 899 900 901 902
    	LOG_E(MAC,
          "%s(): Problem with receiving data: "
          "sfn/sf:%d.%d PDU size:%d, TX_PDU index: %d\n",
          __func__,
          sfn, sf, mch->pdu_size, mch->mch_pdu.mch_pdu_rel8.pdu_index);
Javier Morgade's avatar
Javier Morgade committed
903 904 905
    	return;
  	}
        ue_send_mch_sdu(ue_id, 0, sfn,
Andrew Burger's avatar
Andrew Burger committed
906 907
            tx_req_pdu_list->pdus[pdu_index].segments[0].segment_data,
            tx_req_pdu_list->pdus[pdu_index].segments[0].segment_length,
Javier Morgade's avatar
Javier Morgade committed
908 909 910 911 912
            0,0);
    }
  }
}

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
void hi_dci0_req_UE_MAC(int sfn,
                        int sf,
                        nfapi_hi_dci0_request_pdu_t* hi_dci0,
                        int num_ue) {
  if (hi_dci0->pdu_type != NFAPI_HI_DCI0_DCI_PDU_TYPE)
    return;

  const nfapi_hi_dci0_dci_pdu_rel8_t *dci = &hi_dci0->dci_pdu.dci_pdu_rel8;
  if (!dci->cqi_csi_request)
    return;
  for (int ue_id = 0; ue_id < num_ue; ue_id++) {
    if (dci->rnti == UE_mac_inst[ue_id].crnti) {
      return;
    }
  }
928 929
}

930 931
// The following set of memcpy functions should be getting called as callback
// functions from pnf_p7_subframe_ind.
Andrew Burger's avatar
Andrew Burger committed
932
int memcpy_dl_config_req(L1_rxtx_proc_t *proc,
Michael Cook's avatar
Michael Cook committed
933 934
			nfapi_pnf_p7_config_t *pnf_p7,
                         nfapi_dl_config_request_t *req) {
935

936
  nfapi_dl_config_request_t *p = malloc(sizeof(nfapi_dl_config_request_t));
937

938 939
  // UE_mac_inst[Mod_id].p->header = req->header;
  p->sfn_sf = req->sfn_sf;
940

941
  p->vendor_extension = req->vendor_extension;
942

943 944 945 946
  p->dl_config_request_body.number_dci = req->dl_config_request_body.number_dci;
  p->dl_config_request_body.number_pdcch_ofdm_symbols = req->dl_config_request_body.number_pdcch_ofdm_symbols;
  p->dl_config_request_body.number_pdsch_rnti = req->dl_config_request_body.number_pdsch_rnti;
  p->dl_config_request_body.number_pdu = req->dl_config_request_body.number_pdu;
947

948 949 950 951
  p->dl_config_request_body.tl.tag = req->dl_config_request_body.tl.tag;
  p->dl_config_request_body.tl.length = req->dl_config_request_body.tl.length;

  p->dl_config_request_body.dl_config_pdu_list =
952 953
      calloc(req->dl_config_request_body.number_pdu,
             sizeof(nfapi_dl_config_request_pdu_t));
954 955
  for (int i = 0; i < p->dl_config_request_body.number_pdu; i++) {
    p->dl_config_request_body.dl_config_pdu_list[i] =
956 957
        req->dl_config_request_body.dl_config_pdu_list[i];
  }
958

959
  return 0;
960 961
}

Andrew Burger's avatar
Andrew Burger committed
962
static bool is_my_ul_config_req(nfapi_ul_config_request_t *req)
963
{
Andrew Burger's avatar
Andrew Burger committed
964
  bool is_my_rnti = false;
Andrew Burger's avatar
Andrew Burger committed
965
  const rnti_t rnti = UE_mac_inst[0].crnti; // 0 for standalone pnf mode. TODO: Make this more clear - Andrew
Andrew Burger's avatar
Andrew Burger committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
  for (int i = 0; i < req->ul_config_request_body.number_of_pdus; i++)
  {
    nfapi_ul_config_request_pdu_t *pdu = &req->ul_config_request_body.ul_config_pdu_list[i];
    const int pdu_type = pdu->pdu_type;
    if ((pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE && pdu->ulsch_pdu.ulsch_pdu_rel8.rnti == rnti) ||
        (pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE && pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti) ||
        (pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE && pdu->ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti) ||
        (pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE && pdu->ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti) ||
        (pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE && pdu->uci_cqi_harq_pdu.ue_information.ue_information_rel8.rnti == rnti) ||
        (pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE && pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti == rnti) ||
        (pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE && pdu->uci_cqi_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti))
    {
      is_my_rnti = true;
      break;
    }
    else
    {
      LOG_D(MAC, "UNKNOWN UL_CONFIG_REQ PDU_TYPE: %d or RNTI is not mine \n", pdu_type);
    }
  }

  return is_my_rnti;
}

int memcpy_ul_config_req(L1_rxtx_proc_t *proc, nfapi_pnf_p7_config_t *pnf_p7, nfapi_ul_config_request_t *req)
{
  if (!is_my_ul_config_req(req)) return 0;

994
  nfapi_ul_config_request_t *p = malloc(sizeof(nfapi_ul_config_request_t));
995

996 997
  p->sfn_sf = req->sfn_sf;
  p->vendor_extension = req->vendor_extension;
998

999 1000 1001
  p->ul_config_request_body.number_of_pdus = req->ul_config_request_body.number_of_pdus;
  p->ul_config_request_body.rach_prach_frequency_resources = req->ul_config_request_body.rach_prach_frequency_resources;
  p->ul_config_request_body.srs_present = req->ul_config_request_body.srs_present;
1002

1003 1004
  p->ul_config_request_body.tl.tag = req->ul_config_request_body.tl.tag;
  p->ul_config_request_body.tl.length = req->ul_config_request_body.tl.length;
1005

1006
  p->ul_config_request_body.ul_config_pdu_list =
1007 1008
      calloc(req->ul_config_request_body.number_of_pdus,
             sizeof(nfapi_ul_config_request_pdu_t));
Andrew Burger's avatar
Andrew Burger committed
1009 1010
  for (int i = 0; i < p->ul_config_request_body.number_of_pdus; i++)
  {
1011
    p->ul_config_request_body.ul_config_pdu_list[i] =
1012 1013
        req->ul_config_request_body.ul_config_pdu_list[i];
  }
1014

Andrew Burger's avatar
Andrew Burger committed
1015 1016
  if (!put_queue(&ul_config_req_queue, p))
  {
1017 1018
    free(p);
  }
Andrew Burger's avatar
Andrew Burger committed
1019

1020
  return 0;
1021 1022
}

Andrew Burger's avatar
Andrew Burger committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
void nfapi_free_tx_req_pdu_list(nfapi_tx_req_pdu_list_t *list)
{
  // free all the p[i].segments[j].segment_data memory
  int num_pdus = list->num_pdus;
  nfapi_tx_request_pdu_t *pdus = list->pdus;
  for (int i = 0; i < num_pdus; i++) {
    int num_segments = pdus[i].num_segments;
    for (int j = 0; j < num_segments; j++) {
      free(pdus[i].segments[j].segment_data);
      pdus[i].segments[j].segment_data = NULL;
    }
  }
  free(list);
}

1038
int memcpy_tx_req(nfapi_pnf_p7_config_t *pnf_p7, nfapi_tx_request_t *req) {
Andrew Burger's avatar
Andrew Burger committed
1039 1040 1041 1042 1043 1044 1045 1046

  int num_pdus = req->tx_request_body.number_of_pdus;

  nfapi_tx_req_pdu_list_t *list = calloc(1, sizeof(nfapi_tx_req_pdu_list_t) + num_pdus * sizeof(nfapi_tx_request_pdu_t));
  list->num_pdus = num_pdus;
  nfapi_tx_request_pdu_t *p = list->pdus;

  for (int i = 0; i < num_pdus; i++) {
1047 1048 1049
    p[i].num_segments = req->tx_request_body.tx_pdu_list[i].num_segments;
    p[i].pdu_index = req->tx_request_body.tx_pdu_list[i].pdu_index;
    p[i].pdu_length = req->tx_request_body.tx_pdu_list[i].pdu_length;
1050
    for (int j = 0; j < req->tx_request_body.tx_pdu_list[i].num_segments; j++) {
1051 1052 1053 1054 1055
      p[i].segments[j].segment_length = req->tx_request_body.tx_pdu_list[i].segments[j].segment_length;
      if (p[i].segments[j].segment_length > 0) {
        p[i].segments[j].segment_data = calloc(
            p[i].segments[j].segment_length, sizeof(uint8_t));
        memcpy(p[i].segments[j].segment_data,
1056
               req->tx_request_body.tx_pdu_list[i].segments[j].segment_data,
1057
               p[i].segments[j].segment_length);
1058 1059 1060
      }
    }
  }
1061

1062
  return 0;
1063 1064
}

Andrew Burger's avatar
Andrew Burger committed
1065 1066 1067
static bool is_my_hi_dci0_req(nfapi_hi_dci0_request_t *hi_dci0_req)
{
  bool is_my_rnti = false;
1068
  const rnti_t rnti = UE_mac_inst[0].crnti; // This is 0 representing the num_ues is 1
Andrew Burger's avatar
Andrew Burger committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
  nfapi_hi_dci0_request_body_t *hi_dci0_body = &hi_dci0_req->hi_dci0_request_body;
  for (int i = 0; i < hi_dci0_body->number_of_dci + hi_dci0_body->number_of_hi; i++)
  {
    nfapi_hi_dci0_request_pdu_t *hi_dci0 = &hi_dci0_body->hi_dci0_pdu_list[i];
    if (hi_dci0->pdu_type != NFAPI_HI_DCI0_DCI_PDU_TYPE || hi_dci0->dci_pdu.dci_pdu_rel8.cqi_csi_request)
    {
      continue;
    }
    if (hi_dci0->dci_pdu.dci_pdu_rel8.rnti == rnti)
    {
      is_my_rnti = true;
      break;
    }
  }

  return is_my_rnti;
}

Andrew Burger's avatar
Andrew Burger committed
1087 1088
int memcpy_hi_dci0_req (L1_rxtx_proc_t *proc,
			nfapi_pnf_p7_config_t* pnf_p7,
Michael Cook's avatar
Michael Cook committed
1089
			nfapi_hi_dci0_request_t* req) {
1090 1091 1092 1093 1094 1095

  if (!is_my_hi_dci0_req(req))
  {
    LOG_W(MAC, "Filtering hi_dci0_req\n");
    return 0;
  }
1096
  nfapi_hi_dci0_request_t *p = (nfapi_hi_dci0_request_t *)malloc(sizeof(nfapi_hi_dci0_request_t));
1097 1098
	//if(req!=0){

1099

1100 1101
  p->sfn_sf = req->sfn_sf;
  p->vendor_extension = req->vendor_extension;
1102

1103 1104 1105
  p->hi_dci0_request_body.number_of_dci = req->hi_dci0_request_body.number_of_dci;
  p->hi_dci0_request_body.number_of_hi = req->hi_dci0_request_body.number_of_hi;
  p->hi_dci0_request_body.sfnsf = req->hi_dci0_request_body.sfnsf;
1106

1107
  // UE_mac_inst[Mod_id].p->hi_dci0_request_body.tl =
1108
  // req->hi_dci0_request_body.tl;
1109 1110
  p->hi_dci0_request_body.tl.tag = req->hi_dci0_request_body.tl.tag;
  p->hi_dci0_request_body.tl.length = req->hi_dci0_request_body.tl.length;
1111

1112 1113
  int total_pdus = p->hi_dci0_request_body.number_of_dci
                   + p->hi_dci0_request_body.number_of_hi;
1114

1115
  p->hi_dci0_request_body.hi_dci0_pdu_list =
1116
      calloc(total_pdus, sizeof(nfapi_hi_dci0_request_pdu_t));
1117

1118
  for (int i = 0; i < total_pdus; i++) {
1119
    p->hi_dci0_request_body.hi_dci0_pdu_list[i] = req->hi_dci0_request_body.hi_dci0_pdu_list[i];
1120 1121
    // LOG_I(MAC, "Original hi_dci0 req. type:%d, Copy type: %d
    // \n",req->hi_dci0_request_body.hi_dci0_pdu_list[i].pdu_type,
1122
    // UE_mac_inst[Mod_id].p->hi_dci0_request_body.hi_dci0_pdu_list[i].pdu_type);
1123
  }
1124 1125 1126 1127

  if (!put_queue(&hi_dci0_req_queue, p)) {
    free(p);
  }
Andrew Burger's avatar
Andrew Burger committed
1128
  LOG_I(MAC, "DCI0 QUEUE: %zu\n", hi_dci0_req_queue.num_items);
1129
  return 0;
1130 1131 1132
}

void UE_config_stub_pnf(void) {
1133
  int j;
1134
  paramdef_t L1_Params[] = L1PARAMS_DESC;
1135
  paramlist_def_t L1_ParamList = {CONFIG_STRING_L1_LIST, NULL, 0};
1136

1137
  config_getlist(&L1_ParamList, L1_Params, sizeof(L1_Params) / sizeof(paramdef_t), NULL);
1138
  if (L1_ParamList.numelt > 0) {
1139 1140 1141 1142
    for (j = 0; j < L1_ParamList.numelt; j++) {
      // nb_L1_CC = *(L1_ParamList.paramarray[j][L1_CC_IDX].uptr); // Number of
      // component carriers is of no use for the
      // phy_stub mode UE pnf. Maybe we can completely skip it.
1143

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
      if (strcmp(*(L1_ParamList.paramarray[j][L1_TRANSPORT_N_PREFERENCE_IDX].strptr), "local_mac") == 0) {
        sf_ahead = 4; // Need 4 subframe gap between RX and TX
      }
      // Right now that we have only one UE (thread) it is ok to put the
      // eth_params in the UE_mac_inst. Later I think we have to change that to
      // attribute eth_params to a global element for all the UEs.
      else if (strcmp(*(L1_ParamList.paramarray[j][L1_TRANSPORT_N_PREFERENCE_IDX].strptr), "nfapi") == 0) {
        stub_eth_params.local_if_name = strdup(
            *(L1_ParamList.paramarray[j][L1_LOCAL_N_IF_NAME_IDX].strptr));
        stub_eth_params.my_addr = strdup(
            *(L1_ParamList.paramarray[j][L1_LOCAL_N_ADDRESS_IDX].strptr));
        stub_eth_params.remote_addr = strdup(
            *(L1_ParamList.paramarray[j][L1_REMOTE_N_ADDRESS_IDX].strptr));
        stub_eth_params.my_portc =
            *(L1_ParamList.paramarray[j][L1_LOCAL_N_PORTC_IDX].iptr);
        stub_eth_params.remote_portc =
            *(L1_ParamList.paramarray[j][L1_REMOTE_N_PORTC_IDX].iptr);
        stub_eth_params.my_portd =
            *(L1_ParamList.paramarray[j][L1_LOCAL_N_PORTD_IDX].iptr);
        stub_eth_params.remote_portd =
            *(L1_ParamList.paramarray[j][L1_REMOTE_N_PORTD_IDX].iptr);
        stub_eth_params.transp_preference = ETH_UDP_MODE;

        sf_ahead = 2; // Cannot cope with 4 subframes betweem RX and TX - set it to 2
        // configure_nfapi_pnf(UE_mac_inst[0].eth_params_n.remote_addr,
        // UE_mac_inst[0].eth_params_n.remote_portc,
        // UE_mac_inst[0].eth_params_n.my_addr,
        // UE_mac_inst[0].eth_params_n.my_portd,
        // UE_mac_inst[0].eth_params_n.remote_portd);
        configure_nfapi_pnf(stub_eth_params.remote_addr,
                            stub_eth_params.remote_portc,
                            stub_eth_params.my_addr,
                            stub_eth_params.my_portd,
                            stub_eth_params.remote_portd);
      }
    }
1180 1181 1182
  }
}

Andrew Burger's avatar
Andrew Burger committed
1183
void ue_init_standalone_socket(const char *addr, int tx_port, int rx_port)
1184
{
Andrew Burger's avatar
Andrew Burger committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
  {
    struct sockaddr_in server_address;
    int addr_len = sizeof(server_address);
    memset(&server_address, 0, addr_len);
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(tx_port);

    int sd = socket(server_address.sin_family, SOCK_DGRAM, 0);
    if (sd < 0)
    {
      LOG_E(MAC, "Socket creation error standalone PNF\n");
      return;
    }
1198

Andrew Burger's avatar
Andrew Burger committed
1199 1200 1201 1202 1203 1204
    if (inet_pton(server_address.sin_family, addr, &server_address.sin_addr) <= 0)
    {
      LOG_E(MAC, "Invalid standalone PNF Address\n");
      close(sd);
      return;
    }
1205

Andrew Burger's avatar
Andrew Burger committed
1206 1207 1208 1209 1210 1211 1212 1213 1214
    // Using connect to use send() instead of sendto()
    if (connect(sd, (struct sockaddr *)&server_address, addr_len) < 0)
    {
      LOG_E(MAC, "Connection to standalone PNF failed: %s\n", strerror(errno));
      close(sd);
      return;
    }
    assert(ue_tx_sock_descriptor == -1);
    ue_tx_sock_descriptor = sd;
1215 1216
  }

Andrew Burger's avatar
Andrew Burger committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  {
    struct sockaddr_in server_address;
    int addr_len = sizeof(server_address);
    memset(&server_address, 0, addr_len);
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(rx_port);

    int sd = socket(server_address.sin_family, SOCK_DGRAM, 0);
    if (sd < 0)
    {
      LOG_E(MAC, "Socket creation error standalone PNF\n");
      return;
    }

    if (inet_pton(server_address.sin_family, addr, &server_address.sin_addr) <= 0)
    {
      LOG_E(MAC, "Invalid standalone PNF Address\n");
      close(sd);
      return;
    }

1238
    if (bind(sd, (struct sockaddr *)&server_address, addr_len) < 0)
Andrew Burger's avatar
Andrew Burger committed
1239 1240 1241 1242 1243 1244 1245
    {
      LOG_E(MAC, "Connection to standalone PNF failed: %s\n", strerror(errno));
      close(sd);
      return;
    }
    assert(ue_rx_sock_descriptor == -1);
    ue_rx_sock_descriptor = sd;
1246
  }
1247 1248
}

1249 1250
void *ue_standalone_pnf_task(void *context)
{
Andrew Burger's avatar
Andrew Burger committed
1251
  struct sockaddr_in server_address;
1252
  socklen_t addr_len = sizeof(server_address);
1253 1254
  char buffer[1024];

Andrew Burger's avatar
Andrew Burger committed
1255
  int sd = ue_rx_sock_descriptor;
1256
  assert(sd > 0);
1257 1258 1259

  nfapi_tx_request_t tx_req;
  bool tx_req_valid = false;
1260 1261
  while (true)
  {
RS's avatar
RS committed
1262
    ssize_t len = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&server_address, &addr_len);
1263 1264 1265 1266 1267 1268
    if (len == -1)
    {
      LOG_E(MAC, "reading from standalone pnf sctp socket failed \n");
      continue;
    }

Andrew Burger's avatar
Andrew Burger committed
1269
    if (len == sizeof(uint16_t))
1270
    {
Andrew Burger's avatar
Andrew Burger committed
1271
      uint16_t sfn_sf = 0;
1272
      memcpy((void *)&sfn_sf, buffer, sizeof(sfn_sf));
Andrew Burger's avatar
Andrew Burger committed
1273
      current_sfn_sf = sfn_sf;
1274 1275 1276 1277 1278 1279

      if (sem_post(&sfn_semaphore) != 0)
      {
        LOG_E(MAC, "sem_post() error\n");
        abort();
      }
1280
    }
1281
    else
1282
    {
1283 1284
      nfapi_p7_message_header_t header;
      if (nfapi_p7_message_header_unpack((void *)buffer, len, &header, sizeof(header), NULL) < 0)
1285
      {
1286 1287
        LOG_E(MAC, "Header unpack failed for standalone pnf\n");
        continue;
1288
      }
1289 1290 1291
      switch (header.message_id)
      {
      case NFAPI_DL_CONFIG_REQUEST:
1292
      {
1293 1294 1295 1296 1297
        nfapi_dl_config_request_t dl_config_req;
        if (nfapi_p7_message_unpack((void *)buffer, len, &dl_config_req,
                                    sizeof(dl_config_req), NULL) < 0)
        {
          LOG_E(MAC, "Message dl_config_req failed to unpack\n");
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
          break;
        }

        // In multiUE scenario possible to get a dl_config_req before the tx_req? -Melissa
        // How should we handle this? -Melissa
        LOG_I(MAC, "dl_config_req Frame: %u Subframe: %u\n", dl_config_req.sfn_sf >> 4,
              dl_config_req.sfn_sf & 15);
        if (!tx_req_valid)
        {
          LOG_W(MAC, "The tx_req not valid.\n");
          break;
1309
        }
1310
        if (dl_config_req.sfn_sf != tx_req.sfn_sf)
1311
        {
1312 1313 1314 1315 1316
          LOG_W(MAC, "sfnsf mismatch. dl_config_req Frame: %u Subframe: %u, tx_req Frame: %u Subframe: %u\n",
                dl_config_req.sfn_sf >> 4, dl_config_req.sfn_sf & 15,
                tx_req.sfn_sf >> 4, tx_req.sfn_sf & 15);
          tx_req_valid = false;
          break;
1317
        }
1318 1319 1320 1321

        enqueue_dl_config_req_tx_req(&dl_config_req, &tx_req);
        tx_req_valid = false;

1322
        break;
1323
      }
1324 1325 1326 1327 1328 1329 1330
      case NFAPI_TX_REQUEST:
      {
        // lock this tx_req
        if (nfapi_p7_message_unpack((void *)buffer, len, &tx_req,
                                    sizeof(tx_req), NULL) < 0)
        {
          LOG_E(MAC, "Message tx_req failed to unpack\n");
1331
          break;
1332
        }
1333 1334 1335 1336

        LOG_I(MAC, "tx_req Frame: %u Subframe: %u\n", tx_req.sfn_sf >> 4,
              tx_req.sfn_sf & 15);
        if (tx_req_valid)
1337
        {
1338
          LOG_W(MAC, "Received consecutive tx_reqs\n");
1339
        }
1340 1341 1342

        tx_req_valid = true;

1343 1344 1345
        break;
      }
      case NFAPI_HI_DCI0_REQUEST:
1346 1347 1348 1349 1350 1351 1352
      {
        nfapi_hi_dci0_request_t hi_dci0_req;
        // lock this hi_dci0_req
        if (nfapi_p7_message_unpack((void *)buffer, len, &hi_dci0_req,
                                    sizeof(hi_dci0_req), NULL) < 0)
        {
          LOG_E(MAC, "Message hi_dci0_req failed to unpack\n");
1353
          break;
1354
        }
1355 1356 1357 1358

        // check to see if hi_dci0_req is null
        memcpy_hi_dci0_req(NULL, NULL, &hi_dci0_req);

1359
        break;
1360 1361 1362 1363 1364 1365 1366 1367 1368
      }
      case NFAPI_UL_CONFIG_REQUEST:
      {
        nfapi_ul_config_request_t ul_config_req;
        // lock this ul_config_req
        if (nfapi_p7_message_unpack((void *)buffer, len, &ul_config_req,
                                    sizeof(ul_config_req), NULL) < 0)
        {
          LOG_E(MAC, "Message ul_config_req failed to unpack\n");
1369
          break;
1370
        }
1371 1372 1373 1374

        // check to see if ul_config_req is null
        memcpy_ul_config_req(NULL, NULL, &ul_config_req);

1375
        break;
1376
      }
1377 1378 1379 1380
      default:
        LOG_E(MAC, "Case Statement has no corresponding nfapi message\n");
        break;
      }
1381 1382 1383 1384
    }
  }
}

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510

void *memcpy_dl_config_req_standalone(nfapi_dl_config_request_t *dl_config_req)
{
  nfapi_dl_config_request_t *p = malloc(sizeof(nfapi_dl_config_request_t));

  p->sfn_sf = dl_config_req->sfn_sf;

  p->vendor_extension = dl_config_req->vendor_extension;

  p->dl_config_request_body.number_dci = dl_config_req->dl_config_request_body.number_dci;
  p->dl_config_request_body.number_pdcch_ofdm_symbols = dl_config_req->dl_config_request_body.number_pdcch_ofdm_symbols;
  p->dl_config_request_body.number_pdsch_rnti = dl_config_req->dl_config_request_body.number_pdsch_rnti;
  p->dl_config_request_body.number_pdu = dl_config_req->dl_config_request_body.number_pdu;

  p->dl_config_request_body.tl.tag = dl_config_req->dl_config_request_body.tl.tag;
  p->dl_config_request_body.tl.length = dl_config_req->dl_config_request_body.tl.length;

  p->dl_config_request_body.dl_config_pdu_list =
      calloc(dl_config_req->dl_config_request_body.number_pdu,
             sizeof(nfapi_dl_config_request_pdu_t));
  for (int i = 0; i < p->dl_config_request_body.number_pdu; i++) {
    p->dl_config_request_body.dl_config_pdu_list[i] =
        dl_config_req->dl_config_request_body.dl_config_pdu_list[i];
  }
  return p;
}

void *memcpy_tx_req_standalone(nfapi_tx_request_t *tx_req)
{
  int num_pdus = tx_req->tx_request_body.number_of_pdus;

  nfapi_tx_req_pdu_list_t *list = calloc(1, sizeof(nfapi_tx_req_pdu_list_t) + num_pdus * sizeof(nfapi_tx_request_pdu_t));
  list->num_pdus = num_pdus;
  nfapi_tx_request_pdu_t *p = list->pdus;

  for (int i = 0; i < num_pdus; i++)
  {
    p[i].num_segments = tx_req->tx_request_body.tx_pdu_list[i].num_segments;
    p[i].pdu_index = tx_req->tx_request_body.tx_pdu_list[i].pdu_index;
    p[i].pdu_length = tx_req->tx_request_body.tx_pdu_list[i].pdu_length;
    for (int j = 0; j < tx_req->tx_request_body.tx_pdu_list[i].num_segments; j++)
    {
      p[i].segments[j].segment_length = tx_req->tx_request_body.tx_pdu_list[i].segments[j].segment_length;
      if (p[i].segments[j].segment_length > 0)
      {
        p[i].segments[j].segment_data = calloc(
            p[i].segments[j].segment_length, sizeof(uint8_t));
        memcpy(p[i].segments[j].segment_data,
               tx_req->tx_request_body.tx_pdu_list[i].segments[j].segment_data,
               p[i].segments[j].segment_length);
      }
    }
  }
  return list;
}

static bool is_my_dl_config_req(const nfapi_dl_config_request_t *req)
{

  const rnti_t my_rnti = UE_mac_inst[0].crnti;
  int num_pdus = req->dl_config_request_body.number_pdu;
  // look through list of pdus for rnti type 1 with my_rnti (normal dlsch case)
  for (int i = 0; i < num_pdus; i++)
  {
    nfapi_dl_config_request_pdu_t *pdu = &req->dl_config_request_body.dl_config_pdu_list[i];
    const int pdu_type = pdu->pdu_type;
    if (pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
    {
      const rnti_t dci_rnti = pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti;
      const int rnti_type = pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type;
      if (rnti_type == 1 && dci_rnti == my_rnti)
      {
        return true;
      }
    }
    else if(pdu_type != NFAPI_DL_CONFIG_DLSCH_PDU_TYPE)
    {
      return true; // Because these two pdu_types are coupled and we have to accept all other pdu_types
    }
  }

  // Look for broadcasted rnti types
  for (int i = 0; i < num_pdus; i++)
  {
    nfapi_dl_config_request_pdu_t *pdu = &req->dl_config_request_body.dl_config_pdu_list[i];
    const int pdu_type = pdu->pdu_type;
    if (pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
    {
      const int rnti_type = pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type;
      if (rnti_type != 1)
      {
        return true;
      }
    }
  }


  return false;
}

void enqueue_dl_config_req_tx_req(nfapi_dl_config_request_t *dl_config_req, nfapi_tx_request_t *tx_req)
{
  if (!is_my_dl_config_req(dl_config_req))
  {
    LOG_I(MAC, "Filtering dl_config_req and tx_req\n");
    return;
  }

  nfapi_dl_config_request_t *dl_config_req_temp = memcpy_dl_config_req_standalone(dl_config_req);
  nfapi_tx_req_pdu_list_t *tx_req_temp = memcpy_tx_req_standalone(tx_req);
  LOG_E(MAC, "This is the num_pdus for tx_req: %d\n", tx_req_temp->num_pdus);
  LOG_E(MAC, "This is the num_pdus for dl_config_req and the sfn_sf: %d, %d:%d\n", dl_config_req_temp->dl_config_request_body.number_pdu,
        NFAPI_SFNSF2SFN(dl_config_req_temp->sfn_sf), NFAPI_SFNSF2SF(dl_config_req_temp->sfn_sf));

  nfapi_dl_config_req_tx_req_t *req = malloc(sizeof(nfapi_dl_config_req_tx_req_t));
  req->dl_config_req = dl_config_req_temp;
  req->tx_req_pdu_list = tx_req_temp;

  if (!put_queue(&dl_config_req_tx_req_queue, req))
  {
    free(req->dl_config_req);
    nfapi_free_tx_req_pdu_list(req->tx_req_pdu_list);
    free(req);
  }
}

Michael Cook's avatar
Michael Cook committed
1511
const char *hexdump(const void *data, size_t data_len, char *out, size_t out_len)
1512 1513 1514
{
    char *p = out;
    char *endp = out + out_len;
Michael Cook's avatar
Michael Cook committed
1515
    const uint8_t *q = data;
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
    snprintf(p, endp - p, "[%zu]", data_len);
    p += strlen(p);
    for (size_t i = 0; i < data_len; ++i)
    {
        if (p >= endp)
        {
            static const char ellipses[] = "...";
            char *s = endp - sizeof(ellipses);
            if (s >= p)
            {
                strcpy(s, ellipses);
            }
            break;
        }
        snprintf(p, endp - p, " %02X", *q++);
        p += strlen(p);
    }
    return out;
}

Michael Cook's avatar
Michael Cook committed
1536
__attribute__((unused))
Andrew Burger's avatar
Andrew Burger committed
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
static void print_rx_ind(nfapi_rx_indication_t *p)
{
  printf("Printing RX_IND fields\n");
  printf("header.message_id: %u\n", p->header.message_id);
  printf("header.phy_id: %u\n", p->header.phy_id);
  printf("header.message_id: %u\n", p->header.message_id);
  printf("header.m_segment_sequence: %u\n", p->header.m_segment_sequence);
  printf("header.checksum: %u\n", p->header.checksum);
  printf("header.transmit_timestamp: %u\n", p->header.transmit_timestamp);
  printf("sfn_sf: %u\n", p->sfn_sf);
  printf("rx_indication_body.tl.tag: 0x%x\n", p->rx_indication_body.tl.tag);
  printf("rx_indication_body.tl.length: %u\n", p->rx_indication_body.tl.length);
  printf("rx_indication_body.number_of_pdus: %u\n", p->rx_indication_body.number_of_pdus);

  nfapi_rx_indication_pdu_t *pdu = p->rx_indication_body.rx_pdu_list;
  for (int i = 0; i < p->rx_indication_body.number_of_pdus; i++)
  {
    printf("pdu %d nfapi_rx_ue_information.tl.tag: 0x%x\n", i, pdu->rx_ue_information.tl.tag);
    printf("pdu %d nfapi_rx_ue_information.tl.length: %u\n", i, pdu->rx_ue_information.tl.length);
    printf("pdu %d nfapi_rx_ue_information.handle: %u\n", i, pdu->rx_ue_information.handle);
    printf("pdu %d nfapi_rx_ue_information.rnti: %u\n", i, pdu->rx_ue_information.rnti);
    printf("pdu %d nfapi_rx_indication_rel8.tl.tag: 0x%x\n", i, pdu->rx_indication_rel8.tl.tag);
    printf("pdu %d nfapi_rx_indication_rel8.tl.length: %u\n", i, pdu->rx_indication_rel8.tl.length);
    printf("pdu %d nfapi_rx_indication_rel8.length: %u\n", i, pdu->rx_indication_rel8.length);
    printf("pdu %d nfapi_rx_indication_rel8.offset: %u\n", i, pdu->rx_indication_rel8.offset);
    printf("pdu %d nfapi_rx_indication_rel8.ul_cqi: %u\n", i, pdu->rx_indication_rel8.ul_cqi);
    printf("pdu %d nfapi_rx_indication_rel8.timing_advance: %u\n", i, pdu->rx_indication_rel8.timing_advance);
    printf("pdu %d nfapi_rx_indication_rel9.tl.tag: 0x%x\n", i, pdu->rx_indication_rel9.tl.tag);
    printf("pdu %d nfapi_rx_indication_rel9.tl.length: %u\n", i, pdu->rx_indication_rel9.tl.length);
    printf("pdu %d nfapi_rx_indication_rel9.timing_advance_r9: %u\n", i, pdu->rx_indication_rel9.timing_advance_r9);
  }

  fflush(stdout);
}

1572
  void send_standalone_msg(UL_IND_t *UL, nfapi_message_id_e msg_type)
Andrew Burger's avatar
Andrew Burger committed
1573
  {
1574 1575
    int encoded_size = -1;
    char buffer[1024];
Andrew Burger's avatar
Andrew Burger committed
1576

1577 1578 1579 1580
    switch (msg_type)
    {
    case NFAPI_RACH_INDICATION:
      encoded_size = nfapi_p7_message_pack(&UL->rach_ind, buffer, sizeof(buffer), NULL);
1581
      LOG_A(MAC, "RACH_IND sent to Proxy, Size: %d Frame %d Subframe %d\n", encoded_size,
1582
            NFAPI_SFNSF2SFN(UL->rach_ind.sfn_sf), NFAPI_SFNSF2SF(UL->rach_ind.sfn_sf));
1583 1584 1585
      break;
    case NFAPI_CRC_INDICATION:
      encoded_size = nfapi_p7_message_pack(&UL->crc_ind, buffer, sizeof(buffer), NULL);
Andrew Burger's avatar
Andrew Burger committed
1586 1587 1588
      LOG_I(MAC, "CRC_IND sent to Proxy, Size: %d Frame %d Subframe %d num_crcs: %u\n", encoded_size,
            NFAPI_SFNSF2SFN(UL->crc_ind.sfn_sf), NFAPI_SFNSF2SF(UL->crc_ind.sfn_sf),
            UL->crc_ind.crc_indication_body.number_of_crcs);
1589
      break;
Andrew Burger's avatar
Andrew Burger committed
1590
    case NFAPI_RX_ULSCH_INDICATION:
1591
      encoded_size = nfapi_p7_message_pack(&UL->rx_ind, buffer, sizeof(buffer), NULL);
Andrew Burger's avatar
Andrew Burger committed
1592
      LOG_I(MAC, "RX_IND sent to Proxy, Size: %d Frame %d Subframe %d rx_ind.tl.length: %u num_pdus: %u\n",
1593
            encoded_size, NFAPI_SFNSF2SFN(UL->rx_ind.sfn_sf), NFAPI_SFNSF2SF(UL->rx_ind.sfn_sf),
Andrew Burger's avatar
Andrew Burger committed
1594
            UL->rx_ind.rx_indication_body.tl.length, UL->rx_ind.rx_indication_body.number_of_pdus);
1595
      break;
Andrew Burger's avatar
Andrew Burger committed
1596
    case NFAPI_RX_CQI_INDICATION:
Andrew Burger's avatar
Andrew Burger committed
1597
      encoded_size = nfapi_p7_message_pack(&UL->cqi_ind, buffer, sizeof(buffer), NULL);
Andrew Burger's avatar
Andrew Burger committed
1598 1599
      LOG_I(MAC, "CQI_IND sent to Proxy, Size: %d num_cqis: %u\n", encoded_size,
            UL->cqi_ind.cqi_indication_body.number_of_cqis);
1600 1601 1602
      break;
    case NFAPI_HARQ_INDICATION:
      encoded_size = nfapi_p7_message_pack(&UL->harq_ind, buffer, sizeof(buffer), NULL);
Andrew Burger's avatar
Andrew Burger committed
1603
      LOG_I(MAC, "HARQ_IND sent to Proxy, Size: %d Frame %d Subframe %d\n", encoded_size,
Andrew Burger's avatar
Andrew Burger committed
1604
            NFAPI_SFNSF2SFN(UL->harq_ind.sfn_sf), NFAPI_SFNSF2SF(UL->harq_ind.sfn_sf));
1605
      break;
Andrew Burger's avatar
Andrew Burger committed
1606
    case NFAPI_RX_SR_INDICATION:
1607
      encoded_size = nfapi_p7_message_pack(&UL->sr_ind, buffer, sizeof(buffer), NULL);
Andrew Burger's avatar
Andrew Burger committed
1608
      LOG_I(MAC, "SR_IND sent to Proxy, Size: %d\n", encoded_size);
1609 1610
      break;
    default:
Andrew Burger's avatar
Andrew Burger committed
1611
      LOG_I(MAC, "%s Unknown Message msg_type :: %u\n", __func__, msg_type);
1612 1613 1614 1615 1616 1617 1618
      return;
    }
    if (encoded_size < 0)
    {
      LOG_E(MAC, "standalone pack failed\n");
      return;
    }
Andrew Burger's avatar
Andrew Burger committed
1619
    if (send(ue_tx_sock_descriptor, buffer, encoded_size, 0) < 0)
1620 1621 1622 1623
    {
      LOG_E(MAC, "Send Proxy UE failed\n");
      return;
    }
Andrew Burger's avatar
Andrew Burger committed
1624
  }
1625 1626

  void send_standalone_dummy()
1627
  {
1628
    static const uint16_t dummy[] = {0, 0};
Andrew Burger's avatar
Andrew Burger committed
1629
    if (send(ue_tx_sock_descriptor, dummy, sizeof(dummy), 0) < 0)
1630 1631 1632 1633
    {
      LOG_E(MAC, "send dummy to OAI UE failed: %s\n", strerror(errno));
      return;
    }
1634
  }
1635

Andrew Burger's avatar
Andrew Burger committed
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
const char *dl_pdu_type_to_string(uint8_t pdu_type)
{
  switch (pdu_type)
  {
    case NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE:
      return "DCI_DL_PDU_TYPE";
    case NFAPI_DL_CONFIG_BCH_PDU_TYPE:
      return "BCH_PDU_TYPE";
    case NFAPI_DL_CONFIG_MCH_PDU_TYPE:
      return "MCH_PDU_TYPE";
    case NFAPI_DL_CONFIG_DLSCH_PDU_TYPE:
      return "DLSCH_PDU_TYPE";
    case NFAPI_DL_CONFIG_PCH_PDU_TYPE:
      return "PCH_PDU_TYPE";
    case NFAPI_DL_CONFIG_PRS_PDU_TYPE:
      return "PRS_PDU_TYPE";
    case NFAPI_DL_CONFIG_CSI_RS_PDU_TYPE:
      return "CSI_RS_PDU_TYPE";
    case NFAPI_DL_CONFIG_EPDCCH_DL_PDU_TYPE:
      return "EPDCCH_DL_PDU_TYPE";
    case NFAPI_DL_CONFIG_MPDCCH_PDU_TYPE:
      return "MPDCCH_PDU_TYPE";
    case NFAPI_DL_CONFIG_NBCH_PDU_TYPE:
      return "NBCH_PDU_TYPE";
    case NFAPI_DL_CONFIG_NPDCCH_PDU_TYPE:
      return "NPDCCH_PDU_TYPE";
    case NFAPI_DL_CONFIG_NDLSCH_PDU_TYPE:
      return "NDLSCH_PDU_TYPE";
    default:
      LOG_E(MAC, "%s No corresponding PDU for type: %u\n", __func__, pdu_type);
      return "UNKNOWN";
  }
}

const char *ul_pdu_type_to_string(uint8_t pdu_type)
{
  switch (pdu_type)
  {
    case NFAPI_UL_CONFIG_ULSCH_PDU_TYPE:
      return "UL_CONFIG_ULSCH_PDU_TYPE";
    case NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE:
      return "UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE";
    case NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE:
      return "UL_CONFIG_ULSCH_HARQ_PDU_TYPE";
    case NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE:
      return "UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE";
    case NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE:
      return "UL_CONFIG_UCI_CQI_PDU_TYPE";
    case NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE:
      return "UCI_SR_PDU_TYPE";
    case NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE:
      return "UCI_HARQ_PDU_TYPE";
    case NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE:
      return "UCI_SR_HARQ_PDU_TYPE";
    case NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE:
      return "UCI_CQI_HARQ_PDU_TYPE";
    case NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE:
      return "UCI_CQI_SR_PDU_TYPE";
    case NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE:
      return "UCI_CQI_SR_HARQ_PDU_TYPE";
    case NFAPI_UL_CONFIG_SRS_PDU_TYPE:
      return "SRS_PDU_TYPE";
    case NFAPI_UL_CONFIG_HARQ_BUFFER_PDU_TYPE:
      return "HARQ_BUFFER_PDU_TYPE";
    case NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE:
      return "PDU_TYPE";
    case NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE:
      return "ULSCH_UCI_HARQ_PDU_TYPE";
    case NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE:
      return "ULSCH_CSI_UCI_HARQ_PDU_TYPE";
    case NFAPI_UL_CONFIG_NULSCH_PDU_TYPE:
      return "NULSCH_PDU_TYPE";
    case NFAPI_UL_CONFIG_NRACH_PDU_TYPE:
      return "NRACH_PDU_TYPE";
    default:
      LOG_E(MAC, "%s No corresponding PDU for type: %u\n", __func__, pdu_type);
      return "UNKNOWN";
  }
}

char *nfapi_dl_config_req_to_string(nfapi_dl_config_request_t *req)
{
    const size_t max_result = 1024;
    uint16_t num_pdus = req->dl_config_request_body.number_pdu;
    int subframe = req->sfn_sf & 15;
    int frame = req->sfn_sf >> 4;
    char *result = malloc(max_result);
    snprintf(result, max_result, "num_pdus=%u Frame=%d Subframe=%d",
        num_pdus, frame, subframe);
    for (size_t i = 0; i < num_pdus; ++i)
    {
        int len = strlen(result);
        if (len >= max_result - 1)
        {
            break;
        }
        snprintf(result + len, max_result - len, " pdu_type=%s",
            dl_pdu_type_to_string(req->dl_config_request_body.dl_config_pdu_list[i].pdu_type));
    }
    return result;
}

char *nfapi_ul_config_req_to_string(nfapi_ul_config_request_t *req)
{
    const size_t max_result = 1024;
    uint16_t num_pdus = req->ul_config_request_body.number_of_pdus;
    int subframe = req->sfn_sf & 15;
    int frame = req->sfn_sf >> 4;
    char *result = malloc(max_result);
    snprintf(result, max_result, "num_pdus=%u Frame=%d Subframe=%d",
        num_pdus, frame, subframe);
    for (size_t i = 0; i < num_pdus; ++i)
    {
        int len = strlen(result);
        if (len >= max_result - 1)
        {
            break;
        }
        snprintf(result + len, max_result - len, " pdu_type=%s",
            ul_pdu_type_to_string(req->ul_config_request_body.ul_config_pdu_list[i].pdu_type));
    }
    return result;
}

/* Dummy functions*/
1761 1762 1763 1764 1765 1766 1767

  void handle_nfapi_hi_dci0_dci_pdu(
      PHY_VARS_eNB * eNB,
      int frame,
      int subframe,
      L1_rxtx_proc_t *proc,
      nfapi_hi_dci0_request_pdu_t *hi_dci0_config_pdu)
1768
  {
1769 1770
  }

1771 1772 1773 1774 1775 1776
  void handle_nfapi_hi_dci0_hi_pdu(
      PHY_VARS_eNB * eNB,
      int frame,
      int subframe,
      L1_rxtx_proc_t *proc,
      nfapi_hi_dci0_request_pdu_t *hi_dci0_config_pdu)
1777
  {
1778
  }
1779

1780 1781 1782 1783 1784 1785 1786
  void handle_nfapi_dci_dl_pdu(PHY_VARS_eNB * eNB,
                               int frame,
                               int subframe,
                               L1_rxtx_proc_t *proc,
                               nfapi_dl_config_request_pdu_t *dl_config_pdu)
  {
  }
1787

1788 1789 1790 1791 1792 1793
  void handle_nfapi_bch_pdu(PHY_VARS_eNB * eNB,
                            L1_rxtx_proc_t * proc,
                            nfapi_dl_config_request_pdu_t * dl_config_pdu,
                            uint8_t * sdu)
  {
  }
1794

1795 1796 1797 1798 1799 1800 1801 1802 1803
  void handle_nfapi_dlsch_pdu(PHY_VARS_eNB * eNB,
                              int frame,
                              int subframe,
                              L1_rxtx_proc_t *proc,
                              nfapi_dl_config_request_pdu_t *dl_config_pdu,
                              uint8_t codeword_index,
                              uint8_t *sdu)
  {
  }
1804

1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
  void handle_nfapi_ul_pdu(PHY_VARS_eNB * eNB,
                           L1_rxtx_proc_t * proc,
                           nfapi_ul_config_request_pdu_t * ul_config_pdu,
                           uint16_t frame,
                           uint8_t subframe,
                           uint8_t srs_present)
  {
  }
  void handle_nfapi_mch_pdu(PHY_VARS_eNB * eNB,
                            L1_rxtx_proc_t * proc,
                            nfapi_dl_config_request_pdu_t * dl_config_pdu,
                            uint8_t * sdu)
  {
  }
1819

1820 1821 1822
  void phy_config_request(PHY_Config_t * phy_config)
  {
  }
1823

1824 1825 1826
  void phy_config_update_sib2_request(PHY_Config_t * phy_config)
  {
  }
1827

1828 1829 1830
  void phy_config_update_sib13_request(PHY_Config_t * phy_config)
  {
  }
1831

1832 1833 1834 1835
  uint32_t from_earfcn(int eutra_bandP, uint32_t dl_earfcn)
  {
    return (0);
  }
1836

1837 1838 1839 1840
  int32_t get_uldl_offset(int eutra_bandP)
  {
    return (0);
  }
1841

1842 1843 1844 1845
  int l1_north_init_eNB(void)
  {
    return 0;
  }
1846

1847 1848 1849
  void init_eNB_afterRU(void)
  {
  }