wiced_resource.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 2024, Cypress Semiconductor Corporation (an Infineon company)
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /** @file
  18. * WICED Resource API's
  19. * The Resource Management functions reads resource from a resource location
  20. * and returns the number of bytes from an offset in an caller filled buffer.
  21. *
  22. * Functions to get the resource size and resource data
  23. *
  24. * The Resource could be one of the three locations
  25. *
  26. * - Wiced Filesystem (File System)
  27. * - Internal Memory (Embedded Flash memory)
  28. * - External Storage ( External Flash connected via SPI interface)
  29. *
  30. */
  31. #ifndef INCLUDED_RESOURCE_H_
  32. #define INCLUDED_RESOURCE_H_
  33. #include <stdint.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /******************************************************
  38. * Macros
  39. ******************************************************/
  40. #ifndef MIN
  41. #define MIN(x, y) ( (x) < (y) ? (x) : (y) )
  42. #endif /* ifndef MIN */
  43. /* Suppress unused parameter warning */
  44. #ifndef UNUSED_PARAMETER
  45. #define UNUSED_PARAMETER(x) ( (void)(x) )
  46. #endif
  47. #ifndef RESULT_ENUM
  48. #define RESULT_ENUM(prefix, name, value) prefix ## name = (value)
  49. #endif /* ifndef RESULT_ENUM */
  50. #if defined(CY_SECTION)
  51. #define CY_SECTION_WHD CY_SECTION
  52. #else
  53. #if !defined(CY_SECTION_WHD)
  54. #if defined(__ARMCC_VERSION)
  55. #define CY_SECTION_WHD(name) __attribute__ ( (section(name) ) )
  56. #elif defined (__GNUC__)
  57. #if defined (__clang__)
  58. #define CY_SECTION_WHD(name) __attribute__ ( (section("__DATA, "name) ) )
  59. #else
  60. #define CY_SECTION_WHD(name) __attribute__ ( (section(name) ) )
  61. #endif
  62. #elif defined (__ICCARM__)
  63. #define CY_PRAGMA_WHD(x) _Pragma(#x)
  64. #define CY_SECTION_WHD(name) CY_PRAGMA_WHD(location = name)
  65. #else
  66. #error "An unsupported toolchain"
  67. #endif /* (__ARMCC_VERSION) */
  68. #endif /* !defined(CY_SECTION_WHD) */
  69. #endif /* defined(CY_SECTION) */
  70. /* These Enum result values are for Resource errors
  71. * Values: 4000 - 4999
  72. */
  73. #define RESOURCE_RESULT_LIST(prefix) \
  74. RESULT_ENUM(prefix, SUCCESS, 0), /**< Success */ \
  75. RESULT_ENUM(prefix, UNSUPPORTED, 7), /**< Unsupported function */ \
  76. RESULT_ENUM(prefix, OFFSET_TOO_BIG, 4001), /**< Offset past end of resource */ \
  77. RESULT_ENUM(prefix, FILE_OPEN_FAIL, 4002), /**< Failed to open resource file */ \
  78. RESULT_ENUM(prefix, FILE_SEEK_FAIL, 4003), /**< Failed to seek to requested offset in resource file */ \
  79. RESULT_ENUM(prefix, FILE_READ_FAIL, 4004), /**< Failed to read resource file */
  80. #define resource_get_size(resource) ( (resource)->size )
  81. /******************************************************
  82. * Constants
  83. ******************************************************/
  84. #define RESOURCE_ENUM_OFFSET (1300)
  85. /******************************************************
  86. * Enumerations
  87. ******************************************************/
  88. /**
  89. * Result type for WICED Resource function
  90. */
  91. typedef enum
  92. {
  93. RESOURCE_RESULT_LIST(RESOURCE_)
  94. } resource_result_t;
  95. /******************************************************
  96. * Type Definitions
  97. ******************************************************/
  98. typedef const void *resource_data_t;
  99. typedef unsigned long resource_size_t;
  100. /******************************************************
  101. * Structures
  102. ******************************************************/
  103. /**
  104. * Memory handle
  105. */
  106. typedef struct
  107. {
  108. const char *data; /**< resource data */
  109. } memory_resource_handle_t;
  110. /**
  111. * Filesystem handle
  112. */
  113. typedef struct
  114. {
  115. unsigned long offset; /**< Offset to the start of the resource */
  116. const char *filename; /**< name of the resource */
  117. } filesystem_resource_handle_t;
  118. typedef enum
  119. {
  120. RESOURCE_IN_MEMORY, /**< resource location in memory */
  121. RESOURCE_IN_FILESYSTEM, /**< resource location in filesystem */
  122. RESOURCE_IN_EXTERNAL_STORAGE /**< resource location in external storage */
  123. } resource_location_t;
  124. /**
  125. * Resource handle structure
  126. */
  127. typedef struct
  128. {
  129. resource_location_t location; /**< resource location */
  130. unsigned long size; /**< resource size */
  131. union
  132. {
  133. filesystem_resource_handle_t fs; /** < filesystem resource handle */
  134. memory_resource_handle_t mem; /** < memory resource handle */
  135. void *external_storage_context; /** < external storage context */
  136. } val;
  137. } resource_hnd_t;
  138. /******************************************************
  139. * Global Variables
  140. ******************************************************/
  141. /******************************************************
  142. * Function Declarations
  143. ******************************************************/
  144. /*****************************************************************************/
  145. /** @addtogroup resourceapi Wiced Resource Management API's
  146. * @ingroup framework
  147. *
  148. * WCIED Resource Management API's has functions to get the
  149. * resource size and reads resource data from a resource
  150. * location and returns the number of bytes in an caller
  151. * filled buffer
  152. *
  153. * The Resource could be one of the three locations
  154. *
  155. * - Wiced Filesystem ( File System)
  156. * - Internal Memory (Embedded Flash memory)
  157. * - External Storage ( External Flash connected via SPI interface )
  158. *
  159. * @{
  160. */
  161. /*****************************************************************************/
  162. /** Read resource using the handle specified
  163. *
  164. * @param[in] resource : handle of the resource to read
  165. * @param[in] offset : offset from the beginning of the resource block
  166. * @param[in] maxsize : size of the buffer
  167. * @param[out] size : size of the data successfully read
  168. * @param[in] buffer : pointer to a buffer to contain the read data
  169. *
  170. * @return @ref resource_result_t
  171. */
  172. extern resource_result_t resource_read(const resource_hnd_t *resource, uint32_t offset, uint32_t maxsize,
  173. uint32_t *size, void *buffer);
  174. /** Retrieve a read only resource buffer using the handle specified
  175. *
  176. * @param[in] resource : handle of the resource to read
  177. * @param[in] offset : offset from the beginning of the resource block
  178. * @param[in] maxsize : size of the buffer
  179. * @param[out] size : size of the data successfully read
  180. * @param[out] buffer : pointer to a buffer pointer to point to the resource data
  181. *
  182. * @return @ref resource_result_t
  183. */
  184. extern resource_result_t resource_get_readonly_buffer(const resource_hnd_t *resource, uint32_t offset, uint32_t maxsize,
  185. uint32_t *size_out, const void **buffer);
  186. /** Free a read only resource buffer using the handle specified
  187. *
  188. * @param[in] resource : handle of the resource to read
  189. * @param[in] buffer : pointer to a buffer set using resource_get_readonly_buffer
  190. *
  191. * @return @ref resource_result_t
  192. */
  193. extern resource_result_t resource_free_readonly_buffer(const resource_hnd_t *handle, const void *buffer);
  194. /* @} */
  195. #ifdef __cplusplus
  196. } /*extern "C" */
  197. #endif
  198. #endif /* ifndef INCLUDED_RESOURCE_H_ */