Skip to content

Commit 4ddf060

Browse files
committed
feat(memfault): add location metrics support
- Add location metrics - Remove asset tracker app location metrics Signed-off-by: Gillian Minnehan <[email protected]>
1 parent 6b6426a commit 4ddf060

File tree

13 files changed

+313
-160
lines changed

13 files changed

+313
-160
lines changed
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
/* Application-specific metrics.
22
* Please refer to https://docs.memfault.com/docs/embedded/metrics-api for more details.
33
*/
4-
5-
MEMFAULT_METRICS_KEY_DEFINE(gnss_time_to_fix_ms, kMemfaultMetricType_Unsigned)
6-
MEMFAULT_METRICS_KEY_DEFINE(gnss_satellites_tracked_count, kMemfaultMetricType_Unsigned)
7-
MEMFAULT_METRICS_KEY_DEFINE(location_timeout_search_time_ms, kMemfaultMetricType_Unsigned)

applications/asset_tracker_v2/doc/debug_module.rst

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ This section documents the various features implemented by the module.
2222
Memfault
2323
========
2424

25-
The debug module uses `Memfault SDK`_ to track |NCS| specific metrics such as LTE and stack metrics.
26-
In addition, the following types of custom Memfault metrics are defined and tracked when compiling in the debug module:
27-
28-
* ``gnss_time_to_fix_ms`` - Time duration between the start of a GNSS search and obtaining a fix.
29-
* ``gnss_satellites_tracked_count`` - Number of satellites tracked during a GNSS search window.
30-
* ``location_timeout_search_time_ms`` - Time duration between the start of a location search and a search timeout.
31-
25+
The debug module uses `Memfault SDK`_ to track |NCS| specific metrics such as LTE, location, Bluetooth and stack metrics.
3226
The debug module also implements `Memfault SDK`_ software watchdog, which is designed to trigger an assert before an actual watchdog timeout.
3327
This enables the application to be able to collect coredump data before a reboot occurs.
3428

applications/asset_tracker_v2/overlay-memfault.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CONFIG_MEMFAULT_NCS_PROJECT_KEY=""
99
CONFIG_MEMFAULT_NCS_DEVICE_ID_IMEI=y
1010
CONFIG_MEMFAULT_NCS_LTE_METRICS=y
1111
CONFIG_MEMFAULT_NCS_STACK_METRICS=y
12+
CONFIG_MEMFAULT_NCS_LOCATION_METRICS=y
1213
CONFIG_MEMFAULT_LOGGING_ENABLE=y
1314

1415
CONFIG_MEMFAULT_ROOT_CERT_STORAGE_NRF9160_MODEM=y

applications/asset_tracker_v2/src/modules/debug_module.c

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "events/data_module_event.h"
2626
#include "events/sensor_module_event.h"
2727
#include "events/util_module_event.h"
28-
#include "events/location_module_event.h"
2928
#include "events/modem_module_event.h"
3029
#include "events/ui_module_event.h"
3130
#include "events/debug_module_event.h"
@@ -41,7 +40,6 @@ struct debug_msg_data {
4140
struct sensor_module_event sensor;
4241
struct data_module_event data;
4342
struct app_module_event app;
44-
struct location_module_event location;
4543
struct modem_module_event modem;
4644
} module;
4745
};
@@ -143,15 +141,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
143141
message_handler(&debug_msg);
144142
}
145143

146-
if (is_location_module_event(aeh)) {
147-
struct location_module_event *event = cast_location_module_event(aeh);
148-
struct debug_msg_data debug_msg = {
149-
.module.location = *event
150-
};
151-
152-
message_handler(&debug_msg);
153-
}
154-
155144
if (is_sensor_module_event(aeh)) {
156145
struct sensor_module_event *event =
157146
cast_sensor_module_event(aeh);
@@ -257,38 +246,6 @@ static void send_memfault_data(void)
257246
}
258247
}
259248

