|
| 1 | +/***************************************************************************//** |
| 2 | +* \file cyabs_resource.h |
| 3 | +* |
| 4 | +* \brief |
| 5 | +* Basic abstraction layer for dealing with resources. |
| 6 | +* |
| 7 | +******************************************************************************** |
| 8 | +* \copyright |
| 9 | +* Copyright 2018-2019 Cypress Semiconductor Corporation |
| 10 | +* SPDX-License-Identifier: Apache-2.0 |
| 11 | +* |
| 12 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | +* you may not use this file except in compliance with the License. |
| 14 | +* You may obtain a copy of the License at |
| 15 | +* |
| 16 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +* |
| 18 | +* Unless required by applicable law or agreed to in writing, software |
| 19 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +* See the License for the specific language governing permissions and |
| 22 | +* limitations under the License. |
| 23 | +*******************************************************************************/ |
| 24 | + |
| 25 | +/** |
| 26 | +* \addtogroup group_abstraction_resource Resource abstraction |
| 27 | +* \ingroup group_abstraction |
| 28 | +* \{ |
| 29 | +* Basic abstraction layer for dealing with resources. |
| 30 | +* |
| 31 | +* \defgroup group_abstraction_resource_macros Macros |
| 32 | +* \defgroup group_abstraction_resource_enums Enums |
| 33 | +* \defgroup group_abstraction_resource_data_structures Data Structures |
| 34 | +* \defgroup group_abstraction_resource_functions Functions |
| 35 | +*/ |
| 36 | + |
| 37 | +#pragma once |
| 38 | + |
| 39 | +#include <stdint.h> |
| 40 | +#include <stdbool.h> |
| 41 | +#include "cy_result.h" |
| 42 | + |
| 43 | +#if defined(__cplusplus) |
| 44 | +extern "C" { |
| 45 | +#endif |
| 46 | + |
| 47 | +/** |
| 48 | +* \addtogroup group_abstraction_resource_macros |
| 49 | +* \{ |
| 50 | +*/ |
| 51 | + |
| 52 | +/** Error code for when the specified resource operation is not valid. */ |
| 53 | +#define CY_RSLT_RSC_ERROR_UNSUPPORTED (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 0)) |
| 54 | +/** Error indicating that memory for the specified resource could not be allocated. */ |
| 55 | +#define CY_RSLT_RSC_ERROR_UNAVAILABLE (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 1)) |
| 56 | +/** Error code for when the specified resource offset is not valid for the specified resource. */ |
| 57 | +#define CY_RSLT_RSC_ERROR_OFFSET (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 2)) |
| 58 | +/** Error code for when the specified resource can not be opened for reading. */ |
| 59 | +#define CY_RSLT_RSC_ERROR_OPEN (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 3)) |
| 60 | +/** Error code for when the specified location can not be accessed in the specified resource. */ |
| 61 | +#define CY_RSLT_RSC_ERROR_SEEK (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 4)) |
| 62 | +/** Error code for when the specified resource can not be read. */ |
| 63 | +#define CY_RSLT_RSC_ERROR_READ (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 5)) |
| 64 | + |
| 65 | +/** \} group_abstraction_resource_macros */ |
| 66 | + |
| 67 | + |
| 68 | +/** |
| 69 | +* \addtogroup group_abstraction_resource_enums |
| 70 | +* \{ |
| 71 | +*/ |
| 72 | +/** Different types of memory that the resources can be stored in. */ |
| 73 | +typedef enum |
| 74 | +{ |
| 75 | + CY_RESOURCE_IN_MEMORY, /**< resource location in memory */ |
| 76 | + CY_RESOURCE_IN_FILESYSTEM, /**< resource location in filesystem */ |
| 77 | + CY_RESOURCE_IN_EXTERNAL_STORAGE /**< resource location in external storage */ |
| 78 | +} cy_resource_location_t; |
| 79 | + |
| 80 | +/** \} group_abstraction_resource_enums */ |
| 81 | + |
| 82 | + |
| 83 | +/** |
| 84 | +* \addtogroup group_abstraction_resource_data_structures |
| 85 | +* \{ |
| 86 | +*/ |
| 87 | + |
| 88 | +/** Filesystem handle */ |
| 89 | +typedef struct |
| 90 | +{ |
| 91 | + unsigned long offset; /**< Offset to the start of the resource */ |
| 92 | + const char* filename; /**< name of the resource */ |
| 93 | +} cy_filesystem_resource_handle_t; |
| 94 | + |
| 95 | +/** Resource handle structure */ |
| 96 | +typedef struct |
| 97 | +{ |
| 98 | + cy_resource_location_t location; /**< resource location */ |
| 99 | + unsigned long size; /**< resource size */ |
| 100 | + union |
| 101 | + { |
| 102 | + cy_filesystem_resource_handle_t fs; /** < filesystem resource handle */ |
| 103 | + const uint8_t* mem_data; /** < memory resource handle */ |
| 104 | + void* external_storage_context; /** < external storage context */ |
| 105 | + } val; |
| 106 | +} cy_resource_handle_t; |
| 107 | + |
| 108 | +/** \} group_abstraction_resource_data_structures */ |
| 109 | + |
| 110 | +/** |
| 111 | +* \addtogroup group_abstraction_resource_functions |
| 112 | +* \{ |
| 113 | +*/ |
| 114 | + |
| 115 | +/** |
| 116 | + * \brief return the block size for the resource |
| 117 | + * \param handle handle to a resource |
| 118 | + * \param size pointer to a uint32_t to receive the size |
| 119 | + * \returns CY_RSLT_SUCCESS if the size was found, otherwise an error |
| 120 | + */ |
| 121 | +cy_rslt_t cy_resource_get_block_size(const cy_resource_handle_t *handle, uint32_t *size); |
| 122 | + |
| 123 | +/** |
| 124 | + * \brief return the total size for the resource |
| 125 | + * \param handle handle to a resource |
| 126 | + * \param size pointer to a uint32_t to receive the size |
| 127 | + * \returns CY_RSLT_SUCCESS if the size was found, otherwise an error |
| 128 | + */ |
| 129 | +cy_rslt_t cy_resource_get_total_size(const cy_resource_handle_t *handle, uint32_t *size); |
| 130 | + |
| 131 | +/** |
| 132 | + * \brief return the total number of blocks that exist for the resource |
| 133 | + * \param handle handle to a resource |
| 134 | + * \param blocks pointer to a uint32_t to receive the count |
| 135 | + * \returns CY_RSLT_SUCCESS if the count was found, otherwise an error |
| 136 | + */ |
| 137 | +cy_rslt_t cy_resource_get_block_count(const cy_resource_handle_t *handle, uint32_t *blocks); |
| 138 | + |
| 139 | +/** |
| 140 | + * \brief read data from a resource |
| 141 | + * \param handle the handle to the resource |
| 142 | + * \param blockno the block number of the data from the resource |
| 143 | + * \param buffer pointer to receive buffer address from resource. The buffer must be freed when done. Eg: free(buffer) |
| 144 | + * \param size location to receive the size of the block read |
| 145 | + * \returns CY_RSLT_SUCCESS if data read, otherwise an error |
| 146 | + */ |
| 147 | +cy_rslt_t cy_resource_read(const cy_resource_handle_t *handle, uint32_t blockno, uint8_t **buffer, uint32_t *size); |
| 148 | + |
| 149 | +/** |
| 150 | + * \brief optimized version of read for resources stored in memory (\see CY_RESOURCE_IN_MEMORY) |
| 151 | + * \param handle the handle to the resource |
| 152 | + * \param buffer pointer to receive buffer address from resource. This does NOT need to be freed. |
| 153 | + * \param size location to receive the size of the block read |
| 154 | + * \returns CY_RSLT_SUCCESS if data read, otherwise an error |
| 155 | + */ |
| 156 | +cy_rslt_t cy_resource_readonly_memory(const cy_resource_handle_t *handle, const uint8_t **buffer, uint32_t *size); |
| 157 | + |
| 158 | +/** \} group_abstraction_resource_functions */ |
| 159 | + |
| 160 | +#if defined(__cplusplus) |
| 161 | +} |
| 162 | +#endif |
| 163 | + |
| 164 | +/** \} group_abstraction_resource */ |
0 commit comments