add memory reset when resetting, add debug for share writes and add live share changi...
[mTask.git] / client / sds.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "interface.h"
5 #include "sds.h"
6 #include "mem.h"
7
8 extern uint8_t *mem_top, *mem_bottom, *mem_task, *mem_sds;
9
10 struct sds *sds_head(void)
11 {
12 return mem_sds == mem_top ? NULL
13 : (struct sds *)(mem_top-sizeof(struct sds));
14 }
15
16 struct sds *sds_next(struct sds *s)
17 {
18 uint8_t *next = (uint8_t *)s - sizeof(struct sds);
19 return next < mem_sds ? NULL : (struct sds *)next;
20 }
21
22 struct sds *sds_get(uint8_t id)
23 {
24 struct sds *s = sds_head();
25 while(s != NULL){
26 if(s->id == id){
27 break;
28 }
29 s = sds_next(s);
30 }
31 return s;
32 }
33
34 void sds_register(void)
35 {
36 debug("free memory: %lu", mem_free());
37
38 if(mem_sds-sizeof(struct sds) < mem_task){
39 die("Out of memory... Not enough to allocate sds struct");
40 }
41
42 struct sds *s = (struct sds *)(mem_sds-sizeof(struct sds));
43
44 //Read identifier
45 s->id = read16();
46
47 //Read value
48 s->type = read_byte();
49 s->value = read16();
50
51 write_byte('s');
52 write16(s->id);
53 write_byte('\n');
54
55 mem_sds -= sizeof(struct sds);
56 }
57
58 void sds_delete(uint8_t c)
59 {
60 struct sds *s = sds_get(c);
61
62 if(s != NULL){
63 //We found the sds, now we move verything from the end of the sds up to
64 //the pointer to the right
65 uint8_t *end = (uint8_t *)s;
66 uint8_t *start = end + sizeof(struct sds) - 1;
67 for(int i = 0; i<end-mem_sds; i++){
68 *(start - i) = *(end - i-1);
69 }
70 mem_sds += start-end+1;
71 }
72 }
73
74 bool sds_update(uint8_t id)
75 {
76 //Read identifier
77 struct sds *s = sds_get(id);
78 if(s != NULL){
79 switch(read_byte()){
80 //Long
81 case 'l':
82 //Int
83 case 'i':
84 s->value = read16();
85 break;
86 case 'b': //Bool
87 case 'c': //Character
88 case 'B': //Button
89 case 'L': //UserLED
90 s->value = read_byte();
91 break;
92 }
93 debug("Received sds update %d: %d", s->id, s->value);
94 write_byte('u');
95 write16(s->id);
96 write_byte('\n');
97 return true;
98 }
99 return false;
100 }
101
102 void sds_publish(int id)
103 {
104 struct sds *s = sds_get(id);
105 if(s != NULL){
106 debug("Publish %d=%d", s->id, s->value);
107 write_byte('p');
108 write16(s->id);
109 write_byte(s->type);
110
111 switch(s->type){
112 //Long
113 case 'l':
114 //Int
115 case 'i':
116 write16(s->value);
117 break;
118 case 'b': //Bool
119 case 'c': //Character
120 case 'B': //Button
121 case 'L': //UserLED
122 write_byte(s->value);
123 break;
124 }
125 write_byte('\n');
126 return;
127 } else {
128 debug("SDS identifier unknown: %d", id);
129 die("");
130 }
131 }
132
133 int sds_fetch(int id)
134 {
135 struct sds *s = sds_get(id);
136 if(s != NULL){
137 return s->value;
138 }
139 debug("SDS identifier unknown: %d", id);
140 die("");
141 return 0;
142 }
143
144 void sds_store(int id, int val)
145 {
146 struct sds *s = sds_get(id);
147 if(s != NULL){
148 s->value = val;
149 } else {
150 debug("SDS identifier unknown: %d", id);
151 die("");
152 }
153 }