2019-08-07 17:04:00 +02:00
|
|
|
/** @file
|
|
|
|
* @brief Modem socket / packet size handler
|
|
|
|
*
|
|
|
|
* Generic modem socket and packet size implementation for modem context
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2020-01-27 21:55:49 +01:00
|
|
|
* Copyright (c) 2019-2020 Foundries.io
|
2019-08-07 17:04:00 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/sys/fdtable.h>
|
2019-08-07 17:04:00 +02:00
|
|
|
|
|
|
|
#include "modem_socket.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Packet Size Support Functions
|
|
|
|
*/
|
|
|
|
|
2021-10-19 09:24:12 +02:00
|
|
|
uint16_t modem_socket_next_packet_size(struct modem_socket_config *cfg, struct modem_socket *sock)
|
2020-01-27 22:13:01 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint16_t total = 0U;
|
2020-01-27 22:13:01 +01:00
|
|
|
|
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
|
|
|
|
if (!sock || !sock->packet_count) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = sock->packet_sizes[0];
|
|
|
|
|
|
|
|
exit:
|
|
|
|
k_sem_give(&cfg->sem_lock);
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static uint16_t modem_socket_packet_get_total(struct modem_socket *sock)
|
2019-08-07 17:04:00 +02:00
|
|
|
{
|
|
|
|
int i;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint16_t total = 0U;
|
2019-08-07 17:04:00 +02:00
|
|
|
|
|
|
|
if (!sock || !sock->packet_count) {
|
|
|
|
return 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sock->packet_count; i++) {
|
|
|
|
total += sock->packet_sizes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int modem_socket_packet_drop_first(struct modem_socket *sock)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!sock || !sock->packet_count) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->packet_count--;
|
|
|
|
for (i = 0; i < sock->packet_count; i++) {
|
2021-10-19 09:24:12 +02:00
|
|
|
sock->packet_sizes[i] = sock->packet_sizes[i + 1];
|
2019-08-07 17:04:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sock->packet_sizes[sock->packet_count] = 0U;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:24:12 +02:00
|
|
|
int modem_socket_packet_size_update(struct modem_socket_config *cfg, struct modem_socket *sock,
|
|
|
|
int new_total)
|
2019-08-07 17:04:00 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint16_t old_total = 0U;
|
2019-08-07 17:04:00 +02:00
|
|
|
|
|
|
|
if (!sock) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
if (new_total < 0) {
|
|
|
|
new_total += modem_socket_packet_get_total(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_total <= 0) {
|
|
|
|
/* reset outstanding value here */
|
|
|
|
sock->packet_count = 0U;
|
|
|
|
sock->packet_sizes[0] = 0U;
|
2021-10-21 11:18:17 +02:00
|
|
|
k_poll_signal_reset(&sock->sig_data_ready);
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_total = modem_socket_packet_get_total(sock);
|
|
|
|
if (new_total == old_total) {
|
|
|
|
goto data_ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove sent packets */
|
|
|
|
if (new_total < old_total) {
|
|
|
|
/* remove packets that are not included in new_size */
|
|
|
|
while (old_total > new_total && sock->packet_count > 0) {
|
2020-01-27 21:55:49 +01:00
|
|
|
/* handle partial read */
|
|
|
|
if (old_total - new_total < sock->packet_sizes[0]) {
|
|
|
|
sock->packet_sizes[0] -= old_total - new_total;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
old_total -= sock->packet_sizes[0];
|
|
|
|
modem_socket_packet_drop_first(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto data_ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* new packet to add */
|
|
|
|
if (sock->packet_count >= CONFIG_MODEM_SOCKET_PACKET_COUNT) {
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_total - old_total > 0) {
|
|
|
|
sock->packet_sizes[sock->packet_count] = new_total - old_total;
|
|
|
|
sock->packet_count++;
|
|
|
|
} else {
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_ready:
|
2021-10-21 11:18:17 +02:00
|
|
|
if (sock->packet_sizes[0]) {
|
|
|
|
k_poll_signal_raise(&sock->sig_data_ready, 0);
|
|
|
|
} else {
|
|
|
|
k_poll_signal_reset(&sock->sig_data_ready);
|
|
|
|
}
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return new_total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Socket Support Functions
|
|
|
|
*/
|
|
|
|
|
drivers/modem: Patch fix for sock id and fd
There is is an error in the way that the sock id is
determined. A unique fd is reserved and assigned appropriatly, but
the id, which should correspond to a socket number within the modem,
is set to an invalid, and identical for all sockets, value, and then
not used appropriatly in the drivers, instead, the sock_fd is used,
which can be any value really, it is not related to the socket number
in any way.
This results in the drivers only working if the reserved fd happens
to be between base_socket_num and (socket_len - 1)
This patch assignes the id to the index of the socket + the
base_socket_num, the socket at index 0 will get the id 1 if the
base_socket_num is 1, and the modem_socket_from_id should then
be used to get a pointer to the socket, since the id is not
neccesarily equal to the index.
The FIXME has been solved by adding a note both at the start
of the modem_socket_get function and inside the Kconfig file
for the MODEM_SOCKET option description. It is not an error,
but the user must be aware that it uses the POSIX file
descriptors, for which only 4 are allocated by default.
This patch fixes the bug, without breaking the brittle modem
drivers which currently are built around this bug.
The modem drivers should be updated to use the id as the
socket num instead of the sock_fd after this fix has been merged.
The "socket # needs assigning" has been removed, as that is what
this patch is doing
I also added comments to the id and sock_fd in the modem_socket
structure to help developers use the id and fd appropriately.
Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
2022-07-08 21:28:07 +02:00
|
|
|
/*
|
|
|
|
* This function reserves a file descriptor from the fdtable, make sure to update the
|
|
|
|
* POSIX_FDS_MAX Kconfig option to support at minimum the required amount of sockets
|
|
|
|
*/
|
2021-10-19 09:24:12 +02:00
|
|
|
int modem_socket_get(struct modem_socket_config *cfg, int family, int type, int proto)
|
2019-08-07 17:04:00 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
for (i = 0; i < cfg->sockets_len; i++) {
|
2022-11-21 08:52:44 +01:00
|
|
|
if (cfg->sockets[i].id < cfg->base_socket_id) {
|
2019-08-07 17:04:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= cfg->sockets_len) {
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg->sockets[i].sock_fd = z_reserve_fd();
|
|
|
|
if (cfg->sockets[i].sock_fd < 0) {
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg->sockets[i].family = family;
|
|
|
|
cfg->sockets[i].type = type;
|
|
|
|
cfg->sockets[i].ip_proto = proto;
|
2022-11-21 08:52:44 +01:00
|
|
|
cfg->sockets[i].id = (cfg->assign_id) ? (i + cfg->base_socket_id) :
|
|
|
|
(cfg->base_socket_id + cfg->sockets_len);
|
2019-12-04 10:04:07 +01:00
|
|
|
z_finalize_fd(cfg->sockets[i].sock_fd, &cfg->sockets[i],
|
|
|
|
(const struct fd_op_vtable *)cfg->vtable);
|
2019-08-07 17:04:00 +02:00
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return cfg->sockets[i].sock_fd;
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:24:12 +02:00
|
|
|
struct modem_socket *modem_socket_from_fd(struct modem_socket_config *cfg, int sock_fd)
|
2019-08-07 17:04:00 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
for (i = 0; i < cfg->sockets_len; i++) {
|
|
|
|
if (cfg->sockets[i].sock_fd == sock_fd) {
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return &cfg->sockets[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:24:12 +02:00
|
|
|
struct modem_socket *modem_socket_from_id(struct modem_socket_config *cfg, int id)
|
2019-08-07 17:04:00 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-11-21 08:52:44 +01:00
|
|
|
if (id < cfg->base_socket_id) {
|
2019-08-07 17:04:00 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
for (i = 0; i < cfg->sockets_len; i++) {
|
|
|
|
if (cfg->sockets[i].id == id) {
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
return &cfg->sockets[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_give(&cfg->sem_lock);
|
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct modem_socket *modem_socket_from_newid(struct modem_socket_config *cfg)
|
|
|
|
{
|
2022-11-21 08:52:44 +01:00
|
|
|
return modem_socket_from_id(cfg, cfg->base_socket_id + cfg->sockets_len);
|
2019-08-07 17:04:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void modem_socket_put(struct modem_socket_config *cfg, int sock_fd)
|
|
|
|
{
|
2019-08-13 18:06:42 +02:00
|
|
|
struct modem_socket *sock = modem_socket_from_fd(cfg, sock_fd);
|
|
|
|
|
|
|
|
if (!sock) {
|
2019-08-07 17:04:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
|
2022-11-21 08:52:44 +01:00
|
|
|
sock->id = cfg->base_socket_id - 1;
|
2019-08-13 18:06:42 +02:00
|
|
|
sock->sock_fd = -1;
|
2020-01-27 21:52:56 +01:00
|
|
|
sock->is_waiting = false;
|
2020-01-27 22:14:49 +01:00
|
|
|
sock->is_connected = false;
|
2019-08-13 18:06:42 +02:00
|
|
|
(void)memset(&sock->src, 0, sizeof(struct sockaddr));
|
|
|
|
(void)memset(&sock->dst, 0, sizeof(struct sockaddr));
|
2020-08-04 11:12:39 +02:00
|
|
|
memset(&sock->packet_sizes, 0, sizeof(sock->packet_sizes));
|
|
|
|
sock->packet_count = 0;
|
|
|
|
k_sem_reset(&sock->sem_data_ready);
|
2021-10-21 11:18:17 +02:00
|
|
|
k_poll_signal_reset(&sock->sig_data_ready);
|
2020-01-27 21:59:33 +01:00
|
|
|
|
|
|
|
k_sem_give(&cfg->sem_lock);
|
2019-08-07 17:04:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic Poll Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-10-15 09:28:44 +02:00
|
|
|
* FIXME: The design here makes the poll function non-reentrant for same sockets.
|
|
|
|
* If two different threads poll on two identical sockets we'll end up with unexpected
|
2019-08-07 17:04:00 +02:00
|
|
|
* behavior - the higher priority thread will be unblocked, regardless on which
|
|
|
|
* socket it polled. I think we could live with such limitation though in the
|
|
|
|
* initial implementation, but this should be improved in the future.
|
|
|
|
*/
|
2021-10-19 09:24:12 +02:00
|
|
|
int modem_socket_poll(struct modem_socket_config *cfg, struct zsock_pollfd *fds, int nfds,
|
|
|
|
int msecs)
|
2019-08-07 17:04:00 +02:00
|
|
|
{
|
|
|
|
struct modem_socket *sock;
|
|
|
|
int ret, i;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t found_count = 0;
|
2019-08-07 17:04:00 +02:00
|
|
|
|
2021-10-15 09:28:44 +02:00
|
|
|
if (!cfg || nfds > CONFIG_NET_SOCKETS_POLL_MAX) {
|
2019-08-07 17:04:00 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-10-15 09:28:44 +02:00
|
|
|
struct k_poll_event events[nfds];
|
|
|
|
int eventcount = 0;
|
2020-07-02 15:06:41 +02:00
|
|
|
|
2019-08-07 17:04:00 +02:00
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
sock = modem_socket_from_fd(cfg, fds[i].fd);
|
|
|
|
if (sock) {
|
|
|
|
/*
|
|
|
|
* Handle user check for POLLOUT events:
|
2020-07-02 15:06:41 +02:00
|
|
|
* we consider the socket to always be writable.
|
2019-08-07 17:04:00 +02:00
|
|
|
*/
|
2020-06-08 22:36:14 +02:00
|
|
|
if (fds[i].events & ZSOCK_POLLOUT) {
|
2019-08-07 17:04:00 +02:00
|
|
|
found_count++;
|
2020-07-02 15:06:41 +02:00
|
|
|
break;
|
2020-06-08 22:36:14 +02:00
|
|
|
} else if (fds[i].events & ZSOCK_POLLIN) {
|
2021-10-15 09:28:44 +02:00
|
|
|
k_poll_event_init(&events[eventcount++], K_POLL_TYPE_SIGNAL,
|
2021-10-21 11:18:17 +02:00
|
|
|
K_POLL_MODE_NOTIFY_ONLY, &sock->sig_data_ready);
|
2020-07-02 15:06:41 +02:00
|
|
|
if (sock->packet_sizes[0] > 0U) {
|
|
|
|
found_count++;
|
|
|
|
break;
|
|
|
|
}
|
2019-08-07 17:04:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 15:06:41 +02:00
|
|
|
/* Avoid waiting on semaphore if we have already found an event */
|
|
|
|
ret = 0;
|
|
|
|
if (!found_count) {
|
2021-10-15 09:28:44 +02:00
|
|
|
k_timeout_t timeout = K_FOREVER;
|
|
|
|
|
|
|
|
if (msecs >= 0) {
|
|
|
|
timeout = K_MSEC(msecs);
|
|
|
|
}
|
|
|
|
ret = k_poll(events, eventcount, timeout);
|
2019-08-07 17:04:00 +02:00
|
|
|
}
|
2020-07-02 15:06:41 +02:00
|
|
|
/* Reset counter as we reiterate on all polled sockets */
|
|
|
|
found_count = 0;
|
2019-08-07 17:04:00 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
sock = modem_socket_from_fd(cfg, fds[i].fd);
|
|
|
|
if (!sock) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-02 15:06:41 +02:00
|
|
|
/*
|
|
|
|
* Handle user check for ZSOCK_POLLOUT events:
|
|
|
|
* we consider the socket to always be writable.
|
|
|
|
*/
|
|
|
|
if (fds[i].events & ZSOCK_POLLOUT) {
|
|
|
|
fds[i].revents |= ZSOCK_POLLOUT;
|
|
|
|
found_count++;
|
2021-10-15 09:28:44 +02:00
|
|
|
} else if ((fds[i].events & ZSOCK_POLLIN) && (sock->packet_sizes[0] > 0U)) {
|
2020-06-08 22:36:14 +02:00
|
|
|
fds[i].revents |= ZSOCK_POLLIN;
|
2019-08-07 17:04:00 +02:00
|
|
|
found_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EBUSY, EAGAIN and ETIMEDOUT aren't true errors */
|
|
|
|
if (ret < 0 && ret != -EBUSY && ret != -EAGAIN && ret != -ETIMEDOUT) {
|
|
|
|
errno = ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
return found_count;
|
|
|
|
}
|
|
|
|
|
2021-10-21 11:18:17 +02:00
|
|
|
int modem_socket_poll_prepare(struct modem_socket_config *cfg, struct modem_socket *sock,
|
|
|
|
struct zsock_pollfd *pfd, struct k_poll_event **pev,
|
|
|
|
struct k_poll_event *pev_end)
|
|
|
|
{
|
|
|
|
if (pfd->events & ZSOCK_POLLIN) {
|
|
|
|
if (*pev == pev_end) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_poll_event_init(*pev, K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
|
|
|
|
&sock->sig_data_ready);
|
|
|
|
(*pev)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pfd->events & ZSOCK_POLLOUT) {
|
|
|
|
if (*pev == pev_end) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Not Implemented */
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int modem_socket_poll_update(struct modem_socket *sock, struct zsock_pollfd *pfd,
|
|
|
|
struct k_poll_event **pev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(sock);
|
|
|
|
|
|
|
|
if (pfd->events & ZSOCK_POLLIN) {
|
|
|
|
if ((*pev)->state != K_POLL_STATE_NOT_READY) {
|
|
|
|
pfd->revents |= ZSOCK_POLLIN;
|
|
|
|
}
|
|
|
|
(*pev)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pfd->events & ZSOCK_POLLOUT) {
|
|
|
|
/* Not implemented, but the modem socket is always ready to transmit,
|
|
|
|
* so set the revents
|
|
|
|
*/
|
|
|
|
pfd->revents |= ZSOCK_POLLOUT;
|
|
|
|
(*pev)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:24:12 +02:00
|
|
|
void modem_socket_wait_data(struct modem_socket_config *cfg, struct modem_socket *sock)
|
2020-01-27 21:52:56 +01:00
|
|
|
{
|
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
sock->is_waiting = true;
|
|
|
|
k_sem_give(&cfg->sem_lock);
|
|
|
|
|
|
|
|
k_sem_take(&sock->sem_data_ready, K_FOREVER);
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:24:12 +02:00
|
|
|
void modem_socket_data_ready(struct modem_socket_config *cfg, struct modem_socket *sock)
|
2020-01-27 21:52:56 +01:00
|
|
|
{
|
|
|
|
k_sem_take(&cfg->sem_lock, K_FOREVER);
|
|
|
|
|
|
|
|
if (sock->is_waiting) {
|
|
|
|
/* unblock sockets waiting on recv() */
|
|
|
|
sock->is_waiting = false;
|
|
|
|
k_sem_give(&sock->sem_data_ready);
|
|
|
|
}
|
|
|
|
|
|
|
|
k_sem_give(&cfg->sem_lock);
|
|
|
|
}
|
|
|
|
|
2022-11-21 08:52:44 +01:00
|
|
|
int modem_socket_init(struct modem_socket_config *cfg, struct modem_socket *sockets,
|
|
|
|
size_t sockets_len, int base_socket_id, bool assign_id,
|
|
|
|
const struct socket_op_vtable *vtable)
|
2019-08-07 17:04:00 +02:00
|
|
|
{
|
2022-11-21 08:52:44 +01:00
|
|
|
/* Verify arguments */
|
|
|
|
if (cfg == NULL || sockets == NULL || sockets_len < 1 || vtable == NULL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-08-07 17:04:00 +02:00
|
|
|
|
2022-11-21 08:52:44 +01:00
|
|
|
/* Initialize config */
|
|
|
|
cfg->sockets = sockets;
|
|
|
|
cfg->sockets_len = sockets_len;
|
|
|
|
cfg->base_socket_id = base_socket_id;
|
|
|
|
cfg->assign_id = assign_id;
|
2020-01-27 21:59:33 +01:00
|
|
|
k_sem_init(&cfg->sem_lock, 1, 1);
|
2022-11-21 08:52:44 +01:00
|
|
|
cfg->vtable = vtable;
|
|
|
|
|
|
|
|
/* Initialize associated sockets */
|
|
|
|
for (int i = 0; i < cfg->sockets_len; i++) {
|
|
|
|
/* Clear entire socket structure */
|
|
|
|
memset(&cfg->sockets[i], 0, sizeof(cfg->sockets[i]));
|
|
|
|
|
|
|
|
/* Initialize socket members */
|
2019-08-07 17:04:00 +02:00
|
|
|
k_sem_init(&cfg->sockets[i].sem_data_ready, 0, 1);
|
2021-10-21 11:18:17 +02:00
|
|
|
k_poll_signal_init(&cfg->sockets[i].sig_data_ready);
|
2022-11-21 08:52:44 +01:00
|
|
|
cfg->sockets[i].id = -1;
|
2019-08-07 17:04:00 +02:00
|
|
|
}
|
2022-11-21 08:52:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2019-08-07 17:04:00 +02:00
|
|
|
|
2022-11-21 08:52:44 +01:00
|
|
|
bool modem_socket_is_allocated(const struct modem_socket_config *cfg,
|
|
|
|
const struct modem_socket *sock)
|
|
|
|
{
|
|
|
|
/* Socket is allocated with a reserved id value if id is not dynamically assigned */
|
|
|
|
if (cfg->assign_id == false && sock->id == (cfg->base_socket_id + cfg->sockets_len)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Socket must have been allocated if id is assigned */
|
|
|
|
return modem_socket_id_is_assigned(cfg, sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool modem_socket_id_is_assigned(const struct modem_socket_config *cfg,
|
|
|
|
const struct modem_socket *sock)
|
|
|
|
{
|
|
|
|
/* Verify socket is assigned to a valid value */
|
|
|
|
if ((cfg->base_socket_id <= sock->id) &&
|
|
|
|
(sock->id < (cfg->base_socket_id + cfg->sockets_len))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int modem_socket_id_assign(const struct modem_socket_config *cfg,
|
|
|
|
struct modem_socket *sock, int id)
|
|
|
|
{
|
|
|
|
/* Verify dynamically assigning id is disabled */
|
|
|
|
if (cfg->assign_id) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify id is currently not assigned */
|
|
|
|
if (modem_socket_id_is_assigned(cfg, sock)) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify id is valid */
|
|
|
|
if (id < cfg->base_socket_id || (cfg->base_socket_id + cfg->sockets_len) <= id) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-12-04 10:04:07 +01:00
|
|
|
|
2022-11-21 08:52:44 +01:00
|
|
|
/* Assign id */
|
|
|
|
sock->id = id;
|
2019-08-07 17:04:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|