/*
 * 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
 */

/*
                                enb_config.h
                             -------------------
  AUTHOR  : Lionel GAUTHIER, Navid Nikaein, Laurent Winckel
  COMPANY : EURECOM
  EMAIL   : Lionel.Gauthier@eurecom.fr, navid.nikaein@eurecom.fr
*/

#ifndef ENB_CONFIG_H_
#define ENB_CONFIG_H_
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <libconfig.h>

#include "commonDef.h"
#include "platform_types.h"
#include "platform_constants.h"
#include "PHY/impl_defs_lte.h"
#include "PHY/defs_eNB.h"
#include "s1ap_messages_types.h"
#include "f1ap_messages_types.h"
#include "LTE_SystemInformationBlockType2.h"
#include "rrc_messages_types.h"
#include "RRC/LTE/rrc_defs.h"
#include <intertask_interface.h>
#include "enb_paramdef.h"


#define IPV4_STR_ADDR_TO_INT_NWBO(AdDr_StR,NwBo,MeSsAgE ) do {\
            struct in_addr inp;\
            if ( inet_aton(AdDr_StR, &inp ) < 0 ) {\
                AssertFatal (0, MeSsAgE);\
            } else {\
                NwBo = inp.s_addr;\
            }\
        } while (0);

/** @defgroup _enb_app ENB APP 
 * @ingroup _oai2
 * @{
 */

// Hard to find a defined value for max enb...
#define MAX_ENB 16

#define MAX_DU	4
#define CU_BALANCING_ALL		127
#define CU_BALANCING_ROUND_ROBIN	126

typedef struct mme_ip_address_s {
  unsigned  ipv4:1;
  unsigned  ipv6:1;
  unsigned  active:1;
  char     *ipv4_address;
  char     *ipv6_address;
} mme_ip_address_t;



typedef struct cu_params {
  const char    *local_ipv4_address;
  const uint16_t local_port;
  const char    *remote_ipv4_address;
  const int16_t  remote_port;
} cudu_params_t;

typedef struct ru_config_s {
  // indicates if local or remote rf is used (1 == LOCAL)
  unsigned  local_rf:1;
  // indicates if UDP socket is used
  unsigned  udp:1;
  // indicates if RAW socket is used
  unsigned  raw:1;
  char      *ru_if_name;
  char     *local_address;
  char     *remote_address;
  uint16_t  local_port;
  uint16_t  remote_port;
  uint8_t   udpif4p5;
  uint8_t   rawif4p5;
  uint8_t   rawif5_mobipass;
  uint8_t   if_compress;
} ru_config_t;

extern void RCconfig_RU(void);
extern void RCconfig_flexran(void);
extern void RCconfig_L1(void);
extern void RCconfig_macrlc(int macrlc_has_f1[MAX_MAC_INST]);
extern void UE_config_stub_pnf(void);
extern int  RCconfig_gtpu(void );
extern void RCConfig(void);

  
  /////////////////////////////////////////// NB-IoT ///////////////////////////////////////////////////
  //
  long              rach_raResponseWindowSize_NB[1+MAX_NUM_CCs];
  long              rach_macContentionResolutionTimer_NB[1+MAX_NUM_CCs];
  long              rach_powerRampingStep_NB[1+MAX_NUM_CCs];
  long              rach_preambleInitialReceivedTargetPower_NB[1+MAX_NUM_CCs];
  long              rach_preambleTransMax_CE_NB[1+MAX_NUM_CCs];
  long              bcch_modificationPeriodCoeff_NB[1+MAX_NUM_CCs];
  long              pcch_defaultPagingCycle_NB[1+MAX_NUM_CCs];
  long              nprach_CP_Length[1+MAX_NUM_CCs];
  long              nprach_rsrp_range[1+MAX_NUM_CCs];
  long             nprach_SubcarrierMSG3_RangeStart[1+MAX_NUM_CCs];
  long              maxNumPreambleAttemptCE_NB[1+MAX_NUM_CCs];
  long              npdsch_nrs_Power[1+MAX_NUM_CCs];
  long              npusch_ack_nack_numRepetitions_NB[1+MAX_NUM_CCs];
  long              npusch_srs_SubframeConfig_NB[1+MAX_NUM_CCs];
  long              npusch_threeTone_CyclicShift_r13[1+MAX_NUM_CCs];
  long              npusch_sixTone_CyclicShift_r13[1+MAX_NUM_CCs];
  BOOLEAN_t         npusch_groupHoppingEnabled[1+MAX_NUM_CCs];
  long              npusch_groupAssignmentNPUSCH_r13[1+MAX_NUM_CCs];
  long              dl_GapThreshold_NB[1+MAX_NUM_CCs];
  long              dl_GapPeriodicity_NB[1+MAX_NUM_CCs];
  long       dl_GapDurationCoeff_NB[1+MAX_NUM_CCs];
  long              npusch_p0_NominalNPUSCH[1+MAX_NUM_CCs];
  long              npusch_alpha[1+MAX_NUM_CCs];
  long              deltaPreambleMsg3[1+MAX_NUM_CCs];

  long              ue_TimersAndConstants_t300_NB[1+MAX_NUM_CCs];
  long              ue_TimersAndConstants_t301_NB[1+MAX_NUM_CCs];
  long              ue_TimersAndConstants_t310_NB[1+MAX_NUM_CCs];
  long              ue_TimersAndConstants_t311_NB[1+MAX_NUM_CCs];
  long              ue_TimersAndConstants_n310_NB[1+MAX_NUM_CCs];
  long              ue_TimersAndConstants_n311_NB[1+MAX_NUM_CCs];

  long              nprach_Periodicity[1+MAX_NUM_CCs];
  long              nprach_StartTime[1+MAX_NUM_CCs];
  long              nprach_SubcarrierOffset[1+MAX_NUM_CCs];
  long              nprach_NumSubcarriers[1+MAX_NUM_CCs];
  long              numRepetitionsPerPreambleAttempt[1+MAX_NUM_CCs];
  long              npdcch_NumRepetitions_RA[1+MAX_NUM_CCs];
  long              npdcch_StartSF_CSS_RA[1+MAX_NUM_CCs];
  long             npdcch_Offset_RA[1+MAX_NUM_CCs];

  /////////////////////////////////////////////END/////////////////////////////////////////////
  
void                          enb_config_display(void);
void                          ru_config_display(void);

int RCconfig_RRC(uint32_t i, eNB_RRC_INST *rrc, int macrlc_has_f1);
int RCconfig_S1(MessageDef *msg_p, uint32_t i);

void read_config_and_init(void);
int RCconfig_X2(MessageDef *msg_p, uint32_t i);

void fill_SL_configuration(MessageDef *msg_p,  ccparams_sidelink_t *SLconfig,int cell_idx,int cc_idx,char *config_fname);
void fill_eMTC_configuration(MessageDef *msg_p,  ccparams_eMTC_t *eMTCconfig, int cell_idx,int cc_idx,char *config_fname,char *brparamspath);

int RCconfig_DU_F1(MessageDef *msg_p, uint32_t i);
void handle_f1ap_setup_resp(f1ap_setup_resp_t *resp);

#endif