improved bulk read api

sipo
Ondřej Hruška 7 years ago
parent d8e2d016d2
commit 96d06a086f
  1. 15
      comm/msg_bulkread.c
  2. 32
      comm/msg_bulkread.h
  3. 10
      units/test/unit_test.c

@ -10,17 +10,22 @@
#include "utils/payload_parser.h" #include "utils/payload_parser.h"
#include "utils/payload_builder.h" #include "utils/payload_builder.h"
/** Buffer for preparing bulk chunks */
static uint8_t bulkread_buffer[BULKREAD_MAX_CHUNK]; 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 // this is a final call before timeout, to clean up
if (msg->data == NULL) { if (msg->data == NULL) {
dbg("Bulk rx lst cleanup\r\n"); dbg("Bulk rx lst cleanup\r\n");
goto close; goto close;
} }
struct bulk_read *bulk = msg->userdata;
assert_param(NULL != bulk); assert_param(NULL != bulk);
if (msg->type == MSG_BULK_ABORT) { 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, bulk->len - bulk->offset);
chunk = MIN(chunk, BULKREAD_MAX_CHUNK); chunk = MIN(chunk, BULKREAD_MAX_CHUNK);
bulk->read(bulk->offset, chunk, bulkread_buffer); bulk->read(bulk, chunk, bulkread_buffer);
TF_ClearMsg(msg); TF_ClearMsg(msg);
msg->frame_id = bulk->frame_id; msg->frame_id = bulk->frame_id;
@ -59,12 +64,14 @@ TF_Result bulkread_lst(TinyFrame *tf, TF_Msg *msg)
close: close:
if (msg->userdata) { if (msg->userdata) {
free(msg->userdata); // Ask user to free the bulk and userdata
bulk->read(bulk, 0, NULL);
msg->userdata = NULL; msg->userdata = NULL;
} }
return TF_CLOSE; return TF_CLOSE;
} }
/** Start the bulk read flow */
void bulkread_start(TinyFrame *tf, struct bulk_read *bulk) void bulkread_start(TinyFrame *tf, struct bulk_read *bulk)
{ {
assert_param(bulk); assert_param(bulk);

@ -16,16 +16,34 @@
#define BULKREAD_MAX_CHUNK 512 // this is a static buffer #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 { struct bulk_read {
TF_ID frame_id; TF_ID frame_id; //!< ID of the requesting frame, used for the entire bulk read session
bulkread_data_cb read; bulkread_data_cb read; //!< Read callback
uint32_t len; uint32_t len; //!< Total data length
uint32_t offset; 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); void bulkread_start(TinyFrame *tf, struct bulk_read *bulk);

@ -97,9 +97,15 @@ enum PinCmd_ {
static const char *longtext = "The history of all hitherto existing societies is the history of class struggles.\n\nFreeman and slave, patrician and plebeian, lord and serf, guild-master and journeyman, in a word, oppressor and oppressed, stood in constant opposition to one another, carried on an uninterrupted, now hidden, now open fight, a fight that each time ended, either in a revolutionary re-constitution of society at large, or in the common ruin of the contending classes.\n\nIn the earlier epochs of history, we find almost everywhere a complicated arrangement of society into various orders, a manifold gradation of social rank. In ancient Rome we have patricians, knights, plebeians, slaves; in the Middle Ages, feudal lords, vassals, guild-masters, journeymen, apprentices, serfs; in almost all of these classes, again, subordinate gradations.\n\nThe modern bourgeois society that has sprouted from the ruins of feudal society has not done away with class antagonisms. It has but established new classes, new conditions of oppression, new forms of struggle in place of the old ones. Our epoch, the epoch of the bourgeoisie, possesses, however, this distinctive feature: it has simplified the class antagonisms. Society as a whole is more and more splitting up into two great hostile camps, into two great classes, directly facing each other: Bourgeoisie and Proletariat.\n\nFrom the serfs of the Middle Ages sprang the chartered burghers of the earliest towns. From these burgesses the first elements of the bourgeoisie were developed.\n\nThe discovery of America, the rounding of the Cape, opened up fresh ground for the rising bourgeoisie. The East-Indian and Chinese markets, the colonisation of America, trade with the colonies, the increase in the means of exchange and in commodities generally, gave to commerce, to navigation, to industry, an impulse never before known, and thereby, to the revolutionary element in the tottering feudal society, a rapid development.\n\nThe feudal system of industry, under which industrial production was monopolised by closed guilds, now no longer sufficed for the growing wants of the new markets. The manufacturing system took its place. The guild-masters were pushed on one side by the manufacturing middle class; division of labour between the different corporate guilds vanished in the face of division of labour in each single workshop.\n\nMeantime the markets kept ever growing, the demand ever rising. Even manufacture no longer sufficed. Thereupon, steam and machinery revolutionised industrial production. The place of manufacture was taken by the giant, Modern Industry, the place of the industrial middle class, by industrial millionaires, the leaders of whole industrial armies, the modern bourgeois.\n\nModern industry has established the world-market, for which the discovery of America paved the way. This market has given an immense development to commerce, to navigation, to communication by land. This development has, in its time, reacted on the extension of industry; and in proportion as industry, commerce, navigation, railways extended, in the same proportion the bourgeoisie developed, increased its capital, and pushed into the background every class handed down from the Middle Ages.\n\nWe see, therefore, how the modern bourgeoisie is itself the product of a long course of development, of a series of revolutions in the modes of production and of exchange.\n\nEach step in the development of the bourgeoisie was accompanied by a corresponding political advance of that class. An oppressed class under the sway of the feudal nobility, an armed and self-governing association in the mediaeval commune; here independent urban republic (as in Italy and Germany), there taxable \"third estate\" of the monarchy (as in France), afterwards, in the period of manufacture proper, serving either the semi-feudal or the absolute monarchy as a counterpoise against the nobility, and, in fact, corner-stone of the great monarchies in general, the bourgeoisie has at last, since the establishment of Modern Industry and of the world-market, conquered for itself, in the modern representative State, exclusive political sway. The executive of the modern State is but a committee for managing the common affairs of the whole bourgeoisie."; static const char *longtext = "The history of all hitherto existing societies is the history of class struggles.\n\nFreeman and slave, patrician and plebeian, lord and serf, guild-master and journeyman, in a word, oppressor and oppressed, stood in constant opposition to one another, carried on an uninterrupted, now hidden, now open fight, a fight that each time ended, either in a revolutionary re-constitution of society at large, or in the common ruin of the contending classes.\n\nIn the earlier epochs of history, we find almost everywhere a complicated arrangement of society into various orders, a manifold gradation of social rank. In ancient Rome we have patricians, knights, plebeians, slaves; in the Middle Ages, feudal lords, vassals, guild-masters, journeymen, apprentices, serfs; in almost all of these classes, again, subordinate gradations.\n\nThe modern bourgeois society that has sprouted from the ruins of feudal society has not done away with class antagonisms. It has but established new classes, new conditions of oppression, new forms of struggle in place of the old ones. Our epoch, the epoch of the bourgeoisie, possesses, however, this distinctive feature: it has simplified the class antagonisms. Society as a whole is more and more splitting up into two great hostile camps, into two great classes, directly facing each other: Bourgeoisie and Proletariat.\n\nFrom the serfs of the Middle Ages sprang the chartered burghers of the earliest towns. From these burgesses the first elements of the bourgeoisie were developed.\n\nThe discovery of America, the rounding of the Cape, opened up fresh ground for the rising bourgeoisie. The East-Indian and Chinese markets, the colonisation of America, trade with the colonies, the increase in the means of exchange and in commodities generally, gave to commerce, to navigation, to industry, an impulse never before known, and thereby, to the revolutionary element in the tottering feudal society, a rapid development.\n\nThe feudal system of industry, under which industrial production was monopolised by closed guilds, now no longer sufficed for the growing wants of the new markets. The manufacturing system took its place. The guild-masters were pushed on one side by the manufacturing middle class; division of labour between the different corporate guilds vanished in the face of division of labour in each single workshop.\n\nMeantime the markets kept ever growing, the demand ever rising. Even manufacture no longer sufficed. Thereupon, steam and machinery revolutionised industrial production. The place of manufacture was taken by the giant, Modern Industry, the place of the industrial middle class, by industrial millionaires, the leaders of whole industrial armies, the modern bourgeois.\n\nModern industry has established the world-market, for which the discovery of America paved the way. This market has given an immense development to commerce, to navigation, to communication by land. This development has, in its time, reacted on the extension of industry; and in proportion as industry, commerce, navigation, railways extended, in the same proportion the bourgeoisie developed, increased its capital, and pushed into the background every class handed down from the Middle Ages.\n\nWe see, therefore, how the modern bourgeoisie is itself the product of a long course of development, of a series of revolutions in the modes of production and of exchange.\n\nEach step in the development of the bourgeoisie was accompanied by a corresponding political advance of that class. An oppressed class under the sway of the feudal nobility, an armed and self-governing association in the mediaeval commune; here independent urban republic (as in Italy and Germany), there taxable \"third estate\" of the monarchy (as in France), afterwards, in the period of manufacture proper, serving either the semi-feudal or the absolute monarchy as a counterpoise against the nobility, and, in fact, corner-stone of the great monarchies in general, the bourgeoisie has at last, since the establishment of Modern Industry and of the world-market, conquered for itself, in the modern representative State, exclusive political sway. The executive of the modern State is but a committee for managing the common affairs of the whole bourgeoisie.";
static void br_longtext(uint32_t offset, uint32_t len, uint8_t *buffer) static void br_longtext(struct bulk_read *bulk, uint32_t chunk, uint8_t *buffer)
{ {
memcpy(buffer, longtext+offset, len); // clean-up request
if (buffer == NULL) {
free(bulk);
return;
}
memcpy(buffer, longtext+bulk->offset, chunk);
} }
/** Handle a request message */ /** Handle a request message */

Loading…
Cancel
Save