gusimplewhiteboard
wb_landmark_sighting.c
Go to the documentation of this file.
1/*
2 * file wb_landmark_sighting.c
3 *
4 * This file was generated by classgenerator from landmark_sighting.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Callum McColl. 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 Callum McColl.
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
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_landmark_sighting_description(const struct wb_landmark_sighting* 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, "direction=%d", self->direction);
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, "distance=%u", self->distance);
142 if (len >= bufferSize) {
143 return descString;
144 }
145 len = gu_strlcat(descString, ", ", bufferSize);
146 if (len >= bufferSize) {
147 return descString;
148 }
149 switch (self->sightingType) {
150 case BallSightingType:
151 {
152 len += snprintf(descString + len, bufferSize - len, "sightingType=BallSightingType");
153 break;
154 }
156 {
157 len += snprintf(descString + len, bufferSize - len, "sightingType=CornerHorizonSightingType");
158 break;
159 }
161 {
162 len += snprintf(descString + len, bufferSize - len, "sightingType=CornerLineSightingType");
163 break;
164 }
166 {
167 len += snprintf(descString + len, bufferSize - len, "sightingType=CrossLineSightingType");
168 break;
169 }
171 {
172 len += snprintf(descString + len, bufferSize - len, "sightingType=GenericGoalPostSightingType");
173 break;
174 }
176 {
177 len += snprintf(descString + len, bufferSize - len, "sightingType=GoalLandmarkSightingType");
178 break;
179 }
181 {
182 len += snprintf(descString + len, bufferSize - len, "sightingType=LeftGoalPostSightingType");
183 break;
184 }
186 {
187 len += snprintf(descString + len, bufferSize - len, "sightingType=LineHorizonSightingType");
188 break;
189 }
191 {
192 len += snprintf(descString + len, bufferSize - len, "sightingType=RightGoalPostSightingType");
193 break;
194 }
196 {
197 len += snprintf(descString + len, bufferSize - len, "sightingType=StraightLineSightingType");
198 break;
199 }
201 {
202 len += snprintf(descString + len, bufferSize - len, "sightingType=TIntersectionLineSightingType");
203 break;
204 }
205 }
206 return descString;
207#pragma clang diagnostic pop
208}
209
213const char* wb_landmark_sighting_to_string(const struct wb_landmark_sighting* self, char* toString, size_t bufferSize)
214{
215#pragma clang diagnostic push
216#pragma clang diagnostic ignored "-Wunused-variable"
217 size_t len = 0;
218 if (len >= bufferSize) {
219 return toString;
220 }
221 len += snprintf(toString + len, bufferSize - len, "%d", self->direction);
222 if (len >= bufferSize) {
223 return toString;
224 }
225 len = gu_strlcat(toString, ", ", bufferSize);
226 if (len >= bufferSize) {
227 return toString;
228 }
229 len += snprintf(toString + len, bufferSize - len, "%u", self->distance);
230 if (len >= bufferSize) {
231 return toString;
232 }
233 len = gu_strlcat(toString, ", ", bufferSize);
234 if (len >= bufferSize) {
235 return toString;
236 }
237 switch (self->sightingType) {
238 case BallSightingType:
239 {
240 len += snprintf(toString + len, bufferSize - len, "BallSightingType");
241 break;
242 }
244 {
245 len += snprintf(toString + len, bufferSize - len, "CornerHorizonSightingType");
246 break;
247 }
249 {
250 len += snprintf(toString + len, bufferSize - len, "CornerLineSightingType");
251 break;
252 }
254 {
255 len += snprintf(toString + len, bufferSize - len, "CrossLineSightingType");
256 break;
257 }
259 {
260 len += snprintf(toString + len, bufferSize - len, "GenericGoalPostSightingType");
261 break;
262 }
264 {
265 len += snprintf(toString + len, bufferSize - len, "GoalLandmarkSightingType");
266 break;
267 }
269 {
270 len += snprintf(toString + len, bufferSize - len, "LeftGoalPostSightingType");
271 break;
272 }
274 {
275 len += snprintf(toString + len, bufferSize - len, "LineHorizonSightingType");
276 break;
277 }
279 {
280 len += snprintf(toString + len, bufferSize - len, "RightGoalPostSightingType");
281 break;
282 }
284 {
285 len += snprintf(toString + len, bufferSize - len, "StraightLineSightingType");
286 break;
287 }
289 {
290 len += snprintf(toString + len, bufferSize - len, "TIntersectionLineSightingType");
291 break;
292 }
293 }
294 return toString;
295#pragma clang diagnostic pop
296}
297
302{
303 size_t temp_length = strlen(str);
304 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
305 if (length < 1 || length > LANDMARK_SIGHTING_DESC_BUFFER_SIZE) {
306 return self;
307 }
308 char var_str_buffer[LANDMARK_SIGHTING_DESC_BUFFER_SIZE + 1];
309 char* var_str = &var_str_buffer[0];
310 char key_buffer[13];
311 char* key = &key_buffer[0];
312 int bracecount = 0;
313 int startVar = 0;
314 int index = 0;
315 int startKey = 0;
316 int endKey = -1;
317 int varIndex = 0;
318 if (index == 0 && str[0] == '{') {
319 index = 1;
320 }
321 startVar = index;
322 startKey = startVar;
323 do {
324 for (int i = index; i < length; i++) {
325 index = i + 1;
326 if (bracecount == 0 && str[i] == '=') {
327 endKey = i - 1;
328 startVar = index;
329 continue;
330 }
331 if (bracecount == 0 && isspace(str[i])) {
332 startVar = index;
333 if (endKey == -1) {
334 startKey = index;
335 }
336 continue;
337 }
338 if (bracecount == 0 && str[i] == ',') {
339 index = i - 1;
340 break;
341 }
342 if (str[i] == '{') {
343 bracecount++;
344 continue;
345 }
346 if (str[i] == '}') {
347 bracecount--;
348 if (bracecount < 0) {
349 index = i - 1;
350 break;
351 }
352 }
353 if (i == length - 1) {
354 index = i;
355 }
356 }
357 if (endKey >= startKey && endKey - startKey < length) {
358 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
359 key[(endKey - startKey) + 1] = 0;
360 } else {
361 key[0] = 0;
362 }
363 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
364 var_str[(index - startVar) + 1] = 0;
365 bracecount = 0;
366 index += 2;
367 startVar = index;
368 startKey = startVar;
369 endKey = -1;
370 if (strlen(key) > 0) {
371 if (0 == strcmp("direction", key)) {
372 varIndex = 0;
373 } else if (0 == strcmp("distance", key)) {
374 varIndex = 1;
375 } else if (0 == strcmp("sightingType", key)) {
376 varIndex = 2;
377 } else {
378 varIndex = -1;
379 }
380 }
381 switch (varIndex) {
382 case -1: { break; }
383 case 0:
384 {
385 self->direction = ((int16_t)atoi(var_str));
386 break;
387 }
388 case 1:
389 {
390 self->distance = ((uint16_t)atoi(var_str));
391 break;
392 }
393 case 2:
394 {
395 if (strcmp("BallSightingType", var_str) == 0) {
396#pragma clang diagnostic push
397#pragma clang diagnostic ignored "-Wbad-function-cast"
398 self->sightingType = BallSightingType;
399#pragma clang diagnostic pop
400 } else if (strcmp("CornerHorizonSightingType", var_str) == 0) {
401#pragma clang diagnostic push
402#pragma clang diagnostic ignored "-Wbad-function-cast"
403 self->sightingType = CornerHorizonSightingType;
404#pragma clang diagnostic pop
405 } else if (strcmp("CornerLineSightingType", var_str) == 0) {
406#pragma clang diagnostic push
407#pragma clang diagnostic ignored "-Wbad-function-cast"
408 self->sightingType = CornerLineSightingType;
409#pragma clang diagnostic pop
410 } else if (strcmp("CrossLineSightingType", var_str) == 0) {
411#pragma clang diagnostic push
412#pragma clang diagnostic ignored "-Wbad-function-cast"
413 self->sightingType = CrossLineSightingType;
414#pragma clang diagnostic pop
415 } else if (strcmp("GenericGoalPostSightingType", var_str) == 0) {
416#pragma clang diagnostic push
417#pragma clang diagnostic ignored "-Wbad-function-cast"
418 self->sightingType = GenericGoalPostSightingType;
419#pragma clang diagnostic pop
420 } else if (strcmp("GoalLandmarkSightingType", var_str) == 0) {
421#pragma clang diagnostic push
422#pragma clang diagnostic ignored "-Wbad-function-cast"
423 self->sightingType = GoalLandmarkSightingType;
424#pragma clang diagnostic pop
425 } else if (strcmp("LeftGoalPostSightingType", var_str) == 0) {
426#pragma clang diagnostic push
427#pragma clang diagnostic ignored "-Wbad-function-cast"
428 self->sightingType = LeftGoalPostSightingType;
429#pragma clang diagnostic pop
430 } else if (strcmp("LineHorizonSightingType", var_str) == 0) {
431#pragma clang diagnostic push
432#pragma clang diagnostic ignored "-Wbad-function-cast"
433 self->sightingType = LineHorizonSightingType;
434#pragma clang diagnostic pop
435 } else if (strcmp("RightGoalPostSightingType", var_str) == 0) {
436#pragma clang diagnostic push
437#pragma clang diagnostic ignored "-Wbad-function-cast"
438 self->sightingType = RightGoalPostSightingType;
439#pragma clang diagnostic pop
440 } else if (strcmp("StraightLineSightingType", var_str) == 0) {
441#pragma clang diagnostic push
442#pragma clang diagnostic ignored "-Wbad-function-cast"
443 self->sightingType = StraightLineSightingType;
444#pragma clang diagnostic pop
445 } else if (strcmp("TIntersectionLineSightingType", var_str) == 0) {
446#pragma clang diagnostic push
447#pragma clang diagnostic ignored "-Wbad-function-cast"
448 self->sightingType = TIntersectionLineSightingType;
449#pragma clang diagnostic pop
450 } else {
451#pragma clang diagnostic push
452#pragma clang diagnostic ignored "-Wbad-function-cast"
453 self->sightingType = ((enum LandmarkSightingType)atoi(var_str));
454#pragma clang diagnostic pop
455 }
456 break;
457 }
458 }
459 if (varIndex >= 0) {
460 varIndex++;
461 }
462 } while(index < length);
463 return self;
464}
465
466/*#ifdef WHITEBOARD_SERIALISATION*/
467
472{
473 uint16_t bit_offset = 0;
474 int16_t direction_nbo = htons(self->direction);
475 do {
476 int8_t b;
477 for (b = (16 - 1); b >= 0; b--) {
478 do {
479 uint16_t byte = bit_offset / 8;
480 uint16_t bit = 7 - (bit_offset % 8);
481 unsigned long newbit = !!((direction_nbo >> b) & 1U);
482 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
483 bit_offset = bit_offset + 1;
484 } while(false);
485 }
486 } while(false);
487
488 uint16_t distance_nbo = htons(self->distance);
489 do {
490 int8_t b;
491 for (b = (16 - 1); b >= 0; b--) {
492 do {
493 uint16_t byte = bit_offset / 8;
494 uint16_t bit = 7 - (bit_offset % 8);
495 unsigned long newbit = !!((distance_nbo >> b) & 1U);
496 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
497 bit_offset = bit_offset + 1;
498 } while(false);
499 }
500 } while(false);
501
502 enum LandmarkSightingType sightingType_nbo = htonl(self->sightingType);
503 do {
504 int8_t b;
505 for (b = (32 - 1); b >= 0; b--) {
506 do {
507 uint16_t byte = bit_offset / 8;
508 uint16_t bit = 7 - (bit_offset % 8);
509 unsigned long newbit = !!((sightingType_nbo >> b) & 1U);
510 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
511 bit_offset = bit_offset + 1;
512 } while(false);
513 }
514 } while(false);
515 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
516 (void)self;
517 (void)dst;
518 return bit_offset;
519}
520
525{
526 uint16_t bit_offset = 0;
527 do {
528 int8_t b;
529 for (b = (16 - 1); b >= 0; b--) {
530 do {
531 uint16_t byte = bit_offset / 8;
532 uint16_t bit = 7 - (bit_offset % 8);
533 char dataByte = src[byte];
534 unsigned char bitValue = (dataByte >> bit) & 1U;
535 dst->direction ^= (-bitValue ^ dst->direction) & (1UL << b);
536 bit_offset = bit_offset + 1;
537 } while(false);
538 }
539 } while(false);
540 dst->direction = ntohs(dst->direction);
541
542 do {
543 int8_t b;
544 for (b = (16 - 1); b >= 0; b--) {
545 do {
546 uint16_t byte = bit_offset / 8;
547 uint16_t bit = 7 - (bit_offset % 8);
548 char dataByte = src[byte];
549 unsigned char bitValue = (dataByte >> bit) & 1U;
550 dst->distance ^= (-bitValue ^ dst->distance) & (1UL << b);
551 bit_offset = bit_offset + 1;
552 } while(false);
553 }
554 } while(false);
555 dst->distance = ntohs(dst->distance);
556
557 do {
558 int8_t b;
559 for (b = (32 - 1); b >= 0; b--) {
560 do {
561 uint16_t byte = bit_offset / 8;
562 uint16_t bit = 7 - (bit_offset % 8);
563 char dataByte = src[byte];
564 unsigned char bitValue = (dataByte >> bit) & 1U;
565 dst->sightingType ^= (-bitValue ^ dst->sightingType) & (1UL << b);
566 bit_offset = bit_offset + 1;
567 } while(false);
568 }
569 } while(false);
570 dst->sightingType = ntohl(dst->sightingType);
571 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
572 (void)src;
573 (void)dst;
574 return bit_offset;
575}
576
577/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
int16_t direction
degrees, relative to the center of the nao torso.
uint16_t distance
CM distance to the center point of the sighting.
enum LandmarkSightingType sightingType
The type of sighting.
const char * wb_landmark_sighting_description(const struct wb_landmark_sighting *self, char *descString, size_t bufferSize)
Convert to a description string.
#define htons(x)
size_t wb_landmark_sighting_to_network_serialised(const struct wb_landmark_sighting *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
size_t wb_landmark_sighting_from_network_serialised(const char *src, struct wb_landmark_sighting *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_landmark_sighting_to_string(const struct wb_landmark_sighting *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_landmark_sighting * wb_landmark_sighting_from_string(struct wb_landmark_sighting *self, const char *str)
Convert from a string.
#define ntohl(x)
#define ntohs(x)
#define htonl(x)
#define LANDMARK_SIGHTING_DESC_BUFFER_SIZE
LandmarkSightingType
@ GenericGoalPostSightingType
@ StraightLineSightingType
@ BallSightingType
@ CornerLineSightingType
@ LeftGoalPostSightingType
@ GoalLandmarkSightingType
@ TIntersectionLineSightingType
@ LineHorizonSightingType
@ CrossLineSightingType
@ CornerHorizonSightingType
@ RightGoalPostSightingType