260-
static void add_location_metrics(uint8_t satellites, uint32_t search_time,
261-
enum location_module_event_type event)
262-
{
263-
int err;
264-
265-
switch (event) {
266-
case LOCATION_MODULE_EVT_GNSS_DATA_READY:
267-
err = MEMFAULT_METRIC_SET_UNSIGNED(gnss_time_to_fix_ms, search_time);
268-
if (err) {
269-
LOG_ERR("Failed updating gnss_time_to_fix_ms metric, error: %d", err);
270-
}
271-
break;
272-
case LOCATION_MODULE_EVT_TIMEOUT:
273-
err = MEMFAULT_METRIC_SET_UNSIGNED(location_timeout_search_time_ms, search_time);
274-
if (err) {
275-
LOG_ERR("Failed updating location_timeout_search_time_ms metric, error: %d",
276-
err);
277-
}
278-
break;
279-
default:
280-
LOG_ERR("Unknown location module event.");
281-
return;
282-
}
283-
284-
err = MEMFAULT_METRIC_SET_UNSIGNED(gnss_satellites_tracked_count, satellites);
285-
if (err) {
286-
LOG_ERR("Failed updating gnss_satellites_tracked_count metric, error: %d", err);
287-
}
288-
289-
memfault_metrics_heartbeat_debug_trigger();
290-
}
291-
292249
static void memfault_handle_event(struct debug_msg_data *msg)
293250
{
294251
if (IS_EVENT(msg, app, APP_EVT_START)) {
@@ -349,14 +306,6 @@ static void memfault_handle_event(struct debug_msg_data *msg)
349306
send_memfault_data();
350307
return;
351308
}
352-
353-
if ((IS_EVENT(msg, location, LOCATION_MODULE_EVT_TIMEOUT)) ||
354-
(IS_EVENT(msg, location, LOCATION_MODULE_EVT_GNSS_DATA_READY))) {
355-
add_location_metrics(msg->module.location.data.location.satellites_tracked,
356-
msg->module.location.data.location.search_time,
357-
msg->module.location.type);
358-
return;
359-
}
360309
}
361310
#endif /* defined(CONFIG_MEMFAULT) */
362311

@@ -388,7 +337,6 @@ APP_EVENT_LISTENER(MODULE, app_event_handler);
388337
APP_EVENT_SUBSCRIBE_EARLY(MODULE, app_module_event);
389338
APP_EVENT_SUBSCRIBE_EARLY(MODULE, modem_module_event);
390339
APP_EVENT_SUBSCRIBE_EARLY(MODULE, cloud_module_event);
391-
APP_EVENT_SUBSCRIBE_EARLY(MODULE, location_module_event);
392340
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ui_module_event);
393341
APP_EVENT_SUBSCRIBE_EARLY(MODULE, sensor_module_event);
394342
APP_EVENT_SUBSCRIBE_EARLY(MODULE, data_module_event);

applications/asset_tracker_v2/tests/debug_module/src/debug_module_test.c

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "cmock_watchdog_app.h"
1919

2020
#include "app_module_event.h"
21-
#include "location_module_event.h"
2221
#include "debug_module_event.h"
2322
#include "data_module_event.h"
2423

@@ -29,7 +28,6 @@ extern struct event_listener __event_listener_debug_module;
2928
*/
3029
static struct app_module_event app_module_event_memory;
3130
static struct data_module_event data_module_event_memory;
32-
static struct location_module_event location_module_event_memory;
3331
static struct debug_module_event debug_module_event_memory;
3432

3533
#define DEBUG_MODULE_EVT_HANDLER(aeh) __event_listener_debug_module.notification(aeh)
@@ -38,7 +36,6 @@ static struct debug_module_event debug_module_event_memory;
3836
* depend on these to exist. But since we are unit testing, we dont need
3937
* these subscriptions and hence these structs can remain uninitialized.
4038
*/
41-
struct event_type __event_type_location_module_event;
4239
struct event_type __event_type_debug_module_event;
4340
struct event_type __event_type_app_module_event;
4441
struct event_type __event_type_data_module_event;
@@ -108,62 +105,6 @@ void setup_debug_module_in_init_state(void)
108105
app_event_manager_free(app_module_event);
109106
}
110107

