F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Serializable.cpp
Go to the documentation of this file.
1 #include <FpConfig.hpp>
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 #if FW_SERIALIZABLE_TO_STRING || FW_ENABLE_TEXT_LOGGING || BUILD_UT
21 
22 void Serializable::toString(StringBase& text) const {
23  text = "NOSPEC"; // set to not specified.
24 }
25 
26 #endif
27 
28 #ifdef BUILD_UT
29 std::ostream& operator<<(std::ostream& os, const Serializable& val) {
30  Fw::String out;
31  val.toString(out);
32 
33  os << out;
34 
35  return os;
36 }
37 #endif
38 
39 SerializeBufferBase::SerializeBufferBase() : m_serLoc(0), m_deserLoc(0) {}
40 
42 
43 void SerializeBufferBase::copyFrom(const SerializeBufferBase& src) {
44  this->m_serLoc = src.m_serLoc;
45  this->m_deserLoc = src.m_deserLoc;
46  FW_ASSERT(src.getBuffAddr());
47  FW_ASSERT(this->getBuffAddr());
48  // destination has to be same or bigger
49  FW_ASSERT(
50  src.getBuffLength() <= this->getBuffCapacity(),
51  static_cast<FwAssertArgType>(src.getBuffLength()),
52  static_cast<FwAssertArgType>(this->getBuffLength()));
53  (void)memcpy(this->getBuffAddr(), src.getBuffAddr(), this->m_serLoc);
54 }
55 
56 // Copy constructor doesn't make sense in this virtual class as there is nothing to copy. Derived classes should
57 // call the empty constructor and then call their own copy function
59  this->copyFrom(src);
60  return *this;
61 }
62 
63 // serialization routines
64 
66  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
68  }
69  FW_ASSERT(this->getBuffAddr());
70  this->getBuffAddr()[this->m_serLoc] = val;
71  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
72  this->m_deserLoc = 0;
73 
74  return FW_SERIALIZE_OK;
75 }
76 
78  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
80  }
81  FW_ASSERT(this->getBuffAddr());
82  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val);
83  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
84  this->m_deserLoc = 0;
85  return FW_SERIALIZE_OK;
86 }
87 
88 #if FW_HAS_16_BIT == 1
90  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
92  }
93  FW_ASSERT(this->getBuffAddr());
94  // MSB first
95  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 8);
96  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val);
97  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
98  this->m_deserLoc = 0;
99  return FW_SERIALIZE_OK;
100 }
101 
103  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
105  }
106  FW_ASSERT(this->getBuffAddr());
107  // MSB first
108  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 8);
109  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val);
110  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
111  this->m_deserLoc = 0;
112  return FW_SERIALIZE_OK;
113 }
114 #endif
115 #if FW_HAS_32_BIT == 1
117  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
119  }
120  FW_ASSERT(this->getBuffAddr());
121  // MSB first
122  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 24);
123  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 16);
124  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 8);
125  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val);
126  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
127  this->m_deserLoc = 0;
128  return FW_SERIALIZE_OK;
129 }
130 
132  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
134  }
135  FW_ASSERT(this->getBuffAddr());
136  // MSB first
137  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 24);
138  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 16);
139  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 8);
140  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val);
141  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
142  this->m_deserLoc = 0;
143  return FW_SERIALIZE_OK;
144 }
145 #endif
146 
147 #if FW_HAS_64_BIT == 1
149  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
151  }
152  FW_ASSERT(this->getBuffAddr());
153  // MSB first
154  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 56);
155  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 48);
156  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 40);
157  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val >> 32);
158  this->getBuffAddr()[this->m_serLoc + 4] = static_cast<U8>(val >> 24);
159  this->getBuffAddr()[this->m_serLoc + 5] = static_cast<U8>(val >> 16);
160  this->getBuffAddr()[this->m_serLoc + 6] = static_cast<U8>(val >> 8);
161  this->getBuffAddr()[this->m_serLoc + 7] = static_cast<U8>(val);
162  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
163  this->m_deserLoc = 0;
164  return FW_SERIALIZE_OK;
165 }
166 
168  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->getBuffCapacity()) {
170  }
171  FW_ASSERT(this->getBuffAddr());
172  // MSB first
173  this->getBuffAddr()[this->m_serLoc + 0] = static_cast<U8>(val >> 56);
174  this->getBuffAddr()[this->m_serLoc + 1] = static_cast<U8>(val >> 48);
175  this->getBuffAddr()[this->m_serLoc + 2] = static_cast<U8>(val >> 40);
176  this->getBuffAddr()[this->m_serLoc + 3] = static_cast<U8>(val >> 32);
177  this->getBuffAddr()[this->m_serLoc + 4] = static_cast<U8>(val >> 24);
178  this->getBuffAddr()[this->m_serLoc + 5] = static_cast<U8>(val >> 16);
179  this->getBuffAddr()[this->m_serLoc + 6] = static_cast<U8>(val >> 8);
180  this->getBuffAddr()[this->m_serLoc + 7] = static_cast<U8>(val);
181  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val));
182  this->m_deserLoc = 0;
183  return FW_SERIALIZE_OK;
184 }
185 #endif
186 
187 #if FW_HAS_F64 && FW_HAS_64_BIT
188 
190  // floating point values need to be byte-swapped as well, so copy to U64 and use that routine
191  U64 u64Val;
192  (void)memcpy(&u64Val, &val, sizeof(val));
193  return this->serialize(u64Val);
194 }
195 
196 #endif
197 
199  // floating point values need to be byte-swapped as well, so copy to U32 and use that routine
200  U32 u32Val;
201  (void)memcpy(&u32Val, &val, sizeof(val));
202  return this->serialize(u32Val);
203 }
204 
206  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(U8)) - 1 >= this->getBuffCapacity()) {
208  }
209 
210  FW_ASSERT(this->getBuffAddr());
211  if (val) {
212  this->getBuffAddr()[this->m_serLoc + 0] = FW_SERIALIZE_TRUE_VALUE;
213  } else {
214  this->getBuffAddr()[this->m_serLoc + 0] = FW_SERIALIZE_FALSE_VALUE;
215  }
216 
217  this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(U8));
218  this->m_deserLoc = 0;
219  return FW_SERIALIZE_OK;
220 }
221 
223  if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(void*)) - 1 >= this->getBuffCapacity()) {
225  }
226 
227  return this->serialize(reinterpret_cast<POINTER_CAST>(val));
228 }
229 
231  return this->serialize(buff, static_cast<FwSizeType>(length), Serialization::INCLUDE_LENGTH);
232 }
233 
235  return this->serialize(buff, static_cast<FwSizeType>(length),
237 }
238 
240  // First serialize length
241  SerializeStatus stat;
242  if (mode == Serialization::INCLUDE_LENGTH) {
243  stat = this->serialize(static_cast<FwSizeStoreType>(length));
244  if (stat != FW_SERIALIZE_OK) {
245  return stat;
246  }
247  }
248 
249  // make sure we have enough space
250  if (this->m_serLoc + length > this->getBuffCapacity()) {
252  }
253 
254  // copy buffer to our buffer
255  (void)memcpy(&this->getBuffAddr()[this->m_serLoc], buff, length);
256  this->m_serLoc += static_cast<Serializable::SizeType>(length);
257  this->m_deserLoc = 0;
258 
259  return FW_SERIALIZE_OK;
260 }
261 
263  return val.serialize(*this);
264 }
265 
268  if (this->m_serLoc + size + static_cast<Serializable::SizeType>(sizeof(FwSizeStoreType)) >
269  this->getBuffCapacity()) {
271  }
272 
273  // First, serialize size
274  SerializeStatus stat = this->serialize(static_cast<FwSizeStoreType>(size));
275  if (stat != FW_SERIALIZE_OK) {
276  return stat;
277  }
278 
279  FW_ASSERT(this->getBuffAddr());
280  FW_ASSERT(val.getBuffAddr());
281  // serialize buffer
282  (void)memcpy(&this->getBuffAddr()[this->m_serLoc], val.getBuffAddr(), size);
283  this->m_serLoc += size;
284  this->m_deserLoc = 0;
285 
286  return FW_SERIALIZE_OK;
287 }
288 
291  if ((size < std::numeric_limits<FwSizeStoreType>::min()) || (size > std::numeric_limits<FwSizeStoreType>::max())) {
292  status = FW_SERIALIZE_FORMAT_ERROR;
293  }
294  if (status == FW_SERIALIZE_OK) {
295  status = this->serialize(static_cast<FwSizeStoreType>(size));
296  }
297  return status;
298 }
299 
300 // deserialization routines
301 
303  // check for room
304  if (this->getBuffLength() == this->m_deserLoc) {
306  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
308  }
309  // read from current location
310  FW_ASSERT(this->getBuffAddr());
311  val = this->getBuffAddr()[this->m_deserLoc + 0];
312  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
313  return FW_SERIALIZE_OK;
314 }
315 
317  // check for room
318  if (this->getBuffLength() == this->m_deserLoc) {
320  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
322  }
323  // read from current location
324  FW_ASSERT(this->getBuffAddr());
325  val = static_cast<I8>(this->getBuffAddr()[this->m_deserLoc + 0]);
326  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
327  return FW_SERIALIZE_OK;
328 }
329 
330 #if FW_HAS_16_BIT == 1
332  // check for room
333  if (this->getBuffLength() == this->m_deserLoc) {
335  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
337  }
338  // read from current location
339  FW_ASSERT(this->getBuffAddr());
340  // MSB first
341  val = static_cast<U16>(((this->getBuffAddr()[this->m_deserLoc + 1]) << 0) |
342  ((this->getBuffAddr()[this->m_deserLoc + 0]) << 8));
343  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
344  return FW_SERIALIZE_OK;
345 }
346 
348  // check for room
349  if (this->getBuffLength() == this->m_deserLoc) {
351  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
353  }
354  // read from current location
355  FW_ASSERT(this->getBuffAddr());
356  // MSB first
357  val = static_cast<I16>(((this->getBuffAddr()[this->m_deserLoc + 1]) << 0) |
358  ((this->getBuffAddr()[this->m_deserLoc + 0]) << 8));
359  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
360  return FW_SERIALIZE_OK;
361 }
362 #endif
363 #if FW_HAS_32_BIT == 1
365  // check for room
366  if (this->getBuffLength() == this->m_deserLoc) {
368  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
370  }
371  // read from current location
372  FW_ASSERT(this->getBuffAddr());
373  // MSB first
374  val = (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 3]) << 0) |
375  (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 2]) << 8) |
376  (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 1]) << 16) |
377  (static_cast<U32>(this->getBuffAddr()[this->m_deserLoc + 0]) << 24);
378  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
379  return FW_SERIALIZE_OK;
380 }
381 
383  // check for room
384  if (this->getBuffLength() == this->m_deserLoc) {
386  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
388  }
389  // read from current location
390  FW_ASSERT(this->getBuffAddr());
391  // MSB first
392  val = (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 3]) << 0) |
393  (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 2]) << 8) |
394  (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 1]) << 16) |
395  (static_cast<I32>(this->getBuffAddr()[this->m_deserLoc + 0]) << 24);
396  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
397  return FW_SERIALIZE_OK;
398 }
399 #endif
400 
401 #if FW_HAS_64_BIT == 1
402 
404  // check for room
405  if (this->getBuffLength() == this->m_deserLoc) {
407  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
409  }
410  // read from current location
411  FW_ASSERT(this->getBuffAddr());
412  // MSB first
413  val = (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 7]) << 0) |
414  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 6]) << 8) |
415  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 5]) << 16) |
416  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 4]) << 24) |
417  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 3]) << 32) |
418  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 2]) << 40) |
419  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 1]) << 48) |
420  (static_cast<U64>(this->getBuffAddr()[this->m_deserLoc + 0]) << 56);
421 
422  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
423  return FW_SERIALIZE_OK;
424 }
425 
427  // check for room
428  if (this->getBuffLength() == this->m_deserLoc) {
430  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) {
432  }
433  // read from current location
434  FW_ASSERT(this->getBuffAddr());
435  // MSB first
436  val = (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 7]) << 0) |
437  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 6]) << 8) |
438  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 5]) << 16) |
439  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 4]) << 24) |
440  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 3]) << 32) |
441  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 2]) << 40) |
442  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 1]) << 48) |
443  (static_cast<I64>(this->getBuffAddr()[this->m_deserLoc + 0]) << 56);
444  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val));
445  return FW_SERIALIZE_OK;
446 }
447 #endif
448 
449 #if FW_HAS_F64
450 
452  // deserialize as 64-bit int to handle endianness
453  U64 tempVal;
454  SerializeStatus stat = this->deserialize(tempVal);
455  if (stat != FW_SERIALIZE_OK) {
456  return stat;
457  }
458  // copy to argument
459  (void)memcpy(&val, &tempVal, sizeof(val));
460 
461  return FW_SERIALIZE_OK;
462 }
463 
464 #endif
465 
467  // check for room
468  if (this->getBuffLength() == this->m_deserLoc) {
470  } else if (this->getBuffLength() - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(U8))) {
472  }
473  // read from current location
474  FW_ASSERT(this->getBuffAddr());
475  if (FW_SERIALIZE_TRUE_VALUE == this->getBuffAddr()[this->m_deserLoc + 0]) {
476  val = true;
477  } else if (FW_SERIALIZE_FALSE_VALUE == this->getBuffAddr()[this->m_deserLoc + 0]) {
478  val = false;
479  } else {
481  }
482 
483  this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(U8));
484  return FW_SERIALIZE_OK;
485 }
486 
488  // Deserialize as pointer cast, then convert to void*
489  PlatformPointerCastType pointerCastVal = 0;
490  const SerializeStatus stat = this->deserialize(pointerCastVal);
491  if (stat == FW_SERIALIZE_OK) {
492  val = reinterpret_cast<void*>(pointerCastVal);
493  }
494  return stat;
495 }
496 
498  // deserialize as 64-bit int to handle endianness
499  U32 tempVal;
500  SerializeStatus stat = this->deserialize(tempVal);
501  if (stat != FW_SERIALIZE_OK) {
502  return stat;
503  }
504  (void)memcpy(&val, &tempVal, sizeof(val));
505 
506  return FW_SERIALIZE_OK;
507 }
508 
510  FwSizeType length_in_out = static_cast<FwSizeType>(length);
511  SerializeStatus status = this->deserialize(buff, length_in_out, Serialization::INCLUDE_LENGTH);
512  length = static_cast<Serializable::SizeType>(length_in_out);
513  return status;
514 }
515 
517  FwSizeType length_in_out = static_cast<FwSizeType>(length);
518  SerializeStatus status =
519  this->deserialize(buff, length_in_out, noLength ? Serialization::OMIT_LENGTH : Serialization::INCLUDE_LENGTH);
520  length = static_cast<Serializable::SizeType>(length_in_out);
521  return status;
522 }
523 
525  FW_ASSERT(this->getBuffAddr());
526 
527  if (mode == Serialization::INCLUDE_LENGTH) {
528  FwSizeStoreType storedLength;
529 
530  SerializeStatus stat = this->deserialize(storedLength);
531 
532  if (stat != FW_SERIALIZE_OK) {
533  return stat;
534  }
535 
536  // make sure it fits
537  if ((storedLength > this->getBuffLeft()) or (storedLength > length)) {
539  }
540 
541  (void)memcpy(buff, &this->getBuffAddr()[this->m_deserLoc], storedLength);
542 
543  length = static_cast<FwSizeType>(storedLength);
544 
545  } else {
546  // make sure enough is left
547  if (length > this->getBuffLeft()) {
549  }
550 
551  (void)memcpy(buff, &this->getBuffAddr()[this->m_deserLoc], length);
552  }
553 
554  this->m_deserLoc += static_cast<Serializable::SizeType>(length);
555  return FW_SERIALIZE_OK;
556 }
557 
559  return val.deserialize(*this);
560 }
561 
563  FW_ASSERT(val.getBuffAddr());
565 
566  FwSizeStoreType storedLength;
567 
568  stat = this->deserialize(storedLength);
569 
570  if (stat != FW_SERIALIZE_OK) {
571  return stat;
572  }
573 
574  // make sure destination has enough room
575 
576  if ((storedLength > val.getBuffCapacity()) or (storedLength > this->getBuffLeft())) {
578  }
579 
580  FW_ASSERT(this->getBuffAddr());
581  (void)memcpy(val.getBuffAddr(), &this->getBuffAddr()[this->m_deserLoc], storedLength);
582 
583  stat = val.setBuffLen(storedLength);
584 
585  if (stat != FW_SERIALIZE_OK) {
586  return stat;
587  }
588 
589  this->m_deserLoc += storedLength;
590 
591  return FW_SERIALIZE_OK;
592 }
593 
595  FwSizeStoreType storedSize = 0;
596  Fw::SerializeStatus status = this->deserialize(storedSize);
597  if (status == FW_SERIALIZE_OK) {
598  size = static_cast<FwSizeType>(storedSize);
599  }
600  return status;
601 }
602 
604  this->m_deserLoc = 0;
605  this->m_serLoc = 0;
606 }
607 
609  this->m_deserLoc = 0;
610 }
611 
614  // compute new deser loc
615  const FwSizeType newSerLoc = this->m_serLoc + numBytesToSkip;
616  // check for room
617  if (newSerLoc <= this->getBuffCapacity()) {
618  // update deser loc
619  this->m_serLoc = static_cast<Serializable::SizeType>(newSerLoc);
620  } else {
621  status = FW_SERIALIZE_NO_ROOM_LEFT;
622  }
623  return status;
624 }
625 
627  // check for room
628  if (this->getBuffLength() == this->m_deserLoc) {
630  } else if (this->getBuffLength() - this->m_deserLoc < numBytesToSkip) {
632  }
633  // update location in buffer to skip the value
634  this->m_deserLoc += static_cast<Serializable::SizeType>(numBytesToSkip);
635  return FW_SERIALIZE_OK;
636 }
637 
639  // Reset serialization
640  this->resetSer();
641  // Advance to offset
642  return this->serializeSkip(offset);
643 }
645  // Reset deserialization
646  this->resetDeser();
647  // Advance to offset
648  return this->deserializeSkip(offset);
649 }
650 
652  return this->m_serLoc;
653 }
654 
656  if (this->getBuffCapacity() < length) {
658  } else {
659  FW_ASSERT(src);
660  FW_ASSERT(this->getBuffAddr());
661  memcpy(this->getBuffAddr(), src, length);
662  this->m_serLoc = length;
663  this->m_deserLoc = 0;
664  return FW_SERIALIZE_OK;
665  }
666 }
667 
669  if (this->getBuffCapacity() < length) {
671  } else {
672  this->m_serLoc = length;
673  this->m_deserLoc = 0;
674  return FW_SERIALIZE_OK;
675  }
676 }
677 
679  return this->m_serLoc - this->m_deserLoc;
680 }
681 
683  // make sure there is sufficient size in destination
684  if (dest.getBuffCapacity() < size) {
686  }
687  // otherwise, set destination buffer to data from deserialization pointer plus size
688  SerializeStatus stat = dest.setBuff(&this->getBuffAddr()[this->m_deserLoc], size);
689  if (stat == FW_SERIALIZE_OK) {
690  this->m_deserLoc += size;
691  }
692  return stat;
693 }
694 
696  // make sure there is sufficient size in destination
697  if (dest.getBuffCapacity() < size + dest.getBuffLength()) {
699  }
700  // make sure there is sufficient buffer in source
701  if (this->getBuffLeft() < size) {
703  }
704 
705  // otherwise, serialize bytes to destination without writing length
706  SerializeStatus stat = dest.serialize(&this->getBuffAddr()[this->m_deserLoc], size, true);
707  if (stat == FW_SERIALIZE_OK) {
708  this->m_deserLoc += size;
709  }
710  return stat;
711 }
712 
713 // return address of buffer not yet deserialized. This is used
714 // to copy the remainder of a buffer.
716  return &this->getBuffAddr()[this->m_deserLoc];
717 }
718 
721  return &this->getBuffAddr()[this->m_serLoc];
722 }
723 
724 #ifdef BUILD_UT
725 bool SerializeBufferBase::operator==(const SerializeBufferBase& other) const {
726  if (this->getBuffLength() != other.getBuffLength()) {
727  return false;
728  }
729 
730  const U8* us = this->getBuffAddr();
731  const U8* them = other.getBuffAddr();
732 
733  FW_ASSERT(us);
734  FW_ASSERT(them);
735 
736  for (Serializable::SizeType byte = 0; byte < this->getBuffLength(); byte++) {
737  if (us[byte] != them[byte]) {
738  return false;
739  }
740  }
741 
742  return true;
743 }
744 
745 std::ostream& operator<<(std::ostream& os, const SerializeBufferBase& buff) {
746  const U8* us = buff.getBuffAddr();
747 
748  FW_ASSERT(us);
749 
750  for (Serializable::SizeType byte = 0; byte < buff.getBuffLength(); byte++) {
751  os << "[" << std::setw(2) << std::hex << std::setfill('0') << us[byte] << "]" << std::dec;
752  }
753 
754  return os;
755 }
756 #endif
757 
759  this->setExtBuffer(buffPtr, size);
760 }
761 
763  this->clear();
764 }
765 
767  FW_ASSERT(buffPtr != nullptr);
768  this->m_buff = buffPtr;
769  this->m_buffSize = size;
770 }
771 
773  this->m_buff = nullptr;
774  this->m_buffSize = 0;
775 }
776 
778  return this->m_buffSize;
779 }
780 
782  return this->m_buff;
783 }
784 
786  return this->m_buff;
787 }
788 
789 } // namespace Fw
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:25
float F32
32-bit floating point
Definition: BasicTypes.h:45
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
uint8_t PlatformPointerCastType
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
#define FW_SERIALIZE_TRUE_VALUE
Value encoded during serialization for boolean true.
Definition: FpConfig.h:126
#define FW_SERIALIZE_FALSE_VALUE
Value encoded during serialization for boolean false.
Definition: FpConfig.h:130
U16 FwSizeStoreType
Definition: FpConfig.h:46
PlatformSizeType FwSizeType
Definition: FpConfig.h:30
C++-compatible configuration header for fprime configuration.
U8 * getBuffAddr()
gets buffer address for data filling
Serializable::SizeType getBuffCapacity() const
returns capacity, not current size, of buffer
U8 * m_buff
pointer to external buffer
void setExtBuffer(U8 *buffPtr, Serializable::SizeType size)
Set the external buffer.
void clear()
clear external buffer
ExternalSerializeBuffer()
default constructor
Serializable::SizeType m_buffSize
size of external buffer
forward declaration
NATIVE_UINT_TYPE SizeType
Serializable()
Default constructor.
virtual ~Serializable()
destructor
virtual SerializeStatus deserialize(SerializeBufferBase &buffer)=0
deserialize to contents
virtual SerializeStatus serialize(SerializeBufferBase &buffer) const =0
serialize contents
@ OMIT_LENGTH
Omit length from serialization.
@ INCLUDE_LENGTH
Include length as first token in serialization.
virtual U8 * getBuffAddr()=0
gets buffer address for data filling
SerializeStatus deserializeSize(FwSizeType &size)
deserialize a size value
SerializeStatus setBuffLen(Serializable::SizeType length)
sets buffer length manually after filling with data
void resetDeser()
reset deserialization to beginning
SerializeStatus moveDeserToOffset(FwSizeType offset)
Moves deserialization to the specified offset.
Serializable::SizeType getBuffLeft() const
returns how much deserialization buffer is left
const U8 * getBuffAddrLeft() const
gets address of remaining non-deserialized data.
void resetSer()
reset to beginning of buffer to reuse for serialization
SerializeStatus serializeSkip(FwSizeType numBytesToSkip)
Skips the number of specified bytes for serialization.
SerializeStatus moveSerToOffset(FwSizeType offset)
Moves serialization to the specified offset.
SerializeBufferBase()
default constructor
Serializable::SizeType getBuffLength() const
returns current buffer size
SerializeBufferBase & operator=(const SerializeBufferBase &src)
copy assignment operator
SerializeStatus setBuff(const U8 *src, Serializable::SizeType length)
sets buffer contents and size
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
virtual ~SerializeBufferBase()
destructor
SerializeStatus deserializeSkip(FwSizeType numBytesToSkip)
Skips the number of specified bytes for deserialization.
SerializeStatus copyRaw(SerializeBufferBase &dest, Serializable::SizeType size)
directly copies buffer without looking for a size in the stream.
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
SerializeStatus copyRawOffset(SerializeBufferBase &dest, Serializable::SizeType size)
directly copies buffer without looking for a size in the stream.
virtual Serializable::SizeType getBuffCapacity() const =0
returns capacity, not current size, of buffer
SerializeStatus serializeSize(const FwSizeType size)
serialize a size value
SerializeStatus
forward declaration for string
@ FW_DESERIALIZE_FORMAT_ERROR
Deserialization data had incorrect values (unexpected data types)
@ FW_DESERIALIZE_BUFFER_EMPTY
Deserialization buffer was empty when trying to read more data.
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
@ FW_SERIALIZE_FORMAT_ERROR
Data was the wrong format (e.g. wrong packet type)
@ FW_DESERIALIZE_SIZE_MISMATCH
Data was left in the buffer, but not enough to deserialize.
@ FW_SERIALIZE_NO_ROOM_LEFT
No room left in the buffer to serialize data.
#define U64(C)
Definition: sha.h:176