Skip to content

Commit 996585c

Browse files
committed
src: modules: Use relative timestamps
Use relative timestamps captured at the time of measurement for environmental and location data. Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 6d0f822 commit 996585c

21 files changed

Lines changed: 135 additions & 270 deletions

File tree

app/src/modules/cloud/cloud.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,23 @@ static void handle_network_data_message(const struct network_msg *msg)
341341
{
342342
int err;
343343
bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
344+
int64_t timestamp_ms = NRF_CLOUD_NO_TIMESTAMP;
344345

345346
if (msg->type != NETWORK_QUALITY_SAMPLE_RESPONSE) {
346347
return;
347348
}
348349

350+
/* Convert uptime to unix time */
351+
timestamp_ms = msg->uptime;
352+
err = date_time_uptime_to_unix_time_ms(&timestamp_ms);
353+
if (err) {
354+
LOG_ERR("date_time_uptime_to_unix_time_ms, error: %d", err);
355+
}
356+
349357
err = nrf_cloud_coap_sensor_send(CUSTOM_JSON_APPID_VAL_CONEVAL,
350-
msg->conn_eval_params.energy_estimate,
351-
NRF_CLOUD_NO_TIMESTAMP,
352-
confirmable);
358+
msg->conn_eval_params.energy_estimate,
359+
timestamp_ms,
360+
confirmable);
353361
if (err) {
354362
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
355363
send_request_failed();
@@ -358,9 +366,9 @@ static void handle_network_data_message(const struct network_msg *msg)
358366
}
359367

