sal_low_lvl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 sal_socket
  39. {
  40. uint32_t magic; /* SAL socket magic word */
  41. int socket; /* SAL socket descriptor */
  42. int domain;
  43. int type;
  44. int protocol;
  45. struct netdev *netdev; /* SAL network interface device */
  46. void *user_data; /* user-specific data */
  47. #ifdef SAL_USING_TLS
  48. void *user_data_tls; /* user-specific TLS data */
  49. #endif
  50. };
  51. /* network interface socket opreations */
  52. struct sal_socket_ops
  53. {
  54. int (*socket) (int domain, int type, int protocol);
  55. int (*closesocket)(int s);
  56. int (*bind) (int s, const struct sockaddr *name, socklen_t namelen);
  57. int (*listen) (int s, int backlog);
  58. int (*connect) (int s, const struct sockaddr *name, socklen_t namelen);
  59. int (*accept) (int s, struct sockaddr *addr, socklen_t *addrlen);
  60. int (*sendto) (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
  61. int (*recvfrom) (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
  62. int (*getsockopt) (int s, int level, int optname, void *optval, socklen_t *optlen);
  63. int (*setsockopt) (int s, int level, int optname, const void *optval, socklen_t optlen);
  64. int (*shutdown) (int s, int how);
  65. int (*getpeername)(int s, struct sockaddr *name, socklen_t *namelen);
  66. int (*getsockname)(int s, struct sockaddr *name, socklen_t *namelen);
  67. int (*ioctlsocket)(int s, long cmd, void *arg);
  68. #ifdef SAL_USING_POSIX
  69. int (*poll) (struct dfs_fd *file, struct rt_pollreq *req);
  70. #endif
  71. };
  72. /* sal network database name resolving */
  73. struct sal_netdb_ops
  74. {
  75. struct hostent* (*gethostbyname) (const char *name);
  76. int (*gethostbyname_r)(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop);
  77. int (*getaddrinfo) (const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
  78. void (*freeaddrinfo) (struct addrinfo *ai);
  79. };
  80. struct sal_proto_family
  81. {
  82. int family; /* primary protocol families type */
  83. int sec_family; /* secondary protocol families type */
  84. const struct sal_socket_ops *skt_ops; /* socket opreations */
  85. const struct sal_netdb_ops *netdb_ops; /* network database opreations */
  86. };
  87. /* SAL(Socket Abstraction Layer) initialize */
  88. int sal_init(void);
  89. /* Get SAL socket object by socket descriptor */
  90. struct sal_socket *sal_get_socket(int sock);
  91. /* check SAL socket netweork interface device internet status */
  92. int sal_check_netdev_internet_up(struct netdev *netdev);
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. #endif /* SAL_H__ */