forked from BulldogDrummond/aldl
/
aldldata.c
407 lines (348 loc) · 10.6 KB
/
aldldata.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <limits.h>
#include "serio.h"
#include "config.h"
#include "error.h"
#include "config.h"
#include "aldl-io.h"
#include "useful.h"
/************ SCOPE *********************************
This object contains all of the functions used for
structuring and parsing the ALDL data structures,
including locking and serializing of record retr.
****************************************************/
/* -------- globalstuffs ------------------ */
/* locking */
typedef enum aldl_lock {
LOCK_CONNSTATE = 0,
LOCK_RECORDPTR = 1,
LOCK_STATS = 2,
LOCK_COMQ = 3,
N_LOCKS = 4
} aldl_lock_t;
pthread_mutex_t *aldllock;
timespec_t firstrecordtime; /* timestamp used to calc. relative time */
/* primary memory pool for record storage */
aldl_record_t *recordbuffer; /* circular pool for records */
aldl_data_t *databuffer; /* circular pool for data */
unsigned int indexbuffer; /* index for both of above */
/* linked list forming a FIFO queue of commands */
aldl_comq_t *comq;
/* --------- local function decl. ---------------- */
/* update the value in the record from definition n */
aldl_data_t *aldl_parse_def(aldl_conf_t *aldl, aldl_record_t *r, int n);
/* allocate record and timestamp it */
aldl_record_t *aldl_create_record(aldl_conf_t *aldl);
/* link a prepared record to the linked list */
void link_record(aldl_record_t *rec, aldl_conf_t *aldl);
/* fill a prepared record with data */
aldl_record_t *aldl_fill_record(aldl_conf_t *aldl, aldl_record_t *rec);
/* set and unset locks, wrapper with error checking for pthread funcs */
inline void set_lock(aldl_lock_t lock_number);
inline void unset_lock(aldl_lock_t lock_number);
/* allocate memory pool */
void aldl_alloc_pool(aldl_conf_t *aldl);
/* --------------------------------------------------------- */
void init_locks() {
int x;
int pthreaderr;
aldllock = smalloc(sizeof(pthread_mutex_t) * N_LOCKS);
for(x=0;x<N_LOCKS;x++) {
pthreaderr = pthread_mutex_init(&aldllock[x],NULL);
if(pthreaderr != 0) error(1,ERROR_LOCK,
"error initializing lock %i, pthread error %i",x,pthreaderr);
}
}
inline void set_lock(aldl_lock_t lock_number) {
int rtval;
rtval = pthread_mutex_lock(&aldllock[lock_number]);
if(rtval != 0) error(1,ERROR_LOCK,
"error setting lock %i, pthread error code %i",lock_number,rtval);
}
inline void unset_lock(aldl_lock_t lock_number) {
int rtval;
rtval = pthread_mutex_unlock(&aldllock[lock_number]);
if(rtval != 0) error(1,ERROR_LOCK,
"error unsetting lock %i, pthread error code %i",lock_number,rtval);
}
void lock_stats() {
set_lock(LOCK_STATS);
}
void unlock_stats() {
unset_lock(LOCK_STATS);
}
aldl_record_t *process_data(aldl_conf_t *aldl) {
aldl_record_t *rec = aldl_create_record(aldl);
aldl_fill_record(aldl,rec);
link_record(rec,aldl);
return rec;
}
void link_record(aldl_record_t *rec, aldl_conf_t *aldl) {
rec->next = NULL; /* terminate linked list */
rec->prev = aldl->r; /* previous link */
set_lock(LOCK_RECORDPTR);
aldl->r->next = rec; /* attach to linked list */
aldl->r = rec; /* fix master link */
unset_lock(LOCK_RECORDPTR);
}
void aldl_data_init(aldl_conf_t *aldl) {
aldl_alloc_pool(aldl);
aldl_record_t *rec = aldl_create_record(aldl);
set_lock(LOCK_RECORDPTR);
rec->next = NULL;
rec->prev = NULL;
aldl->r = rec;
unset_lock(LOCK_RECORDPTR);
firstrecordtime = get_time();
comq = NULL; /* no records yet */
}
aldl_record_t *aldl_create_record(aldl_conf_t *aldl) {
/* get memory pool addresses */
aldl_record_t *rec = &recordbuffer[indexbuffer];
rec->data = &databuffer[indexbuffer * aldl->n_defs];
/* advance pool index (for next time around) */
if(indexbuffer > aldl->bufsize - 2) { /* end of buffer */
indexbuffer = 0; /* return to beginning */
} else {
indexbuffer++;
}
/* timestamp record */
rec->t = get_elapsed_ms(firstrecordtime);
#ifdef TIMESTAMP_WRAPAROUND
/* handle wraparound if we're 100 seconds before time limit */
if(rec->t > ULONG_MAX - 100000) firstrecordtime = get_time();
#endif
return rec;
}
aldl_record_t *aldl_fill_record(aldl_conf_t *aldl, aldl_record_t *rec) {
/* process packet data */
int def_n;
for(def_n=0;def_n<aldl->n_defs;def_n++) {
aldl_parse_def(aldl,rec,def_n);
}
return rec;
}
aldl_data_t *aldl_parse_def(aldl_conf_t *aldl, aldl_record_t *r, int n) {
/* check for out of range */
if(n < 0 || n > aldl->n_defs - 1) error(1,ERROR_RANGE,
"def number %i is out of range",n);
aldl_define_t *def = &aldl->def[n]; /* shortcut to definition */
int id = def->packet; /* packet id (array index) */
aldl_packetdef_t *pkt = &aldl->comm->packet[id]; /* ptr to packet */
/* location of actual data byte */
byte *data = pkt->data + def->offset + pkt->offset;
/* location for output of data, matches definition array index ... */
aldl_data_t *out = &r->data[n];
/* location for input of data */
unsigned int x;
/* convert input bit size into unsigned integer */
switch(def->size) {
case 16:
/* turn two 8 bit bytes into a 16 bit int */
x = (unsigned int)((*data<<8)|*(data+1));
break;
case 8: /* direct conversion */
default: /* no other types supported, default to 8 bit */
x = (unsigned int)*data;
}
/* apply any math or whatever and output as desired type */
switch(def->type) {
case ALDL_INT:
out->i = ( (int)x * def->multiplier.i ) + def->adder.i;
if(aldl->minmax == 1) {
out->i = rf_clamp_int(def->min.i,def->max.i,out->i);
}
break;
case ALDL_FLOAT:
out->f = ( (float)x * def->multiplier.f ) + def->adder.f;
if(aldl->minmax == 1) {
out->f = rf_clamp_float(def->min.f,def->max.f,out->f);
}
break;
case ALDL_BOOL:
if(aldl->comm->byteorder == 1) { /* deal with MSB or LSB */
out->i = getbit(x,(7 - def->binary),def->invert);
} else {
out->i = getbit(x,def->binary,def->invert);
}
break;
default:
error(1,ERROR_RANGE,"invalid type spec: %i",def->type);
}
return out;
}
aldl_state_t get_connstate(aldl_conf_t *aldl) {
set_lock(LOCK_CONNSTATE);
aldl_state_t st = aldl->state;
unset_lock(LOCK_CONNSTATE);
return st;
}
void set_connstate(aldl_state_t s, aldl_conf_t *aldl) {
set_lock(LOCK_CONNSTATE);
#ifdef DEBUGSTRUCT
printf("set connection state to %i (%s)\n",s,get_state_string(s));
#endif
aldl->state = s;
unset_lock(LOCK_CONNSTATE);
}
aldl_record_t *newest_record(aldl_conf_t *aldl) {
aldl_record_t *rec = NULL;
set_lock(LOCK_RECORDPTR);
rec = aldl->r;
unset_lock(LOCK_RECORDPTR);
return rec;
}
aldl_record_t *newest_record_wait(aldl_conf_t *aldl, aldl_record_t *rec) {
aldl_record_t *next = NULL;
while(1) {
next = newest_record(aldl);
if(next != rec) {
return next;
} else if(get_connstate(aldl) > 10) {
return NULL;
} else {
#ifndef AGGRESSIVE
usleep(500);
#endif
}
}
}
aldl_record_t *next_record_wait(aldl_conf_t *aldl, aldl_record_t *rec) {
aldl_record_t *next = NULL;
while(next == NULL) {
next = next_record(rec);
if(get_connstate(aldl) > 10) return NULL;
#ifndef AGGRESSIVE
usleep(500); /* throttling ... */
#endif
}
return next;
}
aldl_record_t *next_record_waitf(aldl_conf_t *aldl, aldl_record_t *rec) {
aldl_record_t *next = NULL;
while((next = next_record_wait(aldl,rec)) == NULL) usleep(500);
return next;
}
aldl_record_t *newest_record_waitf(aldl_conf_t *aldl, aldl_record_t *rec) {
aldl_record_t *next = NULL;
while((next = newest_record_wait(aldl,rec)) == NULL) usleep(500);
return next;
}
aldl_record_t *next_record(aldl_record_t *rec) {
#ifdef DEBUGSTRUCT
/* check for underrun ... */
if(rec->prev == NULL) {
error(1,ERROR_BUFFER,"underrun in record retrieve %p",rec);
}
#endif
aldl_record_t *next;
set_lock(LOCK_RECORDPTR);
next = rec->next;
unset_lock(LOCK_RECORDPTR);
return next;
}
void pause_until_connected(aldl_conf_t *aldl) {
while(get_connstate(aldl) > 10) {
#ifdef AGGRESSIVE
usleep(100);
#else
msleep(100);
#endif
}
}
void pause_until_buffered(aldl_conf_t *aldl) {
while(aldl->ready ==0) {
#ifdef AGGRESSIVE
usleep(100);
#else
msleep(100);
#endif
}
}
int get_index_by_name(aldl_conf_t *aldl, char *name) {
int x;
for(x=0;x<aldl->n_defs;x++) {
if(rf_strcmp(name,aldl->def[x].name) == 1) return x;
}
return -1; /* not found */
}
char *get_state_string(aldl_state_t s) {
switch(s) {
case ALDL_CONNECTED:
return "Connected";
case ALDL_CONNECTING:
return "Connecting";
case ALDL_LOADING:
return "Loading";
case ALDL_DESYNC:
return "Lost Sync";
case ALDL_ERROR:
return "Error";
case ALDL_LAGGY:
return "Laggy";
case ALDL_QUIT:
return "Quit";
case ALDL_PAUSE:
return "Paused";
case ALDL_SERIALERROR:
return "Serial ERR";
default:
return "Undefined";
}
}
void aldl_alloc_pool(aldl_conf_t *aldl) {
/* get sizes */
size_t databuffer_size = sizeof(aldl_data_t) * aldl->n_defs * aldl->bufsize;
size_t recordbuffer_size = sizeof(aldl_record_t) * aldl->bufsize;
/* alloc */
databuffer = smalloc(databuffer_size);
recordbuffer = smalloc(recordbuffer_size);
indexbuffer = 0; /* start at ptr 0 */
/* optional print sizes */
#ifdef DEBUGMEM
printf("aldldata.c Circular Buffer: BUF=%u Recs, DATA=%uKb REC=%uKb\n",
aldl->bufsize, (unsigned int)databuffer_size/1024,
(unsigned int)recordbuffer_size/1024);
#endif
}
void aldl_add_command(byte *command, byte length, int delay) {
if(command == NULL) return;
/* build new command */
aldl_comq_t *n; /* new command */
n = smalloc(sizeof(aldl_comq_t));
n->length = length;
n->delay = delay;
n->command = smalloc(sizeof(char) * length);
int x; for(x=0;x<length;x++) { /* copy command */
n->command[x] = command[x];
}
n->next = NULL;
/* link in new command */
set_lock(LOCK_COMQ);
if(comq == NULL) { /* no other commands exist */
comq = n;
} else {
aldl_comq_t *e = comq; /* end of linked list */
while(e->next != NULL) e = e->next; /* seek end */
e->next = n;
}
unset_lock(LOCK_COMQ);
}
aldl_comq_t *aldl_get_command() {
set_lock(LOCK_COMQ);
if(comq == NULL) {
unset_lock(LOCK_COMQ);
return NULL; /* no command available */
}
aldl_comq_t *c = comq;
comq = c->next; /* advance to next command */
unset_lock(LOCK_COMQ);
return c;
/* WARNING you need to free this after you're done with it ... */
}