360368
err = nrf_cloud_coap_sensor_send(NRF_CLOUD_JSON_APPID_VAL_RSRP,
361-
msg->conn_eval_params.rsrp,
362-
NRF_CLOUD_NO_TIMESTAMP,
363-
confirmable);
369+
msg->conn_eval_params.rsrp,
370+
timestamp_ms,
371+
confirmable);
364372
if (err) {
365373
LOG_ERR("nrf_cloud_coap_sensor_send, error: %d", err);
366374
send_request_failed();
@@ -375,27 +383,27 @@ static int send_storage_data_to_cloud(const struct storage_data_item *item)
375383
int64_t timestamp_ms = NRF_CLOUD_NO_TIMESTAMP;
376384
const bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
377385

378-
/* Get current timestamp */
379-
err = date_time_now(&timestamp_ms);
380-
if (err) {
381-
LOG_WRN("Failed to get current time, using no timestamp");
382-
timestamp_ms = NRF_CLOUD_NO_TIMESTAMP;
383-
}
384-
385386
#if defined(CONFIG_APP_POWER)
386387
if (item->type == STORAGE_TYPE_BATTERY) {
387-
double battery_percentage = item->data.BATTERY;
388+
const struct power_msg *power = &item->data.BATTERY;
389+
390+
/* Convert uptime to unix time */
391+
timestamp_ms = power->uptime;
392+
err = date_time_uptime_to_unix_time_ms(&timestamp_ms);
393+
if (err) {
394+
LOG_ERR("date_time_uptime_to_unix_time_ms, error: %d", err);
395+
}
388396

389397
err = nrf_cloud_coap_sensor_send(CUSTOM_JSON_APPID_VAL_BATTERY,
390-
battery_percentage,
398+
power->percentage,
391399
timestamp_ms,
392400
confirmable);
393401
if (err) {
394402
LOG_ERR("Failed to send battery data to cloud, error: %d", err);
395403
return err;
396404
}
397405

398-
LOG_DBG("Battery data sent to cloud: %.1f%%", battery_percentage);
406+
LOG_DBG("Battery data sent to cloud: %.1f%%", power->percentage);
399407

400408
/* Unused variable if no other sources compiled in */
401409
(void)confirmable;
@@ -408,6 +416,13 @@ static int send_storage_data_to_cloud(const struct storage_data_item *item)
408416
if (item->type == STORAGE_TYPE_ENVIRONMENTAL) {
409417
const struct environmental_msg *env = &item->data.ENVIRONMENTAL;
410418

419+
/* Convert uptime to unix time */
420+
timestamp_ms = env->uptime;
421+
err = date_time_uptime_to_unix_time_ms(&timestamp_ms);
422+
if (err) {
423+
LOG_ERR("date_time_uptime_to_unix_time_ms, error: %d", err);
424+
}
425+
411426
return cloud_environmental_send(env, timestamp_ms, confirmable);
412427
}
413428
#endif /* CONFIG_APP_ENVIRONMENTAL */
@@ -432,8 +447,9 @@ static int send_storage_data_to_cloud(const struct storage_data_item *item)
432447

433448
LOG_WRN("Unknown storage data type: %d", item->type);
434449

435-
/* Unused variable if no data sources are enabled */
450+
/* Unused variables if no data sources are enabled */
436451
(void)confirmable;
452+
(void)timestamp_ms;
437453

438454
return -ENOTSUP;
439455
}

app/src/modules/cloud/cloud_location.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,20 @@ static void handle_agnss_request(const struct nrf_modem_gnss_agnss_data_frame *r
255255

256256
#if defined(CONFIG_LOCATION_METHOD_GNSS)
257257
/* Handle GNSS location data from the location module */
258-
static void handle_gnss_location_data(const struct location_data *location_data)
258+
static void handle_gnss_location_data(const struct location_msg *location_msg)
259259
{
260260
int err;
261261
int64_t timestamp_ms = NRF_CLOUD_NO_TIMESTAMP;
262262
bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES);
263+
const struct location_data *location_data = &location_msg->gnss_data;
264+
265+
/* Convert uptime to unix time */
266+
timestamp_ms = location_msg->uptime;
267+
err = date_time_uptime_to_unix_time_ms(&timestamp_ms);
268+
if (err) {
269+
LOG_ERR("date_time_uptime_to_unix_time_ms, error: %d", err);
270+
}
271+
263272
struct nrf_cloud_gnss_data gnss_data = {
264273
.type = NRF_CLOUD_GNSS_TYPE_PVT,
265274
.ts_ms = timestamp_ms,
@@ -275,16 +284,6 @@ static void handle_gnss_location_data(const struct location_data *location_data)
275284
(double)location_data->longitude,
276285
(double)location_data->accuracy);
277286

278-
/* Get current timestamp */
279-
err = date_time_now(&timestamp_ms);
280-
if (err) {
281-
LOG_WRN("Failed to get current time");
282-
283-
timestamp_ms = NRF_CLOUD_NO_TIMESTAMP;
284-
}
285-
286-
gnss_data.ts_ms = timestamp_ms;
287-
288287
#if defined(CONFIG_LOCATION_DATA_DETAILS)
289288
#define CLOUD_GNSS_HEADING_ACC_LIMIT (float)60.0
290289

@@ -333,7 +332,7 @@ void cloud_location_handle_message(const struct location_msg *msg)
333332
#if defined(CONFIG_LOCATION_METHOD_GNSS)
334333
case LOCATION_GNSS_DATA:
335334
LOG_DBG("GNSS location data received");
336-
handle_gnss_location_data(&msg->gnss_data);
335+
handle_gnss_location_data(msg);
337336
break;
338337
#endif /* CONFIG_LOCATION_METHOD_GNSS */
339338

app/src/modules/environmental/Kconfig.environmental

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ config APP_ENVIRONMENTAL_WATCHDOG_TIMEOUT_SECONDS
2828
A small difference between the two can mean more frequent watchdog feeds, which increases
2929
power consumption.
3030

31-
config APP_ENVIRONMENTAL_TIMESTAMP
32-
bool "Include timestamp in environmental message"
33-
depends on DATE_TIME
34-
default y
35-
help
36-
Include a timestamp in the environmental message of ENVIRONMENTAL_SENSOR_SAMPLE_RESPONSE
37-
type.
38-
The timestamp is the current time in milliseconds since epoch.
39-
4031
config APP_ENVIRONMENTAL_MSG_PROCESSING_TIMEOUT_SECONDS
4132
int "Maximum message processing time"
4233
default 3

app/src/modules/environmental/environmental.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include <zephyr/task_wdt/task_wdt.h>
1212
#include <zephyr/smf.h>
1313

14-
#if defined(CONFIG_APP_ENVIRONMENTAL_TIMESTAMP)
15-
#include <date_time.h>
16-
#endif
17-
1814
#include "app_common.h"
1915
#include "environmental.h"
2016

@@ -122,15 +118,9 @@ static void sample_sensors(const struct device *const bme680)
122118
.temperature = sensor_value_to_double(&temp),
123119
.pressure = sensor_value_to_double(&press),
124120
.humidity = sensor_value_to_double(&humidity),
121+
.uptime = k_uptime_get(),
125122
};
126123

127-
#if defined(CONFIG_APP_ENVIRONMENTAL_TIMESTAMP)
128-
err = date_time_now(&msg.timestamp);
129-
if (err) {
130-
LOG_ERR("date_time_now() failed, error: %d, using 0", err);
131-
}
132-
#endif /* CONFIG_APP_ENVIRONMENTAL_TIMESTAMP */
133-
134124
/* Log the environmental values and limit to 2 decimals */
135125
LOG_DBG("Temperature: %.2f C, Pressure: %.2f Pa, Humidity: %.2f %%",
136126
msg.temperature, msg.pressure, msg.humidity);

app/src/modules/environmental/environmental.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ struct environmental_msg {
4848
/** Contains the current pressure in Pa. */
4949
double pressure;
5050

51-
/** Timestamp of the sample in milliseconds since epoch. */
52-
int64_t timestamp;
51+
/** Uptime when the sample was taken in milliseconds.
52+
* Use date_time_uptime_to_unix_time_ms() to convert to unix time before sending to cloud.
53+
* Only valid for ENVIRONMENTAL_SENSOR_SAMPLE_RESPONSE events.
54+
*/
55+
int64_t uptime;
5356
};
5457

5558
#define MSG_TO_ENVIRONMENTAL_MSG(_msg) (*(const struct environmental_msg *)_msg)

app/src/modules/location/location.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ static void gnss_location_send(const struct location_data *location_data)
228228
int err;
229229
struct location_msg location_msg = {
230230
.type = LOCATION_GNSS_DATA,
231-
.gnss_data = *location_data
231+
.gnss_data = *location_data,
232+
.uptime = k_uptime_get()
232233
};
233234

234235
err = zbus_chan_pub(&LOCATION_CHAN, &location_msg, K_SECONDS(1));

app/src/modules/location/location.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ struct location_msg {
183183
*/
184184
struct location_data gnss_data;
185185
};
186+
187+
/** Uptime when the location was obtained in milliseconds.
188+
* Use date_time_uptime_to_unix_time_ms() to convert to unix time before sending to cloud.
189+
* Only valid for LOCATION_GNSS_DATA events.
190+
*/
191+
int64_t uptime;
186192
};
187193

