zephyr/subsys/lorawan/services/lorawan_services.c

181 lines
4.5 KiB
C
Raw Normal View History

/*
* Copyright (c) 2022 Martin Jäger <martin@libre.solar>
* Copyright (c) 2022 tado GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "lorawan_services.h"
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(lorawan_services, CONFIG_LORAWAN_SERVICES_LOG_LEVEL);
struct service_uplink_msg {
sys_snode_t node;
/* absolute ticks when this message should be scheduled */
int64_t ticks;
/* sufficient space for up to 3 answers (max 6 bytes each) */
uint8_t data[18];
uint8_t len;
uint8_t port;
bool used;
};
K_THREAD_STACK_DEFINE(thread_stack_area, CONFIG_LORAWAN_SERVICES_THREAD_STACK_SIZE);
/*
* The services need a dedicated work queue, as the LoRaWAN stack uses the system
* work queue and gets blocked if other LoRaWAN messages are sent and processed from
* the system work queue in parallel.
*/
static struct k_work_q services_workq;
static struct k_work_delayable uplink_work;
/* single-linked list (with pointers) and array for implementation of priority queue */
static struct service_uplink_msg messages[10];
static sys_slist_t msg_list;
static struct k_sem msg_sem;
static void uplink_handler(struct k_work *work)
{
struct service_uplink_msg msg_copy;
struct service_uplink_msg *first;
sys_snode_t *node;
int err;
ARG_UNUSED(work);
/* take semaphore and create a copy of the next message */
k_sem_take(&msg_sem, K_FOREVER);
node = sys_slist_get(&msg_list);
if (node == NULL) {
goto out;
}
first = CONTAINER_OF(node, struct service_uplink_msg, node);
msg_copy = *first;
first->used = false;
sys_slist_remove(&msg_list, NULL, &first->node);
/* semaphore must be given back before calling lorawan_send */
k_sem_give(&msg_sem);
err = lorawan_send(msg_copy.port, msg_copy.data, msg_copy.len, LORAWAN_MSG_UNCONFIRMED);
if (!err) {
LOG_DBG("Message sent to port %d", msg_copy.port);
} else {
LOG_ERR("Sending message to port %d failed: %d",
msg_copy.port, err);
}
/* take the semaphore again to schedule next uplink */
k_sem_take(&msg_sem, K_FOREVER);
node = sys_slist_peek_head(&msg_list);
if (node == NULL) {
goto out;
}
first = CONTAINER_OF(node, struct service_uplink_msg, node);
k_work_reschedule_for_queue(&services_workq, &uplink_work,
K_TIMEOUT_ABS_TICKS(first->ticks));
out:
k_sem_give(&msg_sem);
}
static inline void insert_uplink(struct service_uplink_msg *msg_new)
{
struct service_uplink_msg *msg_prev;
if (sys_slist_is_empty(&msg_list)) {
sys_slist_append(&msg_list, &msg_new->node);
} else {
int count = 0;
SYS_SLIST_FOR_EACH_CONTAINER(&msg_list, msg_prev, node) {
count++;
if (msg_prev->ticks <= msg_new->ticks) {
break;
}
}
if (msg_prev != NULL) {
sys_slist_insert(&msg_list, &msg_prev->node, &msg_new->node);
} else {
sys_slist_append(&msg_list, &msg_new->node);
}
}
}
int lorawan_services_schedule_uplink(uint8_t port, uint8_t *data, uint8_t len, uint32_t timeout)
{
struct service_uplink_msg *next;
int64_t timeout_abs_ticks;
if (len > sizeof(messages[0].data)) {
LOG_ERR("Uplink payload for port %u too long: %u bytes", port, len);
LOG_HEXDUMP_ERR(data, len, "Payload: ");
return -EFBIG;
}
timeout_abs_ticks = k_uptime_ticks() + k_ms_to_ticks_ceil64(timeout);
k_sem_take(&msg_sem, K_FOREVER);
for (int i = 0; i < ARRAY_SIZE(messages); i++) {
if (!messages[i].used) {
memcpy(messages[i].data, data, len);
messages[i].port = port;
messages[i].len = len;
messages[i].ticks = timeout_abs_ticks;
messages[i].used = true;
insert_uplink(&messages[i]);
next = SYS_SLIST_PEEK_HEAD_CONTAINER(&msg_list, next, node);
if (next != NULL) {
k_work_reschedule_for_queue(&services_workq, &uplink_work,
K_TIMEOUT_ABS_TICKS(next->ticks));
}
k_sem_give(&msg_sem);
return 0;
}
}
k_sem_give(&msg_sem);
LOG_WRN("Message queue full, message for port %u dropped.", port);
return -ENOSPC;
}
int lorawan_services_reschedule_work(struct k_work_delayable *dwork, k_timeout_t delay)
{
return k_work_reschedule_for_queue(&services_workq, dwork, delay);
}
init: remove the need for a dummy device pointer in SYS_INIT functions The init infrastructure, found in `init.h`, is currently used by: - `SYS_INIT`: to call functions before `main` - `DEVICE_*`: to initialize devices They are all sorted according to an initialization level + a priority. `SYS_INIT` calls are really orthogonal to devices, however, the required function signature requires a `const struct device *dev` as a first argument. The only reason for that is because the same init machinery is used by devices, so we have something like: ```c struct init_entry { int (*init)(const struct device *dev); /* only set by DEVICE_*, otherwise NULL */ const struct device *dev; } ``` As a result, we end up with such weird/ugly pattern: ```c static int my_init(const struct device *dev) { /* always NULL! add ARG_UNUSED to avoid compiler warning */ ARG_UNUSED(dev); ... } ``` This is really a result of poor internals isolation. This patch proposes a to make init entries more flexible so that they can accept sytem initialization calls like this: ```c static int my_init(void) { ... } ``` This is achieved using a union: ```c union init_function { /* for SYS_INIT, used when init_entry.dev == NULL */ int (*sys)(void); /* for DEVICE*, used when init_entry.dev != NULL */ int (*dev)(const struct device *dev); }; struct init_entry { /* stores init function (either for SYS_INIT or DEVICE*) union init_function init_fn; /* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows * to know which union entry to call. */ const struct device *dev; } ``` This solution **does not increase ROM usage**, and allows to offer clean public APIs for both SYS_INIT and DEVICE*. Note that however, init machinery keeps a coupling with devices. **NOTE**: This is a breaking change! All `SYS_INIT` functions will need to be converted to the new signature. See the script offered in the following commit. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> init: convert SYS_INIT functions to the new signature Conversion scripted using scripts/utils/migrate_sys_init.py. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> manifest: update projects for SYS_INIT changes Update modules with updated SYS_INIT calls: - hal_ti - lvgl - sof - TraceRecorderSource Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> tests: devicetree: devices: adjust test Adjust test according to the recently introduced SYS_INIT infrastructure. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> tests: kernel: threads: adjust SYS_INIT call Adjust to the new signature: int (*init_fn)(void); Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 15:33:44 +08:00
static int lorawan_services_init(void)
{
sys_slist_init(&msg_list);
k_sem_init(&msg_sem, 1, 1);
k_work_queue_init(&services_workq);
k_work_queue_start(&services_workq,
thread_stack_area, K_THREAD_STACK_SIZEOF(thread_stack_area),
CONFIG_LORAWAN_SERVICES_THREAD_PRIORITY, NULL);
k_work_init_delayable(&uplink_work, uplink_handler);
k_thread_name_set(&services_workq.thread, "lorawan_services");
return 0;
}
SYS_INIT(lorawan_services_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);