Skip to content

Commit

Permalink
src: Improve streaming API.
Browse files Browse the repository at this point in the history
Now just start, stream and end.
  • Loading branch information
peterharperuk committed Mar 21, 2023
1 parent 1918b1b commit 86622a3
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 133 deletions.
75 changes: 45 additions & 30 deletions src/cyw43_firmware_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,82 +52,97 @@ typedef struct cyw43_firmware_details {
} cyw43_firmware_details_t;
//!\}

/** \brief Firmware types
*/
typedef enum cyw43_firmare_type {
CYW43_FIRMWARE_WIFI,
CYW43_FIRMWARE_NVRAM,
CYW43_FIRMWARE_CLM,
CYW43_FIRMWARE_BLUETOOTH,
} cyw43_firmare_type_t;

/*!
* \brief Structure to hold function pointers for loading firmware
*/
//!\{
typedef struct cyw43_firmware_funcs {
const cyw43_firmware_details_t* (*firmware_details)(void); ///< get wifi firmware details
int (*start_wifi_fw)(const cyw43_firmware_details_t *fw_details); ///< start wifi firmware loading
int (*start_bt_fw)(const cyw43_firmware_details_t *fw_details); ///< start bt firmware loading
const uint8_t* (*get_wifi_fw)(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz); ///< get block of wifi firmware data
const uint8_t* (*get_bt_fw)(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz); ///< get block of bt firmware data
const uint8_t* (*get_nvram)(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz); ///< get block of nvram data
const uint8_t* (*get_clm)(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz); ///< get block of clm data
void (*end)(void); ///< end firmware loading
int (*start_fw_stream)(const cyw43_firmware_details_t *fw_details, cyw43_firmare_type_t which_firmware, void **streaming_context); ///< start fw streaming
const uint8_t* (*stream_fw)(void *streaming_context, size_t sz_required, uint8_t *working_buffer); ///< get block of firmware data
void (*end_fw_stream)(void *streaming_context, cyw43_firmare_type_t which_firmware); ///< end firmware loading
} cyw43_firmware_funcs_t;
//!\}

/*!
* \brief Get the functions used to load firmware
*
* This method returns pointers to functions that load firmware
* Return pointers to functions that load firmware
*
* \return structure that contains functions that load firmware
*/
const cyw43_firmware_funcs_t *cyw43_get_firmware_funcs(void);

/*!
* \brief Start reading wifi firmware data
*
* Starts the process of reading uncompressed wifi firmware
*
* \param fw_details Firmware details
* \param which_firmware Firmware to start reading
* \param streaming_context On return a pointer to the internal streaming state
* \return Zero on success or else an error code
*/

int cyw43_start_uncompressed_firmware(const cyw43_firmware_details_t *fw_details, cyw43_firmare_type_t which_firmware, void **streaming_context);

/*!
* \brief Read an uncompressed firmware data
*
* This method reads uncompressed firmware data
* Reads uncompressed firmware data
*
* \param addr Address to start reading
* \param sz_in Amount data to read in bytes
* \param streaming_context context created by the start function
* \param sz_required Amount data to read in bytes
* \param buffer Temporary buffer that can be used to read data into
* \param buffer_sz Size of buffer in bytes
* \return Pointer to data read
*/
const uint8_t *cyw43_read_uncompressed_firmware(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz);
const uint8_t *cyw43_read_uncompressed_firmware(void *streaming_context, size_t sz_required, __unused uint8_t *buffer);

/*!
* \brief Start reading wifi firmware data
*
* This method starts the process of reading compressed wifi firmware
* \brief End reading uncompressed firmware data
*
* \param fw Firmware details
* \return Zero on success or else an error code
* Ends the process of reading compressed firmware and frees resources
*/
int cyw43_start_compressed_wifi_firmware(const cyw43_firmware_details_t* fw);
void cyw43_end_uncompressed_firmware(void *streaming_context, cyw43_firmare_type_t which_firmware);

/*!
* \brief Start reading bluetooth firmware data
* \brief Start reading compressed wifi firmware data
*
* This method starts the process of reading compressed bluetooth firmware
* Starts the process of reading compressed wifi firmware
*
* \param fw Firmware details
* \param fw_details Firmware details
* \param which_firmware Firmware to start reading
* \param streaming_context On return a pointer to the internal streaming state
* \return Zero on success or else an error code
*/
int cyw43_start_compressed_bt_firmware(__unused const cyw43_firmware_details_t* fw);
int cyw43_start_compressed_firmware(const cyw43_firmware_details_t *fw_details, cyw43_firmare_type_t which_firmware, void **streaming_context);

