Skip to content

Commit

Permalink
Merge pull request #337 from USC-ACTLab/issue313
Browse files Browse the repository at this point in the history
Allow more than 256 logging vars/parameters
  • Loading branch information
ataffanel authored Aug 7, 2018
2 parents efbd2d0 + 1116d1f commit eba8dd1
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include "trace.h"
#include "usec_time.h"

#define PROTOCOL_VERSION 3
#define PROTOCOL_VERSION 4

#ifdef STM32F4XX
#ifndef P_NAME
Expand Down
203 changes: 189 additions & 14 deletions src/modules/src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@

#include "console.h"
#include "cfassert.h"
#include "debug.h"

#if 0
#include "debug.h"
#define LOG_DEBUG(fmt, ...) DEBUG_PRINT("D/log " fmt, ## __VA_ARGS__)
#define LOG_ERROR(fmt, ...) DEBUG_PRINT("E/log " fmt, ## __VA_ARGS__)
#else
Expand Down Expand Up @@ -97,20 +97,29 @@ struct ops_setting {
uint8_t id;
} __attribute__((packed));

struct ops_setting_v2 {
uint8_t logType;
uint16_t id;
} __attribute__((packed));


#define TOC_CH 0
#define CONTROL_CH 1
#define LOG_CH 2

#define CMD_GET_ITEM 0
#define CMD_GET_INFO 1
#define CMD_GET_ITEM 0 // original version: up to 255 entries
#define CMD_GET_INFO 1 // original version: up to 255 entries
#define CMD_GET_ITEM_V2 2 // version 2: up to 16k entries
#define CMD_GET_INFO_V2 3 // version 2: up to 16k entries

#define CONTROL_CREATE_BLOCK 0
#define CONTROL_APPEND_BLOCK 1
#define CONTROL_DELETE_BLOCK 2
#define CONTROL_START_BLOCK 3
#define CONTROL_STOP_BLOCK 4
#define CONTROL_RESET 5
#define CONTROL_CREATE_BLOCK 0
#define CONTROL_APPEND_BLOCK 1
#define CONTROL_DELETE_BLOCK 2
#define CONTROL_START_BLOCK 3
#define CONTROL_STOP_BLOCK 4
#define CONTROL_RESET 5
#define CONTROL_CREATE_BLOCK_V2 6
#define CONTROL_APPEND_BLOCK_V2 7

#define BLOCK_ID_FREE -1

Expand All @@ -130,15 +139,17 @@ extern struct log_s _log_stop;
static struct log_s * logs;
static int logsLen;
static uint32_t logsCrc;
static int logsCount = 0;
static uint16_t logsCount = 0;

static CRTPPacket p;

static bool isInit = false;

/* Log management functions */
static int logAppendBlock(int id, struct ops_setting * settings, int len);
static int logAppendBlockV2(int id, struct ops_setting_v2 * settings, int len);
static int logCreateBlock(unsigned char id, struct ops_setting * settings, int len);
static int logCreateBlockV2(unsigned char id, struct ops_setting_v2 * settings, int len);
static int logDeleteBlock(int id);
static int logStartBlock(int id, unsigned int period);
static int logStopBlock(int id);
Expand Down Expand Up @@ -170,8 +181,8 @@ void logInit(void)
groupLength = strlen(group);
}
} else {
// CMD_GET_ITEM result's size is: 3 + strlen(logs[i].name) + groupLength + 2
if (strlen(logs[i].name) + groupLength + 2 > 27) {
// CMD_GET_ITEM_V2 result's size is: 3 + strlen(logs[i].name) + groupLength + 2
if (strlen(logs[i].name) + groupLength + 2 > 26) {
LOG_ERROR("'%s.%s' too long\n", group, logs[i].name);
ASSERT_FAILED();
}
Expand Down Expand Up @@ -231,18 +242,24 @@ void logTOCProcess(int command)
{
int ptr = 0;
char * group = "plop";
int n=0;
uint16_t n=0;
uint16_t logId=0;

switch (command)
{
case CMD_GET_INFO: //Get info packet about the log implementation
DEBUG_PRINT("Client uses old logging API!\n");
LOG_DEBUG("Packet is TOC_GET_INFO\n");
ptr = 0;
group = "";
p.header=CRTP_HEADER(CRTP_PORT_LOG, TOC_CH);
p.size=8;
p.data[0]=CMD_GET_INFO;
p.data[1]=logsCount;
if (logsCount < 255) {
p.data[1]=logsCount;
} else {
p.data[1]=255;
}
memcpy(&p.data[2], &logsCrc, 4);
p.data[6]=LOG_MAX_BLOCKS;
p.data[7]=LOG_MAX_OPS;
Expand Down Expand Up @@ -287,6 +304,59 @@ void logTOCProcess(int command)
crtpSendPacket(&p);
}
break;
case CMD_GET_INFO_V2: //Get info packet about the log implementation
LOG_DEBUG("Packet is TOC_GET_INFO\n");
ptr = 0;
group = "";
p.header=CRTP_HEADER(CRTP_PORT_LOG, TOC_CH);
p.size=9;
p.data[0]=CMD_GET_INFO_V2;
memcpy(&p.data[1], &logsCount, 2);
memcpy(&p.data[3], &logsCrc, 4);
p.data[7]=LOG_MAX_BLOCKS;
p.data[8]=LOG_MAX_OPS;
crtpSendPacket(&p);
break;
case CMD_GET_ITEM_V2: //Get log variable
memcpy(&logId, &p.data[1], 2);
LOG_DEBUG("Packet is TOC_GET_ITEM Id: %d\n", logId);
for (ptr=0; ptr<logsLen; ptr++) //Ptr points a group
{
if (logs[ptr].type & LOG_GROUP)
{
if (logs[ptr].type & LOG_START)
group = logs[ptr].name;
else
group = "";
}
else //Ptr points a variable
{
if (n==logId)
break;
n++;
}
}

if (ptr<logsLen)
{
LOG_DEBUG(" Item is \"%s\":\"%s\"\n", group, logs[ptr].name);
p.header=CRTP_HEADER(CRTP_PORT_LOG, TOC_CH);
p.data[0]=CMD_GET_ITEM_V2;
memcpy(&p.data[1], &logId, 2);
p.data[3]=logs[ptr].type;
p.size=4+2+strlen(group)+strlen(logs[ptr].name);
ASSERT(p.size <= CRTP_MAX_DATA_SIZE); // Too long! The name of the group or the parameter may be too long.
memcpy(p.data+4, group, strlen(group)+1);
memcpy(p.data+4+strlen(group)+1, logs[ptr].name, strlen(logs[ptr].name)+1);
crtpSendPacket(&p);
} else {
LOG_DEBUG(" Index out of range!");
p.header=CRTP_HEADER(CRTP_PORT_LOG, TOC_CH);
p.data[0]=CMD_GET_ITEM_V2;
p.size=1;
crtpSendPacket(&p);
}
break;
}
}

Expand Down Expand Up @@ -319,6 +389,16 @@ void logControlProcess()
logReset();
ret = 0;
break;
case CONTROL_CREATE_BLOCK_V2:
ret = logCreateBlockV2( p.data[1],
(struct ops_setting_v2*)&p.data[2],
(p.size-2)/sizeof(struct ops_setting_v2) );
break;
case CONTROL_APPEND_BLOCK_V2:
ret = logAppendBlockV2( p.data[1],
(struct ops_setting_v2*)&p.data[2],
(p.size-2)/sizeof(struct ops_setting_v2) );
break;
}

