Update abstraction-resource to 1.0.0.22384

pull/14787/head
Dustin Crossman 2021-05-25 15:01:41 -07:00
parent c3714868a3
commit 7a7f206344
4 changed files with 124 additions and 86 deletions

View File

@ -1,26 +1,26 @@
/***************************************************************************//**
* \file cyabs_resource.h
*
* \brief
* Basic abstraction layer for dealing with resources.
*
********************************************************************************
* \copyright
* Copyright 2018-2020 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
/***********************************************************************************************//**
* \file cyabs_resource.h
*
* \brief
* Basic abstraction layer for dealing with resources.
*
***************************************************************************************************
* \copyright
* Copyright 2018-2021 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**************************************************************************************************/
#pragma once
@ -32,25 +32,31 @@
extern "C" {
#endif
/**
* \addtogroup group_abstraction_resource Resource Abstraction
* \{
*
* Basic abstraction layer for dealing with resources.
*/
/**
* \addtogroup group_abstraction_resource Resource Abstraction
* \{
*
* Basic abstraction layer for dealing with resources.
*/
/** Error code for when the specified resource operation is not valid. */
#define CY_RSLT_RSC_ERROR_UNSUPPORTED (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 0))
#define CY_RSLT_RSC_ERROR_UNSUPPORTED \
(CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 0))
/** Error indicating that memory for the specified resource could not be allocated. */
#define CY_RSLT_RSC_ERROR_UNAVAILABLE (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 1))
#define CY_RSLT_RSC_ERROR_UNAVAILABLE \
(CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 1))
/** Error code for when the specified resource offset is not valid for the specified resource. */
#define CY_RSLT_RSC_ERROR_OFFSET (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 2))
#define CY_RSLT_RSC_ERROR_OFFSET \
(CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 2))
/** Error code for when the specified resource can not be opened for reading. */
#define CY_RSLT_RSC_ERROR_OPEN (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 3))
#define CY_RSLT_RSC_ERROR_OPEN \
(CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 3))
/** Error code for when the specified location can not be accessed in the specified resource. */
#define CY_RSLT_RSC_ERROR_SEEK (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 4))
#define CY_RSLT_RSC_ERROR_SEEK \
(CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 4))
/** Error code for when the specified resource can not be read. */
#define CY_RSLT_RSC_ERROR_READ (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 5))
#define CY_RSLT_RSC_ERROR_READ \
(CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_RESOURCE, 5))
/** Different types of memory that the resources can be stored in. */
typedef enum
@ -60,23 +66,28 @@ typedef enum
CY_RESOURCE_IN_EXTERNAL_STORAGE /**< resource location in external storage */
} cy_resource_location_t;
/** Filesystem handle */
/** Filesystem handle */
typedef struct
{
unsigned long offset; /**< Offset to the start of the resource */
const char* filename; /**< name of the resource */
const char* filename; /**< name of the resource */
} cy_filesystem_resource_handle_t;
/** Resource handle structure */
typedef struct
{
cy_resource_location_t location; /**< resource location */
unsigned long size; /**< resource size */
unsigned long size; /**< resource size */
union
{
cy_filesystem_resource_handle_t fs; /**< handle for resource in filesystem */
const uint8_t* mem_data; /**< handle for resource in internal memory */
void* external_storage_context; /**< handle for resource in external storage */
/** handle for resource in filesystem */
cy_filesystem_resource_handle_t fs;
/** handle for resource in internal memory */
const uint8_t* mem_data;
/** handle for resource in external storage */
void* external_storage_context;
} val; /**< low-level handle (type varies depending on resource storage location) */
} cy_resource_handle_t;
@ -86,7 +97,7 @@ typedef struct
* \param size pointer to a uint32_t to receive the size
* \returns CY_RSLT_SUCCESS if the size was found, otherwise an error
*/
cy_rslt_t cy_resource_get_block_size(const cy_resource_handle_t *handle, uint32_t *size);
cy_rslt_t cy_resource_get_block_size(const cy_resource_handle_t* handle, uint32_t* size);
/**
* \brief return the total size for the resource
@ -94,7 +105,7 @@ cy_rslt_t cy_resource_get_block_size(const cy_resource_handle_t *handle, uint32_
* \param size pointer to a uint32_t to receive the size
* \returns CY_RSLT_SUCCESS if the size was found, otherwise an error
*/
cy_rslt_t cy_resource_get_total_size(const cy_resource_handle_t *handle, uint32_t *size);
cy_rslt_t cy_resource_get_total_size(const cy_resource_handle_t* handle, uint32_t* size);
/**
* \brief return the total number of blocks that exist for the resource
@ -102,27 +113,30 @@ cy_rslt_t cy_resource_get_total_size(const cy_resource_handle_t *handle, uint32_
* \param blocks pointer to a uint32_t to receive the count
* \returns CY_RSLT_SUCCESS if the count was found, otherwise an error
*/
cy_rslt_t cy_resource_get_block_count(const cy_resource_handle_t *handle, uint32_t *blocks);
cy_rslt_t cy_resource_get_block_count(const cy_resource_handle_t* handle, uint32_t* blocks);
/**
* \brief read data from a resource
* \param handle the handle to the resource
* \param blockno the block number of the data from the resource
* \param buffer pointer to receive buffer address from resource. The buffer must be freed when done. Eg: free(buffer)
* \param buffer pointer to receive buffer address from resource. The buffer must be freed when
* done. Eg: free(buffer)
* \param size location to receive the size of the block read
* \returns CY_RSLT_SUCCESS if data read, otherwise an error
*/
cy_rslt_t cy_resource_read(const cy_resource_handle_t *handle, uint32_t blockno, uint8_t **buffer, uint32_t *size);
cy_rslt_t cy_resource_read(const cy_resource_handle_t* handle, uint32_t blockno, uint8_t** buffer,
uint32_t* size);
/**
* \brief optimized version of read for resources stored in memory
* \brief optimized version of read for resources stored in memory
* \see CY_RESOURCE_IN_MEMORY
* \param handle the handle to the resource
* \param buffer pointer to receive buffer address from resource. This does NOT need to be freed.
* \param size location to receive the size of the block read
* \returns CY_RSLT_SUCCESS if data read, otherwise an error
*/
cy_rslt_t cy_resource_readonly_memory(const cy_resource_handle_t *handle, const uint8_t **buffer, uint32_t *size);
cy_rslt_t cy_resource_readonly_memory(const cy_resource_handle_t* handle, const uint8_t** buffer,
uint32_t* size);
#if defined(__cplusplus)
}

