Skip to content

Commit

Permalink
drivers: modem: context helper: introduce modem context helper driver
Browse files Browse the repository at this point in the history
Initial support for modems in Zephyr use the following driver model:
- Main portions of code live in the modem specific driver.
  This includes internal socket management, command parsing, etc.
- They leverage a UART-based modem receiver helper to gather data.
- Interface with Zephyr networking via net_context offload APIs.

This implementation was good enough to kick start interest in
supporting modem usage in Zephyr, but lacks future scalability:
- The net_context offload APIs don't allow for operations such
  as offloaded DNS, SSL/TLS and other HW specific features.
- Since most of the code lives within the modem drivers, it's
  very hard for the Zephyr community to improve the driver layer
  over time.  Bugs found in 1 driver probably affect others due
  to copy/paste method of development.
- Lack of abstraction for different modem interfaces and command
  handlers makes it impossible to write a "dummy" layer which
  could be used for testing.
- Lack of centralized processing makes implementing low power modes
  and other advanced topics more difficult.

Introducing the modem context helper driver and sub-layers:
- modem context helper acts as an umbrella for several configurable
  layers and exposes this data to externals such as the modem shell.
  Included in the helper is GPIO pin config functions which are
  currently duplicated in most drivers.
- modem interface layer: this layer sits on the HW APIs for the
  peripheral which communicates with the modem.  Users of the modem
  interface can handle data via read/write functions.  Individual
  modem drivers can select from (potentially) several modem
  interfaces.
- modem command parser layer: this layer communicates with the
  modem interface and processes the data for use by modem drivers.

Fixes: zephyrproject-rtos#17922

Signed-off-by: Michael Scott <mike@foundries.io>
  • Loading branch information
mike-scott authored and Steven Wang committed Sep 2, 2019
1 parent 123b54d commit 1aeb159
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 10 deletions.
5 changes: 5 additions & 0 deletions drivers/modem/CMakeLists.txt
Expand Up @@ -3,6 +3,11 @@
zephyr_sources_ifdef(CONFIG_MODEM_RECEIVER modem_receiver.c)
zephyr_sources_ifdef(CONFIG_MODEM_SHELL modem_shell.c)

zephyr_sources_ifdef(CONFIG_MODEM_CONTEXT
modem_context.c
modem_pin.c
)

if(CONFIG_MODEM_UBLOX_SARA_R4)
zephyr_library_include_directories(${ZEPHYR_BASE}/subsys/net/ip)
zephyr_library_sources(ublox-sara-r4.c)
Expand Down
27 changes: 27 additions & 0 deletions drivers/modem/Kconfig
Expand Up @@ -39,6 +39,33 @@ config MODEM_RECEIVER_MAX_CONTEXTS
Maximum number of modem receiver contexts to handle. For most
purposes this should stay at 1.

config MODEM_CONTEXT
bool "Modem context helper driver [EXPERIMENTAL]"
help
This driver allows modem drivers to communicate with an interface
using custom defined protocols. Driver doesn't inspect received data
and all aspects of received protocol data are handled by application
work method provided. This driver combines abstractions for:
modem interface, command handler, pin config and socket handling each
of which will need to be configured.

if MODEM_CONTEXT

config MODEM_CONTEXT_MAX_NUM
int "Maximum number of modem contexts"
default 1
help
Maximum number of modem contexts to handle. For most
purposes this should stay at 1.

config MODEM_CONTEXT_VERBOSE_DEBUG
bool "Verbose debug output in the modem context"
help
Enabling this setting will turn on VERY heavy debugging from the
modem context helper. Do NOT leave on for production.

endif # MODEM_CONTEXT

config MODEM_SHELL
bool "Enable modem shell utilities"
select SHELL
Expand Down
132 changes: 132 additions & 0 deletions drivers/modem/modem_context.c
@@ -0,0 +1,132 @@
/** @file
* @brief Modem context helper driver
*
* A modem context driver allowing application to handle all
* aspects of received protocol data.
*/

