improved bulk read api

This commit is contained in:
2017-12-23 19:10:23 +01:00
parent d8e2d016d2
commit 96d06a086f
3 changed files with 42 additions and 11 deletions
+11 -4
View File
@@ -10,17 +10,22 @@
#include "utils/payload_parser.h"
#include "utils/payload_builder.h"
/** Buffer for preparing bulk chunks */
static uint8_t bulkread_buffer[BULKREAD_MAX_CHUNK];
TF_Result bulkread_lst(TinyFrame *tf, TF_Msg *msg)
/**
* TF listener for the bulk read transaction
*/
static TF_Result bulkread_lst(TinyFrame *tf, TF_Msg *msg)
{
struct bulk_read *bulk = msg->userdata;
// this is a final call before timeout, to clean up
if (msg->data == NULL) {
dbg("Bulk rx lst cleanup\r\n");
goto close;
}
struct bulk_read *bulk = msg->userdata;
assert_param(NULL != bulk);
if (msg->type == MSG_BULK_ABORT) {
@@ -41,7 +46,7 @@ TF_Result bulkread_lst(TinyFrame *tf, TF_Msg *msg)
chunk = MIN(chunk, bulk->len - bulk->offset);
chunk = MIN(chunk, BULKREAD_MAX_CHUNK);
bulk->read(bulk->offset, chunk, bulkread_buffer);
bulk->read(bulk, chunk, bulkread_buffer);
TF_ClearMsg(msg);
msg->frame_id = bulk->frame_id;
@@ -59,12 +64,14 @@ TF_Result bulkread_lst(TinyFrame *tf, TF_Msg *msg)
close:
if (msg->userdata) {
free(msg->userdata);
// Ask user to free the bulk and userdata
bulk->read(bulk, 0, NULL);
msg->userdata = NULL;
}
return TF_CLOSE;
}
/** Start the bulk read flow */
void bulkread_start(TinyFrame *tf, struct bulk_read *bulk)
{
assert_param(bulk);
+23 -5
View File
@@ -16,16 +16,34 @@
#define BULKREAD_MAX_CHUNK 512 // this is a static buffer
typedef struct bulk_read BulkRead;
typedef void (*bulkread_data_cb)(uint32_t offset, uint32_t len, uint8_t *buffer);
/**
* Type of the bulk-read listener. Offset within the data can be retrieved from bulk->offset.
*
* If buffer is NULL, this is the last call and the bulk struct must be freed if it was allocated.
*
* @param bulk - a data object passed to the bulkread_start() function.
* @param chunk - size of the chunk to read
* @param buffer - destination buffer to fill with the data. NULL if bulk should be freed.
*/
typedef void (*bulkread_data_cb)(BulkRead *bulk, uint32_t chunk, uint8_t *buffer);
/** Bulk read structure */
struct bulk_read {
TF_ID frame_id;
bulkread_data_cb read;
uint32_t len;
uint32_t offset;
TF_ID frame_id; //!< ID of the requesting frame, used for the entire bulk read session
bulkread_data_cb read; //!< Read callback
uint32_t len; //!< Total data length
void *userdata; //!< A place for arbitrary userdata
uint32_t offset; //!< Internal offset counter, will be set to 0 on start.
};
/**
* Start a bulk read session
* @param tf - tinyframe instance
* @param bulk - bulk read config structure (malloc'd or static)
*/
void bulkread_start(TinyFrame *tf, struct bulk_read *bulk);