gusimplewhiteboard
wb_location.c
Go to the documentation of this file.
1/*
2 * file wb_location.c
3 *
4 * This file was generated by classgenerator from location.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
64#include "wb_location.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_location_description(const struct wb_location* 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 len += snprintf(descString + len, bufferSize - len, "confidence=%u", self->confidence);
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, "distanceVariance=%u", self->distanceVariance);
158 if (len >= bufferSize) {
159 return descString;
160 }
161 len = gu_strlcat(descString, ", ", bufferSize);
162 if (len >= bufferSize) {
163 return descString;
164 }
165 len += snprintf(descString + len, bufferSize - len, "directionVariance=%u", self->directionVariance);
166 return descString;
167#pragma clang diagnostic pop
168}
169
173const char* wb_location_to_string(const struct wb_location* self, char* toString, size_t bufferSize)
174{
175#pragma clang diagnostic push
176#pragma clang diagnostic ignored "-Wunused-variable"
177 size_t len = 0;
178 if (len >= bufferSize) {
179 return toString;
180 }
181 len += snprintf(toString + len, bufferSize - len, "%d", self->direction);
182 if (len >= bufferSize) {
183 return toString;
184 }
185 len = gu_strlcat(toString, ", ", bufferSize);
186 if (len >= bufferSize) {
187 return toString;
188 }
189 len += snprintf(toString + len, bufferSize - len, "%u", self->distance);
190 if (len >= bufferSize) {
191 return toString;
192 }
193 len = gu_strlcat(toString, ", ", bufferSize);
194 if (len >= bufferSize) {
195 return toString;
196 }
197 len += snprintf(toString + len, bufferSize - len, "%u", self->confidence);
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->distanceVariance);
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->directionVariance);
214 return toString;
215#pragma clang diagnostic pop
216}
217
221struct wb_location* wb_location_from_string(struct wb_location* self, const char* str)
222{
223 size_t temp_length = strlen(str);
224 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
225 if (length < 1 || length > LOCATION_DESC_BUFFER_SIZE) {
226 return self;
227 }
228 char var_str_buffer[LOCATION_DESC_BUFFER_SIZE + 1];
229 char* var_str = &var_str_buffer[0];
230 char key_buffer[18];
231 char* key = &key_buffer[0];
232 int bracecount = 0;
233 int startVar = 0;
234 int index = 0;
235 int startKey = 0;
236 int endKey = -1;
237 int varIndex = 0;
238 if (index == 0 && str[0] == '{') {
239 index = 1;
240 }
241 startVar = index;
242 startKey = startVar;
243 do {
244 for (int i = index; i < length; i++) {
245 index = i + 1;
246 if (bracecount == 0 && str[i] == '=') {
247 endKey = i - 1;
248 startVar = index;
249 continue;
250 }
251 if (bracecount == 0 && isspace(str[i])) {
252 startVar = index;
253 if (endKey == -1) {
254 startKey = index;
255 }
256 continue;
257 }
258 if (bracecount == 0 && str[i] == ',') {
259 index = i - 1;
260 break;
261 }
262 if (str[i] == '{') {
263 bracecount++;
264 continue;
265 }
266 if (str[i] == '}') {
267 bracecount--;
268 if (bracecount < 0) {
269 index = i - 1;
270 break;
271 }
272 }
273 if (i == length - 1) {
274 index = i;
275 }
276 }
277 if (endKey >= startKey && endKey - startKey < length) {
278 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
279 key[(endKey - startKey) + 1] = 0;
280 } else {
281 key[0] = 0;
282 }
283 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
284 var_str[(index - startVar) + 1] = 0;
285 bracecount = 0;
286 index += 2;
287 startVar = index;
288 startKey = startVar;
289 endKey = -1;
290 if (strlen(key) > 0) {
291 if (0 == strcmp("direction", key)) {
292 varIndex = 0;
293 } else if (0 == strcmp("distance", key)) {
294 varIndex = 1;
295 } else if (0 == strcmp("confidence", key)) {
296 varIndex = 2;
297 } else if (0 == strcmp("distanceVariance", key)) {
298 varIndex = 3;
299 } else if (0 == strcmp("directionVariance", key)) {
300 varIndex = 4;
301 } else {
302 varIndex = -1;
303 }
304 }
305 switch (varIndex) {
306 case -1: { break; }
307 case 0:
308 {
309 self->direction = ((int16_t)atoi(var_str));
310 break;
311 }
312 case 1:
313 {
314 self->distance = ((uint16_t)atoi(var_str));
315 break;
316 }
317 case 2:
318 {
319 self->confidence = ((uint8_t)atoi(var_str));
320 break;
321 }
322 case 3:
323 {
324 self->distanceVariance = ((uint32_t)atoi(var_str));
325 break;
326 }
327 case 4:
328 {
329 self->directionVariance = ((uint32_t)atoi(var_str));
330 break;
331 }
332 }
333 if (varIndex >= 0) {
334 varIndex++;
335 }
336 } while(index < length);
337 return self;
338}
339
340/*#ifdef WHITEBOARD_SERIALISATION*/
341
345size_t wb_location_to_network_serialised(const struct wb_location *self, char *dst)
346{
347 uint16_t bit_offset = 0;
348 int16_t direction_nbo = htons(self->direction);
349 do {
350 int8_t b;
351 for (b = (16 - 1); b >= 0; b--) {
352 do {
353 uint16_t byte = bit_offset / 8;
354 uint16_t bit = 7 - (bit_offset % 8);
355 unsigned long newbit = !!((direction_nbo >> b) & 1U);
356 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
357 bit_offset = bit_offset + 1;
358 } while(false);
359 }
360 } while(false);
361
362 uint16_t distance_nbo = htons(self->distance);
363 do {
364 int8_t b;
365 for (b = (16 - 1); b >= 0; b--) {
366 do {
367 uint16_t byte = bit_offset / 8;
368 uint16_t bit = 7 - (bit_offset % 8);
369 unsigned long newbit = !!((distance_nbo >> b) & 1U);
370 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
371 bit_offset = bit_offset + 1;
372 } while(false);
373 }
374 } while(false);
375
376 uint8_t confidence_nbo = (self->confidence);
377 do {
378 int8_t b;
379 for (b = (8 - 1); b >= 0; b--) {
380 do {
381 uint16_t byte = bit_offset / 8;
382 uint16_t bit = 7 - (bit_offset % 8);
383 unsigned long newbit = !!((confidence_nbo >> b) & 1U);
384 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
385 bit_offset = bit_offset + 1;
386 } while(false);
387 }
388 } while(false);
389
390 uint32_t distanceVariance_nbo = htonl(self->distanceVariance);
391 do {
392 int8_t b;
393 for (b = (32 - 1); b >= 0; b--) {
394 do {
395 uint16_t byte = bit_offset / 8;
396 uint16_t bit = 7 - (bit_offset % 8);
397 unsigned long newbit = !!((distanceVariance_nbo >> b) & 1U);
398 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
399 bit_offset = bit_offset + 1;
400 } while(false);
401 }
402 } while(false);
403
404 uint32_t directionVariance_nbo = htonl(self->directionVariance);
405 do {
406 int8_t b;
407 for (b = (32 - 1); b >= 0; b--) {
408 do {
409 uint16_t byte = bit_offset / 8;
410 uint16_t bit = 7 - (bit_offset % 8);
411 unsigned long newbit = !!((directionVariance_nbo >> b) & 1U);
412 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
413 bit_offset = bit_offset + 1;
414 } while(false);
415 }
416 } while(false);
417 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
418 (void)self;
419 (void)dst;
420 return bit_offset;
421}
422
426size_t wb_location_from_network_serialised(const char *src, struct wb_location *dst)
427{
428 uint16_t bit_offset = 0;
429 do {
430 int8_t b;
431 for (b = (16 - 1); b >= 0; b--) {
432 do {
433 uint16_t byte = bit_offset / 8;
434 uint16_t bit = 7 - (bit_offset % 8);
435 char dataByte = src[byte];
436 unsigned char bitValue = (dataByte >> bit) & 1U;
437 dst->direction ^= (-bitValue ^ dst->direction) & (1UL << b);
438 bit_offset = bit_offset + 1;
439 } while(false);
440 }
441 } while(false);
442 dst->direction = ntohs(dst->direction);
443
444 do {
445 int8_t b;
446 for (b = (16 - 1); b >= 0; b--) {
447 do {
448 uint16_t byte = bit_offset / 8;
449 uint16_t bit = 7 - (bit_offset % 8);
450 char dataByte = src[byte];
451 unsigned char bitValue = (dataByte >> bit) & 1U;
452 dst->distance ^= (-bitValue ^ dst->distance) & (1UL << b);
453 bit_offset = bit_offset + 1;
454 } while(false);
455 }
456 } while(false);
457 dst->distance = ntohs(dst->distance);
458
459 do {
460 int8_t b;
461 for (b = (8 - 1); b >= 0; b--) {
462 do {
463 uint16_t byte = bit_offset / 8;
464 uint16_t bit = 7 - (bit_offset % 8);
465 char dataByte = src[byte];
466 unsigned char bitValue = (dataByte >> bit) & 1U;
467 dst->confidence ^= (-bitValue ^ dst->confidence) & (1UL << b);
468 bit_offset = bit_offset + 1;
469 } while(false);
470 }
471 } while(false);
472 dst->confidence = (dst->confidence);
473
474 do {
475 int8_t b;
476 for (b = (32 - 1); b >= 0; b--) {
477 do {
478 uint16_t byte = bit_offset / 8;
479 uint16_t bit = 7 - (bit_offset % 8);
480 char dataByte = src[byte];
481 unsigned char bitValue = (dataByte >> bit) & 1U;
482 dst->distanceVariance ^= (-bitValue ^ dst->distanceVariance) & (1UL << b);
483 bit_offset = bit_offset + 1;
484 } while(false);
485 }
486 } while(false);
488
489 do {
490 int8_t b;
491 for (b = (32 - 1); b >= 0; b--) {
492 do {
493 uint16_t byte = bit_offset / 8;
494 uint16_t bit = 7 - (bit_offset % 8);
495 char dataByte = src[byte];
496 unsigned char bitValue = (dataByte >> bit) & 1U;
497 dst->directionVariance ^= (-bitValue ^ dst->directionVariance) & (1UL << b);
498 bit_offset = bit_offset + 1;
499 } while(false);
500 }
501 } 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)src;
505 (void)dst;
506 return bit_offset;
507}
508
509/*#endif // WHITEBOARD_SERIALISATION*/
510
511struct wb_location rr_coord_to_wb_location(const gu_relative_coordinate coord, const uint8_t confidence, const uint32_t distanceVariance, const uint32_t directionVariance)
512{
513 const struct wb_location out = {
514 deg_d_to_i16(coord.direction),
515 cm_u_to_u16(mm_u_to_cm_u(coord.distance)),
519 };
520 return out;
521}
522
523struct gu_relative_coordinate wb_location_to_rr_coord(const struct wb_location location)
524{
525 const gu_relative_coordinate out = {
526 i16_to_deg_d(location.direction),
527 cm_u_to_mm_u(u16_to_cm_u(location.distance))
528 };
529 return out;
530}
531
532struct gu_optional_relative_coordinate wb_location_to_opt_rr_coord(const struct wb_location location, const uint8_t confidence)
533{
534 const gu_optional_relative_coordinate out = {
535 location.confidence >= confidence,
537 };
538 return out;
539}
WHITEBOARD_POSTER_STRING_CONVERSION.
Definition: wb_location.h:109
uint32_t distanceVariance
The variance of the distance.
Definition: wb_location.h:129
uint32_t directionVariance
The variance of the direction.
Definition: wb_location.h:134
int16_t direction
degrees, relative to the center of the nao torso.
Definition: wb_location.h:114
uint8_t confidence
The type of sighting.
Definition: wb_location.h:124
uint16_t distance
CM distance to the center point of the sighting.
Definition: wb_location.h:119
size_t wb_location_to_network_serialised(const struct wb_location *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
Definition: wb_location.c:345
const char * wb_location_description(const struct wb_location *self, char *descString, size_t bufferSize)
Convert to a description string.
Definition: wb_location.c:125
const char * wb_location_to_string(const struct wb_location *self, char *toString, size_t bufferSize)
Convert to a string.
Definition: wb_location.c:173
struct wb_location rr_coord_to_wb_location(const gu_relative_coordinate coord, const uint8_t confidence, const uint32_t distanceVariance, const uint32_t directionVariance)
WHITEBOARD_POSTER_STRING_CONVERSION.
Definition: wb_location.c:511
#define htons(x)
Definition: wb_location.c:101
struct gu_optional_relative_coordinate wb_location_to_opt_rr_coord(const struct wb_location location, const uint8_t confidence)
Converts a wb_location to a gu_optional_relative_coordinate.
Definition: wb_location.c:532
struct wb_location * wb_location_from_string(struct wb_location *self, const char *str)
Convert from a string.
Definition: wb_location.c:221
#define ntohl(x)
Definition: wb_location.c:98
#define ntohs(x)
Definition: wb_location.c:102
struct gu_relative_coordinate wb_location_to_rr_coord(const struct wb_location location)
Converts a wb_location to a gu_relative_coordinate.
Definition: wb_location.c:523
#define htonl(x)
Definition: wb_location.c:97
size_t wb_location_from_network_serialised(const char *src, struct wb_location *dst)
Convert from a compressed, serialised, network byte order byte stream.
Definition: wb_location.c:426
#define LOCATION_DESC_BUFFER_SIZE
Definition: wb_location.h:97