F´ Flight Software - C/C++ Documentation  NASA-v2.0.0
A framework for building embedded system applications to NASA flight quality standards.
ActiveComponentBase.cpp
Go to the documentation of this file.
1 #include <FpConfig.hpp>
3 #include <Fw/Types/Assert.hpp>
5 #include <stdio.h>
6 
7 //#define DEBUG_PRINT(x,...) printf(x,##__VA_ARGS__); fflush(stdout)
8 #define DEBUG_PRINT(x,...)
9 
10 namespace Fw {
11 
13 
14  public:
16  return sizeof(m_buff);
17  }
18 
19  U8* getBuffAddr(void) {
20  return m_buff;
21  }
22 
23  const U8* getBuffAddr(void) const {
24  return m_buff;
25  }
26 
27  private:
28 
30 
31  };
32 
34 
35  }
36 
38  DEBUG_PRINT("ActiveComponent %s destructor.\n",this->getObjName());
39  }
40 
42  QueuedComponentBase::init(instance);
43  }
44 
45 #if FW_OBJECT_TO_STRING == 1 && FW_OBJECT_NAMES == 1
46  void ActiveComponentBase::toString(char* buffer, NATIVE_INT_TYPE size) {
47  (void)snprintf(buffer, size, "ActComp: %s", this->m_objName);
48  buffer[size-1] = 0;
49  }
50 #endif
51 
52  void ActiveComponentBase::start(NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, NATIVE_INT_TYPE cpuAffinity) {
53 
54  Fw::EightyCharString taskName;
55 
56 #if FW_OBJECT_NAMES == 1
57  taskName = this->getObjName();
58 #else
59  char taskNameChar[FW_TASK_NAME_MAX_SIZE];
60  (void)snprintf(taskNameChar,sizeof(taskNameChar),"ActComp_%d",Os::Task::getNumTasks());
61  taskName = taskNameChar;
62 #endif
63 // If running with the baremetal scheduler, use a variant of the task-loop that
64 // does not loop internal, but waits for an external iteration call.
65 #if FW_BAREMETAL_SCHEDULER == 1
66  Os::Task::TaskStatus status = this->m_task.start(taskName, identifier, priority, stackSize, this->s_baseBareTask, this, cpuAffinity);
67 #else
68  Os::Task::TaskStatus status = this->m_task.start(taskName, identifier, priority, stackSize, this->s_baseTask, this, cpuAffinity);
69 #endif
70  FW_ASSERT(status == Os::Task::TASK_OK,(NATIVE_INT_TYPE)status);
71  }
72 
75  SerializeStatus stat = exitBuff.serialize((I32)ACTIVE_COMPONENT_EXIT);
76  FW_ASSERT(FW_SERIALIZE_OK == stat,static_cast<NATIVE_INT_TYPE>(stat));
77  (void)this->m_queue.send(exitBuff,0,Os::Queue::QUEUE_NONBLOCKING);
78  DEBUG_PRINT("exit %s\n", this->getObjName());
79  }
80 
82  DEBUG_PRINT("join %s\n", this->getObjName());
83  return this->m_task.join(value_ptr);
84  }
85 
86  void ActiveComponentBase::s_baseBareTask(void* ptr) {
87  FW_ASSERT(ptr != NULL);
88  ActiveComponentBase* comp = reinterpret_cast<ActiveComponentBase*>(ptr);
89  //Start if not started
90  if (!comp->m_task.isStarted()) {
91  comp->m_task.setStarted(true);
92  comp->preamble();
93  }
94  //Bare components cannot block, so return to the scheduler
95  if (comp->m_queue.getNumMsgs() == 0) {
96  return;
97  }
99  switch (loopStatus) {
100  case ActiveComponentBase::MSG_DISPATCH_OK: // if normal message processing, continue
101  break;
103  comp->finalizer();
104  comp->m_task.setStarted(false);
105  break;
106  default:
107  FW_ASSERT(0,(NATIVE_INT_TYPE)loopStatus);
108  }
109  }
110  void ActiveComponentBase::s_baseTask(void* ptr) {
111  // cast void* back to active component
112  ActiveComponentBase* comp = static_cast<ActiveComponentBase*> (ptr);
113  // indicated that task is started
114  comp->m_task.setStarted(true);
115  // print out message when task is started
116  // printf("Active Component %s task started.\n",comp->getObjName());
117  // call preamble
118  comp->preamble();
119  // call main task loop until exit or error
120  comp->loop();
121  // if main loop exits, call finalizer
122  comp->finalizer();
123  }
124 
126 
127  bool quitLoop = false;
128  while (!quitLoop) {
129  MsgDispatchStatus loopStatus = this->doDispatch();
130  switch (loopStatus) {
131  case MSG_DISPATCH_OK: // if normal message processing, continue
132  break;
133  case MSG_DISPATCH_EXIT:
134  quitLoop = true;
135  break;
136  default:
137  FW_ASSERT(0,(NATIVE_INT_TYPE)loopStatus);
138  }
139  }
140 
141  }
142 
144  }
145 
147  }
148 
149 }
Os::Task::setStarted
void setStarted(bool started)
set task to started when thread is fully up. Avoids a VxWorks race condition.
Definition: TaskCommon.cpp:23
Fw::ActiveComponentBase::m_task
Os::Task m_task
task object for active component
Definition: ActiveComponentBase.hpp:37
Fw::ActiveComponentBase::join
Os::Task::TaskStatus join(void **value_ptr)
provide return value of thread if value_ptr is not NULL
Definition: ActiveComponentBase.cpp:81
Fw::QueuedComponentBase::doDispatch
virtual MsgDispatchStatus doDispatch(void)=0
method to dispatch a single message in the queue.
Fw::SerializeBufferBase
Definition: Serializable.hpp:43
Fw::SerializeStatus
SerializeStatus
forward declaration for string
Definition: Serializable.hpp:14
Os::Task::getNumTasks
static NATIVE_INT_TYPE getNumTasks(void)
Definition: TaskCommon.cpp:10
Fw::SerializeBufferBase::serialize
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
Definition: Serializable.cpp:67
U8
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.hpp:76
Fw::EightyCharString
Definition: EightyCharString.hpp:10
Fw::QueuedComponentBase::m_queue
Os::Queue m_queue
queue object for active component
Definition: QueuedComponentBase.hpp:36
Fw::ActiveComponentBase::preamble
virtual void preamble(void)
A function that will be called before the event loop is entered.
Definition: ActiveComponentBase.cpp:143
Fw::ActiveComponentBase::start
void start(NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, NATIVE_INT_TYPE cpuAffinity=-1)
called by instantiator when task is to be started
Definition: ActiveComponentBase.cpp:52
Fw::ActiveComponentExitSerializableBuffer
Definition: ActiveComponentBase.cpp:12
Assert.hpp
Fw::FW_SERIALIZE_OK
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.
Definition: Serializable.hpp:15
Os::Queue::send
QueueStatus send(const Fw::SerializeBufferBase &buffer, NATIVE_INT_TYPE priority, QueueBlocking block)
send a message
Definition: QueueCommon.cpp:13
EightyCharString.hpp
Fw::ObjBase::init
void init(void)
Object initializer.
Definition: ObjBase.cpp:26
Fw::QueuedComponentBase::MSG_DISPATCH_OK
@ MSG_DISPATCH_OK
Dispatch was normal.
Definition: QueuedComponentBase.hpp:26
ActiveComponentBase.hpp
Os::Task::TASK_OK
@ TASK_OK
message sent/received okay
Definition: Task.hpp:19
Os::Task::TaskStatus
TaskStatus
Definition: Task.hpp:18
DEBUG_PRINT
#define DEBUG_PRINT(x,...)
Definition: ActiveComponentBase.cpp:8
Fw::ActiveComponentBase::ActiveComponentBase
ActiveComponentBase(const char *name)
Constructor.
Definition: ActiveComponentBase.cpp:33
Fw::QueuedComponentBase::MsgDispatchStatus
MsgDispatchStatus
Definition: QueuedComponentBase.hpp:25
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
Fw::ActiveComponentExitSerializableBuffer::getBuffAddr
const U8 * getBuffAddr(void) const
gets buffer address for data reading, const version
Definition: ActiveComponentBase.cpp:23
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:9
Os::Task::isStarted
bool isStarted(void)
check to see if task is started
Definition: TaskCommon.cpp:19
Fw::ActiveComponentBase::finalizer
virtual void finalizer(void)
A function that will be called after exiting the loop.
Definition: ActiveComponentBase.cpp:146
Fw::ActiveComponentBase
Definition: ActiveComponentBase.hpp:20
Os::Queue::getNumMsgs
NATIVE_INT_TYPE getNumMsgs(void) const
get the number of messages in the queue
Definition: Queue.cpp:210
Fw::QueuedComponentBase
Definition: QueuedComponentBase.hpp:21
Fw::ActiveComponentExitSerializableBuffer::getBuffCapacity
NATIVE_UINT_TYPE getBuffCapacity(void) const
returns capacity, not current size, of buffer
Definition: ActiveComponentBase.cpp:15
FpConfig.hpp
ISF configuration file.
Fw::ActiveComponentBase::~ActiveComponentBase
virtual ~ActiveComponentBase()
Destructor.
Definition: ActiveComponentBase.cpp:37
Os::Task::start
TaskStatus start(const Fw::StringBase &name, NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, taskRoutine routine, void *arg, NATIVE_INT_TYPE cpuAffinity=-1)
start the task
Definition: Task.cpp:16
Fw::QueuedComponentBase::MSG_DISPATCH_EXIT
@ MSG_DISPATCH_EXIT
A message was sent requesting an exit of the loop.
Definition: QueuedComponentBase.hpp:29
Fw::ActiveComponentExitSerializableBuffer::getBuffAddr
U8 * getBuffAddr(void)
gets buffer address for data filling
Definition: ActiveComponentBase.cpp:19
Fw::ActiveComponentBase::ACTIVE_COMPONENT_EXIT
@ ACTIVE_COMPONENT_EXIT
message to exit active component task
Definition: ActiveComponentBase.hpp:27
FW_TASK_NAME_MAX_SIZE
#define FW_TASK_NAME_MAX_SIZE
Max size of task name.
Definition: FpConfig.hpp:215
Os::Queue::QUEUE_NONBLOCKING
@ QUEUE_NONBLOCKING
Queue receive always returns even if there is no message.
Definition: Queue.hpp:42
Fw::ActiveComponentBase::exit
void exit(void)
exit task in active component
Definition: ActiveComponentBase.cpp:73
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
NULL
#define NULL
NULL.
Definition: BasicTypes.hpp:100
Fw
Definition: Buffer.cpp:21
Os::Task::join
TaskStatus join(void **value_ptr)
Wait for task to finish.
Definition: Task.cpp:72
Fw::ActiveComponentBase::loop
virtual void loop(void)
The function that will loop dispatching messages.
Definition: ActiveComponentBase.cpp:125