Skip to content

Commit

Permalink
Removing printf family from Ref, Drv, RPI, OS, Utils (#3141)
Browse files Browse the repository at this point in the history
* Removing printf from Ref compontents

* Removing printf from Drv

* Removing printf from Utils

* Adding Fw::StringUtils::format string format helper

* Using StringUtils::format in SPI driver

* Adding newline to file
  • Loading branch information
LeStarch authored Jan 27, 2025
1 parent b36933a commit 5e6d2b3
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 42 deletions.
8 changes: 4 additions & 4 deletions Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.hpp>
#include <FpConfig.hpp>
#include <Fw/Types/Assert.hpp>

#include <Fw/Types/StringUtils.hpp>
#include <cstdint>
#include <unistd.h>
#include <cstdio>
Expand All @@ -24,6 +24,8 @@
#include <linux/spi/spidev.h>
#include <cerrno>

static_assert(FW_USE_PRINTF_FAMILY_FUNCTIONS_IN_STRING_FORMATTING, "Cannot use SPI driver without full string formatting");

namespace Drv {

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions Fw/Types/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <Fw/Types/Assert.hpp>
#include <cstring>
#include <limits>
#include <Fw/Types/ExternalString.hpp>

char* Fw::StringUtils::string_copy(char* destination, const char* source, FwSizeType num) {
// Handle self-copy and 0 bytes copy
Expand Down Expand Up @@ -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);
}
16 changes: 16 additions & 0 deletions Fw/Types/StringUtils.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef FW_STRINGUTILS_HPP
#define FW_STRINGUTILS_HPP
#include <FpConfig.hpp>
#include <Fw/Types/StringBase.hpp>

namespace Fw {
namespace StringUtils {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions Ref/RecvBuffApp/RecvBuffComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion Ref/RecvBuffApp/RecvBuffComponentImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 0 additions & 12 deletions Ref/SendBuffApp/SendBuffComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions Ref/SendBuffApp/SendBuffComponentImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 0 additions & 7 deletions Ref/SignalGen/SignalGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions Utils/CRCChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
// ======================================================================

#include <FpConfig.hpp>
#include <cstdio> // For snprintf
#include <Utils/CRCChecker.hpp>
#include <Fw/Types/Assert.hpp>
#include <Os/File.hpp>
#include <Os/FileSystem.hpp>
#include <Utils/Hash/Hash.hpp>
#include <Fw/Types/StringUtils.hpp>

namespace Utils {

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5e6d2b3

Please sign in to comment.