udp_api.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2014 - 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 udp_api.h
  41. *
  42. * @defgroup iot_udp UDP Application Interface for Nordic's IPv6 stack
  43. * @ingroup iot_sdk_stack
  44. * @{
  45. * @brief Nordic User Datagram Protocol Application Interface for Nordic's IPv6 stack.
  46. *
  47. * @details This module provides basic features related to User Datagram Protocol (UDP) support.
  48. */
  49. #ifndef UDP_API_H__
  50. #define UDP_API_H__
  51. #include "sdk_config.h"
  52. #include "sdk_common.h"
  53. #include "iot_defines.h"
  54. #include "ipv6_api.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**
  59. * @brief UDP socket reference.
  60. */
  61. typedef struct
  62. {
  63. uint32_t socket_id; /**< UDP socket identifier. */
  64. void * p_app_data; /**< Pointer to application data mapped by the application to the socket. If no mapping is provided by the application using the @ref udp6_socket_app_data_set API, this pointer is NULL. */
  65. } udp6_socket_t;
  66. /**
  67. * @brief UDP data receive callback.
  68. *
  69. * @details API used to notify the application of UDP packets received. If the received data is
  70. * malformed (for example, a checksum error), the packet is still notified to the application.
  71. * The process_result parameter indicates whether the packet was successfully processed by UDP.
  72. * The application should check process_result before
  73. * consuming the packet.
  74. *
  75. * @param[in] p_socket Reference to the socket on which the data is received.
  76. * @param[in] p_ip6_header Pointer to the IP header of the received ICMP packet.
  77. * @param[in] p_udp_header Pointer to the UDP header of the received packet.
  78. * @param[in] process_result Notifies the application if the UDP packet was processed successfully success or
  79. * if an error occurred, for example, the packet was malformed.
  80. * @param[in] p_rx_packet Packet buffer containing the received packed. p_rx_packet->p_payload
  81. * contains the UDP payload.
  82. *
  83. * @returns A provision for the application to notify the module of whether the received packet was
  84. * processed successfully by application. The application may take ownership of the received
  85. * packet by returning IOT_IPV6_ERR_PENDING, in which case the application must take care to
  86. * free it using @ref iot_pbuffer_free.
  87. */
  88. typedef uint32_t (* udp6_handler_t)(const udp6_socket_t * p_socket,
  89. const ipv6_header_t * p_ip_header,
  90. const udp6_header_t * p_udp_header,
  91. uint32_t process_result,
  92. iot_pbuffer_t * p_rx_packet);
  93. /**
  94. * @brief Allocates a UDP socket.
  95. *
  96. * @details This API should be called to be assigned a UDP socket. The maximum number of sockets that can
  97. * be allocated using the API is determined by the define UDP6_MAX_SOCKET_COUNT.
  98. *
  99. * @param[out] p_socket Reference to the allocated socket is provided in the pointer if the procedure
  100. * was successful. Should not be NULL.
  101. *
  102. * @retval NRF_SUCCESS If the socket was allocated successfully. Otherwise, an
  103. * error code that indicates the reason for the failure is returned.
  104. */
  105. uint32_t udp6_socket_allocate(udp6_socket_t * p_socket);
  106. /**
  107. * @brief Frees an allocated UDP socket.
  108. *
  109. * @details API used to free a socket allocated using @ref udp6_socket_allocate.
  110. *
  111. * @param[in] p_socket Handle reference to the socket. Should not be NULL.
  112. *
  113. * @retval NRF_SUCCESS If the socket was freed successfully. Otherwise, an
  114. * error code that indicates the reason for the failure is returned.
  115. *
  116. */
  117. uint32_t udp6_socket_free(const udp6_socket_t * p_socket);
  118. /**
  119. * @brief Registers callback to be notified of data received on a socket.
  120. *
  121. * @details API to register a callback to be notified of data received on a socket.
  122. *
  123. * @param[in] p_socket Handle reference to the socket. Should not be NULL.
  124. * @param[in] callback Callback being registered to receive data. Should not be NULL.
  125. *
  126. * @retval NRF_SUCCESS If the procedure was executed successfully. Otherwise, an
  127. * error code that indicates the reason for the failure is returned.
  128. *
  129. */
  130. uint32_t udp6_socket_recv(const udp6_socket_t * p_socket,
  131. const udp6_handler_t callback);
  132. /**
  133. * @brief Binds a UDP socket to a specific port and address.
  134. *
  135. * @details API used to bind a UDP socket to a local port and an address.
  136. *
  137. * @param[in] p_socket Handle reference to the socket. Should not be NULL.
  138. * @param[in] p_src_addr Local IPv6 address to be bound on specific socket.
  139. * @param[in] src_port Local UDP port to be bound on specific socket.
  140. *
  141. * @retval NRF_SUCCESS If the procedure was executed successfully. Otherwise, an
  142. * error code that indicates the reason for the failure is returned.
  143. *
  144. */
  145. uint32_t udp6_socket_bind(const udp6_socket_t * p_socket,
  146. const ipv6_addr_t * p_src_addr,
  147. uint16_t src_port);
  148. /**
  149. * @brief Connects a UDP socket to aspecific port and address.
  150. *
  151. * @details API used to connect a UDP socket to a remote port and remote address.
  152. *
  153. * @param[in] p_socket Handle reference to the socket. Should not be NULL.
  154. * @param[in] p_dest_addr IPv6 address of the remote destination.
  155. * @param[in] dest_port Remote USP port to connect the socket to.
  156. *
  157. * @retval NRF_SUCCESS If the connection was established successfully.
  158. */
  159. uint32_t udp6_socket_connect(const udp6_socket_t * p_socket,
  160. const ipv6_addr_t * p_dest_addr,
  161. uint16_t dest_port);
  162. /**
  163. * @brief Sends a UDP packet on a specific socket.
  164. *
  165. * @details API used to send UDP data over a UDP socket. Remote port and address must be set with
  166. * \ref udp6_socket_connect() before using this API.
  167. *
  168. * Applications that call this function should allocate a packet with type
  169. * UDP6_PACKET_TYPE (set in the allocation
  170. * parameter) before calling the function.
  171. *
  172. * The application shall not free the allocated packet buffer if the procedure was
  173. * successful, to ensure that no data copies are needed when transmitting a packet.
  174. *
  175. * @param[in] p_socket Handle reference to the socket. Should not be NULL.
  176. * @param[in] p_packet Data to be transmitted on the socket. The application should allocate a packet
  177. * buffer with type UDP6_PACKET_TYPE using \ref iot_pbuffer_allocate.
  178. * p_packet->p_payload and p_packet->length should be appropriately
  179. * populated by the application to contain the payload and length of the UDP
  180. * packet, respectively.
  181. *
  182. * @retval NRF_SUCCESS If the procedure was executed successfully. Otherwise, an
  183. * error code that indicates the reason for the failure is returned.
  184. *
  185. */
  186. uint32_t udp6_socket_send(const udp6_socket_t * p_socket,
  187. iot_pbuffer_t * p_packet);
  188. /**
  189. * @brief Sends a UDP packet on a specific socket to a remote address and port.
  190. *
  191. * @details API used to send UDP data over a UDP socket.
  192. *
  193. * @param[in] p_socket Handle reference to the socket. Should not be NULL.
  194. * @param[in] p_dest_addr IPv6 address of the remote destination.
  195. * @param[in] dest_port Remote UDP port to which data transmission is requested.
  196. * @param[in] p_packet Data to be transmitted on the socket. Application should allocate a
  197. * packet buffer with type UDP6_PACKET_TYPE using \ref
  198. * iot_pbuffer_allocate. p_packet->p_payload and p_packet->length should
  199. * be appropriately populated by the application to contain the payload and
  200. * length of the UDP packet, respectively.
  201. *
  202. * @retval NRF_SUCCESS If the procedure was executed successfully. Otherwise, an
  203. * error code that indicates the reason for the failure is returned.
  204. *
  205. */
  206. uint32_t udp6_socket_sendto(const udp6_socket_t * p_socket,
  207. const ipv6_addr_t * p_dest_addr,
  208. uint16_t dest_port,
  209. iot_pbuffer_t * p_packet);
  210. /**
  211. * @brief Sets application data for a socket.
  212. *
  213. * @details A utility API that allows the application to set any application specific mapping with the
  214. * UDP Socket. The UDP module remembers the pointer provided by the application as long as the
  215. * socket is not freed and if receive data callback is called each time as part of
  216. * udp_socket_t.
  217. *
  218. * @param[in] p_socket Pointer to the socket for which the application data mapping is being set.
  219. * A pointer to the application data should be provided by setting the
  220. * p_socket->p_app_data field.
  221. *
  222. * @retval NRF_SUCCESS If the procedure was executed successfully. Otherwise, an
  223. * error code that indicates the reason for the failure is returned.
  224. *
  225. */
  226. uint32_t udp6_socket_app_data_set(const udp6_socket_t * p_socket);
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif //UDP_API_H__
  231. /**@} */