coap_transport.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Copyright (c) 2014 - 2019, 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 coap_transport.h
  41. *
  42. * @defgroup iot_sdk_coap_transport CoAP transport abstraction
  43. * @ingroup iot_sdk_coap
  44. * @{
  45. * @brief The transport interface that the CoAP depends on for sending and receiving CoAP messages.
  46. *
  47. * @details While the interface is well defined and should not be altered, the implementation of the
  48. * interface depends on the choice of IP stack. The only exception to this is the
  49. * \ref coap_transport_read API. This API is implemented in the CoAP, and the transport layer is
  50. * expected to call this function when data is received on one of the CoAP ports.
  51. */
  52. #ifndef COAP_TRANSPORT_H__
  53. #define COAP_TRANSPORT_H__
  54. #include <stdint.h>
  55. #include <nrf_tls.h>
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /**@brief Port identification information. */
  60. typedef struct
  61. {
  62. uint16_t port_number; /**< Port number. */
  63. } coap_port_t;
  64. /**@brief Remote endpoint. */
  65. typedef struct
  66. {
  67. uint8_t addr[16]; /**< Address of the remote device. */
  68. uint16_t port_number; /**< Remote port number. */
  69. } coap_remote_t;
  70. /**@brief Transport initialization information. */
  71. typedef struct
  72. {
  73. coap_port_t * p_port_table; /**< Information about the ports being registered. Count is assumed to be COAP_PORT_COUNT. */
  74. void * p_arg; /**< Public. Miscellaneous pointer to application provided data that should be passed to the transport. */
  75. } coap_transport_init_t;
  76. /**@brief Initializes the transport layer to have the data ports set up for CoAP.
  77. *
  78. * @param[in] p_param Port count and port numbers.
  79. *
  80. * @retval NRF_SUCCESS If initialization was successful. Otherwise, an error code that indicates the reason for the failure is returned.
  81. */
  82. uint32_t coap_transport_init (const coap_transport_init_t * p_param);
  83. /**@brief Sends data on a CoAP endpoint or port.
  84. *
  85. * @param[in] p_port Port on which the data is to be sent.
  86. * @param[in] p_remote Remote endpoint to which the data is targeted.
  87. * @param[in] p_data Pointer to the data to be sent.
  88. * @param[in] datalen Length of the data to be sent.
  89. *
  90. * @retval NRF_SUCCESS If the data was sent successfully. Otherwise, an error code that indicates the reason for the failure is returned.
  91. */
  92. uint32_t coap_transport_write(const coap_port_t * p_port,
  93. const coap_remote_t * p_remote,
  94. const uint8_t * p_data,
  95. uint16_t datalen);
  96. /**@brief Handles data received on a CoAP endpoint or port.
  97. *
  98. * This API is not implemented by the transport layer, but assumed to exist. This approach
  99. * avoids unnecessary registering of callback and remembering it in the transport layer.
  100. *
  101. * @param[in] p_port Port on which the data is received.
  102. * @param[in] p_remote Remote endpoint from which the data is received.
  103. * @param[in] p_local Local endpoint on which the data is received.
  104. * @param[in] result Indicates if the data was processed successfully by lower layers.
  105. * Possible failures could be NRF_SUCCESS,
  106. * UDP_BAD_CHECKSUM,
  107. * UDP_TRUNCATED_PACKET, or
  108. * UDP_MALFORMED_PACKET.
  109. * @param[in] p_data Pointer to the data received.
  110. * @param[in] datalen Length of the data received.
  111. *
  112. * @retval NRF_SUCCESS If the data was handled successfully. Otherwise, an error code that indicates the reason for the failure is returned.
  113. *
  114. */
  115. uint32_t coap_transport_read(const coap_port_t * p_port,
  116. const coap_remote_t * p_remote,
  117. const coap_remote_t * p_local,
  118. uint32_t result,
  119. const uint8_t * p_data,
  120. uint16_t datalen);
  121. /**@brief Process loop to handle DTLS processing.
  122. *
  123. * @details The function handles any processing of encrypted packets.
  124. * Some encryption libraries requires to be run in a processing
  125. * loop. This function is called by the CoAP library everytime
  126. * \ref coap_time_tick is issued from the library user. Any other process
  127. * specific routines that should be done regularly could be added in
  128. * this function.
  129. */
  130. void coap_transport_process(void);
  131. /**@brief Process loop when using coap BSD socket transport implementation.
  132. *
  133. * @details This is blocking call. The function unblock is only
  134. * triggered upon an socket event registered to select() by coap transport.
  135. * This function must be called as often as possible in order to dispatch incomming
  136. * socket events. Preferred to be put in the application's main loop or similar.
  137. */
  138. void coap_transport_input(void);
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif //COAP_TRANSPORT_H__
  143. /** @} */