aboutsummaryrefslogtreecommitdiff
path: root/src/survive_config.c
blob: 23d7ef1c2c3f5e815672a016d93e62ccc5a37314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// (C) 2017 <>< Joshua Allen, Under MIT/x11 License.
#include <string.h>
#include <assert.h>
#include "survive_config.h"
#include <json_helpers.h>

#include <errno.h>

#define MAX_CONFIG_ENTRIES 100
#define MAX_LIGHTHOUSES 2



config_group global_config_values;
config_group lh_config[MAX_LIGHTHOUSES]; //lighthouse configs

//static uint16_t used_entries = 0;

//static FILE *config_file = NULL;
const FLT* config_set_float_a(config_group *cg, const char *tag, const FLT* values, uint8_t count);

void init_config_group(config_group *cg, uint16_t count) {
	uint16_t i = 0;
	cg->config_entries = malloc(count*sizeof(config_entry));
	cg->used_entries = 0;
	cg->max_entries = count;

	for (i=0;i<count;++i) {
		cg->config_entries[i].data = NULL;
		cg->config_entries[i].tag = NULL;
		cg->config_entries[i].type = CONFIG_UNKNOWN;
		cg->config_entries[i].elements = 0;
	}
}

void config_init() {
	uint16_t i = 0;
	init_config_group(&global_config_values, MAX_CONFIG_ENTRIES);
	for(i=0;i<MAX_LIGHTHOUSES;i++) {
		init_config_group(lh_config+i, 9);
	}
}


void config_set_lighthouse(struct BaseStationData* bsd, uint8_t idx) {
	config_group *cg = lh_config+idx;
	config_set_uint32(cg,"index", idx);
	config_set_uint32(cg,"id", bsd->BaseStationID);
	config_set_float_a(cg,"position", bsd->Position, 3);
	config_set_float_a(cg,"quaternion", bsd->Quaternion, 4);
	config_set_float_a(cg,"fcalphase", bsd->fcalphase, 2);
	config_set_float_a(cg,"fcaltilt", bsd->fcaltilt,2);
	config_set_float_a(cg,"fcalcurve", bsd->fcalcurve,2);
	config_set_float_a(cg,"fcalgibpha", bsd->fcalgibpha,2);
	config_set_float_a(cg,"fcalgibmag", bsd->fcalgibmag,2);
}

void sstrcpy(char** dest, const char *src) {
	uint32_t len = strlen(src)+1;
	assert(dest!=NULL);

	char* ptr = (char*)realloc(*dest, len); //acts like mallos if dest==NULL
	assert(ptr!=NULL);
	*dest = ptr;

	strcpy(*dest,src);
}

config_entry* find_config_entry(config_group *cg, const char *tag) {
	uint16_t i = 0;
	for (i=0;i < cg->used_entries;++i) {
		if ( strcmp(cg->config_entries[i].tag, tag) == 0 ) {
			return cg->config_entries+i;
		}
	}
	return NULL;
}

const char* config_read_str(config_group *cg, const char *tag, const char *def) {
	config_entry *cv = find_config_entry(cg, tag);

	if (cv != NULL) return cv->data;

	return config_set_str(cg,tag,def);
}

uint32_t config_read_uint32(config_group *cg, const char *tag, const uint32_t def) {
	config_entry *cv = find_config_entry(cg, tag);

	if (cv != NULL) return cv->numeric.i;

	return config_set_uint32(cg, tag, def);
}

FLT config_read_float(config_group *cg, const char *tag, const FLT def) {
	config_entry *cv = find_config_entry(cg, tag);

	if (cv != NULL) return cv->numeric.f;

	return config_set_float(cg, tag, def);
}

uint16_t config_read_float_array(config_group *cg, const char *tag, FLT** values, const FLT* def, uint16_t count) {
	config_entry *cv = find_config_entry(cg, tag);

	if (cv != NULL) {
		*values = (FLT*)cv->data;
		return cv->elements;
	}

	if (def == NULL) return 0;

	config_set_float_a(cg, tag, def, count);
	*values = def;
	return count;
}

config_entry* next_unused_entry(config_group *cg) {
	config_entry *cv = cg->config_entries + cg->used_entries;
	assert(cg->used_entries < cg->max_entries);

/*
	if (cg->used_entries >= cg->max_entries) {
		cg->max_entries+=10;
		cg->config_entries = realloc(cg->config_entries, sizeof(config_entry)*cg->max_entries);
	}
*/

	cg->used_entries++;
	return cv;
}

const char* config_set_str(config_group *cg, const char *tag, const char* value) {
	config_entry *cv = find_config_entry(cg, tag);
	if (cv == NULL) cv = next_unused_entry(cg);

	sstrcpy(&(cv->tag), tag);
	sstrcpy(&(cv->data), value);
	cv->type = CONFIG_STRING;

	return value;
}

const uint32_t config_set_uint32(config_group *cg, const char *tag, const uint32_t value) {
	config_entry *cv = find_config_entry(cg, tag);
	if (cv == NULL) cv = next_unused_entry(cg);

	sstrcpy(&(cv->tag), tag);
	cv->numeric.i = value;
	cv->type = CONFIG_UINT32;

	return value;
}

const FLT config_set_float(config_group *cg, const char *tag, const FLT value) {
	config_entry *cv = find_config_entry(cg, tag);
	if (cv == NULL) cv = next_unused_entry(cg);

	sstrcpy(&(cv->tag), tag);
	cv->numeric.f = value;
	cv->type = CONFIG_FLOAT;

	return value;
}

