Skip to content

Commit b15cd28

Browse files
jhn-nordiccarlescufi
authored andcommitted
applications: asset_tracker: Use application workqueue in modules
Use application workqueue in env_sensors, light_sensor and motion module. Jira:NCSDK-4422 Signed-off-by: Jon Helge Nistad <[email protected]>
1 parent 4f5b8d6 commit b15cd28

File tree

8 files changed

+60
-16
lines changed

8 files changed

+60
-16
lines changed

applications/asset_tracker/src/env_sensors/bsec.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ static u32_t data_send_interval_s = CONFIG_ENVIRONMENT_DATA_SEND_INTERVAL;
8080
static bool backoff_enabled;
8181
static bool initialized;
8282

83+
static struct k_work_q *env_sensors_work_q;
84+
8385
static int settings_set(const char *key, size_t len_rd,
8486
settings_read_cb read_cb, void *cb_arg)
8587
{
@@ -242,8 +244,9 @@ int env_sensors_get_air_quality(env_sensor_data_t *sensor_data)
242244

243245
static inline int submit_poll_work(const u32_t delay_s)
244246
{
245-
return k_delayed_work_submit(&env_sensors_poller,
246-
K_SECONDS((u32_t)delay_s));
247+
return k_delayed_work_submit_to_queue(env_sensors_work_q,
248+
&env_sensors_poller,
249+
K_SECONDS((u32_t)delay_s));
247250
}
248251

249252
int env_sensors_poll(void)
@@ -266,11 +269,16 @@ static void env_sensors_poll_fn(struct k_work *work)
266269
CONFIG_ENVIRONMENT_DATA_BACKOFF_TIME : data_send_interval_s);
267270
}
268271

