sal.h 3.6 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13 14 15
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-05-17     ChenYong     First version
 */

#ifndef SAL_H__
#define SAL_H__

#include <rtdevice.h>

16 17 18 19
#ifdef SAL_USING_POSIX
#include <dfs_file.h>
#endif

20 21 22 23
#ifdef __cplusplus
extern "C" {
#endif

24 25 26 27
#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
typedef uint32_t socklen_t;
#endif

28
/* SAL socket magic word */
29 30 31
#define SAL_SOCKET_MAGIC               0x5A10

/* The maximum number of sockets structure */
32
#ifndef SAL_SOCKETS_NUM
33
#define SAL_SOCKETS_NUM                DFS_FD_MAX
34
#endif
35 36 37 38 39 40

/* The maximum number of protocol families */
#ifndef SAL_PROTO_FAMILIES_NUM
#define SAL_PROTO_FAMILIES_NUM         4
#endif

41
/* SAL socket offset */
42 43 44 45
#ifndef SAL_SOCKET_OFFSET
#define SAL_SOCKET_OFFSET              0
#endif

46
struct sal_socket_ops
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
    int (*socket)     (int domain, int type, int protocol);
    int (*closesocket)(int s);
    int (*bind)       (int s, const struct sockaddr *name, socklen_t namelen);
    int (*listen)     (int s, int backlog);
    int (*connect)    (int s, const struct sockaddr *name, socklen_t namelen);
    int (*accept)     (int s, struct sockaddr *addr, socklen_t *addrlen);
    int (*sendto)     (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
    int (*recvfrom)   (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
    int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
    int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
    int (*shutdown)   (int s, int how);
    int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
    int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
    int (*ioctlsocket)(int s, long cmd, void *arg);
62
#ifdef SAL_USING_POSIX
63
    int (*poll)       (struct dfs_fd *file, struct rt_pollreq *req);
64
#endif
65 66
};

67 68 69 70 71 72 73 74
struct sal_proto_ops
{
    struct hostent* (*gethostbyname)  (const char *name);
    int             (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
    int             (*getaddrinfo)    (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
    void            (*freeaddrinfo)   (struct addrinfo *ai);
};

75 76
struct sal_socket
{
77
    uint32_t magic;                    /* SAL socket magic word */
78

79
    int socket;                        /* SAL socket descriptor */
80 81 82 83
    int domain;
    int type;
    int protocol;

84 85 86 87 88 89
    const struct sal_socket_ops *ops;  /* socket options */

    void *user_data;                   /* user-specific data */
#ifdef SAL_USING_TLS
    void *user_data_tls;               /* user-specific TLS data */
#endif
90 91
};

92
struct sal_proto_family
93
{
94 95
    int family;                        /* primary protocol families type */
    int sec_family;                    /* secondary protocol families type */
96
    int (*create)(struct sal_socket *sal_socket, int type, int protocol);   /* register socket options */
97

98
    struct sal_proto_ops *ops;             /* protocol family options */
99 100
};

101
/* SAL(Socket Abstraction Layer) initialize */
102 103 104 105
int sal_init(void);

struct sal_socket *sal_get_socket(int sock);

106 107 108 109 110
/* SAL protocol family register and unregister operate */
int sal_proto_family_register(const struct sal_proto_family *pf);
int sal_proto_family_unregister(int family);
rt_bool_t sal_proto_family_is_registered(int family);
struct sal_proto_family *sal_proto_family_find(int family);
111

112 113 114 115
#ifdef __cplusplus
}
#endif

116
#endif /* SAL_H__ */