diff --git a/Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.cpp b/Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.cpp index be5889bf5c..d437db8ad7 100644 --- a/Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.cpp +++ b/Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.cpp @@ -13,7 +13,7 @@ #include #include #include - +#include #include #include #include @@ -24,6 +24,8 @@ #include #include +static_assert(FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING, "Cannot use SPI driver without full string formatting"); + namespace Drv { // ---------------------------------------------------------------------- @@ -75,9 +77,7 @@ namespace Drv { // Open: char devName[256]; - snprintf(devName,sizeof(devName),"/dev/spidev%d.%d",device,select); - // null terminate - devName[sizeof(devName)-1] = 0; + Fw::StringUtils::format(devName, sizeof devName, "/dev/spidev%d.%d", device, select); fd = ::open(devName, O_RDWR); if (fd == -1) { diff --git a/Fw/Types/StringUtils.cpp b/Fw/Types/StringUtils.cpp index 714b9d4a05..e8cb3428a0 100644 --- a/Fw/Types/StringUtils.cpp +++ b/Fw/Types/StringUtils.cpp @@ -2,6 +2,7 @@ #include #include #include +#include char* Fw::StringUtils::string_copy(char* destination, const char* source, FwSizeType num) { // Handle self-copy and 0 bytes copy @@ -72,3 +73,10 @@ FwSignedSizeType Fw::StringUtils::substring_find(const CHAR* source_string, // if we make it here, no matches were found return -1; } + +void Fw::StringUtils::format(CHAR* destination, Fw::StringBase::SizeType size, const CHAR* format, ...) { + va_list args; + va_start(args, format); + Fw::ExternalString(destination, size).vformat(format, args); + va_end(args); +} diff --git a/Fw/Types/StringUtils.hpp b/Fw/Types/StringUtils.hpp index 5213dba8a7..ab05210e1a 100644 --- a/Fw/Types/StringUtils.hpp +++ b/Fw/Types/StringUtils.hpp @@ -1,6 +1,7 @@ #ifndef FW_STRINGUTILS_HPP #define FW_STRINGUTILS_HPP #include +#include namespace Fw { namespace StringUtils { @@ -44,6 +45,21 @@ FwSizeType string_length(const CHAR* source, FwSizeType buffer_size); */ FwSignedSizeType substring_find(const CHAR* source_string, FwSizeType source_size, const CHAR* sub_string, FwSizeType sub_size); +//! \brief format a string and store into a destination buffer +//! +//! This function will format a string into a destination buffer. The destination buffer must be large enough to hold +//! the formatted string. The format delegates to the standard C library function vsnprintf underneath the hood. +//! +//! \warning if used in execution (i.e. a file path), assert FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING. +//! ``` +//! static_assert(FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING, "String formatting disabled") +//! ``` +//! \param destination: destination buffer to hold formatted string +//! \param size: size of the destination buffer. Output will be truncated to this length +//! \param format: constant format string +//! \param ...: variable arguments to to pass to the format string +void format(CHAR* destination, StringBase::SizeType size, const CHAR* format, ...); + enum StringToNumberStatus { SUCCESSFUL_CONVERSION, //!< Output should be valid NULL_INPUT, //!< A null string was supplied diff --git a/Ref/RecvBuffApp/RecvBuffComponentImpl.cpp b/Ref/RecvBuffApp/RecvBuffComponentImpl.cpp index 82492a3a46..cfa51e2449 100644 --- a/Ref/RecvBuffApp/RecvBuffComponentImpl.cpp +++ b/Ref/RecvBuffApp/RecvBuffComponentImpl.cpp @@ -72,16 +72,6 @@ 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.toChar(), - (int) this->m_buffsReceived); -#else - (void)snprintf(str, buffer_size, "RecvBuffImpl: ATM recd count: %d", - (int) this->m_buffsReceived); -#endif - } - void RecvBuffImpl::parameterUpdated(FwPrmIdType id) { this->log_ACTIVITY_LO_BuffRecvParameterUpdated(id); Fw::ParamValid valid; diff --git a/Ref/RecvBuffApp/RecvBuffComponentImpl.hpp b/Ref/RecvBuffApp/RecvBuffComponentImpl.hpp index 42a952cc91..4c0cdb7da5 100644 --- a/Ref/RecvBuffApp/RecvBuffComponentImpl.hpp +++ b/Ref/RecvBuffApp/RecvBuffComponentImpl.hpp @@ -23,7 +23,6 @@ namespace Ref { U32 m_errBuffs; // !< number of buffers with errors received F32 m_sensor1; F32 m_sensor2; - void toString(char* str, I32 buffer_size); // parameter update notification void parameterUpdated(FwPrmIdType id); diff --git a/Ref/SendBuffApp/SendBuffComponentImpl.cpp b/Ref/SendBuffApp/SendBuffComponentImpl.cpp index 1d5a273707..4e7da0df5d 100644 --- a/Ref/SendBuffApp/SendBuffComponentImpl.cpp +++ b/Ref/SendBuffApp/SendBuffComponentImpl.cpp @@ -87,18 +87,6 @@ namespace Ref { this->tlmWrite_SendState(this->m_state); } - 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.toChar(), - (int) this->m_invocations, (int) this->m_buffsSent); - str[buffer_size-1] = 0; -#else - (void) snprintf(str, buffer_size, "Lps Atm Component: count: %d ATMs: %d", - (int) this->m_invocations, (int) this->m_buffsSent); -#endif - } - - void SendBuffImpl::SB_START_PKTS_cmdHandler(FwOpcodeType opCode, U32 cmdSeq) { this->m_sendPackets = true; this->m_state = SendBuff_ActiveState::SEND_ACTIVE; diff --git a/Ref/SendBuffApp/SendBuffComponentImpl.hpp b/Ref/SendBuffApp/SendBuffComponentImpl.hpp index 1fa944d896..095b2072e6 100644 --- a/Ref/SendBuffApp/SendBuffComponentImpl.hpp +++ b/Ref/SendBuffApp/SendBuffComponentImpl.hpp @@ -47,8 +47,6 @@ namespace Ref { U32 m_currPacketId; //!< current packet ID to be sent bool m_firstPacketSent; //!< set if first packet - void toString(char* str, I32 buffer_size); //!< writes a string representation of the object - // buffer data Drv::DataBuffer m_testBuff; //!< data buffer to send diff --git a/Ref/SignalGen/SignalGen.cpp b/Ref/SignalGen/SignalGen.cpp index 904cb8639a..382a9edbd7 100644 --- a/Ref/SignalGen/SignalGen.cpp +++ b/Ref/SignalGen/SignalGen.cpp @@ -135,16 +135,9 @@ namespace Ref { // if a Data product is being generated, store a record if (this->m_dpInProgress) { - // printf("DP1: %u %u %lu %u\n", - // this->m_dpContainer.getBuffer().getSize(), - // SignalInfo::SERIALIZED_SIZE, - // this->m_dpContainer.getDataSize(), - // this->m_dpContainer.getBuffer().getSerializeRepr().getBuffLeft() - // ); Fw::SerializeStatus stat = this->m_dpContainer.serializeRecord_DataRecord(sigInfo); this->m_currDp++; this->m_dpBytes += SignalInfo::SERIALIZED_SIZE; - // printf("DP2: %u %u %lu\n",this->m_dpContainer.getBuffer().getSize(),this->m_dpBytes,this->m_dpContainer.getDataSize()); // check for full data product if (Fw::SerializeStatus::FW_SERIALIZE_NO_ROOM_LEFT == stat) { this->log_WARNING_LO_DpRecordFull(this->m_currDp,this->m_dpBytes); diff --git a/Utils/CRCChecker.cpp b/Utils/CRCChecker.cpp index 4698aee730..705477b4df 100644 --- a/Utils/CRCChecker.cpp +++ b/Utils/CRCChecker.cpp @@ -10,12 +10,12 @@ // ====================================================================== #include -#include // For snprintf #include #include #include #include #include +#include namespace Utils { @@ -32,7 +32,6 @@ namespace Utils { Os::File::Status stat; Utils::Hash hash; U32 checksum; - I32 s_stat; FwSignedSizeType int_file_size; FwSignedSizeType bytes_to_read; FwSignedSizeType bytes_to_write; @@ -90,8 +89,8 @@ namespace Utils { hash.final(checksum); // open checksum file - s_stat = snprintf(hashFilename, CRC_MAX_FILENAME_SIZE, "%s%s", fname, HASH_EXTENSION_STRING); - FW_ASSERT(s_stat > 0); + FW_ASSERT(CRC_MAX_FILENAME_SIZE > (Fw::StringUtils::string_length(fname, CRC_MAX_FILENAME_SIZE) + sizeof HASH_EXTENSION_STRING)); + Fw::StringUtils::format(hashFilename, CRC_MAX_FILENAME_SIZE, "%s%s", fname, HASH_EXTENSION_STRING); stat = f.open(hashFilename, Os::File::OPEN_WRITE); if(stat != Os::File::OP_OK) @@ -120,8 +119,8 @@ namespace Utils { char hashFilename[CRC_MAX_FILENAME_SIZE]; FW_ASSERT(fname != nullptr); // open checksum file - I32 s_stat = snprintf(hashFilename, CRC_MAX_FILENAME_SIZE, "%s%s", fname, HASH_EXTENSION_STRING); - FW_ASSERT(s_stat > 0); + FW_ASSERT(CRC_MAX_FILENAME_SIZE > (Fw::StringUtils::string_length(fname, CRC_MAX_FILENAME_SIZE) + sizeof HASH_EXTENSION_STRING)); + Fw::StringUtils::format(hashFilename, CRC_MAX_FILENAME_SIZE, "%s%s", fname, HASH_EXTENSION_STRING); stat = f.open(hashFilename, Os::File::OPEN_READ); if(stat != Os::File::OP_OK)