diff --git a/lib/location/CMakeLists.txt b/lib/location/CMakeLists.txt index 84e16468d0ad..5125dbb81806 100644 --- a/lib/location/CMakeLists.txt +++ b/lib/location/CMakeLists.txt @@ -9,9 +9,12 @@ zephyr_library_sources(location.c) zephyr_library_sources(location_core.c) zephyr_library_sources(location_utils.c) zephyr_library_sources_ifdef(CONFIG_LOCATION_METHOD_GNSS method_gnss.c) -zephyr_library_sources_ifdef(CONFIG_LOCATION_METHOD_CELLULAR scan_cellular.c) zephyr_library_sources_ifdef(CONFIG_LOCATION_METHOD_WIFI scan_wifi.c) +if(CONFIG_LOCATION_METHOD_CELLULAR OR CONFIG_LOCATION_METHOD_GNSS) +zephyr_library_sources(scan_cellular.c) +endif() + if(CONFIG_LOCATION_METHOD_CELLULAR OR CONFIG_LOCATION_METHOD_WIFI) zephyr_library_sources(method_cloud_location.c) add_subdirectory(cloud_service) diff --git a/lib/location/location_utils.c b/lib/location/location_utils.c index 118bc12b70dd..77bc970e3566 100644 --- a/lib/location/location_utils.c +++ b/lib/location/location_utils.c @@ -55,61 +55,6 @@ bool location_utils_is_lte_available(void) return is_active; } -int location_utils_modem_params_read(struct location_utils_modem_params_info *modem_params) -{ - /* Parsed strings include double quotes */ - char plmn_str[MODEM_PARAM_STR_MAX_LEN + 1] = { 0 }; - char tac_str[MODEM_PARAM_STR_MAX_LEN + 1] = { 0 }; - char cell_id_str[MODEM_PARAM_STR_MAX_LEN + 1] = { 0 }; - int err = 0; - - __ASSERT_NO_MSG(modem_params != NULL); - - err = nrf_modem_at_scanf( - "AT%XMONITOR", - "%%XMONITOR: " - "%*d" /* : ignored */ - ",%*[^,]" /* : ignored */ - ",%*[^,]" /* : ignored */ - ",%"L(MODEM_PARAM_STR_MAX_LEN)"[^,]" /* */ - ",%"L(MODEM_PARAM_STR_MAX_LEN)"[^,]" /* */ - ",%*d" /* : ignored */ - ",%*d" /* : ignored */ - ",%"L(MODEM_PARAM_STR_MAX_LEN)"[^,]" /* */ - ",%d", /* */ - plmn_str, tac_str, cell_id_str, &modem_params->phys_cell_id); - - if (err <= 2) { - LOG_ERR("Cannot get modem parameters, err %d", err); - } else { - /* Indicate success to the caller with zero return value */ - err = 0; - - /* Read MNC and store as integer. The MNC starts as the fifth character - * in the string, following double quote and three characters long MCC. - */ - modem_params->mnc = strtol(&plmn_str[4], NULL, 10); - - /* Null-terminated MCC, read and store it. */ - plmn_str[4] = '\0'; - - modem_params->mcc = strtol(plmn_str + 1, NULL, 10); - - /* */ - modem_params->tac = strtol(tac_str + 1, NULL, 16); - - /* */ - modem_params->cell_id = strtol(cell_id_str + 1, NULL, 16); - - LOG_DBG("parsed modem parameters: " - "mcc %d, mnc %d, tac %d (string: %s), cell_id %d (string: %s) phys_cell_id %d", - modem_params->mcc, modem_params->mnc, modem_params->tac, - tac_str, modem_params->cell_id, cell_id_str, - modem_params->phys_cell_id); - } - return err; -} - #else /* CONFIG_NRF_MODEM_LIB */ bool location_utils_is_lte_available(void) @@ -117,12 +62,7 @@ bool location_utils_is_lte_available(void) return false; } -int location_utils_modem_params_read(struct location_utils_modem_params_info *modem_params) -{ - return 0; -} - -#endif /* CONFIG_NRF_MODEM_LIB*/ +#endif /* CONFIG_NRF_MODEM_LIB */ const char *location_utils_nrf_cloud_jwt_generate(void) { diff --git a/lib/location/location_utils.h b/lib/location/location_utils.h index eb4359660b34..9149d7020fa1 100644 --- a/lib/location/location_utils.h +++ b/lib/location/location_utils.h @@ -9,24 +9,6 @@ #include -/** Modem parameters. */ -struct location_utils_modem_params_info { - /** Mobile Country Code. */ - int mcc; - - /** Mobile Network Code. */ - int mnc; - - /** E-UTRAN cell ID */ - uint32_t cell_id; - - /** Tracking area code. */ - uint32_t tac; - - /** Physical cell ID. */ - uint16_t phys_cell_id; -}; - /** * @brief Check if LTE networking is available. * @@ -38,16 +20,6 @@ struct location_utils_modem_params_info { */ bool location_utils_is_lte_available(void); -/** - * @brief Read modem parameters. - * - * @param[out] modem_params Context where parameters are filled. - * - * @retval true If modem parameters were received successfully. - * @retval false If modem parameters were not received successfully. - */ -int location_utils_modem_params_read(struct location_utils_modem_params_info *modem_params); - /** * @brief Generate JWT buffer to be used for nRF Cloud REST API use. * diff --git a/lib/location/method_cloud_location.c b/lib/location/method_cloud_location.c index c9788f717d2a..dce31a35fddb 100644 --- a/lib/location/method_cloud_location.c +++ b/lib/location/method_cloud_location.c @@ -65,13 +65,13 @@ static void method_cloud_location_positioning_work_fn(struct k_work *work) #if defined(CONFIG_LOCATION_METHOD_WIFI) if (wifi_config != NULL) { - err = scan_wifi_start(&wifi_scan_ready); + scan_wifi_start(&wifi_scan_ready); } #endif #if defined(CONFIG_LOCATION_METHOD_CELLULAR) if (cell_config != NULL) { - err = scan_cellular_start(cell_config->cell_count); + scan_cellular_start(cell_config->cell_count, false); scan_cellular_info = scan_cellular_results_get(); } #endif diff --git a/lib/location/method_gnss.c b/lib/location/method_gnss.c index 9207b7629bdd..2459138c67e1 100644 --- a/lib/location/method_gnss.c +++ b/lib/location/method_gnss.c @@ -15,6 +15,7 @@ #include "location_core.h" #include "location_utils.h" #if defined(CONFIG_NRF_CLOUD_AGPS) +#include "scan_cellular.h" #include #include #include @@ -289,19 +290,20 @@ static void method_gnss_nrf_cloud_agps_request(void) }; struct lte_lc_cells_info net_info = {0}; - struct location_utils_modem_params_info modem_params = { 0 }; + struct lte_lc_cells_info *scan_results; /* Get network info for the A-GPS location request. */ - err = location_utils_modem_params_read(&modem_params); - - if (err < 0) { + scan_cellular_start(0, true); + scan_results = scan_cellular_results_get(); + if (scan_results == NULL) { LOG_WRN("Requesting A-GPS data without location assistance"); } else { - net_info.current_cell.id = modem_params.cell_id; - net_info.current_cell.tac = modem_params.tac; - net_info.current_cell.mcc = modem_params.mcc; - net_info.current_cell.mnc = modem_params.mnc; - net_info.current_cell.phys_cell_id = modem_params.phys_cell_id; + net_info.current_cell.mcc = scan_results->current_cell.mcc; + net_info.current_cell.mnc = scan_results->current_cell.mnc; + net_info.current_cell.id = scan_results->current_cell.id; + net_info.current_cell.tac = scan_results->current_cell.tac; + net_info.current_cell.phys_cell_id = scan_results->current_cell.phys_cell_id; + net_info.current_cell.rsrp = scan_results->current_cell.rsrp; request.net_info = &net_info; } @@ -1201,5 +1203,14 @@ int method_gnss_init(void) method_gnss_modem_sleep_notif_subscribe(MIN_SLEEP_DURATION_FOR_STARTING_GNSS); #endif lte_lc_register_handler(method_gnss_lte_ind_handler); + +#if !defined(CONFIG_LOCATION_METHOD_CELLULAR) + /* Cellular location method is disabled, but GNSS method uses the cellular scan + * functionality for A-GNSS request. The module needs to be initialized explicitly, because + * init is not called by core. + */ + scan_cellular_init(); +#endif + return 0; } diff --git a/lib/location/scan_cellular.c b/lib/location/scan_cellular.c index c4e529320c54..70cf91b82a6d 100644 --- a/lib/location/scan_cellular.c +++ b/lib/location/scan_cellular.c @@ -84,11 +84,11 @@ void scan_cellular_lte_ind_handler(const struct lte_lc_evt *const evt) } } -int scan_cellular_start(uint8_t cell_count) +void scan_cellular_start(uint8_t cell_count, bool light_search) { - struct location_utils_modem_params_info modem_params = { 0 }; struct lte_lc_ncellmeas_params ncellmeas_params = { - .search_type = LTE_LC_NEIGHBOR_SEARCH_TYPE_EXTENDED_COMPLETE, + .search_type = light_search ? LTE_LC_NEIGHBOR_SEARCH_TYPE_EXTENDED_LIGHT : + LTE_LC_NEIGHBOR_SEARCH_TYPE_EXTENDED_COMPLETE, .gci_count = 0 }; int err; @@ -103,26 +103,7 @@ int scan_cellular_start(uint8_t cell_count) err = lte_lc_neighbor_cell_measurement(&ncellmeas_params); if (err) { - LOG_WRN("Failed to initiate neighbor cell measurements: %d, " - "next: fallback to get modem parameters", - err); - - /* Doing fallback to get only the mandatory items manually: - * cell id, mcc, mnc and tac - */ - err = location_utils_modem_params_read(&modem_params); - if (err < 0) { - LOG_ERR("Could not obtain modem parameters"); - } else { - memset(&scan_cellular_info, 0, sizeof(struct lte_lc_cells_info)); - - /* Filling only the mandatory parameters */ - scan_cellular_info.current_cell.mcc = modem_params.mcc; - scan_cellular_info.current_cell.mnc = modem_params.mnc; - scan_cellular_info.current_cell.tac = modem_params.tac; - scan_cellular_info.current_cell.id = modem_params.cell_id; - scan_cellular_info.current_cell.phys_cell_id = modem_params.phys_cell_id; - } + LOG_ERR("Failed to initiate neighbor cell measurements: %d", err); goto end; } err = k_sem_take(&scan_cellular_sem_ncellmeas_evt, K_FOREVER); @@ -160,7 +141,6 @@ int scan_cellular_start(uint8_t cell_count) end: running = false; - return err; } int scan_cellular_cancel(void) diff --git a/lib/location/scan_cellular.h b/lib/location/scan_cellular.h index f9e86c620ca9..b45a585dee30 100644 --- a/lib/location/scan_cellular.h +++ b/lib/location/scan_cellular.h @@ -10,7 +10,7 @@ #include int scan_cellular_init(void); -int scan_cellular_start(uint8_t cell_count); +void scan_cellular_start(uint8_t cell_count, bool light_search); struct lte_lc_cells_info *scan_cellular_results_get(void); int scan_cellular_cancel(void); diff --git a/lib/location/scan_wifi.c b/lib/location/scan_wifi.c index 6051ccc3f4b7..7ee8049c55be 100644 --- a/lib/location/scan_wifi.c +++ b/lib/location/scan_wifi.c @@ -46,7 +46,7 @@ struct wifi_scan_info *scan_wifi_results_get(void) return &scan_wifi_info; } -int scan_wifi_start(struct k_sem *wifi_scan_ready) +void scan_wifi_start(struct k_sem *wifi_scan_ready) { int ret; @@ -64,7 +64,6 @@ int scan_wifi_start(struct k_sem *wifi_scan_ready) k_sem_give(wifi_scan_ready); wifi_scan_ready = NULL; } - return ret; } static void scan_wifi_result_handle(struct net_mgmt_event_callback *cb) diff --git a/lib/location/scan_wifi.h b/lib/location/scan_wifi.h index 1b133a9de124..30a4c365be6b 100644 --- a/lib/location/scan_wifi.h +++ b/lib/location/scan_wifi.h @@ -10,7 +10,7 @@ #include int scan_wifi_init(void); -int scan_wifi_start(struct k_sem *wifi_scan_ready); +void scan_wifi_start(struct k_sem *wifi_scan_ready); struct wifi_scan_info *scan_wifi_results_get(void); int scan_wifi_cancel(void);