iot_file.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include "sdk_config.h"
  41. #include "iot_file.h"
  42. #include "iot_common.h"
  43. /**
  44. * @defgroup api_param_check API Parameters check macros.
  45. *
  46. * @details Macros that verify parameters passed to the module in the APIs. These macros
  47. * could be mapped to nothing in final versions of code to save execution and size.
  48. * IOT_FILE_DISABLE_API_PARAM_CHECK should be set to 0 to enable these checks.
  49. *
  50. * @{
  51. */
  52. #if (IOT_FILE_DISABLE_API_PARAM_CHECK == 0)
  53. /**@brief Verify NULL parameters are not passed to API by application. */
  54. #define NULL_PARAM_CHECK(PARAM) \
  55. if ((PARAM) == NULL) \
  56. { \
  57. return (NRF_ERROR_NULL | IOT_FILE_ERR_BASE); \
  58. }
  59. #else // IOT_FILE_DISABLE_API_PARAM_CHECK
  60. #define NULL_PARAM_CHECK(PARAM)
  61. #endif // IOT_FILE_DISABLE_API_PARAM_CHECK
  62. /** @} */
  63. /**
  64. * @brief Function to open IoT file. Depending on port, it should allocate required buffer.
  65. */
  66. uint32_t iot_file_fopen(iot_file_t * p_file, uint32_t requested_size)
  67. {
  68. NULL_PARAM_CHECK(p_file);
  69. if (p_file->open != NULL)
  70. {
  71. return p_file->open(p_file, requested_size);
  72. }
  73. else
  74. {
  75. p_file->buffer_size = requested_size;
  76. return NRF_ERROR_API_NOT_IMPLEMENTED;
  77. }
  78. }
  79. /**
  80. * @brief Function to write data buffer into a file.
  81. */
  82. uint32_t iot_file_fwrite(iot_file_t * p_file, const void * p_data, uint32_t size)
  83. {
  84. NULL_PARAM_CHECK(p_file);
  85. if (p_file->write != NULL)
  86. {
  87. return p_file->write(p_file, p_data, size);
  88. }
  89. else
  90. {
  91. return NRF_ERROR_API_NOT_IMPLEMENTED;
  92. }
  93. }
  94. /**
  95. * @brief Function to read data buffer from file.
  96. */
  97. uint32_t iot_file_fread(iot_file_t * p_file, void * p_data, uint32_t size)
  98. {
  99. NULL_PARAM_CHECK(p_file);
  100. if (p_file->read != NULL)
  101. {
  102. return p_file->read(p_file, p_data, size);
  103. }
  104. else
  105. {
  106. return NRF_ERROR_API_NOT_IMPLEMENTED;
  107. }
  108. }
  109. /**
  110. * @brief Function to read current cursor position.
  111. */
  112. uint32_t iot_file_ftell(iot_file_t * p_file, uint32_t * p_cursor)
  113. {
  114. NULL_PARAM_CHECK(p_file);
  115. if (p_file->tell != NULL)
  116. {
  117. return p_file->tell(p_file, p_cursor);
  118. }
  119. else
  120. {
  121. return NRF_ERROR_API_NOT_IMPLEMENTED;
  122. }
  123. }
  124. /**
  125. * @brief Function to set current cursor position.
  126. */
  127. uint32_t iot_file_fseek(iot_file_t * p_file, uint32_t cursor)
  128. {
  129. NULL_PARAM_CHECK(p_file);
  130. if (p_file->seek != NULL)
  131. {
  132. return p_file->seek(p_file, cursor);
  133. }
  134. else
  135. {
  136. return NRF_ERROR_API_NOT_IMPLEMENTED;
  137. }
  138. }
  139. /**
  140. * @brief Function to rewind file cursor.
  141. */
  142. uint32_t iot_file_frewind(iot_file_t * p_file)
  143. {
  144. NULL_PARAM_CHECK(p_file);
  145. if (p_file->rewind != NULL)
  146. {
  147. return p_file->rewind(p_file);
  148. }
  149. else
  150. {
  151. return NRF_ERROR_API_NOT_IMPLEMENTED;
  152. }
  153. }
  154. /**
  155. * @brief Function to close IoT file. Depending on port, it should free used buffer.
  156. */
  157. uint32_t iot_file_fclose(iot_file_t * p_file)
  158. {
  159. NULL_PARAM_CHECK(p_file);
  160. if (p_file->close != NULL)
  161. {
  162. return p_file->close(p_file);
  163. }
  164. else
  165. {
  166. return NRF_ERROR_API_NOT_IMPLEMENTED;
  167. }
  168. }