work on bulk, not finished

sipo
Ondřej Hruška 6 years ago
parent 26aea3200c
commit 40479a0b24
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 9
      comm/messages.h
  2. 5
      gex.mk
  3. 4
      platform/platform.c
  4. 245
      units/test/unit_test.c
  5. 13
      units/test/unit_test.h

@ -24,9 +24,18 @@ enum TF_Types_ {
MSG_SUCCESS = 0x00, //!< Generic success response; used by default in all responses; payload is transaction-specific
MSG_PING = 0x01, //!< Ping request (or response), used to test connection
MSG_ERROR = 0x02, //!< Generic failure response (when a request fails to execute)
MSG_BULK_READ_OFFER = 0x03, //!< Offer of data to read. Payload: u32 total len
MSG_BULK_READ_POLL = 0x04, //!< Request to read a previously announced chunk.
MSG_BULK_WRITE_OFFER = 0x05, //!< Offer to receive data in a write transaction. Payload: u32 max size, u32 max chunk
MSG_BULK_DATA = 0x06, //!< Writing a chunk, or sending a chunk to master.
MSG_BULK_END = 0x07, //!< Bulk transfer is done, no more data to read or write. Recipient shall check total len and discard it on mismatch. There could be a checksum ...
MSG_BULK_ABORT = 0x08, //!< Discard the ongoing transfer
// Unit messages
MSG_UNIT_REQUEST = 0x10, //!< Command addressed to a particular unit
MSG_UNIT_REPORT = 0x11, //!< Spontaneous report from a unit
// System messages
MSG_LIST_UNITS = 0x20, //!< Get all unit call-signs and names
};

@ -7,6 +7,7 @@ GEX_SRC_DIR = \
User/units \
User/units/system \
User/units/neopixel \
User/units/test \
User/units/pin \
User/TinyFrame \
User/CWPack \
@ -36,10 +37,6 @@ GEX_INCLUDES = \
-IUser/TinyFrame \
-IUser/vfs \
-IUser/utils \
-IUser/units \
-IUser/units/system \
-IUser/units/neopixel \
-IUser/units/pin \
-IUser/framework \
-IUser/platform \
-IUser/tasks \

@ -6,11 +6,11 @@
#include "usbd_core.h"
#include "USB/usb_device.h"
#include "framework/resources.h"
#include "framework/unit_registry.h"
#include "units/pin/unit_pin.h"
#include "units/neopixel/unit_neopixel.h"
#include "units/test/unit_test.h"
// ----- SUPPORTED UNITS -----
@ -20,6 +20,8 @@ void plat_register_units(void)
ureg_add_type(&UNIT_PIN);
ureg_add_type(&UNIT_NEOPIXEL);
ureg_add_type(&UNIT_TEST);
// Platform-specific units could be added here
}

