Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions generated/include/infuse/tdf/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,18 @@ struct tdf_network_scan_count {
uint32_t frame[_count]; \
} __packed;

/** Battery voltage */
struct tdf_battery_voltage {
/** Battery voltage (milliVolts) */
uint16_t voltage;
} __packed;

/** Battery state of charge */
struct tdf_battery_soc {
/** State of charge (percent) */
uint8_t soc;
} __packed;

/** Infuse-IoT builtin TDF definitions */
enum tdf_builtin_id {
/** Common announcement packet */
Expand Down Expand Up @@ -785,6 +797,10 @@ enum tdf_builtin_id {
TDF_NETWORK_SCAN_COUNT = 51,
/** Generic exception stack frame */
TDF_EXCEPTION_STACK_FRAME = 52,
/** Battery voltage */
TDF_BATTERY_VOLTAGE = 53,
/** Battery state of charge */
TDF_BATTERY_SOC = 54,
/** End of builtin TDF range */
TDF_BUILTIN_END = 1024,
};
Expand Down Expand Up @@ -838,6 +854,8 @@ enum tdf_builtin_id {
#define _TDF_WIFI_CONNECTION_FAILED_TYPE struct tdf_wifi_connection_failed
#define _TDF_WIFI_DISCONNECTED_TYPE struct tdf_wifi_disconnected
#define _TDF_NETWORK_SCAN_COUNT_TYPE struct tdf_network_scan_count
#define _TDF_BATTERY_VOLTAGE_TYPE struct tdf_battery_voltage
#define _TDF_BATTERY_SOC_TYPE struct tdf_battery_soc

/** Size of builtin TDF definitions */
enum tdf_builtin_size {
Expand Down Expand Up @@ -888,6 +906,8 @@ enum tdf_builtin_size {
_TDF_WIFI_CONNECTION_FAILED_SIZE = sizeof(struct tdf_wifi_connection_failed),
_TDF_WIFI_DISCONNECTED_SIZE = sizeof(struct tdf_wifi_disconnected),
_TDF_NETWORK_SCAN_COUNT_SIZE = sizeof(struct tdf_network_scan_count),
_TDF_BATTERY_VOLTAGE_SIZE = sizeof(struct tdf_battery_voltage),
_TDF_BATTERY_SOC_SIZE = sizeof(struct tdf_battery_soc),
};

/** @endcond */
Expand Down
4 changes: 4 additions & 0 deletions include/infuse/task_runner/tasks/battery_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ extern "C" {
enum {
/* Battery voltage, charge current, charge percentage */
TASK_BATTERY_LOG_COMPLETE = BIT(0),
/* Battery voltage */
TASK_BATTERY_LOG_VOLTAGE = BIT(1),
/* Battery charge percentage */
TASK_BATTERY_LOG_SOC = BIT(2),
};

/** @brief Battery task arguments */
Expand Down
14 changes: 14 additions & 0 deletions scripts/west_commands/cloud_definitions/tdf.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,20 @@
"fields": [
{"name": "frame", "type": "uint32_t", "num":0, "description": "Stack frame value", "display": {"fmt": "hex", "digits": 8}}
]
},
"53": {
"name": "BATTERY_VOLTAGE",
"description": "Battery voltage",
"fields": [
{"name": "voltage", "type": "uint16_t", "description": "Battery voltage (milliVolts)", "display": {"postfix": "mV"}}
]
},
"54": {
"name": "BATTERY_SOC",
"description": "Battery state of charge",
"fields": [
{"name": "soc", "type": "uint8_t", "description": "State of charge (percent)", "display": {"postfix": "%"}}
]
}
}
}
15 changes: 15 additions & 0 deletions subsys/task_runner/tasks/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ config TASK_TDF_LOGGER_ENVIRONMENTAL_TIMEOUT_SEC
int "Environmental sample age must be shorter than this to log"
default 3600

choice TASK_TDF_LOGGER_BATTERY_TYPE
prompt "TDF type for battery logging"
default TASK_TDF_LOGGER_BATTERY_TYPE_COMPLETE

config TASK_TDF_LOGGER_BATTERY_TYPE_COMPLETE
bool "Log TDF_BATTERY_STATE (Voltage, Current, SoC)"

config TASK_TDF_LOGGER_BATTERY_TYPE_VOLTAGE
bool "Log TDF_BATTERY_VOLTAGE (Voltage only)"

config TASK_TDF_LOGGER_BATTERY_TYPE_SOC
bool "Log TDF_BATTERY_SOC (SoC only)"

endchoice

endif # TASK_RUNNER_TASK_TDF_LOGGER

config TASK_RUNNER_TASK_IMU
Expand Down
17 changes: 14 additions & 3 deletions subsys/task_runner/tasks/task_battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ void battery_task_fn(struct k_work *work)
const struct task_battery_args *args = &sch->task_args.infuse.battery;
const struct device *fuel_gauge = task->executor.workqueue.task_arg.const_arg;
struct tdf_battery_state tdf_battery = {0};
struct tdf_battery_voltage tdf_voltage;
struct tdf_battery_soc tdf_soc;
uint64_t epoch_time;
int rc;

if (task_runner_task_block(&task->terminate_signal, K_NO_WAIT) == 1) {
Expand All @@ -100,9 +103,17 @@ void battery_task_fn(struct k_work *work)

rc = task_battery_manual_run(fuel_gauge, &sch->task_args.infuse.battery, &tdf_battery);
if (rc == 0) {
/* Log output TDF */
TASK_SCHEDULE_TDF_LOG(sch, TASK_BATTERY_LOG_COMPLETE, TDF_BATTERY_STATE,
epoch_time_now(), &tdf_battery);
tdf_voltage.voltage = tdf_battery.voltage_mv;
tdf_soc.soc = tdf_battery.soc;
epoch_time = epoch_time_now();

/* Log output TDFs */
TASK_SCHEDULE_TDF_LOG(sch, TASK_BATTERY_LOG_COMPLETE, TDF_BATTERY_STATE, epoch_time,
&tdf_battery);
TASK_SCHEDULE_TDF_LOG(sch, TASK_BATTERY_LOG_VOLTAGE, TDF_BATTERY_VOLTAGE,
epoch_time, &tdf_voltage);
TASK_SCHEDULE_TDF_LOG(sch, TASK_BATTERY_LOG_SOC, TDF_BATTERY_SOC, epoch_time,
&tdf_soc);
}

if (args->repeat_interval_ms) {
Expand Down
12 changes: 12 additions & 0 deletions subsys/task_runner/tasks/task_tdf_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,19 @@ static void log_battery(uint8_t loggers, uint64_t timestamp)
/* Get latest value */
zbus_chan_read(C_GET(INFUSE_ZBUS_CHAN_BATTERY), &battery, K_FOREVER);
/* Add to specified loggers */
#if defined(CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_COMPLETE)
TDF_DATA_LOGGER_LOG(loggers, TDF_BATTERY_STATE, timestamp, &battery);
#elif defined(CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_VOLTAGE)
struct tdf_battery_voltage tdf = {.voltage = battery.voltage_mv};

TDF_DATA_LOGGER_LOG(loggers, TDF_BATTERY_VOLTAGE, timestamp, &tdf);
#elif defined(CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_SOC)
struct tdf_battery_soc tdf = {.soc = battery.soc};

TDF_DATA_LOGGER_LOG(loggers, TDF_BATTERY_SOC, timestamp, &tdf);
#else
#error Unknown battery logging type
#endif
#endif
}

Expand Down
75 changes: 56 additions & 19 deletions tests/subsys/task_runner/tasks/battery/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,64 @@ static void task_schedule(struct task_data *data)
k_work_reschedule(&data->executor.workqueue.work, K_NO_WAIT);
}

static void expect_logging(uint32_t battery_uv, int32_t current_ua, uint8_t soc)
static void expect_logging(uint8_t log_mask, uint32_t battery_uv, int32_t current_ua, uint8_t soc)
{
struct k_fifo *tx_queue = epacket_dummmy_transmit_fifo_get();
struct tdf_battery_state *state;
struct tdf_parsed tdf;
struct net_buf *pkt;
int rc;

tdf_data_logger_flush(TDF_DATA_LOGGER_SERIAL);
pkt = k_fifo_get(tx_queue, K_MSEC(10));
if (log_mask == 0) {
zassert_is_null(pkt);
return;
}

zassert_not_null(pkt);

net_buf_pull(pkt, sizeof(struct epacket_dummy_frame));
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_STATE, &tdf));
state = tdf.data;