/*!
* \brief Read compressed firmware data
*
* This method reads compressed firmware data
* Reads compressed firmware data
*
* \param addr Address to start reading
* \param sz_in Amount data to read in bytes
* \param streaming_context context created by the start function
* \param sz_required Amount data to read in bytes
* \param buffer Temporary buffer that can be used to read data into
* \param buffer_sz Size of buffer in bytes
* \return Pointer to data read
*/
const uint8_t *cyw43_read_compressed_firmware(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz);
const uint8_t* cyw43_read_compressed_firmware(void *streaming_context, size_t sz_required, uint8_t *buffer);

/*!
* \brief End reading compressed firmware data
*
* This method ends the process of reading compressed firmware
* Ends the process of reading compressed firmware and frees resources
*/
void cyw43_end_compressed_firmware(void);
void cyw43_end_compressed_firmware(void *streaming_context, cyw43_firmare_type_t which_firmware);

#endif
19 changes: 13 additions & 6 deletions src/cyw43_gz_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,21 @@ typedef struct uzlib_data {
struct uzlib_uncomp state;
size_t amount_left;
} uzlib_data_t;
static uzlib_data_t *uzlib;

#define CYW43_DECOMPRESS_ERR_NO_MEM -1
#define CYW43_DECOMPRESS_ERR_BAD_HEADER -2
#define CYW43_DECOMPRESS_ERR_NO_MORE -3
#define CYW43_DECOMPRESS_ERR_DECOMPRESS -4

int cyw43_gz_read_start(const uint8_t *raw_data, size_t raw_size)
int cyw43_gz_read_start(void **uzlib_context, const uint8_t *raw_data, size_t raw_size)
{
uzlib_init();

uzlib = cyw43_malloc(sizeof(*uzlib));
uzlib_data_t *uzlib = cyw43_malloc(sizeof(uzlib_data_t));
assert(uzlib);
if (!uzlib) {
return CYW43_DECOMPRESS_ERR_NO_MEM;
}

uzlib->amount_left = raw_data[raw_size - 1];
uzlib->amount_left = 256 * uzlib->amount_left + raw_data[raw_size - 2];
uzlib->amount_left = 256 * uzlib->amount_left + raw_data[raw_size - 3];
Expand All @@ -76,14 +74,19 @@ int cyw43_gz_read_start(const uint8_t *raw_data, size_t raw_size)
int res = uzlib_gzip_parse_header(&uzlib->state);
assert(res == TINF_OK);
if (res != TINF_OK) {
cyw43_free(uzlib);
return CYW43_DECOMPRESS_ERR_BAD_HEADER;
}

*uzlib_context = uzlib;
return (int)uzlib->amount_left;
}

int cyw43_gz_read_next(uint8_t *buffer, size_t sz)
int cyw43_gz_read_next(void *uzlib_context, uint8_t *buffer, size_t sz)
{
assert(uzlib_context);
uzlib_data_t *uzlib = (uzlib_data_t *)uzlib_context;

assert(uzlib->amount_left > 0);
if (uzlib->amount_left <= 0) {
return CYW43_DECOMPRESS_ERR_NO_MORE;
Expand All @@ -100,8 +103,12 @@ int cyw43_gz_read_next(uint8_t *buffer, size_t sz)
return chunk_sz;
}

void cyw43_gz_read_end(void)
void cyw43_gz_read_end(void *uzlib_context)
{
assert(uzlib_context);
uzlib_data_t *uzlib = (uzlib_data_t *)uzlib_context;

uzlib->amount_left = 0;
if (uzlib) {
cyw43_free(uzlib);
uzlib = NULL;
Expand Down
9 changes: 6 additions & 3 deletions src/cyw43_gz_read.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,31 @@
*
* This method prepares for decompressing data.
*
* \param uzlib_context Pointer used to return the decompression context
* \param raw_data The compressed data
* \param raw_size The size in bytes of the data
* \return >0 on success, the uncompressed data size in bytes or <0 on error
*/
int cyw43_gz_read_start(const uint8_t *raw_data, size_t raw_size);
int cyw43_gz_read_start(void **uzlib_context, const uint8_t *raw_data, size_t raw_size);

/*!
* \brief Get the next block of uncompressed data
*
* This method returns the next block of uncompressed data.
*
* \param uzlib_context Pointer to the decompression context
* \param buffer Buffer to fill with uncompressed data
* \param sz Requested size of uncompressed data in bytes
* \return The amount of data returned in the buffer or <0 on error
*/
int cyw43_gz_read_next(uint8_t *buffer, size_t sz);
int cyw43_gz_read_next(void *uzlib_context, uint8_t *buffer, size_t sz);

/*!
* \brief Finish decompressing data
*
* \param uzlib_context Pointer to the decompression context
* This method frees any resources used for decompressing data.
*/
void cyw43_gz_read_end(void);
void cyw43_gz_read_end(void *uzlib_context);

#endif

0 comments on commit 86622a3

Please sign in to comment.