2021-06-09 15:22:38 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
2021-10-25 17:40:28 +02:00
|
|
|
* Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
|
2021-06-09 15:22:38 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/ipc/ipc_service.h>
|
|
|
|
#include <zephyr/ipc/ipc_service_backend.h>
|
2021-06-09 15:22:38 +02:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/device.h>
|
2021-06-09 15:22:38 +02:00
|
|
|
|
2021-07-26 17:22:07 +02:00
|
|
|
LOG_MODULE_REGISTER(ipc_service, CONFIG_IPC_SERVICE_LOG_LEVEL);
|
2021-06-09 15:22:38 +02:00
|
|
|
|
2021-11-11 16:34:51 +01:00
|
|
|
int ipc_service_open_instance(const struct device *instance)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!instance) {
|
|
|
|
LOG_ERR("Invalid instance");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend = (const struct ipc_service_backend *) instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->open_instance) {
|
|
|
|
/* maybe not needed on backend */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->open_instance(instance);
|
|
|
|
}
|
|
|
|
|
2022-07-12 17:38:27 +02:00
|
|
|
int ipc_service_close_instance(const struct device *instance)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!instance) {
|
|
|
|
LOG_ERR("Invalid instance");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend = (const struct ipc_service_backend *) instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->close_instance) {
|
|
|
|
/* maybe not needed on backend */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->close_instance(instance);
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:40:28 +02:00
|
|
|
int ipc_service_register_endpoint(const struct device *instance,
|
|
|
|
struct ipc_ept *ept,
|
|
|
|
const struct ipc_ept_cfg *cfg)
|
2021-06-09 15:22:38 +02:00
|
|
|
{
|
2021-10-25 17:40:28 +02:00
|
|
|
const struct ipc_service_backend *backend;
|
2021-06-09 15:22:38 +02:00
|
|
|
|
2021-10-25 17:40:28 +02:00
|
|
|
if (!instance || !ept || !cfg) {
|
|
|
|
LOG_ERR("Invalid instance, endpoint or configuration");
|
2021-06-09 15:22:38 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:40:28 +02:00
|
|
|
backend = (const struct ipc_service_backend *) instance->api;
|
2021-06-09 15:22:38 +02:00
|
|
|
|
|
|
|
if (!backend || !backend->register_endpoint) {
|
2021-10-25 17:40:28 +02:00
|
|
|
LOG_ERR("Invalid backend configuration");
|
2021-06-09 15:22:38 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:40:28 +02:00
|
|
|
LOG_DBG("Register endpoint %s", cfg->name ? cfg->name : "");
|
2021-06-09 15:22:38 +02:00
|
|
|
|
2021-10-25 17:40:28 +02:00
|
|
|
ept->instance = instance;
|
|
|
|
|
|
|
|
return backend->register_endpoint(instance, &ept->token, cfg);
|
2021-06-09 15:22:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 16:42:02 +02:00
|
|
|
int ipc_service_deregister_endpoint(struct ipc_ept *ept)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!ept) {
|
|
|
|
LOG_ERR("Invalid endpoint");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend = ept->instance->api;
|
|
|
|
|
|
|
|
if (!backend || !backend->deregister_endpoint) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = backend->deregister_endpoint(ept->instance, ept->token);
|
|
|
|
if (err != 0) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ept->instance = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-09 15:22:38 +02:00
|
|
|
int ipc_service_send(struct ipc_ept *ept, const void *data, size_t len)
|
|
|
|
{
|
2021-10-25 17:40:28 +02:00
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
2021-11-15 15:45:43 +01:00
|
|
|
if (!ept) {
|
|
|
|
LOG_ERR("Invalid endpoint");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:24:58 +02:00
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:40:28 +02:00
|
|
|
backend = ept->instance->api;
|
|
|
|
|
2021-06-09 15:22:38 +02:00
|
|
|
if (!backend || !backend->send) {
|
2021-10-25 17:40:28 +02:00
|
|
|
LOG_ERR("Invalid backend configuration");
|
2021-06-09 15:22:38 +02:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:40:28 +02:00
|
|
|
return backend->send(ept->instance, ept->token, data, len);
|
2021-06-09 15:22:38 +02:00
|
|
|
}
|
2022-03-21 16:19:13 +01:00
|
|
|
|
|
|
|
int ipc_service_get_tx_buffer_size(struct ipc_ept *ept)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!ept) {
|
|
|
|
LOG_ERR("Invalid endpoint");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:24:58 +02:00
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2022-03-21 16:19:13 +01:00
|
|
|
backend = ept->instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->get_tx_buffer_size) {
|
|
|
|
LOG_ERR("No-copy feature not available");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->get_tx_buffer_size(ept->instance, ept->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ipc_service_get_tx_buffer(struct ipc_ept *ept, void **data, uint32_t *len, k_timeout_t wait)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!ept || !data || !len) {
|
|
|
|
LOG_ERR("Invalid endpoint, data or len pointer");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:24:58 +02:00
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2022-03-21 16:19:13 +01:00
|
|
|
backend = ept->instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->send_nocopy || !backend->get_tx_buffer) {
|
|
|
|
LOG_ERR("No-copy feature not available");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->get_tx_buffer(ept->instance, ept->token, data, len, wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ipc_service_drop_tx_buffer(struct ipc_ept *ept, const void *data)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!ept || !data) {
|
|
|
|
LOG_ERR("Invalid endpoint or data pointer");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:24:58 +02:00
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2022-03-21 16:19:13 +01:00
|
|
|
backend = ept->instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->drop_tx_buffer) {
|
|
|
|
LOG_ERR("No-copy feature not available");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->drop_tx_buffer(ept->instance, ept->token, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ipc_service_send_nocopy(struct ipc_ept *ept, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!ept) {
|
|
|
|
LOG_ERR("Invalid endpoint");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:24:58 +02:00
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2022-03-21 16:19:13 +01:00
|
|
|
backend = ept->instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->get_tx_buffer || !backend->send_nocopy) {
|
|
|
|
LOG_ERR("No-copy feature not available");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->send_nocopy(ept->instance, ept->token, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ipc_service_hold_rx_buffer(struct ipc_ept *ept, void *data)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!ept) {
|
|
|
|
LOG_ERR("Invalid endpoint");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:24:58 +02:00
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2022-03-21 16:19:13 +01:00
|
|
|
backend = ept->instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We also need the release function */
|
|
|
|
if (!backend->release_rx_buffer || !backend->hold_rx_buffer) {
|
|
|
|
LOG_ERR("No-copy feature not available");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->hold_rx_buffer(ept->instance, ept->token, data);
|
|
|
|
}
|
|
|
|
int ipc_service_release_rx_buffer(struct ipc_ept *ept, void *data)
|
|
|
|
{
|
|
|
|
const struct ipc_service_backend *backend;
|
|
|
|
|
|
|
|
if (!ept) {
|
|
|
|
LOG_ERR("Invalid endpoint");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:24:58 +02:00
|
|
|
if (!ept->instance) {
|
|
|
|
LOG_ERR("Endpoint not registered\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2022-03-21 16:19:13 +01:00
|
|
|
backend = ept->instance->api;
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
LOG_ERR("Invalid backend configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We also need the hold function */
|
|
|
|
if (!backend->hold_rx_buffer || !backend->release_rx_buffer) {
|
|
|
|
LOG_ERR("No-copy feature not available");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend->release_rx_buffer(ept->instance, ept->token, data);
|
|
|
|
}
|