const FLT* config_set_float_a(config_group *cg, const char *tag, const FLT* values, uint8_t count) {
	config_entry *cv = find_config_entry(cg, tag);
	if (cv == NULL) cv = next_unused_entry(cg);

	sstrcpy(&(cv->tag), tag);

	char* ptr = (char*)realloc(cv->data, sizeof(FLT)*count);
	assert(ptr!=NULL);
	cv->data = ptr;

	printf("float array\n");

	memcpy(cv->data,values,sizeof(FLT)*count);
	cv->type = CONFIG_FLOAT_ARRAY;
	cv->elements = count;

	return values;
}

void _json_write_float_array(FILE* f, const char* tag, FLT* v, uint8_t count) {
	#ifdef USE_DOUBLE
		json_write_double_array(f,tag,v,count);
	#else
		json_write_float_array(f,tag,v,count);
	#endif
}

void write_config_group(FILE* f, config_group *cg, char *tag) {
	uint16_t i = 0;

	if (tag != NULL) {
		fprintf(f, "\"%s\":{\n", tag);
	}

	for (i=0;i < cg->used_entries;++i) {
		if (cg->config_entries[i].type == CONFIG_FLOAT) {
			json_write_float(f, cg->config_entries[i].tag, cg->config_entries[i].numeric.f);
		} else if (cg->config_entries[i].type == CONFIG_UINT32) {
			json_write_uint32(f, cg->config_entries[i].tag, cg->config_entries[i].numeric.i);
		} else if (cg->config_entries[i].type == CONFIG_STRING) {
			json_write_str(f, cg->config_entries[i].tag, cg->config_entries[i].data);
		} else if (cg->config_entries[i].type == CONFIG_FLOAT_ARRAY) {
			_json_write_float_array(f, cg->config_entries[i].tag, (FLT*)cg->config_entries[i].data, cg->config_entries[i].elements);
		}
		if ((i+1) < cg->used_entries) fprintf(f,",");
		fprintf(f,"\n");
	};

	if (tag != NULL) {
		fprintf(f,"}\n");
	}
}

void config_save(const char* path) {
	uint16_t i = 0;

	FILE* f = fopen(path, "w");

	write_config_group(f,&global_config_values, NULL);
	write_config_group(f,lh_config, "lighthouse0");
	write_config_group(f,lh_config+1, "lighthouse1");

	fclose(f);
}

void print_json_value(char* tag, char** values, uint16_t count) {
	uint16_t i = 0;
	for (i=0;i<count; ++i) {
		printf("%s:%s \n", tag, values[i]);
	}
}

config_group* cg_stack[10]; //handle 10 nested objects deep
uint8_t cg_stack_head = 0;

void handle_config_group(char* tag) {
	cg_stack_head++;
	if (strcmp("lighthouse0",tag) == 0) {
		cg_stack[cg_stack_head] = lh_config;
	} else if (strcmp("lighthouse1",tag) == 0) {
		cg_stack[cg_stack_head] = lh_config+1;
	} else {
		cg_stack[cg_stack_head] = &global_config_values;
	}
}

void pop_config_group() {
	cg_stack_head--;
}


int parse_floats(char* tag, char** values, uint16_t count) {
	uint16_t i = 0;
	FLT f[count];
	char* end = NULL;
	config_group* cg = cg_stack[cg_stack_head];

	for(i=0;i<count;++i) {

		#ifdef USE_DOUBLE
		f[i] = strtod(values[i], &end);
		#else
		f[i] = strtof(values[i], &end);
		#endif

//		if (values[i] == end) return 0; //not a float
		if (*end != '\0') return 0; //not an integer
	}

	if (count>1) {
		config_set_float_a(cg, tag, f, count);
	}
	else {
		config_set_float(cg, tag, f[0]);
	}

	return 1;
}

int parse_uint32(char* tag, char** values, uint16_t count) {
	uint16_t i = 0;
	uint32_t l[count];
	char* end = NULL;
	config_group* cg = cg_stack[cg_stack_head];

/*
	//look for non numeric values
	for(end=values[0];*end!='\0';++end) {
		if ((*end<48) || (*end>57)) return 0;
	}

	end=NULL;
*/
	for(i=0;i<count;++i) {
		l[i] = strtol(values[i], &end, 10);
//		if (values[i] == end) return 0; //not an integer
		if (*end != '\0') return 0; //not an integer
	}

//	if (count>1)
//		config_set_uint32_array(cg, tag, f, count);
//	else
		config_set_uint32(cg, tag, l[0]);

	return 1;
}

void handle_tag_value(char* tag, char** values, uint16_t count) {
	print_json_value(tag,values,count);
	config_group* cg = cg_stack[cg_stack_head];

	if (parse_uint32(tag,values,count) > 0) return; //parse integers first, stricter rules

	if (parse_floats(tag,values,count) > 0) return;

	//should probably also handle string arrays
	config_set_str(cg,tag,values[0]);
//	else if (count>1) config_set_str
}

void config_read(const char* path) {
	json_begin_object = handle_config_group;
	json_end_object = pop_config_group;
	json_tag_value = handle_tag_value;

	cg_stack[0] = &global_config_values;

	json_load_file(path);

	json_begin_object = NULL;
	json_end_object = NULL;
	json_tag_value = NULL;
}