sal_low_lvl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef SAL_LOW_LEVEL_H__
  13. #define SAL_LOW_LEVEL_H__
  14. #include <rtdevice.h>
  15. #ifdef SAL_USING_POSIX
  16. #include <dfs_file.h>
  17. #endif
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
  22. typedef uint32_t socklen_t;
  23. #endif
  24. /* SAL socket magic word */
  25. #define SAL_SOCKET_MAGIC 0x5A10
  26. /* The maximum number of sockets structure */
  27. #ifndef SAL_SOCKETS_NUM
  28. #define SAL_SOCKETS_NUM DFS_FD_MAX
  29. #endif
  30. /* The maximum number of protocol families */
  31. #ifndef SAL_PROTO_FAMILIES_NUM
  32. #define SAL_PROTO_FAMILIES_NUM 4
  33. #endif
  34. /* SAL socket offset */
  35. #ifndef SAL_SOCKET_OFFSET
  36. #define SAL_SOCKET_OFFSET 0
  37. #endif
  38. struct sockaddr;
  39. struct msghdr;
  40. struct addrinfo;
  41. struct sal_socket
  42. {
  43. uint32_t magic; /* SAL socket magic word */
  44. int socket; /* SAL socket descriptor */
  45. int domain;
  46. int type;
  47. int protocol;
  48. struct netdev *netdev; /* SAL network interface device */
  49. void *user_data; /* user-specific data */
  50. #ifdef SAL_USING_TLS
  51. void *user_data_tls; /* user-specific TLS data */
  52. #endif
  53. };
  54. /* network interface socket opreations */
  55. struct sal_socket_ops
  56. {
  57. int (*socket) (int domain, int type, int protocol);
  58. int (*closesocket)(int s);
  59. int (*bind) (int s, const struct sockaddr *name, socklen_t namelen);
  60. int (*listen) (int s, int backlog);
  61. int (*connect) (int s, const struct sockaddr *name, socklen_t namelen);
  62. int (*accept) (int s, struct sockaddr *addr, socklen_t *addrlen);
  63. int (*sendto) (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  64. int (*sendmsg) (int s, const struct msghdr *message, int flags);
  65. int (*recvmsg) (int s, struct msghdr *message, int flags);
  66. int (*recvfrom) (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  67. int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
  68. int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
  69. int (*shutdown) (int s, int how);
  70. int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
  71. int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
  72. int (*ioctlsocket)(int s, long cmd, void *arg);
  73. int (*socketpair) (int s, int type, int protocol, int *fds);
  74. #ifdef SAL_USING_POSIX
  75. int (*poll) (struct dfs_file *file, struct rt_pollreq *req);
  76. #endif
  77. };
  78. /* sal network database name resolving */
  79. struct sal_netdb_ops
  80. {
  81. struct hostent* (*gethostbyname) (const char *name);
  82. int (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
  83. int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  84. void (*freeaddrinfo) (struct addrinfo *ai);
  85. };
  86. struct sal_proto_family
  87. {
  88. int family; /* primary protocol families type */
  89. int sec_family; /* secondary protocol families type */
  90. const struct sal_socket_ops *skt_ops; /* socket opreations */
  91. const struct sal_netdb_ops *netdb_ops; /* network database opreations */
  92. };
  93. /* SAL(Socket Abstraction Layer) initialize */
  94. int sal_init(void);
  95. /* Get SAL socket object by socket descriptor */
  96. struct sal_socket *sal_get_socket(int sock);
  97. /* check SAL socket netweork interface device internet status */
  98. int sal_check_netdev_internet_up(struct netdev *netdev);
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif /* SAL_H__ */