Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | /*
* Copyright (c) 2021 Nordic Semiconductor ASA
* Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ipc/ipc_service.h>
#include <zephyr/ipc/ipc_service_backend.h>
#include <zephyr/logging/log.h>
#include <zephyr/zephyr.h>
#include <zephyr/device.h>
LOG_MODULE_REGISTER(ipc_service, CONFIG_IPC_SERVICE_LOG_LEVEL);
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);
}
int ipc_service_register_endpoint(const struct device *instance,
struct ipc_ept *ept,
const struct ipc_ept_cfg *cfg)
{
const struct ipc_service_backend *backend;
if (!instance || !ept || !cfg) {
LOG_ERR("Invalid instance, endpoint or configuration");
return -EINVAL;
}
backend = (const struct ipc_service_backend *) instance->api;
if (!backend || !backend->register_endpoint) {
LOG_ERR("Invalid backend configuration");
return -EIO;
}
LOG_DBG("Register endpoint %s", cfg->name ? cfg->name : "");
ept->instance = instance;
return backend->register_endpoint(instance, &ept->token, cfg);
}
int ipc_service_send(struct ipc_ept *ept, const void *data, size_t len)
{
const struct ipc_service_backend *backend;
if (!ept) {
LOG_ERR("Invalid endpoint");
return -EINVAL;
}
backend = ept->instance->api;
if (!backend || !backend->send) {
LOG_ERR("Invalid backend configuration");
return -EIO;
}
return backend->send(ept->instance, ept->token, data, len);
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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);
}
|