111-
/* Test whether the correct Memfault metrics are set upon a GNSS fix. */
112-
void test_memfault_trigger_metric_sampling_on_gnss_fix(void)
113-
{
114-
setup_debug_module_in_init_state();
115-
116-
__cmock_memfault_metrics_heartbeat_set_unsigned_ExpectAndReturn(
117-
MEMFAULT_METRICS_KEY(gnss_time_to_fix_ms), 60000, 0);
118-
__cmock_memfault_metrics_heartbeat_set_unsigned_ExpectAndReturn(
119-
MEMFAULT_METRICS_KEY(gnss_satellites_tracked_count),
120-
4,
121-
0);
122-
__cmock_memfault_metrics_heartbeat_debug_trigger_Expect();
123-
124-
__cmock_app_event_manager_alloc_ExpectAnyArgsAndReturn(&location_module_event_memory);
125-
__cmock_app_event_manager_free_ExpectAnyArgs();
126-
struct location_module_event *location_module_event = new_location_module_event();
127-
128-
location_module_event->type = LOCATION_MODULE_EVT_GNSS_DATA_READY;
129-
location_module_event->data.location.satellites_tracked = 4;
130-
location_module_event->data.location.search_time = 60000;
131-
132-
TEST_ASSERT_EQUAL(0, DEBUG_MODULE_EVT_HANDLER(
133-
(struct app_event_header *)location_module_event));
134-
app_event_manager_free(location_module_event);
135-
}
136-
137-
/* Test whether the correct Memfault metrics are set upon a GNSS timeout. */
138-
void test_memfault_trigger_metric_sampling_on_location_timeout(void)
139-
{
140-
resetTest();
141-
setup_debug_module_in_init_state();
142-
143-
/* Update this function to expect the search time and number of satellites. */
144-
__cmock_memfault_metrics_heartbeat_set_unsigned_ExpectAndReturn(
145-
MEMFAULT_METRICS_KEY(location_timeout_search_time_ms),
146-
30000,
147-
0);
148-
__cmock_memfault_metrics_heartbeat_set_unsigned_ExpectAndReturn(
149-
MEMFAULT_METRICS_KEY(gnss_satellites_tracked_count),
150-
2,
151-
0);
152-
__cmock_memfault_metrics_heartbeat_debug_trigger_Ignore();
153-
154-
__cmock_app_event_manager_alloc_ExpectAnyArgsAndReturn(&location_module_event_memory);
155-
__cmock_app_event_manager_free_ExpectAnyArgs();
156-
struct location_module_event *location_module_event = new_location_module_event();
157-
158-
location_module_event->type = LOCATION_MODULE_EVT_TIMEOUT;
159-
location_module_event->data.location.satellites_tracked = 2;
160-
location_module_event->data.location.search_time = 30000;
161-
162-
TEST_ASSERT_EQUAL(0, DEBUG_MODULE_EVT_HANDLER(
163-
(struct app_event_header *)location_module_event));
164-
app_event_manager_free(location_module_event);
165-
}
166-
167108
/* Test that the debug module is able to submit Memfault data externally through events
168109
* of type DEBUG_EVT_MEMFAULT_DATA_READY carrying chunks of data.
169110
*/
@@ -198,42 +139,6 @@ void test_memfault_trigger_data_send(void)
198139
k_sleep(K_SECONDS(1));
199140
}
200141

