transport_if.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file transport_if.h
  41. *
  42. * @defgroup iot_socket_transport_if Transport implementation interface
  43. * @ingroup iot_sdk_socket
  44. * @{
  45. * @brief Transport implementation interface.
  46. *
  47. * The transport interface defines the hook to be used for socket transport. The implementation of
  48. * this interface would be different across platforms and event IP stacks (i.e. Nordic IPv6 vs LwIP).
  49. */
  50. #ifndef TRANSPORT_IF_H__
  51. #define TRANSPORT_IF_H__
  52. #include "iot_defines.h"
  53. #include "socket_api.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * @brief Function for opening a socket.
  59. */
  60. typedef uint32_t (*tr_open_t) (socket_t * p_socket);
  61. /**
  62. * @brief Function for connecting a socket to an address.
  63. */
  64. typedef uint32_t (*tr_connect_t)(socket_t * p_socket, const void * p_addr, socklen_t addrlen);
  65. /**
  66. * @brief Function for sending data on a socket.
  67. */
  68. typedef uint32_t (*tr_send_t)(socket_t * p_socket,
  69. const void * p_buf,
  70. uint32_t len,
  71. int flags,
  72. const void * p_destaddr,
  73. socklen_t destaddr_len);
  74. /**
  75. * @brief Function for receiving data from a socket.
  76. */
  77. typedef uint32_t (*tr_recv_t)(socket_t * p_socket,
  78. void * p_buf,
  79. uint32_t * sz,
  80. int flags,
  81. void * p_srcaddr,
  82. socklen_t * p_srcaddr_len);
  83. /**
  84. * @brief Function for binding a socket to an address and port.
  85. */
  86. typedef uint32_t (*tr_bind_t)(socket_t * p_socket, const void * p_addr, socklen_t addrlen);
  87. /**
  88. * @brief Function for listening on a socket.
  89. */
  90. typedef uint32_t (*tr_listen_t)(socket_t * p_socket, int backlog);
  91. /**
  92. * @brief Function for accepting a connection on a socket.
  93. */
  94. typedef uint32_t (*tr_accept_t)(socket_t * p_socket,
  95. socket_t * p_client,
  96. void * p_client_addr,
  97. socklen_t * p_client_addr_len);
  98. /**
  99. * @brief Function for closing a socket.
  100. */
  101. typedef uint32_t (*tr_close_t)(socket_t * p_socket);
  102. /**
  103. * @brief Function for setting options on a socket.
  104. */
  105. typedef uint32_t (*tr_setsockopt_t)(socket_t * p_socket,
  106. int level,
  107. int optname,
  108. const void * p_optval,
  109. socklen_t optlen);
  110. /**
  111. * @brief Function for getting options from a socket.
  112. */
  113. typedef uint32_t (*tr_getsockopt_t)(socket_t * p_socket,
  114. int level,
  115. int optname,
  116. void * p_optval,
  117. socklen_t * p_optlen);
  118. /**
  119. * @brief The transport interface.
  120. */
  121. typedef struct socket_transport
  122. {
  123. tr_open_t open; /**< Open a socket. */
  124. tr_connect_t connect; /**< Connect a socket to an address. */
  125. tr_send_t send; /**< Send data on a socket. */
  126. tr_recv_t recv; /**< Receive data from a socket. */
  127. tr_bind_t bind; /**< Bind a socket to an address and port. */
  128. tr_listen_t listen; /**< Listen on a socket. */
  129. tr_accept_t accept; /**< Accept connection on a socket. */
  130. tr_close_t close; /**< Close a socket. */
  131. tr_setsockopt_t setsockopt; /**< Set options on a socket. */
  132. tr_getsockopt_t getsockopt; /**< Get options from a socket. */
  133. } socket_transport_t;
  134. #if SOCKET_TRANSPORT_ENABLE == 1
  135. extern socket_transport_t transport_impl;
  136. void transport_handler_init(void);
  137. void transport_interface_up(void);
  138. void transport_interface_down(void);
  139. #endif
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif // TRANSPORT_IF_H__
  144. /**@} */