zassert_equal(battery_uv / 1000, state->voltage_mv);
zassert_equal(current_ua, state->current_ua);
zassert_equal(soc, state->soc);
if (log_mask & TASK_BATTERY_LOG_COMPLETE) {
struct tdf_battery_state *state;

rc = tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_STATE, &tdf);
zassert_equal(0, rc);
state = tdf.data;

zassert_equal(battery_uv / 1000, state->voltage_mv);
zassert_equal(current_ua, state->current_ua);
zassert_equal(soc, state->soc);
}
if (log_mask & TASK_BATTERY_LOG_VOLTAGE) {
struct tdf_battery_voltage *state;

rc = tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_VOLTAGE, &tdf);
zassert_equal(0, rc);
state = tdf.data;

zassert_equal(battery_uv / 1000, state->voltage);
}
if (log_mask & TASK_BATTERY_LOG_SOC) {
struct tdf_battery_soc *state;

rc = tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_SOC, &tdf);
zassert_equal(0, rc);
state = tdf.data;

zassert_equal(soc, state->soc);
}

net_buf_unref(pkt);
}

static void test_battery(uint32_t battery_uv, int32_t current_ua, bool log)
static void test_battery(uint32_t battery_uv, int32_t current_ua, uint8_t log_mask)
{
INFUSE_ZBUS_TYPE(INFUSE_ZBUS_CHAN_BATTERY) battery_reading;
uint32_t pub_count;

if (log) {
schedule.task_logging[0].tdf_mask = TASK_BATTERY_LOG_COMPLETE;
schedule.task_logging[0].loggers = TDF_DATA_LOGGER_SERIAL;
}
schedule.task_logging[0].tdf_mask = log_mask;
schedule.task_logging[0].loggers = TDF_DATA_LOGGER_SERIAL;

/* Configure emulator */
emul_fuel_gauge_set_battery_charging(EMUL_DEV, battery_uv, current_ua);
Expand All @@ -109,9 +136,7 @@ static void test_battery(uint32_t battery_uv, int32_t current_ua, bool log)
zassert_equal(current_ua, battery_reading.current_ua);
zassert_equal(1, battery_reading.soc);

if (log) {
expect_logging(battery_uv, current_ua, 1);
}
expect_logging(log_mask, battery_uv, current_ua, 1);
}