201-
/* Test that no Memfault SDK specific APIs are called on GNSS module events
202-
* that should not be handled.
203-
*/
204-
void test_memfault_unhandled_event(void)
205-
{
206-
resetTest();
207-
setup_debug_module_in_init_state();
208-
209-
/* Expect no memfault APIs to be called on LOCATION_MODULE_EVT_ACTIVE */
210-
211-
__cmock_app_event_manager_alloc_ExpectAnyArgsAndReturn(&location_module_event_memory);
212-
__cmock_app_event_manager_free_ExpectAnyArgs();
213-
struct location_module_event *location_module_event = new_location_module_event();
214-
215-
location_module_event->type = LOCATION_MODULE_EVT_ACTIVE;
216-
TEST_ASSERT_EQUAL(0, DEBUG_MODULE_EVT_HANDLER(
217-
(struct app_event_header *)location_module_event));
218-
219-
location_module_event->type = LOCATION_MODULE_EVT_INACTIVE;
220-
TEST_ASSERT_EQUAL(0, DEBUG_MODULE_EVT_HANDLER(
221-
(struct app_event_header *)location_module_event));
222-
223-
location_module_event->type = LOCATION_MODULE_EVT_SHUTDOWN_READY;
224-
TEST_ASSERT_EQUAL(0, DEBUG_MODULE_EVT_HANDLER(
225-
(struct app_event_header *)location_module_event));
226-
227-
location_module_event->type = LOCATION_MODULE_EVT_AGNSS_NEEDED;
228-
TEST_ASSERT_EQUAL(0, DEBUG_MODULE_EVT_HANDLER(
229-
(struct app_event_header *)location_module_event));
230-
231-
location_module_event->type = LOCATION_MODULE_EVT_ERROR_CODE;
232-
TEST_ASSERT_EQUAL(0, DEBUG_MODULE_EVT_HANDLER(
233-
(struct app_event_header *)location_module_event));
234-
app_event_manager_free(location_module_event);
235-
}
236-
237142
/* Test whether the correct Memfault software watchdog APIs are called on callbacks from the
238143
* application watchdog library.
239144
*/

doc/nrf/libraries/debug/memfault_ncs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ The Kconfig options for Memfault that are defined in |NCS| provide some addition
9797
* :kconfig:option:`CONFIG_MEMFAULT_NCS_PROVISION_CERTIFICATES`
9898
* :kconfig:option:`CONFIG_MEMFAULT_NCS_INTERNAL_FLASH_BACKED_COREDUMP`
9999
* :kconfig:option:`CONFIG_MEMFAULT_NCS_LTE_METRICS`
100+
* :kconfig:option:`CONFIG_MEMFAULT_NCS_LOCATION_METRICS`
100101
* :kconfig:option:`CONFIG_MEMFAULT_NCS_STACK_METRICS`
101102
* :kconfig:option:`CONFIG_MEMFAULT_NCS_BT_METRICS`
102103

modules/memfault-firmware-sdk/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ zephyr_library_sources_ifdef(
2222
memfault_lte_metrics.c
2323
)
2424

25+
zephyr_library_sources_ifdef(
26+
CONFIG_MEMFAULT_NCS_LOCATION_METRICS
27+
memfault_location_metrics.c
28+
)
29+
2530
zephyr_library_sources_ifdef(
2631
CONFIG_MEMFAULT_NCS_BT_METRICS
2732
memfault_bt_metrics.c)

modules/memfault-firmware-sdk/Kconfig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,18 @@ config MEMFAULT_NCS_BT_METRICS
168168
help
169169
Collect metrics related to the Bluetooth stack in background while application is running.
170170

171+
config MEMFAULT_NCS_LOCATION_METRICS
172+
bool "Collect location metrics"
173+
depends on LOCATION
174+
depends on LOCATION_DATA_DETAILS
175+
default y
176+
help
177+
Collect metrics related to location fixes while the application is running.
178+
171179
config MEMFAULT_NCS_USE_DEFAULT_METRICS
172180
bool
173181
select MEMFAULT_METRICS_EXTRA_DEFS_FILE
174-
default y if (MEMFAULT_NCS_STACK_METRICS || MEMFAULT_NCS_LTE_METRICS || MEMFAULT_NCS_BT_METRICS)
182+
default y if (MEMFAULT_NCS_STACK_METRICS || MEMFAULT_NCS_LTE_METRICS || MEMFAULT_NCS_BT_METRICS || MEMFAULT_NCS_LOCATION_METRICS)
175183

176184
config MEMFAULT_NCS_IMPLEMENT_METRICS_COLLECTION
177185
bool "Implement metrics collection"

