F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
PortBase.cpp
Go to the documentation of this file.
2 #include <Fw/Logger/Logger.hpp>
3 #include <Fw/Port/PortBase.hpp>
4 #include <cstdio>
5 #include "Fw/Types/Assert.hpp"
7 
8 #if FW_PORT_TRACING
9 void setConnTrace(bool trace) {
10  Fw::PortBase::setTrace(trace);
11 }
12 
13 namespace Fw {
14 bool PortBase::s_trace = false;
15 }
16 
17 #endif // FW_PORT_TRACING
18 
19 namespace Fw {
20 
22  : Fw::ObjBase(nullptr),
23  m_connObj(nullptr)
24 #if FW_PORT_TRACING == 1
25  ,
26  m_trace(false),
27  m_ovr_trace(false)
28 #endif
29 {
30 }
31 
33 
35  ObjBase::init();
36 }
37 
38 bool PortBase::isConnected() const {
39  return m_connObj == nullptr ? false : true;
40 }
41 
42 #if FW_PORT_TRACING == 1
43 
44 void PortBase::trace() const {
45  // Ports are only traced as part of invocation, which requires a connection
46  FW_ASSERT(this->isConnected());
47  bool do_trace = false;
48 
49  if (this->m_ovr_trace) {
50  if (this->m_trace) {
51  do_trace = true;
52  }
53  } else if (PortBase::s_trace) {
54  do_trace = true;
55  }
56 
57  if (do_trace) {
58 #if FW_OBJECT_NAMES == 1
59  Fw::Logger::log("Trace: %s\n", this->m_objName.toChar());
60 #else
61  Fw::Logger::log("Trace: %p\n", this);
62 #endif
63  }
64 }
65 
66 void PortBase::setTrace(bool trace) {
67  PortBase::s_trace = trace;
68 }
69 
70 void PortBase::ovrTrace(bool ovr, bool trace) {
71  this->m_ovr_trace = ovr;
72  this->m_trace = trace;
73 }
74 
75 #endif // FW_PORT_TRACING
76 
77 #if FW_OBJECT_TO_STRING == 1
78 const char* PortBase::getToStringFormatString() {
79  return "Port: %s %s->(%s)";
80 }
81 
82 void PortBase::toString(char* buffer, FwSizeType size) {
83  FW_ASSERT(size > 0);
84  FW_ASSERT(buffer != nullptr);
85  // Get the port-custom format string
86  const char* formatString = this->getToStringFormatString();
87  // Determine this port object name (or use "UNKNOWN")
88  const char* object_name =
89 #if FW_OBJECT_NAMES == 1
90  this->m_objName.toChar();
91 #else
92  "UNKNOWN";
93 #endif
94  // Get the C/NC for connected or not
95  const char* this_is_connected = this->isConnected() ? "C" : "NC";
96 
97  // Get the name of the connection object, "UNKNOWN" or "NONE"
98  const char* connected_to = this->isConnected() ?
99 #if FW_OBJECT_NAMES == 1
100  this->m_connObj->getObjName()
101 #else
102  "UNKNOWN"
103 #endif
104  : "None";
105  // Format the external string or use "" on error
106  const Fw::FormatStatus formatStatus = Fw::ExternalString(buffer, static_cast<Fw::ExternalString::SizeType>(size))
107  .format(formatString, object_name, this_is_connected, connected_to);
108  if (formatStatus != Fw::FormatStatus::SUCCESS) {
109  buffer[0] = 0;
110  }
111 }
112 #endif // FW_OBJECT_TO_STRING
113 
114 } // namespace Fw
PlatformSizeType FwSizeType
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
virtual void init()
Definition: PortBase.cpp:34
void init()
Object initializer.
Definition: ObjBase.cpp:24
Brief class description.
Definition: ObjBase.hpp:35
A string backed by an external buffer.
bool isConnected() const
Definition: PortBase.cpp:38
#define FW_PORT_TRACING
Indicates whether port calls are traced (more code, more visibility into execution) ...
Definition: FpConfig.h:78
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:58
virtual ~PortBase()
Definition: PortBase.cpp:32
Implementation of malloc based allocator.
Fw::ObjBase * m_connObj
Definition: PortBase.hpp:34
#define FW_ASSERT(...)
Definition: Assert.hpp:14
FormatStatus
status of string format calls
Definition: format.hpp:18