//Commands answer
Expand Down Expand Up @@ -356,6 +436,35 @@ static int logCreateBlock(unsigned char id, struct ops_setting * settings, int l
return logAppendBlock(id, settings, len);
}

static int logCreateBlockV2(unsigned char id, struct ops_setting_v2 * settings, int len)
{
int i;

for (i=0; i<LOG_MAX_BLOCKS; i++)
if (id == logBlocks[i].id) return EEXIST;

for (i=0; i<LOG_MAX_BLOCKS; i++)
if (logBlocks[i].id == BLOCK_ID_FREE) break;

if (i == LOG_MAX_BLOCKS)
return ENOMEM;

logBlocks[i].id = id;
logBlocks[i].timer = xTimerCreate( "logTimer", M2T(1000),
pdTRUE, &logBlocks[i], logBlockTimed );
logBlocks[i].ops = NULL;

if (logBlocks[i].timer == NULL)
{
logBlocks[i].id = BLOCK_ID_FREE;
return ENOMEM;
}

LOG_DEBUG("Added block ID %d\n", id);

return logAppendBlockV2(id, settings, len);
}

static int blockCalcLength(struct log_block * block);
static struct log_ops * opsMalloc();
static void opsFree(struct log_ops * ops);
Expand Down Expand Up @@ -428,6 +537,72 @@ static int logAppendBlock(int id, struct ops_setting * settings, int len)
return 0;
}

static int logAppendBlockV2(int id, struct ops_setting_v2 * settings, int len)
{
int i;
struct log_block * block;

LOG_DEBUG("Appending %d variable to block %d\n", len, id);

for (i=0; i<LOG_MAX_BLOCKS; i++)
if (logBlocks[i].id == id) break;

if (i >= LOG_MAX_BLOCKS) {
LOG_ERROR("Trying to append block id %d that doesn't exist.", id);
return ENOENT;
}

block = &logBlocks[i];

for (i=0; i<len; i++)
{
int currentLength = blockCalcLength(block);
struct log_ops * ops;
int varId;

if ((currentLength + typeLength[settings[i].logType&0x0F])>LOG_MAX_LEN) {
LOG_ERROR("Trying to append a full block. Block id %d.\n", id);
return E2BIG;
}

ops = opsMalloc();

if(!ops) {
LOG_ERROR("No more ops memory free!\n");
return ENOMEM;
}

if (settings[i].id != 255) //TOC variable
{
varId = variableGetIndex(settings[i].id);

if (varId<0) {
LOG_ERROR("Trying to add variable Id %d that does not exists.", settings[i].id);
return ENOENT;
}

ops->variable = logs[varId].address;
ops->storageType = logs[varId].type;
ops->logType = settings[i].logType&0x0F;

LOG_DEBUG("Appended variable %d to block %d\n", settings[i].id, id);
} else { //Memory variable
//TODO: Check that the address is in ram
ops->variable = (void*)(&settings[i]+1);
ops->storageType = (settings[i].logType>>4)&0x0F;
ops->logType = settings[i].logType&0x0F;
i += 2;

LOG_DEBUG("Appended var addr 0x%x to block %d\n", (int)ops->variable, id);
}
blockAppendOps(block, ops);

LOG_DEBUG(" Now lenght %d\n", blockCalcLength(block));
}

return 0;
}

static int logDeleteBlock(int id)
{
int i;
Expand Down
Loading

0 comments on commit eba8dd1

Please sign in to comment.