/** * * \section COPYRIGHT * * Copyright 2013-2014 The libLTE Developers. See the * COPYRIGHT file at the top-level directory of this distribution. * * \section LICENSE * * This file is part of the libLTE library. * * libLTE is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * libLTE is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * A copy of the GNU Lesser General Public License can be found in * the LICENSE file in the top-level directory of this distribution * and at http://www.gnu.org/licenses/. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include "liblte/rrc/rrc.h" #include "liblte/phy/phy.h" #include "liblte/cuhd/cuhd.h" #include "cuhd_utils.h" cell_search_cfg_t cell_detect_config = { 5000, 100, // nof_frames_total 16.0 // threshold }; #define B210_DEFAULT_GAIN 40.0 #define B210_DEFAULT_GAIN_CORREC 110.0 // Gain of the Rx chain when the gain is set to 40 float gain_offset = B210_DEFAULT_GAIN_CORREC; /********************************************************************** * Program arguments processing ***********************************************************************/ typedef struct { int nof_subframes; int force_N_id_2; uint16_t rnti; uint32_t file_nof_prb; char *uhd_args; float uhd_rx_freq; float uhd_tx_freq; float uhd_tx_freq_offset; float uhd_tx_gain; float uhd_rx_gain; }prog_args_t; void args_default(prog_args_t *args) { args->nof_subframes = -1; args->rnti = SIRNTI; args->force_N_id_2 = -1; // Pick the best args->file_nof_prb = 6; args->uhd_args = ""; args->uhd_rx_freq = 2112500000.0; args->uhd_tx_freq = 1922500000.0; args->uhd_tx_freq_offset = 8000000.0; args->uhd_tx_gain = 60.0; args->uhd_rx_gain = 60.0; } void usage(prog_args_t *args, char *prog) { printf("Usage: %s [agfFrlnv]\n", prog); printf("\t-a UHD args [Default %s]\n", args->uhd_args); printf("\t-g UHD TX/RX gain [Default %.2f dB]\n", args->uhd_rx_gain); printf("\t-G UHD TX/RX gain [Default %.2f dB]\n", args->uhd_tx_gain); printf("\t-f UHD RX freq [Default %.1f MHz]\n", args->uhd_rx_freq/1000000); printf("\t-F UHD TX freq [Default %.1f MHz]\n", args->uhd_tx_freq/1000000); printf("\t-r RNTI [Default 0x%x]\n",args->rnti); printf("\t-l Force N_id_2 [Default best]\n"); printf("\t-n nof_subframes [Default %d]\n", args->nof_subframes); printf("\t-v [set verbose to debug, default none]\n"); } void parse_args(prog_args_t *args, int argc, char **argv) { int opt; args_default(args); while ((opt = getopt(argc, argv, "agGfFrlnv")) != -1) { switch (opt) { case 'a': args->uhd_args = argv[optind]; break; case 'g': args->uhd_rx_gain = atof(argv[optind]); break; case 'G': args->uhd_tx_gain = atof(argv[optind]); break; case 'f': args->uhd_rx_freq = atof(argv[optind]); break; case 'F': args->uhd_tx_freq = atof(argv[optind]); break; case 'n': args->nof_subframes = atoi(argv[optind]); break; case 'r': args->rnti = atoi(argv[optind]); break; case 'l': args->force_N_id_2 = atoi(argv[optind]); break; case 'v': verbose++; break; default: usage(args, argv[0]); exit(-1); } } if (args->uhd_tx_freq < 0 && args->uhd_rx_freq < 0) { usage(args, argv[0]); exit(-1); } } /**********************************************************************/ /* TODO: Do something with the output data */ uint8_t data_rx[20000]; bool go_exit = false; void sig_int_handler(int signo) { if (signo == SIGINT) { go_exit = true; } } int cuhd_recv_wrapper_timed(void *h, void *data, uint32_t nsamples, timestamp_t *uhd_time) { DEBUG(" ---- Receive %d samples ---- \n", nsamples); return cuhd_recv_with_time(h, data, nsamples, true, &uhd_time->full_secs, &uhd_time->frac_secs); } extern float mean_exec_time; enum receiver_state { DECODE_MIB, SEND_PRACH, RECV_RAR} state; #define NOF_PRACH_SEQUENCES 52 ue_dl_t ue_dl; ue_ul_t ue_ul; ue_sync_t ue_sync; prach_t prach; cf_t *prach_buffers[NOF_PRACH_SEQUENCES]; int prach_buffer_len; prog_args_t prog_args; uint32_t sfn = 0; // system frame number cf_t *sf_buffer = NULL; int generate_prach_sequences(){ for(int i=0;ihdr_type]); fprintf(stream, "Hopping flag: %s\n", bool_to_string(msg->hopping_flag)); fprintf(stream, "TPC command: %s\n", tpc_command_text[msg->tpc_command]); fprintf(stream, "UL delay: %s\n", bool_to_string(msg->ul_delay)); fprintf(stream, "CSI required: %s\n", bool_to_string(msg->csi_req)); fprintf(stream, "RBA: %d\n", msg->rba); fprintf(stream, "TA: %d\n", msg->timing_adv_cmd); fprintf(stream, "T-CRNTI: %d\n", msg->temp_c_rnti); fprintf(stream, "MCS: %d\n", msg->mcs); fprintf(stream, "RAPID: %d\n", msg->RAPID); fprintf(stream, "BI: %d\n", msg->BI); } int rar_unpack(uint8_t *buffer, rar_msg_t *msg) { int ret = LIBLTE_ERROR; uint8_t *ptr = buffer; if(buffer != NULL && msg != NULL) { ptr++; msg->hdr_type = *ptr++; if(msg->hdr_type == rar_header_type_bi) { ptr += 2; msg->BI = bit_unpack(&ptr, 4); ret = LIBLTE_SUCCESS; } else if (msg->hdr_type == rar_header_type_rapid) { msg->RAPID = bit_unpack(&ptr, 6); ptr++; msg->timing_adv_cmd = bit_unpack(&ptr, 11); msg->hopping_flag = *ptr++; msg->rba = bit_unpack(&ptr, 10); msg->mcs = bit_unpack(&ptr, 4); msg->tpc_command = (rar_tpc_command_t) bit_unpack(&ptr, 3); msg->ul_delay = *ptr++; msg->csi_req = *ptr++; msg->temp_c_rnti = bit_unpack(&ptr, 16); ret = LIBLTE_SUCCESS; } } return(ret); } #define kk #define use_usrp int main(int argc, char **argv) { int ret; lte_cell_t cell; int64_t sf_cnt; ue_mib_t ue_mib; void *uhd; int n; uint8_t bch_payload[BCH_PAYLOAD_LEN], bch_payload_unpacked[BCH_PAYLOAD_LEN]; uint32_t sfn_offset; rar_msg_t rar_msg; ra_pusch_t ra_pusch; uint32_t rar_window_start = 0, rar_trials = 0, rar_window_stop = 0; timestamp_t uhd_time; timestamp_t next_tx_time; const uint8_t conn_request_msg[] = {0x20, 0x06, 0x1F, 0x5C, 0x2C, 0x04, 0xB2, 0xAC, 0xF6, 0x00, 0x00, 0x00}; uint8_t data[500]; parse_args(&prog_args, argc, argv); #ifdef use_usrp printf("Opening UHD device...\n"); if (cuhd_open(prog_args.uhd_args, &uhd)) { fprintf(stderr, "Error opening uhd\n"); exit(-1); } /* Set receiver gain */ float x = cuhd_set_rx_gain(uhd, prog_args.uhd_rx_gain); printf("Set RX gain to %.1f dB\n", x); x = cuhd_set_tx_gain(uhd, prog_args.uhd_tx_gain); printf("Set TX gain to %.1f dB\n", x); /* set receiver frequency */ cuhd_set_rx_freq(uhd, (double) prog_args.uhd_rx_freq); cuhd_rx_wait_lo_locked(uhd); printf("Tunning RX receiver to %.3f MHz\n", (double ) prog_args.uhd_rx_freq/1000000); cuhd_set_tx_freq_offset(uhd, prog_args.uhd_tx_freq, prog_args.uhd_tx_freq_offset); printf("Tunning TX receiver to %.3f MHz\n", (double ) prog_args.uhd_tx_freq/1000000); #endif #ifdef kk ret = cuhd_search_and_decode_mib(uhd, &cell_detect_config, prog_args.force_N_id_2, &cell); if (ret < 0) { fprintf(stderr, "Error searching for cell\n"); exit(-1); } else if (ret == 0) { printf("Cell not found\n"); exit(0); } #else cell.id = 1; cell.nof_ports = 1; cell.nof_prb = 25; cell.cp = CPNORM; cell.phich_length = PHICH_NORM; cell.phich_resources = R_1; #endif #ifdef use_usrp /* set sampling frequency */ int srate = lte_sampling_freq_hz(cell.nof_prb); if (srate != -1) { cuhd_set_rx_srate(uhd, (double) srate); cuhd_set_tx_srate(uhd, (double) srate); } else { fprintf(stderr, "Invalid number of PRB %d\n", cell.nof_prb); exit(-1); } INFO("Stopping UHD and flushing buffer...\r",0); cuhd_stop_rx_stream(uhd); cuhd_flush_buffer(uhd); #endif if (ue_mib_init(&ue_mib, cell)) { fprintf(stderr, "Error initaiting UE MIB decoder\n"); exit(-1); } if (prach_init(&prach, lte_symbol_sz(cell.nof_prb), 0, 0, false, 1)) { fprintf(stderr, "Error initializing PRACH\n"); exit(-1); } prach_buffer_len = prach.N_seq + prach.N_cp; for(int i=0;i 1000) { switch (state) { case DECODE_MIB: if (ue_sync_get_sfidx(&ue_sync) == 0) { pbch_decode_reset(&ue_mib.pbch); n = ue_mib_decode(&ue_mib, sf_buffer, bch_payload_unpacked, NULL, &sfn_offset); if (n < 0) { fprintf(stderr, "Error decoding UE MIB\n"); exit(-1); } else if (n == MIB_FOUND) { bit_unpack_vector(bch_payload_unpacked, bch_payload, BCH_PAYLOAD_LEN); bcch_bch_unpack(bch_payload, BCH_PAYLOAD_LEN, &cell, &sfn); printf("Decoded MIB. SFN: %d, offset: %d\n", sfn, sfn_offset); sfn = (sfn + sfn_offset)%1024; state = SEND_PRACH; } } break; case SEND_PRACH: #ifdef kk if (((sfn%2) == 1) && (ue_sync_get_sfidx(&ue_sync) == 1)) { ue_sync_get_last_timestamp(&ue_sync, &uhd_time); timestamp_copy(&next_tx_time, &uhd_time); timestamp_add(&next_tx_time, 0, 0.01); // send next frame (10 ms) printf("Send prach sfn: %d. Last frame time = %.6f, send prach time = %.6f\n", sfn, timestamp_real(&uhd_time), timestamp_real(&next_tx_time)); cuhd_send_timed(uhd, prach_buffers[7], prach_buffer_len, next_tx_time.full_secs, next_tx_time.frac_secs); ra_rnti = 2; rar_window_start = sfn+1; rar_window_stop = sfn+3; state = RECV_RAR; } #else cuhd_recv_with_time(uhd, dummy, 4, 1, &rx_time.full_secs, &rx_time.frac_secs); timestamp_copy(&tx_time, &rx_time); printf("Transmitting PRACH...\n"); vec_save_file("prach_tx", prach_buffers[7], prach_buffer_len*sizeof(cf_t)); while(1) { timestamp_add(&tx_time, 0, 0.001); // send every (10 ms) cuhd_send_timed(uhd, prach_buffers[7], prach_buffer_len, tx_time.full_secs, tx_time.frac_secs); } #endif break; case RECV_RAR: #ifdef kk if ((sfn == rar_window_start && ue_sync_get_sfidx(&ue_sync) > 3) || sfn > rar_window_start) { printf("Looking for RAR in sfn: %d sf_idx: %d\n", sfn, ue_sync_get_sfidx(&ue_sync)); n = ue_dl_decode_rnti(&ue_dl, sf_buffer, data_rx, ue_sync_get_sfidx(&ue_sync), ra_rnti); if (n < 0) { fprintf(stderr, "Error decoding UE DL\n");fflush(stdout); } else if (n > 0) { cuhd_stop_rx_stream(uhd); cuhd_flush_buffer(uhd); rar_unpack(data_rx, &rar_msg); rar_msg_fprint(stdout, &rar_msg); dci_rar_to_ra_ul(rar_msg.rba, rar_msg.mcs, rar_msg.hopping_flag, cell.nof_prb, &ra_pusch); ra_pusch_fprint(stdout, &ra_pusch, cell.nof_prb); ra_ul_alloc(&ra_pusch.prb_alloc, &ra_pusch, 0, cell.nof_prb); ue_sync_get_last_timestamp(&ue_sync, &uhd_time); bit_pack_vector((uint8_t*) conn_request_msg, data, ra_pusch.mcs.tbs); uint32_t n_ta = lte_N_ta_new_rar(rar_msg.timing_adv_cmd); printf("ta: %d, n_ta: %d\n", rar_msg.timing_adv_cmd, n_ta); float time_adv_sec = ((float) n_ta+15)/(15000.0*lte_symbol_sz(cell.nof_prb)); #define N_TX 5 const uint32_t rv[N_TX]={0,2,3,1,0}; for (int i=0; i= rar_window_stop) { state = SEND_PRACH; rar_trials++; if (rar_trials >= 1) { go_exit = 1; } } } #else ra_pusch.mcs.mod = LTE_QPSK; ra_pusch.mcs.tbs = 94; ra_pusch.rv_idx = 0; ra_pusch.prb_alloc.freq_hopping = 0; ra_pusch.prb_alloc.L_prb = 4; ra_pusch.prb_alloc.n_prb[0] = 19; ra_pusch.prb_alloc.n_prb[1] = 19; uint32_t ul_sf_idx = 4; printf("L: %d\n", ra_pusch.prb_alloc.L_prb); // ue_ul_set_cfo(&ue_ul, sync_get_cfo(&ue_sync.strack)); bit_pack_vector((uint8_t*) conn_request_msg, data, ra_pusch.mcs.tbs); n = ue_ul_pusch_encode_rnti(&ue_ul, &ra_pusch, data, ul_sf_idx, 111, ul_signal); if (n < 0) { fprintf(stderr, "Error encoding PUSCH\n"); exit(-1); } vec_save_file("pusch_tx", ul_signal, SF_LEN_PRB(cell.nof_prb)*sizeof(cf_t)); #ifdef use_usrp cuhd_recv_with_time(uhd, dummy, 4, 1, &uhd_time.full_secs, &uhd_time.frac_secs); timestamp_copy(&next_tx_time, &uhd_time); while(1) { timestamp_add(&next_tx_time, 0, 0.002); // send every 2 ms cuhd_send_timed(uhd, ul_signal, SF_LEN_PRB(cell.nof_prb), next_tx_time.full_secs, next_tx_time.frac_secs); } #else exit(-1); #endif #endif break; } if (ue_sync_get_sfidx(&ue_sync) == 9) { sfn++; if (sfn == 1024) { sfn = 0; } } } } else if (ret == 0) { printf("Finding PSS... Peak: %8.1f, FrameCnt: %d, State: %d\r", sync_get_peak_value(&ue_sync.sfind), ue_sync.frame_total_cnt, ue_sync.state); } sf_cnt++; } // Main loop ue_dl_free(&ue_dl); ue_sync_free(&ue_sync); ue_mib_free(&ue_mib); cuhd_close(uhd); printf("\nBye\n"); exit(0); }