Skip to content

Commit

Permalink
Fix ESP builds
Browse files Browse the repository at this point in the history
  • Loading branch information
CurlyMoo committed Apr 15, 2024
1 parent da84ad2 commit a9f07ab
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
50 changes: 31 additions & 19 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ typedef struct rule_timer_t {
static struct rule_timer_t timestamp;
#endif

#ifdef __linux__
#define PROGMEM
#endif

typedef struct array_t {
const char *key;
union {
Expand Down Expand Up @@ -103,7 +107,7 @@ struct unittest_t {
uint16_t bytes;
} run[3];
int8_t dofail;
} unittests[] = {
} unittests[] PROGMEM = {
/*
* Valid rules
* Run the first rule twice to get
Expand Down Expand Up @@ -834,9 +838,11 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {
rule_options.vm_value_get = vm_value_get;
rule_options.event_cb = event_cb;

int nrtests = sizeof(unittests)/sizeof(unittests[0]);
#ifdef ESP8266
struct unittest_t unittest;
memset(&unittest, 0, sizeof(struct unittest_t));

int nrtests = sizeof(unittests)/sizeof(struct unittest_t);
#ifdef ESP8266
/*LCOV_EXCL_START*/
if(*i >= nrtests) {
delay(3);
Expand All @@ -845,7 +851,13 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {
/*LCOV_EXCL_STOP*/
#endif

int len = strlen(unittests[(*i)].rule), oldoffset = 0;
#ifdef ESP8266
memcpy_P(&unittest, &unittests[(*i)], sizeof(struct unittest_t));
#else
memcpy(&unittest, &unittests[(*i)], sizeof(struct unittest_t));
#endif

int len = strlen(unittest.rule), oldoffset = 0;
int ret = 0;

struct pbuf mem;
Expand All @@ -862,12 +874,12 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {
#if (!defined(NON32XFER_HANDLER) && defined(MMU_SEC_HEAP)) || defined(COVERALLS)
if((void *)mempool >= (void *)MMU_SEC_HEAP) {
for(y=0;y<len;y++) {
mmu_set_uint8((void *)&(mempool[txtoffset+y]), (uint8_t)unittests[(*i)].rule[y]);
mmu_set_uint8((void *)&(mempool[txtoffset+y]), (uint8_t)unittest.rule[y]);
}
} else {
#endif
for(y=0;y<len;y++) {
mempool[txtoffset+y] = (uint8_t)unittests[(*i)].rule[y];
mempool[txtoffset+y] = (uint8_t)unittest.rule[y];
}
#if (!defined(NON32XFER_HANDLER) && defined(MMU_SEC_HEAP)) || defined(COVERALLS)
}
Expand Down Expand Up @@ -935,16 +947,16 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {
}
}

if(unittests[(*i)].dofail == 0 && strcmp(out, unittests[(*i)].validate[rule_nr-1].output) != 0) {
if(unittest.dofail == 0 && strcmp(out, unittest.validate[rule_nr-1].output) != 0) {
#ifdef ESP8266
char str[OUTPUT_SIZE];
memset(&str, 0, OUTPUT_SIZE);
snprintf((char *)&str, OUTPUT_SIZE, "Expected: %s\nWas: %s", unittests[(*i)].validate[rule_nr-1].output, out);
snprintf((char *)&str, OUTPUT_SIZE, "Expected: %s\nWas: %s", unittest.validate[rule_nr-1].output, out);
Serial.println(str);
exit(-1);
#else
/*LCOV_EXCL_START*/
printf("Expected: %s\n", unittests[(*i)].validate[rule_nr-1].output);
printf("Expected: %s\n", unittest.validate[rule_nr-1].output);
printf("Was: %s\n", out);
exit(-1);
/*LCOV_EXCL_STOP*/
Expand All @@ -953,8 +965,8 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {

#ifndef ESP8266
/*LCOV_EXCL_START*/
if((uint16_t)(rules[nrrules-1]->bc.nrbytes + (rules[nrrules-1]->heap->nrbytes) + rules_memused()) != unittests[(*i)].validate[rules[nrrules-1]->nr-1].bytes) {
printf("Expected: %d\n", unittests[(*i)].validate[rules[nrrules-1]->nr-1].bytes);
if((uint16_t)(rules[nrrules-1]->bc.nrbytes + (rules[nrrules-1]->heap->nrbytes) + rules_memused()) != unittest.validate[rules[nrrules-1]->nr-1].bytes) {
printf("Expected: %d\n", unittest.validate[rules[nrrules-1]->nr-1].bytes);
printf("Was: %d\n", rules[nrrules-1]->bc.nrbytes + (rules[nrrules-1]->heap->nrbytes) + rules_memused());

exit(-1);
Expand Down Expand Up @@ -983,8 +995,8 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {

#ifndef ESP8266
/*LCOV_EXCL_START*/
if((uint16_t)(rules[nrrules-1]->bc.nrbytes + (rules[nrrules-1]->heap->nrbytes) + rules_memused()) != unittests[(*i)].validate[rules[nrrules-1]->nr-1].bytes) {
printf("Expected: %d\n", unittests[(*i)].validate[rules[nrrules-1]->nr-1].bytes);
if((uint16_t)(rules[nrrules-1]->bc.nrbytes + (rules[nrrules-1]->heap->nrbytes) + rules_memused()) != unittest.validate[rules[nrrules-1]->nr-1].bytes) {
printf("Expected: %d\n", unittest.validate[rules[nrrules-1]->nr-1].bytes);
printf("Was: %d\n", rules[nrrules-1]->bc.nrbytes + (rules[nrrules-1]->heap->nrbytes) + rules_memused());

exit(-1);
Expand Down Expand Up @@ -1022,16 +1034,16 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {
}


if(unittests[(*i)].dofail == 0 && strcmp(out, unittests[(*i)].run[rule_nr-1].output) != 0) {
if(unittest.dofail == 0 && strcmp(out, unittest.run[rule_nr-1].output) != 0) {
#ifdef ESP8266
char str[OUTPUT_SIZE];
memset(&str, 0, OUTPUT_SIZE);
snprintf((char *)&str, OUTPUT_SIZE, "Expected: %s\nWas: %s", unittests[(*i)].run[rule_nr-1].output, out);
snprintf((char *)&str, OUTPUT_SIZE, "Expected: %s\nWas: %s", unittest.run[rule_nr-1].output, out);
Serial.println(str);
exit(-1);
#else
/*LCOV_EXCL_START*/
printf("Expected: %s\n", unittests[(*i)].run[rule_nr-1].output);
printf("Expected: %s\n", unittest.run[rule_nr-1].output);
printf("Was: %s\n", out);
exit(-1);
/*LCOV_EXCL_STOP*/
Expand Down Expand Up @@ -1059,16 +1071,16 @@ void run_test(int *i, unsigned char *mempool, uint16_t size) {
}
}

if(unittests[(*i)].dofail != ret) {
if(unittest.dofail != ret) {
/*LCOV_EXCL_START*/
printf("error %d\n", __LINE__);
exit(-1);
/*LCOV_EXCL_STOP*/
}

if(ret == -1) {
const char *rule = unittests[(*i)].rule;
int size = strlen(unittests[(*i)].rule);
const char *rule = unittest.rule;
int size = strlen(unittest.rule);
#ifdef ESP8266
char str[OUTPUT_SIZE];
/*LCOV_EXCL_START*/
Expand Down
44 changes: 23 additions & 21 deletions rules.ino
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,27 @@ void loop() {
ESP.wdtFeed();

#ifdef MMU_SEC_HEAP
if(heap == 0) {
if(testnr >= 0) {
if(heap == 0) {
#endif
Serial.println(F("Running from 1st heap"));
memset(mempool, 0, MEMPOOL_SIZE);
if(testnr >= 0) {
run_test(&testnr, mempool, MEMPOOL_SIZE);
}
Serial.println(F("Running from 1st heap"));
memset(mempool, 0, MEMPOOL_SIZE);
if(testnr >= 0) {
run_test(&testnr, mempool, MEMPOOL_SIZE);
}
#ifdef MMU_SEC_HEAP
} else {
Serial.println(F("Running from 2nd heap"));
memset((unsigned char *)MMU_SEC_HEAP, 0, MMU_SEC_HEAP_SIZE);
if(testnr >= 0) {
run_test(&testnr, (unsigned char *)MMU_SEC_HEAP, MMU_SEC_HEAP_SIZE);
testnr += heap;
} else {
Serial.println(F("Running from 2nd heap"));
memset((unsigned char *)MMU_SEC_HEAP, 0, MMU_SEC_HEAP_SIZE);
if(testnr >= 0) {
run_test(&testnr, (unsigned char *)MMU_SEC_HEAP, MMU_SEC_HEAP_SIZE);
testnr += heap;
}
}
heap ^= 1;
} else {
testnr++;
}
heap ^= 1;
#else
testnr++;
#endif
Expand All @@ -95,21 +99,19 @@ void loop() {
uint8_t loc[2];
int8_t ret;
} tests[nrtests] = {
{ { 750, 500 }, { 284, 0 }, {1, 0}, 0 },
{ { 300, 300 }, { 196, 88 }, {0, 1}, 0 },
{ { 300, 300 }, { 196, 88 }, {1, 0}, 0 },
{ { 300, 300 }, { 196, 88 }, {1, 1}, 0 },
{ { 300, 300 }, { 196, 88 }, {0, 0}, 0 },
{ { 175, 175 }, { 0, 180 }, {0, 0}, -1 }
{ { 750, 500 }, { 252, 0 }, {1, 0}, 0 },
{ { 300, 300 }, { 168, 84 }, {0, 1}, 0 },
{ { 300, 300 }, { 168, 84 }, {1, 0}, 0 },
{ { 300, 300 }, { 168, 84 }, {1, 1}, 0 },
{ { 300, 300 }, { 168, 84 }, {0, 0}, 0 },
{ { 175, 175 }, { 0, 120 }, {0, 0}, -1 }
};

for(uint8_t i=0;i<nrtests;i++) {
struct pbuf mem;
struct pbuf mem1;
struct pbuf input;
memset(&mem, 0, sizeof(struct pbuf));
memset(&mem1, 0, sizeof(struct pbuf));
memset(&input, 0, sizeof(struct pbuf));

#ifdef MMU_SEC_HEAP
memset((unsigned char *)MMU_SEC_HEAP, 0, MMU_SEC_HEAP_SIZE);
Expand Down
13 changes: 10 additions & 3 deletions src/rules/rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ static int8_t lexer_parse_quoted_string(char *text, uint16_t len, uint16_t *pos)
while(*pos < len) {
if((current < 9 || current > 10) && (current < 32 || current > 127)) {
return -2;
} else if(current == '\\' && text[(*pos+1)] == start) {
} else if(current == '\\' && getval(text[(*pos+1)]) == start) {
(*pos)+=2;
} else if(start == current) {
(*pos)--;
Expand Down Expand Up @@ -4630,6 +4630,7 @@ int8_t rule_run(struct rules_t *obj, uint8_t validate) {

/*****************/
STEP_CLEAR: {

memset(stack->buffer, 0, getval(stack->bufsize));
setval(stack->nrbytes, 4);
pos += sizeof(struct vm_top_t);
Expand Down Expand Up @@ -4872,7 +4873,7 @@ void rules_gc(struct rules_t ***rules, uint8_t *nrrules) {

int8_t rule_initialize(struct pbuf *input, struct rules_t ***rules, uint8_t *nrrules, struct pbuf *mempool, void *userdata) {
struct pbuf *mempool_rule = NULL;
uint16_t newlen = getval(input->tot_len);
uint16_t newlen = getval(input->tot_len), max_varstack_size = 4;
uint16_t heapsize = 4, bcsize = 0, varsize = 0, memsize = 0;
if(varstack == NULL) {
if((varstack = (struct rule_stack_t *)MALLOC(sizeof(struct rule_stack_t))) == NULL) {
Expand All @@ -4884,6 +4885,12 @@ int8_t rule_initialize(struct pbuf *input, struct rules_t ***rules, uint8_t *nrr
#endif
}

if(stack != NULL) {
if(getval(stack->bufsize) > max_varstack_size) {
max_varstack_size = getval(stack->bufsize);
}
}

if(newlen == 0) {
return 1;
}
Expand Down Expand Up @@ -4926,7 +4933,6 @@ int8_t rule_initialize(struct pbuf *input, struct rules_t ***rules, uint8_t *nrr
memset((*rules)[*nrrules], 0, sizeof(struct rules_t));
mempool->len += sizeof(struct rules_t);


(*rules)[*nrrules]->userdata = userdata;
struct rules_t *obj = (*rules)[*nrrules];
#if defined(DEBUG) || defined(COVERALLS)
Expand Down Expand Up @@ -5041,6 +5047,7 @@ int8_t rule_initialize(struct pbuf *input, struct rules_t ***rules, uint8_t *nrr
mempool->len += heapsize+sizeof(struct rule_stack_t);

stack = (struct rule_stack_t *)&((unsigned char *)mempool->payload)[mempool->len];
setval(stack->bufsize, max_varstack_size);
setval(stack->nrbytes, 4);
stack->buffer = &((unsigned char *)mempool->payload)[mempool->len+sizeof(struct rule_stack_t)];

Expand Down

0 comments on commit a9f07ab

Please sign in to comment.