1 /** @file
2  * @brief Modem socket header file.
3  *
4  * Generic modem socket and packet size implementation for modem context
5  */
6 
7 /*
8  * Copyright (c) 2019-2020 Foundries.io
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SOCKET_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SOCKET_H_
15 
16 #include <kernel.h>
17 #include <net/net_ip.h>
18 #include <net/socket.h>
19 
20 #include "sockets_internal.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 __net_socket struct modem_socket {
27 	sa_family_t family;
28 	enum net_sock_type type;
29 	int ip_proto;
30 	struct sockaddr src;
31 	struct sockaddr dst;
32 	int id;
33 	int sock_fd;
34 
35 	/** packet data */
36 	uint16_t packet_sizes[CONFIG_MODEM_SOCKET_PACKET_COUNT];
37 	uint16_t packet_count;
38 
39 	/** data ready semaphore */
40 	struct k_sem sem_data_ready;
41 
42 	/** socket state */
43 	bool is_connected;
44 	bool is_waiting;
45 	bool is_polled;
46 
47 	/** temporary socket data */
48 	void *data;
49 };
50 
51 struct modem_socket_config {
52 	struct modem_socket *sockets;
53 	size_t sockets_len;
54 
55 	/* beginning socket id (modems can set this to 0 or 1 as needed) */
56 	int base_socket_num;
57 	struct k_sem sem_poll;
58 	struct k_sem sem_lock;
59 
60 	const struct socket_op_vtable *vtable;
61 };
62 
63 /* return size of the first packet */
64 uint16_t modem_socket_next_packet_size(struct modem_socket_config *cfg,
65 				    struct modem_socket *sock);
66 int modem_socket_packet_size_update(struct modem_socket_config *cfg,
67 				    struct modem_socket *sock, int new_total);
68 int modem_socket_get(struct modem_socket_config *cfg, int family, int type,
69 		     int proto);
70 struct modem_socket *modem_socket_from_fd(struct modem_socket_config *cfg,
71 					  int sock_fd);
72 struct modem_socket *modem_socket_from_id(struct modem_socket_config *cfg,
73 					  int id);
74 struct modem_socket *modem_socket_from_newid(struct modem_socket_config *cfg);
75 void modem_socket_put(struct modem_socket_config *cfg, int sock_fd);
76 int modem_socket_poll(struct modem_socket_config *cfg,
77 		      struct zsock_pollfd *fds, int nfds, int msecs);
78 void modem_socket_wait_data(struct modem_socket_config *cfg,
79 			    struct modem_socket *sock);
80 void modem_socket_data_ready(struct modem_socket_config *cfg,
81 			     struct modem_socket *sock);
82 int modem_socket_init(struct modem_socket_config *cfg,
83 		      const struct socket_op_vtable *vtable);
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif /* ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SOCKET_H_ */
90