ZTEST(task_bat, test_no_log)
Expand All @@ -124,12 +149,15 @@ ZTEST(task_bat, test_no_log)
/* Setup links between task config and data */
task_runner_init(&schedule, &state, 1, &config, &data, 1);

test_battery(3700000, 10000, false);
test_battery(3501000, -15000, false);
test_battery(3700000, 10000, 0);
test_battery(3501000, -15000, 0);
}

ZTEST(task_bat, test_log)
{
uint8_t log_all =
TASK_BATTERY_LOG_COMPLETE | TASK_BATTERY_LOG_VOLTAGE | TASK_BATTERY_LOG_SOC;

schedule = (struct task_schedule){
.task_id = TASK_ID_BATTERY,
.validity = TASK_VALID_ALWAYS,
Expand All @@ -138,8 +166,14 @@ ZTEST(task_bat, test_log)
/* Setup links between task config and data */
task_runner_init(&schedule, &state, 1, &config, &data, 1);

test_battery(3700000, 10000, true);
test_battery(3501000, -15000, true);
test_battery(3700000, 10000, TASK_BATTERY_LOG_COMPLETE);
test_battery(3501000, -15000, TASK_BATTERY_LOG_COMPLETE);

test_battery(4200000, 18000, TASK_BATTERY_LOG_VOLTAGE);
test_battery(4201000, -7000, TASK_BATTERY_LOG_VOLTAGE);

test_battery(3600000, 15000, log_all);
test_battery(3601000, -10000, log_all);
}

ZTEST(task_bat, test_periodic)
Expand All @@ -161,6 +195,9 @@ ZTEST(task_bat, test_periodic)
},
};

/* Configure emulator */
emul_fuel_gauge_set_battery_charging(EMUL_DEV, 3700000, 1000);

/* Setup links between task config and data */
task_runner_init(&schedule, &state, 1, &config, &data, 1);

Expand Down
25 changes: 19 additions & 6 deletions tests/subsys/task_runner/tasks/tdf_logger/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ ZBUS_CHAN_DEFINE_WITH_ID(INFUSE_ZBUS_NAME(INFUSE_ZBUS_CHAN_IMU), INFUSE_ZBUS_CHA
struct imu_sample_container, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
ZBUS_MSG_INIT(0));

