Skip to content

Commit 86622a3

Browse files
committed
src: Improve streaming API.
Now just start, stream and end.
1 parent 1918b1b commit 86622a3

4 files changed

Lines changed: 263 additions & 133 deletions

File tree

src/cyw43_firmware_defs.h

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,82 +52,97 @@ typedef struct cyw43_firmware_details {
5252
} cyw43_firmware_details_t;
5353
//!\}
5454

55+
/** \brief Firmware types
56+
*/
57+
typedef enum cyw43_firmare_type {
58+
CYW43_FIRMWARE_WIFI,
59+
CYW43_FIRMWARE_NVRAM,
60+
CYW43_FIRMWARE_CLM,
61+
CYW43_FIRMWARE_BLUETOOTH,
62+
} cyw43_firmare_type_t;
63+
5564
/*!
5665
* \brief Structure to hold function pointers for loading firmware
5766
*/
5867
//!\{
5968
typedef struct cyw43_firmware_funcs {
6069
const cyw43_firmware_details_t* (*firmware_details)(void); ///< get wifi firmware details
61-
int (*start_wifi_fw)(const cyw43_firmware_details_t *fw_details); ///< start wifi firmware loading
62-
int (*start_bt_fw)(const cyw43_firmware_details_t *fw_details); ///< start bt firmware loading
63-
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
64-
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
65-
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
66-
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
67-
void (*end)(void); ///< end firmware loading
70+
int (*start_fw_stream)(const cyw43_firmware_details_t *fw_details, cyw43_firmare_type_t which_firmware, void **streaming_context); ///< start fw streaming
71+
const uint8_t* (*stream_fw)(void *streaming_context, size_t sz_required, uint8_t *working_buffer); ///< get block of firmware data
72+
void (*end_fw_stream)(void *streaming_context, cyw43_firmare_type_t which_firmware); ///< end firmware loading
6873
} cyw43_firmware_funcs_t;
6974
//!\}
7075

7176
/*!
7277
* \brief Get the functions used to load firmware
7378
*
74-
* This method returns pointers to functions that load firmware
79+
* Return pointers to functions that load firmware
7580
*
7681
* \return structure that contains functions that load firmware
7782
*/
7883
const cyw43_firmware_funcs_t *cyw43_get_firmware_funcs(void);
7984

85+
/*!
86+
* \brief Start reading wifi firmware data
87+
*
88+
* Starts the process of reading uncompressed wifi firmware
89+
*
90+
* \param fw_details Firmware details
91+
* \param which_firmware Firmware to start reading
92+
* \param streaming_context On return a pointer to the internal streaming state
93+
* \return Zero on success or else an error code
94+
*/
95+
96+
int cyw43_start_uncompressed_firmware(const cyw43_firmware_details_t *fw_details, cyw43_firmare_type_t which_firmware, void **streaming_context);
97+
8098
/*!
8199
* \brief Read an uncompressed firmware data
82100
*
83-
* This method reads uncompressed firmware data
101+
* Reads uncompressed firmware data
84102
*
85-
* \param addr Address to start reading
86-
* \param sz_in Amount data to read in bytes
103+
* \param streaming_context context created by the start function
104+
* \param sz_required Amount data to read in bytes
87105
* \param buffer Temporary buffer that can be used to read data into
88-
* \param buffer_sz Size of buffer in bytes
89106
* \return Pointer to data read
90107
*/
91-
const uint8_t *cyw43_read_uncompressed_firmware(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz);
108+
const uint8_t *cyw43_read_uncompressed_firmware(void *streaming_context, size_t sz_required, __unused uint8_t *buffer);
92109

93110
/*!
94-
* \brief Start reading wifi firmware data
95-
*
96-
* This method starts the process of reading compressed wifi firmware
111+
* \brief End reading uncompressed firmware data
97112
*
98-
* \param fw Firmware details
99-
* \return Zero on success or else an error code
113+
* Ends the process of reading compressed firmware and frees resources
100114
*/
101-
int cyw43_start_compressed_wifi_firmware(const cyw43_firmware_details_t* fw);
115+
void cyw43_end_uncompressed_firmware(void *streaming_context, cyw43_firmare_type_t which_firmware);
102116

103117
/*!
104-
* \brief Start reading bluetooth firmware data
118+
* \brief Start reading compressed wifi firmware data
105119
*
106-
* This method starts the process of reading compressed bluetooth firmware
120+
* Starts the process of reading compressed wifi firmware
107121
*
108-
* \param fw Firmware details
122+
* \param fw_details Firmware details
123+
* \param which_firmware Firmware to start reading
124+
* \param streaming_context On return a pointer to the internal streaming state
109125
* \return Zero on success or else an error code
110126
*/
111-
int cyw43_start_compressed_bt_firmware(__unused const cyw43_firmware_details_t* fw);
127+
int cyw43_start_compressed_firmware(const cyw43_firmware_details_t *fw_details, cyw43_firmare_type_t which_firmware, void **streaming_context);
112128

113129
/*!
114130
* \brief Read compressed firmware data
115131
*
116-
* This method reads compressed firmware data
132+
* Reads compressed firmware data
117133
*
118-
* \param addr Address to start reading
119-
* \param sz_in Amount data to read in bytes
134+
* \param streaming_context context created by the start function
135+
* \param sz_required Amount data to read in bytes
120136
* \param buffer Temporary buffer that can be used to read data into
121-
* \param buffer_sz Size of buffer in bytes
122137
* \return Pointer to data read
123138
*/
124-
const uint8_t *cyw43_read_compressed_firmware(const uint8_t *addr, size_t sz_in, uint8_t *buffer, size_t buffer_sz);
139+
const uint8_t* cyw43_read_compressed_firmware(void *streaming_context, size_t sz_required, uint8_t *buffer);
125140

