Skip to content

Commit

Permalink
Add Fw::ObjectName to hold Fw::ObjBase name (#2497)
Browse files Browse the repository at this point in the history
* Implement Fw::ObjectName

* format

* Add test case

* Fix PolyDb UT and spelling

* Explicit constructor and use CHAR

* Revise ObjBase.hpp

Don't include ObjectName header unless object names are enabled

* Update requirements.txt for fpp==2.1.0a4

* Fix static code analysis warnings

* cast string_copy return value to void

* Resolve unchecked argument

---------

Co-authored-by: Robert L. Bocchino Jr <bocchino@jpl.nasa.gov>
  • Loading branch information
thomas-bc and bocchino authored Mar 5, 2024
1 parent c1d51d3 commit bdfe241
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 31 deletions.
1 change: 1 addition & 0 deletions Fw/Cfg/SerIds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Fw {
FW_TYPEID_EIGHTY_CHAR_STRING = 50, //!< 80 char string Buffer type id
FW_TYPEID_INTERNAL_INTERFACE_STRING = 51, //!< interface string Buffer type id
FW_TYPEID_FIXED_LENGTH_STRING = 52, //!< 256 char string Buffer type id
FW_TYPEID_OBJECT_NAME = 53, //!< ObjectName string Buffer type id
};
}