188194
#define MSG_TO_LOCATION_TYPE(_msg) (((const struct location_msg *)_msg)->type)

app/src/modules/network/network.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ static void sample_network_quality(void)
258258
int ret;
259259
struct network_msg msg = {
260260
.type = NETWORK_QUALITY_SAMPLE_RESPONSE,
261+
.uptime = k_uptime_get()
261262
};
262263

263264
ret = lte_lc_conn_eval_params_get(&msg.conn_eval_params);

app/src/modules/network/network.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ struct network_msg {
141141
IF_ENABLED(CONFIG_LTE_LC_CONN_EVAL_MODULE,
142142
(struct lte_lc_conn_eval_params conn_eval_params));
143143
};
144+
145+
/** Uptime when the sample was taken in milliseconds.
146+
* Use date_time_uptime_to_unix_time_ms() to convert to unix time before sending to cloud.
147+
* Only valid for NETWORK_QUALITY_SAMPLE_RESPONSE events.
148+
*/
149+
int64_t uptime;
144150
};
145151

146152
#define MSG_TO_NETWORK_MSG(_msg) (*(const struct network_msg *)_msg)

app/src/modules/power/Kconfig.power

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ config APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS
5252
Maximum time allowed for processing a single message in the module's state machine.
5353
The value must be smaller than CONFIG_APP_POWER_WATCHDOG_TIMEOUT_SECONDS.
5454

55-
config APP_POWER_TIMESTAMP
56-
bool "Include timestamp in power message"
57-
depends on DATE_TIME
58-
default y
59-
help
60-
Include a timestamp in the power message of POWER_BATTERY_PERCENTAGE_SAMPLE_RESPONSE
61-
type.
62-
The timestamp is the current time in milliseconds since epoch.
63-
6455
module = APP_POWER
6556
module-str = Power
6657
source "subsys/logging/Kconfig.template.log_config"

0 commit comments

Comments
 (0)