modules/memfault-firmware-sdk/config/memfault_metrics_heartbeat_extra.def

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,62 @@ MEMFAULT_METRICS_KEY_DEFINE(ncs_bt_connection_time_ms, kMemfaultMetricType_Timer
3838
MEMFAULT_METRICS_KEY_DEFINE(ncs_bt_connection_count, kMemfaultMetricType_Unsigned)
3939
MEMFAULT_METRICS_KEY_DEFINE(ncs_bt_bond_count, kMemfaultMetricType_Unsigned)
4040
#endif /* CONFIG_MEMFAULT_NCS_BT_METRICS */
41+
42+
#ifdef CONFIG_MEMFAULT_NCS_LOCATION_METRICS
43+
44+
/*
45+
* Heartbeat metrics
46+
*/
47+
MEMFAULT_METRICS_KEY_DEFINE(ncs_loc_search_request_count, kMemfaultMetricType_Unsigned)
48+
49+
/*
50+
* Session metrics
51+
*/
52+
MEMFAULT_METRICS_SESSION_KEY_DEFINE(ncs_loc)
53+
54+
/* Successful search metrics */
55+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_search_success, kMemfaultMetricType_Unsigned, ncs_loc)
56+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_search_time_ms, kMemfaultMetricType_Unsigned, ncs_loc)
57+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_accuracy_cm, kMemfaultMetricType_Unsigned, ncs_loc)
58+
59+
/* Failed search metrics */
60+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_search_failure, kMemfaultMetricType_Unsigned, ncs_loc)
61+
62+
/*
63+
* Method-specific metrics (GNSS, Cellular, Wi-Fi)
64+
* - All reported in both successful and failed searches unless noted
65+
* - *_method_result metric values map to the location_event_id enumeration in
66+
* nrf/include/modem/location.h
67+
*/
68+
69+
/* GNSS */
70+
#if defined(CONFIG_LOCATION_METHOD_GNSS)
71+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_gnss_method_time_ms, kMemfaultMetricType_Unsigned, ncs_loc)
72+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_gnss_method_result, kMemfaultMetricType_Unsigned, ncs_loc)
73+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_gnss_satellites_tracked_count, kMemfaultMetricType_Unsigned, ncs_loc)
74+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_gnss_satellites_used_count, kMemfaultMetricType_Unsigned, ncs_loc)
75+
/* Maps to elapsed_time_gnss in nrf/include/modem/location.h */
76+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_gnss_on_time_ms, kMemfaultMetricType_Unsigned, ncs_loc)
77+
/* Maps to execution_time in nrfxlib/nrf_modem/include/nrf_modem_gnss.h */
78+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_gnss_exec_time_ms, kMemfaultMetricType_Unsigned, ncs_loc)
79+
80+
/* Only reported in successful session */
81+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_gnss_time_to_fix_ms, kMemfaultMetricType_Unsigned, ncs_loc)
82+
#endif /* CONFIG_LOCATION_METHOD_GNSS */
83+
84+
/* Cellular */
85+
#if defined(CONFIG_LOCATION_METHOD_CELLULAR)
86+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_lte_method_time_ms, kMemfaultMetricType_Unsigned, ncs_loc)
87+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_lte_method_result, kMemfaultMetricType_Unsigned, ncs_loc)
88+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_lte_neighbor_cells_count, kMemfaultMetricType_Unsigned, ncs_loc)
89+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_lte_gci_cells_count, kMemfaultMetricType_Unsigned, ncs_loc)
90+
#endif /* CONFIG_LOCATION_METHOD_CELLULAR */
91+
92+
/* Wi-Fi */
93+
#if defined(CONFIG_LOCATION_METHOD_WIFI)
94+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_wifi_method_time_ms, kMemfaultMetricType_Unsigned, ncs_loc)
95+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_wifi_method_result, kMemfaultMetricType_Unsigned, ncs_loc)
96+
MEMFAULT_METRICS_KEY_DEFINE_WITH_SESSION(ncs_loc_wifi_ap_count, kMemfaultMetricType_Unsigned, ncs_loc)
97+
#endif /* CONFIG_LOCATION_METHOD_WIFI */
98+
99+
#endif /* CONFIG_MEMFAULT_NCS_LOCATION_METRICS */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef MEMFAULT_LOCATION_METRICS_H_
8+
#define MEMFAULT_LOCATION_METRICS_H_
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
/** @brief Initialize default location metrics. */
15+
void memfault_location_metrics_init(void);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif
20+
21+
#endif /* MEMFAULT_LOCATION_METRICS_H_ */

0 commit comments

Comments
 (0)