2019-08-07 23:04:00 +08:00
|
|
|
/** @file
|
|
|
|
* @brief Modem socket header file.
|
|
|
|
*
|
|
|
|
* Generic modem socket and packet size implementation for modem context
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2020-01-28 04:59:33 +08:00
|
|
|
* Copyright (c) 2019-2020 Foundries.io
|
2019-08-07 23:04:00 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SOCKET_H_
|
|
|
|
#define ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SOCKET_H_
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/net/net_ip.h>
|
|
|
|
#include <zephyr/net/socket.h>
|
2019-12-04 17:04:07 +08:00
|
|
|
|
|
|
|
#include "sockets_internal.h"
|
2019-08-07 23:04:00 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2020-05-30 04:33:12 +08:00
|
|
|
__net_socket struct modem_socket {
|
2019-08-07 23:04:00 +08:00
|
|
|
sa_family_t family;
|
|
|
|
enum net_sock_type type;
|
2021-03-31 15:33:34 +08:00
|
|
|
int ip_proto;
|
2019-08-07 23:04:00 +08:00
|
|
|
struct sockaddr src;
|
|
|
|
struct sockaddr dst;
|
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-09 03:28:07 +08:00
|
|
|
|
|
|
|
/** The number identifying the socket handle inside the modem */
|
2019-08-07 23:04:00 +08:00
|
|
|
int id;
|
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-09 03:28:07 +08:00
|
|
|
|
|
|
|
/** The file descriptor identifying the socket in the fdtable */
|
2019-08-07 23:04:00 +08:00
|
|
|
int sock_fd;
|
|
|
|
|
|
|
|
/** packet data */
|
2020-05-28 00:26:57 +08:00
|
|
|
uint16_t packet_sizes[CONFIG_MODEM_SOCKET_PACKET_COUNT];
|
|
|
|
uint16_t packet_count;
|
2019-08-07 23:04:00 +08:00
|
|
|
|
|
|
|
/** data ready semaphore */
|
|
|
|
struct k_sem sem_data_ready;
|
2021-10-15 15:28:44 +08:00
|
|
|
/** data ready poll signal */
|
2021-10-21 17:18:17 +08:00
|
|
|
struct k_poll_signal sig_data_ready;
|
2019-08-07 23:04:00 +08:00
|
|
|
|
|
|
|
/** socket state */
|
2020-01-28 05:14:49 +08:00
|
|
|
bool is_connected;
|
2020-01-28 04:52:56 +08:00
|
|
|
bool is_waiting;
|
2019-08-07 23:04:00 +08:00
|
|
|
|
|
|
|
/** temporary socket data */
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct modem_socket_config {
|
|
|
|
struct modem_socket *sockets;
|
|
|
|
size_t sockets_len;
|
|
|
|
|
|
|
|
/* beginning socket id (modems can set this to 0 or 1 as needed) */
|
2022-11-21 15:52:44 +08:00
|
|
|
int base_socket_id;
|
|
|
|
|
|
|
|
/* dynamically assign id when modem socket is allocated */
|
|
|
|
bool assign_id;
|
|
|
|
|
2020-01-28 04:59:33 +08:00
|
|
|
struct k_sem sem_lock;
|
2019-12-04 17:04:07 +08:00
|
|
|
|
|
|
|
const struct socket_op_vtable *vtable;
|
2019-08-07 23:04:00 +08:00
|
|
|
};
|
|
|
|
|
2020-01-28 05:13:01 +08:00
|
|
|
/* return size of the first packet */
|
2021-10-19 15:24:12 +08:00
|
|
|
uint16_t modem_socket_next_packet_size(struct modem_socket_config *cfg, struct modem_socket *sock);
|
|
|
|
int modem_socket_packet_size_update(struct modem_socket_config *cfg, struct modem_socket *sock,
|
|
|
|
int new_total);
|
|
|
|
int modem_socket_get(struct modem_socket_config *cfg, int family, int type, int proto);
|
|
|
|
struct modem_socket *modem_socket_from_fd(struct modem_socket_config *cfg, int sock_fd);
|
|
|
|
struct modem_socket *modem_socket_from_id(struct modem_socket_config *cfg, int id);
|
2019-08-07 23:04:00 +08:00
|
|
|
struct modem_socket *modem_socket_from_newid(struct modem_socket_config *cfg);
|
|
|
|
void modem_socket_put(struct modem_socket_config *cfg, int sock_fd);
|
2021-10-19 15:24:12 +08:00
|
|
|
int modem_socket_poll(struct modem_socket_config *cfg, struct zsock_pollfd *fds, int nfds,
|
|
|
|
int msecs);
|
2021-10-21 17:18:17 +08:00
|
|
|
int modem_socket_poll_update(struct modem_socket *sock, struct zsock_pollfd *pfd,
|
|
|
|
struct k_poll_event **pev);
|
|
|
|
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);
|
2021-10-19 15:24:12 +08:00
|
|
|
void modem_socket_wait_data(struct modem_socket_config *cfg, struct modem_socket *sock);
|
|
|
|
void modem_socket_data_ready(struct modem_socket_config *cfg, struct modem_socket *sock);
|
2022-11-21 15:52:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize modem socket config struct and associated modem sockets
|
|
|
|
*
|
|
|
|
* @param cfg The config to initialize
|
|
|
|
* @param sockets The array of sockets associated with the modem socket config
|
|
|
|
* @param sockets_len The length of the array of sockets associated with the modem socket config
|
|
|
|
* @param base_socket_id The lowest socket id supported by the modem
|
|
|
|
* @param assign_id Dynamically assign modem socket id when allocated using modem_socket_get()
|
|
|
|
* @param vtable Socket API implementation used by this config and associated sockets
|
|
|
|
*
|
|
|
|
* @return -EINVAL if any argument is invalid
|
|
|
|
* @return 0 if successful
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if modem socket has been allocated
|
|
|
|
*
|
|
|
|
* @details A modem socket is allocated after a successful invocation of modem_socket_get, and
|
|
|
|
* released after a successful invocation of modem_socket_put.
|
|
|
|
*
|
|
|
|
* @note If socket id is automatically assigned, the socket id will be a value between
|
|
|
|
* base_socket_id and (base_socket_id + socket_len).
|
|
|
|
* Otherwise, the socket id will be assigned to (base_socket_id + socket_len) when allocated.
|
|
|
|
*
|
|
|
|
* @param cfg The modem socket config which the modem socket belongs to
|
|
|
|
* @param sock The modem socket which is checked
|
|
|
|
*
|
|
|
|
* @return true if the socket has been allocated
|
|
|
|
* @return false if the socket has not been allocated
|
|
|
|
*/
|
|
|
|
bool modem_socket_is_allocated(const struct modem_socket_config *cfg,
|
|
|
|
const struct modem_socket *sock);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if modem socket id has been assigned
|
|
|
|
*
|
|
|
|
* @note An assigned modem socket will have an id between base_socket_id and
|
|
|
|
* (base_socket_id + socket_len).
|
|
|
|
*
|
|
|
|
* @param cfg The modem socket config which the modem socket belongs to
|
|
|
|
* @param sock The modem socket for which the id is checked
|
|
|
|
*
|
|
|
|
* @return true if the socket id is been assigned
|
|
|
|
* @return false if the socket has not been assigned
|
|
|
|
*/
|
|
|
|
bool modem_socket_id_is_assigned(const struct modem_socket_config *cfg,
|
|
|
|
const struct modem_socket *sock);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Assign id to modem socket
|
|
|
|
*
|
|
|
|
* @param cfg The modem socket config which the modem socket belongs to
|
|
|
|
* @param sock The modem socket for which the id will be assigned
|
|
|
|
* @param id The id to assign to the modem socket
|
|
|
|
*
|
|
|
|
* @return -EPERM if id has been assigned previously
|
|
|
|
* @return -EINVAL if id is invalid
|
|
|
|
* @return 0 if successful
|
|
|
|
*/
|
|
|
|
int modem_socket_id_assign(const struct modem_socket_config *cfg,
|
|
|
|
struct modem_socket *sock,
|
|
|
|
int id);
|
2019-08-07 23:04:00 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SOCKET_H_ */
|