diff --git a/lte/examples/cell_search_utils.c b/lte/examples/cell_search_utils.c index 083e33117..873e17ede 100644 --- a/lte/examples/cell_search_utils.c +++ b/lte/examples/cell_search_utils.c @@ -137,6 +137,8 @@ int find_cell(void *uhd, ue_celldetect_result_t *found_cell, uint32_t N_id_2) uint32_t flen = 4800; int n; + ue_celldetect_set_N_id_2(&cd, N_id_2); + do { if (cuhd_recv(uhd, buffer, flen, 1)<0) { fprintf(stderr, "Error receiving from USRP\n"); @@ -145,7 +147,7 @@ int find_cell(void *uhd, ue_celldetect_result_t *found_cell, uint32_t N_id_2) DEBUG("Scanning cell at N_id_2=%d\n",N_id_2); - n = ue_celldetect_scan(&cd, buffer, flen, found_cell, N_id_2); + n = ue_celldetect_scan(&cd, buffer, flen); switch(n) { case CS_FRAME_UNALIGNED: printf("Realigning frame\n"); @@ -157,6 +159,7 @@ int find_cell(void *uhd, ue_celldetect_result_t *found_cell, uint32_t N_id_2) ret = -1; goto free_and_exit; case CS_CELL_DETECTED: + ue_celldetect_get_cell(&cd, found_cell); if (found_cell->peak > 0) { printf("\n\tCELL ID: %d, CP: %s, Peak: %.2f, Mode: %d/%d\n", found_cell->cell_id, diff --git a/lte/phy/include/liblte/phy/ue/ue_celldetect.h b/lte/phy/include/liblte/phy/ue/ue_celldetect.h index 46984274d..7ce2f86c9 100644 --- a/lte/phy/include/liblte/phy/ue/ue_celldetect.h +++ b/lte/phy/include/liblte/phy/ue/ue_celldetect.h @@ -106,9 +106,13 @@ LIBLTE_API void ue_celldetect_reset(ue_celldetect_t *q); LIBLTE_API int ue_celldetect_scan(ue_celldetect_t *q, cf_t *signal, - uint32_t nsamples, - ue_celldetect_result_t *found_cell, - uint32_t N_id_2); + uint32_t nsamples); + +LIBLTE_API int ue_celldetect_set_N_id_2(ue_celldetect_t *q, + uint32_t N_id_2); + +LIBLTE_API void ue_celldetect_get_cell(ue_celldetect_t * q, + ue_celldetect_result_t *found_cell); LIBLTE_API int ue_celldetect_set_nof_frames_total(ue_celldetect_t *q, uint32_t nof_frames); diff --git a/lte/phy/lib/ue/src/ue_celldetect.c b/lte/phy/lib/ue/src/ue_celldetect.c index 909dd3f69..48711fbaa 100644 --- a/lte/phy/lib/ue/src/ue_celldetect.c +++ b/lte/phy/lib/ue/src/ue_celldetect.c @@ -137,7 +137,7 @@ int ue_celldetect_set_nof_frames_detected(ue_celldetect_t * q, uint32_t nof_fram } /* Decide the most likely cell based on the mode */ -void decide_cell(ue_celldetect_t * q, ue_celldetect_result_t *found_cell) +void ue_celldetect_get_cell(ue_celldetect_t * q, ue_celldetect_result_t *found_cell) { uint32_t i, j; @@ -183,13 +183,16 @@ void decide_cell(ue_celldetect_t * q, ue_celldetect_result_t *found_cell) found_cell->cp = CPEXT; } found_cell->mode = q->mode_ntimes[mode_pos]; + q->current_nof_detected = q->current_nof_total = 0; +} + +int ue_celldetect_set_N_id_2(ue_celldetect_t *q, uint32_t N_id_2) { + return sync_set_N_id_2(&q->sfind, N_id_2); } int ue_celldetect_scan(ue_celldetect_t * q, cf_t *signal, - uint32_t nsamples, - ue_celldetect_result_t *found_cell, - uint32_t N_id_2) + uint32_t nsamples) { int ret = LIBLTE_ERROR_INVALID_INPUTS; uint32_t peak_idx; @@ -198,8 +201,7 @@ int ue_celldetect_scan(ue_celldetect_t * q, if (q != NULL && signal != NULL && - nsamples >= 4800 && - lte_N_id_2_isvalid(N_id_2)) + nsamples >= 4800) { ret = LIBLTE_SUCCESS; @@ -210,12 +212,9 @@ int ue_celldetect_scan(ue_celldetect_t * q, nof_input_frames = nsamples/4800; for (uint32_t nf=0;nfsfind, N_id_2)) { - return LIBLTE_ERROR; - } INFO("[%3d/%3d]: Searching cells with N_id_2=%d. %d frames\n", - q->current_nof_detected, q->current_nof_total, N_id_2, nof_input_frames); + q->current_nof_detected, q->current_nof_total, q->sfind.N_id_2, nof_input_frames); /* Find peak and cell id */ ret = sync_find(&q->sfind, &signal[nf*4800], 0, &peak_idx); @@ -251,8 +250,6 @@ int ue_celldetect_scan(ue_celldetect_t * q, /* Decide cell ID and CP if we detected up to nof_frames_detected */ if (q->current_nof_detected == q->nof_frames_detected) { - decide_cell(q, found_cell); - q->current_nof_detected = q->current_nof_total = 0; ret = CS_CELL_DETECTED; } else if (q->current_nof_total == q->nof_frames_total) { q->current_nof_detected = q->current_nof_total = 0; diff --git a/lte/phy/lib/ue/test/CMakeLists.txt b/lte/phy/lib/ue/test/CMakeLists.txt index dc376a5de..614ac207f 100644 --- a/lte/phy/lib/ue/test/CMakeLists.txt +++ b/lte/phy/lib/ue/test/CMakeLists.txt @@ -28,9 +28,6 @@ LIST(FIND OPTIONAL_LIBS graphics GRAPHICS_FIND) IF(${CUHD_FIND} GREATER -1) ADD_EXECUTABLE(ue_sync_usrp ue_sync_usrp.c) TARGET_LINK_LIBRARIES(ue_sync_usrp lte_phy cuhd) - - ADD_EXECUTABLE(ue_celldetect_mib_test ue_celldetect_mib_test.c) - TARGET_LINK_LIBRARIES(ue_celldetect_mib_test lte_phy cuhd) ENDIF(${CUHD_FIND} GREATER -1) IF(${GRAPHICS_FIND} EQUAL -1) diff --git a/lte/phy/lib/ue/test/ue_celldetect_mib_test.c b/lte/phy/lib/ue/test/ue_celldetect_mib_test.c deleted file mode 100644 index 040f60baf..000000000 --- a/lte/phy/lib/ue/test/ue_celldetect_mib_test.c +++ /dev/null @@ -1,266 +0,0 @@ -/** - * - * \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 "liblte/phy/phy.h" - -#include "liblte/cuhd/cuhd.h" - -int nof_frames_total = CS_DEFAULT_NOFFRAMES_TOTAL; -int nof_frames_detected = CS_DEFAULT_NOFFRAMES_DETECTED; -float threshold = -1; - -float uhd_freq = 0.0, uhd_gain = 20.0; -char *uhd_args = ""; - -void usage(char *prog) { - printf("Usage: %s [agntdv] -f uhd_freq\n", prog); - printf("\t-a UHD args [Default %s]\n", uhd_args); - printf("\t-g UHD RX gain [Default %.2f dB]\n", uhd_gain); - printf("\t-n nof_frames_total [Default 100]\n"); - printf("\t-d nof_frames_detected [Default 10]\n"); - printf("\t-t threshold [Default %.2f]\n",threshold); - printf("\t-v [set verbose to debug, default none]\n"); -} - -void parse_args(int argc, char **argv) { - int opt; - while ((opt = getopt(argc, argv, "agndtvf")) != -1) { - switch (opt) { - case 'n': - nof_frames_total = atoi(argv[optind]); - break; - case 'd': - nof_frames_detected = atoi(argv[optind]); - break; - case 'a': - uhd_args = argv[optind]; - break; - case 'g': - uhd_gain = atof(argv[optind]); - break; - case 'f': - uhd_freq = atof(argv[optind]); - break; - case 't': - threshold = atof(argv[optind]); - break; - case 'v': - verbose++; - break; - default: - usage(argv[0]); - exit(-1); - } - } - if (uhd_freq == 0.0) { - usage(argv[0]); - exit(-1); - } -} - -int decode_pbch(void *uhd, cf_t *buffer, ue_celldetect_result_t *found_cell) -{ - ue_mib_t uemib; - uint8_t bch_payload[BCH_PAYLOAD_LEN]; - int n; - uint32_t nof_tx_ports; - - uint32_t nof_frames = 0; - uint32_t flen = MIB_FRAME_SIZE; - - if (ue_mib_init(&uemib, found_cell->cell_id, found_cell->cp)) { - fprintf(stderr, "Error initiating PBCH decoder\n"); - return LIBLTE_ERROR; - } - - INFO("Setting sampling frequency 1.92 MHz for PBCH decoding\n", 0); - cuhd_set_rx_srate(uhd, 1920000.0); - INFO("Starting receiver...\n", 0); - cuhd_start_rx_stream(uhd); - - do { - if (cuhd_recv(uhd, buffer, flen, 1)<0) { - fprintf(stderr, "Error receiving from USRP\n"); - return LIBLTE_ERROR; - } - - INFO("Calling ue_mib_decode() %d/%d\n", nof_frames, nof_frames_total); - - n = ue_mib_decode(&uemib, buffer, flen, bch_payload, &nof_tx_ports, NULL); - if (n == LIBLTE_ERROR || n == LIBLTE_ERROR_INVALID_INPUTS) { - fprintf(stderr, "Error calling ue_mib_decode()\n"); - return LIBLTE_ERROR; - } - if (n == MIB_FRAME_UNALIGNED) { - printf("Realigning frame\n"); - if (cuhd_recv(uhd, buffer, flen/2, 1)<0) { - fprintf(stderr, "Error receiving from USRP\n"); - return LIBLTE_ERROR; - } - } - nof_frames++; - } while (n != MIB_FOUND && nof_frames < nof_frames_total); - if (n == MIB_FOUND) { - printf("\n\nMIB decoded in %d ms (%d half frames). %d TX ports\n", nof_frames*5, nof_frames, nof_tx_ports); - vec_fprint_hex(stdout, bch_payload, BCH_PAYLOAD_LEN); - } else { - printf("\nCould not decode MIB\n"); - } - - cuhd_stop_rx_stream(uhd); - cuhd_flush_buffer(uhd); - - ue_mib_free(&uemib); - - return LIBLTE_SUCCESS; -} - -int find_cell(void *uhd, ue_celldetect_t *s, cf_t *buffer, ue_celldetect_result_t *found_cell) -{ - int n; - - INFO("Setting sampling frequency 960 KHz for PSS search\n", 0); - cuhd_set_rx_srate(uhd, 960000.0); - INFO("Starting receiver...\n", 0); - cuhd_start_rx_stream(uhd); - - uint32_t nof_scanned_cells = 0; - uint32_t flen = 4800; - - do { - - if (cuhd_recv(uhd, buffer, flen, 1)<0) { - fprintf(stderr, "Error receiving from USRP\n"); - return LIBLTE_ERROR; - } - - n = ue_celldetect_scan(s, buffer, flen, found_cell, nof_scanned_cells); - switch(n) { - case CS_FRAME_UNALIGNED: - printf("Realigning frame\n"); - if (cuhd_recv(uhd, buffer, flen/2, 1)<0) { - fprintf(stderr, "Error receiving from USRP\n"); - return LIBLTE_ERROR; - } - return LIBLTE_ERROR; - case CS_CELL_DETECTED: - if (found_cell->peak > 0) { - printf("\tCELL ID: %d, CP: %s, Peak: %.2f, Mode: %d/%d\n", - found_cell->cell_id, lte_cp_string(found_cell->cp), - found_cell->peak, found_cell->mode, s->nof_frames_detected); - } - nof_scanned_cells++; - break; - case CS_CELL_NOT_DETECTED: - nof_scanned_cells++; - break; - case LIBLTE_ERROR: - case LIBLTE_ERROR_INVALID_INPUTS: - fprintf(stderr, "Error calling cellsearch_scan()\n"); - return LIBLTE_ERROR; - } - } while(nof_scanned_cells < 3 && n != CS_CELL_DETECTED); - - INFO("Stopping receiver...\n", 0); - cuhd_stop_rx_stream(uhd); - cuhd_flush_buffer(uhd); - - return n; -} - -int main(int argc, char **argv) { - int n; - void *uhd; - ue_celldetect_t s; - ue_celldetect_result_t found_cell; - cf_t *buffer; - - parse_args(argc, argv); - - printf("Opening UHD device...\n"); - if (cuhd_open(uhd_args, &uhd)) { - fprintf(stderr, "Error opening uhd\n"); - exit(-1); - } - cuhd_set_rx_gain(uhd, uhd_gain); - - /* set uhd_freq */ - cuhd_set_rx_freq(uhd, (double) uhd_freq); - cuhd_rx_wait_lo_locked(uhd); - DEBUG("Set uhd_freq to %.3f MHz\n", (double ) uhd_freq/1000000); - - buffer = vec_malloc(sizeof(cf_t) * 96000); - if (!buffer) { - perror("malloc"); - return LIBLTE_ERROR; - } - - if (ue_celldetect_init(&s)) { - fprintf(stderr, "Error initiating UE sync module\n"); - exit(-1); - } - if (threshold > 0) { - ue_celldetect_set_threshold(&s, threshold); - } - - if (nof_frames_total > 0) { - ue_celldetect_set_nof_frames_total(&s, nof_frames_total); - } - if (nof_frames_detected > 0) { - ue_celldetect_set_nof_frames_detected(&s, nof_frames_detected); - } - - n = find_cell(uhd, &s, buffer, &found_cell); - if (n < 0) { - fprintf(stderr, "Error searching cell\n"); - exit(-1); - } - if (n == CS_CELL_DETECTED) { - if (decode_pbch(uhd, buffer, &found_cell)) { - fprintf(stderr, "Error decoding PBCH\n"); - exit(-1); - } - } - - ue_celldetect_free(&s); - cuhd_close(uhd); - exit(0); -} - -