mqtt_transport.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 2016 - 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 mqtt_transport.h
  41. *
  42. * @brief Internal functions to handle transport in MQTT module.
  43. */
  44. #ifndef MQTT_TRANSPORT_H_
  45. #define MQTT_TRANSPORT_H_
  46. #include "mqtt.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**@brief Transport for handling transport connect procedure. */
  51. typedef uint32_t (*transport_connect_handler_t)(mqtt_client_t * p_client);
  52. /**@brief Transport write handler. */
  53. typedef uint32_t (*transport_write_handler_t)(mqtt_client_t * p_client, uint8_t const * data, uint32_t datalen);
  54. /**@brief Transport read handler. */
  55. typedef uint32_t (*transport_read_handler_t)(mqtt_client_t * p_client, uint8_t * data, uint32_t datalen);
  56. /**@brief Transport disconnect handler. */
  57. typedef uint32_t (*transport_disconnect_handler_t)(mqtt_client_t * p_client);
  58. /**@brief Transport procedure handlers. */
  59. typedef struct
  60. {
  61. transport_connect_handler_t connect; /**< Transport connect handler. Handles TCP connection callback based on type of transport.*/
  62. transport_write_handler_t write; /**< Transport write handler. Handles transport write based on type of transport. */
  63. transport_read_handler_t read; /**< Transport read handler. Handles transport read based on type of transport. */
  64. transport_disconnect_handler_t disconnect; /**< Transport disconnect handler. Handles transport disconnection based on type of transport. */
  65. } transport_procedure_t;
  66. /**@brief Handles TCP Connection Complete for configured transport.
  67. *
  68. * @param[in] p_client Identifies the client on which the procedure is requested.
  69. *
  70. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  71. */
  72. uint32_t mqtt_transport_connect(mqtt_client_t * p_client);
  73. /**@brief Handles write requests on configured transport.
  74. *
  75. * @param[in] p_client Identifies the client on which the procedure is requested.
  76. * @param[in] p_data Data to be written on the transport.
  77. * @param[in] datalen Length of data to be written on the transport.
  78. *
  79. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  80. */
  81. uint32_t mqtt_transport_write(mqtt_client_t * p_client, uint8_t const * p_data, uint32_t datalen);
  82. /**@brief Handles read requests on configured transport.
  83. *
  84. * @param[in] p_client Identifies the client on which the procedure is requested.
  85. * @param[in] p_data Pointer where read data is to be fetched.
  86. * @param[in] datalen Size of memory provided for the operation.
  87. *
  88. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  89. */
  90. uint32_t mqtt_transport_read(mqtt_client_t * p_client, uint8_t * p_data, uint32_t datalen);
  91. /**@brief Handles transport disconnection requests on configured transport.
  92. *
  93. * @param[in] p_client Identifies the client on which the procedure is requested.
  94. *
  95. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  96. */
  97. uint32_t mqtt_transport_disconnect(mqtt_client_t * p_client);
  98. /**@brief Initiates TCP Connection.
  99. *
  100. * @param[in] p_client Identifies the client on which the procedure is requested.
  101. *
  102. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  103. */
  104. uint32_t tcp_request_connection(mqtt_client_t * p_client);
  105. /**
  106. * @brief Wait for an incoming MQTT packet.
  107. * The registered callback will be called with the packet payload.
  108. *
  109. * @param[in] p_client Client instance for which the procedure is requested.
  110. * Shall not be NULL.
  111. * @param[in] timeout Maximum interval (in milliseconds) to wait for a packet.
  112. * If timeout is 0, the interval is indefinitely.
  113. *
  114. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  115. */
  116. uint32_t tcp_receive_packet(mqtt_client_t * p_client, uint32_t timeout);
  117. /**@brief Handles TCP Connection Complete for TCP(non-secure) transport.
  118. *
  119. * @param[in] p_client Identifies the client on which the procedure is requested.
  120. *
  121. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  122. */
  123. uint32_t mqtt_client_tcp_connect(mqtt_client_t * p_client);
  124. /**@brief Handles write requests on TCP(non-secure) transport.
  125. *
  126. * @param[in] p_client Identifies the client on which the procedure is requested.
  127. * @param[in] p_data Data to be written on the transport.
  128. * @param[in] datalen Length of data to be written on the transport.
  129. *
  130. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  131. */
  132. uint32_t mqtt_client_tcp_write(mqtt_client_t * p_client, uint8_t const * p_data, uint32_t datalen);
  133. /**@brief Handles read requests on TCP(non-secure) transport.
  134. *
  135. * @param[in] p_client Identifies the client on which the procedure is requested.
  136. * @param[in] p_data Pointer where read data is to be fetched.
  137. * @param[in] datalen Size of memory provided for the operation.
  138. *
  139. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  140. */
  141. uint32_t mqtt_client_tcp_read(mqtt_client_t * p_client, uint8_t * p_data, uint32_t datalen);
  142. /**@brief Handles transport disconnection requests on TCP(non-secure) transport.
  143. *
  144. * @param[in] p_client Identifies the client on which the procedure is requested.
  145. *
  146. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  147. */
  148. uint32_t mqtt_client_tcp_disconnect(mqtt_client_t * p_client);
  149. /**@brief Handles read requests on TLS(secure) transport.
  150. *
  151. * @param[in] p_client Identifies the client on which the procedure is requested.
  152. * @param[in] p_data Pointer where read data is to be fetched.
  153. * @param[in] datalen Size of memory provided for the operation.
  154. *
  155. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  156. */
  157. uint32_t mqtt_client_tls_connect(mqtt_client_t * p_client);
  158. /**@brief Handles write requests on TLS(secure) transport.
  159. *
  160. * @param[in] p_client Identifies the client on which the procedure is requested.
  161. * @param[in] p_data Data to be written on the transport.
  162. * @param[in] datalen Length of data to be written on the transport.
  163. *
  164. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  165. */
  166. uint32_t mqtt_client_tls_write(mqtt_client_t * p_client, uint8_t const * p_data, uint32_t datalen);
  167. /**@brief Handles read requests on TLS(secure) transport.
  168. *
  169. * @param[in] p_client Identifies the client on which the procedure is requested.
  170. * @param[in] p_data Pointer where read data is to be fetched.
  171. * @param[in] datalen Size of memory provided for the operation.
  172. *
  173. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  174. */
  175. uint32_t mqtt_client_tls_read(mqtt_client_t * p_client, uint8_t * p_data, uint32_t datalen);
  176. /**@brief Handles transport disconnection requests on TLS(secure) transport.
  177. *
  178. * @param[in] p_client Identifies the client on which the procedure is requested.
  179. *
  180. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  181. */
  182. uint32_t mqtt_client_tls_disconnect(mqtt_client_t * p_client);
  183. /**@brief Aborts TCP connection.
  184. *
  185. * @param[in] p_client Identifies the client on which the procedure is requested.
  186. *
  187. * @retval NRF_SUCCESS or an error code indicating reason for failure.
  188. */
  189. void mqtt_client_tcp_abort(mqtt_client_t * p_client);
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. #endif // MQTT_TRANSPORT_H_