126141
/*!
127142
* \brief End reading compressed firmware data
128143
*
129-
* This method ends the process of reading compressed firmware
144+
* Ends the process of reading compressed firmware and frees resources
130145
*/
131-
void cyw43_end_compressed_firmware(void);
146+
void cyw43_end_compressed_firmware(void *streaming_context, cyw43_firmare_type_t which_firmware);
132147

133148
#endif

src/cyw43_gz_read.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,21 @@ typedef struct uzlib_data {
4545
struct uzlib_uncomp state;
4646
size_t amount_left;
4747
} uzlib_data_t;
48-
static uzlib_data_t *uzlib;
4948

5049
#define CYW43_DECOMPRESS_ERR_NO_MEM -1
5150
#define CYW43_DECOMPRESS_ERR_BAD_HEADER -2
5251
#define CYW43_DECOMPRESS_ERR_NO_MORE -3
5352
#define CYW43_DECOMPRESS_ERR_DECOMPRESS -4
5453

55-
int cyw43_gz_read_start(const uint8_t *raw_data, size_t raw_size)
54+
int cyw43_gz_read_start(void **uzlib_context, const uint8_t *raw_data, size_t raw_size)
5655
{
5756
uzlib_init();
5857

59-
uzlib = cyw43_malloc(sizeof(*uzlib));
58+
uzlib_data_t *uzlib = cyw43_malloc(sizeof(uzlib_data_t));
6059
assert(uzlib);
6160
if (!uzlib) {
6261
return CYW43_DECOMPRESS_ERR_NO_MEM;
6362
}
64-
6563
uzlib->amount_left = raw_data[raw_size - 1];
6664
uzlib->amount_left = 256 * uzlib->amount_left + raw_data[raw_size - 2];
6765
uzlib->amount_left = 256 * uzlib->amount_left + raw_data[raw_size - 3];
@@ -76,14 +74,19 @@ int cyw43_gz_read_start(const uint8_t *raw_data, size_t raw_size)
7674
int res = uzlib_gzip_parse_header(&uzlib->state);
7775
assert(res == TINF_OK);
7876
if (res != TINF_OK) {
77+
cyw43_free(uzlib);
7978
return CYW43_DECOMPRESS_ERR_BAD_HEADER;
8079
}
8180

81+
*uzlib_context = uzlib;
8282
return (int)uzlib->amount_left;
8383
}
8484

85-
int cyw43_gz_read_next(uint8_t *buffer, size_t sz)
85+
int cyw43_gz_read_next(void *uzlib_context, uint8_t *buffer, size_t sz)
8686
{
87+
assert(uzlib_context);
88+
uzlib_data_t *uzlib = (uzlib_data_t *)uzlib_context;
89+
8790
assert(uzlib->amount_left > 0);
8891
if (uzlib->amount_left <= 0) {
8992
return CYW43_DECOMPRESS_ERR_NO_MORE;
@@ -100,8 +103,12 @@ int cyw43_gz_read_next(uint8_t *buffer, size_t sz)
100103
return chunk_sz;
101104
}
102105

103-
void cyw43_gz_read_end(void)
106+
void cyw43_gz_read_end(void *uzlib_context)
104107
{
108+
assert(uzlib_context);
109+
uzlib_data_t *uzlib = (uzlib_data_t *)uzlib_context;
110+
111+
uzlib->amount_left = 0;
105112
if (uzlib) {
106113
cyw43_free(uzlib);
107114
uzlib = NULL;

src/cyw43_gz_read.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,31 @@
3939
*
4040
* This method prepares for decompressing data.
4141
*
42+
* \param uzlib_context Pointer used to return the decompression context
4243
* \param raw_data The compressed data
4344
* \param raw_size The size in bytes of the data
4445
* \return >0 on success, the uncompressed data size in bytes or <0 on error
4546
*/
46-
int cyw43_gz_read_start(const uint8_t *raw_data, size_t raw_size);
47+
int cyw43_gz_read_start(void **uzlib_context, const uint8_t *raw_data, size_t raw_size);
4748

4849
/*!
4950
* \brief Get the next block of uncompressed data
5051
*
5152
* This method returns the next block of uncompressed data.
5253
*
54+
* \param uzlib_context Pointer to the decompression context
5355
* \param buffer Buffer to fill with uncompressed data
5456
* \param sz Requested size of uncompressed data in bytes
5557
* \return The amount of data returned in the buffer or <0 on error
5658
*/
57-
int cyw43_gz_read_next(uint8_t *buffer, size_t sz);
59+
int cyw43_gz_read_next(void *uzlib_context, uint8_t *buffer, size_t sz);
5860

5961
/*!
6062
* \brief Finish decompressing data
6163
*
64+
* \param uzlib_context Pointer to the decompression context
6265
* This method frees any resources used for decompressing data.
6366
*/
64-
void cyw43_gz_read_end(void);
67+
void cyw43_gz_read_end(void *uzlib_context);
6568

6669
#endif

0 commit comments

Comments
 (0)