2. Usage Guide
2.1 Getting Started
2.1.1 Compilation Configuration
To enable the location library in your project, set the following CMake variables:
set(SID_SDK_CONFIG_ENABLE_LOCATION 1)
set(SID_SDK_CONFIG_ENABLE_WIFI 1)
set(SID_SDK_CONFIG_ENABLE_GNSS 1)
add_compile_definitions(SID_SDK_CONFIG_ENABLE_LOCATION=1)
add_compile_definitions(SID_SDK_CONFIG_ENABLE_WIFI=1)
add_compile_definitions(SID_SDK_CONFIG_ENABLE_GNSS=1)
Set SID_SDK_CONFIG_ENABLE_LOCATION to build the Location Library. It is required for every level, including L1 and L5. Set SID_SDK_CONFIG_ENABLE_WIFI and/or SID_SDK_CONFIG_ENABLE_GNSS only if the hardware can scan. L5 requires SID_SDK_CONFIG_ENABLE_LINK_TYPE_3.
Additional configurations may be required depending on the build system of the target.
2.1.2 Library Overview
The Amazon Sidewalk Location Library provides APIs to configure the library and request locations. The application initializes the library once (2.1.2.1), then requests locations at runtime (2.1.2.3).
2.1.2.1 Initialization
The location library must be initialized using sid_location_init() after sid_init().
sid_error_t sid_location_init(struct sid_handle *handle, struct sid_location_config *config);
- handle: A pointer to the handle returned by
sid_init(). - config: Location configuration structure containing your location methods, levels, and callback function.
The function returns SID_ERROR_NONE in case of success.
Key configuration fields for the location config:
sid_location_type_mask: The location methods the device uses:SID_LOCATION_METHOD_GATEWAY(L1),SID_LOCATION_METHOD_WIFI(L3),SID_LOCATION_METHOD_GNSS(L4),SID_LOCATION_METHOD_LORA_MLAT(L5), orSID_LOCATION_METHOD_ALL. The Wi-Fi and GNSS PALs are initialized only when their bit is set.manage_effort: Whether the library chooses the level for each request. See 2.1.2.4.max_effort: Highest level the library uses in managed effort.callbacks.on_update: Your callback function to receive location results, including scan data for Wi-Fi and GNSS.callbacks.context: Optional user-defined context pointer passed to the callback.stepdowns: Time (ms) to wait after a successful scan before stepping back down:l4_to_l3after a GNSS scan,l3_to_l2after a Wi-Fi scan (steps down to L1).0steps down immediately.l2_to_l1is not used.fragmentation: Timeout (timeout_ms) and retries (max_retries) for fragmented Wi-Fi and GNSS uplinks over LoRa. Atimeout_msof 0 uses the default. See 2.2.3.auto_link_pause_resume: Whentrue, pauses the Sub-GHz links during Wi-Fi and GNSS scans to avoid hardware conflicts, and resumes them when the scan completes.
2.1.2.2 Setting Up the Location Callback
The on_update callback provides an asynchronous notification of location operation results. This callback must be implemented to handle the outcomes of location scanning and uplink operations.
Parameters:
result: Pointer tostruct sid_location_resultcontaining operation results.context: User-defined context pointer (optional, can beNULL).
The sid_location_result structure
The result structure contains the following fields:
status: Operation status (enum sid_location_status):SID_LOCATION_LVL1_READY: BLE gateway location (L1) is available on the current gateway connection after init.SID_LOCATION_LVL1_UNAVAILABLE: BLE gateway location (L1) is not available on the current gateway connection after init.SID_LOCATION_SEND_ONLY_DONE: Send-only operation completed.SID_LOCATION_SCAN_ONLY_DONE: Scan-only operation completed.SID_LOCATION_SCAN_AND_SEND_DONE: Scan-and-send operation completed.SID_LOCATION_RESOLVE_MSG_SENT,SID_LOCATION_RESOLVE_MSG_ACK_RECEIVED,SID_LOCATION_RESOLVE_MSG_TIMEOUT: Delivery status of asid_put_msg()location request. See 2.7.3.
err: Error code indicating success or failure (sid_error_t).mode: The level used for this operation (enum sid_location_effort_mode). LoRa requests reportSID_LOCATION_EFFORT_L5.link: The link type used for the uplink (enum sid_link_type).result_type: Which member ofparsedis valid (SID_LOCATION_RESULT_WIFI,SID_LOCATION_RESULT_GNSS, orSID_LOCATION_RESULT_NONEfor L1 and L5).parsed: The scan data (parsed.wifiorparsed.gnss), at mostSID_LOCATION_MAX_PAYLOAD_SIZE(106) bytes and valid only during the callback. Decode it withsid_location_parse_wifi()orsid_location_parse_gnss().
Configuring the callback
// Define your callback function
void location_callback(const struct sid_location_result *const result, void *context)
{
// Handle location operation results
// Check result->status and result->err, and read result->parsed based on result->result_type
}
static struct sid_location_config cfg = {
.sid_location_type_mask = SID_LOCATION_METHOD_ALL,
.manage_effort = true,
.max_effort = SID_LOCATION_EFFORT_L4,
.callbacks = {
.on_update = location_callback,
.context = NULL, // Optional context pointer
},
// ... other configuration fields
};
2.1.2.3 Runtime
Location operations can be executed using sid_location_run() after the Sidewalk loop has been started (sid_start()). This API initiates location scanning and/or uplink operations.
sid_error_t sid_location_run(struct sid_handle *handle,
struct sid_location_run_config *config,
uint32_t delay_s);
Parameters:
handle: A pointer to the handle returned bysid_init().config: Struct containing the configuration for the location request.delay_s: Delay in seconds before starting a scan (L3, L4).
Configuration fields in struct sid_location_run_config:
mode: Level for this operation (enum sid_location_effort_mode).type: Operation type (enum sid_location_run_type):SID_LOCATION_SEND_ONLY: Send a Wi-Fi or GNSS payload supplied inpayload, without scanning.SID_LOCATION_SCAN_ONLY: Scan without sending (L3, L4).SID_LOCATION_SCAN_AND_SEND: Scan if the level requires it, then send. Use this type for L1 and L5.
payload_type/payload: A Wi-Fi or GNSS payload to send. Used only withSID_LOCATION_SEND_ONLY.levels[]/num_levels: Optional list of levels to try in order (up toSID_LOCATION_MAX_LEVELS). The library moves to the next entry when a level is not supported or finds nothing. Whennum_levelsis 0,modeis used.
The function returns SID_ERROR_NONE in case of success.
2.1.2.4 Managed Effort Mode
The manage_effort configuration field controls whether the location library automatically selects and adjusts the level used for location operations.
When manage_effort is true:
- The application can pass
SID_LOCATION_EFFORT_DEFAULTas the mode insid_location_run(). The library will use its internally tracked level. - The library starts at
SID_LOCATION_EFFORT_L1and moves to L3, then L4, when a level is not supported or finds nothing. - After a successful scan at L3 or L4, the library starts a stepdown timer. When the timer expires, the level steps back toward L1 based on the configured
stepdownstimeouts. - The maximum level the library can escalate to is bounded by
max_effort. - The application can override the maximum allowed level at runtime using
sid_location_set_max_mode(), for example to limit power consumption during low-battery conditions.
When manage_effort is false:
- The application must specify an explicit level (L1, L3, L4, or L5) in every call to
sid_location_run(). PassingSID_LOCATION_EFFORT_DEFAULTreturns an error. - No automatic escalation or stepdown occurs. The library uses exactly the level requested.
- This mode gives the application full control over which location method is used for each operation.
Effort Level Mapping:
SID_LOCATION_EFFORT_L1: BLE gateway location (requires a BLE connection to a Sidewalk Gateway that allows location sharing).SID_LOCATION_EFFORT_L3: Wi-Fi scan (requires the Wi-Fi PAL).SID_LOCATION_EFFORT_L4: GNSS scan (requires the GNSS PAL).SID_LOCATION_EFFORT_L5: LoRa multilateration (requires Link Type 3 network sync).
Managed effort uses L1, L3 and L4. To use L5, set mode to SID_LOCATION_EFFORT_L5 or include it in levels[]. sid_location_init() rejects a max_effort above the highest level the build supports, so max_effort cannot be L5. max_effort does not limit explicit requests.
2.1.2.5 De-Initialization
The location library must be deinitialized using sid_location_deinit() before calling sid_deinit(). The function takes the following parameter:
handle: A pointer to the handle returned bysid_init().
The function returns SID_ERROR_NONE in case of success.
For detailed API documentation including function signatures, parameters, return values, and other available APIs, see the Amazon Sidewalk SID API Developer Guide.
2.2 Wi-Fi and GNSS Location Example (LR1110)
The following example shows how to enable GNSS and Wi-Fi with the location library for use with the LR1110 shield.
2.2.1 Example Platform Configuration
// file: app_location_lr11xx_config.h
#include <lr11xx_gnss_wifi_config.h>
const lr11xx_gnss_wifi_config_t *get_location_cfg(void);
The GNSS configuration supports two key parameters:
Constellation Type:
SMTC_MODEM_GNSS_CONSTELLATION_GPS— Use the GPS-only constellation.SMTC_MODEM_GNSS_CONSTELLATION_BEIDOU— Use the BeiDou-only constellation.SMTC_MODEM_GNSS_CONSTELLATION_GPS_BEIDOU— Use both the GPS and BeiDou constellations.
Scan Mode:
SMTC_MODEM_GNSS_MODE_STATIC— Scanning mode for non-moving objects.SMTC_MODEM_GNSS_MODE_MOBILE— Scanning mode for moving objects.
// file: app_location_lr11xx_config.c
static lr11xx_gnss_wifi_config_t gnss_wifi_config = {
.constellation_type = SMTC_MODEM_GNSS_CONSTELLATION_GPS_BEIDOU,
.scan_mode = SMTC_MODEM_GNSS_MODE_MOBILE
};
const lr11xx_gnss_wifi_config_t *get_location_cfg(void)
{
return &gnss_wifi_config;
}
// Platform parameters setup (inside app initialization)
platform_parameters_t platform_parameters = {
.mfg_store_region = sid_mfg_config_get(),
.platform_init_parameters.radio_cfg =
(radio_lr11xx_device_config_t *)get_radio_cfg(),
.platform_init_parameters.gnss_wifi_cfg =
(lr11xx_gnss_wifi_config_t *)get_location_cfg(),
};
2.2.2 Location Library Usage
// Create a location config with user supplied callback
void location_callback(const struct sid_location_result *const result, void *context)
{
// Read Wi-Fi or GNSS scan data from result->parsed based on result->result_type
}
static struct sid_location_config cfg = {
.sid_location_type_mask = SID_LOCATION_METHOD_WIFI | SID_LOCATION_METHOD_GNSS,
.manage_effort = true,
.max_effort = SID_LOCATION_EFFORT_L4,
.callbacks = {
.on_update = location_callback,
.context = NULL,
},
.stepdowns = { // Time (ms) to wait after a scan before stepping down in managed effort mode
.l4_to_l3 = 0,
.l3_to_l2 = 0,
},
.fragmentation = { // Fragmentation configurations to use for location payload uplinks
.timeout_ms = 0, // 0 indicates use default timeout value
.max_retries = 1,
},
.auto_link_pause_resume = true, // Pause Sub-GHz links during Wi-Fi and GNSS scans
};
// Initialize after sid_init()
sid_error_t res = sid_location_init(*app_context->sidewalk_handle, &cfg);
// Wi-Fi scan and send uplink
struct sid_location_run_config config = {
.type = SID_LOCATION_SCAN_AND_SEND,
.mode = SID_LOCATION_EFFORT_L3
};
res = sid_location_run(*app_context->sidewalk_handle, &config, 0);
// GNSS scan and send uplink
config.mode = SID_LOCATION_EFFORT_L4;
res = sid_location_run(*app_context->sidewalk_handle, &config, 0);
2.2.3 Fragmentation Timeout Configuration
fragmentation.timeout_ms sets how long the library keeps retrying a fragmented Wi-Fi or GNSS uplink over LoRa. If it is 0, the library calculates it as:
timeout_ms = ceil(total_size / MTU) × link_type_duration_ms × 2
Where:
ceil(total_size / MTU)is the number of fragments.link_type_duration_msis the maximum time to uplink one fragment: 5000 ms on LoRa.- The factor of 2 leaves margin for retries.
BLE uplinks are not fragmented, so these settings do not apply to BLE.
For example, a message that needs 4 fragments over LoRa gets 4 × 5000 × 2 = 40000 ms. Set timeout_ms to at least the value from the formula. A shorter value can stop retries the cloud would still accept.
2.3 BLE-only Location Example
The following example shows how to use the location library with BLE gateway location (L1) only.
// Create a location config with user supplied callback
void location_callback(const struct sid_location_result *const result, void *context)
{
// User can get the results of the uplink here
// Handle the location result based on status
}
static struct sid_location_config cfg = {
.sid_location_type_mask = SID_LOCATION_METHOD_GATEWAY,
.manage_effort = true,
.max_effort = SID_LOCATION_EFFORT_L1,
.callbacks = {
.on_update = location_callback,
.context = NULL,
},
.auto_link_pause_resume = false, // Not used in BLE-only mode
};
// Initialize after sid_init()
sid_error_t res = sid_location_init(*app_context->sidewalk_handle, &cfg);
// Send location request after sid_start()
// (requires a BLE connection to a Sidewalk Gateway that allows location sharing)
struct sid_location_run_config config = {
.type = SID_LOCATION_SCAN_AND_SEND,
.mode = SID_LOCATION_EFFORT_DEFAULT
};
res = sid_location_run(*app_context->sidewalk_handle, &config, 0);
2.4 LoRa Multilateration Example
The following example requests a LoRa multilateration (L5) location. This requires SDK v2.0 or later and SID_SDK_CONFIG_ENABLE_LINK_TYPE_3.
void location_callback(const struct sid_location_result *const result, void *context)
{
// result->mode is SID_LOCATION_EFFORT_L5 and result->link is SID_LINK_TYPE_3.
// result->err reports whether the request was sent. No position is returned to the device.
}
static struct sid_location_config cfg = {
.sid_location_type_mask = SID_LOCATION_METHOD_LORA_MLAT,
.manage_effort = false,
.max_effort = SID_LOCATION_EFFORT_L1, // Does not limit explicit requests such as L5
.callbacks = {
.on_update = location_callback,
.context = NULL,
},
};
// Initialize after sid_init()
sid_error_t res = sid_location_init(*app_context->sidewalk_handle, &cfg);
// Request a location after sid_start(), once SID_STATUS_TIME_SYNCED is reported
// and SID_LINK_TYPE_3 is in link_status_mask
struct sid_location_run_config config = {
.type = SID_LOCATION_SCAN_AND_SEND,
.mode = SID_LOCATION_EFFORT_L5,
};
res = sid_location_run(*app_context->sidewalk_handle, &config, 0);
The callback reports SID_LOCATION_SCAN_AND_SEND_DONE. sid_location_run() returns SID_ERROR_NO_ROUTE_AVAILABLE if the LoRa link is not synced, and SID_ERROR_NOSUPPORT if the firmware is built without Link Type 3.
2.5 Platform Abstraction Layers
If the device supports Wi-Fi and GNSS scanning (L3 and L4), the Wi-Fi and GNSS Platform Abstraction Layers (PALs) must be implemented and linked. Reference implementations are provided that use the Semtech LoRa Basics Modem middleware APIs.
The reference implementation in the Amazon Sidewalk SDK combines the Wi-Fi and GNSS PALs, because both use the LoRa Basics Modem and share resources.
2.6 Best Practices
- Use
sid_location_set_max_mode()to limit the levels used in managed effort, for example on low battery. - Configure stepdown timeouts based on your application requirements.
- Allow the library to manage effort levels automatically when possible.
- Consider using scan-only operations when immediate uplink is not required.
- Request L5 only after
SID_STATUS_TIME_SYNCEDis reported and the LoRa link is up.
2.7 Requesting Location with sid_put_msg()
An application can also request a location with a regular uplink by setting resolve_location in the message descriptor’s TX attributes.
When resolve_location is set to SID_TX_LOCATION_LINK_RESOLVE, Amazon Sidewalk also resolves the device’s location. Over BLE it works like L1, and over LoRa like L5. The message payload is delivered to the uplink destination as usual, and the position to the location destination.
2.7.1 Prerequisites
SID_SDK_CONFIG_ENABLE_LOCATIONmust be enabled at compile time.sid_location_init()must have been called successfully.- The Sidewalk stack must be in
SID_STATE_READYstate.
2.7.2 Usage
// Location callback handles both explicit location and resolve_location events
void location_callback(const struct sid_location_result *const result, void *context)
{
switch (result->status) {
case SID_LOCATION_RESOLVE_MSG_SENT:
// Message with location request sent
break;
case SID_LOCATION_RESOLVE_MSG_ACK_RECEIVED:
// Acknowledgement received from the cloud (only if request_ack was set)
break;
case SID_LOCATION_RESOLVE_MSG_TIMEOUT:
// Message send failed or timed out
break;
default:
// Handle other location statuses (L1 ready, scan done, etc.)
break;
}
}
// Initialize location library with callback
static struct sid_location_config loc_cfg = {
.sid_location_type_mask = SID_LOCATION_METHOD_GATEWAY | SID_LOCATION_METHOD_LORA_MLAT,
.manage_effort = true,
.max_effort = SID_LOCATION_EFFORT_L1,
.callbacks = {
.on_update = location_callback,
},
};
sid_location_init(handle, &loc_cfg);
// Send a message with a location request
struct sid_msg msg = {
.data = payload_data,
.size = payload_size,
};
struct sid_msg_desc desc = {
.type = SID_MSG_TYPE_NOTIFY,
.link_type = SID_LINK_TYPE_ANY,
.msg_desc_attr = {
.tx_attr = {
.resolve_location = SID_TX_LOCATION_LINK_RESOLVE,
},
},
};
sid_error_t ret = sid_put_msg(handle, &msg, &desc);
With an acknowledgement for confirmed delivery:
struct sid_msg_desc desc = {
.type = SID_MSG_TYPE_NOTIFY,
.link_type = SID_LINK_TYPE_ANY,
.msg_desc_attr = {
.tx_attr = {
.resolve_location = SID_TX_LOCATION_LINK_RESOLVE,
.request_ack = true,
.num_retries = 3,
.ttl_in_seconds = 60,
},
},
};
sid_error_t ret = sid_put_msg(handle, &msg, &desc);
// on_update reports SID_LOCATION_RESOLVE_MSG_ACK_RECEIVED on success
// or SID_LOCATION_RESOLVE_MSG_TIMEOUT on failure
2.7.3 Delivery Status Notifications
The on_update callback registered with sid_location_init() reports the delivery status of the message:
SID_LOCATION_RESOLVE_MSG_SENT: The message was sent.SID_LOCATION_RESOLVE_MSG_ACK_RECEIVED: The cloud acknowledged the message. Reported only whenrequest_ackistrue.SID_LOCATION_RESOLVE_MSG_TIMEOUT: The message could not be sent, or no acknowledgement arrived withinttl_in_seconds.
result->mode is SID_LOCATION_EFFORT_L1 for BLE and SID_LOCATION_EFFORT_L5 for LoRa. result->link is the link that carried the message. The callback does not report a position.
2.7.4 Error Handling
sid_put_msg() returns the following errors specific to resolve_location:
SID_ERROR_INVALID_STATE: The Location Library is not initialized (sid_location_init()not called).SID_ERROR_NOSUPPORT:SID_SDK_CONFIG_ENABLE_LOCATIONis not enabled.