View File

@ -1,26 +1,26 @@
/***************************************************************************//**
* \file cyabs_resource.c
*
* \brief
* Provides API for reading data from resource files however they are stored.
*
********************************************************************************
* \copyright
* Copyright 2018-2020 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
/***********************************************************************************************//**
* \file cyabs_resource.c
*
* \brief
* Provides API for reading data from resource files however they are stored.
*
***************************************************************************************************
* \copyright
* Copyright 2018-2021 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**************************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
@ -32,16 +32,20 @@
extern "C" {
#endif
cy_rslt_t cy_resource_get_block_size(const cy_resource_handle_t *handle, uint32_t *size)
//--------------------------------------------------------------------------------------------------
// cy_resource_get_block_size
//--------------------------------------------------------------------------------------------------
cy_rslt_t cy_resource_get_block_size(const cy_resource_handle_t* handle, uint32_t* size)
{
switch (handle->location)
{
case CY_RESOURCE_IN_MEMORY:
*size = handle->size;
return CY_RSLT_SUCCESS;
case CY_RESOURCE_IN_FILESYSTEM:
case CY_RESOURCE_IN_EXTERNAL_STORAGE:
//TODO: implement
//TODO: implement
default:
CY_ASSERT(false);
*size = 0;
@ -49,18 +53,26 @@ cy_rslt_t cy_resource_get_block_size(const cy_resource_handle_t *handle, uint32_
}
}
cy_rslt_t cy_resource_get_total_size(const cy_resource_handle_t *handle, uint32_t *size)
//--------------------------------------------------------------------------------------------------
// cy_resource_get_total_size
//--------------------------------------------------------------------------------------------------
cy_rslt_t cy_resource_get_total_size(const cy_resource_handle_t* handle, uint32_t* size)
{
*size = handle->size;
return CY_RSLT_SUCCESS;
}
cy_rslt_t cy_resource_get_block_count(const cy_resource_handle_t *handle, uint32_t *blocks)
//--------------------------------------------------------------------------------------------------
// cy_resource_get_block_count
//--------------------------------------------------------------------------------------------------
cy_rslt_t cy_resource_get_block_count(const cy_resource_handle_t* handle, uint32_t* blocks)
{
uint32_t block_size;
uint32_t total_size = handle->size;
cy_rslt_t rslt = cy_resource_get_block_size(handle, &block_size);
if (rslt == CY_RSLT_SUCCESS && block_size > 0)
uint32_t block_size;
uint32_t total_size = handle->size;
cy_rslt_t rslt = cy_resource_get_block_size(handle, &block_size);
if ((rslt == CY_RSLT_SUCCESS) && (block_size > 0))
{
*blocks = (total_size + block_size - 1) / block_size;
}
@ -71,7 +83,12 @@ cy_rslt_t cy_resource_get_block_count(const cy_resource_handle_t *handle, uint32
return rslt;
}
cy_rslt_t cy_resource_read(const cy_resource_handle_t *handle, uint32_t blockno, uint8_t **buffer, uint32_t *size)
//--------------------------------------------------------------------------------------------------
// cy_resource_read
//--------------------------------------------------------------------------------------------------
cy_rslt_t cy_resource_read(const cy_resource_handle_t* handle, uint32_t blockno, uint8_t** buffer,
uint32_t* size)
{
cy_rslt_t result = cy_resource_get_block_size(handle, size);
if (CY_RSLT_SUCCESS != result)
@ -79,7 +96,7 @@ cy_rslt_t cy_resource_read(const cy_resource_handle_t *handle, uint32_t blockno,
return result;
}
void *p = malloc(*size);
void* p = malloc(*size);
if (!p)
{
return CY_RSLT_RSC_ERROR_UNAVAILABLE;
@ -89,13 +106,14 @@ cy_rslt_t cy_resource_read(const cy_resource_handle_t *handle, uint32_t blockno,
switch (handle->location)
{
case CY_RESOURCE_IN_MEMORY:
CY_UNUSED_PARAMETER(blockno); /* CY_ASSERT only processes in DEBUG, ignores for others */
CY_UNUSED_PARAMETER(blockno); // CY_ASSERT only processes in DEBUG, ignores for others
CY_ASSERT(0 == blockno);
memcpy(*buffer, handle->val.mem_data, *size);
return CY_RSLT_SUCCESS;
case CY_RESOURCE_IN_FILESYSTEM:
case CY_RESOURCE_IN_EXTERNAL_STORAGE:
//TODO: implement
//TODO: implement
default:
free(p);
CY_ASSERT(false);
@ -104,12 +122,17 @@ cy_rslt_t cy_resource_read(const cy_resource_handle_t *handle, uint32_t blockno,
}
}
cy_rslt_t cy_resource_readonly_memory(const cy_resource_handle_t *handle, const uint8_t **buffer, uint32_t *size)
//--------------------------------------------------------------------------------------------------
// cy_resource_readonly_memory
//--------------------------------------------------------------------------------------------------
cy_rslt_t cy_resource_readonly_memory(const cy_resource_handle_t* handle, const uint8_t** buffer,
uint32_t* size)
{
if (CY_RESOURCE_IN_MEMORY == handle->location)
{
*buffer = handle->val.mem_data;
*size = handle->size;
*size = handle->size;
return CY_RSLT_SUCCESS;
}
else
@ -118,6 +141,7 @@ cy_rslt_t cy_resource_readonly_memory(const cy_resource_handle_t *handle, const
}
}
#if defined(__cplusplus)
}
#endif

View File

@ -1 +1 @@
<version>1.0.0.17634</version>
<version>1.0.0.22384</version>