From dbc31958306f82eff1477ce12fd3e25bd85a8d34 Mon Sep 17 00:00:00 2001 From: ismagom Date: Sat, 2 Aug 2014 01:36:34 +0200 Subject: [PATCH] Improved cell search program --- lte/phy/examples/cell_search.c | 254 +++++++++++++----- lte/phy/include/liblte/phy/phy.h | 2 +- .../ue/{ue_cellsearch.h => ue_celldetect.h} | 28 +- lte/phy/lib/sync/src/sync.c | 90 ++----- .../src/{ue_cellsearch.c => ue_celldetect.c} | 36 +-- lte/phy/lib/ue/test/CMakeLists.txt | 4 +- ...cell_detect.c => ue_celldetect_mib_test.c} | 239 ++++++++-------- 7 files changed, 369 insertions(+), 284 deletions(-) rename lte/phy/include/liblte/phy/ue/{ue_cellsearch.h => ue_celldetect.h} (79%) rename lte/phy/lib/ue/src/{ue_cellsearch.c => ue_celldetect.c} (88%) rename lte/phy/lib/ue/test/{ue_cell_detect.c => ue_celldetect_mib_test.c} (60%) diff --git a/lte/phy/examples/cell_search.c b/lte/phy/examples/cell_search.c index 4b35684c3..501568bd5 100644 --- a/lte/phy/examples/cell_search.c +++ b/lte/phy/examples/cell_search.c @@ -46,32 +46,34 @@ #define FLEN 9600 #define FLEN_PERIOD 0.005 +#define MAX_EARFCN 1000 + int band = -1; int earfcn_start=-1, earfcn_end = -1; -int nof_frames_find=200; +int nof_frames_total = 50; +int nof_frames_detected = 10; +float threshold = -1; float uhd_gain = 60.0; char *uhd_args=""; -#define MAX_EARFCN 1000 -lte_earfcn_t channels[MAX_EARFCN]; - - void usage(char *prog) { - printf("Usage: %s [asefgv] -b band\n", prog); + printf("Usage: %s [agsendtvb] -b band\n", prog); printf("\t-a UHD args [Default %s]\n", uhd_args); printf("\t-g UHD gain [Default %.2f dB]\n", uhd_gain); printf("\t-s earfcn_start [Default All]\n"); printf("\t-e earfcn_end [Default All]\n"); - printf("\t-f nof_frames_find [Default %d]\n", nof_frames_find); + 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, "asefgvb")) != -1) { + while ((opt = getopt(argc, argv, "agsendtvb")) != -1) { switch(opt) { case 'a': uhd_args = argv[optind]; @@ -85,8 +87,14 @@ void parse_args(int argc, char **argv) { case 'e': earfcn_end = atoi(argv[optind]); break; - case 'f': - nof_frames_find = atoi(argv[optind]); + case 'n': + nof_frames_total = atoi(argv[optind]); + break; + case 'd': + nof_frames_detected = atoi(argv[optind]); + break; + case 't': + threshold = atof(argv[optind]); break; case 'g': uhd_gain = atof(argv[optind]); @@ -105,97 +113,195 @@ void parse_args(int argc, char **argv) { } } -int cuhd_recv_wrapper(void *h, void *data, uint32_t nsamples) { - DEBUG(" ---- Receive %d samples ---- \n", nsamples); - return cuhd_recv(h, data, nsamples, 1); +int decode_pbch(void *uhd, cf_t *buffer, ue_celldetect_result_t *found_cell) +{ + ue_mib_t uemib; + pbch_mib_t mib; + int n; + + bzero(&mib, sizeof(pbch_mib_t)); + + 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, &mib); + 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 < 2*nof_frames_total); + if (n == MIB_FOUND) { + printf("\n\nMIB decoded in %d ms (%d half frames)\n", nof_frames*5, nof_frames); + pbch_mib_fprint(stdout, &mib, found_cell->cell_id); + } 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); + 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("\n\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 ret; - int frame_cnt; - int nof_freqs; - uint32_t freq; - ue_sync_t uesync; + int n; void *uhd; + ue_celldetect_t s; + ue_celldetect_result_t found_cell; cf_t *buffer; - lte_cell_t cell; - - if (argc < 3) { - usage(argv[0]); - exit(-1); - } - - parse_args(argc,argv); - + int nof_freqs; + lte_earfcn_t channels[MAX_EARFCN]; + uint32_t freq; + + parse_args(argc, argv); + printf("Opening UHD device...\n"); if (cuhd_open(uhd_args, &uhd)) { fprintf(stderr, "Error opening uhd\n"); exit(-1); - } - - /* set uhd_gain */ + } cuhd_set_rx_gain(uhd, uhd_gain); - + nof_freqs = lte_band_get_fd_band(band, channels, earfcn_start, earfcn_end, MAX_EARFCN); if (nof_freqs < 0) { fprintf(stderr, "Error getting EARFCN list\n"); exit(-1); } + + 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); + } - for (freq=0;freqcp; } +static lte_cp_t detect_cp(cf_t *input, uint32_t peak_pos) +{ + return CPNORM; +} + int sync_sss(sync_t *q, cf_t *input, uint32_t peak_pos) { uint32_t m0, m1; - int sss_idx_n, sss_idx_e, ret; - float m0_value_e, m1_value_e,m0_value_n, m1_value_n; - uint32_t sf_idx_e, N_id_1_e, sf_idx_n, N_id_1_n; + int sss_idx, ret; + float m0_value, m1_value; sss_synch_set_N_id_2(&q->sss, q->N_id_2); - - /* Make sure we have enough room to find SSS sequence */ - sss_idx_n = (int) peak_pos - 2*(q->fft_size + CP(q->fft_size, CPNORM_LEN)); - sss_idx_e = (int) peak_pos - 2*(q->fft_size + CP(q->fft_size, CPEXT_LEN)); if (q->detect_cp) { - if (sss_idx_n < 0 || sss_idx_e < 0) { - INFO("Not enough room to decode SSS (%d, %d)\n", sss_idx_n, sss_idx_e); - return LIBLTE_SUCCESS; - } - } else { - if (CP_ISNORM(q->cp)) { - if (sss_idx_n < 0) { - INFO("Not enough room to decode normal CP SSS (sss_idx=%d, peak_pos=%d)\n", sss_idx_n, peak_pos); - return LIBLTE_SUCCESS; - } - } else { - if (sss_idx_e < 0) { - INFO("Not enough room to decode extended CP SSS (sss_idx=%d, peak_pos=%d)\n", sss_idx_e, peak_pos); - return LIBLTE_SUCCESS; - } - } + q->cp = detect_cp(input, peak_pos); } - - sf_idx_n = 0; - sf_idx_e = 0; - N_id_1_n = 0; - N_id_1_e = 0; - /* try Normal CP length */ - if (q->detect_cp || CP_ISNORM(q->cp)) { - sss_synch_m0m1(&q->sss, &input[sss_idx_n], &m0, &m0_value_n, &m1, &m1_value_n); + /* Make sure we have enough room to find SSS sequence */ + sss_idx = (int) peak_pos - 2*(q->fft_size + CP(q->fft_size, q->cp)); - sf_idx_n = sss_synch_subframe(m0, m1); - ret = sss_synch_N_id_1(&q->sss, m0, m1); - if (ret >= 0) { - N_id_1_n = (uint32_t) ret; - } else { - N_id_1_n = 1000; - } + if (sss_idx < 0) { + INFO("Not enough room to decode CP SSS (sss_idx=%d, peak_pos=%d)\n", sss_idx, peak_pos); + return LIBLTE_SUCCESS; } + + /* try Normal CP length */ + sss_synch_m0m1(&q->sss, &input[sss_idx], &m0, &m0_value, &m1, &m1_value); - if (q->detect_cp || CP_ISEXT(q->cp)) { - /* Now try Extended CP length */ - sss_synch_m0m1(&q->sss, &input[sss_idx_e], &m0, &m0_value_e, &m1, &m1_value_e); - - sf_idx_e = sss_synch_subframe(m0, m1); - ret = sss_synch_N_id_1(&q->sss, m0, m1); - if (ret >= 0) { - N_id_1_e = (uint32_t) ret; - } else { - N_id_1_e = 1000; - } - } - - /* Correlation with extended CP hypoteshis is greater than with normal? */ - if ((q->detect_cp && m0_value_e * m1_value_e > m0_value_n * m1_value_n) - || CP_ISEXT(q->cp)) { - q->cp = CPEXT; - q->sf_idx = sf_idx_e; - q->N_id_1 = N_id_1_e; - /* otherwise is normal CP */ + q->sf_idx = sss_synch_subframe(m0, m1); + ret = sss_synch_N_id_1(&q->sss, m0, m1); + if (ret >= 0) { + q->N_id_1 = (uint32_t) ret; } else { - q->cp = CPNORM; - q->sf_idx = sf_idx_n; - q->N_id_1 = N_id_1_n; + q->N_id_1 = 1000; } - - DEBUG("SSS detected N_id_1=%d, sf_idx=%d, position=%d/%d %s CP\n", - q->N_id_1, q->sf_idx, sss_idx_n, sss_idx_e, CP_ISNORM(q->cp)?"Normal":"Extended"); + + DEBUG("SSS detected N_id_1=%d, sf_idx=%d, %s CP\n", + q->N_id_1, q->sf_idx, CP_ISNORM(q->cp)?"Normal":"Extended"); return 1; } @@ -276,7 +236,7 @@ int sync_find(sync_t *q, cf_t *input, uint32_t find_offset, uint32_t *peak_posit } } } else { - printf("Warning: no space for CFO computation\n"); + INFO("Warning: no space for CFO computation\n",0); } if (peak_position) { diff --git a/lte/phy/lib/ue/src/ue_cellsearch.c b/lte/phy/lib/ue/src/ue_celldetect.c similarity index 88% rename from lte/phy/lib/ue/src/ue_cellsearch.c rename to lte/phy/lib/ue/src/ue_celldetect.c index 8bb6701d1..27cd1b5f8 100644 --- a/lte/phy/lib/ue/src/ue_cellsearch.c +++ b/lte/phy/lib/ue/src/ue_celldetect.c @@ -31,7 +31,7 @@ #include #include -#include "liblte/phy/ue/ue_cellsearch.h" +#include "liblte/phy/ue/ue_celldetect.h" #include "liblte/phy/utils/debug.h" #include "liblte/phy/utils/vector.h" @@ -39,19 +39,19 @@ #define FIND_FFTSIZE 64 #define FIND_SFLEN 5*SF_LEN(FIND_FFTSIZE) -int ue_cellsearch_init(ue_cellsearch_t * q) { - return ue_cellsearch_init_max(q, CS_DEFAULT_MAXFRAMES_TOTAL, CS_DEFAULT_MAXFRAMES_DETECTED); +int ue_celldetect_init(ue_celldetect_t * q) { + return ue_celldetect_init_max(q, CS_DEFAULT_MAXFRAMES_TOTAL, CS_DEFAULT_MAXFRAMES_DETECTED); } -int ue_cellsearch_init_max(ue_cellsearch_t * q, uint32_t max_frames_total, uint32_t max_frames_detected) { +int ue_celldetect_init_max(ue_celldetect_t * q, uint32_t max_frames_total, uint32_t max_frames_detected) { int ret = LIBLTE_ERROR_INVALID_INPUTS; if (q != NULL) { ret = LIBLTE_ERROR; - bzero(q, sizeof(ue_cellsearch_t)); + bzero(q, sizeof(ue_celldetect_t)); - q->candidates = malloc(sizeof(ue_cellsearch_result_t) * max_frames_detected); + q->candidates = malloc(sizeof(ue_celldetect_result_t) * max_frames_detected); if (!q->candidates) { perror("malloc"); goto clean_exit; @@ -78,19 +78,19 @@ int ue_cellsearch_init_max(ue_cellsearch_t * q, uint32_t max_frames_total, uint3 q->nof_frames_total = CS_DEFAULT_NOFFRAMES_TOTAL; q->nof_frames_detected = CS_DEFAULT_NOFFRAMES_DETECTED; - ue_cellsearch_reset(q); + ue_celldetect_reset(q); ret = LIBLTE_SUCCESS; } clean_exit: if (ret == LIBLTE_ERROR) { - ue_cellsearch_free(q); + ue_celldetect_free(q); } return ret; } -void ue_cellsearch_free(ue_cellsearch_t * q) +void ue_celldetect_free(ue_celldetect_t * q) { if (q->candidates) { free(q->candidates); @@ -105,21 +105,21 @@ void ue_cellsearch_free(ue_cellsearch_t * q) } -void ue_cellsearch_reset(ue_cellsearch_t * q) +void ue_celldetect_reset(ue_celldetect_t * q) { q->current_nof_detected = 0; q->current_nof_total = 0; q->current_N_id_2 = 0; } -void ue_cellsearch_set_threshold(ue_cellsearch_t * q, float threshold) +void ue_celldetect_set_threshold(ue_celldetect_t * q, float threshold) { sync_set_threshold(&q->sfind, threshold); } -int ue_cellsearch_set_nof_frames_total(ue_cellsearch_t * q, uint32_t nof_frames) +int ue_celldetect_set_nof_frames_total(ue_celldetect_t * q, uint32_t nof_frames) { - if (nof_frames > q->max_frames_total) { + if (nof_frames <= q->max_frames_total) { q->nof_frames_total = nof_frames; return LIBLTE_SUCCESS; } else { @@ -127,9 +127,9 @@ int ue_cellsearch_set_nof_frames_total(ue_cellsearch_t * q, uint32_t nof_frames) } } -int ue_cellsearch_set_nof_frames_detected(ue_cellsearch_t * q, uint32_t nof_frames) +int ue_celldetect_set_nof_frames_detected(ue_celldetect_t * q, uint32_t nof_frames) { - if (nof_frames > q->max_frames_detected) { + if (nof_frames <= q->max_frames_detected) { q->nof_frames_detected = nof_frames; return LIBLTE_SUCCESS; } else { @@ -138,7 +138,7 @@ int ue_cellsearch_set_nof_frames_detected(ue_cellsearch_t * q, uint32_t nof_fram } /* Decide the most likely cell based on the mode */ -void decide_cell(ue_cellsearch_t * q, ue_cellsearch_result_t *found_cell) +void decide_cell(ue_celldetect_t * q, ue_celldetect_result_t *found_cell) { uint32_t i, j; @@ -186,10 +186,10 @@ void decide_cell(ue_cellsearch_t * q, ue_cellsearch_result_t *found_cell) found_cell->mode = q->mode_ntimes[mode_pos]; } -int ue_cellsearch_scan(ue_cellsearch_t * q, +int ue_celldetect_scan(ue_celldetect_t * q, cf_t *signal, uint32_t nsamples, - ue_cellsearch_result_t *found_cell) + ue_celldetect_result_t *found_cell) { int ret = LIBLTE_ERROR_INVALID_INPUTS; uint32_t peak_idx; diff --git a/lte/phy/lib/ue/test/CMakeLists.txt b/lte/phy/lib/ue/test/CMakeLists.txt index e44f12b70..dc376a5de 100644 --- a/lte/phy/lib/ue/test/CMakeLists.txt +++ b/lte/phy/lib/ue/test/CMakeLists.txt @@ -29,8 +29,8 @@ 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_cell_detect ue_cell_detect.c) - TARGET_LINK_LIBRARIES(ue_cell_detect 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_cell_detect.c b/lte/phy/lib/ue/test/ue_celldetect_mib_test.c similarity index 60% rename from lte/phy/lib/ue/test/ue_cell_detect.c rename to lte/phy/lib/ue/test/ue_celldetect_mib_test.c index 6ef13f707..c3921aba4 100644 --- a/lte/phy/lib/ue/test/ue_cell_detect.c +++ b/lte/phy/lib/ue/test/ue_celldetect_mib_test.c @@ -40,7 +40,6 @@ #include "liblte/phy/phy.h" #include "liblte/cuhd/cuhd.h" -void *uhd; int nof_frames_total = CS_DEFAULT_NOFFRAMES_TOTAL; int nof_frames_detected = CS_DEFAULT_NOFFRAMES_DETECTED; @@ -95,83 +94,96 @@ void parse_args(int argc, char **argv) { } } -void input_init() { - - 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); - - INFO("Setting sampling frequency 960 KHz for PSS search\n", 0); - cuhd_set_rx_srate(uhd, 960000.0); - - DEBUG("Starting receiver...\n", 0); - cuhd_start_rx_stream(uhd); - -} - -int main(int argc, char **argv) { - ue_cellsearch_t s; +int decode_pbch(void *uhd, cf_t *buffer, ue_celldetect_result_t *found_cell) +{ ue_mib_t uemib; - ue_cellsearch_result_t found_cell; pbch_mib_t mib; - uint32_t flen; - cf_t *buffer; int n; - parse_args(argc, argv); + 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; + } - input_init(); + INFO("Calling ue_mib_decode() %d/%d\n", nof_frames, nof_frames_total); - // allocate for the maximum size (10 ms at 1.92 MHz for PBCH decoding) - buffer = vec_malloc(sizeof(cf_t) * 19200*30); - if (!buffer) { - perror("malloc"); - exit(-1); - } - - if (ue_cellsearch_init(&s)) { - fprintf(stderr, "Error initiating UE sync module\n"); - exit(-1); - } - if (threshold > 0) { - ue_cellsearch_set_threshold(&s, threshold); + n = ue_mib_decode(&uemib, buffer, flen, &mib); + 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)\n", nof_frames*5, nof_frames); + pbch_mib_fprint(stdout, &mib, found_cell->cell_id); + } else { + printf("\nCould not decode MIB\n"); } + + cuhd_stop_rx_stream(uhd); + cuhd_flush_buffer(uhd); + + ue_mib_free(&uemib); - if (nof_frames_total > 0) { - ue_cellsearch_set_nof_frames_total(&s, nof_frames_total); - } - if (nof_frames_detected > 0) { - ue_cellsearch_set_nof_frames_detected(&s, nof_frames_detected); - } + 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; - flen = 4800; - + uint32_t flen = 4800; + do { if (cuhd_recv(uhd, buffer, flen, 1)<0) { fprintf(stderr, "Error receiving from USRP\n"); - exit(-1); + return LIBLTE_ERROR; } - n = ue_cellsearch_scan(&s, buffer, flen, &found_cell); + n = ue_celldetect_scan(s, buffer, flen, found_cell); switch(n) { case CS_FRAME_UNALIGNED: - fprintf(stderr, "Unaliged frame!! Exiting\n"); - exit(-1); + 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: - 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); - + 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: @@ -180,65 +192,72 @@ int main(int argc, char **argv) { case LIBLTE_ERROR: case LIBLTE_ERROR_INVALID_INPUTS: fprintf(stderr, "Error calling cellsearch_scan()\n"); - exit(-1); + 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); - if (n == CS_CELL_DETECTED) { - INFO("Stopping receiver...\n", 0); - cuhd_stop_rx_stream(uhd); - - cuhd_flush_buffer(uhd); - - if (ue_mib_init(&uemib, found_cell.cell_id, found_cell.cp)) { - fprintf(stderr, "Error initiating PBCH decoder\n"); - exit(-1); - } + return n; +} - 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); - - usleep(50000); - - uint32_t nof_frames = 0; - flen = MIB_FRAME_SIZE; +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); - do { - if (cuhd_recv(uhd, buffer, flen, 1)<0) { - fprintf(stderr, "Error receiving from USRP\n"); - exit(-1); - } - - INFO("Calling ue_mib_decode() %d/%d\n", nof_frames, nof_frames_total); - - n = ue_mib_decode(&uemib, buffer, flen, &mib); - if (n == LIBLTE_ERROR || n == LIBLTE_ERROR_INVALID_INPUTS) { - fprintf(stderr, "Error calling ue_mib_decode()\n"); - exit(-1); - } - 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"); - exit(-1); - } - } - 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)\n", nof_frames*5, nof_frames); - pbch_mib_fprint(stdout, &mib, found_cell.cell_id); - } else { - printf("\nCould not decode MIB\n"); - } - } + 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; + } - ue_mib_free(&uemib); - ue_cellsearch_free(&s); + 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); }