iot_tftp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Copyright (c) 2015 - 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 iot_tftp.h
  41. *
  42. * @defgroup iot_tftp TFTP Application Interface for Nordic's IPv6 stack
  43. * @ingroup iot_sdk_stack
  44. * @{
  45. * @brief Trivial File Transfer Protocol module provides implementation of TFTP Client.
  46. *
  47. */
  48. #ifndef IOT_TFTP_H__
  49. #define IOT_TFTP_H__
  50. #include "sdk_common.h"
  51. #include "iot_common.h"
  52. #include "iot_timer.h"
  53. #include "iot_file.h"
  54. #include "iot_defines.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**@brief TFTP global instance number. */
  59. typedef uint32_t iot_tftp_t;
  60. /**@brief TFTP module Events. */
  61. typedef enum
  62. {
  63. IOT_TFTP_EVT_ERROR, /**< Event code indicating that an error occurred. */
  64. IOT_TFTP_EVT_TRANSFER_DATA_RECEIVED, /**< Event code indicating that a data packet was received during read transfer. */
  65. IOT_TFTP_EVT_TRANSFER_GET_COMPLETE, /**< Event code indicating that transfer read was completed. */
  66. IOT_TFTP_EVT_TRANSFER_PUT_COMPLETE, /**< Event code indicating that transfer write was completed. */
  67. } iot_tftp_evt_id_t;
  68. /**@brief TFTP error event structure. */
  69. typedef struct
  70. {
  71. uint32_t code; /**< Error code. */
  72. char * p_msg; /**< Message describing the reason or NULL if no description is available. */
  73. uint32_t size_transfered; /**< In case of an error, variable indicates a number of successfully read or write bytes. */
  74. } iot_tftp_evt_err_t;
  75. /**@brief TFTP data received event structure. */
  76. typedef struct
  77. {
  78. uint8_t * p_data; /**< Pointer to received data chunk. */
  79. uint16_t size; /**< Size of received data chunk. */
  80. } iot_tftp_evt_data_received_t;
  81. /**@brief TFTP event structure. */
  82. typedef union
  83. {
  84. iot_tftp_evt_err_t err; /**< Error event structure. Used only in case of IOT_TFTP_EVT_ERROR error. */
  85. iot_tftp_evt_data_received_t data_received; /**< Data received event structure. Used only in case of IOT_TFTP_EVT_TRANSFER_DATA_RECEIVED event. */
  86. } iot_tftp_evt_param_t;
  87. /**@brief Asynchronous event type. */
  88. typedef struct
  89. {
  90. iot_tftp_evt_id_t id; /**< Event code. */
  91. iot_tftp_evt_param_t param; /**< Union to structures describing event. */
  92. iot_file_t * p_file; /**< File associated with TFTP transfer. */
  93. } iot_tftp_evt_t;
  94. /**@brief TFTP Transmission initialization structure (both GET and PUT). */
  95. typedef struct
  96. {
  97. uint32_t next_retr; /**< Number of seconds between retransmissions. */
  98. uint16_t block_size; /**< Maximum or negotiated size of data block. */
  99. } iot_tftp_trans_params_t;
  100. /**@brief User callback from TFTP module.
  101. *
  102. * @note TFTP module user callback will be invoked even if user asks TFTP to abort (TFTP error event).
  103. *
  104. * @param[in] p_tftp Pointer to the TFTP instance.
  105. * @param[in] p_evt Pointer to the TFTP event structure, describing reason.
  106. *
  107. * @retval None.
  108. */
  109. typedef void (*iot_tftp_callback_t)(iot_tftp_t * p_tftp, iot_tftp_evt_t * p_evt);
  110. /**@brief TFTP initialization structure. */
  111. typedef struct
  112. {
  113. ipv6_addr_t * p_ipv6_addr; /**< IPv6 address of the server. */
  114. uint16_t src_port; /**< Source port (local UDP port) from which all request and data will be sent. Should be choosen randomly. */
  115. uint16_t dst_port; /**< Destination port - UDP port on which server listens for new connections. */
  116. iot_tftp_callback_t callback; /**< Reference to the user callback. */
  117. const char * p_password; /**< Server password for all requests. Shall be NULL if no password is required. */
  118. } iot_tftp_init_t;
  119. /**@brief Initializes TFTP client.
  120. *
  121. * @param[in] p_tftp Pointer to the TFTP instance. Should not be NULL.
  122. * @param[in] p_init_params Initialization structure for TFTP client. Should not be NULL.
  123. *
  124. * @retval NRF_SUCCESS on successful execution of procedure, else an error code indicating reason
  125. * for failure.
  126. */
  127. uint32_t iot_tftp_init(iot_tftp_t * p_tftp, iot_tftp_init_t * p_init_params);
  128. /**@brief Function used in order to change initial connection parameters.
  129. *
  130. * @param[in] p_tftp Reference to the TFTP instance.
  131. * @param[in] p_params Pointer to transmission parameters structure. Should not be NULL.
  132. *
  133. * @retval NRF_SUCCESS if parameters successfully set, else an error code indicating reason
  134. * for failure.
  135. */
  136. uint32_t iot_tftp_set_params(iot_tftp_t * p_tftp, iot_tftp_trans_params_t * p_params);
  137. /**@brief Retrieves file from remote server into p_file.
  138. *
  139. * If @p p_file is a NULL pointer, the content of received file can be retrieved by handling
  140. * @ref IOT_TFTP_EVT_TRANSFER_DATA_RECEIVED event. This event is generated each time a data
  141. * packet (containing a chunk of requested file) is received.
  142. * IOT_TFTP_EVT_TRANSFER_GET_COMPLETE event is generated after download is complete.
  143. *
  144. * @note This function should not be called until previous download operation is completed.
  145. *
  146. * @param[in] p_tftp Pointer to the TFTP instance.
  147. * @param[in] p_file Reference to the file from which data should be read.
  148. * @param[in] p_path Path of the requested file on the remote server. Shall not be NULL.
  149. *
  150. * @retval NRF_SUCCESS on successful execution of procedure, else an error code indicating reason
  151. * for failure.
  152. */
  153. uint32_t iot_tftp_get(iot_tftp_t * p_tftp, iot_file_t * p_file, const char * p_path);
  154. /**@brief Sends local file p_file to a remote server.
  155. *
  156. * @param[in] p_tftp Pointer to the TFTP instance.
  157. * @param[in] p_file Reference to the file to which data should be stored. Should not be NULL.
  158. * @param[in] p_path Path of the file on the remote server. Shall not be NULL.
  159. *
  160. * @retval NRF_SUCCESS on successful execution of procedure, else an error code indicating reason
  161. * for failure.
  162. */
  163. uint32_t iot_tftp_put(iot_tftp_t * p_tftp, iot_file_t * p_file, const char * p_path);
  164. /**@brief Holds transmission of ACK (use in order to slow transmission).
  165. *
  166. * @param[in] p_tftp Pointer to the TFTP instance.
  167. *
  168. * @retval NRF_SUCCESS on successful execution of procedure, else an error code indicating reason
  169. * for failure.
  170. */
  171. uint32_t iot_tftp_hold(iot_tftp_t * p_tftp);
  172. /**@brief Resumes transmission.
  173. *
  174. * @param[in] p_tftp Pointer to the TFTP instance.
  175. *
  176. * @retval NRF_SUCCESS on successful execution of procedure, else an error code indicating reason
  177. * for failure.
  178. */
  179. uint32_t iot_tftp_resume(iot_tftp_t * p_tftp);
  180. /**@brief Resets TFTP client instance, so it is possible to make another request after error.
  181. *
  182. * @param[in] p_tftp Pointer to the TFTP instance.
  183. *
  184. * @retval NRF_SUCCESS on successful execution of procedure, else an error code indicating reason
  185. * for failure.
  186. */
  187. uint32_t iot_tftp_abort(iot_tftp_t * p_tftp);
  188. /**@brief Frees assigned sockets.
  189. *
  190. * @param[in] p_tftp Pointer to the TFTP instance.
  191. *
  192. * @retval NRF_SUCCESS on successful execution of procedure, else an error code indicating reason
  193. * for failure.
  194. */
  195. uint32_t iot_tftp_uninit(iot_tftp_t * p_tftp);
  196. /**@brief Function for performing retransmissions of TFTP acknowledgments.
  197. *
  198. * @note TFTP module implements the retransmission mechanism by invoking this function periodically.
  199. * So that method has to be added to IoT Timer client list and has to be called with minimum of
  200. * TFTP_RETRANSMISSION_INTERVAL resolution.
  201. *
  202. * @param[in] wall_clock_value The value of the wall clock that triggered the callback.
  203. *
  204. * @retval None.
  205. */
  206. void iot_tftp_timeout_process(iot_timer_time_in_ms_t wall_clock_value);
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif // IOT_TFTP_H__
  211. /** @} */