zephyr/subsys/modem/modem_pipelink.c
Bjarki Arge Andreasen 1f7d0b6cb0 modem: add modem_pipelink module
Add modem pipelink module which exposes modem pipes globally.
The pipelink module implements a callback to inform when a
pipe becomes available to use by whichever modem is attached
to it. This could be a shell, or a network interface.

The module aims to allow modem drivers to be split into modules,
and allowing applications to implement their own custom logic
without altering the modem drivers.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
2024-06-10 15:12:34 -05:00

83 lines
1.6 KiB
C

/*
* Copyright (c) 2024 Trackunit Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/modem/pipelink.h>
static void try_callback(struct modem_pipelink *link, enum modem_pipelink_event event)
{
if (link->callback == NULL) {
return;
}
link->callback(link, event, link->user_data);
}
void modem_pipelink_attach(struct modem_pipelink *link,
modem_pipelink_callback callback,
void *user_data)
{
K_SPINLOCK(&link->spinlock) {
link->callback = callback;
link->user_data = user_data;
}
}
bool modem_pipelink_is_connected(struct modem_pipelink *link)
{
bool connected;
K_SPINLOCK(&link->spinlock) {
connected = link->connected;
}
return connected;
}
struct modem_pipe *modem_pipelink_get_pipe(struct modem_pipelink *link)
{
return link->pipe;
}
void modem_pipelink_release(struct modem_pipelink *link)
{
K_SPINLOCK(&link->spinlock) {
link->callback = NULL;
link->user_data = NULL;
}
}
void modem_pipelink_init(struct modem_pipelink *link, struct modem_pipe *pipe)
{
link->pipe = pipe;
link->callback = NULL;
link->user_data = NULL;
link->connected = false;
}
void modem_pipelink_notify_connected(struct modem_pipelink *link)
{
K_SPINLOCK(&link->spinlock) {
if (link->connected) {
K_SPINLOCK_BREAK;
}
link->connected = true;
try_callback(link, MODEM_PIPELINK_EVENT_CONNECTED);
}
}
void modem_pipelink_notify_disconnected(struct modem_pipelink *link)
{
K_SPINLOCK(&link->spinlock) {
if (!link->connected) {
K_SPINLOCK_BREAK;
}
link->connected = false;
try_callback(link, MODEM_PIPELINK_EVENT_DISCONNECTED);
}
}