269-
int env_sensors_init_and_start(const env_sensors_data_ready_cb cb)
272+
int env_sensors_init_and_start(struct k_work_q *work_q,
273+
const env_sensors_data_ready_cb cb)
270274
{
271275
return_values_init bsec_ret;
272276
int ret;
273277

278+
if ((work_q == NULL) || (cb == NULL)) {
279+
return -EINVAL;
280+
}
281+
274282
i2c_master = device_get_binding("I2C_2");
275283
if (!i2c_master) {
276284
LOG_ERR("cannot bind to BME680");
@@ -298,6 +306,8 @@ int env_sensors_init_and_start(const env_sensors_data_ready_cb cb)
298306

299307
data_ready_cb = cb;
300308

309+
env_sensors_work_q = work_q;
310+
301311
k_delayed_work_init(&env_sensors_poller, env_sensors_poll_fn);
302312

303313
initialized = true;

applications/asset_tracker/src/env_sensors/env_sensors.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ LOG_MODULE_REGISTER(env_sensors, CONFIG_ASSET_TRACKER_LOG_LEVEL);
1717
#define ENV_INIT_DELAY_S (5) /* Polling delay upon initialization */
1818
#define MAX_INTERVAL_S (INT_MAX/MSEC_PER_SEC)
1919

20+
static struct k_work_q *env_sensors_work_q;
21+
2022
struct env_sensor {
2123
env_sensor_data_t sensor;
2224
enum sensor_channel channel;
@@ -67,8 +69,9 @@ static bool initialized;
6769

6870
static inline int submit_poll_work(const u32_t delay_s)
6971
{
70-
return k_delayed_work_submit(&env_sensors_poller,
71-
K_SECONDS((u32_t)delay_s));
72+
return k_delayed_work_submit_to_queue(env_sensors_work_q,
73+
&env_sensors_poller,
74+
K_SECONDS((u32_t)delay_s));
7275
}
7376

7477
int env_sensors_poll(void)
@@ -126,15 +129,22 @@ static void env_sensors_poll_fn(struct k_work *work)
126129
}
127130

128131
/**@brief Initialize environment sensors. */
129-
int env_sensors_init_and_start(const env_sensors_data_ready_cb cb)
132+
int env_sensors_init_and_start(struct k_work_q *work_q,
133+
const env_sensors_data_ready_cb cb)
130134
{
135+
if ((work_q == NULL) || (cb == NULL)) {
136+
return -EINVAL;
137+
}
138+
131139
for (int i = 0; i < ARRAY_SIZE(env_sensors); i++) {
132140
env_sensors[i]->dev =
133141
device_get_binding(env_sensors[i]->dev_name);
134142
__ASSERT(env_sensors[i]->dev, "Could not get device %s\n",
135143
env_sensors[i]->dev_name);
136144
}
137145

146+
env_sensors_work_q = work_q;
147+
138148
data_ready_cb = cb;
139149

140150
k_delayed_work_init(&env_sensors_poller, env_sensors_poll_fn);

applications/asset_tracker/src/env_sensors/env_sensors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ int env_sensors_get_air_quality(env_sensor_data_t *sensor_data);
8989
*
9090
* @return 0 if the operation was successful, otherwise a (negative) error code.
9191
*/
92-
int env_sensors_init_and_start(const env_sensors_data_ready_cb cb);
92+
int env_sensors_init_and_start(struct k_work_q *work_q,
93+
const env_sensors_data_ready_cb cb);
9394

9495
/**
9596
* @brief Set environmental sensor's poll/send interval.

applications/asset_tracker/src/light_sensor/light_sensor.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,36 @@ static struct ls_ch_data *ls_data[LS_CH__END] = { [LS_CH_RED] = &ls_ch_red,
4343
[LS_CH_IR] = &ls_ch_ir };
4444
static light_sensor_data_ready_cb ls_cb;
4545
static struct k_delayed_work ls_poller;
46+
static struct k_work_q *ls_work_q;
4647
static u32_t data_send_interval_s = CONFIG_LIGHT_SENSOR_DATA_SEND_INTERVAL;
4748
static bool initialized;
4849

4950
static void light_sensor_poll_fn(struct k_work *work);
5051

5152
static inline int submit_poll_work(const u32_t delay_s)
5253
{
53-
return k_delayed_work_submit(&ls_poller, K_SECONDS((u32_t)delay_s));
54+
return k_delayed_work_submit_to_queue(ls_work_q, &ls_poller,
55+
K_SECONDS((u32_t)delay_s));
5456
}
5557

56-
int light_sensor_init_and_start(const light_sensor_data_ready_cb cb)
58+
int light_sensor_init_and_start(struct k_work_q *work_q,
59+
const light_sensor_data_ready_cb cb)
5760
{
5861
if (!IS_ENABLED(CONFIG_LIGHT_SENSOR)) {
5962
return -ENXIO;
6063
}
6164

65+
if ((work_q == NULL) || (cb == NULL)) {
66+
return -EINVAL;
67+
}
68+
6269
ls_dev = device_get_binding(ls_dev_name);
6370

6471
if (ls_dev == NULL) {
6572
return -ENODEV;
6673
}
6774

75+
ls_work_q = work_q;
6876
ls_cb = cb;
6977

7078
k_delayed_work_init(&ls_poller, light_sensor_poll_fn);

applications/asset_tracker/src/light_sensor/light_sensor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ typedef void (*light_sensor_data_ready_cb)(void);
3838
*
3939
* @return 0 if the operation was successful, otherwise a (negative) error code.
4040
*/
41-
int light_sensor_init_and_start(const light_sensor_data_ready_cb cb);
41+
int light_sensor_init_and_start(struct k_work_q *work_q,
42+
const light_sensor_data_ready_cb cb);
4243

4344
/**
4445
* @brief Get latest sampled light data.

applications/asset_tracker/src/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,17 +1029,18 @@ static void sensors_init(void)
10291029
{
10301030
int err;
10311031

1032-
err = motion_init_and_start(motion_handler);
1032+
err = motion_init_and_start(&application_work_q, motion_handler);
10331033
if (err) {
10341034
LOG_ERR("motion module init failed, error: %d", err);
10351035
}
10361036

1037-
err = env_sensors_init_and_start(env_data_send);
1037+
err = env_sensors_init_and_start(&application_work_q, env_data_send);
10381038
if (err) {
10391039
LOG_ERR("Environmental sensors init failed, error: %d", err);
10401040
}
10411041
#if CONFIG_LIGHT_SENSOR
1042-
err = light_sensor_init_and_start(light_sensor_data_send);
1042+
err = light_sensor_init_and_start(&application_work_q,
1043+
light_sensor_data_send);
10431044
if (err) {
10441045
LOG_ERR("Light sensor init failed, error: %d", err);
10451046
}

applications/asset_tracker/src/motion/motion.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ LOG_MODULE_REGISTER(motion, CONFIG_ASSET_TRACKER_LOG_LEVEL);
1515

1616
motion_handler_t handler;
1717
static struct device *accel_dev;
18+
static struct k_work_q *motion_work_q;
19+
static struct k_delayed_work motion_work;
1820

1921
#define FLIP_ACCELERATION_THRESHOLD 5.0
2022

@@ -99,6 +101,12 @@ static void sensor_trigger_handler(struct device *dev,
99101
ARG_UNUSED(dev);
100102
ARG_UNUSED(trigger);
101103

104+
k_delayed_work_submit_to_queue(motion_work_q, &motion_work, K_NO_WAIT);
105+
}
106+
107+
/**@brief Workqueue handler that runs the callback provided by application.*/
108+
static void motion_work_q_handler(struct k_work *work)
109+
{
102110
motion_data_t motion_data;
103111

104112
if (accelerometer_poll(&motion_data.acceleration) == 0) {
@@ -142,15 +150,19 @@ static int accelerometer_init(void)
142150
}
143151

144152
/**@brief Initialize motion module. */
145-
int motion_init_and_start(motion_handler_t motion_handler)
153+
int motion_init_and_start(struct k_work_q *work_q,
154+
motion_handler_t motion_handler)
146155
{
147-
if (motion_handler == NULL) {
156+
if ((work_q == NULL) || (motion_handler == NULL)) {
148157
return -EINVAL;
149158
}
150159

151160
int err;
152161

162+
motion_work_q = work_q;
153163
handler = motion_handler;
164+
165+
k_delayed_work_init(&motion_work, motion_work_q_handler);
154166
err = accelerometer_init();
155167

156168
if (err) {

applications/asset_tracker/src/motion/motion.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ typedef void (*motion_handler_t)(motion_data_t motion_data);
5151
*
5252
* @return 0 if the operation was successful, otherwise a (negative) error code.
5353
*/
54-
int motion_init_and_start(motion_handler_t motion_handler);
54+
int motion_init_and_start(struct k_work_q *work_q,
55+
motion_handler_t motion_handler);
5556

5657
/**
5758
* @brief Manually trigger the motion module to fetch data.

0 commit comments

Comments
 (0)