gusimplewhiteboard
wb_data_logger.c
Go to the documentation of this file.
1/*
2 * file wb_data_logger.c
3 *
4 * This file was generated by classgenerator from data_logger.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Dimitri Joukoff. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 *
24 * This product includes software developed by Dimitri Joukoff.
25 *
26 * 4. Neither the name of the author nor the names of contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * -----------------------------------------------------------------------
43 * This program is free software; you can redistribute it and/or
44 * modify it under the above terms or under the terms of the GNU
45 * General Public License as published by the Free Software Foundation;
46 * either version 2 of the License, or (at your option) any later version.
47 *
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, see http://www.gnu.org/licenses/
55 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
56 * Fifth Floor, Boston, MA 02110-1301, USA.
57 *
58 */
59
60#ifndef WHITEBOARD_POSTER_STRING_CONVERSION
61#define WHITEBOARD_POSTER_STRING_CONVERSION
62#endif // WHITEBOARD_POSTER_STRING_CONVERSION
63
64#include "wb_data_logger.h"
65#include <stdio.h>
66#include <string.h>
67#include <stdlib.h>
68#include <ctype.h>
69#include <limits.h>
70
71/* Network byte order functions */
72#pragma clang diagnostic push
73#pragma clang diagnostic ignored "-Wunused-macros"
74#if defined(__linux)
75# include <endian.h>
76# include <byteswap.h>
77#elif defined(__APPLE__)
78# include <machine/endian.h> //Needed for __BYTE_ORDER
79# include <architecture/byte_order.h> //Needed for byte swap functions
80# define bswap_16(x) NXSwapShort(x)
81# define bswap_32(x) NXSwapInt(x)
82# define bswap_64(x) NXSwapLongLong(x)
83#elif defined(ESP8266)
84# define bswap_16(x) __builtin_bswap16(x)
85# define bswap_32(x) __builtin_bswap32(x)
86# define bswap_64(x) __builtin_bswap64(x)
87#else
88 //Manually define swap macros?
89#endif
90
91#if (!defined(__BYTE_ORDER) && !defined(__LITTLE_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN)
92# if !defined(htonll) && !defined(ntohll)
93# define htonll(x) bswap_64(x)
94# define ntohll(x) bswap_64(x)
95# endif
96# if !defined(htonl) && !defined(ntohl)
97# define htonl(x) bswap_32(x)
98# define ntohl(x) bswap_32(x)
99# endif
100# if !defined(htons) && !defined(ntohs)
101# define htons(x) bswap_16(x)
102# define ntohs(x) bswap_16(x)
103# endif
104#else
105# if !defined(htonll) && !defined(ntohll)
106# define htonll(x) (x)
107# define ntohll(x) (x)
108# endif
109# if !defined(htonl) && !defined(ntohl)
110# define htonl(x) (x)
111# define ntohl(x) (x)
112# endif
113# if !defined(htons) && !defined(ntohs)
114# define htons(x) (x)
115# define ntohs(x) (x)
116# endif
117#endif
118#pragma clang diagnostic pop
119
120
121
125const char* wb_data_logger_description(const struct wb_data_logger* self, char* descString, size_t bufferSize)
126{
127#pragma clang diagnostic push
128#pragma clang diagnostic ignored "-Wunused-variable"
129 size_t len = 0;
130 if (len >= bufferSize) {
131 return descString;
132 }
133 len += snprintf(descString + len, bufferSize - len, "machineName=%s", self->machineName);
134 if (len >= bufferSize) {
135 return descString;
136 }
137 len = gu_strlcat(descString, ", ", bufferSize);
138 if (len >= bufferSize) {
139 return descString;
140 }
141 len += snprintf(descString + len, bufferSize - len, "currentState=%u", self->currentState);
142 if (len >= bufferSize) {
143 return descString;
144 }
145 len = gu_strlcat(descString, ", ", bufferSize);
146 if (len >= bufferSize) {
147 return descString;
148 }
149 len += snprintf(descString + len, bufferSize - len, "currentSection=%u", self->currentSection);
150 if (len >= bufferSize) {
151 return descString;
152 }
153 len = gu_strlcat(descString, ", ", bufferSize);
154 if (len >= bufferSize) {
155 return descString;
156 }
157 len += snprintf(descString + len, bufferSize - len, "dataSet=%u", self->dataSet);
158 if (len >= bufferSize) {
159 return descString;
160 }
161 len = gu_strlcat(descString, ", ", bufferSize);
162 if (len >= bufferSize) {
163 return descString;
164 }
165 len = gu_strlcat(descString, self->loggerRunning ? "loggerRunning=true" : "loggerRunning=false", bufferSize);
166 if (len >= bufferSize) {
167 return descString;
168 }
169 len = gu_strlcat(descString, ", ", bufferSize);
170 if (len >= bufferSize) {
171 return descString;
172 }
173 len = gu_strlcat(descString, self->shouldExit ? "shouldExit=true" : "shouldExit=false", bufferSize);
174 if (len >= bufferSize) {
175 return descString;
176 }
177 len = gu_strlcat(descString, ", ", bufferSize);
178 if (len >= bufferSize) {
179 return descString;
180 }
181 len += snprintf(descString + len, bufferSize - len, "comment=%s", self->comment);
182 return descString;
183#pragma clang diagnostic pop
184}
185
189const char* wb_data_logger_to_string(const struct wb_data_logger* self, char* toString, size_t bufferSize)
190{
191#pragma clang diagnostic push
192#pragma clang diagnostic ignored "-Wunused-variable"
193 size_t len = 0;
194 if (len >= bufferSize) {
195 return toString;
196 }
197 len += snprintf(toString + len, bufferSize - len, "%s", self->machineName);
198 if (len >= bufferSize) {
199 return toString;
200 }
201 len = gu_strlcat(toString, ", ", bufferSize);
202 if (len >= bufferSize) {
203 return toString;
204 }
205 len += snprintf(toString + len, bufferSize - len, "%u", self->currentState);
206 if (len >= bufferSize) {
207 return toString;
208 }
209 len = gu_strlcat(toString, ", ", bufferSize);
210 if (len >= bufferSize) {
211 return toString;
212 }
213 len += snprintf(toString + len, bufferSize - len, "%u", self->currentSection);
214 if (len >= bufferSize) {
215 return toString;
216 }
217 len = gu_strlcat(toString, ", ", bufferSize);
218 if (len >= bufferSize) {
219 return toString;
220 }
221 len += snprintf(toString + len, bufferSize - len, "%u", self->dataSet);
222 if (len >= bufferSize) {
223 return toString;
224 }
225 len = gu_strlcat(toString, ", ", bufferSize);
226 if (len >= bufferSize) {
227 return toString;
228 }
229 len = gu_strlcat(toString, self->loggerRunning ? "true" : "false", bufferSize);
230 if (len >= bufferSize) {
231 return toString;
232 }
233 len = gu_strlcat(toString, ", ", bufferSize);
234 if (len >= bufferSize) {
235 return toString;
236 }
237 len = gu_strlcat(toString, self->shouldExit ? "true" : "false", bufferSize);
238 if (len >= bufferSize) {
239 return toString;
240 }
241 len = gu_strlcat(toString, ", ", bufferSize);
242 if (len >= bufferSize) {
243 return toString;
244 }
245 len += snprintf(toString + len, bufferSize - len, "%s", self->comment);
246 return toString;
247#pragma clang diagnostic pop
248}
249
253struct wb_data_logger* wb_data_logger_from_string(struct wb_data_logger* self, const char* str)
254{
255 size_t temp_length = strlen(str);
256 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
257 if (length < 1 || length > DATA_LOGGER_DESC_BUFFER_SIZE) {
258 return self;
259 }
260 char var_str_buffer[DATA_LOGGER_DESC_BUFFER_SIZE + 1];
261 char* var_str = &var_str_buffer[0];
262 char key_buffer[15];
263 char* key = &key_buffer[0];
264 int bracecount = 0;
265 int startVar = 0;
266 int index = 0;
267 int startKey = 0;
268 int endKey = -1;
269 int varIndex = 0;
270 if (index == 0 && str[0] == '{') {
271 index = 1;
272 }
273 startVar = index;
274 startKey = startVar;
275 do {
276 for (int i = index; i < length; i++) {
277 index = i + 1;
278 if (bracecount == 0 && str[i] == '=') {
279 endKey = i - 1;
280 startVar = index;
281 continue;
282 }
283 if (bracecount == 0 && isspace(str[i])) {
284 startVar = index;
285 if (endKey == -1) {
286 startKey = index;
287 }
288 continue;
289 }
290 if (bracecount == 0 && str[i] == ',') {
291 index = i - 1;
292 break;
293 }
294 if (str[i] == '{') {
295 bracecount++;
296 continue;
297 }
298 if (str[i] == '}') {
299 bracecount--;
300 if (bracecount < 0) {
301 index = i - 1;
302 break;
303 }
304 }
305 if (i == length - 1) {
306 index = i;
307 }
308 }
309 if (endKey >= startKey && endKey - startKey < length) {
310 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
311 key[(endKey - startKey) + 1] = 0;
312 } else {
313 key[0] = 0;
314 }
315 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
316 var_str[(index - startVar) + 1] = 0;
317 bracecount = 0;
318 index += 2;
319 startVar = index;
320 startKey = startVar;
321 endKey = -1;
322 if (strlen(key) > 0) {
323 if (0 == strcmp("machineName", key)) {
324 varIndex = 0;
325 } else if (0 == strcmp("currentState", key)) {
326 varIndex = 1;
327 } else if (0 == strcmp("currentSection", key)) {
328 varIndex = 2;
329 } else if (0 == strcmp("dataSet", key)) {
330 varIndex = 3;
331 } else if (0 == strcmp("loggerRunning", key)) {
332 varIndex = 4;
333 } else if (0 == strcmp("shouldExit", key)) {
334 varIndex = 5;
335 } else if (0 == strcmp("comment", key)) {
336 varIndex = 6;
337 } else {
338 varIndex = -1;
339 }
340 }
341 switch (varIndex) {
342 case -1: { break; }
343 case 0:
344 {
345 strncpy(self->machineName, var_str, 40);
346 break;
347 }
348 case 1:
349 {
350 self->currentState = ((uint32_t)atoi(var_str));
351 break;
352 }
353 case 2:
354 {
355 self->currentSection = ((uint32_t)atoi(var_str));
356 break;
357 }
358 case 3:
359 {
360 self->dataSet = ((uint8_t)atoi(var_str));
361 break;
362 }
363 case 4:
364 {
365 self->loggerRunning = strcmp(var_str, "true") == 0 || strcmp(var_str, "1") == 0;
366 break;
367 }
368 case 5:
369 {
370 self->shouldExit = strcmp(var_str, "true") == 0 || strcmp(var_str, "1") == 0;
371 break;
372 }
373 case 6:
374 {
375 strncpy(self->comment, var_str, 30);
376 break;
377 }
378 }
379 if (varIndex >= 0) {
380 varIndex++;
381 }
382 } while(index < length);
383 return self;
384}
385
386/*#ifdef WHITEBOARD_SERIALISATION*/
387
391size_t wb_data_logger_to_network_serialised(const struct wb_data_logger *self, char *dst)
392{
393 uint16_t bit_offset = 0;
394 do { //limit declaration scope
395 uint8_t len = (uint8_t) strlen(self->machineName);
396 int8_t b;
397 for (b = 7; b >= 0; b--) {
398 do {
399 uint16_t byte = bit_offset / 8;
400 uint16_t bit = 7 - (bit_offset % 8);
401 unsigned long newbit = !!((len >> b) & 1U);
402 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
403 bit_offset = bit_offset + 1;
404 } while(false);
405 }
406 uint8_t c;
407 for (c = 0; c < len; c++) {
408 for (b = 7; b >= 0; b--) {
409 do {
410 uint16_t byte = bit_offset / 8;
411 uint16_t bit = 7 - (bit_offset % 8);
412 unsigned long newbit = !!((self->machineName[c] >> b) & 1U);
413 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
414 bit_offset = bit_offset + 1;
415 } while(false);
416 }
417 }
418 } while(false);
419
420 uint32_t currentState_nbo = htonl(self->currentState);
421 do {
422 int8_t b;
423 for (b = (32 - 1); b >= 0; b--) {
424 do {
425 uint16_t byte = bit_offset / 8;
426 uint16_t bit = 7 - (bit_offset % 8);
427 unsigned long newbit = !!((currentState_nbo >> b) & 1U);
428 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
429 bit_offset = bit_offset + 1;
430 } while(false);
431 }
432 } while(false);
433
434 uint32_t currentSection_nbo = htonl(self->currentSection);
435 do {
436 int8_t b;
437 for (b = (32 - 1); b >= 0; b--) {
438 do {
439 uint16_t byte = bit_offset / 8;
440 uint16_t bit = 7 - (bit_offset % 8);
441 unsigned long newbit = !!((currentSection_nbo >> b) & 1U);
442 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
443 bit_offset = bit_offset + 1;
444 } while(false);
445 }
446 } while(false);
447
448 uint8_t dataSet_nbo = (self->dataSet);
449 do {
450 int8_t b;
451 for (b = (8 - 1); b >= 0; b--) {
452 do {
453 uint16_t byte = bit_offset / 8;
454 uint16_t bit = 7 - (bit_offset % 8);
455 unsigned long newbit = !!((dataSet_nbo >> b) & 1U);
456 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
457 bit_offset = bit_offset + 1;
458 } while(false);
459 }
460 } while(false);
461
462 do {
463 uint16_t byte = bit_offset / 8;
464 uint16_t bit = 7 - (bit_offset % 8);
465 unsigned long newbit = !!(self->loggerRunning ? 1U : 0U);
466 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
467 bit_offset = bit_offset + 1;
468 } while(false);
469
470 do {
471 uint16_t byte = bit_offset / 8;
472 uint16_t bit = 7 - (bit_offset % 8);
473 unsigned long newbit = !!(self->shouldExit ? 1U : 0U);
474 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
475 bit_offset = bit_offset + 1;
476 } while(false);
477
478 do { //limit declaration scope
479 uint8_t len = (uint8_t) strlen(self->comment);
480 int8_t b;
481 for (b = 7; b >= 0; b--) {
482 do {
483 uint16_t byte = bit_offset / 8;
484 uint16_t bit = 7 - (bit_offset % 8);
485 unsigned long newbit = !!((len >> b) & 1U);
486 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
487 bit_offset = bit_offset + 1;
488 } while(false);
489 }
490 uint8_t c;
491 for (c = 0; c < len; c++) {
492 for (b = 7; b >= 0; b--) {
493 do {
494 uint16_t byte = bit_offset / 8;
495 uint16_t bit = 7 - (bit_offset % 8);
496 unsigned long newbit = !!((self->comment[c] >> b) & 1U);
497 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
498 bit_offset = bit_offset + 1;
499 } while(false);
500 }
501 }
502 } while(false);
503 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
504 (void)self;
505 (void)dst;
506 return bit_offset;
507}
508
512size_t wb_data_logger_from_network_serialised(const char *src, struct wb_data_logger *dst)
513{
514 uint16_t bit_offset = 0;
515 do {
516 uint8_t len = 0;
517 int8_t b;
518 for (b = 7; b >= 0; b--) {
519 do {
520 uint16_t byte = bit_offset / 8;
521 uint16_t bit = 7 - (bit_offset % 8);
522 char dataByte = src[byte];
523 unsigned char bitValue = (dataByte >> bit) & 1U;
524 len ^= (-bitValue ^ len) & (1UL << b);
525 bit_offset = bit_offset + 1;
526 } while(false);
527 }
528 uint8_t c;
529 for (c = 0; c < len; c++) {
530 for (b = 7; b >= 0; b--) {
531 do {
532 uint16_t byte = bit_offset / 8;
533 uint16_t bit = 7 - (bit_offset % 8);
534 char dataByte = src[byte];
535 unsigned char bitValue = (dataByte >> bit) & 1U;
536 dst->machineName[c] ^= (-bitValue ^ dst->machineName[c]) & (1UL << b);
537 bit_offset = bit_offset + 1;
538 } while(false);
539 }
540 }
541 } while (false);
542
543 do {
544 int8_t b;
545 for (b = (32 - 1); b >= 0; b--) {
546 do {
547 uint16_t byte = bit_offset / 8;
548 uint16_t bit = 7 - (bit_offset % 8);
549 char dataByte = src[byte];
550 unsigned char bitValue = (dataByte >> bit) & 1U;
551 dst->currentState ^= (-bitValue ^ dst->currentState) & (1UL << b);
552 bit_offset = bit_offset + 1;
553 } while(false);
554 }
555 } while(false);
556 dst->currentState = ntohl(dst->currentState);
557
558 do {
559 int8_t b;
560 for (b = (32 - 1); b >= 0; b--) {
561 do {
562 uint16_t byte = bit_offset / 8;
563 uint16_t bit = 7 - (bit_offset % 8);
564 char dataByte = src[byte];
565 unsigned char bitValue = (dataByte >> bit) & 1U;
566 dst->currentSection ^= (-bitValue ^ dst->currentSection) & (1UL << b);
567 bit_offset = bit_offset + 1;
568 } while(false);
569 }
570 } while(false);
572
573 do {
574 int8_t b;
575 for (b = (8 - 1); b >= 0; b--) {
576 do {
577 uint16_t byte = bit_offset / 8;
578 uint16_t bit = 7 - (bit_offset % 8);
579 char dataByte = src[byte];
580 unsigned char bitValue = (dataByte >> bit) & 1U;
581 dst->dataSet ^= (-bitValue ^ dst->dataSet) & (1UL << b);
582 bit_offset = bit_offset + 1;
583 } while(false);
584 }
585 } while(false);
586 dst->dataSet = (dst->dataSet);
587
588 do {
589 uint16_t byte = bit_offset / 8;
590 uint16_t bit = 7 - (bit_offset % 8);
591 char dataByte = src[byte];
592 unsigned char bitValue = (dataByte >> bit) & 1U;
593 dst->loggerRunning = bitValue != 0;
594 bit_offset = bit_offset + 1;
595 } while(false);
596
597 do {
598 uint16_t byte = bit_offset / 8;
599 uint16_t bit = 7 - (bit_offset % 8);
600 char dataByte = src[byte];
601 unsigned char bitValue = (dataByte >> bit) & 1U;
602 dst->shouldExit = bitValue != 0;
603 bit_offset = bit_offset + 1;
604 } while(false);
605
606 do {
607 uint8_t len = 0;
608 int8_t b;
609 for (b = 7; b >= 0; b--) {
610 do {
611 uint16_t byte = bit_offset / 8;
612 uint16_t bit = 7 - (bit_offset % 8);
613 char dataByte = src[byte];
614 unsigned char bitValue = (dataByte >> bit) & 1U;
615 len ^= (-bitValue ^ len) & (1UL << b);
616 bit_offset = bit_offset + 1;
617 } while(false);
618 }
619 uint8_t c;
620 for (c = 0; c < len; c++) {
621 for (b = 7; b >= 0; b--) {
622 do {
623 uint16_t byte = bit_offset / 8;
624 uint16_t bit = 7 - (bit_offset % 8);
625 char dataByte = src[byte];
626 unsigned char bitValue = (dataByte >> bit) & 1U;
627 dst->comment[c] ^= (-bitValue ^ dst->comment[c]) & (1UL << b);
628 bit_offset = bit_offset + 1;
629 } while(false);
630 }
631 }
632 } while (false);
633 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
634 (void)src;
635 (void)dst;
636 return bit_offset;
637}
638
639/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
uint8_t dataSet
Enables selection of a data set to be logged (default = 0, joints and sensors) (NYI)
bool shouldExit
Switch to cause DataLogger to exit.
bool loggerRunning
Starts/Stops logging.
char machineName[40]
Name of the machine being monitored, but could be anything.
uint32_t currentSection
Section within the state (e.g.
char comment[30]
Comment to be attached to the logged data (NYI)
uint32_t currentState
The machine's currently executing state (use integer rather than the state's name)
const char * wb_data_logger_description(const struct wb_data_logger *self, char *descString, size_t bufferSize)
Convert to a description string.
size_t wb_data_logger_to_network_serialised(const struct wb_data_logger *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
struct wb_data_logger * wb_data_logger_from_string(struct wb_data_logger *self, const char *str)
Convert from a string.
const char * wb_data_logger_to_string(const struct wb_data_logger *self, char *toString, size_t bufferSize)
Convert to a string.
size_t wb_data_logger_from_network_serialised(const char *src, struct wb_data_logger *dst)
Convert from a compressed, serialised, network byte order byte stream.
#define ntohl(x)
#define htonl(x)
#define DATA_LOGGER_DESC_BUFFER_SIZE