F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Serializable.cpp
Go to the documentation of this file.
2 #include <Fw/Types/Assert.hpp>
5 #include <cstdio>
6 #include <cstring> // memcpy
7 #ifdef BUILD_UT
8 #include <Fw/Types/String.hpp>
9 #include <iomanip>
10 #endif
11 
12 // Some macros/functions to optimize for architectures
13 
14 namespace Fw {
15 
17 
19 
20 // ----------------------------------------------------------------------
21 #if FW_SERIALIZABLE_TO_STRING || FW_ENABLE_TEXT_LOGGING || BUILD_UT
22 
23 void Serializable::toString(StringBase& text) const {
24  text = "NOSPEC"; // set to not specified.
25 }
26 
27 #endif
28 
29 #ifdef BUILD_UT
30 std::ostream& operator<<(std::ostream& os, const Serializable& val) {
31  Fw::String out;
32  val.toString(out);
33 
34  os << out;
35 
36  return os;
37 }
38 #endif
39 
40 SerializeBufferBase::SerializeBufferBase() : m_serLoc(0), m_deserLoc(0) {}
41 
43 
44 void SerializeBufferBase::copyFrom(const SerializeBufferBase& src) {
45  this->m_serLoc = src.m_serLoc;
46  this->m_deserLoc = src.m_deserLoc;
47  FW_ASSERT(src.getBuffAddr());
48  FW_ASSERT(this->getBuffAddr());
49  // destination has to be same or bigger
50  FW_ASSERT(src.getBuffLength() <= this->getBuffCapacity(), static_cast<FwAssertArgType>(src.getBuffLength()),
51  static_cast<FwAssertArgType>(this->getBuffLength()));
52  (void)memcpy(this->getBuffAddr(), src.getBuffAddr(), static_cast<size_t>(this->m_serLoc));
53 }
54 
55 // Copy constructor doesn't make sense in this virtual class as there is nothing to copy. Derived classes should
56 // call the empty constructor and then call their own copy function
58  this->copyFrom(src);
59  return *this;
60 }
61 
62 // serialization routines
63 
65  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
67  }
68  FW_ASSERT(this->getBuffAddr());
69  this->getBuffAddr()[this->m_serLoc] = val;
70  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
71  this->m_deserLoc = 0;
72 
73  return FW_SERIALIZE_OK;
74 }
75 
77  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
79  }
80  FW_ASSERT(this->getBuffAddr());
81  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val);
82  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
83  this->m_deserLoc = 0;
84  return FW_SERIALIZE_OK;
85 }
86 
87 #if FW_HAS_16_BIT == 1
89  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
91  }
92  FW_ASSERT(this->getBuffAddr());
93  // MSB first
94  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 8);
95  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val);
96  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
97  this->m_deserLoc = 0;
98  return FW_SERIALIZE_OK;
99 }
100 
102  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
104  }
105  FW_ASSERT(this->getBuffAddr());
106  // MSB first
107  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 8);
108  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val);
109  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
110  this->m_deserLoc = 0;
111  return FW_SERIALIZE_OK;
112 }
113 #endif
114 #if FW_HAS_32_BIT == 1
116  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
118  }
119  FW_ASSERT(this->getBuffAddr());
120  // MSB first
121  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 24);
122  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 16);
123  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 8);
124  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val);
125  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
126  this->m_deserLoc = 0;
127  return FW_SERIALIZE_OK;
128 }
129 
131  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
133  }
134  FW_ASSERT(this->getBuffAddr());
135  // MSB first
136  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 24);
137  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 16);
138  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 8);
139  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val);
140  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
141  this->m_deserLoc = 0;
142  return FW_SERIALIZE_OK;
143 }
144 #endif
145 
146 #if FW_HAS_64_BIT == 1
148  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
150  }
151  FW_ASSERT(this->getBuffAddr());
152  // MSB first
153  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 56);
154  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 48);
155  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 40);
156  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val >> 32);
157  this->getBuffAddr()[this->m_serLoc + 4] = static_cast<U8>(val >> 24);
158  this->getBuffAddr()[this->m_serLoc + 5] = static_cast<U8>(val >> 16);
159  this->getBuffAddr()[this->m_serLoc + 6] = static_cast<U8>(val >> 8);
160  this->getBuffAddr()[this->m_serLoc + 7] = static_cast<U8>(val);
161  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
162  this->m_deserLoc = 0;
163  return FW_SERIALIZE_OK;
164 }
165 
167  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
169  }
170  FW_ASSERT(this->getBuffAddr());
171  // MSB first
172  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 56);
173  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 48);
174  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 40);
175  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val >> 32);
176  this->getBuffAddr()[this->m_serLoc + 4] = static_cast<U8>(val >> 24);
177  this->getBuffAddr()[this->m_serLoc + 5] = static_cast<U8>(val >> 16);
178  this->getBuffAddr()[this->m_serLoc + 6] = static_cast<U8>(val >> 8);
179  this->getBuffAddr()[this->m_serLoc + 7] = static_cast<U8>(val);
180  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
181  this->m_deserLoc = 0;
182  return FW_SERIALIZE_OK;
183 }
184 #endif
185 
187  // floating point values need to be byte-swapped as well, so copy to U64 and use that routine
188  U64 u64Val;
189  (void)memcpy(&u64Val, &val, sizeof(val));
190  return this->serializeFrom(u64Val);
191 }
192 
194  // floating point values need to be byte-swapped as well, so copy to U32 and use that routine
195  U32 u32Val;
196  (void)memcpy(&u32Val, &val, sizeof(val));
197  return this->serializeFrom(u32Val);
198 }
199 
201  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(U8)) - 1 >= this->getBuffCapacity()) {
203  }
204 
205  FW_ASSERT(this->getBuffAddr());
206  if (val) {
207  this->getBuffAddr()[this->m_serLoc + 0] = FW_SERIALIZE_TRUE_VALUE;
208  } else {
209  this->getBuffAddr()[this->m_serLoc + 0] = FW_SERIALIZE_FALSE_VALUE;
210  }
211 
212  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(U8));
213  this->m_deserLoc = 0;
214  return FW_SERIALIZE_OK;
215 }
216 
218  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(void*)) - 1 >= this->getBuffCapacity()) {
220  }
221 
222  return this->serializeFrom(reinterpret_cast<PlatformPointerCastType>(val));
223 }
224 
226  return this->serializeFrom(buff, static_cast<FwSizeType>(length), Serialization::INCLUDE_LENGTH);
227 }
228 
230  // First serialize length
231  SerializeStatus stat;
232  if (mode == Serialization::INCLUDE_LENGTH) {
233  stat = this->serializeFrom(static_cast<FwSizeStoreType>(length));
234  if (stat != FW_SERIALIZE_OK) {
235  return stat;
236  }
237  }
238 
239  // make sure we have enough space
240  if (this->m_serLoc + length > this->getBuffCapacity()) {
242  }
243 
244  // copy buffer to our buffer
245  (void)memcpy(&this->getBuffAddr()[this->m_serLoc], buff, static_cast<size_t>(length));
246  this->m_serLoc += static_cast<Serializable::SizeType>(length);
247  this->m_deserLoc = 0;
248 
249  return FW_SERIALIZE_OK;
250 }
251 
253  return val.serializeTo(*this);
254 }
255 
258  if (this->m_serLoc + size + static_cast<Serializable::SizeType>(sizeof(FwSizeStoreType)) >
259  this->getBuffCapacity()) {
261  }
262 
263  // First, serialize size
264  SerializeStatus stat = this->serializeFrom(static_cast<FwSizeStoreType>(size));
265  if (stat != FW_SERIALIZE_OK) {
266  return stat;
267  }
268 
269  FW_ASSERT(this->getBuffAddr());
270  FW_ASSERT(val.getBuffAddr());
271  // serialize buffer
272  (void)memcpy(&this->getBuffAddr()[this->m_serLoc], val.getBuffAddr(), static_cast<size_t>(size));
273  this->m_serLoc += size;
274  this->m_deserLoc = 0;
275 
276  return FW_SERIALIZE_OK;
277 }
278 
281  if ((size < std::numeric_limits<FwSizeStoreType>::min()) || (size > std::numeric_limits<FwSizeStoreType>::max())) {
282  status = FW_SERIALIZE_FORMAT_ERROR;
283  }
284  if (status == FW_SERIALIZE_OK) {
285  status = this->serializeFrom(static_cast<FwSizeStoreType>(size));
286  }
287  return status;
288 }
289 
290 // deserialization routines
291 
293  // check for room
294  if (this->getBuffLength() == this->m_deserLoc) {
296  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
298  }
299  // read from current location
300  FW_ASSERT(this->getBuffAddr());
301  val = this->getBuffAddr()[this->m_deserLoc + 0];
302  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
303  return FW_SERIALIZE_OK;
304 }
305 
307  // check for room
308  if (this->getBuffLength() == this->m_deserLoc) {
310  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
312  }
313  // read from current location
314  FW_ASSERT(this->getBuffAddr());
315  val = static_cast<I8>(this->getBuffAddr()[this->m_deserLoc + 0]);
316  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
317  return FW_SERIALIZE_OK;
318 }
319 
320 #if FW_HAS_16_BIT == 1
322  // check for room
323  if (this->getBuffLength() == this->m_deserLoc) {
325  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
327  }
328  // read from current location
329  FW_ASSERT(this->getBuffAddr());
330  // MSB first
331  val = static_cast<U16>(((this->getBuffAddr()[this->m_deserLoc + 1]) << 0) |
332  ((this->getBuffAddr()[this->m_deserLoc + 0]) << 8));
333  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
334  return FW_SERIALIZE_OK;
335 }
336 
338  // check for room
339  if (this->getBuffLength() == this->m_deserLoc) {
341  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
343  }
344  // read from current location
345  FW_ASSERT(this->getBuffAddr());
346  // MSB first
347  val = static_cast<I16>(((this->getBuffAddr()[this->m_deserLoc + 1]) << 0) |
348  ((this->getBuffAddr()[this->m_deserLoc + 0]) << 8));
349  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
350  return FW_SERIALIZE_OK;
351 }
352 #endif
353 #if FW_HAS_32_BIT == 1
355  // check for room
356  if (this->getBuffLength() == this->m_deserLoc) {
358  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
360  }
361  // read from current location
362  FW_ASSERT(this->getBuffAddr());
363  // MSB first
364  val = (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 3]) << 0) |
365  (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 2]) << 8) |
366  (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 1]) << 16) |
367  (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 0]) << 24);
368  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
369  return FW_SERIALIZE_OK;
370 }
371 
373  // check for room
374  if (this->getBuffLength() == this->m_deserLoc) {
376  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
378  }
379  // read from current location
380  FW_ASSERT(this->getBuffAddr());
381  // MSB first
382  val = (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 3]) << 0) |
383  (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 2]) << 8) |
384  (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 1]) << 16) |
385  (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 0]) << 24);
386  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
387  return FW_SERIALIZE_OK;
388 }
389 #endif
390 
391 #if FW_HAS_64_BIT == 1
392 
394  // check for room
395  if (this->getBuffLength() == this->m_deserLoc) {
397  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
399  }
400  // read from current location
401  FW_ASSERT(this->getBuffAddr());
402  // MSB first
403  val = (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 7]) << 0) |
404  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 6]) << 8) |
405  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 5]) << 16) |
406  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 4]) << 24) |
407  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 3]) << 32) |
408  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 2]) << 40) |
409  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 1]) << 48) |
410  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 0]) << 56);
411 
412  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
413  return FW_SERIALIZE_OK;
414 }
415 
417  // check for room
418  if (this->getBuffLength() == this->m_deserLoc) {
420  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
422  }
423  // read from current location
424  FW_ASSERT(this->getBuffAddr());
425  // MSB first
426  val = (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 7]) << 0) |
427  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 6]) << 8) |
428  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 5]) << 16) |
429  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 4]) << 24) |
430  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 3]) << 32) |
431  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 2]) << 40) |
432  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 1]) << 48) |
433  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 0]) << 56);
434  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
435  return FW_SERIALIZE_OK;
436 }
437 #endif
438 
440  // deserialize as 64-bit int to handle endianness
441  U64 tempVal;
442  SerializeStatus stat = this->deserializeTo(tempVal);
443  if (stat != FW_SERIALIZE_OK) {
444  return stat;
445  }
446  // copy to argument
447  (void)memcpy(&val, &tempVal, sizeof(val));
448 
449  return FW_SERIALIZE_OK;
450 }
451 
453  // check for room
454  if (this->getBuffLength() == this->m_deserLoc) {
456  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(U8))) {
458  }
459  // read from current location
460  FW_ASSERT(this->getBuffAddr());
461  if (FW_SERIALIZE_TRUE_VALUE == this->getBuffAddr()[this->m_deserLoc + 0]) {
462  val = true;
463  } else if (FW_SERIALIZE_FALSE_VALUE == this->getBuffAddr()[this->m_deserLoc + 0]) {
464  val = false;
465  } else {
467  }
468 
469  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(U8));
470  return FW_SERIALIZE_OK;
471 }
472 
474  // Deserialize as pointer cast, then convert to void*
475  PlatformPointerCastType pointerCastVal = 0;
476  const SerializeStatus stat = this->deserializeTo(pointerCastVal);
477  if (stat == FW_SERIALIZE_OK) {
478  val = reinterpret_cast<void*>(pointerCastVal);
479  }
480  return stat;
481 }
482 
484  // deserialize as 64-bit int to handle endianness
485  U32 tempVal;
486  SerializeStatus stat = this->deserializeTo(tempVal);
487  if (stat != FW_SERIALIZE_OK) {
488  return stat;
489  }
490  (void)memcpy(&val, &tempVal, sizeof(val));
491 
492  return FW_SERIALIZE_OK;
493 }
494 
496  FwSizeType length_in_out = static_cast<FwSizeType>(length);
497  SerializeStatus status = this->deserializeTo(buff, length_in_out, Serialization::INCLUDE_LENGTH);
498  length = static_cast<Serializable::SizeType>(length_in_out);
499  return status;
500 }
501 
503  FW_ASSERT(this->getBuffAddr());
504 
505  if (mode == Serialization::INCLUDE_LENGTH) {
506  FwSizeStoreType storedLength;
507 
508  SerializeStatus stat = this->deserializeTo(storedLength);
509 
510  if (stat != FW_SERIALIZE_OK) {
511  return stat;
512  }
513 
514  // make sure it fits
515  if ((storedLength > this->getBuffLeft()) or (storedLength > length)) {
517  }
518 
519  (void)memcpy(buff, &this->getBuffAddr()[this->m_deserLoc], static_cast<size_t>(storedLength));
520 
521  length = static_cast<FwSizeType>(storedLength);
522 
523  } else {
524  // make sure enough is left
525  if (length > this->getBuffLeft()) {
527  }
528 
529  (void)memcpy(buff, &this->getBuffAddr()[this->m_deserLoc], static_cast<size_t>(length));
530  }
531 
532  this->m_deserLoc += static_cast<Serializable::SizeType>(length);
533  return FW_SERIALIZE_OK;
534 }
535 
537  return val.deserializeFrom(*this);
538 }
539 
541  FW_ASSERT(val.getBuffAddr());
543 
544  FwSizeStoreType storedLength;
545 
546  stat = this->deserializeTo(storedLength);
547 
548  if (stat != FW_SERIALIZE_OK) {
549  return stat;
550  }
551 
552  // make sure destination has enough room
553 
554  if ((storedLength > val.getBuffCapacity()) or (storedLength > this->getBuffLeft())) {
556  }
557 
558  FW_ASSERT(this->getBuffAddr());
559  (void)memcpy(val.getBuffAddr(), &this->getBuffAddr()[this->m_deserLoc], static_cast<size_t>(storedLength));
560 
561  stat = val.setBuffLen(storedLength);
562 
563  if (stat != FW_SERIALIZE_OK) {
564  return stat;
565  }
566 
567  this->m_deserLoc += storedLength;
568 
569  return FW_SERIALIZE_OK;
570 }
571 
573  FwSizeStoreType storedSize = 0;
574  Fw::SerializeStatus status = this->deserializeTo(storedSize);
575  if (status == FW_SERIALIZE_OK) {
576  size = static_cast<FwSizeType>(storedSize);
577  }
578  return status;
579 }
580 
582  this->m_deserLoc = 0;
583  this->m_serLoc = 0;
584 }
585 
587  this->m_deserLoc = 0;
588 }
589 
592  // compute new deser loc
593  const FwSizeType newSerLoc = this->m_serLoc + numBytesToSkip;
594  // check for room
595  if (newSerLoc <= this->getBuffCapacity()) {
596  // update deser loc
597  this->m_serLoc = static_cast<Serializable::SizeType>(newSerLoc);
598  } else {
599  status = FW_SERIALIZE_NO_ROOM_LEFT;
600  }
601  return status;
602 }
603 
605  // check for room
606  if (this->getBuffLength() == this->m_deserLoc) {
608  } else if (this->getBuffLength() - this->m_deserLoc < numBytesToSkip) {
610  }
611  // update location in buffer to skip the value
612  this->m_deserLoc += static_cast<Serializable::SizeType>(numBytesToSkip);
613  return FW_SERIALIZE_OK;
614 }
615 
617  // Reset serialization
618  this->resetSer();
619  // Advance to offset
620  return this->serializeSkip(offset);
621 }
623  // Reset deserialization
624  this->resetDeser();
625  // Advance to offset
626  return this->deserializeSkip(offset);
627 }
628 
630  return this->m_serLoc;
631 }
632 
634  if (this->getBuffCapacity() < length) {
636  } else {
637  FW_ASSERT(src);
638  FW_ASSERT(this->getBuffAddr());
639  (void)memcpy(this->getBuffAddr(), src, static_cast<size_t>(length));
640  this->m_serLoc = length;
641  this->m_deserLoc = 0;
642  return FW_SERIALIZE_OK;
643  }
644 }
645 
647  if (this->getBuffCapacity() < length) {
649  } else {
650  this->m_serLoc = length;
651  this->m_deserLoc = 0;
652  return FW_SERIALIZE_OK;
653  }
654 }
655 
657  FW_ASSERT(this->m_serLoc >= this->m_deserLoc, static_cast<FwAssertArgType>(this->m_serLoc),
658  static_cast<FwAssertArgType>(this->m_deserLoc));
659  return this->m_serLoc - this->m_deserLoc;
660 }
661 
663  // make sure there is sufficient size in destination
664  if (dest.getBuffCapacity() < size) {
666  }
667  // otherwise, set destination buffer to data from deserialization pointer plus size
668  SerializeStatus stat = dest.setBuff(&this->getBuffAddr()[this->m_deserLoc], size);
669  if (stat == FW_SERIALIZE_OK) {
670  this->m_deserLoc += size;
671  }
672  return stat;
673 }
674 
676  // make sure there is sufficient size in destination
677  if (dest.getBuffCapacity() < size + dest.getBuffLength()) {
679  }
680  // make sure there is sufficient buffer in source
681  if (this->getBuffLeft() < size) {
683  }
684 
685  // otherwise, serialize bytes to destination without writing length
686  SerializeStatus stat =
688  if (stat == FW_SERIALIZE_OK) {
689  this->m_deserLoc += size;
690  }
691  return stat;
692 }
693 
694 // return address of buffer not yet deserialized. This is used
695 // to copy the remainder of a buffer.
697  return &this->getBuffAddr()[this->m_deserLoc];
698 }
699 
702  return &this->getBuffAddr()[this->m_serLoc];
703 }
704 
705 #ifdef BUILD_UT
706 bool SerializeBufferBase::operator==(const SerializeBufferBase& other) const {
707  if (this->getBuffLength() != other.getBuffLength()) {
708  return false;
709  }
710 
711  const U8* us = this->getBuffAddr();
712  const U8* them = other.getBuffAddr();
713 
714  FW_ASSERT(us);
715  FW_ASSERT(them);
716 
717  for (Serializable::SizeType byte = 0; byte < this->getBuffLength(); byte++) {
718  if (us[byte] != them[byte]) {
719  return false;
720  }
721  }
722 
723  return true;
724 }
725 
726 std::ostream& operator<<(std::ostream& os, const SerializeBufferBase& buff) {
727  const U8* us = buff.getBuffAddr();
728 
729  FW_ASSERT(us);
730 
731  for (Serializable::SizeType byte = 0; byte < buff.getBuffLength(); byte++) {
732  os << "[" << std::setw(2) << std::hex << std::setfill('0') << us[byte] << "]" << std::dec;
733  }
734 
735  return os;
736 }
737 #endif
738 
740  this->setExtBuffer(buffPtr, size);
741 }
742 
744  this->clear();
745 }
746 
748  FW_ASSERT(buffPtr != nullptr);
749  this->clear();
750  this->m_buff = buffPtr;
751  this->m_buffSize = size;
752 }
753 
755  this->resetSer();
756  this->resetDeser();
757  this->m_buff = nullptr;
758  this->m_buffSize = 0;
759 }
760 
762  return this->m_buffSize;
763 }
764 
766  return this->m_buff;
767 }
768 
770  return this->m_buff;
771 }
772 
773 // ----------------------------------------------------------------------
774 // Deprecated method implementations for backward compatibility
775 // ----------------------------------------------------------------------
776 
778  return this->serializeFrom(val);
779 }
781  return this->serializeFrom(val);
782 }
783 #if FW_HAS_16_BIT == 1
785  return this->serializeFrom(val);
786 }
788  return this->serializeFrom(val);
789 }
790 #endif
791 #if FW_HAS_32_BIT == 1
793  return this->serializeFrom(val);
794 }
796  return this->serializeFrom(val);
797 }
798 #endif
799 #if FW_HAS_64_BIT == 1
801  return this->serializeFrom(val);
802 }
804  return this->serializeFrom(val);
805 }
806 #endif
808  return this->serializeFrom(val);
809 }
811  return this->serializeFrom(val);
812 }
814  return this->serializeFrom(val);
815 }
817  return this->serializeFrom(val);
818 }
819 
820 // Deprecated method for backward compatibility
821 SerializeStatus SerializeBufferBase::serialize(const U8* buff, FwSizeType length, bool noLength) {
823  return this->serializeFrom(buff, length, mode);
824 }
825 
827  return this->serializeFrom(buff, length);
828 }
830  return this->serializeFrom(buff, length, mode);
831 }
833  return this->serializeFrom(val);
834 }
836  return this->serializeFrom(val);
837 }
838 
840  return this->deserializeTo(val);
841 }
843  return this->deserializeTo(val);
844 }
845 #if FW_HAS_16_BIT == 1
847  return this->deserializeTo(val);
848 }
850  return this->deserializeTo(val);
851 }
852 #endif
853 #if FW_HAS_32_BIT == 1
855  return this->deserializeTo(val);
856 }
858  return this->deserializeTo(val);
859 }
860 #endif
861 #if FW_HAS_64_BIT == 1
863  return this->deserializeTo(val);
864 }
866  return this->deserializeTo(val);
867 }
868 #endif
870  return this->deserializeTo(val);
871 }
873  return this->deserializeTo(val);
874 }
876  return this->deserializeTo(val);
877 }
879  return this->deserializeTo(val);
880 }
881 
882 // Deprecated method for backward compatibility
883 SerializeStatus SerializeBufferBase::deserialize(U8* buff, FwSizeType& length, bool noLength) {
885  return this->deserializeTo(buff, length, mode);
886 }
887 
889  return this->deserializeTo(buff, length, Serialization::INCLUDE_LENGTH);
890 }
891 
893  return this->deserializeTo(buff, length, mode);
894 }
895 
897  return this->deserializeTo(val);
898 }
900  return this->deserializeTo(val);
901 }
902 
903 } // namespace Fw
Serialization/Deserialization operation was successful.
void clear()
clear external buffer
SerializeBufferBase()
default constructor
Deserialization buffer was empty when trying to read more data.
PlatformSizeType FwSizeType
void resetSer()
reset to beginning of buffer to reuse for serialization
SerializeStatus copyRaw(SerializeBufferBase &dest, Serializable::SizeType size)
directly copies buffer without looking for a size in the stream.
SerializeStatus serialize(U8 val)
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:50
No room left in the buffer to serialize data.
#define FW_SERIALIZE_FALSE_VALUE
Value encoded during serialization for boolean false.
Definition: FpConfig.h:36
Serializable::SizeType m_buffSize
size of external buffer
virtual ~SerializeBufferBase()
destructor
Deserialization data had incorrect values (unexpected data types)
Serializable::SizeType m_serLoc
current offset in buffer of serialized data
const U8 * getBuffAddrLeft() const
gets address of remaining non-deserialized data.
ExternalSerializeBuffer()
default constructor
SerializeStatus
forward declaration for string
float F32
32-bit floating point
Definition: BasicTypes.h:83
U16 FwSizeStoreType
The type used to serialize a size value.
Serializable::SizeType getBuffLength() const
returns current buffer size
Serializable::SizeType getBuffLeft() const
returns how much deserialization buffer is left
Include length as first token in serialization.
Data was the wrong format (e.g. wrong packet type)
U8 * m_buff
pointer to external buffer
#define FW_SERIALIZE_TRUE_VALUE
Value encoded during serialization for boolean true.
Definition: FpConfig.h:32
SerializeStatus serializeSize(const FwSizeType size)
serialize a size value
Data was left in the buffer, but not enough to deserialize.
FwSizeType SizeType
SerializeStatus deserializeSize(FwSizeType &size)
deserialize a size value
Serializable()
Default constructor.
void resetDeser()
reset deserialization to beginning
SerializeStatus copyRawOffset(SerializeBufferBase &dest, Serializable::SizeType size)
directly copies buffer without looking for a size in the stream.
SerializeStatus moveDeserToOffset(FwSizeType offset)
Moves deserialization to the specified offset.
SerializeStatus moveSerToOffset(FwSizeType offset)
Moves serialization to the specified offset.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
virtual Serializable::SizeType getBuffCapacity() const =0
returns capacity, not current size, of buffer
SerializeStatus deserializeSkip(FwSizeType numBytesToSkip)
Skips the number of specified bytes for deserialization.
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Omit length from serialization.
virtual ~Serializable()
destructor
Serializable::SizeType m_deserLoc
current offset for deserialization
SerializeStatus deserialize(U8 &val)
double F64
64-bit floating point (double). Required for compiler-supplied double promotion.
Definition: BasicTypes.h:85
SerializeBufferBase & operator=(const SerializeBufferBase &src)
copy assignment operator
SerializeStatus setBuff(const U8 *src, Serializable::SizeType length)
sets buffer contents and size
forward declaration
SerializeStatus serializeFrom(U8 val)
serialize 8-bit unsigned int
Serializable::SizeType getBuffCapacity() const
returns capacity, not current size, of buffer
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
virtual SerializeStatus deserializeFrom(SerializeBufferBase &buffer)=0
deserialize contents from buffer
#define FW_ASSERT(...)
Definition: Assert.hpp:14
SerializeStatus serializeSkip(FwSizeType numBytesToSkip)
Skips the number of specified bytes for serialization.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
U8 * getBuffAddr()
gets buffer address for data filling
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data
SerializeStatus deserializeTo(U8 &val)
deserialize 8-bit unsigned int
virtual SerializeStatus serializeTo(SerializeBufferBase &buffer) const =0
serialize contents to buffer
#define U64(C)
Definition: sha.h:180