@ -0,0 +1,245 @@
//
// Created by MightyPork on 2017/11/25.
//
#include "comm/messages.h"
#include "unit_base.h"
#include "unit_test.h"
/** Private data structure */
struct priv {
uint32_t unused;
};
// ------------------------------------------------------------------------
/** Load from a binary buffer stored in Flash */
static void Tst_loadBinary(Unit *unit, PayloadParser *pp)
{
struct priv *priv = unit->data;
//
}
/** Write to a binary buffer for storing in Flash */
static void Tst_writeBinary(Unit *unit, PayloadBuilder *pb)
{
struct priv *priv = unit->data;
//
}
// ------------------------------------------------------------------------
/** Parse a key-value pair from the INI file */
static bool Tst_loadIni(Unit *unit, const char *key, const char *value)
{
bool suc = true;
struct priv *priv = unit->data;
//
return suc;
}
/** Generate INI file section for the unit */
static void Tst_writeIni(Unit *unit, IniWriter *iw)
{
struct priv *priv = unit->data;
//
}
// ------------------------------------------------------------------------
/** Allocate data structure and set defaults */
static bool Tst_preInit(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data = calloc_ck(1, sizeof(struct priv), &suc);
CHECK_SUC();
//
return true;
}
/** Finalize unit set-up */
static bool Tst_init(Unit *unit)
{
bool suc = true;
struct priv *priv = unit->data;
//
return true;
}
/** Tear down the unit */
static void Tst_deInit(Unit *unit)
{
struct priv *priv = unit->data;
//
// Free memory
free(unit->data);
unit->data = NULL;
}
// ------------------------------------------------------------------------
enum PinCmd_ {
CMD_PING = 0,
CMD_ECHO = 1,
CMD_BULKREAD = 2,
};
static void job_echo(Job *job)
{
tf_respond_buf(MSG_SUCCESS, job->frame_id, job->buf, job->len);
free(job->buf);
}
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 job_bulkread_chunk(Job *job)
{
dbg("Tx a chunk");
tf_respond_buf(MSG_BULK_DATA, job->frame_id, job->buf, job->len);
}
static void job_bulkread_close(Job *job)
{
dbg("Bulk read close.");
tf_respond_buf(MSG_BULK_END, job->frame_id, NULL, 0);
}
static void job_bulkread_offer(Job *job)
{
uint8_t buf[10];
PayloadBuilder pb = pb_start(buf, 10, NULL);
pb_u32(&pb, job->d32);
dbg("Offer bulk xfer of %d bytes", job->d32);
tf_respond_buf(MSG_BULK_READ_OFFER, job->frame_id, buf, (uint32_t) pb_length(&pb));
}
static TF_Result bulkread_lst(TinyFrame *tf, TF_Msg *msg)
{
if (msg->data == NULL) {
dbg("Bulk rx lst cleanup\r\n");
return TF_CLOSE;
} // this is a final call before timeout, to clean up
if (msg->type == MSG_BULK_READ_POLL) {
uint32_t pos = (uint32_t) msg->userdata2;
uint32_t total = (uint32_t) strlen(longtext); // normally we'd not calculate it here
dbg("BR poll, at %d", pos);
// Say we're done and close if it's over
if (pos >= total) {
Job job = {
.cb = job_bulkread_close,
.frame_id = msg->frame_id
};
scheduleJob(&job, TSK_SCHED_HIGH);
return TF_CLOSE;
}
PayloadParser pp = pp_start(msg->data, msg->len, NULL);
uint32_t chunk = pp_u32(&pp);
chunk = MIN(chunk, total - pos);
// this isn't at all resistant to error, we expect the communication to be lossless
Job job = {
.cb = job_bulkread_chunk,
.frame_id = msg->frame_id,
.buf = (uint8_t *) (longtext + pos),
.len = chunk,
};
scheduleJob(&job, TSK_SCHED_HIGH);
// advance the position pointer
pos += chunk;
msg->userdata2 = (void *) pos;
}
if (msg->type == MSG_BULK_ABORT) {
return TF_CLOSE;
}
return TF_STAY;
}
/** Handle a request message */
static bool Tst_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{
(void)pp;
struct priv *priv = unit->data;
switch (command) {
case CMD_PING:
sched_respond_suc(frame_id);
break;
case CMD_ECHO:;
uint32_t len = (uint32_t) pp_length(pp);
uint8_t *cpy = malloc(len);
memcpy(cpy, pp_tail(pp, NULL), len);
Job job = {
.cb = job_echo,
.frame_id = frame_id,
.buf = cpy,
.len = len,
};
dbg("Rx len %d, %.*s\r\n", len, len, cpy);
scheduleJob(&job, TSK_SCHED_HIGH);
break;
case CMD_BULKREAD:;
TF_Msg msg = {
.frame_id = frame_id,
.userdata = unit, // we'll keep a reference to the unit here
.userdata2 = 0 // and current position here.
};
// in a real scenario, we'd put a malloc'd struct here.
TF_AddIdListener(comm, &msg, bulkread_lst, 200);
Job job2 = {
.cb = job_bulkread_offer,
.frame_id = frame_id,
.d32 = (uint32_t) strlen(longtext),
};
scheduleJob(&job2, TSK_SCHED_HIGH);
break;
default:
sched_respond_bad_cmd(frame_id);
return false;
}
return true;
}
// ------------------------------------------------------------------------
/** Unit template */
const UnitDriver UNIT_TEST = {
.name = "TEST",
.description = "Test unit",
// Settings
.preInit = Tst_preInit,
.cfgLoadBinary = Tst_loadBinary,
.cfgWriteBinary = Tst_writeBinary,
.cfgLoadIni = Tst_loadIni,
.cfgWriteIni = Tst_writeIni,
// Init
.init = Tst_init,
.deInit = Tst_deInit,
// Function
.handleRequest = Tst_handleRequest,
};

@ -0,0 +1,13 @@
//
// Created by MightyPork on 2017/11/25.
// Testing unit that uses most of the protocol to verify the core functionality
//
#ifndef U_TEST_H
#define U_TEST_H
#include "unit.h"
extern const UnitDriver UNIT_TEST;
#endif //U_TEST_H
Loading…
Cancel
Save