#if defined(CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_COMPLETE)
#define TDF_BATTERY_TYPE TDF_BATTERY_STATE
#define TDF_BATTERY_SIZE sizeof(TDF_TYPE(TDF_BATTERY_STATE))
#elif defined(CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_VOLTAGE)
#define TDF_BATTERY_TYPE TDF_BATTERY_VOLTAGE
#define TDF_BATTERY_SIZE sizeof(TDF_TYPE(TDF_BATTERY_VOLTAGE))
#elif defined(CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_SOC)
#define TDF_BATTERY_TYPE TDF_BATTERY_SOC
#define TDF_BATTERY_SIZE sizeof(TDF_TYPE(TDF_BATTERY_SOC))
#else
#error Unknown battery logging type
#endif

static void task_schedule(struct task_data *data)
{
data->schedule_idx = 0;
Expand Down Expand Up @@ -94,7 +107,7 @@ ZTEST(task_tdf_logger, test_log_before_data)
net_buf_pull(pkt, sizeof(struct epacket_dummy_frame));
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_ANNOUNCE, &tdf));
zassert_equal(0, tdf.time);
zassert_equal(-ENOMEM, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_STATE, &tdf));
zassert_equal(-ENOMEM, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_TYPE, &tdf));
zassert_equal(-ENOMEM,
tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_AMBIENT_TEMP_PRES_HUM, &tdf));
net_buf_unref(pkt);
Expand Down Expand Up @@ -126,7 +139,7 @@ ZTEST(task_tdf_logger, test_no_flush)
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_ANNOUNCE, &tdf));
zassert_not_equal(0, tdf.time);
zassert_equal(sizeof(struct tdf_announce), tdf.tdf_len);
zassert_equal(-ENOMEM, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_STATE, &tdf));
zassert_equal(-ENOMEM, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_TYPE, &tdf));
zassert_equal(-ENOMEM,
tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_AMBIENT_TEMP_PRES_HUM, &tdf));
net_buf_unref(pkt);
Expand Down Expand Up @@ -238,9 +251,9 @@ ZTEST(task_tdf_logger, test_battery)
pkt = k_fifo_get(tx_queue, K_MSEC(100));
zassert_not_null(pkt);
net_buf_pull(pkt, sizeof(struct epacket_dummy_frame));
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_STATE, &tdf));
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_TYPE, &tdf));
zassert_equal(0, tdf.time);
zassert_equal(sizeof(struct tdf_battery_state), tdf.tdf_len);
zassert_equal(TDF_BATTERY_SIZE, tdf.tdf_len);
net_buf_unref(pkt);
}

Expand Down Expand Up @@ -574,7 +587,7 @@ ZTEST(task_tdf_logger, test_multi)
pkt = k_fifo_get(tx_queue, K_MSEC(100));
zassert_not_null(pkt);
net_buf_pull(pkt, sizeof(struct epacket_dummy_frame));
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_STATE, &tdf));
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_TYPE, &tdf));
zassert_equal(0,
tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_AMBIENT_TEMP_PRES_HUM, &tdf));
zassert_equal(0, tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_GCS_WGS84_LLHA, &tdf));
Expand Down Expand Up @@ -609,7 +622,7 @@ ZTEST(task_tdf_logger, test_multi_iteration)
zassert_not_null(pkt);
net_buf_pull(pkt, sizeof(struct epacket_dummy_frame));
zassert_equal(iter == 3 ? -ENOMEM : 0,
tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_STATE, &tdf));
tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_BATTERY_TYPE, &tdf));
zassert_equal(iter == 2 ? -ENOMEM : 0,
tdf_parse_find_in_buf(pkt->data, pkt->len, TDF_AMBIENT_TEMP_PRES_HUM,
&tdf));
Expand Down
16 changes: 11 additions & 5 deletions tests/subsys/task_runner/tasks/tdf_logger/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ common:
tags: data_logger tdf
min_flash: 64
min_ram: 32
required_snippets:
- infuse
integration_platforms:
- mps2/an385
tests:
task_runner.tasks.tdf_logger:
required_snippets:
- infuse
integration_platforms:
- mps2/an385
task_runner.tasks.tdf_logger.complete: {}
task_runner.tasks.tdf_logger.voltage:
extra_configs:
- CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_VOLTAGE=y
task_runner.tasks.tdf_logger.soc:
extra_configs:
- CONFIG_TASK_TDF_LOGGER_BATTERY_TYPE_SOC=y