X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Fsds.c;fp=client%2Fsds.c;h=56f5895500520d14a10610b6a014e090c407af0e;hb=cc93aff1c0867a33c41b4120c001b407d1b8850a;hp=e6bf3b560d04b54fc06db50350cdf836d96d5da1;hpb=09b207a39b7791098daafd7d87c3ad9d3db3e19f;p=mTask.git diff --git a/client/sds.c b/client/sds.c index e6bf3b5..56f5895 100644 --- a/client/sds.c +++ b/client/sds.c @@ -1,119 +1,133 @@ #include #include - -#ifndef STM -#include #include -#endif #include "interface.h" -#include "interpret.h" #include "sds.h" +#include "mem.h" -struct sds sdss[MAXSDSS]; -uint8_t c; +extern uint8_t *mem_top; +extern uint8_t *mem_bottom; +extern uint8_t *mem_task; +extern uint8_t *mem_sds; -void sds_init(void) +struct sds *sds_head(void) { - memset(&sdss, 0, sizeof(struct sds)*MAXSDSS); + return mem_sds == mem_top ? NULL + : (struct sds *)(mem_top-sizeof(struct sds)); } -void sds_register(void) +struct sds *sds_next(struct sds *s) { - uint8_t cs; - for(cs = 0; csid == id){ break; + } + s = sds_next(s); + } + return s; +} - if(cs == MAXSDSS) - die("Trying to add too much sdss..."); +void sds_register(void) +{ + debug("free memory: %lu", mem_free()); + + if(mem_sds-sizeof(struct sds) < mem_task){ + die("Out of memory... Not enough to allocate sds struct"); + } + + struct sds *s = (struct sds *)(mem_sds-sizeof(struct sds)); - memset(&sdss[cs], 0, sizeof(struct sds)); //Read identifier - sdss[cs].id = read16(); - //Read value - sdss[cs].type = read_byte(); - sdss[cs].value = read16(); + s->id = read16(); - debug("Received sds %d: %d", sdss[cs].id, sdss[cs].value); - sdss[cs].used = true; + //Read value + s->type = read_byte(); + s->value = read16(); write_byte('s'); - write16(sdss[cs].id); + write16(s->id); write_byte('\n'); + + mem_sds -= sizeof(struct sds); } -void sds_delete(void) +void sds_delete(uint8_t c) { - uint8_t cs; - cs = read16(); - sdss[cs].used = false; - write_byte('a'); - write16(sdss[cs].id); - write_byte('\n'); + struct sds *s = sds_get(c); + + if(s != NULL){ + //We found the sds, now we move verything from the end of the sds up to + //the pointer to the right + uint8_t *end = (uint8_t *)s; + uint8_t *start = end + sizeof(struct sds) - 1; + for(int i = 0; ivalue = read16(); + debug("Received sds update %d: %d", s->id, s->value); + write_byte('u'); + write16(s->id); + write_byte('\n'); + return true; } return false; } void sds_publish(int id) { - uint8_t cs; - for(cs = 0; csid, s->value); + write_byte('p'); + write16(s->id); + write_byte(s->type); + + switch(s->type){ + //Long + case 'l': + //Int + case 'i': + write16(s->value); + break; + case 'b': //Bool + case 'c': //Character + case 'B': //Button + case 'L': //UserLED + write_byte(s->value); + break; } + write_byte('\n'); + return; + + } else { + debug("SDS identifier unknown: %d", id); + die(""); } - debug("SDS identifier unknown: %d", id); - die(""); } int sds_fetch(int id) { - uint8_t cs; - for(cs = 0; csvalue; + } debug("SDS identifier unknown: %d", id); die(""); return 0; @@ -121,13 +135,11 @@ int sds_fetch(int id) void sds_store(int id, int val) { - uint8_t cs; - for(cs = 0; csvalue = val; + } else { + debug("SDS identifier unknown: %d", id); + die(""); } - debug("SDS identifier unknown: %d", id); - die(""); }