1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-05-17     ChenYong     First version
9  * 2022-05-15     Meco Man     rename sal.h as sal_low_lvl.h to avoid conflicts
10  *                             with Microsoft Visual Studio header file
11  */
12 
13 #ifndef SAL_LOW_LEVEL_H__
14 #define SAL_LOW_LEVEL_H__
15 
16 #include <rtdevice.h>
17 
18 #ifdef SAL_USING_POSIX
19 #include <dfs_file.h>
20 #endif
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
27 typedef uint32_t socklen_t;
28 #endif
29 
30 /* SAL socket magic word */
31 #define SAL_SOCKET_MAGIC               0x5A10
32 
33 /* The maximum number of sockets structure */
34 #ifndef SAL_SOCKETS_NUM
35 #define SAL_SOCKETS_NUM                DFS_FD_MAX
36 #endif
37 
38 /* The maximum number of protocol families */
39 #ifndef SAL_PROTO_FAMILIES_NUM
40 #define SAL_PROTO_FAMILIES_NUM         4
41 #endif
42 
43 /* SAL socket offset */
44 #ifndef SAL_SOCKET_OFFSET
45 #define SAL_SOCKET_OFFSET              0
46 #endif
47 
48 struct sockaddr;
49 struct msghdr;
50 struct addrinfo;
51 struct sal_socket
52 {
53     uint32_t magic;                    /* SAL socket magic word */
54 
55     int socket;                        /* SAL socket descriptor */
56     int domain;
57     int type;
58     int protocol;
59 
60     struct netdev *netdev;             /* SAL network interface device */
61 
62     void *user_data;                   /* user-specific data */
63 #ifdef SAL_USING_TLS
64     void *user_data_tls;               /* user-specific TLS data */
65 #endif
66 };
67 
68 /* network interface socket opreations */
69 struct sal_socket_ops
70 {
71     int (*socket)     (int domain, int type, int protocol);
72     int (*closesocket)(int s);
73     int (*bind)       (int s, const struct sockaddr *name, socklen_t namelen);
74     int (*listen)     (int s, int backlog);
75     int (*connect)    (int s, const struct sockaddr *name, socklen_t namelen);
76     int (*accept)     (int s, struct sockaddr *addr, socklen_t *addrlen);
77     int (*sendto)     (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
78     int (*sendmsg)    (int s, const struct msghdr *message, int flags);
79     int (*recvmsg)    (int s, struct msghdr *message, int flags);
80     int (*recvfrom)   (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
81     int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
82     int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
83     int (*shutdown)   (int s, int how);
84     int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
85     int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
86     int (*ioctlsocket)(int s, long cmd, void *arg);
87     int (*socketpair) (int s, int type, int protocol, int *fds);
88 #ifdef SAL_USING_POSIX
89     int (*poll)       (struct dfs_file *file, struct rt_pollreq *req);
90 #endif
91 };
92 
93 /* sal network database name resolving */
94 struct sal_netdb_ops
95 {
96     struct hostent* (*gethostbyname)  (const char *name);
97     int             (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
98     int             (*getaddrinfo)    (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
99     void            (*freeaddrinfo)   (struct addrinfo *ai);
100 };
101 
102 struct sal_proto_family
103 {
104     int family;                                  /* primary protocol families type */
105     int sec_family;                              /* secondary protocol families type */
106     const struct sal_socket_ops *skt_ops;        /* socket opreations */
107     const struct sal_netdb_ops *netdb_ops;       /* network database opreations */
108 };
109 
110 /* SAL(Socket Abstraction Layer) initialize */
111 int sal_init(void);
112 /* Get SAL socket object by socket descriptor */
113 struct sal_socket *sal_get_socket(int sock);
114 
115 /* check SAL socket netweork interface device internet status */
116 int sal_check_netdev_internet_up(struct netdev *netdev);
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif /* SAL_H__ */
123