/*
* Copyright (c) 2019 Foundries.io
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <logging/log.h>
LOG_MODULE_REGISTER(modem_context, CONFIG_MODEM_LOG_LEVEL);

#include <kernel.h>

#include "modem_context.h"

static struct modem_context *contexts[CONFIG_MODEM_CONTEXT_MAX_NUM];

char *modem_context_sprint_ip_addr(const struct sockaddr *addr)
{
static char buf[NET_IPV6_ADDR_LEN];

if (addr->sa_family == AF_INET6) {
return net_addr_ntop(AF_INET6, &net_sin6(addr)->sin6_addr,
buf, sizeof(buf));
}

if (addr->sa_family == AF_INET) {
return net_addr_ntop(AF_INET, &net_sin(addr)->sin_addr,
buf, sizeof(buf));
}

LOG_ERR("Unknown IP address family:%d", addr->sa_family);
strcpy(buf, "unk");
return buf;
}

int modem_context_get_addr_port(const struct sockaddr *addr, u16_t *port)
{
if (!addr || !port) {
return -EINVAL;
}

if (addr->sa_family == AF_INET6) {
*port = ntohs(net_sin6(addr)->sin6_port);
return 0;
} else if (addr->sa_family == AF_INET) {
*port = ntohs(net_sin(addr)->sin_port);
return 0;
}

return -EPROTONOSUPPORT;
}

/**
* @brief Finds modem context which owns the iface device.
*
* @param *dev: device used by the modem iface.
*
* @retval Modem context or NULL.
*/
struct modem_context *modem_context_from_iface_dev(struct device *dev)
{
int i;

for (i = 0; i < ARRAY_SIZE(contexts); i++) {
if (contexts[i] && contexts[i]->iface.dev == dev) {
return contexts[i];
}
}

return NULL;
}

/**
* @brief Assign a modem context if there is free space.
*
* @note Amount of stored modem contexts is determined by
* CONFIG_MODEM_CONTEXT_MAX_NUM.
*
* @param *ctx: modem context to persist.
*
* @retval 0 if ok, < 0 if error.
*/
static int modem_context_get(struct modem_context *ctx)
{
int i;

for (i = 0; i < ARRAY_SIZE(contexts); i++) {
if (!contexts[i]) {
contexts[i] = ctx;
return 0;
}
}

return -ENOMEM;
}

struct modem_context *modem_context_from_id(int id)
{
if (id >= 0 && id < ARRAY_SIZE(contexts)) {
return contexts[id];
} else {
return NULL;
}
}

int modem_context_register(struct modem_context *ctx)
{
int ret;

if (!ctx) {
return -EINVAL;
}

ret = modem_context_get(ctx);
if (ret < 0) {
return ret;
}

ret = modem_pin_init(ctx);
if (ret < 0) {
LOG_ERR("modem pin init error: %d", ret);
return ret;
}

return 0;
}
138 changes: 138 additions & 0 deletions drivers/modem/modem_context.h
@@ -0,0 +1,138 @@
/** @file
* @brief Modem context header file.
*
* A modem context driver allowing application to handle all
* aspects of received protocol data.
*/

/*
* Copyright (c) 2019 Foundries.io
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_CONTEXT_H_
#define ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_CONTEXT_H_

#include <kernel.h>
#include <net/buf.h>
#include <net/net_ip.h>
#include <sys/ring_buffer.h>

#ifdef __cplusplus
extern "C" {
#endif

#define MODEM_PIN(name_, pin_, flags_) { \
.dev_name = name_, \
.pin = pin_, \
.init_flags = flags_ \
}

struct modem_iface {
struct device *dev;

int (*read)(struct modem_iface *iface, u8_t *buf, size_t size,
size_t *bytes_read);
int (*write)(struct modem_iface *iface, const u8_t *buf, size_t size);

/* implementation data */
void *iface_data;
};

struct modem_cmd_handler {
void (*process)(struct modem_cmd_handler *cmd_handler,
struct modem_iface *iface);

/* implementation data */
void *cmd_handler_data;
};

