From fa9eaee498fda7184df4ee0f01f48c21a4c960e7 Mon Sep 17 00:00:00 2001 From: Ismael Gomez Date: Fri, 23 Jun 2017 19:15:30 +0200 Subject: [PATCH] new NAS/RRC structure. Attaching and ping working --- lib/include/srslte/common/bcd_helpers.h | 7 + lib/src/phy/rf/rf_uhd_imp.c | 4 +- lib/src/radio/radio.cc | 4 +- srsue/hdr/phy/phch_recv.h | 36 +- srsue/hdr/ue.h | 25 +- srsue/hdr/upper/nas.h | 3 + srsue/hdr/upper/rrc.h | 3 +- srsue/src/mac/mac.cc | 2 +- srsue/src/main.cc | 5 +- srsue/src/phy/phch_recv.cc | 213 +- srsue/src/ue.cc | 12 +- srsue/src/upper/nas.cc | 948 ++++---- srsue/src/upper/rrc.cc | 2696 +++++++++++------------ 13 files changed, 1997 insertions(+), 1961 deletions(-) diff --git a/lib/include/srslte/common/bcd_helpers.h b/lib/include/srslte/common/bcd_helpers.h index d44a66483..55411ae33 100644 --- a/lib/include/srslte/common/bcd_helpers.h +++ b/lib/include/srslte/common/bcd_helpers.h @@ -30,6 +30,7 @@ #include #include #include +#include namespace srslte { @@ -112,6 +113,12 @@ inline bool mnc_to_string(uint16_t mnc, std::string *str) *str += (mnc & 0x000F) + '0'; return true; } +inline std::string plmn_id_to_c_str(LIBLTE_RRC_PLMN_IDENTITY_STRUCT plmn_id) { + std::string mcc_str, mnc_str; + mnc_to_string(plmn_id.mnc, &mnc_str); + mcc_to_string(plmn_id.mcc, &mcc_str); + return mcc_str + mnc_str; +} } // namespace srslte diff --git a/lib/src/phy/rf/rf_uhd_imp.c b/lib/src/phy/rf/rf_uhd_imp.c index 319093994..5b80df0ec 100644 --- a/lib/src/phy/rf/rf_uhd_imp.c +++ b/lib/src/phy/rf/rf_uhd_imp.c @@ -623,7 +623,9 @@ int rf_uhd_recv_with_time_multi(void *h, log_overflow(handler); } else if (error_code == UHD_RX_METADATA_ERROR_CODE_LATE_COMMAND) { log_late(handler); - } else if (error_code != UHD_RX_METADATA_ERROR_CODE_NONE) { + } else if (error_code == UHD_RX_METADATA_ERROR_CODE_TIMEOUT) { + fprintf(stderr, "Error timed out while receiving asynchronoous messages from UHD.\n"); + } else if (error_code != UHD_RX_METADATA_ERROR_CODE_NONE ) { fprintf(stderr, "Error code 0x%x was returned during streaming. Aborting.\n", error_code); } diff --git a/lib/src/radio/radio.cc b/lib/src/radio/radio.cc index bc74660b0..66f1a10f8 100644 --- a/lib/src/radio/radio.cc +++ b/lib/src/radio/radio.cc @@ -47,7 +47,7 @@ bool radio::init(char *args, char *devname) cur_tx_srate = 0; is_start_of_burst = true; - // Suppress radio stdout + // Suppress radio stdout srslte_rf_suppress_stdout(&rf_device); tx_adv_auto = true; @@ -389,8 +389,6 @@ void radio::set_tx_srate(float srate) // Calculate TX advance in seconds from samples and sampling rate tx_adv_sec = nsamples/cur_tx_srate; - - printf("Setting TX/RX offset %d samples, %.2f us\n", nsamples, tx_adv_sec*1e6); } void radio::start_rx() diff --git a/srsue/hdr/phy/phch_recv.h b/srsue/hdr/phy/phch_recv.h index 59fa10ebc..0bfae26c9 100644 --- a/srsue/hdr/phy/phch_recv.h +++ b/srsue/hdr/phy/phch_recv.h @@ -74,13 +74,12 @@ private: void set_ue_sync_opts(srslte_ue_sync_t *q); void run_thread(); - int sync_sfn(); - + bool running; - srslte::radio_multi *radio_h; + srslte::radio_multi *radio_h; mac_interface_phy *mac; - rrc_interface_phy *rrc; + rrc_interface_phy *rrc; srslte::log *log_h; srslte::thread_pool *workers_pool; phch_common *worker_com; @@ -91,16 +90,20 @@ private: uint32_t nof_rx_antennas; - cf_t *sf_buffer_sfn[SRSLTE_MAX_PORTS]; + cf_t *sf_buffer[SRSLTE_MAX_PORTS]; // Sync metrics sync_metrics_t metrics; enum { - IDLE, CELL_SEARCH, CELL_SELECT, CAMPING - } phy_state; + IDLE, CELL_SEARCH, CELL_MEASURE, CELL_SELECT, CELL_CAMP + } phy_state; + + enum { + SRATE_NONE=0, SRATE_FIND, SRATE_CAMP + } srate_mode; - srslte_cell_t cell; + srslte_cell_t cell; bool cell_is_set; bool is_sfn_synched; bool started; @@ -118,10 +121,19 @@ private: const static uint32_t SYNC_SFN_TIMEOUT = 5000; float ul_dl_factor; int cur_earfcn_index; - - bool cell_search(int force_N_id_2 = -1); - bool init_cell(); - void free_cell(); + bool cell_search_in_progress; + uint32_t measure_cnt; + float measure_rsrp; + srslte_ue_dl_t ue_dl_measure; + + + const static int RSRP_MEASURE_NOF_FRAMES = 20; + + int cell_sync_sfn(); + int cell_meas_rsrp(); + bool cell_search(int force_N_id_2 = -1); + bool init_cell(); + void free_cell(); }; } // namespace srsue diff --git a/srsue/hdr/ue.h b/srsue/hdr/ue.h index 6fb593d55..60aff8469 100644 --- a/srsue/hdr/ue.h +++ b/srsue/hdr/ue.h @@ -61,15 +61,16 @@ namespace srsue { *******************************************************************************/ typedef struct { + uint32_t dl_earfcn; float dl_freq; float ul_freq; float rx_gain; float tx_gain; - uint32_t nof_rx_ant; - std::string device_name; - std::string device_args; - std::string time_adv_nsamples; - std::string burst_preamble; + uint32_t nof_rx_ant; + std::string device_name; + std::string device_args; + std::string time_adv_nsamples; + std::string burst_preamble; }rf_args_t; typedef struct { @@ -110,16 +111,16 @@ typedef struct { }gui_args_t; typedef struct { - phy_args_t phy; + phy_args_t phy; float metrics_period_secs; bool pregenerate_signals; int ue_cateogry; - + }expert_args_t; typedef struct { rf_args_t rf; - rf_cal_t rf_cal; + rf_cal_t rf_cal; pcap_args_t pcap; trace_args_t trace; log_args_t log; @@ -144,7 +145,7 @@ public: void stop(); bool is_attached(); void start_plot(); - + static void rf_msg(srslte_rf_error_t error); void handle_rf_msg(srslte_rf_error_t error); @@ -152,10 +153,10 @@ public: bool get_metrics(ue_metrics_t &m); void pregenerate_signals(bool enable); - + // Testing - void test_con_restablishment(); - + void test_con_restablishment(); + private: static ue *instance; diff --git a/srsue/hdr/upper/nas.h b/srsue/hdr/upper/nas.h index e21bff432..edf5dcf27 100644 --- a/srsue/hdr/upper/nas.h +++ b/srsue/hdr/upper/nas.h @@ -107,6 +107,9 @@ namespace srsue { plmn_selection_state_t plmn_selection; LIBLTE_RRC_PLMN_IDENTITY_STRUCT current_plmn; + LIBLTE_RRC_PLMN_IDENTITY_STRUCT home_plmn; + + std::vector known_plmns; // Save short MAC diff --git a/srsue/hdr/upper/rrc.h b/srsue/hdr/upper/rrc.h index f33043d10..225659d6f 100644 --- a/srsue/hdr/upper/rrc.h +++ b/srsue/hdr/upper/rrc.h @@ -61,8 +61,7 @@ namespace srsue { typedef enum { SI_ACQUIRE_IDLE = 0, SI_ACQUIRE_SIB1, - SI_ACQUIRE_SIB2, - SI_ACQUIRE_DONE + SI_ACQUIRE_SIB2 } si_acquire_state_t; diff --git a/srsue/src/mac/mac.cc b/srsue/src/mac/mac.cc index 316320f31..5a74492e6 100644 --- a/srsue/src/mac/mac.cc +++ b/srsue/src/mac/mac.cc @@ -141,7 +141,7 @@ void mac::run_thread() { while(started) { - while (!phy_h->sync_status()) { + while (!phy_h->sync_status() && started) { usleep(5000); if (phy_h->sync_status()) { Debug("Setting ttysync to %d\n", phy_h->get_current_tti()); diff --git a/srsue/src/main.cc b/srsue/src/main.cc index f114c0606..965e6a918 100644 --- a/srsue/src/main.cc +++ b/srsue/src/main.cc @@ -61,8 +61,9 @@ void parse_args(all_args_t *args, int argc, char *argv[]) { // Command line or config file options bpo::options_description common("Configuration options"); common.add_options() - ("rf.dl_freq", bpo::value(&args->rf.dl_freq)->default_value(2680000000), "Downlink centre frequency") - ("rf.ul_freq", bpo::value(&args->rf.ul_freq)->default_value(2560000000), "Uplink centre frequency") + ("rf.dl_earfcn", bpo::value(&args->rf.dl_earfcn)->default_value(3400), "Downlink EARFCN") + ("rf.dl_freq", bpo::value(&args->rf.dl_freq)->default_value(2680000000), "(optional) Downlink centre frequency") + ("rf.ul_freq", bpo::value(&args->rf.ul_freq)->default_value(2560000000), "(optional) Uplink centre frequency") ("rf.rx_gain", bpo::value(&args->rf.rx_gain)->default_value(-1), "Front-end receiver gain") ("rf.tx_gain", bpo::value(&args->rf.tx_gain)->default_value(-1), "Front-end transmitter gain") ("rf.nof_rx_ant", bpo::value(&args->rf.nof_rx_ant)->default_value(1), "Number of RX antennas") diff --git a/srsue/src/phy/phch_recv.cc b/srsue/src/phy/phch_recv.cc index 7895ca66a..3dedcb606 100644 --- a/srsue/src/phy/phch_recv.cc +++ b/srsue/src/phy/phch_recv.cc @@ -25,6 +25,7 @@ */ #include +#include #include "srslte/srslte.h" #include "srslte/common/log.h" #include "phy/phch_worker.h" @@ -62,9 +63,11 @@ namespace srsue { time_adv_sec = 0; cell_is_set = false; sync_sfn_cnt = 0; + srate_mode = SRATE_NONE; + cell_search_in_progress = false; for (uint32_t i = 0; i < nof_rx_antennas; i++) { - sf_buffer_sfn[i] = (cf_t *) srslte_vec_malloc(sizeof(cf_t) * 3 * SRSLTE_SF_LEN_PRB(100)); + sf_buffer[i] = (cf_t *) srslte_vec_malloc(sizeof(cf_t) * 3 * SRSLTE_SF_LEN_PRB(100)); } nof_tx_mutex = MUTEX_X_WORKER * workers_pool->get_nof_workers(); @@ -82,8 +85,8 @@ namespace srsue { running = false; wait_thread_finish(); for (uint32_t i = 0; i < nof_rx_antennas; i++) { - if (sf_buffer_sfn[i]) { - free(sf_buffer_sfn[i]); + if (sf_buffer[i]) { + free(sf_buffer[i]); } } } @@ -152,9 +155,14 @@ namespace srsue { // Set options defined in expert section set_ue_sync_opts(&ue_sync); + if (srslte_ue_dl_init_multi(&ue_dl_measure, cell, nof_rx_antennas)) { + Error("Setting cell: initiating ue_dl_measure\n"); + return false; + } + for (uint32_t i = 0; i < workers_pool->get_nof_workers(); i++) { if (!((phch_worker *) workers_pool->get_worker(i))->init_cell(cell)) { - Error("Error setting cell: initiating PHCH worker\n"); + Error("Setting cell: initiating PHCH worker\n"); return false; } } @@ -179,6 +187,10 @@ namespace srsue { usleep(2000); } + srslte_ue_sync_free(&ue_sync); + + srslte_ue_dl_free(&ue_dl_measure); + if (cell_is_set) { for (uint32_t i = 0; i < workers_pool->get_nof_workers(); i++) { ((phch_worker *) workers_pool->get_worker(i))->free_cell(); @@ -198,7 +210,6 @@ namespace srsue { bzero(found_cells, 3 * sizeof(srslte_ue_cellsearch_result_t)); - log_h->console("Searching for cell...\n"); if (srslte_ue_cellsearch_init_multi(&cs, SRSLTE_DEFAULT_MAX_FRAMES_PSS, radio_recv_wrapper_cs, nof_rx_antennas, radio_h)) { Error("Initiating UE cell search\n"); @@ -214,7 +225,10 @@ namespace srsue { srslte_ue_sync_start_agc(&cs.ue_sync, callback_set_rx_gain, last_gain); } - radio_h->set_rx_srate(1.92e6); + if (srate_mode != SRATE_FIND) { + srate_mode = SRATE_FIND; + radio_h->set_rx_srate(1.92e6); + } radio_h->start_rx(); /* Find a cell in the given N_id_2 or go through the 3 of them to find the strongest */ @@ -246,9 +260,6 @@ namespace srsue { cell.cp = found_cells[max_peak_cell].cp; cellsearch_cfo = found_cells[max_peak_cell].cfo; - log_h->console("Found CELL ID: %d CP: %s, CFO: %.1f KHz.\nTrying to decode MIB...\n", - cell.id, srslte_cp_string(cell.cp), cellsearch_cfo / 1000); - srslte_ue_mib_sync_t ue_mib_sync; if (srslte_ue_mib_sync_init_multi(&ue_mib_sync, cell.id, cell.cp, radio_recv_wrapper_cs, nof_rx_antennas, @@ -281,10 +292,6 @@ namespace srsue { if (ret == 1) { srslte_pbch_mib_unpack(bch_payload, &cell, NULL); worker_com->set_cell(cell); - srslte_cell_fprint(stdout, &cell, 0); - - srslte_bit_pack_vector(bch_payload, bch_payload_bits, SRSLTE_BCH_PAYLOAD_LEN); - mac->bch_decoded_ok(bch_payload_bits, SRSLTE_BCH_PAYLOAD_LEN / 8); return true; } else { Warning("Error decoding MIB: Error decoding PBCH\n"); @@ -293,45 +300,6 @@ namespace srsue { } - int phch_recv::sync_sfn(void) { - - int ret = SRSLTE_ERROR; - uint8_t bch_payload[SRSLTE_BCH_PAYLOAD_LEN]; - - srslte_ue_sync_decode_sss_on_track(&ue_sync, true); - ret = srslte_ue_sync_zerocopy_multi(&ue_sync, sf_buffer_sfn); - if (ret < 0) { - Error("Error calling ue_sync_get_buffer"); - return -1; - } - - if (ret == 1) { - if (srslte_ue_sync_get_sfidx(&ue_sync) == 0) { - int sfn_offset = 0; - Info("SYNC: Decoding MIB...\n"); - int n = srslte_ue_mib_decode(&ue_mib, sf_buffer_sfn[0], bch_payload, NULL, &sfn_offset); - if (n < 0) { - Error("Error decoding MIB while synchronising SFN"); - return -1; - } else if (n == SRSLTE_UE_MIB_FOUND) { - uint32_t sfn; - srslte_pbch_mib_unpack(bch_payload, &cell, &sfn); - - sfn = (sfn+sfn_offset)%1024; - tti = sfn * 10; - - srslte_ue_sync_decode_sss_on_track(&ue_sync, true); - Info("SYNC: DONE, TTI=%d, sfn_offset=%d\n", tti, sfn_offset); - srslte_ue_mib_reset(&ue_mib); - return 1; - } - } - } else { - Debug("SYNC: PSS/SSS not found...\n"); - } - return 0; - } - void phch_recv::resync_sfn() { sync_sfn_cnt = 0; phy_state = CELL_SELECT; @@ -342,17 +310,17 @@ namespace srsue { } void phch_recv::cell_search_next() { + cell_search_in_progress = true; cur_earfcn_index++; if (cur_earfcn_index >= 0) { - if ((uint32_t) cur_earfcn_index >= earfcn.size() - 1) { + if (cur_earfcn_index >= (int) earfcn.size() - 1) { cur_earfcn_index = 0; } // If PHY is running, stop and free resources free_cell(); - float dl_freq = srslte_band_fd(earfcn[cur_earfcn_index]); + float dl_freq = 1e6*srslte_band_fd(earfcn[cur_earfcn_index]); if (dl_freq >= 0) { - log_h->console("Cell Search: Set DL EARFCN=%d, frequency=%.1f MHz\n", earfcn[cur_earfcn_index], dl_freq / 1e6); log_h->info("Cell Search: Set DL EARFCN=%d, frequency=%.1f MHz, channel_index=%d\n", earfcn[cur_earfcn_index], dl_freq / 1e6, cur_earfcn_index); radio_h->set_rx_freq(dl_freq); @@ -366,25 +334,39 @@ namespace srsue { } void phch_recv::cell_search_start() { - cur_earfcn_index = -1; - log_h->console("Cell Search: Starting procedure...\n"); - log_h->info("Cell Search: Starting procedure...\n"); - cell_search_next(); + if (earfcn.size() > 0) { + cur_earfcn_index = -1; + log_h->console("Starting Cell Search procedure in %d EARFCNs...\n", earfcn.size()); + log_h->info("Cell Search: Starting procedure...\n"); + cell_search_next(); + } else { + log_h->info("Empty EARFCN list. Stopping cell search...\n"); + log_h->console("Empty EARFCN list. Stopping cell search...\n"); + } } bool phch_recv::cell_select(uint32_t earfcn, srslte_cell_t cell) { free_cell(); - float dl_freq = srslte_band_fd(earfcn); - float ul_freq = srslte_band_ul_earfcn(earfcn); + int cnt=0; + while(phy_state == CELL_SEARCH && cnt<100) { + usleep(10000); + log_h->info("PHY in CELL_SEARCH. Waiting...\n"); + } + if (phy_state==CELL_SEARCH) { + log_h->warning("PHY still in CELL_SEARCH, forcing CELL_SELECT...\n"); + } + float dl_freq = 1e6*srslte_band_fd(earfcn); + float ul_freq = 1e6*srslte_band_fu(srslte_band_ul_earfcn(earfcn)); if (dl_freq >= 0 || ul_freq <= 0) { - log_h->console("Cell Select: Set EARFCN=%d, DL frequency=%.1f MHz, UL frequency=%.1f MHz\n", earfcn, - dl_freq / 1e6, ul_freq / 1e6); log_h->info("Cell Select: Set EARFCN=%d, frequency=%.1f MHz, UL frequency=%.1f MHz\n", earfcn, dl_freq / 1e6, ul_freq / 1e6); radio_h->set_rx_freq(dl_freq); radio_h->set_tx_freq(ul_freq); + ul_dl_factor = ul_freq/dl_freq; + + cell_search_in_progress = false; this->cell = cell; if (init_cell()) { phy_state = CELL_SELECT; @@ -398,6 +380,77 @@ namespace srsue { return false; } + + int phch_recv::cell_sync_sfn(void) { + + int ret = SRSLTE_ERROR; + uint8_t bch_payload[SRSLTE_BCH_PAYLOAD_LEN]; + + srslte_ue_sync_decode_sss_on_track(&ue_sync, true); + ret = srslte_ue_sync_zerocopy_multi(&ue_sync, sf_buffer); + if (ret < 0) { + Error("Error calling ue_sync_get_buffer"); + return -1; + } + + if (ret == 1) { + if (srslte_ue_sync_get_sfidx(&ue_sync) == 0) { + int sfn_offset = 0; + Info("SYNC: Decoding MIB...\n"); + int n = srslte_ue_mib_decode(&ue_mib, sf_buffer[0], bch_payload, NULL, &sfn_offset); + if (n < 0) { + Error("SYNC: Error decoding MIB while synchronising SFN"); + return -1; + } else if (n == SRSLTE_UE_MIB_FOUND) { + uint32_t sfn; + srslte_pbch_mib_unpack(bch_payload, &cell, &sfn); + + sfn = (sfn+sfn_offset)%1024; + tti = sfn * 10; + + srslte_ue_sync_decode_sss_on_track(&ue_sync, true); + Info("SYNC: DONE, TTI=%d, sfn_offset=%d\n", tti, sfn_offset); + srslte_ue_mib_reset(&ue_mib); + return 1; + } + } + } else { + Debug("SYNC: PSS/SSS not found...\n"); + } + return 0; + } + + int phch_recv::cell_meas_rsrp() { + + uint32_t cfi = 0; + + tti = (tti+1) % 10240; + log_h->step(tti); + + uint32_t sf_idx = tti%10; + + int sync_res = srslte_ue_sync_zerocopy_multi(&ue_sync, sf_buffer); + if (sync_res == 1) { + if (srslte_ue_dl_decode_fft_estimate_multi(&ue_dl_measure, sf_buffer, sf_idx, &cfi)) { + log_h->error("SYNC: Measuring RSRP: Estimating channel\n"); + return -1; + } + float rsrp = srslte_chest_dl_get_rsrp(&ue_dl_measure.chest); + measure_rsrp = SRSLTE_VEC_CMA(rsrp, measure_rsrp, measure_cnt); + measure_cnt++; + log_h->info("SYNC: Measuring RSRP %d/%d, sf_idx=%d, RSRP=%.1f dBm\n", + measure_cnt, RSRP_MEASURE_NOF_FRAMES, sf_idx, 10 * log10(rsrp / 1000)); + if (measure_cnt >= RSRP_MEASURE_NOF_FRAMES) { + return 1; + } + } else { + log_h->error("SYNC: Measuring RSRP: Sync error\n"); + return -1; + } + + return 0; + } + void phch_recv::run_thread() { int sync_res; phch_worker *worker = NULL; @@ -406,7 +459,6 @@ namespace srsue { switch (phy_state) { case CELL_SEARCH: if (cell_search()) { - log_h->console("Initializating cell configuration...\n"); init_cell(); float srate = (float) srslte_sampling_freq_hz(cell.nof_prb); @@ -416,12 +468,11 @@ namespace srsue { radio_h->set_master_clock_rate(23.04e6); } - log_h->console("Setting Sampling frequency %.2f MHz\n", (float) srate / 1000000); + log_h->info("Setting Sampling frequency %.2f MHz\n", (float) srate / 1000000); + srate_mode = SRATE_CAMP; radio_h->set_rx_srate(srate); radio_h->set_tx_srate(srate); - ul_dl_factor = radio_h->get_tx_freq() / radio_h->get_rx_freq(); - Info("SYNC: Cell found. Synchronizing...\n"); phy_state = CELL_SELECT; sync_sfn_cnt = 0; @@ -438,14 +489,21 @@ namespace srsue { radio_is_streaming = true; } - switch (sync_sfn()) { + switch (cell_sync_sfn()) { default: log_h->console("Going IDLE\n"); phy_state = IDLE; break; case 1: srslte_ue_sync_set_agc_period(&ue_sync, 20); - phy_state = CAMPING; + if (!cell_search_in_progress) { + phy_state = CELL_CAMP; + log_h->console("Sync OK. Camping on cell PCI=%d...\n", cell.id); + } else { + measure_cnt = 0; + measure_rsrp = 0; + phy_state = CELL_MEASURE; + } break; case 0: break; @@ -459,10 +517,21 @@ namespace srsue { log_h->warning("Timeout while synchronizing SFN\n"); } break; - case CAMPING: + case CELL_MEASURE: + switch(cell_meas_rsrp()) { + case 1: + rrc->cell_found(earfcn[cur_earfcn_index], cell, 10*log10(measure_rsrp/1000)); + phy_state = CELL_CAMP; + case 0: + break; + default: + log_h->error("SYNC: Getting RSRP cell measurement.\n"); + cell_search_next(); + } + break; + case CELL_CAMP: tti = (tti+1) % 10240; worker = (phch_worker *) workers_pool->wait_worker(tti); - sync_res = 0; if (worker) { for (uint32_t i = 0; i < nof_rx_antennas; i++) { buffer[i] = worker->get_buffer(i); @@ -535,7 +604,7 @@ namespace srsue { } bool phch_recv::status_is_sync() { - return phy_state == CAMPING; + return phy_state == CELL_CAMP; } void phch_recv::get_current_cell(srslte_cell_t *cell_) { diff --git a/srsue/src/ue.cc b/srsue/src/ue.cc index ac98aeea1..2fd38c3c8 100644 --- a/srsue/src/ue.cc +++ b/srsue/src/ue.cc @@ -33,6 +33,7 @@ #include #include #include +#include using namespace srslte; @@ -74,7 +75,7 @@ ue::~ue() bool ue::init(all_args_t *args_) { args = args_; - + logger.init(args->log.filename); rf_log.init("RF ", &logger); phy_log.init("PHY ", &logger, true); @@ -169,6 +170,7 @@ bool ue::init(all_args_t *args_) } if (args->rf.tx_gain > 0) { radio.set_tx_gain(args->rf.tx_gain); + printf("set tx gain %f\n", args->rf.tx_gain); } else { radio.set_tx_gain(args->rf.rx_gain); std::cout << std::endl << @@ -189,6 +191,14 @@ bool ue::init(all_args_t *args_) gw.init(&pdcp, &rrc, this, &gw_log); usim.init(&args->usim, &usim_log); + // Currently EARFCN list is set to only one frequency as indicated in ue.conf + std::vector earfcn_list; + earfcn_list.push_back(args->rf.dl_earfcn); + phy.set_earfcn(earfcn_list); + + printf("\n\nRequesting NAS Attach...\n"); + nas.attach_request(); + started = true; return true; } diff --git a/srsue/src/upper/nas.cc b/srsue/src/upper/nas.cc index be03ec840..f9bc88064 100644 --- a/srsue/src/upper/nas.cc +++ b/srsue/src/upper/nas.cc @@ -25,7 +25,9 @@ */ +#include "srslte/asn1/liblte_rrc.h" #include "upper/nas.h" +#include "srslte/common/bcd_helpers.h" using namespace srslte; @@ -45,11 +47,9 @@ namespace srsue { gw = gw_; nas_log = nas_log_; state = EMM_STATE_DEREGISTERED; - - // Manual PLMN selection procedure - current_plmn.mcc = 1; - current_plmn.mnc = 1; - plmn_selection = PLMN_SELECTED; + plmn_selection = PLMN_NOT_SELECTED; + home_plmn.mcc = 61441; // This is 001 + home_plmn.mnc = 65281; // This is 01 } void nas::stop() {} @@ -62,11 +62,14 @@ namespace srsue { UE interface *******************************************************************************/ void nas::attach_request() { + nas_log->info("Attach Request\n"); if (state == EMM_STATE_DEREGISTERED) { state = EMM_STATE_REGISTERED_INITIATED; if (plmn_selection == PLMN_NOT_SELECTED) { + nas_log->info("Starting PLMN Search...\n"); rrc->plmn_search(); } else if (plmn_selection == PLMN_SELECTED) { + nas_log->info("Selecting PLMN %s\n", plmn_id_to_c_str(current_plmn).c_str()); rrc->plmn_select(current_plmn); } } else { @@ -83,239 +86,227 @@ namespace srsue { RRC interface *******************************************************************************/ -void nas::plmn_found(LIBLTE_RRC_PLMN_IDENTITY_STRUCT plmn_id, uint16_t tracking_area_code) { - // if it's the plmn we want rrc->plmn_select() and plmn_selection = PLMN_SELECTED -} + void nas::plmn_found(LIBLTE_RRC_PLMN_IDENTITY_STRUCT plmn_id, uint16_t tracking_area_code) { + + // Store PLMN if not registered + for (uint32_t i=0;iinfo("Detected known PLMN %s\n", plmn_id_to_c_str(plmn_id).c_str()); + if (plmn_id.mcc == home_plmn.mcc && plmn_id.mnc == home_plmn.mnc) { + rrc->plmn_select(plmn_id); + } + return; + } + } + nas_log->info("Found PLMN: Id=%s, TAC=%d\n", plmn_id_to_c_str(plmn_id).c_str(), + tracking_area_code); + nas_log->console("Found PLMN: Id=%s, TAC=%d\n", plmn_id_to_c_str(plmn_id).c_str(), + tracking_area_code); + if (plmn_id.mcc == home_plmn.mcc && plmn_id.mnc == home_plmn.mnc) { + rrc->plmn_select(plmn_id); + } + } + + void nas::cell_selected() { + if (state == EMM_STATE_REGISTERED_INITIATED) { + rrc->connect(); + } else { + nas_log->info("Cell selected in invalid state = %s\n", emm_state_text[state]); + } + } -void nas::cell_selected() { - if (state == EMM_STATE_REGISTERED_INITIATED) { - rrc->connect(); - } else { - nas_log->info("Cell selcted in invalid state = %s\n", emm_state_text[state]); + bool nas::is_attached() { + return state == EMM_STATE_REGISTERED; } -} - -bool nas::is_attached() -{ - return state == EMM_STATE_REGISTERED; -} - -void nas::notify_connection_setup() -{ - nas_log->debug("State = %s\n", emm_state_text[state]); - if(EMM_STATE_REGISTERED_INITIATED == state) { - send_attach_request(); - } else { - send_service_request(); + + void nas::notify_connection_setup() { + nas_log->debug("State = %s\n", emm_state_text[state]); + if (EMM_STATE_REGISTERED_INITIATED == state) { + send_attach_request(); + } else { + send_service_request(); + } } -} - -void nas::write_pdu(uint32_t lcid, byte_buffer_t *pdu) -{ - uint8 pd; - uint8 msg_type; - - nas_log->info_hex(pdu->msg, pdu->N_bytes, "DL %s PDU", rb_id_text[lcid]); - - // Parse the message - liblte_mme_parse_msg_header((LIBLTE_BYTE_MSG_STRUCT*)pdu, &pd, &msg_type); - switch(msg_type) - { - case LIBLTE_MME_MSG_TYPE_ATTACH_ACCEPT: - parse_attach_accept(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_ATTACH_REJECT: - parse_attach_reject(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_AUTHENTICATION_REQUEST: - parse_authentication_request(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_AUTHENTICATION_REJECT: - parse_authentication_reject(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_IDENTITY_REQUEST: - parse_identity_request(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_SECURITY_MODE_COMMAND: - parse_security_mode_command(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_SERVICE_REJECT: - parse_service_reject(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_ESM_INFORMATION_REQUEST: - parse_esm_information_request(lcid, pdu); - break; - case LIBLTE_MME_MSG_TYPE_EMM_INFORMATION: - parse_emm_information(lcid, pdu); - break; - default: - nas_log->error("Not handling NAS message with MSG_TYPE=%02X\n",msg_type); - pool->deallocate(pdu); - break; + + void nas::write_pdu(uint32_t lcid, byte_buffer_t *pdu) { + uint8 pd; + uint8 msg_type; + + nas_log->info_hex(pdu->msg, pdu->N_bytes, "DL %s PDU", rb_id_text[lcid]); + + // Parse the message + liblte_mme_parse_msg_header((LIBLTE_BYTE_MSG_STRUCT *) pdu, &pd, &msg_type); + switch (msg_type) { + case LIBLTE_MME_MSG_TYPE_ATTACH_ACCEPT: + parse_attach_accept(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_ATTACH_REJECT: + parse_attach_reject(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_AUTHENTICATION_REQUEST: + parse_authentication_request(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_AUTHENTICATION_REJECT: + parse_authentication_reject(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_IDENTITY_REQUEST: + parse_identity_request(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_SECURITY_MODE_COMMAND: + parse_security_mode_command(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_SERVICE_REJECT: + parse_service_reject(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_ESM_INFORMATION_REQUEST: + parse_esm_information_request(lcid, pdu); + break; + case LIBLTE_MME_MSG_TYPE_EMM_INFORMATION: + parse_emm_information(lcid, pdu); + break; + default: + nas_log->error("Not handling NAS message with MSG_TYPE=%02X\n", msg_type); + pool->deallocate(pdu); + break; + } + } + + uint32_t nas::get_ul_count() { + return count_ul; } -} - -uint32_t nas::get_ul_count() -{ - return count_ul; -} - -bool nas::get_s_tmsi(LIBLTE_RRC_S_TMSI_STRUCT *s_tmsi) -{ - if(is_guti_set) { - s_tmsi->mmec = guti.mme_code; - s_tmsi->m_tmsi = guti.m_tmsi; - return true; - } else { - return false; + + bool nas::get_s_tmsi(LIBLTE_RRC_S_TMSI_STRUCT *s_tmsi) { + if (is_guti_set) { + s_tmsi->mmec = guti.mme_code; + s_tmsi->m_tmsi = guti.m_tmsi; + return true; + } else { + return false; + } } -} /******************************************************************************* Security *******************************************************************************/ -void nas::integrity_generate(uint8_t *key_128, - uint32_t count, - uint8_t rb_id, - uint8_t direction, - uint8_t *msg, - uint32_t msg_len, - uint8_t *mac) -{ - switch(integ_algo) - { - case INTEGRITY_ALGORITHM_ID_EIA0: - break; - case INTEGRITY_ALGORITHM_ID_128_EIA1: - security_128_eia1(key_128, - count, - rb_id, - direction, - msg, - msg_len, - mac); - break; - case INTEGRITY_ALGORITHM_ID_128_EIA2: - security_128_eia2(key_128, - count, - rb_id, - direction, - msg, - msg_len, - mac); - break; - default: - break; + void nas::integrity_generate(uint8_t *key_128, + uint32_t count, + uint8_t rb_id, + uint8_t direction, + uint8_t *msg, + uint32_t msg_len, + uint8_t *mac) { + switch (integ_algo) { + case INTEGRITY_ALGORITHM_ID_EIA0: + break; + case INTEGRITY_ALGORITHM_ID_128_EIA1: + security_128_eia1(key_128, + count, + rb_id, + direction, + msg, + msg_len, + mac); + break; + case INTEGRITY_ALGORITHM_ID_128_EIA2: + security_128_eia2(key_128, + count, + rb_id, + direction, + msg, + msg_len, + mac); + break; + default: + break; + } } -} - -void nas::integrity_check() -{ -} + void nas::integrity_check() { -void nas::cipher_encrypt() -{ + } -} + void nas::cipher_encrypt() { -void nas::cipher_decrypt() -{ + } -} + void nas::cipher_decrypt() { + } /******************************************************************************* Parsers *******************************************************************************/ -void nas::parse_attach_accept(uint32_t lcid, byte_buffer_t *pdu) -{ - LIBLTE_MME_ATTACH_ACCEPT_MSG_STRUCT attach_accept; - LIBLTE_MME_ACTIVATE_DEFAULT_EPS_BEARER_CONTEXT_REQUEST_MSG_STRUCT act_def_eps_bearer_context_req; - LIBLTE_MME_ATTACH_COMPLETE_MSG_STRUCT attach_complete; - LIBLTE_MME_ACTIVATE_DEFAULT_EPS_BEARER_CONTEXT_ACCEPT_MSG_STRUCT act_def_eps_bearer_context_accept; - - nas_log->info("Received Attach Accept\n"); - count_dl++; - - liblte_mme_unpack_attach_accept_msg((LIBLTE_BYTE_MSG_STRUCT*)pdu, &attach_accept); - - if(attach_accept.eps_attach_result == LIBLTE_MME_EPS_ATTACH_RESULT_EPS_ONLY) - { - //FIXME: Handle t3412.unit - //FIXME: Handle tai_list - if(attach_accept.guti_present) - { - memcpy(&guti, &attach_accept.guti.guti, sizeof(LIBLTE_MME_EPS_MOBILE_ID_GUTI_STRUCT)); - is_guti_set = true; - // TODO: log message to console - } - if(attach_accept.lai_present) - { - } - if(attach_accept.ms_id_present) - {} - if(attach_accept.emm_cause_present) - {} - if(attach_accept.t3402_present) - {} - if(attach_accept.t3423_present) - {} - if(attach_accept.equivalent_plmns_present) - {} - if(attach_accept.emerg_num_list_present) - {} - if(attach_accept.eps_network_feature_support_present) - {} - if(attach_accept.additional_update_result_present) - {} - if(attach_accept.t3412_ext_present) - {} - - liblte_mme_unpack_activate_default_eps_bearer_context_request_msg(&attach_accept.esm_msg, &act_def_eps_bearer_context_req); - - if(LIBLTE_MME_PDN_TYPE_IPV4 == act_def_eps_bearer_context_req.pdn_addr.pdn_type) - { - ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[0] << 24; - ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[1] << 16; - ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[2] << 8; - ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[3]; - - nas_log->info("IP allocated by network %u.%u.%u.%u\n", - act_def_eps_bearer_context_req.pdn_addr.addr[0], - act_def_eps_bearer_context_req.pdn_addr.addr[1], - act_def_eps_bearer_context_req.pdn_addr.addr[2], - act_def_eps_bearer_context_req.pdn_addr.addr[3]); - - nas_log->console("Network attach successful. IP: %u.%u.%u.%u\n", - act_def_eps_bearer_context_req.pdn_addr.addr[0], - act_def_eps_bearer_context_req.pdn_addr.addr[1], - act_def_eps_bearer_context_req.pdn_addr.addr[2], - act_def_eps_bearer_context_req.pdn_addr.addr[3]); - - // Setup GW - char *err_str = NULL; - if(gw->setup_if_addr(ip_addr, err_str)) - { - nas_log->error("Failed to set gateway address - %s\n", err_str); + void nas::parse_attach_accept(uint32_t lcid, byte_buffer_t *pdu) { + LIBLTE_MME_ATTACH_ACCEPT_MSG_STRUCT attach_accept; + LIBLTE_MME_ACTIVATE_DEFAULT_EPS_BEARER_CONTEXT_REQUEST_MSG_STRUCT act_def_eps_bearer_context_req; + LIBLTE_MME_ATTACH_COMPLETE_MSG_STRUCT attach_complete; + LIBLTE_MME_ACTIVATE_DEFAULT_EPS_BEARER_CONTEXT_ACCEPT_MSG_STRUCT act_def_eps_bearer_context_accept; + + nas_log->info("Received Attach Accept\n"); + count_dl++; + + liblte_mme_unpack_attach_accept_msg((LIBLTE_BYTE_MSG_STRUCT *) pdu, &attach_accept); + + if (attach_accept.eps_attach_result == LIBLTE_MME_EPS_ATTACH_RESULT_EPS_ONLY) { + //FIXME: Handle t3412.unit + //FIXME: Handle tai_list + if (attach_accept.guti_present) { + memcpy(&guti, &attach_accept.guti.guti, sizeof(LIBLTE_MME_EPS_MOBILE_ID_GUTI_STRUCT)); + is_guti_set = true; + // TODO: log message to console } - } - else - { - nas_log->error("Not handling IPV6 or IPV4V6\n"); - pool->deallocate(pdu); - return; - } - eps_bearer_id = act_def_eps_bearer_context_req.eps_bearer_id; - if(act_def_eps_bearer_context_req.transaction_id_present) - { + if (attach_accept.lai_present) { + } + if (attach_accept.ms_id_present) {} + if (attach_accept.emm_cause_present) {} + if (attach_accept.t3402_present) {} + if (attach_accept.t3423_present) {} + if (attach_accept.equivalent_plmns_present) {} + if (attach_accept.emerg_num_list_present) {} + if (attach_accept.eps_network_feature_support_present) {} + if (attach_accept.additional_update_result_present) {} + if (attach_accept.t3412_ext_present) {} + + liblte_mme_unpack_activate_default_eps_bearer_context_request_msg(&attach_accept.esm_msg, + &act_def_eps_bearer_context_req); + + if (LIBLTE_MME_PDN_TYPE_IPV4 == act_def_eps_bearer_context_req.pdn_addr.pdn_type) { + ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[0] << 24; + ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[1] << 16; + ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[2] << 8; + ip_addr |= act_def_eps_bearer_context_req.pdn_addr.addr[3]; + + nas_log->info("IP allocated by network %u.%u.%u.%u\n", + act_def_eps_bearer_context_req.pdn_addr.addr[0], + act_def_eps_bearer_context_req.pdn_addr.addr[1], + act_def_eps_bearer_context_req.pdn_addr.addr[2], + act_def_eps_bearer_context_req.pdn_addr.addr[3]); + + nas_log->console("Network attach successful. IP: %u.%u.%u.%u\n", + act_def_eps_bearer_context_req.pdn_addr.addr[0], + act_def_eps_bearer_context_req.pdn_addr.addr[1], + act_def_eps_bearer_context_req.pdn_addr.addr[2], + act_def_eps_bearer_context_req.pdn_addr.addr[3]); + + // Setup GW + char *err_str = NULL; + if (gw->setup_if_addr(ip_addr, err_str)) { + nas_log->error("Failed to set gateway address - %s\n", err_str); + } + } else { + nas_log->error("Not handling IPV6 or IPV4V6\n"); + pool->deallocate(pdu); + return; + } + eps_bearer_id = act_def_eps_bearer_context_req.eps_bearer_id; + if (act_def_eps_bearer_context_req.transaction_id_present) { transaction_id = act_def_eps_bearer_context_req.proc_transaction_id; - } + } - //FIXME: Handle the following parameters + //FIXME: Handle the following parameters // act_def_eps_bearer_context_req.eps_qos.qci // act_def_eps_bearer_context_req.eps_qos.br_present // act_def_eps_bearer_context_req.eps_qos.br_ext_present @@ -328,336 +319,315 @@ void nas::parse_attach_accept(uint32_t lcid, byte_buffer_t *pdu) // act_def_eps_bearer_context_req.protocol_cnfg_opts_present // act_def_eps_bearer_context_req.connectivity_type_present - // FIXME: Setup the default EPS bearer context - - state = EMM_STATE_REGISTERED; + // FIXME: Setup the default EPS bearer context + + state = EMM_STATE_REGISTERED; + + // Send EPS bearer context accept and attach complete + count_ul++; + act_def_eps_bearer_context_accept.eps_bearer_id = eps_bearer_id; + act_def_eps_bearer_context_accept.proc_transaction_id = transaction_id; + act_def_eps_bearer_context_accept.protocol_cnfg_opts_present = false; + liblte_mme_pack_activate_default_eps_bearer_context_accept_msg(&act_def_eps_bearer_context_accept, + &attach_complete.esm_msg); + liblte_mme_pack_attach_complete_msg(&attach_complete, + LIBLTE_MME_SECURITY_HDR_TYPE_INTEGRITY_AND_CIPHERED, + count_ul, + (LIBLTE_BYTE_MSG_STRUCT *) pdu); + integrity_generate(&k_nas_int[16], + count_ul, + lcid - 1, + SECURITY_DIRECTION_UPLINK, + &pdu->msg[5], + pdu->N_bytes - 5, + &pdu->msg[1]); - // Send EPS bearer context accept and attach complete - count_ul++; - act_def_eps_bearer_context_accept.eps_bearer_id = eps_bearer_id; - act_def_eps_bearer_context_accept.proc_transaction_id = transaction_id; - act_def_eps_bearer_context_accept.protocol_cnfg_opts_present = false; - liblte_mme_pack_activate_default_eps_bearer_context_accept_msg(&act_def_eps_bearer_context_accept, &attach_complete.esm_msg); - liblte_mme_pack_attach_complete_msg(&attach_complete, - LIBLTE_MME_SECURITY_HDR_TYPE_INTEGRITY_AND_CIPHERED, - count_ul, - (LIBLTE_BYTE_MSG_STRUCT*)pdu); - integrity_generate(&k_nas_int[16], - count_ul, - lcid-1, - SECURITY_DIRECTION_UPLINK, - &pdu->msg[5], - pdu->N_bytes-5, - &pdu->msg[1]); + // Instruct RRC to enable capabilities + rrc->enable_capabilities(); - // Instruct RRC to enable capabilities - rrc->enable_capabilities(); + nas_log->info("Sending Attach Complete\n"); + rrc->write_sdu(lcid, pdu); - nas_log->info("Sending Attach Complete\n"); - rrc->write_sdu(lcid, pdu); - + } else { + nas_log->info("Not handling attach type %u\n", attach_accept.eps_attach_result); + state = EMM_STATE_DEREGISTERED; + pool->deallocate(pdu); + } } - else - { - nas_log->info("Not handling attach type %u\n", attach_accept.eps_attach_result); + + void nas::parse_attach_reject(uint32_t lcid, byte_buffer_t *pdu) { + LIBLTE_MME_ATTACH_REJECT_MSG_STRUCT attach_rej; + + liblte_mme_unpack_attach_reject_msg((LIBLTE_BYTE_MSG_STRUCT *) pdu, &attach_rej); + nas_log->warning("Received Attach Reject. Cause= %02X\n", attach_rej.emm_cause); + nas_log->console("Received Attach Reject. Cause= %02X\n", attach_rej.emm_cause); state = EMM_STATE_DEREGISTERED; pool->deallocate(pdu); + // FIXME: Command RRC to release? } -} - -void nas::parse_attach_reject(uint32_t lcid, byte_buffer_t *pdu) -{ - LIBLTE_MME_ATTACH_REJECT_MSG_STRUCT attach_rej; - - liblte_mme_unpack_attach_reject_msg((LIBLTE_BYTE_MSG_STRUCT*)pdu, &attach_rej); - nas_log->warning("Received Attach Reject. Cause= %02X\n", attach_rej.emm_cause); - nas_log->console("Received Attach Reject. Cause= %02X\n", attach_rej.emm_cause); - state = EMM_STATE_DEREGISTERED; - pool->deallocate(pdu); - // FIXME: Command RRC to release? -} - -void nas::parse_authentication_request(uint32_t lcid, byte_buffer_t *pdu) -{ - LIBLTE_MME_AUTHENTICATION_REQUEST_MSG_STRUCT auth_req; - LIBLTE_MME_AUTHENTICATION_RESPONSE_MSG_STRUCT auth_res; - - nas_log->info("Received Authentication Request\n");; - liblte_mme_unpack_authentication_request_msg((LIBLTE_BYTE_MSG_STRUCT*)pdu, &auth_req); - - // Reuse the pdu for the response message - pdu->reset(); - - // Generate authentication response using RAND, AUTN & KSI-ASME - uint16 mcc, mnc; - mcc = rrc->get_mcc(); - mnc = rrc->get_mnc(); - - nas_log->info("MCC=%d, MNC=%d\n", mcc, mnc); - - bool net_valid; - uint8_t res[16]; - usim->generate_authentication_response(auth_req.rand, auth_req.autn, mcc, mnc, &net_valid, res); - - if(net_valid) - { - nas_log->info("Network authentication successful\n"); - for(int i=0; i<8; i++) - { - auth_res.res[i] = res[i]; - } - liblte_mme_pack_authentication_response_msg(&auth_res, (LIBLTE_BYTE_MSG_STRUCT*)pdu); - nas_log->info("Sending Authentication Response\n"); - rrc->write_sdu(lcid, pdu); + void nas::parse_authentication_request(uint32_t lcid, byte_buffer_t *pdu) { + LIBLTE_MME_AUTHENTICATION_REQUEST_MSG_STRUCT auth_req; + LIBLTE_MME_AUTHENTICATION_RESPONSE_MSG_STRUCT auth_res; + + nas_log->info("Received Authentication Request\n");; + liblte_mme_unpack_authentication_request_msg((LIBLTE_BYTE_MSG_STRUCT *) pdu, &auth_req); + + // Reuse the pdu for the response message + pdu->reset(); + + // Generate authentication response using RAND, AUTN & KSI-ASME + uint16 mcc, mnc; + mcc = rrc->get_mcc(); + mnc = rrc->get_mnc(); + + nas_log->info("MCC=%d, MNC=%d\n", mcc, mnc); + + bool net_valid; + uint8_t res[16]; + usim->generate_authentication_response(auth_req.rand, auth_req.autn, mcc, mnc, &net_valid, res); + + if (net_valid) { + nas_log->info("Network authentication successful\n"); + for (int i = 0; i < 8; i++) { + auth_res.res[i] = res[i]; + } + liblte_mme_pack_authentication_response_msg(&auth_res, (LIBLTE_BYTE_MSG_STRUCT *) pdu); + + nas_log->info("Sending Authentication Response\n"); + rrc->write_sdu(lcid, pdu); + } else { + nas_log->warning("Network authentication failure\n"); + nas_log->console("Warning: Network authentication failure\n"); + pool->deallocate(pdu); + } } - else - { - nas_log->warning("Network authentication failure\n"); - nas_log->console("Warning: Network authentication failure\n"); + + void nas::parse_authentication_reject(uint32_t lcid, byte_buffer_t *pdu) { + nas_log->warning("Received Authentication Reject\n"); pool->deallocate(pdu); + state = EMM_STATE_DEREGISTERED; + // FIXME: Command RRC to release? } -} - -void nas::parse_authentication_reject(uint32_t lcid, byte_buffer_t *pdu) -{ - nas_log->warning("Received Authentication Reject\n"); - pool->deallocate(pdu); - state = EMM_STATE_DEREGISTERED; - // FIXME: Command RRC to release? -} - -void nas::parse_identity_request(uint32_t lcid, byte_buffer_t *pdu) -{ - nas_log->error("TODO:parse_identity_request\n"); -} - -void nas::parse_security_mode_command(uint32_t lcid, byte_buffer_t *pdu) -{ - bool success; - LIBLTE_MME_SECURITY_MODE_COMMAND_MSG_STRUCT sec_mode_cmd; - LIBLTE_MME_SECURITY_MODE_COMPLETE_MSG_STRUCT sec_mode_comp; - LIBLTE_MME_SECURITY_MODE_REJECT_MSG_STRUCT sec_mode_rej; - - nas_log->info("Received Security Mode Command\n"); - liblte_mme_unpack_security_mode_command_msg((LIBLTE_BYTE_MSG_STRUCT*)pdu, &sec_mode_cmd); - - ksi = sec_mode_cmd.nas_ksi.nas_ksi; - cipher_algo = (CIPHERING_ALGORITHM_ID_ENUM)sec_mode_cmd.selected_nas_sec_algs.type_of_eea; - integ_algo = (INTEGRITY_ALGORITHM_ID_ENUM)sec_mode_cmd.selected_nas_sec_algs.type_of_eia; - // FIXME: Handle nonce_ue, nonce_mme - // FIXME: Currently only handling ciphering EEA0 (null) and integrity EIA1,EIA2 - // FIXME: Use selected_nas_sec_algs to choose correct algos - - nas_log->debug("Security details: ksi=%d, eea=%s, eia=%s\n", - ksi, ciphering_algorithm_id_text[cipher_algo], integrity_algorithm_id_text[integ_algo]); - - - if(CIPHERING_ALGORITHM_ID_EEA0 != cipher_algo || - (INTEGRITY_ALGORITHM_ID_128_EIA2 != integ_algo && - INTEGRITY_ALGORITHM_ID_128_EIA1 != integ_algo) || - sec_mode_cmd.nas_ksi.tsc_flag != LIBLTE_MME_TYPE_OF_SECURITY_CONTEXT_FLAG_NATIVE) - { - sec_mode_rej.emm_cause = LIBLTE_MME_EMM_CAUSE_UE_SECURITY_CAPABILITIES_MISMATCH; - nas_log->warning("Sending Security Mode Reject due to security capabilities mismatch\n"); - success = false; + + void nas::parse_identity_request(uint32_t lcid, byte_buffer_t *pdu) { + nas_log->error("TODO:parse_identity_request\n"); } - else - { - // Generate NAS encryption key and integrity protection key - usim->generate_nas_keys(k_nas_enc, k_nas_int, cipher_algo, integ_algo); - nas_log->debug_hex(k_nas_enc, 32, "NAS encryption key - k_nas_enc"); - nas_log->debug_hex(k_nas_int, 32, "NAS integrity key - k_nas_int"); - - // Check incoming MAC - uint8_t *inMAC = &pdu->msg[1]; - uint8_t genMAC[4]; - integrity_generate(&k_nas_int[16], - count_dl, - lcid-1, - SECURITY_DIRECTION_DOWNLINK, - &pdu->msg[5], - pdu->N_bytes-5, - genMAC); - - nas_log->info_hex(inMAC, 4, "Incoming PDU MAC:"); - nas_log->info_hex(genMAC, 4, "Generated PDU MAC:"); - - bool match=true; - for(int i=0;i<4;i++) { - if(inMAC[i] != genMAC[i]) { - match = false; - } - } - if(!match) { - sec_mode_rej.emm_cause = LIBLTE_MME_EMM_CAUSE_SECURITY_MODE_REJECTED_UNSPECIFIED; - nas_log->warning("Sending Security Mode Reject due to integrity check failure\n"); + + void nas::parse_security_mode_command(uint32_t lcid, byte_buffer_t *pdu) { + bool success; + LIBLTE_MME_SECURITY_MODE_COMMAND_MSG_STRUCT sec_mode_cmd; + LIBLTE_MME_SECURITY_MODE_COMPLETE_MSG_STRUCT sec_mode_comp; + LIBLTE_MME_SECURITY_MODE_REJECT_MSG_STRUCT sec_mode_rej; + + nas_log->info("Received Security Mode Command\n"); + liblte_mme_unpack_security_mode_command_msg((LIBLTE_BYTE_MSG_STRUCT *) pdu, &sec_mode_cmd); + + ksi = sec_mode_cmd.nas_ksi.nas_ksi; + cipher_algo = (CIPHERING_ALGORITHM_ID_ENUM) sec_mode_cmd.selected_nas_sec_algs.type_of_eea; + integ_algo = (INTEGRITY_ALGORITHM_ID_ENUM) sec_mode_cmd.selected_nas_sec_algs.type_of_eia; + // FIXME: Handle nonce_ue, nonce_mme + // FIXME: Currently only handling ciphering EEA0 (null) and integrity EIA1,EIA2 + // FIXME: Use selected_nas_sec_algs to choose correct algos + + nas_log->debug("Security details: ksi=%d, eea=%s, eia=%s\n", + ksi, ciphering_algorithm_id_text[cipher_algo], integrity_algorithm_id_text[integ_algo]); + + + if (CIPHERING_ALGORITHM_ID_EEA0 != cipher_algo || + (INTEGRITY_ALGORITHM_ID_128_EIA2 != integ_algo && + INTEGRITY_ALGORITHM_ID_128_EIA1 != integ_algo) || + sec_mode_cmd.nas_ksi.tsc_flag != LIBLTE_MME_TYPE_OF_SECURITY_CONTEXT_FLAG_NATIVE) { + sec_mode_rej.emm_cause = LIBLTE_MME_EMM_CAUSE_UE_SECURITY_CAPABILITIES_MISMATCH; + nas_log->warning("Sending Security Mode Reject due to security capabilities mismatch\n"); success = false; } else { + // Generate NAS encryption key and integrity protection key + usim->generate_nas_keys(k_nas_enc, k_nas_int, cipher_algo, integ_algo); + nas_log->debug_hex(k_nas_enc, 32, "NAS encryption key - k_nas_enc"); + nas_log->debug_hex(k_nas_int, 32, "NAS integrity key - k_nas_int"); + + // Check incoming MAC + uint8_t *inMAC = &pdu->msg[1]; + uint8_t genMAC[4]; + integrity_generate(&k_nas_int[16], + count_dl, + lcid - 1, + SECURITY_DIRECTION_DOWNLINK, + &pdu->msg[5], + pdu->N_bytes - 5, + genMAC); + + nas_log->info_hex(inMAC, 4, "Incoming PDU MAC:"); + nas_log->info_hex(genMAC, 4, "Generated PDU MAC:"); + + bool match = true; + for (int i = 0; i < 4; i++) { + if (inMAC[i] != genMAC[i]) { + match = false; + } + } + if (!match) { + sec_mode_rej.emm_cause = LIBLTE_MME_EMM_CAUSE_SECURITY_MODE_REJECTED_UNSPECIFIED; + nas_log->warning("Sending Security Mode Reject due to integrity check failure\n"); + success = false; + } else { - if(sec_mode_cmd.imeisv_req_present && LIBLTE_MME_IMEISV_REQUESTED == sec_mode_cmd.imeisv_req) - { + if (sec_mode_cmd.imeisv_req_present && LIBLTE_MME_IMEISV_REQUESTED == sec_mode_cmd.imeisv_req) { sec_mode_comp.imeisv_present = true; sec_mode_comp.imeisv.type_of_id = LIBLTE_MME_MOBILE_ID_TYPE_IMEISV; usim->get_imei_vec(sec_mode_comp.imeisv.imeisv, 15); sec_mode_comp.imeisv.imeisv[14] = 5; sec_mode_comp.imeisv.imeisv[15] = 3; - } - else - { + } else { sec_mode_comp.imeisv_present = false; + } + + // Reuse pdu for response + pdu->reset(); + liblte_mme_pack_security_mode_complete_msg(&sec_mode_comp, + LIBLTE_MME_SECURITY_HDR_TYPE_INTEGRITY_AND_CIPHERED, + count_ul, + (LIBLTE_BYTE_MSG_STRUCT *) pdu); + integrity_generate(&k_nas_int[16], + count_ul, + lcid - 1, + SECURITY_DIRECTION_UPLINK, + &pdu->msg[5], + pdu->N_bytes - 5, + &pdu->msg[1]); + nas_log->info("Sending Security Mode Complete nas_count_ul=%d, RB=%s\n", + count_ul, + rb_id_text[lcid]); + success = true; } + } + if (!success) { // Reuse pdu for response pdu->reset(); - liblte_mme_pack_security_mode_complete_msg(&sec_mode_comp, - LIBLTE_MME_SECURITY_HDR_TYPE_INTEGRITY_AND_CIPHERED, - count_ul, - (LIBLTE_BYTE_MSG_STRUCT*)pdu); - integrity_generate(&k_nas_int[16], - count_ul, - lcid-1, - SECURITY_DIRECTION_UPLINK, - &pdu->msg[5], - pdu->N_bytes-5, - &pdu->msg[1]); - nas_log->info("Sending Security Mode Complete nas_count_ul=%d, RB=%s\n", - count_ul, - rb_id_text[lcid]); - success = true; + liblte_mme_pack_security_mode_reject_msg(&sec_mode_rej, (LIBLTE_BYTE_MSG_STRUCT *) pdu); } + + rrc->write_sdu(lcid, pdu); } - if(!success) { - // Reuse pdu for response - pdu->reset(); - liblte_mme_pack_security_mode_reject_msg(&sec_mode_rej, (LIBLTE_BYTE_MSG_STRUCT*)pdu); + void nas::parse_service_reject(uint32_t lcid, byte_buffer_t *pdu) { + nas_log->error("TODO:parse_service_reject\n"); + } + + void nas::parse_esm_information_request(uint32_t lcid, byte_buffer_t *pdu) { + nas_log->error("TODO:parse_esm_information_request\n"); } - rrc->write_sdu(lcid, pdu); -} - -void nas::parse_service_reject(uint32_t lcid, byte_buffer_t *pdu) -{ - nas_log->error("TODO:parse_service_reject\n"); -} -void nas::parse_esm_information_request(uint32_t lcid, byte_buffer_t *pdu) -{ - nas_log->error("TODO:parse_esm_information_request\n"); -} -void nas::parse_emm_information(uint32_t lcid, byte_buffer_t *pdu) -{ - nas_log->error("TODO:parse_emm_information\n"); -} + void nas::parse_emm_information(uint32_t lcid, byte_buffer_t *pdu) { + nas_log->error("TODO:parse_emm_information\n"); + } /******************************************************************************* Senders *******************************************************************************/ -void nas::send_attach_request() -{ - LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT attach_req; - byte_buffer_t *msg = pool_allocate; - u_int32_t i; + void nas::send_attach_request() { + LIBLTE_MME_ATTACH_REQUEST_MSG_STRUCT attach_req; + byte_buffer_t *msg = pool_allocate; + u_int32_t i; - attach_req.eps_attach_type = LIBLTE_MME_EPS_ATTACH_TYPE_EPS_ATTACH; + attach_req.eps_attach_type = LIBLTE_MME_EPS_ATTACH_TYPE_EPS_ATTACH; - for(i=0; i<8; i++) - { + for (i = 0; i < 8; i++) { attach_req.ue_network_cap.eea[i] = false; attach_req.ue_network_cap.eia[i] = false; + } + attach_req.ue_network_cap.eea[0] = true; // EEA0 supported + attach_req.ue_network_cap.eia[0] = true; // EIA0 supported + attach_req.ue_network_cap.eia[1] = true; // EIA1 supported + attach_req.ue_network_cap.eia[2] = true; // EIA2 supported + + attach_req.ue_network_cap.uea_present = false; // UMTS encryption algos + attach_req.ue_network_cap.uia_present = false; // UMTS integrity algos + + attach_req.ms_network_cap_present = false; // A/Gb mode (2G) or Iu mode (3G) + + attach_req.eps_mobile_id.type_of_id = LIBLTE_MME_EPS_MOBILE_ID_TYPE_IMSI; + usim->get_imsi_vec(attach_req.eps_mobile_id.imsi, 15); + + // ESM message (PDN connectivity request) for first default bearer + gen_pdn_connectivity_request(&attach_req.esm_msg); + + attach_req.old_p_tmsi_signature_present = false; + attach_req.additional_guti_present = false; + attach_req.last_visited_registered_tai_present = false; + attach_req.drx_param_present = false; + attach_req.ms_network_cap_present = false; + attach_req.old_lai_present = false; + attach_req.tmsi_status_present = false; + attach_req.ms_cm2_present = false; + attach_req.ms_cm3_present = false; + attach_req.supported_codecs_present = false; + attach_req.additional_update_type_present = false; + attach_req.voice_domain_pref_and_ue_usage_setting_present = false; + attach_req.device_properties_present = false; + attach_req.old_guti_type_present = false; + + // Pack the message + liblte_mme_pack_attach_request_msg(&attach_req, (LIBLTE_BYTE_MSG_STRUCT *) msg); + + nas_log->info("Sending attach request\n"); + rrc->write_sdu(RB_ID_SRB1, msg); } - attach_req.ue_network_cap.eea[0] = true; // EEA0 supported - attach_req.ue_network_cap.eia[0] = true; // EIA0 supported - attach_req.ue_network_cap.eia[1] = true; // EIA1 supported - attach_req.ue_network_cap.eia[2] = true; // EIA2 supported - - attach_req.ue_network_cap.uea_present = false; // UMTS encryption algos - attach_req.ue_network_cap.uia_present = false; // UMTS integrity algos - - attach_req.ms_network_cap_present = false; // A/Gb mode (2G) or Iu mode (3G) - - attach_req.eps_mobile_id.type_of_id = LIBLTE_MME_EPS_MOBILE_ID_TYPE_IMSI; - usim->get_imsi_vec(attach_req.eps_mobile_id.imsi, 15); - - // ESM message (PDN connectivity request) for first default bearer - gen_pdn_connectivity_request(&attach_req.esm_msg); - - attach_req.old_p_tmsi_signature_present = false; - attach_req.additional_guti_present = false; - attach_req.last_visited_registered_tai_present = false; - attach_req.drx_param_present = false; - attach_req.ms_network_cap_present = false; - attach_req.old_lai_present = false; - attach_req.tmsi_status_present = false; - attach_req.ms_cm2_present = false; - attach_req.ms_cm3_present = false; - attach_req.supported_codecs_present = false; - attach_req.additional_update_type_present = false; - attach_req.voice_domain_pref_and_ue_usage_setting_present = false; - attach_req.device_properties_present = false; - attach_req.old_guti_type_present = false; - - // Pack the message - liblte_mme_pack_attach_request_msg(&attach_req, (LIBLTE_BYTE_MSG_STRUCT*)msg); - - nas_log->info("Sending attach request\n"); - rrc->write_sdu(RB_ID_SRB1, msg); -} - -void nas::gen_pdn_connectivity_request(LIBLTE_BYTE_MSG_STRUCT *msg) -{ - LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT pdn_con_req; + + void nas::gen_pdn_connectivity_request(LIBLTE_BYTE_MSG_STRUCT *msg) { + LIBLTE_MME_PDN_CONNECTIVITY_REQUEST_MSG_STRUCT pdn_con_req; nas_log->info("Generating PDN Connectivity Request\n"); // Set the PDN con req parameters - pdn_con_req.eps_bearer_id = 0x00; // Unassigned bearer ID + pdn_con_req.eps_bearer_id = 0x00; // Unassigned bearer ID pdn_con_req.proc_transaction_id = 0x01; // First transaction ID - pdn_con_req.pdn_type = LIBLTE_MME_PDN_TYPE_IPV4; - pdn_con_req.request_type = LIBLTE_MME_REQUEST_TYPE_INITIAL_REQUEST; + pdn_con_req.pdn_type = LIBLTE_MME_PDN_TYPE_IPV4; + pdn_con_req.request_type = LIBLTE_MME_REQUEST_TYPE_INITIAL_REQUEST; // Set the optional flags - pdn_con_req.esm_info_transfer_flag_present = false; //FIXME: Check if this is needed - pdn_con_req.apn_present = false; - pdn_con_req.protocol_cnfg_opts_present = false; - pdn_con_req.device_properties_present = false; + pdn_con_req.esm_info_transfer_flag_present = false; //FIXME: Check if this is needed + pdn_con_req.apn_present = false; + pdn_con_req.protocol_cnfg_opts_present = false; + pdn_con_req.device_properties_present = false; // Pack the message liblte_mme_pack_pdn_connectivity_request_msg(&pdn_con_req, msg); -} + } -void nas::send_identity_response(){} + void nas::send_identity_response() {} -void nas::send_service_request() -{ - byte_buffer_t *msg = pool_allocate; - count_ul++; + void nas::send_service_request() { + byte_buffer_t *msg = pool_allocate; + count_ul++; - // Pack the service request message directly - msg->msg[0] = (LIBLTE_MME_SECURITY_HDR_TYPE_SERVICE_REQUEST << 4) | (LIBLTE_MME_PD_EPS_MOBILITY_MANAGEMENT); - msg->N_bytes++; - msg->msg[1] = (ksi & 0x07) << 5; - msg->msg[1] |= count_ul & 0x1F; - msg->N_bytes++; + // Pack the service request message directly + msg->msg[0] = (LIBLTE_MME_SECURITY_HDR_TYPE_SERVICE_REQUEST << 4) | (LIBLTE_MME_PD_EPS_MOBILITY_MANAGEMENT); + msg->N_bytes++; + msg->msg[1] = (ksi & 0x07) << 5; + msg->msg[1] |= count_ul & 0x1F; + msg->N_bytes++; - uint8_t mac[4]; - integrity_generate(&k_nas_int[16], - count_ul, - RB_ID_SRB1-1, - SECURITY_DIRECTION_UPLINK, - &msg->msg[0], - 2, - &mac[0]); - // Set the short MAC - msg->msg[2] = mac[2]; - msg->N_bytes++; - msg->msg[3] = mac[3]; - msg->N_bytes++; - nas_log->info("Sending service request\n"); - rrc->write_sdu(RB_ID_SRB1, msg); -} - -void nas::send_esm_information_response(){} + uint8_t mac[4]; + integrity_generate(&k_nas_int[16], + count_ul, + RB_ID_SRB1 - 1, + SECURITY_DIRECTION_UPLINK, + &msg->msg[0], + 2, + &mac[0]); + // Set the short MAC + msg->msg[2] = mac[2]; + msg->N_bytes++; + msg->msg[3] = mac[3]; + msg->N_bytes++; + nas_log->info("Sending service request\n"); + rrc->write_sdu(RB_ID_SRB1, msg); + } + + void nas::send_esm_information_response() {} } // namespace srsue diff --git a/srsue/src/upper/rrc.cc b/srsue/src/upper/rrc.cc index 20a4db1a2..3e8bfcab8 100644 --- a/srsue/src/upper/rrc.cc +++ b/srsue/src/upper/rrc.cc @@ -27,6 +27,9 @@ #include #include +#include +#include +#include #include "upper/rrc.h" #include "srslte/common/security.h" @@ -36,90 +39,83 @@ using namespace srslte; -namespace srsue{ +namespace srsue { + - /******************************************************************************* Base functions *******************************************************************************/ - -rrc::rrc() - :state(RRC_STATE_IDLE) - ,drb_up(false) -{} - -static void liblte_rrc_handler(void *ctx, char *str) { - rrc *r = (rrc*) ctx; - r->liblte_rrc_log(str); -} - -void rrc::liblte_rrc_log(char* str) -{ - if (rrc_log) { - rrc_log->warning("[ASN]: %s\n", str); - } else { - printf("[ASN]: %s\n", str); + + rrc::rrc() + : state(RRC_STATE_IDLE), drb_up(false) {} + + static void liblte_rrc_handler(void *ctx, char *str) { + rrc *r = (rrc *) ctx; + r->liblte_rrc_log(str); + } + + void rrc::liblte_rrc_log(char *str) { + if (rrc_log) { + rrc_log->warning("[ASN]: %s\n", str); + } else { + printf("[ASN]: %s\n", str); + } } -} - -void rrc::init(phy_interface_rrc *phy_, - mac_interface_rrc *mac_, - rlc_interface_rrc *rlc_, - pdcp_interface_rrc *pdcp_, - nas_interface_rrc *nas_, - usim_interface_rrc *usim_, - mac_interface_timers *mac_timers_, - srslte::log *rrc_log_) -{ - pool = byte_buffer_pool::get_instance(); - phy = phy_; - mac = mac_; - rlc = rlc_; - pdcp = pdcp_; - nas = nas_; - usim = usim_; - rrc_log = rrc_log_; - mac_timers = mac_timers_; - state = RRC_STATE_IDLE; - si_acquire_state = SI_ACQUIRE_IDLE; - - thread_running = true; - start(); - - pthread_mutex_init(&mutex, NULL); - - ue_category = SRSLTE_UE_CATEGORY; - - transaction_id = 0; - - // Register logging handler with liblte_rrc - liblte_rrc_log_register_handler(this, liblte_rrc_handler); - - // Set default values for all layers - set_rrc_default(); - set_phy_default(); - set_mac_default(); -} - -void rrc::stop() -{ - thread_running = false; - wait_thread_finish(); -} - -rrc_state_t rrc::get_state() -{ - return state; -} - -void rrc::set_ue_category(int category) -{ - if(category >= 1 && category <= 5) { - ue_category = category; - } else { - rrc_log->error("Unsupported UE category %d\n", category); + + void rrc::init(phy_interface_rrc *phy_, + mac_interface_rrc *mac_, + rlc_interface_rrc *rlc_, + pdcp_interface_rrc *pdcp_, + nas_interface_rrc *nas_, + usim_interface_rrc *usim_, + mac_interface_timers *mac_timers_, + srslte::log *rrc_log_) { + pool = byte_buffer_pool::get_instance(); + phy = phy_; + mac = mac_; + rlc = rlc_; + pdcp = pdcp_; + nas = nas_; + usim = usim_; + rrc_log = rrc_log_; + mac_timers = mac_timers_; + state = RRC_STATE_IDLE; + si_acquire_state = SI_ACQUIRE_IDLE; + + thread_running = true; + start(); + + pthread_mutex_init(&mutex, NULL); + + ue_category = SRSLTE_UE_CATEGORY; + + transaction_id = 0; + + // Register logging handler with liblte_rrc + liblte_rrc_log_register_handler(this, liblte_rrc_handler); + + // Set default values for all layers + set_rrc_default(); + set_phy_default(); + set_mac_default(); + } + + void rrc::stop() { + thread_running = false; + wait_thread_finish(); + } + + rrc_state_t rrc::get_state() { + return state; + } + + void rrc::set_ue_category(int category) { + if (category >= 1 && category <= 5) { + ue_category = category; + } else { + rrc_log->error("Unsupported UE category %d\n", category); + } } -} @@ -138,286 +134,300 @@ void rrc::set_ue_category(int category) NAS interface *******************************************************************************/ -uint16_t rrc::get_mcc() -{ - if (current_cell) { - if(current_cell->sib1.N_plmn_ids > 0) { - return current_cell->sib1.plmn_id[0].id.mcc; + uint16_t rrc::get_mcc() { + if (current_cell) { + if (current_cell->sib1.N_plmn_ids > 0) { + return current_cell->sib1.plmn_id[0].id.mcc; + } } + return 0; } - return 0; -} - -uint16_t rrc::get_mnc() -{ - if (current_cell) { - if(current_cell->sib1.N_plmn_ids > 0) { - return current_cell->sib1.plmn_id[0].id.mnc; + + uint16_t rrc::get_mnc() { + if (current_cell) { + if (current_cell->sib1.N_plmn_ids > 0) { + return current_cell->sib1.plmn_id[0].id.mnc; + } } + return 0; + } + + void rrc::plmn_search() { + rrc_log->info("Starting PLMN search procedure\n"); + state = RRC_STATE_PLMN_SELECTION; + phy->cell_search_start(); } - return 0; -} - -void rrc::plmn_search() -{ - state = RRC_STATE_PLMN_SELECTION; - phy->cell_search_start(); -} - -void rrc::plmn_select(LIBLTE_RRC_PLMN_IDENTITY_STRUCT plmn_id) -{ - bool sync_ok = false; - - state = RRC_STATE_CELL_SELECTING; - - // Sort cells according to RSRP - - selected_plmn_id = plmn_id; - last_selected_cell = -1; - - select_next_cell_in_plmn(); -} - -void rrc::connect() -{ - pthread_mutex_lock(&mutex); - if(RRC_STATE_CELL_SELECTED == state) { - if (si_acquire_state == SI_ACQUIRE_IDLE) { + + void rrc::plmn_select(LIBLTE_RRC_PLMN_IDENTITY_STRUCT plmn_id) { + rrc_log->info("PLMN %s selected\n", plmn_id_to_c_str(plmn_id).c_str()); + + state = RRC_STATE_CELL_SELECTING; + + // Sort cells according to RSRP + + selected_plmn_id = plmn_id; + last_selected_cell = -1; + + select_next_cell_in_plmn(); + } + + void rrc::connect() { + pthread_mutex_lock(&mutex); + if (RRC_STATE_CELL_SELECTED == state) { rrc_log->info("RRC in IDLE state - sending connection request.\n"); state = RRC_STATE_CONNECTING; send_con_request(); } else { - rrc_log->warning("Received connect() but SI not acquired\n"); + rrc_log->warning("Received connect() but cell is not selected\n"); } - } else { - rrc_log->warning("Received connect() but cell is not selected\n"); + pthread_mutex_unlock(&mutex); } - pthread_mutex_unlock(&mutex); -} - -void rrc::select_next_cell_in_plmn() { - for (uint32_t i=last_selected_cell+1;icell_select(known_cells[i].earfcn, known_cells[i].phy_cell)) { - si_acquire_state = SI_ACQUIRE_SIB1; - last_selected_cell = i; - return; + + void rrc::select_next_cell_in_plmn() { + for (uint32_t i = last_selected_cell + 1; i < known_cells.size(); i++) { + for (uint32_t j = 0; j < known_cells[i].sib1.N_plmn_ids; j++) { + if (known_cells[i].sib1.plmn_id[j].id.mcc == selected_plmn_id.mcc || + known_cells[i].sib1.plmn_id[j].id.mnc == selected_plmn_id.mnc) { + rrc_log->info("Selecting cell PCI=%d, EARFCN=%d, Cell ID=0x%x\n", + known_cells[i].phy_cell.id, known_cells[i].earfcn, + known_cells[i].sib1.cell_id); + rrc_log->console("Selecting cell PCI=%d, EARFCN=%d, Cell ID=0x%x\n", + known_cells[i].phy_cell.id, known_cells[i].earfcn, + known_cells[i].sib1.cell_id); + // Check that cell satisfies S criteria + if (phy->cell_select(known_cells[i].earfcn, known_cells[i].phy_cell)) { + // Give time to the PHY to sync on the new cell + int cnt=0; + while(!phy->sync_status() && cnt<100) { + usleep(1000); + cnt++; + } + if (phy->sync_status()) { + if (!known_cells[i].has_valid_sib1) { + si_acquire_state = SI_ACQUIRE_SIB1; + } else if (!known_cells[i].has_valid_sib2) { + si_acquire_state = SI_ACQUIRE_SIB2; + } else { + si_acquire_state = SI_ACQUIRE_IDLE; + } + last_selected_cell = i; + } else { + rrc_log->warning("Selecting cell EARFCN=%d, Cell ID=0x%x: Could not synchronize\n", + known_cells[i].earfcn, known_cells[i].sib1.cell_id); + } + return; + } else { + rrc_log->warning("Selecting cell EARFCN=%d, Cell ID=0x%x.\n", + known_cells[i].earfcn, known_cells[i].sib1.cell_id); + } } - } + } } } -} /******************************************************************************* PHY interface *******************************************************************************/ -void rrc::cell_found(uint32_t earfcn, srslte_cell_t phy_cell, float rsrp) { - - // find if cell_id-earfcn combination already exists - for (uint32_t i=0;iinfo("Updating cell EARFCN=%d, PCI=%d, RSRP=%d dBm\n", known_cells[i].earfcn, known_cells[i].phy_cell.id, known_cells[i].rsrp); - return; + void rrc::cell_found(uint32_t earfcn, srslte_cell_t phy_cell, float rsrp) { + + // find if cell_id-earfcn combination already exists + for (uint32_t i = 0; i < known_cells.size(); i++) { + if (earfcn == known_cells[i].earfcn && phy_cell.id == known_cells[i].phy_cell.id) { + known_cells[i].rsrp = rsrp; + current_cell = &known_cells[i]; + rrc_log->info("Updating cell EARFCN=%d, PCI=%d, RSRP=%.1f dBm\n", known_cells[i].earfcn, + known_cells[i].phy_cell.id, known_cells[i].rsrp); + return; + } } + // add to list of known cells + cell_t cell; + cell.phy_cell = phy_cell; + cell.rsrp = rsrp; + cell.earfcn = earfcn; + cell.has_valid_sib1 = false; + cell.has_valid_sib2 = false; + known_cells.push_back(cell); + + // save current cell + current_cell = &known_cells.back(); + + si_acquire_state = SI_ACQUIRE_SIB1; + + rrc_log->info("Found Cell: PCI=%d, PRB=%d, Ports=%d, EARFCN=%d, RSRP=%.1f dBm\n", + cell.phy_cell.id, cell.phy_cell.nof_prb, cell.phy_cell.nof_ports, + cell.earfcn, cell.rsrp); + + rrc_log->console("Found Cell: PCI=%d, PRB=%d, Ports=%d, EARFCN=%d, RSRP=%.1f dBm\n", + cell.phy_cell.id, cell.phy_cell.nof_prb, cell.phy_cell.nof_ports, + cell.earfcn, cell.rsrp); + } - // add to list of known cells - cell_t cell; - cell.phy_cell = phy_cell; - cell.rsrp = rsrp; - cell.earfcn = earfcn; - cell.has_valid_sib1 = false; - cell.has_valid_sib2 = false; - known_cells.push_back(cell); - - // save current cell - current_cell = &known_cells.back(); - - rrc_log->info("Found new cell EARFCN=%d, PCI=%d, RSRP=%d dBm\n", cell.earfcn, cell.phy_cell.id, cell.rsrp); - -} // Detection of physical layer problems (5.3.11.1) -void rrc::out_of_sync() -{ - if (!mac_timers->get(t311)->is_running() && !mac_timers->get(t310)->is_running()) { - n310_cnt++; - if (n310_cnt == N310) { - mac_timers->get(t310)->reset(); - mac_timers->get(t310)->run(); - n310_cnt = 0; - rrc_log->info("Detected %d out-of-sync from PHY. Starting T310 timer\n", N310); + void rrc::out_of_sync() { + if (!mac_timers->get(t311)->is_running() && !mac_timers->get(t310)->is_running()) { + n310_cnt++; + if (n310_cnt == N310) { + mac_timers->get(t310)->reset(); + mac_timers->get(t310)->run(); + n310_cnt = 0; + rrc_log->info("Detected %d out-of-sync from PHY. Starting T310 timer\n", N310); + } } } -} // Recovery of physical layer problems (5.3.11.2) -void rrc::in_sync() -{ - if (mac_timers->get(t310)->is_running()) { - n311_cnt++; - if (n311_cnt == N311) { - mac_timers->get(t310)->stop(); - n311_cnt = 0; - rrc_log->info("Detected %d in-sync from PHY. Stopping T310 timer\n", N311); + void rrc::in_sync() { + if (mac_timers->get(t310)->is_running()) { + n311_cnt++; + if (n311_cnt == N311) { + mac_timers->get(t310)->stop(); + n311_cnt = 0; + rrc_log->info("Detected %d in-sync from PHY. Stopping T310 timer\n", N311); + } } } -} /******************************************************************************* PDCP interface *******************************************************************************/ -void rrc::write_pdu_bcch_bch(byte_buffer_t *pdu) -{ - pool->deallocate(pdu); - if (state == RRC_STATE_PLMN_SELECTION) { - // Do we need to do something with BCH? - rrc_log->info_hex(pdu->msg, pdu->N_bytes, "BCCH BCH message received."); - si_acquire_state = SI_ACQUIRE_SIB1; - } else { - rrc_log->warning("Received BCCH BCH in incorrect state\n"); - } -} - -void rrc::write_pdu_bcch_dlsch(byte_buffer_t *pdu) -{ - rrc_log->info_hex(pdu->msg, pdu->N_bytes, "BCCH DLSCH message received."); - rrc_log->info("BCCH DLSCH message Stack latency: %ld us\n", pdu->get_latency_us()); - LIBLTE_RRC_BCCH_DLSCH_MSG_STRUCT dlsch_msg; - srslte_bit_unpack_vector(pdu->msg, bit_buf.msg, pdu->N_bytes*8); - bit_buf.N_bits = pdu->N_bytes*8; - pool->deallocate(pdu); - liblte_rrc_unpack_bcch_dlsch_msg((LIBLTE_BIT_MSG_STRUCT*)&bit_buf, &dlsch_msg); - - if (dlsch_msg.N_sibs > 0) { - if (LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_1 == dlsch_msg.sibs[0].sib_type && SI_ACQUIRE_SIB1 == si_acquire_state) - { - mac->bcch_stop_rx(); - - // Handle SIB1 - memcpy(¤t_cell->sib1, &dlsch_msg.sibs[0].sib.sib1, sizeof(LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_1_STRUCT)); - - rrc_log->info("SIB1 received, CellID=%d, si_window=%d, sib2_period=%d\n", - current_cell->sib1.cell_id&0xfff, - liblte_rrc_si_window_length_num[current_cell->sib1.si_window_length], - liblte_rrc_si_periodicity_num[current_cell->sib1.sched_info[0].si_periodicity]); - - // Send PLMN and TAC to NAS - std::stringstream ss; - for(uint32_t i=0;isib1.N_plmn_ids;i++){ - std::string mcc; - std::string mnc; - mcc_to_string(current_cell->sib1.plmn_id[i].id.mcc, &mcc); - mnc_to_string(current_cell->sib1.plmn_id[i].id.mnc, &mnc); - ss << " PLMN Id: MCC " << mcc << " MNC " << mnc; - - nas->plmn_found(current_cell->sib1.plmn_id[i].id, current_cell->sib1.tracking_area_code); - } - - // Set TDD Config - if (current_cell->sib1.tdd) { - phy->set_config_tdd(¤t_cell->sib1.tdd_cnfg); - } - - rrc_log->console("SIB1 received, CellID=%d, %s\n", - current_cell->sib1.cell_id&0xfff, - ss.str().c_str()); - - current_cell->has_valid_sib1 = true; - si_acquire_state = SI_ACQUIRE_SIB2; - - } else if (LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_2 == dlsch_msg.sibs[0].sib_type && SI_ACQUIRE_SIB2 == si_acquire_state) - { - mac->bcch_stop_rx(); - - // Handle SIB2 - memcpy(¤t_cell->sib2, &dlsch_msg.sibs[0].sib.sib2, sizeof(LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_2_STRUCT)); - rrc_log->console("SIB2 received\n"); - rrc_log->info("SIB2 received\n"); - - apply_sib2_configs(¤t_cell->sib2); - - current_cell->has_valid_sib2 = true; - si_acquire_state = SI_ACQUIRE_IDLE; + void rrc::write_pdu_bcch_bch(byte_buffer_t *pdu) { + pool->deallocate(pdu); + if (state == RRC_STATE_PLMN_SELECTION) { + // Do we need to do something with BCH? + rrc_log->info_hex(pdu->msg, pdu->N_bytes, "BCCH BCH message received."); + } else { + rrc_log->warning("Received BCCH BCH in incorrect state\n"); } } -} + void rrc::write_pdu_bcch_dlsch(byte_buffer_t *pdu) { + rrc_log->info_hex(pdu->msg, pdu->N_bytes, "BCCH DLSCH message received."); + rrc_log->info("BCCH DLSCH message Stack latency: %ld us\n", pdu->get_latency_us()); + LIBLTE_RRC_BCCH_DLSCH_MSG_STRUCT dlsch_msg; + srslte_bit_unpack_vector(pdu->msg, bit_buf.msg, pdu->N_bytes * 8); + bit_buf.N_bits = pdu->N_bytes * 8; + pool->deallocate(pdu); + liblte_rrc_unpack_bcch_dlsch_msg((LIBLTE_BIT_MSG_STRUCT *) &bit_buf, &dlsch_msg); -// Right now, this thread only controls System Information acquisition procedure -void rrc::run_thread() -{ - uint32_t tti ; - uint32_t si_win_start, si_win_len; - uint16_t period; - uint32_t nof_sib1_trials = 0; - const int SIB1_SEARCH_TIMEOUT = 30; - - while(thread_running) - { - switch(si_acquire_state) - { - case SI_ACQUIRE_SIB1: - // Instruct MAC to look for SIB1 - if (!current_cell->has_valid_sib1) { - tti = mac->get_current_tti(); - si_win_start = sib_start_tti(tti, 2, 5); - mac->bcch_start_rx(si_win_start, 1); - rrc_log->debug("Instructed MAC to search for SIB1, win_start=%d, win_len=%d\n", - si_win_start, 1); - nof_sib1_trials++; - if (nof_sib1_trials >= SIB1_SEARCH_TIMEOUT) { - if (state == RRC_STATE_CELL_SELECTING) { - select_next_cell_in_plmn(); + if (dlsch_msg.N_sibs > 0) { + if (LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_1 == dlsch_msg.sibs[0].sib_type && SI_ACQUIRE_SIB1 == si_acquire_state) { + mac->bcch_stop_rx(); + + // Handle SIB1 + memcpy(¤t_cell->sib1, &dlsch_msg.sibs[0].sib.sib1, sizeof(LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_1_STRUCT)); + + rrc_log->info("SIB1 received, CellID=%d, si_window=%d, sib2_period=%d\n", + current_cell->sib1.cell_id & 0xfff, + liblte_rrc_si_window_length_num[current_cell->sib1.si_window_length], + liblte_rrc_si_periodicity_num[current_cell->sib1.sched_info[0].si_periodicity]); + + + // Set TDD Config + if (current_cell->sib1.tdd) { + phy->set_config_tdd(¤t_cell->sib1.tdd_cnfg); + } + + current_cell->has_valid_sib1 = true; + + // Jump to next state + switch(state) { + case RRC_STATE_CELL_SELECTING: + si_acquire_state = SI_ACQUIRE_SIB2; + break; + case RRC_STATE_PLMN_SELECTION: si_acquire_state = SI_ACQUIRE_IDLE; - } else if (state == RRC_STATE_PLMN_SELECTION) { + rrc_log->info("SI Acquisition done. Searching next cell...\n"); + usleep(5000); phy->cell_search_next(); - } - nof_sib1_trials = 0; + break; + default: + si_acquire_state = SI_ACQUIRE_IDLE; } - } else { - si_acquire_state = SI_ACQUIRE_SIB2; - } - break; - case SI_ACQUIRE_SIB2: - // Instruct MAC to look for SIB2 only when selecting a cell - if (state == RRC_STATE_CELL_SELECTING && !current_cell->has_valid_sib2) { - tti = mac->get_current_tti(); - period = liblte_rrc_si_periodicity_num[current_cell->sib1.sched_info[0].si_periodicity]; - si_win_start = sib_start_tti(tti, period, 0); - si_win_len = liblte_rrc_si_window_length_num[current_cell->sib1.si_window_length]; - - mac->bcch_start_rx(si_win_start, si_win_len); - rrc_log->debug("Instructed MAC to search for SIB2, win_start=%d, win_len=%d\n", - si_win_start, si_win_len); - } else { - si_acquire_state = SI_ACQUIRE_DONE; + + // Send PLMN and TAC to NAS + std::stringstream ss; + for (uint32_t i = 0; i < current_cell->sib1.N_plmn_ids; i++) { + nas->plmn_found(current_cell->sib1.plmn_id[i].id, current_cell->sib1.tracking_area_code); + } + + } else if (LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_2 == dlsch_msg.sibs[0].sib_type && + SI_ACQUIRE_SIB2 == si_acquire_state) { + mac->bcch_stop_rx(); + + // Handle SIB2 + memcpy(¤t_cell->sib2, &dlsch_msg.sibs[0].sib.sib2, sizeof(LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_2_STRUCT)); + rrc_log->info("SIB2 received\n"); + + apply_sib2_configs(¤t_cell->sib2); + + current_cell->has_valid_sib2 = true; + + // Jump to next state + switch(state) { + case RRC_STATE_CELL_SELECTING: + si_acquire_state = SI_ACQUIRE_IDLE; + state = RRC_STATE_CELL_SELECTED; + nas->cell_selected(); + break; + default: + si_acquire_state = SI_ACQUIRE_IDLE; + } + } - break; - case SI_ACQUIRE_DONE: - - // After acquiring SI, tell NAS that the cell is selected or go to next cell in case of PLMN selection - - if (state == RRC_STATE_CELL_SELECTING) { - nas->cell_selected(); - state = RRC_STATE_CELL_SELECTED; - } else if (state == RRC_STATE_PLMN_SELECTION) { - phy->cell_search_next(); + } + } + + +// Right now, this thread only controls System Information acquisition procedure + void rrc::run_thread() { + uint32_t tti; + uint32_t si_win_start, si_win_len; + uint16_t period; + uint32_t nof_sib1_trials = 0; + const int SIB1_SEARCH_TIMEOUT = 30; + + while (thread_running) { + switch (si_acquire_state) { + case SI_ACQUIRE_SIB1: + // Instruct MAC to look for SIB1 + tti = mac->get_current_tti(); + si_win_start = sib_start_tti(tti, 2, 5); + mac->bcch_start_rx(si_win_start, 1); + rrc_log->debug("Instructed MAC to search for SIB1, win_start=%d, win_len=%d\n", + si_win_start, 1); + nof_sib1_trials++; + if (nof_sib1_trials >= SIB1_SEARCH_TIMEOUT) { + if (state == RRC_STATE_CELL_SELECTING) { + select_next_cell_in_plmn(); + si_acquire_state = SI_ACQUIRE_IDLE; + } else if (state == RRC_STATE_PLMN_SELECTION) { + phy->cell_search_next(); + } + nof_sib1_trials = 0; + } + usleep(20000); + break; + case SI_ACQUIRE_SIB2: + // Instruct MAC to look for SIB2 only when selecting a cell + tti = mac->get_current_tti(); + period = liblte_rrc_si_periodicity_num[current_cell->sib1.sched_info[0].si_periodicity]; + si_win_start = sib_start_tti(tti, period, 0); + si_win_len = liblte_rrc_si_window_length_num[current_cell->sib1.si_window_length]; + + mac->bcch_start_rx(si_win_start, si_win_len); + rrc_log->debug("Instructed MAC to search for SIB2, win_start=%d, win_len=%d\n", + si_win_start, si_win_len); + usleep(current_cell->sib1.si_window_length*1000); + break; + default: + break; } - si_acquire_state = SI_ACQUIRE_IDLE; - break; - default: - break; - } - usleep(10000); + usleep(10000); + } } -} @@ -456,23 +466,21 @@ void rrc::run_thread() NAS interface *******************************************************************************/ -void rrc::write_sdu(uint32_t lcid, byte_buffer_t *sdu) -{ - rrc_log->info_hex(sdu->msg, sdu->N_bytes, "RX %s SDU", rb_id_text[lcid]); - - switch(state) - { - case RRC_STATE_CONNECTING: - send_con_setup_complete(sdu); - break; - case RRC_STATE_CONNECTED: - send_ul_info_transfer(lcid, sdu); - break; - default: - rrc_log->error("SDU received from NAS while RRC state = %s", rrc_state_text[state]); - break; + void rrc::write_sdu(uint32_t lcid, byte_buffer_t *sdu) { + rrc_log->info_hex(sdu->msg, sdu->N_bytes, "RX %s SDU", rb_id_text[lcid]); + + switch (state) { + case RRC_STATE_CONNECTING: + send_con_setup_complete(sdu); + break; + case RRC_STATE_CONNECTED: + send_ul_info_transfer(lcid, sdu); + break; + default: + rrc_log->error("SDU received from NAS while RRC state = %s", rrc_state_text[state]); + break; + } } -} @@ -480,1154 +488,1110 @@ void rrc::write_sdu(uint32_t lcid, byte_buffer_t *sdu) MAC interface *******************************************************************************/ /* Reception of PUCCH/SRS release procedure (Section 5.3.13) */ -void rrc::release_pucch_srs() -{ - // Apply default configuration for PUCCH (CQI and SR) and SRS (release) - set_phy_default_pucch_srs(); - - // Configure RX signals without pregeneration because default option is release - phy->configure_ul_params(true); -} + void rrc::release_pucch_srs() { + // Apply default configuration for PUCCH (CQI and SR) and SRS (release) + set_phy_default_pucch_srs(); -void rrc::ra_problem() { - radio_link_failure(); -} + // Configure RX signals without pregeneration because default option is release + phy->configure_ul_params(true); + } + + void rrc::ra_problem() { + radio_link_failure(); + } /******************************************************************************* GW interface *******************************************************************************/ -bool rrc::is_connected() -{ - return (RRC_STATE_CONNECTED == state); -} + bool rrc::is_connected() { + return (RRC_STATE_CONNECTED == state); + } -bool rrc::have_drb() -{ - return drb_up; -} + bool rrc::have_drb() { + return drb_up; + } /******************************************************************************* PDCP interface *******************************************************************************/ -void rrc::write_pdu(uint32_t lcid, byte_buffer_t *pdu) -{ - rrc_log->info_hex(pdu->msg, pdu->N_bytes, "TX %s PDU", rb_id_text[lcid]); - rrc_log->info("TX PDU Stack latency: %ld us\n", pdu->get_latency_us()); - - switch(lcid) - { - case RB_ID_SRB0: - parse_dl_ccch(pdu); - break; - case RB_ID_SRB1: - case RB_ID_SRB2: - parse_dl_dcch(lcid, pdu); - break; - default: - rrc_log->error("TX PDU with invalid bearer id: %s", lcid); - break; + void rrc::write_pdu(uint32_t lcid, byte_buffer_t *pdu) { + rrc_log->info_hex(pdu->msg, pdu->N_bytes, "TX %s PDU", rb_id_text[lcid]); + rrc_log->info("TX PDU Stack latency: %ld us\n", pdu->get_latency_us()); + + switch (lcid) { + case RB_ID_SRB0: + parse_dl_ccch(pdu); + break; + case RB_ID_SRB1: + case RB_ID_SRB2: + parse_dl_dcch(lcid, pdu); + break; + default: + rrc_log->error("TX PDU with invalid bearer id: %s", lcid); + break; + } + } -} + void rrc::write_pdu_pcch(byte_buffer_t *pdu) { + if (pdu->N_bytes > 0 && pdu->N_bytes < SRSLTE_MAX_BUFFER_SIZE_BITS) { + rrc_log->info_hex(pdu->msg, pdu->N_bytes, "PCCH message received %d bytes\n", pdu->N_bytes); + rrc_log->info("PCCH message Stack latency: %ld us\n", pdu->get_latency_us()); + rrc_log->console("PCCH message received %d bytes\n", pdu->N_bytes); -void rrc::write_pdu_pcch(byte_buffer_t *pdu) -{ - if (pdu->N_bytes > 0 && pdu->N_bytes < SRSLTE_MAX_BUFFER_SIZE_BITS) { - rrc_log->info_hex(pdu->msg, pdu->N_bytes, "PCCH message received %d bytes\n", pdu->N_bytes); - rrc_log->info("PCCH message Stack latency: %ld us\n", pdu->get_latency_us()); - rrc_log->console("PCCH message received %d bytes\n", pdu->N_bytes); - - LIBLTE_RRC_PCCH_MSG_STRUCT pcch_msg; - srslte_bit_unpack_vector(pdu->msg, bit_buf.msg, pdu->N_bytes*8); - bit_buf.N_bits = pdu->N_bytes*8; - pool->deallocate(pdu); - liblte_rrc_unpack_pcch_msg((LIBLTE_BIT_MSG_STRUCT*)&bit_buf, &pcch_msg); - - if (pcch_msg.paging_record_list_size > LIBLTE_RRC_MAX_PAGE_REC) { - pcch_msg.paging_record_list_size = LIBLTE_RRC_MAX_PAGE_REC; - } + LIBLTE_RRC_PCCH_MSG_STRUCT pcch_msg; + srslte_bit_unpack_vector(pdu->msg, bit_buf.msg, pdu->N_bytes * 8); + bit_buf.N_bits = pdu->N_bytes * 8; + pool->deallocate(pdu); + liblte_rrc_unpack_pcch_msg((LIBLTE_BIT_MSG_STRUCT *) &bit_buf, &pcch_msg); - LIBLTE_RRC_S_TMSI_STRUCT s_tmsi; - if(!nas->get_s_tmsi(&s_tmsi)) { - rrc_log->info("No S-TMSI present in NAS\n"); - return; - } - - LIBLTE_RRC_S_TMSI_STRUCT *s_tmsi_paged; - for (uint32_t i=0;iinfo("Received paging (%d/%d) for UE 0x%x\n", i+1, pcch_msg.paging_record_list_size, - pcch_msg.paging_record_list[i].ue_identity.s_tmsi); - rrc_log->console("Received paging (%d/%d) for UE 0x%x\n", i+1, pcch_msg.paging_record_list_size, + if (pcch_msg.paging_record_list_size > LIBLTE_RRC_MAX_PAGE_REC) { + pcch_msg.paging_record_list_size = LIBLTE_RRC_MAX_PAGE_REC; + } + + LIBLTE_RRC_S_TMSI_STRUCT s_tmsi; + if (!nas->get_s_tmsi(&s_tmsi)) { + rrc_log->info("No S-TMSI present in NAS\n"); + return; + } + + LIBLTE_RRC_S_TMSI_STRUCT *s_tmsi_paged; + for (uint32_t i = 0; i < pcch_msg.paging_record_list_size; i++) { + s_tmsi_paged = &pcch_msg.paging_record_list[i].ue_identity.s_tmsi; + rrc_log->info("Received paging (%d/%d) for UE 0x%x\n", i + 1, pcch_msg.paging_record_list_size, pcch_msg.paging_record_list[i].ue_identity.s_tmsi); - if(s_tmsi.mmec == s_tmsi_paged->mmec && s_tmsi.m_tmsi == s_tmsi_paged->m_tmsi) { - rrc_log->info("S-TMSI match in paging message\n"); - rrc_log->console("S-TMSI match in paging message\n"); - mac->pcch_stop_rx(); - if(RRC_STATE_IDLE == state) { - rrc_log->info("RRC in IDLE state - sending connection request.\n"); - state = RRC_STATE_CONNECTING; - send_con_request(); + rrc_log->console("Received paging (%d/%d) for UE 0x%x\n", i + 1, pcch_msg.paging_record_list_size, + pcch_msg.paging_record_list[i].ue_identity.s_tmsi); + if (s_tmsi.mmec == s_tmsi_paged->mmec && s_tmsi.m_tmsi == s_tmsi_paged->m_tmsi) { + rrc_log->info("S-TMSI match in paging message\n"); + rrc_log->console("S-TMSI match in paging message\n"); + mac->pcch_stop_rx(); + if (RRC_STATE_IDLE == state) { + rrc_log->info("RRC in IDLE state - sending connection request.\n"); + state = RRC_STATE_CONNECTING; + send_con_request(); + } } } } } -} /******************************************************************************* RLC interface *******************************************************************************/ -void rrc::max_retx_attempted() -{ - //TODO: Handle the radio link failure - rrc_log->warning("Max RLC reTx attempted\n"); - //radio_link_failure(); -} + void rrc::max_retx_attempted() { + //TODO: Handle the radio link failure + rrc_log->warning("Max RLC reTx attempted\n"); + //radio_link_failure(); + } /******************************************************************************* Senders *******************************************************************************/ -void rrc::send_con_request() -{ - rrc_log->debug("Preparing RRC Connection Request\n"); - LIBLTE_RRC_UL_CCCH_MSG_STRUCT ul_ccch_msg; - LIBLTE_RRC_S_TMSI_STRUCT s_tmsi; - - // Prepare ConnectionRequest packet - ul_ccch_msg.msg_type = LIBLTE_RRC_UL_CCCH_MSG_TYPE_RRC_CON_REQ; - if(nas->get_s_tmsi(&s_tmsi)) { - ul_ccch_msg.msg.rrc_con_req.ue_id_type = LIBLTE_RRC_CON_REQ_UE_ID_TYPE_S_TMSI; - ul_ccch_msg.msg.rrc_con_req.ue_id.s_tmsi = s_tmsi; - } else { - ul_ccch_msg.msg.rrc_con_req.ue_id_type = LIBLTE_RRC_CON_REQ_UE_ID_TYPE_RANDOM_VALUE; - ul_ccch_msg.msg.rrc_con_req.ue_id.random = 1000; - } - ul_ccch_msg.msg.rrc_con_req.cause = LIBLTE_RRC_CON_REQ_EST_CAUSE_MO_SIGNALLING; - liblte_rrc_pack_ul_ccch_msg(&ul_ccch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); - } - byte_buffer_t *pdcp_buf = pool_allocate;; - srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); - pdcp_buf->N_bytes = bit_buf.N_bits/8; - pdcp_buf->set_timestamp(); - - // Set UE contention resolution ID in MAC - uint64_t uecri=0; - uint8_t *ue_cri_ptr = (uint8_t*) &uecri; - uint32_t nbytes = 6; - for (uint32_t i=0;imsg[i]; - } - rrc_log->debug("Setting UE contention resolution ID: %d\n", uecri); - - mac->set_contention_id(uecri); + void rrc::send_con_request() { + rrc_log->debug("Preparing RRC Connection Request\n"); + LIBLTE_RRC_UL_CCCH_MSG_STRUCT ul_ccch_msg; + LIBLTE_RRC_S_TMSI_STRUCT s_tmsi; + + // Prepare ConnectionRequest packet + ul_ccch_msg.msg_type = LIBLTE_RRC_UL_CCCH_MSG_TYPE_RRC_CON_REQ; + if (nas->get_s_tmsi(&s_tmsi)) { + ul_ccch_msg.msg.rrc_con_req.ue_id_type = LIBLTE_RRC_CON_REQ_UE_ID_TYPE_S_TMSI; + ul_ccch_msg.msg.rrc_con_req.ue_id.s_tmsi = s_tmsi; + } else { + ul_ccch_msg.msg.rrc_con_req.ue_id_type = LIBLTE_RRC_CON_REQ_UE_ID_TYPE_RANDOM_VALUE; + ul_ccch_msg.msg.rrc_con_req.ue_id.random = 1000; + } + ul_ccch_msg.msg.rrc_con_req.cause = LIBLTE_RRC_CON_REQ_EST_CAUSE_MO_SIGNALLING; + liblte_rrc_pack_ul_ccch_msg(&ul_ccch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + byte_buffer_t *pdcp_buf = pool_allocate;; + srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); + pdcp_buf->N_bytes = bit_buf.N_bits / 8; + pdcp_buf->set_timestamp(); + + // Set UE contention resolution ID in MAC + uint64_t uecri = 0; + uint8_t *ue_cri_ptr = (uint8_t *) &uecri; + uint32_t nbytes = 6; + for (uint32_t i = 0; i < nbytes; i++) { + ue_cri_ptr[nbytes - i - 1] = pdcp_buf->msg[i]; + } + rrc_log->debug("Setting UE contention resolution ID: %d\n", uecri); - rrc_log->info("Sending RRC Connection Request on SRB0\n"); - pdcp->write_sdu(RB_ID_SRB0, pdcp_buf); -} + mac->set_contention_id(uecri); + + rrc_log->info("Sending RRC Connection Request on SRB0\n"); + pdcp->write_sdu(RB_ID_SRB0, pdcp_buf); + } /* RRC connection re-establishment procedure (5.3.7) */ -void rrc::send_con_restablish_request() -{ - - srslte_cell_t cell; - phy->get_current_cell(&cell); - - LIBLTE_RRC_UL_CCCH_MSG_STRUCT ul_ccch_msg; - LIBLTE_RRC_S_TMSI_STRUCT s_tmsi; - - // Compute shortMAC-I - uint8_t varShortMAC[128], varShortMAC_packed[16]; - bzero(varShortMAC, 128); - bzero(varShortMAC_packed, 16); - uint8_t *msg_ptr = varShortMAC; - liblte_rrc_pack_cell_identity_ie(0x1a2d0, &msg_ptr); - liblte_rrc_pack_phys_cell_id_ie(cell.id, &msg_ptr); - mac_interface_rrc::ue_rnti_t ue_rnti; - mac->get_rntis(&ue_rnti); - liblte_rrc_pack_c_rnti_ie(ue_rnti.crnti, &msg_ptr); - srslte_bit_pack_vector(varShortMAC, varShortMAC_packed, msg_ptr - varShortMAC); - - uint8_t mac_key[4]; - security_128_eia2(&k_rrc_int[16], - 1, - 1, - 1, - varShortMAC_packed, - 7, - mac_key); - - mac_interface_rrc::ue_rnti_t uernti; - mac->get_rntis(&uernti); - - // Prepare ConnectionRestalishmentRequest packet - ul_ccch_msg.msg_type = LIBLTE_RRC_UL_CCCH_MSG_TYPE_RRC_CON_REEST_REQ; - ul_ccch_msg.msg.rrc_con_reest_req.ue_id.c_rnti = uernti.crnti; - ul_ccch_msg.msg.rrc_con_reest_req.ue_id.phys_cell_id = cell.id; - ul_ccch_msg.msg.rrc_con_reest_req.ue_id.short_mac_i = mac_key[2]<<8 | mac_key[3]; - ul_ccch_msg.msg.rrc_con_reest_req.cause = LIBLTE_RRC_CON_REEST_REQ_CAUSE_OTHER_FAILURE; - liblte_rrc_pack_ul_ccch_msg(&ul_ccch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - rrc_log->info("Initiating RRC Connection Reestablishment Procedure\n"); - rrc_log->console("RRC Connection Reestablishment\n"); - mac_timers->get(t310)->stop(); - mac_timers->get(t311)->reset(); - mac_timers->get(t311)->run(); - - set_phy_default(); - mac->reset(); - - // FIXME: Cell selection should be different?? - phy->resync_sfn(); - - // Wait for cell re-synchronization - uint32_t timeout_cnt = 0; - while(!phy->sync_status() && timeout_cnt < TIMEOUT_RESYNC_REESTABLISH){ - usleep(10000); - timeout_cnt++; - } - mac_timers->get(t301)->reset(); - mac_timers->get(t301)->run(); - mac_timers->get(t311)->stop(); - rrc_log->info("Cell Selection finished. Initiating transmission of RRC Connection Reestablishment Request\n"); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); - } - byte_buffer_t *pdcp_buf = pool_allocate;; - srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); - pdcp_buf->N_bytes = bit_buf.N_bits/8; - - // Set UE contention resolution ID in MAC - uint64_t uecri=0; - uint8_t *ue_cri_ptr = (uint8_t*) &uecri; - uint32_t nbytes = 6; - for (uint32_t i=0;imsg[i]; + void rrc::send_con_restablish_request() { + + srslte_cell_t cell; + phy->get_current_cell(&cell); + + LIBLTE_RRC_UL_CCCH_MSG_STRUCT ul_ccch_msg; + LIBLTE_RRC_S_TMSI_STRUCT s_tmsi; + + // Compute shortMAC-I + uint8_t varShortMAC[128], varShortMAC_packed[16]; + bzero(varShortMAC, 128); + bzero(varShortMAC_packed, 16); + uint8_t *msg_ptr = varShortMAC; + liblte_rrc_pack_cell_identity_ie(0x1a2d0, &msg_ptr); + liblte_rrc_pack_phys_cell_id_ie(cell.id, &msg_ptr); + mac_interface_rrc::ue_rnti_t ue_rnti; + mac->get_rntis(&ue_rnti); + liblte_rrc_pack_c_rnti_ie(ue_rnti.crnti, &msg_ptr); + srslte_bit_pack_vector(varShortMAC, varShortMAC_packed, msg_ptr - varShortMAC); + + uint8_t mac_key[4]; + security_128_eia2(&k_rrc_int[16], + 1, + 1, + 1, + varShortMAC_packed, + 7, + mac_key); + + mac_interface_rrc::ue_rnti_t uernti; + mac->get_rntis(&uernti); + + // Prepare ConnectionRestalishmentRequest packet + ul_ccch_msg.msg_type = LIBLTE_RRC_UL_CCCH_MSG_TYPE_RRC_CON_REEST_REQ; + ul_ccch_msg.msg.rrc_con_reest_req.ue_id.c_rnti = uernti.crnti; + ul_ccch_msg.msg.rrc_con_reest_req.ue_id.phys_cell_id = cell.id; + ul_ccch_msg.msg.rrc_con_reest_req.ue_id.short_mac_i = mac_key[2] << 8 | mac_key[3]; + ul_ccch_msg.msg.rrc_con_reest_req.cause = LIBLTE_RRC_CON_REEST_REQ_CAUSE_OTHER_FAILURE; + liblte_rrc_pack_ul_ccch_msg(&ul_ccch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + rrc_log->info("Initiating RRC Connection Reestablishment Procedure\n"); + rrc_log->console("RRC Connection Reestablishment\n"); + mac_timers->get(t310)->stop(); + mac_timers->get(t311)->reset(); + mac_timers->get(t311)->run(); + + set_phy_default(); + mac->reset(); + + // FIXME: Cell selection should be different?? + phy->resync_sfn(); + + // Wait for cell re-synchronization + uint32_t timeout_cnt = 0; + while (!phy->sync_status() && timeout_cnt < TIMEOUT_RESYNC_REESTABLISH) { + usleep(10000); + timeout_cnt++; + } + mac_timers->get(t301)->reset(); + mac_timers->get(t301)->run(); + mac_timers->get(t311)->stop(); + rrc_log->info("Cell Selection finished. Initiating transmission of RRC Connection Reestablishment Request\n"); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + byte_buffer_t *pdcp_buf = pool_allocate;; + srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); + pdcp_buf->N_bytes = bit_buf.N_bits / 8; + + // Set UE contention resolution ID in MAC + uint64_t uecri = 0; + uint8_t *ue_cri_ptr = (uint8_t *) &uecri; + uint32_t nbytes = 6; + for (uint32_t i = 0; i < nbytes; i++) { + ue_cri_ptr[nbytes - i - 1] = pdcp_buf->msg[i]; + } + rrc_log->debug("Setting UE contention resolution ID: %d\n", uecri); + mac->set_contention_id(uecri); + + rrc_log->info("Sending RRC Connection Resetablishment Request on SRB0\n"); + pdcp->write_sdu(RB_ID_SRB0, pdcp_buf); } - rrc_log->debug("Setting UE contention resolution ID: %d\n", uecri); - mac->set_contention_id(uecri); - - rrc_log->info("Sending RRC Connection Resetablishment Request on SRB0\n"); - pdcp->write_sdu(RB_ID_SRB0, pdcp_buf); -} - - -void rrc::send_con_restablish_complete() -{ - rrc_log->debug("Preparing RRC Connection Reestablishment Complete\n"); - LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; - - // Prepare ConnectionSetupComplete packet - ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_RRC_CON_REEST_COMPLETE; - ul_dcch_msg.msg.rrc_con_reest_complete.rrc_transaction_id = transaction_id; - liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + + + void rrc::send_con_restablish_complete() { + rrc_log->debug("Preparing RRC Connection Reestablishment Complete\n"); + LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; + + // Prepare ConnectionSetupComplete packet + ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_RRC_CON_REEST_COMPLETE; + ul_dcch_msg.msg.rrc_con_reest_complete.rrc_transaction_id = transaction_id; + liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + byte_buffer_t *pdcp_buf = pool_allocate;; + srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); + pdcp_buf->N_bytes = bit_buf.N_bits / 8; + + state = RRC_STATE_CONNECTED; + rrc_log->console("RRC Connected\n"); + rrc_log->info("Sending RRC Connection Reestablishment Complete\n"); + pdcp->write_sdu(RB_ID_SRB1, pdcp_buf); } - byte_buffer_t *pdcp_buf = pool_allocate;; - srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); - pdcp_buf->N_bytes = bit_buf.N_bits/8; - - state = RRC_STATE_CONNECTED; - rrc_log->console("RRC Connected\n"); - rrc_log->info("Sending RRC Connection Reestablishment Complete\n"); - pdcp->write_sdu(RB_ID_SRB1, pdcp_buf); -} - -void rrc::send_con_setup_complete(byte_buffer_t *nas_msg) -{ - rrc_log->debug("Preparing RRC Connection Setup Complete\n"); - LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; - - // Prepare ConnectionSetupComplete packet - ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_RRC_CON_SETUP_COMPLETE; - ul_dcch_msg.msg.rrc_con_setup_complete.registered_mme_present = false; - ul_dcch_msg.msg.rrc_con_setup_complete.rrc_transaction_id = transaction_id; - ul_dcch_msg.msg.rrc_con_setup_complete.selected_plmn_id = 1; - memcpy(ul_dcch_msg.msg.rrc_con_setup_complete.dedicated_info_nas.msg, nas_msg->msg, nas_msg->N_bytes); - ul_dcch_msg.msg.rrc_con_setup_complete.dedicated_info_nas.N_bytes = nas_msg->N_bytes; - liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + + void rrc::send_con_setup_complete(byte_buffer_t *nas_msg) { + rrc_log->debug("Preparing RRC Connection Setup Complete\n"); + LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; + + // Prepare ConnectionSetupComplete packet + ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_RRC_CON_SETUP_COMPLETE; + ul_dcch_msg.msg.rrc_con_setup_complete.registered_mme_present = false; + ul_dcch_msg.msg.rrc_con_setup_complete.rrc_transaction_id = transaction_id; + ul_dcch_msg.msg.rrc_con_setup_complete.selected_plmn_id = 1; + memcpy(ul_dcch_msg.msg.rrc_con_setup_complete.dedicated_info_nas.msg, nas_msg->msg, nas_msg->N_bytes); + ul_dcch_msg.msg.rrc_con_setup_complete.dedicated_info_nas.N_bytes = nas_msg->N_bytes; + liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + byte_buffer_t *pdcp_buf = pool_allocate;; + srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); + pdcp_buf->N_bytes = bit_buf.N_bits / 8; + pdcp_buf->set_timestamp(); + + state = RRC_STATE_CONNECTED; + rrc_log->console("RRC Connected\n"); + rrc_log->info("Sending RRC Connection Setup Complete\n"); + pdcp->write_sdu(RB_ID_SRB1, pdcp_buf); } - byte_buffer_t *pdcp_buf = pool_allocate;; - srslte_bit_pack_vector(bit_buf.msg, pdcp_buf->msg, bit_buf.N_bits); - pdcp_buf->N_bytes = bit_buf.N_bits/8; - pdcp_buf->set_timestamp(); - - state = RRC_STATE_CONNECTED; - rrc_log->console("RRC Connected\n"); - rrc_log->info("Sending RRC Connection Setup Complete\n"); - pdcp->write_sdu(RB_ID_SRB1, pdcp_buf); -} - -void rrc::send_ul_info_transfer(uint32_t lcid, byte_buffer_t *sdu) -{ - rrc_log->debug("Preparing RX Info Transfer\n"); - LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; - - // Prepare RX INFO packet - ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_UL_INFO_TRANSFER; - ul_dcch_msg.msg.ul_info_transfer.dedicated_info_type = LIBLTE_RRC_UL_INFORMATION_TRANSFER_TYPE_NAS; - memcpy(ul_dcch_msg.msg.ul_info_transfer.dedicated_info.msg, sdu->msg, sdu->N_bytes); - ul_dcch_msg.msg.ul_info_transfer.dedicated_info.N_bytes = sdu->N_bytes; - liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - // Reset and reuse sdu buffer - byte_buffer_t *pdu = sdu; - pdu->reset(); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + + void rrc::send_ul_info_transfer(uint32_t lcid, byte_buffer_t *sdu) { + rrc_log->debug("Preparing RX Info Transfer\n"); + LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; + + // Prepare RX INFO packet + ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_UL_INFO_TRANSFER; + ul_dcch_msg.msg.ul_info_transfer.dedicated_info_type = LIBLTE_RRC_UL_INFORMATION_TRANSFER_TYPE_NAS; + memcpy(ul_dcch_msg.msg.ul_info_transfer.dedicated_info.msg, sdu->msg, sdu->N_bytes); + ul_dcch_msg.msg.ul_info_transfer.dedicated_info.N_bytes = sdu->N_bytes; + liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + // Reset and reuse sdu buffer + byte_buffer_t *pdu = sdu; + pdu->reset(); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); + pdu->N_bytes = bit_buf.N_bits / 8; + pdu->set_timestamp(); + pdu->set_timestamp(); + + rrc_log->info("Sending RX Info Transfer\n"); + pdcp->write_sdu(lcid, pdu); } - srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); - pdu->N_bytes = bit_buf.N_bits/8; - pdu->set_timestamp(); - pdu->set_timestamp(); - - rrc_log->info("Sending RX Info Transfer\n"); - pdcp->write_sdu(lcid, pdu); -} - -void rrc::send_security_mode_complete(uint32_t lcid, byte_buffer_t *pdu) -{ - rrc_log->debug("Preparing Security Mode Complete\n"); - LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; - ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_SECURITY_MODE_COMPLETE; - ul_dcch_msg.msg.security_mode_complete.rrc_transaction_id = transaction_id; - liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + + void rrc::send_security_mode_complete(uint32_t lcid, byte_buffer_t *pdu) { + rrc_log->debug("Preparing Security Mode Complete\n"); + LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; + ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_SECURITY_MODE_COMPLETE; + ul_dcch_msg.msg.security_mode_complete.rrc_transaction_id = transaction_id; + liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); + pdu->N_bytes = bit_buf.N_bits / 8; + pdu->set_timestamp(); + + rrc_log->info("Sending Security Mode Complete\n"); + pdcp->write_sdu(lcid, pdu); } - srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); - pdu->N_bytes = bit_buf.N_bits/8; - pdu->set_timestamp(); - - rrc_log->info("Sending Security Mode Complete\n"); - pdcp->write_sdu(lcid, pdu); -} - -void rrc::send_rrc_con_reconfig_complete(uint32_t lcid, byte_buffer_t *pdu) -{ - rrc_log->debug("Preparing RRC Connection Reconfig Complete\n"); - LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; - - ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_RRC_CON_RECONFIG_COMPLETE; - ul_dcch_msg.msg.rrc_con_reconfig_complete.rrc_transaction_id = transaction_id; - liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + + void rrc::send_rrc_con_reconfig_complete(uint32_t lcid, byte_buffer_t *pdu) { + rrc_log->debug("Preparing RRC Connection Reconfig Complete\n"); + LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; + + ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_RRC_CON_RECONFIG_COMPLETE; + ul_dcch_msg.msg.rrc_con_reconfig_complete.rrc_transaction_id = transaction_id; + liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); + pdu->N_bytes = bit_buf.N_bits / 8; + pdu->set_timestamp(); + + rrc_log->info("Sending RRC Connection Reconfig Complete\n"); + pdcp->write_sdu(lcid, pdu); } - srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); - pdu->N_bytes = bit_buf.N_bits/8; - pdu->set_timestamp(); - - rrc_log->info("Sending RRC Connection Reconfig Complete\n"); - pdcp->write_sdu(lcid, pdu); -} - -void rrc::enable_capabilities() -{ - bool enable_ul_64 = ue_category>=5 && current_cell->sib2.rr_config_common_sib.pusch_cnfg.enable_64_qam; - rrc_log->info("%s 64QAM PUSCH\n", enable_ul_64?"Enabling":"Disabling"); - phy->set_config_64qam_en(enable_ul_64); -} - -void rrc::send_rrc_ue_cap_info(uint32_t lcid, byte_buffer_t *pdu) -{ - rrc_log->debug("Preparing UE Capability Info\n"); - LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; - - ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_UE_CAPABILITY_INFO; - ul_dcch_msg.msg.ue_capability_info.rrc_transaction_id = transaction_id; - - LIBLTE_RRC_UE_CAPABILITY_INFORMATION_STRUCT *info = &ul_dcch_msg.msg.ue_capability_info; - info->N_ue_caps = 1; - info->ue_capability_rat[0].rat_type = LIBLTE_RRC_RAT_TYPE_EUTRA; - - LIBLTE_RRC_UE_EUTRA_CAPABILITY_STRUCT *cap = &info->ue_capability_rat[0].eutra_capability; - cap->access_stratum_release = LIBLTE_RRC_ACCESS_STRATUM_RELEASE_REL8; - cap->ue_category = ue_category; - - cap->pdcp_params.max_rohc_ctxts_present = false; - cap->pdcp_params.supported_rohc_profiles[0] = false; - cap->pdcp_params.supported_rohc_profiles[1] = false; - cap->pdcp_params.supported_rohc_profiles[2] = false; - cap->pdcp_params.supported_rohc_profiles[3] = false; - cap->pdcp_params.supported_rohc_profiles[4] = false; - cap->pdcp_params.supported_rohc_profiles[5] = false; - cap->pdcp_params.supported_rohc_profiles[6] = false; - cap->pdcp_params.supported_rohc_profiles[7] = false; - cap->pdcp_params.supported_rohc_profiles[8] = false; - - cap->phy_params.specific_ref_sigs_supported = false; - cap->phy_params.tx_antenna_selection_supported = false; - - //TODO: Generate this from user input? - cap->rf_params.N_supported_band_eutras = 3; - cap->rf_params.supported_band_eutra[0].band_eutra = 3; - cap->rf_params.supported_band_eutra[0].half_duplex = false; - cap->rf_params.supported_band_eutra[1].band_eutra = 7; - cap->rf_params.supported_band_eutra[1].half_duplex = false; - cap->rf_params.supported_band_eutra[2].band_eutra = 20; - cap->rf_params.supported_band_eutra[2].half_duplex = false; - - cap->meas_params.N_band_list_eutra = 3; - cap->meas_params.band_list_eutra[0].N_inter_freq_need_for_gaps = 3; - cap->meas_params.band_list_eutra[0].inter_freq_need_for_gaps[0] = true; - cap->meas_params.band_list_eutra[0].inter_freq_need_for_gaps[1] = true; - cap->meas_params.band_list_eutra[0].inter_freq_need_for_gaps[2] = true; - cap->meas_params.band_list_eutra[1].N_inter_freq_need_for_gaps = 3; - cap->meas_params.band_list_eutra[1].inter_freq_need_for_gaps[0] = true; - cap->meas_params.band_list_eutra[1].inter_freq_need_for_gaps[1] = true; - cap->meas_params.band_list_eutra[1].inter_freq_need_for_gaps[2] = true; - cap->meas_params.band_list_eutra[2].N_inter_freq_need_for_gaps = 3; - cap->meas_params.band_list_eutra[2].inter_freq_need_for_gaps[0] = true; - cap->meas_params.band_list_eutra[2].inter_freq_need_for_gaps[1] = true; - cap->meas_params.band_list_eutra[2].inter_freq_need_for_gaps[2] = true; - - cap->feature_group_indicator_present = true; - cap->feature_group_indicator = 0x62001000; - cap->inter_rat_params.utra_fdd_present = false; - cap->inter_rat_params.utra_tdd128_present = false; - cap->inter_rat_params.utra_tdd384_present = false; - cap->inter_rat_params.utra_tdd768_present = false; - cap->inter_rat_params.geran_present = false; - cap->inter_rat_params.cdma2000_hrpd_present = false; - cap->inter_rat_params.cdma2000_1xrtt_present = false; - - liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT*)&bit_buf); - - // Byte align and pack the message bits for PDCP - if((bit_buf.N_bits % 8) != 0) - { - for(uint32_t i=0; i<8-(bit_buf.N_bits % 8); i++) - bit_buf.msg[bit_buf.N_bits + i] = 0; - bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + + void rrc::enable_capabilities() { + bool enable_ul_64 = ue_category >= 5 && current_cell->sib2.rr_config_common_sib.pusch_cnfg.enable_64_qam; + rrc_log->info("%s 64QAM PUSCH\n", enable_ul_64 ? "Enabling" : "Disabling"); + phy->set_config_64qam_en(enable_ul_64); } - srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); - pdu->N_bytes = bit_buf.N_bits/8; - pdu->set_timestamp(); - rrc_log->info("Sending UE Capability Info\n"); - pdcp->write_sdu(lcid, pdu); -} + void rrc::send_rrc_ue_cap_info(uint32_t lcid, byte_buffer_t *pdu) { + rrc_log->debug("Preparing UE Capability Info\n"); + LIBLTE_RRC_UL_DCCH_MSG_STRUCT ul_dcch_msg; + + ul_dcch_msg.msg_type = LIBLTE_RRC_UL_DCCH_MSG_TYPE_UE_CAPABILITY_INFO; + ul_dcch_msg.msg.ue_capability_info.rrc_transaction_id = transaction_id; + + LIBLTE_RRC_UE_CAPABILITY_INFORMATION_STRUCT *info = &ul_dcch_msg.msg.ue_capability_info; + info->N_ue_caps = 1; + info->ue_capability_rat[0].rat_type = LIBLTE_RRC_RAT_TYPE_EUTRA; + + LIBLTE_RRC_UE_EUTRA_CAPABILITY_STRUCT *cap = &info->ue_capability_rat[0].eutra_capability; + cap->access_stratum_release = LIBLTE_RRC_ACCESS_STRATUM_RELEASE_REL8; + cap->ue_category = ue_category; + + cap->pdcp_params.max_rohc_ctxts_present = false; + cap->pdcp_params.supported_rohc_profiles[0] = false; + cap->pdcp_params.supported_rohc_profiles[1] = false; + cap->pdcp_params.supported_rohc_profiles[2] = false; + cap->pdcp_params.supported_rohc_profiles[3] = false; + cap->pdcp_params.supported_rohc_profiles[4] = false; + cap->pdcp_params.supported_rohc_profiles[5] = false; + cap->pdcp_params.supported_rohc_profiles[6] = false; + cap->pdcp_params.supported_rohc_profiles[7] = false; + cap->pdcp_params.supported_rohc_profiles[8] = false; + + cap->phy_params.specific_ref_sigs_supported = false; + cap->phy_params.tx_antenna_selection_supported = false; + + //TODO: Generate this from user input? + cap->rf_params.N_supported_band_eutras = 3; + cap->rf_params.supported_band_eutra[0].band_eutra = 3; + cap->rf_params.supported_band_eutra[0].half_duplex = false; + cap->rf_params.supported_band_eutra[1].band_eutra = 7; + cap->rf_params.supported_band_eutra[1].half_duplex = false; + cap->rf_params.supported_band_eutra[2].band_eutra = 20; + cap->rf_params.supported_band_eutra[2].half_duplex = false; + + cap->meas_params.N_band_list_eutra = 3; + cap->meas_params.band_list_eutra[0].N_inter_freq_need_for_gaps = 3; + cap->meas_params.band_list_eutra[0].inter_freq_need_for_gaps[0] = true; + cap->meas_params.band_list_eutra[0].inter_freq_need_for_gaps[1] = true; + cap->meas_params.band_list_eutra[0].inter_freq_need_for_gaps[2] = true; + cap->meas_params.band_list_eutra[1].N_inter_freq_need_for_gaps = 3; + cap->meas_params.band_list_eutra[1].inter_freq_need_for_gaps[0] = true; + cap->meas_params.band_list_eutra[1].inter_freq_need_for_gaps[1] = true; + cap->meas_params.band_list_eutra[1].inter_freq_need_for_gaps[2] = true; + cap->meas_params.band_list_eutra[2].N_inter_freq_need_for_gaps = 3; + cap->meas_params.band_list_eutra[2].inter_freq_need_for_gaps[0] = true; + cap->meas_params.band_list_eutra[2].inter_freq_need_for_gaps[1] = true; + cap->meas_params.band_list_eutra[2].inter_freq_need_for_gaps[2] = true; + + cap->feature_group_indicator_present = true; + cap->feature_group_indicator = 0x62001000; + cap->inter_rat_params.utra_fdd_present = false; + cap->inter_rat_params.utra_tdd128_present = false; + cap->inter_rat_params.utra_tdd384_present = false; + cap->inter_rat_params.utra_tdd768_present = false; + cap->inter_rat_params.geran_present = false; + cap->inter_rat_params.cdma2000_hrpd_present = false; + cap->inter_rat_params.cdma2000_1xrtt_present = false; + + liblte_rrc_pack_ul_dcch_msg(&ul_dcch_msg, (LIBLTE_BIT_MSG_STRUCT *) &bit_buf); + + // Byte align and pack the message bits for PDCP + if ((bit_buf.N_bits % 8) != 0) { + for (uint32_t i = 0; i < 8 - (bit_buf.N_bits % 8); i++) + bit_buf.msg[bit_buf.N_bits + i] = 0; + bit_buf.N_bits += 8 - (bit_buf.N_bits % 8); + } + srslte_bit_pack_vector(bit_buf.msg, pdu->msg, bit_buf.N_bits); + pdu->N_bytes = bit_buf.N_bits / 8; + pdu->set_timestamp(); + + rrc_log->info("Sending UE Capability Info\n"); + pdcp->write_sdu(lcid, pdu); + } /******************************************************************************* Parsers *******************************************************************************/ -void rrc::parse_dl_ccch(byte_buffer_t *pdu) -{ - srslte_bit_unpack_vector(pdu->msg, bit_buf.msg, pdu->N_bytes*8); - bit_buf.N_bits = pdu->N_bytes*8; - pool->deallocate(pdu); - bzero(&dl_ccch_msg, sizeof(LIBLTE_RRC_DL_CCCH_MSG_STRUCT)); - liblte_rrc_unpack_dl_ccch_msg((LIBLTE_BIT_MSG_STRUCT*)&bit_buf, &dl_ccch_msg); - - rrc_log->info("SRB0 - Received %s\n", - liblte_rrc_dl_ccch_msg_type_text[dl_ccch_msg.msg_type]); - - switch(dl_ccch_msg.msg_type) - { - case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_REJ: - rrc_log->info("Connection Reject received. Wait time: %d\n", - dl_ccch_msg.msg.rrc_con_rej.wait_time); - state = RRC_STATE_IDLE; - break; - case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_SETUP: - rrc_log->info("Connection Setup received\n"); - transaction_id = dl_ccch_msg.msg.rrc_con_setup.rrc_transaction_id; - handle_con_setup(&dl_ccch_msg.msg.rrc_con_setup); - rrc_log->info("Notifying NAS of connection setup\n"); - nas->notify_connection_setup(); - break; - case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_REEST: - rrc_log->info("Connection Reestablishment received\n"); - rrc_log->console("Reestablishment OK\n"); - transaction_id = dl_ccch_msg.msg.rrc_con_reest.rrc_transaction_id; - handle_con_reest(&dl_ccch_msg.msg.rrc_con_reest); - break; - case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_REEST_REJ: - rrc_log->info("Connection Reestablishment Reject received\n"); - rrc_log->console("Reestablishment Reject\n"); - usleep(50000); - rrc_connection_release(); - break; - default: - break; + void rrc::parse_dl_ccch(byte_buffer_t *pdu) { + srslte_bit_unpack_vector(pdu->msg, bit_buf.msg, pdu->N_bytes * 8); + bit_buf.N_bits = pdu->N_bytes * 8; + pool->deallocate(pdu); + bzero(&dl_ccch_msg, sizeof(LIBLTE_RRC_DL_CCCH_MSG_STRUCT)); + liblte_rrc_unpack_dl_ccch_msg((LIBLTE_BIT_MSG_STRUCT *) &bit_buf, &dl_ccch_msg); + + rrc_log->info("SRB0 - Received %s\n", + liblte_rrc_dl_ccch_msg_type_text[dl_ccch_msg.msg_type]); + + switch (dl_ccch_msg.msg_type) { + case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_REJ: + rrc_log->info("Connection Reject received. Wait time: %d\n", + dl_ccch_msg.msg.rrc_con_rej.wait_time); + state = RRC_STATE_IDLE; + break; + case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_SETUP: + rrc_log->info("Connection Setup received\n"); + transaction_id = dl_ccch_msg.msg.rrc_con_setup.rrc_transaction_id; + handle_con_setup(&dl_ccch_msg.msg.rrc_con_setup); + rrc_log->info("Notifying NAS of connection setup\n"); + nas->notify_connection_setup(); + break; + case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_REEST: + rrc_log->info("Connection Reestablishment received\n"); + rrc_log->console("Reestablishment OK\n"); + transaction_id = dl_ccch_msg.msg.rrc_con_reest.rrc_transaction_id; + handle_con_reest(&dl_ccch_msg.msg.rrc_con_reest); + break; + case LIBLTE_RRC_DL_CCCH_MSG_TYPE_RRC_CON_REEST_REJ: + rrc_log->info("Connection Reestablishment Reject received\n"); + rrc_log->console("Reestablishment Reject\n"); + usleep(50000); + rrc_connection_release(); + break; + default: + break; + } } -} - -void rrc::parse_dl_dcch(uint32_t lcid, byte_buffer_t *pdu) -{ - srslte_bit_unpack_vector(pdu->msg, bit_buf.msg, pdu->N_bytes*8); - bit_buf.N_bits = pdu->N_bytes*8; - liblte_rrc_unpack_dl_dcch_msg((LIBLTE_BIT_MSG_STRUCT*)&bit_buf, &dl_dcch_msg); - - rrc_log->info("%s - Received %s\n", - rb_id_text[lcid], - liblte_rrc_dl_dcch_msg_type_text[dl_dcch_msg.msg_type]); - - // Reset and reuse pdu buffer if possible - pdu->reset(); - - switch(dl_dcch_msg.msg_type) - { - case LIBLTE_RRC_DL_DCCH_MSG_TYPE_DL_INFO_TRANSFER: - memcpy(pdu->msg, dl_dcch_msg.msg.dl_info_transfer.dedicated_info.msg, dl_dcch_msg.msg.dl_info_transfer.dedicated_info.N_bytes); - pdu->N_bytes = dl_dcch_msg.msg.dl_info_transfer.dedicated_info.N_bytes; - nas->write_pdu(lcid, pdu); - break; - case LIBLTE_RRC_DL_DCCH_MSG_TYPE_SECURITY_MODE_COMMAND: - transaction_id = dl_dcch_msg.msg.security_mode_cmd.rrc_transaction_id; - - cipher_algo = (CIPHERING_ALGORITHM_ID_ENUM)dl_dcch_msg.msg.security_mode_cmd.sec_algs.cipher_alg; - integ_algo = (INTEGRITY_ALGORITHM_ID_ENUM)dl_dcch_msg.msg.security_mode_cmd.sec_algs.int_alg; - - // Configure PDCP for security - usim->generate_as_keys(nas->get_ul_count(), k_rrc_enc, k_rrc_int, k_up_enc, k_up_int, cipher_algo, integ_algo); - pdcp->config_security(lcid, k_rrc_enc, k_rrc_int, cipher_algo, integ_algo); - send_security_mode_complete(lcid, pdu); - break; - case LIBLTE_RRC_DL_DCCH_MSG_TYPE_RRC_CON_RECONFIG: - transaction_id = dl_dcch_msg.msg.rrc_con_reconfig.rrc_transaction_id; - handle_rrc_con_reconfig(lcid, &dl_dcch_msg.msg.rrc_con_reconfig, pdu); - break; - case LIBLTE_RRC_DL_DCCH_MSG_TYPE_UE_CAPABILITY_ENQUIRY: - transaction_id = dl_dcch_msg.msg.ue_cap_enquiry.rrc_transaction_id; - for(uint32_t i=0; imsg, bit_buf.msg, pdu->N_bytes * 8); + bit_buf.N_bits = pdu->N_bytes * 8; + liblte_rrc_unpack_dl_dcch_msg((LIBLTE_BIT_MSG_STRUCT *) &bit_buf, &dl_dcch_msg); + + rrc_log->info("%s - Received %s\n", + rb_id_text[lcid], + liblte_rrc_dl_dcch_msg_type_text[dl_dcch_msg.msg_type]); + + // Reset and reuse pdu buffer if possible + pdu->reset(); + + switch (dl_dcch_msg.msg_type) { + case LIBLTE_RRC_DL_DCCH_MSG_TYPE_DL_INFO_TRANSFER: + memcpy(pdu->msg, dl_dcch_msg.msg.dl_info_transfer.dedicated_info.msg, + dl_dcch_msg.msg.dl_info_transfer.dedicated_info.N_bytes); + pdu->N_bytes = dl_dcch_msg.msg.dl_info_transfer.dedicated_info.N_bytes; + nas->write_pdu(lcid, pdu); + break; + case LIBLTE_RRC_DL_DCCH_MSG_TYPE_SECURITY_MODE_COMMAND: + transaction_id = dl_dcch_msg.msg.security_mode_cmd.rrc_transaction_id; + + cipher_algo = (CIPHERING_ALGORITHM_ID_ENUM) dl_dcch_msg.msg.security_mode_cmd.sec_algs.cipher_alg; + integ_algo = (INTEGRITY_ALGORITHM_ID_ENUM) dl_dcch_msg.msg.security_mode_cmd.sec_algs.int_alg; + + // Configure PDCP for security + usim->generate_as_keys(nas->get_ul_count(), k_rrc_enc, k_rrc_int, k_up_enc, k_up_int, cipher_algo, integ_algo); + pdcp->config_security(lcid, k_rrc_enc, k_rrc_int, cipher_algo, integ_algo); + send_security_mode_complete(lcid, pdu); + break; + case LIBLTE_RRC_DL_DCCH_MSG_TYPE_RRC_CON_RECONFIG: + transaction_id = dl_dcch_msg.msg.rrc_con_reconfig.rrc_transaction_id; + handle_rrc_con_reconfig(lcid, &dl_dcch_msg.msg.rrc_con_reconfig, pdu); + break; + case LIBLTE_RRC_DL_DCCH_MSG_TYPE_UE_CAPABILITY_ENQUIRY: + transaction_id = dl_dcch_msg.msg.ue_cap_enquiry.rrc_transaction_id; + for (uint32_t i = 0; i < dl_dcch_msg.msg.ue_cap_enquiry.N_ue_cap_reqs; i++) { + if (LIBLTE_RRC_RAT_TYPE_EUTRA == dl_dcch_msg.msg.ue_cap_enquiry.ue_capability_request[i]) { + send_rrc_ue_cap_info(lcid, pdu); + break; + } + } + break; + case LIBLTE_RRC_DL_DCCH_MSG_TYPE_RRC_CON_RELEASE: + rrc_connection_release(); + break; + default: break; - } } - break; - case LIBLTE_RRC_DL_DCCH_MSG_TYPE_RRC_CON_RELEASE: - rrc_connection_release(); - break; - default: - break; } -} /******************************************************************************* Timer expiration callback *******************************************************************************/ -void rrc::timer_expired(uint32_t timeout_id) -{ - if (timeout_id == t310) { - rrc_log->info("Timer T310 expired: Radio Link Failure\n"); - radio_link_failure(); - } else if (timeout_id == t311) { - rrc_log->info("Timer T311 expired: Going to RRC IDLE\n"); - rrc_connection_release(); - } else if (timeout_id == t301) { - rrc_log->info("Timer T301 expired: Going to RRC IDLE\n"); - rrc_connection_release(); - } else if (timeout_id == safe_reset_timer) { - reset_ue(); - } else { - rrc_log->error("Timeout from unknown timer id %d\n", timeout_id); + void rrc::timer_expired(uint32_t timeout_id) { + if (timeout_id == t310) { + rrc_log->info("Timer T310 expired: Radio Link Failure\n"); + radio_link_failure(); + } else if (timeout_id == t311) { + rrc_log->info("Timer T311 expired: Going to RRC IDLE\n"); + rrc_connection_release(); + } else if (timeout_id == t301) { + rrc_log->info("Timer T301 expired: Going to RRC IDLE\n"); + rrc_connection_release(); + } else if (timeout_id == safe_reset_timer) { + reset_ue(); + } else { + rrc_log->error("Timeout from unknown timer id %d\n", timeout_id); + } } -} /******************************************************************************* Helpers *******************************************************************************/ -void rrc::reset_ue() { - phy->reset(); - mac->reset(); - pdcp->reset(); - rlc->reset(); - mac->pcch_start_rx(); - mac_timers->get(safe_reset_timer)->stop(); - mac_timers->get(safe_reset_timer)->reset(); - rrc_log->console("RRC Connection released.\n"); -} - -void rrc::rrc_connection_release() { - pthread_mutex_lock(&mutex); - drb_up = false; - state = RRC_STATE_IDLE; - set_phy_default(); - set_mac_default(); - mac_timers->get(t311)->run(); - mac_timers->get(t310)->stop(); - mac_timers->get(t311)->stop(); - mac_timers->get(safe_reset_timer)->stop(); - mac_timers->get(safe_reset_timer)->reset(); - mac_timers->get(safe_reset_timer)->run(); - pthread_mutex_unlock(&mutex); -} - -void rrc::test_con_restablishment() -{ - printf("Testing connection Reestablishment\n"); - send_con_restablish_request(); -} + void rrc::reset_ue() { + phy->reset(); + mac->reset(); + pdcp->reset(); + rlc->reset(); + mac->pcch_start_rx(); + mac_timers->get(safe_reset_timer)->stop(); + mac_timers->get(safe_reset_timer)->reset(); + rrc_log->console("RRC Connection released.\n"); + } -/* Detection of radio link failure (5.3.11.3) */ -void rrc::radio_link_failure() { - // TODO: Generate and store failure report - - rrc_log->warning("Detected Radio-Link Failure\n"); - rrc_log->console("Warning: Detected Radio-Link Failure\n"); - if (state != RRC_STATE_CONNECTED) { - rrc_connection_release(); - } else { + void rrc::rrc_connection_release() { + pthread_mutex_lock(&mutex); + drb_up = false; + state = RRC_STATE_IDLE; + set_phy_default(); + set_mac_default(); + mac_timers->get(t311)->run(); + mac_timers->get(t310)->stop(); + mac_timers->get(t311)->stop(); + mac_timers->get(safe_reset_timer)->stop(); + mac_timers->get(safe_reset_timer)->reset(); + mac_timers->get(safe_reset_timer)->run(); + pthread_mutex_unlock(&mutex); + } + + void rrc::test_con_restablishment() { + printf("Testing connection Reestablishment\n"); send_con_restablish_request(); } -} +/* Detection of radio link failure (5.3.11.3) */ + void rrc::radio_link_failure() { + // TODO: Generate and store failure report -// Determine SI messages scheduling as in 36.331 5.2.3 Acquisition of an SI message -uint32_t rrc::sib_start_tti(uint32_t tti, uint32_t period, uint32_t x) { - return (period*10*(1+tti/(period*10))+x)%10240; // the 1 means next opportunity -} - -void rrc::apply_sib2_configs(LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_2_STRUCT *sib2) -{ - - // Apply RACH timeAlginmentTimer configuration - mac_interface_rrc::mac_cfg_t cfg; - mac->get_config(&cfg); - cfg.main.time_alignment_timer = sib2->time_alignment_timer; - memcpy(&cfg.rach, &sib2->rr_config_common_sib.rach_cnfg, sizeof(LIBLTE_RRC_RACH_CONFIG_COMMON_STRUCT)); - cfg.prach_config_index = sib2->rr_config_common_sib.prach_cnfg.root_sequence_index; - mac->set_config(&cfg); - - rrc_log->info("Set RACH ConfigCommon: NofPreambles=%d, ResponseWindow=%d, ContentionResolutionTimer=%d ms\n", - liblte_rrc_number_of_ra_preambles_num[sib2->rr_config_common_sib.rach_cnfg.num_ra_preambles], - liblte_rrc_ra_response_window_size_num[sib2->rr_config_common_sib.rach_cnfg.ra_resp_win_size], - liblte_rrc_mac_contention_resolution_timer_num[sib2->rr_config_common_sib.rach_cnfg.mac_con_res_timer]); - - // Apply PHY RR Config Common - phy_interface_rrc::phy_cfg_common_t common; - memcpy(&common.pdsch_cnfg, &sib2->rr_config_common_sib.pdsch_cnfg, sizeof(LIBLTE_RRC_PDSCH_CONFIG_COMMON_STRUCT)); - memcpy(&common.pusch_cnfg, &sib2->rr_config_common_sib.pusch_cnfg, sizeof(LIBLTE_RRC_PUSCH_CONFIG_COMMON_STRUCT)); - memcpy(&common.pucch_cnfg, &sib2->rr_config_common_sib.pucch_cnfg, sizeof(LIBLTE_RRC_PUCCH_CONFIG_COMMON_STRUCT)); - memcpy(&common.ul_pwr_ctrl, &sib2->rr_config_common_sib.ul_pwr_ctrl, sizeof(LIBLTE_RRC_UL_POWER_CONTROL_COMMON_STRUCT)); - memcpy(&common.prach_cnfg, &sib2->rr_config_common_sib.prach_cnfg, sizeof(LIBLTE_RRC_PRACH_CONFIG_SIB_STRUCT)); - if (sib2->rr_config_common_sib.srs_ul_cnfg.present) { - memcpy(&common.srs_ul_cnfg, &sib2->rr_config_common_sib.srs_ul_cnfg, sizeof(LIBLTE_RRC_SRS_UL_CONFIG_COMMON_STRUCT)); - } else { - // default is release - common.srs_ul_cnfg.present = false; + rrc_log->warning("Detected Radio-Link Failure\n"); + rrc_log->console("Warning: Detected Radio-Link Failure\n"); + if (state != RRC_STATE_CONNECTED) { + rrc_connection_release(); + } else { + send_con_restablish_request(); + } } - phy->set_config_common(&common); - - phy->configure_ul_params(); - - rrc_log->info("Set PUSCH ConfigCommon: HopOffset=%d, RSGroup=%d, RSNcs=%d, N_sb=%d\n", - sib2->rr_config_common_sib.pusch_cnfg.pusch_hopping_offset, - sib2->rr_config_common_sib.pusch_cnfg.ul_rs.group_assignment_pusch, - sib2->rr_config_common_sib.pusch_cnfg.ul_rs.cyclic_shift, - sib2->rr_config_common_sib.pusch_cnfg.n_sb); - - rrc_log->info("Set PUCCH ConfigCommon: DeltaShift=%d, CyclicShift=%d, N1=%d, NRB=%d\n", - liblte_rrc_delta_pucch_shift_num[sib2->rr_config_common_sib.pucch_cnfg.delta_pucch_shift], - sib2->rr_config_common_sib.pucch_cnfg.n_cs_an, - sib2->rr_config_common_sib.pucch_cnfg.n1_pucch_an, - sib2->rr_config_common_sib.pucch_cnfg.n_rb_cqi); - - rrc_log->info("Set PRACH ConfigCommon: SeqIdx=%d, HS=%s, FreqOffset=%d, ZC=%d, ConfigIndex=%d\n", - sib2->rr_config_common_sib.prach_cnfg.root_sequence_index, - sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.high_speed_flag?"yes":"no", - sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.prach_freq_offset, - sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.zero_correlation_zone_config, - sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.prach_config_index); - - rrc_log->info("Set SRS ConfigCommon: BW-Configuration=%d, SF-Configuration=%d, ACKNACK=%s\n", - liblte_rrc_srs_bw_config_num[sib2->rr_config_common_sib.srs_ul_cnfg.bw_cnfg], - liblte_rrc_srs_subfr_config_num[sib2->rr_config_common_sib.srs_ul_cnfg.subfr_cnfg], - sib2->rr_config_common_sib.srs_ul_cnfg.ack_nack_simul_tx?"yes":"no"); - - mac_timers->get(t301)->set(this, liblte_rrc_t301_num[sib2->ue_timers_and_constants.t301]); - mac_timers->get(t310)->set(this, liblte_rrc_t310_num[sib2->ue_timers_and_constants.t310]); - mac_timers->get(t311)->set(this, liblte_rrc_t311_num[sib2->ue_timers_and_constants.t311]); - N310 = liblte_rrc_n310_num[sib2->ue_timers_and_constants.n310]; - N311 = liblte_rrc_n311_num[sib2->ue_timers_and_constants.n311]; - - rrc_log->info("Set Constants and Timers: N310=%d, N311=%d, t301=%d, t310=%d, t311=%d\n", - N310, N311, mac_timers->get(t301)->get_timeout(), - mac_timers->get(t310)->get_timeout(), mac_timers->get(t311)->get_timeout()); - -} - // Go through all information elements and apply defaults (9.2.4) if not defined -void rrc::apply_phy_config_dedicated(LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT *phy_cnfg, bool apply_defaults) -{ - // Get current configuration - LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT *current_cfg; - phy_interface_rrc::phy_cfg_t c; - phy->get_config(&c); - current_cfg = &c.dedicated; - - if(phy_cnfg->pucch_cnfg_ded_present) { - memcpy(¤t_cfg->pucch_cnfg_ded, &phy_cnfg->pucch_cnfg_ded, sizeof(LIBLTE_RRC_PUCCH_CONFIG_DEDICATED_STRUCT)); - } else if (apply_defaults) { - current_cfg->pucch_cnfg_ded.tdd_ack_nack_feedback_mode = LIBLTE_RRC_TDD_ACK_NACK_FEEDBACK_MODE_BUNDLING; - current_cfg->pucch_cnfg_ded.ack_nack_repetition_setup_present = false; - } - if(phy_cnfg->pusch_cnfg_ded_present) { - memcpy(¤t_cfg->pusch_cnfg_ded, &phy_cnfg->pusch_cnfg_ded, sizeof(LIBLTE_RRC_PUSCH_CONFIG_DEDICATED_STRUCT)); - } else if (apply_defaults) { - current_cfg->pusch_cnfg_ded.beta_offset_ack_idx = 10; - current_cfg->pusch_cnfg_ded.beta_offset_ri_idx = 12; - current_cfg->pusch_cnfg_ded.beta_offset_cqi_idx = 15; - } - if(phy_cnfg->ul_pwr_ctrl_ded_present) { - memcpy(¤t_cfg->ul_pwr_ctrl_ded, &phy_cnfg->ul_pwr_ctrl_ded, sizeof(LIBLTE_RRC_UL_POWER_CONTROL_DEDICATED_STRUCT)); - } else if (apply_defaults) { - current_cfg->ul_pwr_ctrl_ded.p0_ue_pusch = 0; - current_cfg->ul_pwr_ctrl_ded.delta_mcs_en = LIBLTE_RRC_DELTA_MCS_ENABLED_EN0; - current_cfg->ul_pwr_ctrl_ded.accumulation_en = true; - current_cfg->ul_pwr_ctrl_ded.p0_ue_pucch = 0; - current_cfg->ul_pwr_ctrl_ded.p_srs_offset = 7; - current_cfg->ul_pwr_ctrl_ded.filter_coeff = LIBLTE_RRC_FILTER_COEFFICIENT_FC4; - current_cfg->ul_pwr_ctrl_ded.filter_coeff_present = true; - } - if(phy_cnfg->tpc_pdcch_cnfg_pucch_present) { - memcpy(¤t_cfg->tpc_pdcch_cnfg_pucch, &phy_cnfg->tpc_pdcch_cnfg_pucch, sizeof(LIBLTE_RRC_TPC_PDCCH_CONFIG_STRUCT)); - } else if (apply_defaults) { - current_cfg->tpc_pdcch_cnfg_pucch.setup_present = false; + +// Determine SI messages scheduling as in 36.331 5.2.3 Acquisition of an SI message + uint32_t rrc::sib_start_tti(uint32_t tti, uint32_t period, uint32_t x) { + return (period * 10 * (1 + tti / (period * 10)) + x) % 10240; // the 1 means next opportunity } - if(phy_cnfg->tpc_pdcch_cnfg_pusch_present) { - memcpy(¤t_cfg->tpc_pdcch_cnfg_pusch, &phy_cnfg->tpc_pdcch_cnfg_pusch, sizeof(LIBLTE_RRC_TPC_PDCCH_CONFIG_STRUCT)); - } else { - current_cfg->tpc_pdcch_cnfg_pusch.setup_present = false; + + void rrc::apply_sib2_configs(LIBLTE_RRC_SYS_INFO_BLOCK_TYPE_2_STRUCT *sib2) { + + // Apply RACH timeAlginmentTimer configuration + mac_interface_rrc::mac_cfg_t cfg; + mac->get_config(&cfg); + cfg.main.time_alignment_timer = sib2->time_alignment_timer; + memcpy(&cfg.rach, &sib2->rr_config_common_sib.rach_cnfg, sizeof(LIBLTE_RRC_RACH_CONFIG_COMMON_STRUCT)); + cfg.prach_config_index = sib2->rr_config_common_sib.prach_cnfg.root_sequence_index; + mac->set_config(&cfg); + + rrc_log->info("Set RACH ConfigCommon: NofPreambles=%d, ResponseWindow=%d, ContentionResolutionTimer=%d ms\n", + liblte_rrc_number_of_ra_preambles_num[sib2->rr_config_common_sib.rach_cnfg.num_ra_preambles], + liblte_rrc_ra_response_window_size_num[sib2->rr_config_common_sib.rach_cnfg.ra_resp_win_size], + liblte_rrc_mac_contention_resolution_timer_num[sib2->rr_config_common_sib.rach_cnfg.mac_con_res_timer]); + + // Apply PHY RR Config Common + phy_interface_rrc::phy_cfg_common_t common; + memcpy(&common.pdsch_cnfg, &sib2->rr_config_common_sib.pdsch_cnfg, sizeof(LIBLTE_RRC_PDSCH_CONFIG_COMMON_STRUCT)); + memcpy(&common.pusch_cnfg, &sib2->rr_config_common_sib.pusch_cnfg, sizeof(LIBLTE_RRC_PUSCH_CONFIG_COMMON_STRUCT)); + memcpy(&common.pucch_cnfg, &sib2->rr_config_common_sib.pucch_cnfg, sizeof(LIBLTE_RRC_PUCCH_CONFIG_COMMON_STRUCT)); + memcpy(&common.ul_pwr_ctrl, &sib2->rr_config_common_sib.ul_pwr_ctrl, + sizeof(LIBLTE_RRC_UL_POWER_CONTROL_COMMON_STRUCT)); + memcpy(&common.prach_cnfg, &sib2->rr_config_common_sib.prach_cnfg, sizeof(LIBLTE_RRC_PRACH_CONFIG_SIB_STRUCT)); + if (sib2->rr_config_common_sib.srs_ul_cnfg.present) { + memcpy(&common.srs_ul_cnfg, &sib2->rr_config_common_sib.srs_ul_cnfg, + sizeof(LIBLTE_RRC_SRS_UL_CONFIG_COMMON_STRUCT)); + } else { + // default is release + common.srs_ul_cnfg.present = false; + } + phy->set_config_common(&common); + + phy->configure_ul_params(); + + rrc_log->info("Set PUSCH ConfigCommon: HopOffset=%d, RSGroup=%d, RSNcs=%d, N_sb=%d\n", + sib2->rr_config_common_sib.pusch_cnfg.pusch_hopping_offset, + sib2->rr_config_common_sib.pusch_cnfg.ul_rs.group_assignment_pusch, + sib2->rr_config_common_sib.pusch_cnfg.ul_rs.cyclic_shift, + sib2->rr_config_common_sib.pusch_cnfg.n_sb); + + rrc_log->info("Set PUCCH ConfigCommon: DeltaShift=%d, CyclicShift=%d, N1=%d, NRB=%d\n", + liblte_rrc_delta_pucch_shift_num[sib2->rr_config_common_sib.pucch_cnfg.delta_pucch_shift], + sib2->rr_config_common_sib.pucch_cnfg.n_cs_an, + sib2->rr_config_common_sib.pucch_cnfg.n1_pucch_an, + sib2->rr_config_common_sib.pucch_cnfg.n_rb_cqi); + + rrc_log->info("Set PRACH ConfigCommon: SeqIdx=%d, HS=%s, FreqOffset=%d, ZC=%d, ConfigIndex=%d\n", + sib2->rr_config_common_sib.prach_cnfg.root_sequence_index, + sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.high_speed_flag ? "yes" : "no", + sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.prach_freq_offset, + sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.zero_correlation_zone_config, + sib2->rr_config_common_sib.prach_cnfg.prach_cnfg_info.prach_config_index); + + rrc_log->info("Set SRS ConfigCommon: BW-Configuration=%d, SF-Configuration=%d, ACKNACK=%s\n", + liblte_rrc_srs_bw_config_num[sib2->rr_config_common_sib.srs_ul_cnfg.bw_cnfg], + liblte_rrc_srs_subfr_config_num[sib2->rr_config_common_sib.srs_ul_cnfg.subfr_cnfg], + sib2->rr_config_common_sib.srs_ul_cnfg.ack_nack_simul_tx ? "yes" : "no"); + + mac_timers->get(t301)->set(this, liblte_rrc_t301_num[sib2->ue_timers_and_constants.t301]); + mac_timers->get(t310)->set(this, liblte_rrc_t310_num[sib2->ue_timers_and_constants.t310]); + mac_timers->get(t311)->set(this, liblte_rrc_t311_num[sib2->ue_timers_and_constants.t311]); + N310 = liblte_rrc_n310_num[sib2->ue_timers_and_constants.n310]; + N311 = liblte_rrc_n311_num[sib2->ue_timers_and_constants.n311]; + + rrc_log->info("Set Constants and Timers: N310=%d, N311=%d, t301=%d, t310=%d, t311=%d\n", + N310, N311, mac_timers->get(t301)->get_timeout(), + mac_timers->get(t310)->get_timeout(), mac_timers->get(t311)->get_timeout()); + } - if(phy_cnfg->cqi_report_cnfg_present) { - if (phy_cnfg->cqi_report_cnfg.report_periodic_present) { - memcpy(¤t_cfg->cqi_report_cnfg.report_periodic, &phy_cnfg->cqi_report_cnfg.report_periodic, sizeof(LIBLTE_RRC_CQI_REPORT_PERIODIC_STRUCT)); - current_cfg->cqi_report_cnfg.report_periodic_setup_present = phy_cnfg->cqi_report_cnfg.report_periodic_setup_present; + + // Go through all information elements and apply defaults (9.2.4) if not defined + void rrc::apply_phy_config_dedicated(LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT *phy_cnfg, bool apply_defaults) { + // Get current configuration + LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT *current_cfg; + phy_interface_rrc::phy_cfg_t c; + phy->get_config(&c); + current_cfg = &c.dedicated; + + if (phy_cnfg->pucch_cnfg_ded_present) { + memcpy(¤t_cfg->pucch_cnfg_ded, &phy_cnfg->pucch_cnfg_ded, sizeof(LIBLTE_RRC_PUCCH_CONFIG_DEDICATED_STRUCT)); } else if (apply_defaults) { - current_cfg->cqi_report_cnfg.report_periodic_setup_present = false; + current_cfg->pucch_cnfg_ded.tdd_ack_nack_feedback_mode = LIBLTE_RRC_TDD_ACK_NACK_FEEDBACK_MODE_BUNDLING; + current_cfg->pucch_cnfg_ded.ack_nack_repetition_setup_present = false; } - if (phy_cnfg->cqi_report_cnfg.report_mode_aperiodic_present) { - current_cfg->cqi_report_cnfg.report_mode_aperiodic = phy_cnfg->cqi_report_cnfg.report_mode_aperiodic; - current_cfg->cqi_report_cnfg.report_mode_aperiodic_present = phy_cnfg->cqi_report_cnfg.report_mode_aperiodic_present; + if (phy_cnfg->pusch_cnfg_ded_present) { + memcpy(¤t_cfg->pusch_cnfg_ded, &phy_cnfg->pusch_cnfg_ded, sizeof(LIBLTE_RRC_PUSCH_CONFIG_DEDICATED_STRUCT)); } else if (apply_defaults) { - current_cfg->cqi_report_cnfg.report_mode_aperiodic_present = false; + current_cfg->pusch_cnfg_ded.beta_offset_ack_idx = 10; + current_cfg->pusch_cnfg_ded.beta_offset_ri_idx = 12; + current_cfg->pusch_cnfg_ded.beta_offset_cqi_idx = 15; } - current_cfg->cqi_report_cnfg.nom_pdsch_rs_epre_offset = phy_cnfg->cqi_report_cnfg.nom_pdsch_rs_epre_offset; - } - if(phy_cnfg->srs_ul_cnfg_ded_present && phy_cnfg->srs_ul_cnfg_ded.setup_present) { - memcpy(¤t_cfg->srs_ul_cnfg_ded, &phy_cnfg->srs_ul_cnfg_ded, sizeof(LIBLTE_RRC_SRS_UL_CONFIG_DEDICATED_STRUCT)); - } else if (apply_defaults) { - current_cfg->srs_ul_cnfg_ded.setup_present = false; - } - if(phy_cnfg->antenna_info_present) { - if (!phy_cnfg->antenna_info_default_value) { - if(phy_cnfg->antenna_info_explicit_value.tx_mode != LIBLTE_RRC_TRANSMISSION_MODE_1 && - phy_cnfg->antenna_info_explicit_value.tx_mode != LIBLTE_RRC_TRANSMISSION_MODE_2) { - rrc_log->error("Transmission mode TM%s not currently supported by srsUE\n", liblte_rrc_transmission_mode_text[phy_cnfg->antenna_info_explicit_value.tx_mode]); + if (phy_cnfg->ul_pwr_ctrl_ded_present) { + memcpy(¤t_cfg->ul_pwr_ctrl_ded, &phy_cnfg->ul_pwr_ctrl_ded, + sizeof(LIBLTE_RRC_UL_POWER_CONTROL_DEDICATED_STRUCT)); + } else if (apply_defaults) { + current_cfg->ul_pwr_ctrl_ded.p0_ue_pusch = 0; + current_cfg->ul_pwr_ctrl_ded.delta_mcs_en = LIBLTE_RRC_DELTA_MCS_ENABLED_EN0; + current_cfg->ul_pwr_ctrl_ded.accumulation_en = true; + current_cfg->ul_pwr_ctrl_ded.p0_ue_pucch = 0; + current_cfg->ul_pwr_ctrl_ded.p_srs_offset = 7; + current_cfg->ul_pwr_ctrl_ded.filter_coeff = LIBLTE_RRC_FILTER_COEFFICIENT_FC4; + current_cfg->ul_pwr_ctrl_ded.filter_coeff_present = true; + } + if (phy_cnfg->tpc_pdcch_cnfg_pucch_present) { + memcpy(¤t_cfg->tpc_pdcch_cnfg_pucch, &phy_cnfg->tpc_pdcch_cnfg_pucch, + sizeof(LIBLTE_RRC_TPC_PDCCH_CONFIG_STRUCT)); + } else if (apply_defaults) { + current_cfg->tpc_pdcch_cnfg_pucch.setup_present = false; + } + if (phy_cnfg->tpc_pdcch_cnfg_pusch_present) { + memcpy(¤t_cfg->tpc_pdcch_cnfg_pusch, &phy_cnfg->tpc_pdcch_cnfg_pusch, + sizeof(LIBLTE_RRC_TPC_PDCCH_CONFIG_STRUCT)); + } else { + current_cfg->tpc_pdcch_cnfg_pusch.setup_present = false; + } + if (phy_cnfg->cqi_report_cnfg_present) { + if (phy_cnfg->cqi_report_cnfg.report_periodic_present) { + memcpy(¤t_cfg->cqi_report_cnfg.report_periodic, &phy_cnfg->cqi_report_cnfg.report_periodic, + sizeof(LIBLTE_RRC_CQI_REPORT_PERIODIC_STRUCT)); + current_cfg->cqi_report_cnfg.report_periodic_setup_present = phy_cnfg->cqi_report_cnfg.report_periodic_setup_present; + } else if (apply_defaults) { + current_cfg->cqi_report_cnfg.report_periodic_setup_present = false; + } + if (phy_cnfg->cqi_report_cnfg.report_mode_aperiodic_present) { + current_cfg->cqi_report_cnfg.report_mode_aperiodic = phy_cnfg->cqi_report_cnfg.report_mode_aperiodic; + current_cfg->cqi_report_cnfg.report_mode_aperiodic_present = phy_cnfg->cqi_report_cnfg.report_mode_aperiodic_present; + } else if (apply_defaults) { + current_cfg->cqi_report_cnfg.report_mode_aperiodic_present = false; + } + current_cfg->cqi_report_cnfg.nom_pdsch_rs_epre_offset = phy_cnfg->cqi_report_cnfg.nom_pdsch_rs_epre_offset; + } + if (phy_cnfg->srs_ul_cnfg_ded_present && phy_cnfg->srs_ul_cnfg_ded.setup_present) { + memcpy(¤t_cfg->srs_ul_cnfg_ded, &phy_cnfg->srs_ul_cnfg_ded, + sizeof(LIBLTE_RRC_SRS_UL_CONFIG_DEDICATED_STRUCT)); + } else if (apply_defaults) { + current_cfg->srs_ul_cnfg_ded.setup_present = false; + } + if (phy_cnfg->antenna_info_present) { + if (!phy_cnfg->antenna_info_default_value) { + if (phy_cnfg->antenna_info_explicit_value.tx_mode != LIBLTE_RRC_TRANSMISSION_MODE_1 && + phy_cnfg->antenna_info_explicit_value.tx_mode != LIBLTE_RRC_TRANSMISSION_MODE_2) { + rrc_log->error("Transmission mode TM%s not currently supported by srsUE\n", + liblte_rrc_transmission_mode_text[phy_cnfg->antenna_info_explicit_value.tx_mode]); + } + memcpy(¤t_cfg->antenna_info_explicit_value, &phy_cnfg->antenna_info_explicit_value, + sizeof(LIBLTE_RRC_ANTENNA_INFO_DEDICATED_STRUCT)); + } else if (apply_defaults) { + current_cfg->antenna_info_explicit_value.tx_mode = LIBLTE_RRC_TRANSMISSION_MODE_2; + current_cfg->antenna_info_explicit_value.codebook_subset_restriction_present = false; + current_cfg->antenna_info_explicit_value.ue_tx_antenna_selection_setup_present = false; } - memcpy(¤t_cfg->antenna_info_explicit_value, &phy_cnfg->antenna_info_explicit_value, sizeof(LIBLTE_RRC_ANTENNA_INFO_DEDICATED_STRUCT)); } else if (apply_defaults) { current_cfg->antenna_info_explicit_value.tx_mode = LIBLTE_RRC_TRANSMISSION_MODE_2; - current_cfg->antenna_info_explicit_value.codebook_subset_restriction_present = false; - current_cfg->antenna_info_explicit_value.ue_tx_antenna_selection_setup_present = false; + current_cfg->antenna_info_explicit_value.codebook_subset_restriction_present = false; + current_cfg->antenna_info_explicit_value.ue_tx_antenna_selection_setup_present = false; + } + if (phy_cnfg->sched_request_cnfg_present) { + memcpy(¤t_cfg->sched_request_cnfg, &phy_cnfg->sched_request_cnfg, + sizeof(LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT)); + } else if (apply_defaults) { + current_cfg->sched_request_cnfg.setup_present = false; + } + if (phy_cnfg->pdsch_cnfg_ded_present) { + current_cfg->pdsch_cnfg_ded = phy_cnfg->pdsch_cnfg_ded; + } else if (apply_defaults) { + current_cfg->pdsch_cnfg_ded = LIBLTE_RRC_PDSCH_CONFIG_P_A_DB_0; } - } else if (apply_defaults) { - current_cfg->antenna_info_explicit_value.tx_mode = LIBLTE_RRC_TRANSMISSION_MODE_2; - current_cfg->antenna_info_explicit_value.codebook_subset_restriction_present = false; - current_cfg->antenna_info_explicit_value.ue_tx_antenna_selection_setup_present = false; - } - if(phy_cnfg->sched_request_cnfg_present) { - memcpy(¤t_cfg->sched_request_cnfg, &phy_cnfg->sched_request_cnfg, sizeof(LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT)); - } else if (apply_defaults) { - current_cfg->sched_request_cnfg.setup_present = false; - } - if(phy_cnfg->pdsch_cnfg_ded_present) { - current_cfg->pdsch_cnfg_ded = phy_cnfg->pdsch_cnfg_ded; - } else if (apply_defaults) { - current_cfg->pdsch_cnfg_ded = LIBLTE_RRC_PDSCH_CONFIG_P_A_DB_0; - } - if (phy_cnfg->cqi_report_cnfg_present) { - if (phy_cnfg->cqi_report_cnfg.report_periodic_present) { - rrc_log->info("Set cqi-PUCCH-ResourceIndex=%d, cqi-pmi-ConfigIndex=%d, cqi-FormatIndicatorPeriodic=%s\n", - current_cfg->cqi_report_cnfg.report_periodic.pucch_resource_idx, - current_cfg->cqi_report_cnfg.report_periodic.pmi_cnfg_idx, - liblte_rrc_cqi_format_indicator_periodic_text[current_cfg->cqi_report_cnfg.report_periodic.format_ind_periodic]); - } - if (phy_cnfg->cqi_report_cnfg.report_mode_aperiodic_present) { - rrc_log->info("Set cqi-ReportModeAperiodic=%s\n", - liblte_rrc_cqi_report_mode_aperiodic_text[current_cfg->cqi_report_cnfg.report_mode_aperiodic]); - } - - } - - if (phy_cnfg->sched_request_cnfg_present) { - rrc_log->info("Set PHY config ded: SR-n_pucch=%d, SR-ConfigIndex=%d, SR-TransMax=%d\n", - current_cfg->sched_request_cnfg.sr_pucch_resource_idx, - current_cfg->sched_request_cnfg.sr_cnfg_idx, - liblte_rrc_dsr_trans_max_num[current_cfg->sched_request_cnfg.dsr_trans_max]); - } - - if (current_cfg->srs_ul_cnfg_ded_present) { - rrc_log->info("Set PHY config ded: SRS-ConfigIndex=%d, SRS-bw=%s, SRS-Nrcc=%d, SRS-hop=%s, SRS-Ncs=%s\n", - current_cfg->srs_ul_cnfg_ded.srs_cnfg_idx, - liblte_rrc_srs_bandwidth_text[current_cfg->srs_ul_cnfg_ded.srs_bandwidth], - current_cfg->srs_ul_cnfg_ded.freq_domain_pos, - liblte_rrc_srs_hopping_bandwidth_text[current_cfg->srs_ul_cnfg_ded.srs_hopping_bandwidth], - liblte_rrc_cyclic_shift_text[current_cfg->srs_ul_cnfg_ded.cyclic_shift]); - } - - phy->set_config_dedicated(current_cfg); - - // Apply changes to PHY - phy->configure_ul_params(); - -} - -void rrc::apply_mac_config_dedicated(LIBLTE_RRC_MAC_MAIN_CONFIG_STRUCT *mac_cnfg, bool apply_defaults) -{ - // Set Default MAC main configuration (9.2.2) - LIBLTE_RRC_MAC_MAIN_CONFIG_STRUCT default_cfg; - bzero(&default_cfg, sizeof(LIBLTE_RRC_MAC_MAIN_CONFIG_STRUCT)); - default_cfg.ulsch_cnfg.max_harq_tx = LIBLTE_RRC_MAX_HARQ_TX_N5; - default_cfg.ulsch_cnfg.periodic_bsr_timer = LIBLTE_RRC_PERIODIC_BSR_TIMER_INFINITY; - default_cfg.ulsch_cnfg.retx_bsr_timer = LIBLTE_RRC_RETRANSMISSION_BSR_TIMER_SF2560; - default_cfg.ulsch_cnfg.tti_bundling = false; - default_cfg.drx_cnfg.setup_present = false; - default_cfg.phr_cnfg.setup_present = false; - default_cfg.time_alignment_timer = LIBLTE_RRC_TIME_ALIGNMENT_TIMER_INFINITY; - - - if (!apply_defaults) { - if(mac_cnfg->ulsch_cnfg_present) - { - if(mac_cnfg->ulsch_cnfg.max_harq_tx_present) { - default_cfg.ulsch_cnfg.max_harq_tx = mac_cnfg->ulsch_cnfg.max_harq_tx; - default_cfg.ulsch_cnfg.max_harq_tx_present = true; + if (phy_cnfg->cqi_report_cnfg_present) { + if (phy_cnfg->cqi_report_cnfg.report_periodic_present) { + rrc_log->info("Set cqi-PUCCH-ResourceIndex=%d, cqi-pmi-ConfigIndex=%d, cqi-FormatIndicatorPeriodic=%s\n", + current_cfg->cqi_report_cnfg.report_periodic.pucch_resource_idx, + current_cfg->cqi_report_cnfg.report_periodic.pmi_cnfg_idx, + liblte_rrc_cqi_format_indicator_periodic_text[current_cfg->cqi_report_cnfg.report_periodic.format_ind_periodic]); } - if(mac_cnfg->ulsch_cnfg.periodic_bsr_timer_present) { - default_cfg.ulsch_cnfg.periodic_bsr_timer = mac_cnfg->ulsch_cnfg.periodic_bsr_timer; - default_cfg.ulsch_cnfg.periodic_bsr_timer_present = true; + if (phy_cnfg->cqi_report_cnfg.report_mode_aperiodic_present) { + rrc_log->info("Set cqi-ReportModeAperiodic=%s\n", + liblte_rrc_cqi_report_mode_aperiodic_text[current_cfg->cqi_report_cnfg.report_mode_aperiodic]); } - default_cfg.ulsch_cnfg.retx_bsr_timer = mac_cnfg->ulsch_cnfg.retx_bsr_timer; - default_cfg.ulsch_cnfg.tti_bundling = mac_cnfg->ulsch_cnfg.tti_bundling; + } - if(mac_cnfg->drx_cnfg_present) { - memcpy(&default_cfg.drx_cnfg, &mac_cnfg->drx_cnfg, sizeof(LIBLTE_RRC_DRX_CONFIG_STRUCT)); - default_cfg.drx_cnfg_present = true; + + if (phy_cnfg->sched_request_cnfg_present) { + rrc_log->info("Set PHY config ded: SR-n_pucch=%d, SR-ConfigIndex=%d, SR-TransMax=%d\n", + current_cfg->sched_request_cnfg.sr_pucch_resource_idx, + current_cfg->sched_request_cnfg.sr_cnfg_idx, + liblte_rrc_dsr_trans_max_num[current_cfg->sched_request_cnfg.dsr_trans_max]); } - if(mac_cnfg->phr_cnfg_present) { - memcpy(&default_cfg.phr_cnfg, &mac_cnfg->phr_cnfg, sizeof(LIBLTE_RRC_PHR_CONFIG_STRUCT)); - default_cfg.phr_cnfg_present = true; + + if (current_cfg->srs_ul_cnfg_ded_present) { + rrc_log->info("Set PHY config ded: SRS-ConfigIndex=%d, SRS-bw=%s, SRS-Nrcc=%d, SRS-hop=%s, SRS-Ncs=%s\n", + current_cfg->srs_ul_cnfg_ded.srs_cnfg_idx, + liblte_rrc_srs_bandwidth_text[current_cfg->srs_ul_cnfg_ded.srs_bandwidth], + current_cfg->srs_ul_cnfg_ded.freq_domain_pos, + liblte_rrc_srs_hopping_bandwidth_text[current_cfg->srs_ul_cnfg_ded.srs_hopping_bandwidth], + liblte_rrc_cyclic_shift_text[current_cfg->srs_ul_cnfg_ded.cyclic_shift]); } - default_cfg.time_alignment_timer = mac_cnfg->time_alignment_timer; - } - - // Setup MAC configuration - mac->set_config_main(&default_cfg); - - rrc_log->info("Set MAC main config: harq-MaxReTX=%d, bsr-TimerReTX=%d, bsr-TimerPeriodic=%d\n", - liblte_rrc_max_harq_tx_num[default_cfg.ulsch_cnfg.max_harq_tx], - liblte_rrc_retransmission_bsr_timer_num[default_cfg.ulsch_cnfg.retx_bsr_timer], - liblte_rrc_periodic_bsr_timer_num[default_cfg.ulsch_cnfg.periodic_bsr_timer]); - if (default_cfg.phr_cnfg_present) { - rrc_log->info("Set MAC PHR config: periodicPHR-Timer=%d, prohibitPHR-Timer=%d, dl-PathlossChange=%d\n", - liblte_rrc_periodic_phr_timer_num[default_cfg.phr_cnfg.periodic_phr_timer], - liblte_rrc_prohibit_phr_timer_num[default_cfg.phr_cnfg.prohibit_phr_timer], - liblte_rrc_dl_pathloss_change_num[default_cfg.phr_cnfg.dl_pathloss_change]); - } -} - -void rrc::apply_rr_config_dedicated(LIBLTE_RRC_RR_CONFIG_DEDICATED_STRUCT *cnfg) { - if(cnfg->phy_cnfg_ded_present) { - apply_phy_config_dedicated(&cnfg->phy_cnfg_ded, false); - // Apply SR configuration to MAC - if (cnfg->phy_cnfg_ded.sched_request_cnfg_present) { - mac->set_config_sr(&cnfg->phy_cnfg_ded.sched_request_cnfg); - } - } - - if(cnfg->mac_main_cnfg_present) { - apply_mac_config_dedicated(&cnfg->mac_main_cnfg.explicit_value, cnfg->mac_main_cnfg.default_value); - } - - if(cnfg->sps_cnfg_present) { - //TODO - } - if(cnfg->rlf_timers_and_constants_present) { - //TODO - } - for(uint32_t i=0; isrb_to_add_mod_list_size; i++) { - // TODO: handle SRB modification - add_srb(&cnfg->srb_to_add_mod_list[i]); - } - for(uint32_t i=0; idrb_to_release_list_size; i++) { - release_drb(cnfg->drb_to_release_list[i]); - } - for(uint32_t i=0; idrb_to_add_mod_list_size; i++) { - // TODO: handle DRB modification - add_drb(&cnfg->drb_to_add_mod_list[i]); + + phy->set_config_dedicated(current_cfg); + + // Apply changes to PHY + phy->configure_ul_params(); + } -} -void rrc::handle_con_setup(LIBLTE_RRC_CONNECTION_SETUP_STRUCT *setup) -{ - // Apply the Radio Resource configuration - apply_rr_config_dedicated(&setup->rr_cnfg); -} + void rrc::apply_mac_config_dedicated(LIBLTE_RRC_MAC_MAIN_CONFIG_STRUCT *mac_cnfg, bool apply_defaults) { + // Set Default MAC main configuration (9.2.2) + LIBLTE_RRC_MAC_MAIN_CONFIG_STRUCT default_cfg; + bzero(&default_cfg, sizeof(LIBLTE_RRC_MAC_MAIN_CONFIG_STRUCT)); + default_cfg.ulsch_cnfg.max_harq_tx = LIBLTE_RRC_MAX_HARQ_TX_N5; + default_cfg.ulsch_cnfg.periodic_bsr_timer = LIBLTE_RRC_PERIODIC_BSR_TIMER_INFINITY; + default_cfg.ulsch_cnfg.retx_bsr_timer = LIBLTE_RRC_RETRANSMISSION_BSR_TIMER_SF2560; + default_cfg.ulsch_cnfg.tti_bundling = false; + default_cfg.drx_cnfg.setup_present = false; + default_cfg.phr_cnfg.setup_present = false; + default_cfg.time_alignment_timer = LIBLTE_RRC_TIME_ALIGNMENT_TIMER_INFINITY; + + + if (!apply_defaults) { + if (mac_cnfg->ulsch_cnfg_present) { + if (mac_cnfg->ulsch_cnfg.max_harq_tx_present) { + default_cfg.ulsch_cnfg.max_harq_tx = mac_cnfg->ulsch_cnfg.max_harq_tx; + default_cfg.ulsch_cnfg.max_harq_tx_present = true; + } + if (mac_cnfg->ulsch_cnfg.periodic_bsr_timer_present) { + default_cfg.ulsch_cnfg.periodic_bsr_timer = mac_cnfg->ulsch_cnfg.periodic_bsr_timer; + default_cfg.ulsch_cnfg.periodic_bsr_timer_present = true; + } + default_cfg.ulsch_cnfg.retx_bsr_timer = mac_cnfg->ulsch_cnfg.retx_bsr_timer; + default_cfg.ulsch_cnfg.tti_bundling = mac_cnfg->ulsch_cnfg.tti_bundling; + } + if (mac_cnfg->drx_cnfg_present) { + memcpy(&default_cfg.drx_cnfg, &mac_cnfg->drx_cnfg, sizeof(LIBLTE_RRC_DRX_CONFIG_STRUCT)); + default_cfg.drx_cnfg_present = true; + } + if (mac_cnfg->phr_cnfg_present) { + memcpy(&default_cfg.phr_cnfg, &mac_cnfg->phr_cnfg, sizeof(LIBLTE_RRC_PHR_CONFIG_STRUCT)); + default_cfg.phr_cnfg_present = true; + } + default_cfg.time_alignment_timer = mac_cnfg->time_alignment_timer; + } -/* Reception of RRCConnectionReestablishment by the UE 5.3.7.5 */ -void rrc::handle_con_reest(LIBLTE_RRC_CONNECTION_REESTABLISHMENT_STRUCT *setup) -{ - mac_timers->get(t301)->stop(); - - // TODO: Restablish DRB1. Not done because never was suspended - - // Apply the Radio Resource configuration - apply_rr_config_dedicated(&setup->rr_cnfg); - - // TODO: Some security stuff here... is it necessary? - - send_con_restablish_complete(); -} - - -void rrc::handle_rrc_con_reconfig(uint32_t lcid, LIBLTE_RRC_CONNECTION_RECONFIGURATION_STRUCT *reconfig, byte_buffer_t *pdu) -{ - uint32_t i; - - if (reconfig->rr_cnfg_ded_present) { - apply_rr_config_dedicated(&reconfig->rr_cnfg_ded); - } else { - printf("received con reconfig no rr confg present\n"); + // Setup MAC configuration + mac->set_config_main(&default_cfg); + + rrc_log->info("Set MAC main config: harq-MaxReTX=%d, bsr-TimerReTX=%d, bsr-TimerPeriodic=%d\n", + liblte_rrc_max_harq_tx_num[default_cfg.ulsch_cnfg.max_harq_tx], + liblte_rrc_retransmission_bsr_timer_num[default_cfg.ulsch_cnfg.retx_bsr_timer], + liblte_rrc_periodic_bsr_timer_num[default_cfg.ulsch_cnfg.periodic_bsr_timer]); + if (default_cfg.phr_cnfg_present) { + rrc_log->info("Set MAC PHR config: periodicPHR-Timer=%d, prohibitPHR-Timer=%d, dl-PathlossChange=%d\n", + liblte_rrc_periodic_phr_timer_num[default_cfg.phr_cnfg.periodic_phr_timer], + liblte_rrc_prohibit_phr_timer_num[default_cfg.phr_cnfg.prohibit_phr_timer], + liblte_rrc_dl_pathloss_change_num[default_cfg.phr_cnfg.dl_pathloss_change]); + } } - if(reconfig->meas_cnfg_present) - { - //TODO: handle meas_cnfg + + void rrc::apply_rr_config_dedicated(LIBLTE_RRC_RR_CONFIG_DEDICATED_STRUCT *cnfg) { + if (cnfg->phy_cnfg_ded_present) { + apply_phy_config_dedicated(&cnfg->phy_cnfg_ded, false); + // Apply SR configuration to MAC + if (cnfg->phy_cnfg_ded.sched_request_cnfg_present) { + mac->set_config_sr(&cnfg->phy_cnfg_ded.sched_request_cnfg); + } + } + + if (cnfg->mac_main_cnfg_present) { + apply_mac_config_dedicated(&cnfg->mac_main_cnfg.explicit_value, cnfg->mac_main_cnfg.default_value); + } + + if (cnfg->sps_cnfg_present) { + //TODO + } + if (cnfg->rlf_timers_and_constants_present) { + //TODO + } + for (uint32_t i = 0; i < cnfg->srb_to_add_mod_list_size; i++) { + // TODO: handle SRB modification + add_srb(&cnfg->srb_to_add_mod_list[i]); + } + for (uint32_t i = 0; i < cnfg->drb_to_release_list_size; i++) { + release_drb(cnfg->drb_to_release_list[i]); + } + for (uint32_t i = 0; i < cnfg->drb_to_add_mod_list_size; i++) { + // TODO: handle DRB modification + add_drb(&cnfg->drb_to_add_mod_list[i]); + } } - if(reconfig->mob_ctrl_info_present) - { - //TODO: handle mob_ctrl_info + + void rrc::handle_con_setup(LIBLTE_RRC_CONNECTION_SETUP_STRUCT *setup) { + // Apply the Radio Resource configuration + apply_rr_config_dedicated(&setup->rr_cnfg); } - send_rrc_con_reconfig_complete(lcid, pdu); +/* Reception of RRCConnectionReestablishment by the UE 5.3.7.5 */ + void rrc::handle_con_reest(LIBLTE_RRC_CONNECTION_REESTABLISHMENT_STRUCT *setup) { + mac_timers->get(t301)->stop(); + + // TODO: Restablish DRB1. Not done because never was suspended - byte_buffer_t *nas_sdu; - for(i=0;iN_ded_info_nas;i++) - { - nas_sdu = pool_allocate;; - memcpy(nas_sdu->msg, &reconfig->ded_info_nas_list[i].msg, reconfig->ded_info_nas_list[i].N_bytes); - nas_sdu->N_bytes = reconfig->ded_info_nas_list[i].N_bytes; - nas->write_pdu(lcid, nas_sdu); + // Apply the Radio Resource configuration + apply_rr_config_dedicated(&setup->rr_cnfg); + + // TODO: Some security stuff here... is it necessary? + + send_con_restablish_complete(); } -} - -void rrc::add_srb(LIBLTE_RRC_SRB_TO_ADD_MOD_STRUCT *srb_cnfg) -{ - // Setup PDCP - pdcp->add_bearer(srb_cnfg->srb_id); - if(RB_ID_SRB2 == srb_cnfg->srb_id) - pdcp->config_security(srb_cnfg->srb_id, k_rrc_enc, k_rrc_int, cipher_algo, integ_algo); - - // Setup RLC - if(srb_cnfg->rlc_cnfg_present) - { - if(srb_cnfg->rlc_default_cnfg_present) - { - rlc->add_bearer(srb_cnfg->srb_id); - }else{ - rlc->add_bearer(srb_cnfg->srb_id, &srb_cnfg->rlc_explicit_cnfg); + + + void rrc::handle_rrc_con_reconfig(uint32_t lcid, LIBLTE_RRC_CONNECTION_RECONFIGURATION_STRUCT *reconfig, + byte_buffer_t *pdu) { + uint32_t i; + + if (reconfig->rr_cnfg_ded_present) { + apply_rr_config_dedicated(&reconfig->rr_cnfg_ded); + } else { + printf("received con reconfig no rr confg present\n"); + } + if (reconfig->meas_cnfg_present) { + //TODO: handle meas_cnfg + } + if (reconfig->mob_ctrl_info_present) { + //TODO: handle mob_ctrl_info + } + + send_rrc_con_reconfig_complete(lcid, pdu); + + byte_buffer_t *nas_sdu; + for (i = 0; i < reconfig->N_ded_info_nas; i++) { + nas_sdu = pool_allocate;; + memcpy(nas_sdu->msg, &reconfig->ded_info_nas_list[i].msg, reconfig->ded_info_nas_list[i].N_bytes); + nas_sdu->N_bytes = reconfig->ded_info_nas_list[i].N_bytes; + nas->write_pdu(lcid, nas_sdu); } } - // Setup MAC - uint8_t log_chan_group = 0; - uint8_t priority = 1; - int prioritized_bit_rate = -1; - int bucket_size_duration = -1; - - if(srb_cnfg->lc_cnfg_present) - { - if(srb_cnfg->lc_default_cnfg_present) - { - if(RB_ID_SRB2 == srb_cnfg->srb_id) - priority = 3; - }else{ - if(srb_cnfg->lc_explicit_cnfg.log_chan_sr_mask_present) - { - //TODO - } - if(srb_cnfg->lc_explicit_cnfg.ul_specific_params_present) - { - if(srb_cnfg->lc_explicit_cnfg.ul_specific_params.log_chan_group_present) - log_chan_group = srb_cnfg->lc_explicit_cnfg.ul_specific_params.log_chan_group; - - priority = srb_cnfg->lc_explicit_cnfg.ul_specific_params.priority; - prioritized_bit_rate = liblte_rrc_prioritized_bit_rate_num[srb_cnfg->lc_explicit_cnfg.ul_specific_params.prioritized_bit_rate]; - bucket_size_duration = liblte_rrc_bucket_size_duration_num[srb_cnfg->lc_explicit_cnfg.ul_specific_params.bucket_size_duration]; + void rrc::add_srb(LIBLTE_RRC_SRB_TO_ADD_MOD_STRUCT *srb_cnfg) { + // Setup PDCP + pdcp->add_bearer(srb_cnfg->srb_id); + if (RB_ID_SRB2 == srb_cnfg->srb_id) + pdcp->config_security(srb_cnfg->srb_id, k_rrc_enc, k_rrc_int, cipher_algo, integ_algo); + + // Setup RLC + if (srb_cnfg->rlc_cnfg_present) { + if (srb_cnfg->rlc_default_cnfg_present) { + rlc->add_bearer(srb_cnfg->srb_id); + } else { + rlc->add_bearer(srb_cnfg->srb_id, &srb_cnfg->rlc_explicit_cnfg); } } - mac->setup_lcid(srb_cnfg->srb_id, log_chan_group, priority, prioritized_bit_rate, bucket_size_duration); - } - srbs[srb_cnfg->srb_id] = *srb_cnfg; - rrc_log->info("Added radio bearer %s\n", rb_id_text[srb_cnfg->srb_id]); -} + // Setup MAC + uint8_t log_chan_group = 0; + uint8_t priority = 1; + int prioritized_bit_rate = -1; + int bucket_size_duration = -1; -void rrc::add_drb(LIBLTE_RRC_DRB_TO_ADD_MOD_STRUCT *drb_cnfg) -{ + if (srb_cnfg->lc_cnfg_present) { + if (srb_cnfg->lc_default_cnfg_present) { + if (RB_ID_SRB2 == srb_cnfg->srb_id) + priority = 3; + } else { + if (srb_cnfg->lc_explicit_cnfg.log_chan_sr_mask_present) { + //TODO + } + if (srb_cnfg->lc_explicit_cnfg.ul_specific_params_present) { + if (srb_cnfg->lc_explicit_cnfg.ul_specific_params.log_chan_group_present) + log_chan_group = srb_cnfg->lc_explicit_cnfg.ul_specific_params.log_chan_group; - if(!drb_cnfg->pdcp_cnfg_present || - !drb_cnfg->rlc_cnfg_present || - !drb_cnfg->lc_cnfg_present) - { - rrc_log->error("Cannot add DRB - incomplete configuration\n"); - return; - } - uint32_t lcid = 0; - if (drb_cnfg->lc_id_present) { - lcid = drb_cnfg->lc_id; - } else { - lcid = RB_ID_SRB2 + drb_cnfg->drb_id; - rrc_log->warning("LCID not present, using %d\n", lcid); + priority = srb_cnfg->lc_explicit_cnfg.ul_specific_params.priority; + prioritized_bit_rate = liblte_rrc_prioritized_bit_rate_num[srb_cnfg->lc_explicit_cnfg.ul_specific_params.prioritized_bit_rate]; + bucket_size_duration = liblte_rrc_bucket_size_duration_num[srb_cnfg->lc_explicit_cnfg.ul_specific_params.bucket_size_duration]; + } + } + mac->setup_lcid(srb_cnfg->srb_id, log_chan_group, priority, prioritized_bit_rate, bucket_size_duration); + } + + srbs[srb_cnfg->srb_id] = *srb_cnfg; + rrc_log->info("Added radio bearer %s\n", rb_id_text[srb_cnfg->srb_id]); } - - // Setup PDCP - pdcp->add_bearer(lcid, &drb_cnfg->pdcp_cnfg); - // TODO: setup PDCP security (using k_up_enc) - - // Setup RLC - rlc->add_bearer(lcid, &drb_cnfg->rlc_cnfg); - - // Setup MAC - uint8_t log_chan_group = 0; - uint8_t priority = 1; - int prioritized_bit_rate = -1; - int bucket_size_duration = -1; - if(drb_cnfg->lc_cnfg.ul_specific_params_present) - { - if(drb_cnfg->lc_cnfg.ul_specific_params.log_chan_group_present) { - log_chan_group = drb_cnfg->lc_cnfg.ul_specific_params.log_chan_group; + + void rrc::add_drb(LIBLTE_RRC_DRB_TO_ADD_MOD_STRUCT *drb_cnfg) { + + if (!drb_cnfg->pdcp_cnfg_present || + !drb_cnfg->rlc_cnfg_present || + !drb_cnfg->lc_cnfg_present) { + rrc_log->error("Cannot add DRB - incomplete configuration\n"); + return; + } + uint32_t lcid = 0; + if (drb_cnfg->lc_id_present) { + lcid = drb_cnfg->lc_id; } else { - rrc_log->warning("LCG not present, setting to 0\n"); + lcid = RB_ID_SRB2 + drb_cnfg->drb_id; + rrc_log->warning("LCID not present, using %d\n", lcid); } - priority = drb_cnfg->lc_cnfg.ul_specific_params.priority; - prioritized_bit_rate = liblte_rrc_prioritized_bit_rate_num[drb_cnfg->lc_cnfg.ul_specific_params.prioritized_bit_rate]; - - if (prioritized_bit_rate > 0) { - rrc_log->warning("PBR>0 currently not supported. Setting it to Inifinty\n"); - prioritized_bit_rate = -1; + + // Setup PDCP + pdcp->add_bearer(lcid, &drb_cnfg->pdcp_cnfg); + // TODO: setup PDCP security (using k_up_enc) + + // Setup RLC + rlc->add_bearer(lcid, &drb_cnfg->rlc_cnfg); + + // Setup MAC + uint8_t log_chan_group = 0; + uint8_t priority = 1; + int prioritized_bit_rate = -1; + int bucket_size_duration = -1; + if (drb_cnfg->lc_cnfg.ul_specific_params_present) { + if (drb_cnfg->lc_cnfg.ul_specific_params.log_chan_group_present) { + log_chan_group = drb_cnfg->lc_cnfg.ul_specific_params.log_chan_group; + } else { + rrc_log->warning("LCG not present, setting to 0\n"); + } + priority = drb_cnfg->lc_cnfg.ul_specific_params.priority; + prioritized_bit_rate = liblte_rrc_prioritized_bit_rate_num[drb_cnfg->lc_cnfg.ul_specific_params.prioritized_bit_rate]; + + if (prioritized_bit_rate > 0) { + rrc_log->warning("PBR>0 currently not supported. Setting it to Inifinty\n"); + prioritized_bit_rate = -1; + } + + bucket_size_duration = liblte_rrc_bucket_size_duration_num[drb_cnfg->lc_cnfg.ul_specific_params.bucket_size_duration]; } - - bucket_size_duration = liblte_rrc_bucket_size_duration_num[drb_cnfg->lc_cnfg.ul_specific_params.bucket_size_duration]; - } - mac->setup_lcid(lcid, log_chan_group, priority, prioritized_bit_rate, bucket_size_duration); - - drbs[lcid] = *drb_cnfg; - drb_up = true; - rrc_log->info("Added radio bearer %s\n", rb_id_text[lcid]); -} - -void rrc::release_drb(uint8_t lcid) -{ - // TODO -} + mac->setup_lcid(lcid, log_chan_group, priority, prioritized_bit_rate, bucket_size_duration); + + drbs[lcid] = *drb_cnfg; + drb_up = true; + rrc_log->info("Added radio bearer %s\n", rb_id_text[lcid]); + } + + void rrc::release_drb(uint8_t lcid) { + // TODO + } /************************** * DEFAULT VALUES Section 9 ****************************/ // PHY CONFIG DEDICATED Defaults (3GPP 36.331 v10 9.2.4) -void rrc::set_phy_default_pucch_srs() -{ - - phy_interface_rrc::phy_cfg_t current_cfg; - phy->get_config(¤t_cfg); - - // Set defaults to CQI, SRS and SR - current_cfg.dedicated.cqi_report_cnfg_present = false; - current_cfg.dedicated.srs_ul_cnfg_ded_present = false; - current_cfg.dedicated.sched_request_cnfg_present = false; - - apply_phy_config_dedicated(¤t_cfg.dedicated, true); - - // Release SR configuration from MAC - LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT cfg; - bzero(&cfg, sizeof(LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT)); - mac->set_config_sr(&cfg); -} - -void rrc::set_phy_default() -{ - LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT defaults; - bzero(&defaults, sizeof(LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT)); - apply_phy_config_dedicated(&defaults, true); -} - -void rrc::set_mac_default() -{ - apply_mac_config_dedicated(NULL, true); - LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT sr_cfg; - bzero(&sr_cfg, sizeof(LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT)); - sr_cfg.setup_present = false; - mac->set_config_sr(&sr_cfg); -} - -void rrc::set_rrc_default() { - N310 = 1; - N311 = 1; - t301 = mac_timers->get_unique_id(); - t310 = mac_timers->get_unique_id(); - t311 = mac_timers->get_unique_id(); - safe_reset_timer = mac_timers->get_unique_id(); - mac_timers->get(t310)->set(this, 1000); - mac_timers->get(t311)->set(this, 1000); - mac_timers->get(safe_reset_timer)->set(this, 10); -} + void rrc::set_phy_default_pucch_srs() { + + phy_interface_rrc::phy_cfg_t current_cfg; + phy->get_config(¤t_cfg); + + // Set defaults to CQI, SRS and SR + current_cfg.dedicated.cqi_report_cnfg_present = false; + current_cfg.dedicated.srs_ul_cnfg_ded_present = false; + current_cfg.dedicated.sched_request_cnfg_present = false; + + apply_phy_config_dedicated(¤t_cfg.dedicated, true); + + // Release SR configuration from MAC + LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT cfg; + bzero(&cfg, sizeof(LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT)); + mac->set_config_sr(&cfg); + } + + void rrc::set_phy_default() { + LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT defaults; + bzero(&defaults, sizeof(LIBLTE_RRC_PHYSICAL_CONFIG_DEDICATED_STRUCT)); + apply_phy_config_dedicated(&defaults, true); + } + + void rrc::set_mac_default() { + apply_mac_config_dedicated(NULL, true); + LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT sr_cfg; + bzero(&sr_cfg, sizeof(LIBLTE_RRC_SCHEDULING_REQUEST_CONFIG_STRUCT)); + sr_cfg.setup_present = false; + mac->set_config_sr(&sr_cfg); + } + + void rrc::set_rrc_default() { + N310 = 1; + N311 = 1; + t301 = mac_timers->get_unique_id(); + t310 = mac_timers->get_unique_id(); + t311 = mac_timers->get_unique_id(); + safe_reset_timer = mac_timers->get_unique_id(); + mac_timers->get(t310)->set(this, 1000); + mac_timers->get(t311)->set(this, 1000); + mac_timers->get(safe_reset_timer)->set(this, 10); + } } // namespace srsue