F´ Flight Software - C/C++ Documentation  NASA-v2.0.1
A framework for building embedded system applications to NASA flight quality standards.
TlmString.cpp
Go to the documentation of this file.
3 #include <Fw/Tlm/TlmString.hpp>
4 #include <Fw/Types/Assert.hpp>
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 namespace Fw {
10 
11  TlmString::TlmString(const char* src) : StringBase(), m_maxSer(FW_TLM_STRING_MAX_SIZE) {
12  this->copyBuff(src,sizeof(this->m_buf));
13  }
14 
16  this->copyBuff(src.toChar(),sizeof(this->m_buf));
17  }
18 
20  this->copyBuff(src.m_buf,sizeof(this->m_buf));
21  }
22 
24  this->m_buf[0] = 0;
25  }
26 
28  }
29 
31  return strnlen(this->m_buf,sizeof(this->m_buf));
32  }
33 
34  const char* TlmString::toChar(void) const {
35  return this->m_buf;
36  }
37 
38  void TlmString::copyBuff(const char* buff, NATIVE_UINT_TYPE size) {
39  FW_ASSERT(buff);
40  // check for self copy
41  if (buff != this->m_buf) {
42  (void)strncpy(this->m_buf,buff,size);
43  // NULL terminate
44  this->terminate(sizeof(this->m_buf));
45  }
46  }
47 
49  NATIVE_UINT_TYPE strSize = strnlen(this->m_buf,sizeof(this->m_buf));
50 #if FW_AMPCS_COMPATIBLE
51  // serialize string in AMPC compatible way
52  // AMPC requires an 8-bit argument size value before the string
53 
54  // Omit the null terminator character because AMPCS does not like
55  // \0 in its strings. So subtract 1 from strSize
56  strSize--;
57 
58  // serialize 8-bit size
59  SerializeStatus stat = buffer.serialize(static_cast<U8>(strSize));
60  if (stat != FW_SERIALIZE_OK) {
61  return stat;
62  }
63  return buffer.serialize((U8*)this->m_buf,strSize,true);
64 #else
65  return buffer.serialize((U8*)this->m_buf,strSize);
66 #endif
67  }
68 
70  NATIVE_UINT_TYPE maxSize = sizeof(this->m_buf);
71  // deserialize string
72 #if FW_AMPCS_COMPATIBLE
73  // AMPCS encodes 8-bit string size
74  U8 strSize;
75  SerializeStatus stat = buffer.deserialize(strSize);
76  if (stat != FW_SERIALIZE_OK) {
77  return stat;
78  }
79  NATIVE_UINT_TYPE buffSize = strSize;
80  // To make sure there is space when we add the null terminator
81  // which was omitted in the serialization of this buffer
82  strSize++;
83  stat = buffer.deserialize((U8*)this->m_buf,buffSize,true);
84  this->m_buf[strSize-1] = 0;
85 #else
86  // deserialize string
87  SerializeStatus stat = buffer.deserialize((U8*)this->m_buf,maxSize);
88 #endif
89  // make sure it is null-terminated
90  this->terminate(maxSize);
91 
92  return stat;
93  }
94 
96  this->m_maxSer = FW_MIN(size,FW_TLM_STRING_MAX_SIZE);
97  }
98 
99  NATIVE_UINT_TYPE TlmString::getCapacity(void) const {
100  return FW_TLM_STRING_MAX_SIZE;
101  }
102 
103  void TlmString::terminate(NATIVE_UINT_TYPE size) {
104  // null terminate the string
105  this->m_buf[size < sizeof(this->m_buf)?size:sizeof(this->m_buf)-1] = 0;
106  }
107 
108  const TlmString& TlmString::operator=(const TlmString& other) {
109  this->copyBuff(other.m_buf,this->getCapacity());
110  return *this;
111  }
112 
113 
114 #if FW_SERIALIZABLE_TO_STRING
115  void TlmString::toString(StringBase& text) const {
116  text = this->m_buf;
117  }
118 #endif
119 }
StringType.hpp
Declares ISF string base class.
Fw::TlmString
Definition: TlmString.hpp:11
Fw::SerializeBufferBase
Definition: Serializable.hpp:43
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
Fw::SerializeBufferBase::serialize
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
Definition: Serializable.cpp:67
Fw::StringBase
Definition: StringType.hpp:23
Fw::TlmString::TlmString
TlmString(void)
Definition: TlmString.cpp:23
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Fw::TlmString::toChar
const char * toChar(void) const
Definition: TlmString.cpp:34
Fw::TlmString::deserialize
SerializeStatus deserialize(SerializeBufferBase &buffer)
deserialize to contents
Definition: TlmString.cpp:69
Assert.hpp
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
FW_MIN
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.hpp:106
Fw::TlmString::setMaxSerialize
void setMaxSerialize(NATIVE_UINT_TYPE size)
Definition: TlmString.cpp:95
TlmString.hpp
Fw::TlmString::serialize
SerializeStatus serialize(SerializeBufferBase &buffer) const
serialize contents
Definition: TlmString.cpp:48
Fw::StringBase::toChar
virtual const char * toChar(void) const =0
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
Fw::TlmString::length
NATIVE_UINT_TYPE length(void) const
Get length of string.
Definition: TlmString.cpp:30
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Fw::TlmString::operator=
const TlmString & operator=(const TlmString &other)
equal operator for other strings
Definition: TlmString.cpp:108
Fw::SerializeBufferBase::deserialize
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
Definition: Serializable.cpp:290
FW_TLM_STRING_MAX_SIZE
#define FW_TLM_STRING_MAX_SIZE
Max size of channelized telemetry string type.
Definition: FpConfig.hpp:261
Fw::TlmString::~TlmString
~TlmString(void)
Definition: TlmString.cpp:27
BasicTypes.hpp
Declares ISF basic types.
Fw
Definition: Buffer.cpp:21