struct modem_pin {
struct device *gpio_port_dev;
char *dev_name;
u32_t pin;
int init_flags;
};

struct modem_context {
/* modem data */
char *data_manufacturer;
char *data_model;
char *data_revision;
char *data_imei;
int data_rssi;

/* pin config */
struct modem_pin *pins;
size_t pins_len;

/* interface config */
struct modem_iface iface;

/* command handler config */
struct modem_cmd_handler cmd_handler;

/* driver data */
void *driver_data;
};

/**
* @brief IP address to string
*
* @param addr: sockaddr to be converted
*
* @retval Buffer with IP in string form
*/
char *modem_context_sprint_ip_addr(const struct sockaddr *addr);

/**
* @brief Get port from IP address
*
* @param addr: sockaddr
* @param port: store port
*
* @retval 0 if ok, < 0 if error.
*/
int modem_context_get_addr_port(const struct sockaddr *addr, u16_t *port);

/**
* @brief Gets modem context by id.
*
* @param id: modem context id.
*
* @retval modem context or NULL.
*/
struct modem_context *modem_context_from_id(int id);

/**
* @brief Finds modem context which owns the iface device.
*
* @param *dev: device used by the modem iface.
*
* @retval Modem context or NULL.
*/
struct modem_context *modem_context_from_iface_dev(struct device *dev);

/**
* @brief Registers modem context.
*
* @note Prepares modem context to be used.
*
* @param *ctx: modem context to register.
*
* @retval 0 if ok, < 0 if error.
*/
int modem_context_register(struct modem_context *ctx);

/* pin config functions */
int modem_pin_read(struct modem_context *ctx, u32_t pin);
int modem_pin_write(struct modem_context *ctx, u32_t pin, u32_t value);
int modem_pin_config(struct modem_context *ctx, u32_t pin, int flags);
int modem_pin_init(struct modem_context *ctx);

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_CONTEXT_H_ */
78 changes: 78 additions & 0 deletions drivers/modem/modem_pin.c
@@ -0,0 +1,78 @@
/** @file
* @brief Modem pin setup for modem context driver
*
* GPIO-based pin handling for the modem context driver
*/

/*
* Copyright (c) 2019 Foundries.io
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/types.h>
#include <device.h>
#include <drivers/gpio.h>

#include "modem_context.h"

int modem_pin_read(struct modem_context *ctx, u32_t pin)
{
int ret = 0;
u32_t value = 0;

if (pin < 0 || pin >= ctx->pins_len) {
return -ENODEV;
}

ret = gpio_pin_read(ctx->pins[pin].gpio_port_dev, ctx->pins[pin].pin,
&value);
if (ret < 0) {
return ret;
}

return (int)value;
}

int modem_pin_write(struct modem_context *ctx, u32_t pin, u32_t value)
{
if (pin < 0 || pin >= ctx->pins_len) {
return -ENODEV;
}

return gpio_pin_write(ctx->pins[pin].gpio_port_dev, ctx->pins[pin].pin,
value);
}

int modem_pin_config(struct modem_context *ctx, u32_t pin, int flags)
{
if (pin < 0 || pin >= ctx->pins_len) {
return -ENODEV;
}

return gpio_pin_configure(ctx->pins[pin].gpio_port_dev,
ctx->pins[pin].pin, flags);
}

int modem_pin_init(struct modem_context *ctx)
{
int i, ret;

/* setup port devices and pin directions */
for (i = 0; i < ctx->pins_len; i++) {
ctx->pins[i].gpio_port_dev =
device_get_binding(ctx->pins[i].dev_name);
if (!ctx->pins[i].gpio_port_dev) {
return -ENODEV;
}

ret = gpio_pin_configure(ctx->pins[i].gpio_port_dev,
ctx->pins[i].pin,
ctx->pins[i].init_flags);
if (ret < 0) {
return ret;
}
}

return 0;
}

0 comments on commit 1aeb159

Please sign in to comment.