Location
The location library provides functionality for retrieving the location of a device using different positioning methods such as:
GNSS satellite positioning including Assisted GPS (A-GPS) and Predicted GPS (P-GPS) data.
Cellular positioning.
Wi-Fi positioning.
Overview
This library provides an API for applications to request the location of a device. The application can determine the preferred order of the location methods to be used along with other configuration information. If a method fails to provide the location, the library performs a fallback to the next preferred method.
Both cellular and Wi-Fi positioning detect the base stations and use web services for retrieving the location. GNSS positioning uses satellites to compute the location of the device. This library can use the assistance data (A-GPS and P-GPS) to find the satellites faster.
Implementation
The location library has a compact API and a location core that handles the functionality that is independent of the location method, such as fallback to the next preferred method and timeouts. Each location method has its own implementation for the location retrieval:
GNSS positioning
GNSS interface for getting the location.
A-GPS and P-GPS are managed with nRF Cloud A-GPS and nRF Cloud P-GPS.
The application may also use some other source for the data and use
location_agps_data_process()
to pass the data to the location library.The data format of A-GPS or P-GPS must be as received from nRF Cloud A-GPS.
The data transport method for nRF Cloud A-GPS and nRF Cloud P-GPS can be configured to be either MQTT (
CONFIG_NRF_CLOUD_MQTT
) or REST (CONFIG_NRF_CLOUD_REST
).If different transport is desired for different location methods, (
CONFIG_NRF_CLOUD_MQTT
) and (CONFIG_NRF_CLOUD_REST
) can be enabled simultaneously. In such a case, MQTT takes precedence as the transport method of GNSS assistance data.Note that acquiring GNSS fix only starts when LTE connection, more specifically Radio Resource Control (RRC) connection, is idle.
Also, if A-GPS is not used and Power Saving Mode (PSM) is enabled, Location library will wait for the modem to enter PSM.
Selectable location accuracy (low/normal/high).
Cellular positioning
LTE link controller for getting visible cellular base stations.
Multicell location for sending cell information to the selected location service and getting the calculated location back to the device.
The service is selected in the
location_method_config
structure when requesting for location.The services available are nRF Cloud Location Services, HERE Positioning, Skyhook Precision Location and Polte Location API
The data transport method for the service is mainly REST. However, either MQTT (
CONFIG_NRF_CLOUD_MQTT
) or REST (CONFIG_NRF_CLOUD_REST
) can be configured for nRF Cloud Location Services.
Wi-Fi positioning
Zephyr’s Network Management API Network Management for getting the visible Wi-Fi access points.
Sending access point information to the selected location service and getting the calculated location back to the device:
The location library has an implementation for the Wi-Fi location services.
The service is selected in the
location_method_config
structure when requesting for location.The services available are nRF Cloud Location Services, HERE Positioning and Skyhook Precision Location.
The data transport method for the service is REST.
Requirements
nRF Cloud certificates
When using nRF Cloud for any location data, you must have the certificate provisioned. See Updating the nRF Cloud certificate for more information. nRF9160 DK comes pre-provisioned with certificates for nRF Cloud.
Location service accounts
To use the location services that provide A-GPS or P-GPS, cellular or Wi-Fi positioning data, see the respective documentation for setting up your account and getting the required credentials for authentication:
You can configure the required credentials for the location services using Kconfig options.
Wi-Fi chip
None of the supported DKs have a Wi-Fi chip. You can use external Wi-Fi chip, such as ESP8266, and connect it to the nRF9160 DK. You can see nRF9160: Location and its DTC overlay for some more information on ESP8266 integration.
Library files
This library is found under lib/location
in the nRF Connect SDK folder structure.
Configuration
Configure the following Kconfig options to enable this library:
CONFIG_LOCATION
- Enables the Location library.CONFIG_NRF_MODEM_LIB
- Enable modem library.CONFIG_LTE_LINK_CONTROL
- Enable LTE link control.
Configure the following Kconfig options to enable Wi-Fi interface:
CONFIG_WIFI
- Enable Wi-Fi for Zephyr.CONFIG_LOCATION_METHOD_WIFI_DEV_NAME
- Name of the Wi-Fi device.
Configure the following options to enable location methods of your choice:
CONFIG_LOCATION_METHOD_GNSS
- Enables GNSS location method.CONFIG_LOCATION_METHOD_CELLULAR
- Enables cellular location method.CONFIG_LOCATION_METHOD_WIFI
- Enables Wi-Fi location method.
The following options control the use of GNSS assistance data:
CONFIG_LOCATION_METHOD_GNSS_AGPS_EXTERNAL
- Enables A-GPS data retrieval from an external source, implemented separately by the application. If enabled, the library triggers aLOCATION_EVT_GNSS_ASSISTANCE_REQUEST
event when assistance is needed. Once the application has obtained the assistance data, it should call thelocation_agps_data_process()
function to feed it into the library.CONFIG_LOCATION_METHOD_GNSS_PGPS_EXTERNAL
- Enables P-GPS data retrieval from an external source, implemented separately by the application. If enabled, the library triggers aLOCATION_EVT_GNSS_PREDICTION_REQUEST
event when assistance is needed. Once the application has obtained the assistance data, it should call thelocation_pgps_data_process()
function to feed it into the library.CONFIG_NRF_CLOUD_AGPS
- Enables A-GPS data retrieval from nRF Cloud.CONFIG_NRF_CLOUD_PGPS
- Enables P-GPS data retrieval from nRF Cloud.CONFIG_NRF_CLOUD_AGPS_FILTERED
- Reduces assistance size by only downloading ephemerides for visible satellites.
The following option is useful when setting CONFIG_NRF_CLOUD_AGPS_FILTERED
:
CONFIG_NRF_CLOUD_AGPS_ELEVATION_MASK
- Sets elevation threshold angle.
The following options control the transport method used with nRF Cloud:
CONFIG_NRF_CLOUD_REST
- Uses REST APIs to communicate with nRF Cloud ifCONFIG_NRF_CLOUD_MQTT
is not set.CONFIG_NRF_CLOUD_MQTT
- Uses MQTT transport to communicate with nRF Cloud.CONFIG_REST_CLIENT
- Enable REST client library.
Both cellular and Wi-Fi location services are selected using the runtime configuration but the available services must be configured first. For cellular location services, use at least one of the following sets of options and configure corresponding authentication parameters (for more details and configuration options, see Multicell location):
CONFIG_MULTICELL_LOCATION_SERVICE_HERE
andCONFIG_MULTICELL_LOCATION_HERE_API_KEY
CONFIG_MULTICELL_LOCATION_SERVICE_SKYHOOK
andCONFIG_MULTICELL_LOCATION_SKYHOOK_API_KEY
CONFIG_MULTICELL_LOCATION_SERVICE_POLTE
andCONFIG_MULTICELL_LOCATION_POLTE_CUSTOMER_ID
andCONFIG_MULTICELL_LOCATION_POLTE_API_TOKEN
For Wi-Fi location services, use at least one of the following sets of options and configure the corresponding authentication parameters:
CONFIG_LOCATION_METHOD_WIFI_SERVICE_HERE
andCONFIG_LOCATION_METHOD_WIFI_SERVICE_HERE_API_KEY
CONFIG_LOCATION_METHOD_WIFI_SERVICE_SKYHOOK
andCONFIG_LOCATION_METHOD_WIFI_SERVICE_SKYHOOK_API_KEY
The following options are related to the Wi-Fi service and can usually have the default values:
Usage
To use the Location library, perform the following steps:
Initialize the library with the
location_init()
function.Create the configuration (
location_config
structure).Set the default values by passing the configuration to the
location_config_defaults_set()
function together with the list of method types.Set any required non-default values to the structures.
Call the
location_request()
function with the configuration.
Samples using the library
The following nRF Connect SDK samples use this library:
Limitations
The Location library can only have one application registered at a time. If there is already an application handler registered, another initialization will override the existing handler.
Cellular neighbor information used for cellular positioning is more accurate on modem firmware (MFW) 1.3.0 compared to earlier MFW releases that do not have an API for scanning the neighboring cells. For MFW releases older than 1.3.0, only serving cell information is provided and it can be hours or days old, or even older, depending on the modem sleep states.
Dependencies
This library uses the following nRF Connect SDK libraries:
It uses the following sdk-nrfxlib library:
It uses the following Zephyr libraries:
API documentation
include/modem/location.h
lib/location
- group location
Typedefs
-
typedef void (*location_event_handler_t)(const struct location_event_data *event_data)
Event handler prototype.
- Param event_data
[in] Event data.
Enums
-
enum location_method
Location method.
Values:
-
enumerator LOCATION_METHOD_CELLULAR
LTE cellular positioning.
-
enumerator LOCATION_METHOD_GNSS
Global Navigation Satellite System (GNSS).
-
enumerator LOCATION_METHOD_WIFI
Wi-Fi positioning.
-
enumerator LOCATION_METHOD_CELLULAR
-
enum location_event_id
Event IDs.
Values:
-
enumerator LOCATION_EVT_LOCATION
Location update.
-
enumerator LOCATION_EVT_TIMEOUT
Getting location timed out.
-
enumerator LOCATION_EVT_ERROR
An error occurred when trying to get the location.
-
enumerator LOCATION_EVT_GNSS_ASSISTANCE_REQUEST
GNSS is requesting A-GPS data. Application should obtain the data and send it to location_agps_data_process().
-
enumerator LOCATION_EVT_GNSS_PREDICTION_REQUEST
GNSS is requesting P-GPS data. Application should obtain the data and send it to location_pgps_data_process().
-
enumerator LOCATION_EVT_LOCATION
-
enum location_accuracy
Location accuracy.
Values:
-
enumerator LOCATION_ACCURACY_LOW
Allow lower accuracy to conserve power.
-
enumerator LOCATION_ACCURACY_NORMAL
Normal accuracy.
-
enumerator LOCATION_ACCURACY_HIGH
Try to improve accuracy. Increases power consumption.
-
enumerator LOCATION_ACCURACY_LOW
-
enum location_service
Location service.
Values:
-
enumerator LOCATION_SERVICE_ANY
Use any location service that has been configured to be available. This is useful when only one service is configured but can also be used even if several services are available.
-
enumerator LOCATION_SERVICE_NRF_CLOUD
nRF Cloud location service.
-
enumerator LOCATION_SERVICE_HERE
HERE location service.
-
enumerator LOCATION_SERVICE_SKYHOOK
Skyhook location service.
-
enumerator LOCATION_SERVICE_POLTE
Polte location service.
-
enumerator LOCATION_SERVICE_ANY
Functions
-
int location_init(location_event_handler_t event_handler)
Initializes the library.
Initializes the library and sets the event handler function. The first call to this function must be called before going to LTE normal mode. This can be called multiple times, which sets the event handler again.
- Parameters
event_handler – [in] Event handler function.
- Return values
-EINVAL – Given event_handler is NULL.
-EFAULT – Failed to obtain Wi-Fi interface.
- Returns
0 on success, or negative error code on failure.
-
int location_request(const struct location_config *config)
Requests the current position or starts periodic position updates.
Requests the current position using the given configuration. Depending on the configuration, a single position or periodic position updates are given. If the position request is successful, the results are delivered to the event handler function in LOCATION_EVT_LOCATION event. Otherwise LOCATION_EVT_TIMEOUT or LOCATION_EVT_ERROR event is triggered.
In periodic mode, position update is always attempted after the configured interval regardless of the outcome of any previous attempt. Periodic position updates can be stopped by calling location_cancel().
- Parameters
config – [in] Used configuration or NULL to get a single position update with the default configuration. Default configuration has the following location methods in priority order (if they are enabled in library configuration): GNSS, Wi-Fi and Cellular.
- Return values
-EPERM – Library not initialized.
-EBUSY – Previous location request still ongoing.
-EINVAL – Invalid configuration given.
- Returns
0 on success, or negative error code on failure.
-
int location_request_cancel(void)
Cancels periodic position updates.
- Return values
-EPERM – Library not initialized.
- Returns
0 on success, or negative error code on failure.
-
void location_config_defaults_set(struct location_config *config, uint8_t methods_count, enum location_method *method_types)
Sets the default values to a given configuration.
- Parameters
config – [inout] Configuration to be supplied with default values.
methods_count – [in] Number of location methods. This must not exceed CONFIG_LOCATION_METHODS_LIST_SIZE.
method_types – [in] List of location method types in priority order. Location methods with these types are initialized with default values into given configuration. List size must be as given in ‘methods_count’.
-
const char *location_method_str(enum location_method method)
Return location method as a string.
- Parameters
method – [in] Location method.
- Returns
Location method in string format. Returns “Unknown” if given method is not known.
-
int location_agps_data_process(const char *buf, size_t buf_len)
Feed in A-GPS data to be processed by library.
If Location library is not receiving A-GPS assistance data directly from nRF Cloud, it triggers the LOCATION_EVT_GNSS_ASSISTANCE_REQUEST event when assistance is needed. Once application has obtained the assistance data it can be handed over to Location library using this function.
Note that the data must be formatted similarly to the nRF Cloud A-GPS data, see for example nrf_cloud_agps_schema_v1.h.
- Parameters
buf – [in] Data received.
buf_len – [in] Buffer size of data to be processed.
- Return values
-EINVAL – Given buffer is NULL or buffer length zero.
- Returns
0 on success, or negative error code on failure.
-
int location_pgps_data_process(const char *buf, size_t buf_len)
Feed in P-GPS data to be processed by library.
If Location library is not receiving P-GPS assistance data directly from nRF Cloud, it triggers the LOCATION_EVT_GNSS_PREDICTION_REQUEST event when assistance is needed. Once application has obtained the assistance data it can be handed over to Location library using this function.
Note that the data must be formatted similarly to the nRF Cloud P-GPS data, see for example nrf_cloud_pgps_schema_v1.h.
- Parameters
buf – [in] Data received.
buf_len – [in] Buffer size of data to be processed.
- Return values
-EINVAL – Given buffer is NULL or buffer length zero.
- Returns
0 on success, or negative error code on failure.
-
struct location_datetime
- #include <location.h>
Date and time (UTC).
-
struct location_data
- #include <location.h>
Location data.
Public Members
-
enum location_method method
Used location method.
-
double latitude
Geodetic latitude (deg) in WGS-84.
-
double longitude
Geodetic longitude (deg) in WGS-84.
-
float accuracy
Location accuracy in meters (1-sigma).
-
struct location_datetime datetime
Date and time (UTC).
-
enum location_method method
-
struct location_event_data
- #include <location.h>
Location event data.
Public Members
-
enum location_event_id id
Event ID.
-
struct location_data location
Current location, used with event LOCATION_EVT_LOCATION.
-
enum location_event_id id
-
struct location_gnss_config
- #include <location.h>
GNSS configuration.
Public Members
-
uint16_t timeout
Timeout (in seconds), meaning how long GNSS is allowed to run when trying to acquire a fix. Zero means that the timer is disabled, meaning that GNSS will continue to search until it gets a fix or the application calls cancel.
Note that this is not real time as experienced by the user. Since GNSS cannot run while LTE is operating, the running time is counted from the instant when GNSS is allowed to start. Library waits until RRC connection is inactive before starting GNSS. If LTE power saving mode (PSM) is enabled and A-GPS is disabled, library waits until modem enters PSM before starting GNSS, thus maximizing uninterrupted operating window and minimizing power consumption.
-
enum location_accuracy accuracy
Desired accuracy level.
If accuracy is set to LOCATION_ACCURACY_LOW, GNSS relaxes the criteria for a qualifying fix and may produce fixes using only three satellites.
If accuracy is set to LOCATION_ACCURACY_HIGH, instead of using the first fix, GNSS is allowed to run for a longer time. This typically improves the location accuracy.
-
uint8_t num_consecutive_fixes
The number of fixes GNSS is allowed to produce before the library outputs the current location when accuracy is set to LOCATION_ACCURACY_HIGH.
If accuracy is set to LOCATION_ACCURACY_NORMAL or LOCATION_ACCURACY_LOW this parameter has no effect.
-
bool visibility_detection
Obstructed visibility detection. If set to true, the library tries to detect situations where getting a GNSS fix is unlikely or would consume a lot of energy. When such a situation is detected, GNSS is stopped without waiting for a fix or a timeout.
See Kconfig for related configuration options.
-
uint16_t timeout
-
struct location_cellular_config
- #include <location.h>
LTE cellular positioning configuration.
Public Members
-
uint16_t timeout
Timeout (in seconds) of how long the cellular positioning procedure can take. Zero means that the timer is disabled.
-
enum location_service service
Used cellular positioning service.
-
uint16_t timeout
-
struct location_wifi_config
- #include <location.h>
Wi-Fi positioning configuration.
Public Members
-
uint16_t timeout
Timeout (in seconds) of how long the Wi-Fi positioning procedure can take. Zero means that the timer is disabled.
-
enum location_service service
Used Wi-Fi positioning service.
-
uint16_t timeout
-
struct location_method_config
- #include <location.h>
Location method configuration.
Public Members
-
enum location_method method
Location method.
-
struct location_cellular_config cellular
Configuration for LOCATION_METHOD_CELLULAR.
-
struct location_gnss_config gnss
Configuration for LOCATION_METHOD_GNSS.
-
struct location_wifi_config wifi
Configuration for LOCATION_METHOD_WIFI.
-
enum location_method method
-
struct location_config
- #include <location.h>
Location request configuration.
Public Members
-
uint8_t methods_count
Number of location methods in ‘methods’.
-
struct location_method_config methods[CONFIG_LOCATION_METHODS_LIST_SIZE]
Selected location methods and associated configurations in priority order.
Index 0 has the highest priority. Number of used methods is indicated in ‘methods_count’ and it can be smaller than the size of this table.
-
uint16_t interval
Position update interval in seconds.
Set to 0 for a single position update. For periodic position updates the valid range is 10…65535 seconds.
-
uint8_t methods_count
-
typedef void (*location_event_handler_t)(const struct location_event_data *event_data)