Expand Down
4 changes: 3 additions & 1 deletion Fw/Comp/ActiveComponentBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ namespace Fw {
#if FW_OBJECT_TO_STRING == 1 && FW_OBJECT_NAMES == 1
void ActiveComponentBase::toString(char* buffer, NATIVE_INT_TYPE size) {
FW_ASSERT(size > 0);
if (snprintf(buffer, size, "ActComp: %s", this->m_objName) < 0) {
FW_ASSERT(buffer != nullptr);
PlatformIntType status = snprintf(buffer, size, "ActComp: %s", this->m_objName.toChar());
if (status < 0) {
buffer[0] = 0;
}
}
Expand Down
5 changes: 3 additions & 2 deletions Fw/Comp/PassiveComponentBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ namespace Fw {

#if FW_OBJECT_TO_STRING == 1 && FW_OBJECT_NAMES == 1
void PassiveComponentBase::toString(char* buffer, NATIVE_INT_TYPE size) {
FW_ASSERT(buffer);
FW_ASSERT(size > 0);
if (snprintf(buffer, size, "Comp: %s", this->m_objName) < 0) {
FW_ASSERT(buffer != nullptr);
PlatformIntType status = snprintf(buffer, size, "Comp: %s", this->m_objName.toChar());
if (status < 0) {
buffer[0] = 0;
}
}
Expand Down
4 changes: 3 additions & 1 deletion Fw/Comp/QueuedComponentBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ namespace Fw {
#if FW_OBJECT_TO_STRING == 1 && FW_OBJECT_NAMES == 1
void QueuedComponentBase::toString(char* buffer, NATIVE_INT_TYPE size) {
FW_ASSERT(size > 0);
if (snprintf(buffer, size,"QueueComp: %s", this->m_objName) < 0) {
FW_ASSERT(buffer != nullptr);
PlatformIntType status = snprintf(buffer, size, "QueueComp: %s", this->m_objName.toChar());
if (status < 0) {
buffer[0] = 0;
}
}
Expand Down
9 changes: 5 additions & 4 deletions Fw/Obj/ObjBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ namespace Fw {

#if FW_OBJECT_NAMES == 1
const char* ObjBase::getObjName() {
return this->m_objName;
return this->m_objName.toChar();
}

void ObjBase::setObjName(const char* name) {
strncpy(this->m_objName, name, sizeof(this->m_objName));
this->m_objName[sizeof(this->m_objName)-1] = 0;
this->m_objName = name;
}
#if FW_OBJECT_TO_STRING == 1
void ObjBase::toString(char* str, NATIVE_INT_TYPE size) {
FW_ASSERT(size > 0);
if (snprintf(str, size, "Obj: %s",this->m_objName) < 0) {
FW_ASSERT(str != nullptr);
PlatformIntType status = snprintf(str, size, "Obj: %s", this->m_objName.toChar());
if (status < 0) {
str[0] = 0;
}
}
Expand Down
5 changes: 4 additions & 1 deletion Fw/Obj/ObjBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#define FW_OBJ_BASE_HPP

#include <FpConfig.hpp>
#if FW_OBJECT_NAMES == 1
#include <Fw/Types/ObjectName.hpp>
#endif

namespace Fw {

Expand Down Expand Up @@ -79,7 +82,7 @@ namespace Fw {
protected:

#if FW_OBJECT_NAMES == 1
char m_objName[FW_OBJ_NAME_MAX_SIZE]; //!< stores object name
Fw::ObjectName m_objName; //!< stores object name
#endif

//! \brief ObjBase constructor
Expand Down
6 changes: 4 additions & 2 deletions Fw/Port/InputPortBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ namespace Fw {
void InputPortBase::toString(char* buffer, NATIVE_INT_TYPE size) {
#if FW_OBJECT_NAMES == 1
FW_ASSERT(size > 0);
if (snprintf(buffer, size, "InputPort: %s->%s", this->m_objName,
this->isConnected() ? this->m_connObj->getObjName() : "None") < 0) {
FW_ASSERT(buffer != nullptr);
PlatformIntType status = snprintf(buffer, size, "InputPort: %s->%s", this->m_objName.toChar(),
this->isConnected() ? this->m_connObj->getObjName() : "None");
if (status < 0) {
buffer[0] = 0;
}
#else
Expand Down
2 changes: 1 addition & 1 deletion Fw/Port/InputSerializePort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Fw {
void InputSerializePort::toString(char* buffer, NATIVE_INT_TYPE size) {
#if FW_OBJECT_NAMES == 1
FW_ASSERT(size > 0);
if (snprintf(buffer, size, "Input Serial Port: %s %s->(%s)", this->m_objName, this->isConnected() ? "C" : "NC",
if (snprintf(buffer, size, "Input Serial Port: %s %s->(%s)", this->m_objName.toChar(), this->isConnected() ? "C" : "NC",
this->isConnected() ? this->m_connObj->getObjName() : "None") < 0) {
buffer[0] = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Fw/Port/OutputPortBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Fw {
void OutputPortBase::toString(char* buffer, NATIVE_INT_TYPE size) {
#if FW_OBJECT_NAMES == 1
FW_ASSERT(size > 0);
if (snprintf(buffer, size, "OutputPort: %s %s->(%s)", this->m_objName, this->isConnected() ? "C" : "NC",
if (snprintf(buffer, size, "OutputPort: %s %s->(%s)", this->m_objName.toChar(), this->isConnected() ? "C" : "NC",
this->isConnected() ? this->m_connObj->getObjName() : "None") < 0) {
buffer[0] = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Fw/Port/OutputSerializePort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Fw {
void OutputSerializePort::toString(char* buffer, NATIVE_INT_TYPE size) {
#if FW_OBJECT_NAMES == 1
FW_ASSERT(size > 0);
if (snprintf(buffer, size, "Output Serial Port: %s %s->(%s)", this->m_objName, this->isConnected() ? "C" : "NC",
if (snprintf(buffer, size, "Output Serial Port: %s %s->(%s)", this->m_objName.toChar(), this->isConnected() ? "C" : "NC",
this->isConnected() ? this->m_connObj->getObjName() : "None") < 0) {
buffer[0] = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions Fw/Port/PortBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Fw {

if (do_trace) {
#if FW_OBJECT_NAMES == 1
Fw::Logger::logMsg("Trace: %s\n", reinterpret_cast<POINTER_CAST>(this->m_objName), 0, 0, 0, 0, 0);
Fw::Logger::logMsg("Trace: %s\n", reinterpret_cast<POINTER_CAST>(this->m_objName.toChar()), 0, 0, 0, 0, 0);
#else
Fw::Logger::logMsg("Trace: %p\n", reinterpret_cast<POINTER_CAST>(this), 0, 0, 0, 0, 0);
#endif
Expand All @@ -79,7 +79,7 @@ namespace Fw {
#if FW_OBJECT_TO_STRING == 1
void PortBase::toString(char* buffer, NATIVE_INT_TYPE size) {
FW_ASSERT(size > 0);
if (snprintf(buffer, size, "Port: %s %s->(%s)", this->m_objName, this->m_connObj ? "C" : "NC",
if (snprintf(buffer, size, "Port: %s %s->(%s)", this->m_objName.toChar(), this->m_connObj ? "C" : "NC",
this->m_connObj ? this->m_connObj->getObjName() : "None") < 0) {
buffer[0] = 0;
}
Expand Down
1 change: 1 addition & 0 deletions Fw/Types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(SOURCE_FILES "${CMAKE_CURRENT_LIST_DIR}/Types.fpp"
"${CMAKE_CURRENT_LIST_DIR}/InternalInterfaceString.cpp"
"${CMAKE_CURRENT_LIST_DIR}/MallocAllocator.cpp"
"${CMAKE_CURRENT_LIST_DIR}/MemAllocator.cpp"
"${CMAKE_CURRENT_LIST_DIR}/ObjectName.cpp"
"${CMAKE_CURRENT_LIST_DIR}/PolyType.cpp"
"${CMAKE_CURRENT_LIST_DIR}/SerialBuffer.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Serializable.cpp"
Expand Down
55 changes: 55 additions & 0 deletions Fw/Types/ObjectName.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <Fw/Types/ObjectName.hpp>
#include <Fw/Types/StringUtils.hpp>

namespace Fw {

ObjectName::ObjectName(const CHAR* src) : StringBase() {
(void) Fw::StringUtils::string_copy(this->m_buf, src, sizeof(this->m_buf));
}

ObjectName::ObjectName(const StringBase& src) : StringBase() {
(void) Fw::StringUtils::string_copy(this->m_buf, src.toChar(), sizeof(this->m_buf));
}

ObjectName::ObjectName(const ObjectName& src) : StringBase() {
(void) Fw::StringUtils::string_copy(this->m_buf, src.toChar(), sizeof(this->m_buf));
}

ObjectName::ObjectName() : StringBase() {
this->m_buf[0] = 0;
}

ObjectName& ObjectName::operator=(const ObjectName& other) {
if(this == &other) {
return *this;
}

(void) Fw::StringUtils::string_copy(this->m_buf, other.toChar(), sizeof(this->m_buf));
return *this;
}

ObjectName& ObjectName::operator=(const StringBase& other) {
if(this == &other) {
return *this;
}

(void) Fw::StringUtils::string_copy(this->m_buf, other.toChar(), sizeof(this->m_buf));
return *this;
}

ObjectName& ObjectName::operator=(const CHAR* other) {
(void) Fw::StringUtils::string_copy(this->m_buf, other, sizeof(this->m_buf));
return *this;
}

ObjectName::~ObjectName() {
}

const CHAR* ObjectName::toChar() const {
return this->m_buf;
}

NATIVE_UINT_TYPE ObjectName::getCapacity() const {
return STRING_SIZE;
}
}
37 changes: 37 additions & 0 deletions Fw/Types/ObjectName.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef FW_OBJECT_NAME_TYPE_HPP
#define FW_OBJECT_NAME_TYPE_HPP

#include <FpConfig.hpp>
#include <Fw/Types/StringType.hpp>
#include <Fw/Cfg/SerIds.hpp>

namespace Fw {

class ObjectName : public Fw::StringBase {
public:

enum {
SERIALIZED_TYPE_ID = FW_TYPEID_OBJECT_NAME, //!< typeid for string type
STRING_SIZE = FW_OBJ_NAME_MAX_SIZE, //!< Storage for string
SERIALIZED_SIZE = STRING_SIZE + sizeof(FwBuffSizeType) //!< Serialized size is size of buffer + size field
};

explicit ObjectName(const CHAR* src); //!< char* source constructor
explicit ObjectName(const StringBase& src); //!< StringBase string constructor
ObjectName(const ObjectName& src); //!< ObjectName string constructor
ObjectName(); //!< default constructor
ObjectName& operator=(const ObjectName& other); //!< assignment operator
ObjectName& operator=(const StringBase& other); //!< StringBase string assignment operator
ObjectName& operator=(const CHAR* other); //!< char* assignment operator
~ObjectName(); //!< destructor

const CHAR* toChar() const; //!< gets char buffer
NATIVE_UINT_TYPE getCapacity() const; //!< return buffer size

private:

CHAR m_buf[STRING_SIZE]; //!< storage for string data
};
}

#endif
2 changes: 2 additions & 0 deletions Fw/Types/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ char* Fw::StringUtils::string_copy(char* destination, const char* source, U32 nu
if(destination == source || num == 0) {
return destination;
}
FW_ASSERT(source != nullptr);
FW_ASSERT(destination != nullptr);

// Copying an overlapping range is undefined
U32 source_len = string_length(source, num) + 1;
Expand Down
31 changes: 31 additions & 0 deletions Fw/Types/test/ut/TypesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Fw/Types/Assert.hpp>
#include <Fw/Types/String.hpp>
#include <Fw/Types/InternalInterfaceString.hpp>
#include <Fw/Types/ObjectName.hpp>
#include <Fw/Types/PolyType.hpp>
#include <Fw/Types/MallocAllocator.hpp>
//
Expand Down Expand Up @@ -1214,6 +1215,36 @@ TEST(TypesTest,EightyCharTest) {

}

TEST(TypesTest,ObjectNameTest) {
Fw::ObjectName str;
str = "foo";
Fw::ObjectName str2;
str2 = "foo";
ASSERT_EQ(str,str2);
ASSERT_EQ(str,"foo");
str2 = "_bar";
ASSERT_NE(str,str2);

Fw::ObjectName str3 = str;
str3 += str2;
ASSERT_EQ(str3,"foo_bar");

str3 += "_foo";
ASSERT_EQ(str3,"foo_bar_foo");


Fw::ObjectName copyStr("ASTRING");
ASSERT_EQ(copyStr,"ASTRING");
Fw::ObjectName copyStr2(copyStr);
ASSERT_EQ(copyStr2,"ASTRING");

Fw::InternalInterfaceString ifstr("IfString");
Fw::ObjectName if2(ifstr);

ASSERT_EQ(ifstr,if2);
ASSERT_EQ(if2,"IfString");
}

TEST(TypesTest,StringFormatTest) {
Fw::String str;
str.format("Int %d String %s",10,"foo");
Expand Down
2 changes: 1 addition & 1 deletion Ref/RecvBuffApp/RecvBuffComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace Ref {

void RecvBuffImpl::toString(char* str, I32 buffer_size) {
#if FW_OBJECT_NAMES == 1
(void)snprintf(str, buffer_size, "RecvBuffImpl: %s: ATM recd count: %d", this->m_objName,
(void)snprintf(str, buffer_size, "RecvBuffImpl: %s: ATM recd count: %d", this->m_objName.toChar(),
(int) this->m_buffsReceived);
#else
(void)snprintf(str, buffer_size, "RecvBuffImpl: ATM recd count: %d",
Expand Down
2 changes: 1 addition & 1 deletion Ref/SendBuffApp/SendBuffComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace Ref {

void SendBuffImpl::toString(char* str, I32 buffer_size) {
#if FW_OBJECT_NAMES == 1
(void) snprintf(str, buffer_size, "Send Buff Component: %s: count: %d Buffs: %d", this->m_objName,
(void) snprintf(str, buffer_size, "Send Buff Component: %s: count: %d Buffs: %d", this->m_objName.toChar(),
(int) this->m_invocations, (int) this->m_buffsSent);
str[buffer_size-1] = 0;
#else
Expand Down
4 changes: 2 additions & 2 deletions Svc/PolyDb/test/ut/PolyDbComponentTestAc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Svc {
this->m_getValue_OutputPort[port].init();
#if FW_OBJECT_NAMES == 1
char portName[120];
snprintf(portName, sizeof(portName), "%s_getValue_OutputPort[%d]", this->m_objName, port);
snprintf(portName, sizeof(portName), "%s_getValue_OutputPort[%d]", this->m_objName.toChar(), port);
this->m_getValue_OutputPort[port].setObjName(portName);
#endif
}
Expand All @@ -45,7 +45,7 @@ namespace Svc {
this->m_setValue_OutputPort[port].init();
#if FW_OBJECT_NAMES == 1
char portName[120];
snprintf(portName, sizeof(portName), "%s_setValue_OutputPort[%d]", this->m_objName, port);
snprintf(portName, sizeof(portName), "%s_setValue_OutputPort[%d]", this->m_objName.toChar(), port);
this->m_setValue_OutputPort[port].setObjName(portName);
#endif
}
Expand Down
22 changes: 11 additions & 11 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ fprime-fpl-convert-xml==1.0.3
fprime-fpl-extract-xml==1.0.3
fprime-fpl-layout==1.0.3
fprime-fpl-write-pic==1.0.3
fprime-fpp-check==2.1.0a3
fprime-fpp-depend==2.1.0a3
fprime-fpp-filenames==2.1.0a3
fprime-fpp-format==2.1.0a3
fprime-fpp-from-xml==2.1.0a3
fprime-fpp-locate-defs==2.1.0a3
fprime-fpp-locate-uses==2.1.0a3
fprime-fpp-syntax==2.1.0a3
fprime-fpp-to-cpp==2.1.0a3
fprime-fpp-to-json==2.1.0a3
fprime-fpp-to-xml==2.1.0a3
fprime-fpp-check==2.1.0a4
fprime-fpp-depend==2.1.0a4
fprime-fpp-filenames==2.1.0a4
fprime-fpp-format==2.1.0a4
fprime-fpp-from-xml==2.1.0a4
fprime-fpp-locate-defs==2.1.0a4
fprime-fpp-locate-uses==2.1.0a4
fprime-fpp-syntax==2.1.0a4
fprime-fpp-to-cpp==2.1.0a4
fprime-fpp-to-json==2.1.0a4
fprime-fpp-to-xml==2.1.0a4
fprime-gds==3.4.3
fprime-tools==3.4.4
fprime-visual==1.0.2
Expand Down

0 comments on commit bdfe241

Please sign in to comment.