RTXI  3.0.0
The Real-Time eXperiment Interface Reference Manual
rt.hpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah,
4  Will Cornell Medical College
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #ifndef RT_H
22 #define RT_H
23 
24 #include <unordered_map>
25 #include <variant>
26 #include <vector>
27 
28 #include "event.hpp"
29 #include "fifo.hpp"
30 #include "io.hpp"
31 #include "rtos.hpp"
32 
33 // forward declaration
34 namespace Widgets
35 {
36 class Component;
37 } // namespace Widgets
38 
43 namespace RT
44 {
45 
46 namespace State
47 {
48 
52 enum state_t : int8_t
53 {
61 };
62 } // namespace State
63 
67 namespace Telemitry
68 {
69 typedef int response_t;
73 constexpr response_t RT_NOOP = 3;
75  4;
77  5;
79  6;
81  7;
82 constexpr response_t RT_ERROR =
83  -1;
84 constexpr response_t NO_TELEMITRY = -2;
96 struct Response
97 {
99  Event::Object* cmd = nullptr;
100 };
101 } // namespace Telemitry
102 
112 class Device : public IO::Block
113 {
114 public:
115  Device(std::string n, const std::vector<IO::channel_t>& c)
116  : IO::Block(std::move(n), c, /*isdependent=*/false)
117  {
118  }
119  Device(const Device&) = default; // copy constructor
120  Device& operator=(const Device&) = default; // copy assignment operator
121  Device(Device&&) = delete; // move constructor
122  Device& operator=(Device&&) = delete; // move assignment operator
123  ~Device() override = default;
124 
130  virtual void read() = 0;
131 
137  virtual void write() = 0;
138 }; // class Device
139 
149 class Thread : public IO::Block
150 {
151 public:
152  Thread(std::string n, const std::vector<IO::channel_t>& c)
153  : IO::Block(std::move(n), c, /*isdependent=*/true)
154  {
155  }
156  Thread(const Thread& connector) = default; // copy constructor
157  Thread& operator=(const Thread& connector) =
158  default; // copy assignment operator
159  Thread(Thread&&) = delete; // move constructor
160  Thread& operator=(Thread&&) = delete; // move assignment operator
161  ~Thread() override = default;
162 
168  virtual void execute() = 0;
169 }; // class Thread
170 
185 typedef struct block_connection_t
186 {
187  IO::Block* src = nullptr;
189  size_t src_port = 0;
190  IO::Block* dest = nullptr;
191  size_t dest_port = 0;
192  bool operator==(const block_connection_t& rhs) const
193  {
194  return (this->src == rhs.src) && (this->src_port_type == rhs.src_port_type)
195  && (this->src_port == rhs.src_port) && (this->dest == rhs.dest)
196  && (this->dest_port == rhs.dest_port);
197  }
198  bool operator!=(const block_connection_t& rhs) const
199  {
200  return !operator==(rhs);
201  }
203 
223 {
224 public:
225  Connector() = default; // default constructor
226  Connector(const Connector& connector) = delete; // copy constructor
227  Connector& operator=(const Connector& connector) =
228  delete; // copy assignment operator
229  Connector(Connector&&) = delete; // move constructor
230  Connector& operator=(Connector&&) = delete; // move assignment operator
231  ~Connector() = default;
232 
245  int connect(block_connection_t connection);
246 
259  void disconnect(block_connection_t connection);
260 
272  bool connected(block_connection_t connection);
273 
285  void insertBlock(IO::Block* block,
286  std::vector<RT::block_connection_t>& block_connections);
287 
293  void removeBlock(IO::Block* block);
294 
301  bool isRegistered(IO::Block* block);
302 
311  std::vector<RT::Device*> getDevices();
312 
321  std::vector<RT::Thread*> getThreads();
322 
330  std::vector<RT::block_connection_t> getOutputs(IO::Block* src);
331 
344 
350  void clearAllConnections(IO::Block* block);
351 
358  std::vector<IO::Block*> getRegisteredBlocks();
359 
368  std::vector<RT::block_connection_t> getAllConnections();
369 
370 private:
371  int find_cycle(RT::block_connection_t conn, IO::Block* ref_block);
372  std::vector<RT::Thread*> topological_sort();
373  std::vector<IO::Block*> block_registry;
374  std::vector<std::vector<RT::block_connection_t>> connections;
375 }; // class Connector
376 
381 using command_param_t = std::variant<std::monostate,
382  int64_t,
383  int64_t*,
384  uint64_t,
385  double,
386  RT::Thread*,
387  std::vector<RT::Thread*>*,
388  RT::Device*,
389  std::vector<RT::Device*>*,
390  IO::Block*,
394  std::string>;
409 class System : public Event::Handler
410 {
411 public:
412  explicit System(Event::Manager* em, RT::Connector* rtc);
413  System(const System& system) = delete; // copy constructor
414  System& operator=(const System& system) = delete; // copy assignment operator
415  System(System&&) = delete; // move constructor
416  System& operator=(System&&) = delete; // move assignment operator
417  ~System() override;
418 
424  int64_t getPeriod();
425 
431  std::vector<RT::Telemitry::Response> getTelemitry();
432 
447 
461  void receiveEvent(Event::Object* event) override;
462 
463 private:
464  // We want our cmd class to be private. the only way to access
465  // RT::System functions is through its event handler.
466  class CMD : public Event::Object
467  {
468  public:
469  explicit CMD(Event::Type et);
470  command_param_t getRTParam(const std::string_view& param_name);
471  void setRTParam(const std::string_view& param_name,
472  const command_param_t& value);
473 
474  private:
475  struct rt_param
476  {
477  std::string_view name;
478  RT::command_param_t value;
479  };
480  std::vector<rt_param> rt_params;
481  };
482 
483  void insertDevice(Event::Object* event);
484  void removeDevice(Event::Object* event);
485  void insertThread(Event::Object* event);
486  void removeThread(Event::Object* event);
487  // void blockActivityChange(Event::Object* event);
488  void threadActivityChange(Event::Object* event);
489  void deviceActivityChange(Event::Object* event);
490  void ioLinkChange(Event::Object* event);
491  void connectionsInfoRequest(Event::Object* event);
492  void allConnectionsInfoRequest(Event::Object* event);
493  void blockInfoRequest(Event::Object* event);
494  void setPeriod(Event::Object* event);
495  void shutdown(Event::Object* event);
496  void NOOP(Event::Object* event);
497  void getPeriodValues(Event::Object* event);
498  void provideTimetickPointers(Event::Object* event);
499  void changeWidgetParameters(Event::Object* event);
500  void changeWidgetState(Event::Object* event);
501 
502  void executeCMD(CMD* cmd);
503  void updateDeviceList(CMD* cmd);
504  void updateThreadList(CMD* cmd);
505  void ioLinkUpdateCMD(CMD* cmd);
506  void updateBlockActivity(CMD* cmd);
507  void setPeriod(CMD* cmd);
508  void shutdown(CMD* cmd);
509  void getPeriodTicksCMD(CMD* cmd);
510  void changeWidgetParametersCMD(CMD* cmd);
511  void changeWidgetStateCMD(CMD* cmd);
512 
513  void postTelemitry(RT::Telemitry::Response telemitry);
514 
515  static void execute(void* sys);
516 
517  int64_t periodStartTime = 1;
518  int64_t periodEndTime = 1;
519  int64_t lastperiodStartTime = 1;
520 
521  // System owns the task object and the pipe used to communicate with it
522  std::unique_ptr<RT::OS::Task> task;
523  std::unique_ptr<RT::OS::Fifo> eventFifo;
524  std::thread telemitry_processing_thread;
525  std::atomic<bool> telemitry_processing_thread_running = true;
526 
527  // system doesn't own any of the below variables. That's why they are
528  // only pointers.
529  Event::Manager* event_manager;
530  RT::Connector* rt_connector;
531 
532  // System's real-time loop maintains copy of device and thread pointers
533  std::vector<RT::Device*> devices;
534  std::vector<RT::Thread*> threads;
535 }; // class System
536 } // namespace RT
537 #endif // RT_H
Definition: io.hpp:79
Block(std::string blockname, const std::vector< channel_t > &channels, bool isdependent)
Definition: io.cpp:29
std::vector< RT::block_connection_t > getAllConnections()
Definition: rt.cpp:265
Connector()=default
std::vector< IO::Block * > getRegisteredBlocks()
Definition: rt.cpp:254
std::vector< RT::Thread * > getThreads()
Definition: rt.cpp:222
Connector(const Connector &connector)=delete
void propagateBlockConnections(IO::Block *block)
Definition: rt.cpp:235
void disconnect(block_connection_t connection)
Definition: rt.cpp:84
int connect(block_connection_t connection)
Definition: rt.cpp:50
void insertBlock(IO::Block *block, std::vector< RT::block_connection_t > &block_connections)
Definition: rt.cpp:100
bool isRegistered(IO::Block *block)
Definition: rt.cpp:142
std::vector< RT::block_connection_t > getOutputs(IO::Block *src)
Definition: rt.cpp:227
bool connected(block_connection_t connection)
Definition: rt.cpp:70
void clearAllConnections(IO::Block *block)
Definition: rt.cpp:243
Connector(Connector &&)=delete
Connector & operator=(Connector &&)=delete
Connector & operator=(const Connector &connector)=delete
std::vector< RT::Device * > getDevices()
Definition: rt.cpp:208
~Connector()=default
void removeBlock(IO::Block *block)
Definition: rt.cpp:132
Device & operator=(const Device &)=default
Device(const Device &)=default
virtual void read()=0
Device(std::string n, const std::vector< IO::channel_t > &c)
Definition: rt.hpp:115
~Device() override=default
Device & operator=(Device &&)=delete
Device(Device &&)=delete
virtual void write()=0
System(System &&)=delete
std::vector< RT::Telemitry::Response > getTelemitry()
Definition: rt.cpp:344
System(Event::Manager *em, RT::Connector *rtc)
Definition: rt.cpp:274
void receiveEvent(Event::Object *event) override
Definition: rt.cpp:538
System & operator=(const System &system)=delete
System & operator=(System &&)=delete
int64_t getPeriod()
Definition: rt.cpp:308
void createTelemitryProcessor()
Definition: rt.cpp:318
~System() override
Definition: rt.cpp:296
System(const System &system)=delete
Thread(Thread &&)=delete
Thread(const Thread &connector)=default
Thread & operator=(const Thread &connector)=default
Thread & operator=(Thread &&)=delete
Thread(std::string n, const std::vector< IO::channel_t > &c)
Definition: rt.hpp:152
virtual void execute()=0
~Thread() override=default
Type
Definition: event.hpp:55
Connection Oriented Classes.
Definition: io.hpp:40
flags_t
Definition: io.hpp:52
@ UNKNOWN
Definition: io.hpp:55
state_t
Definition: rt.hpp:53
@ EXIT
Definition: rt.hpp:60
@ EXEC
Definition: rt.hpp:55
@ UNPAUSE
Definition: rt.hpp:59
@ PAUSE
Definition: rt.hpp:58
@ INIT
Definition: rt.hpp:54
@ MODIFY
Definition: rt.hpp:56
@ PERIOD
Definition: rt.hpp:57
constexpr response_t RT_WIDGET_PARAM_UPDATE
Definition: rt.hpp:76
constexpr response_t RT_DEVICE_LIST_UPDATE
Definition: rt.hpp:72
constexpr response_t RT_NOOP
Definition: rt.hpp:73
constexpr response_t NO_TELEMITRY
Definition: rt.hpp:84
constexpr response_t IO_LINK_UPDATED
Definition: rt.hpp:78
constexpr response_t RT_PERIOD_UPDATE
Definition: rt.hpp:70
int response_t
Definition: rt.hpp:69
constexpr response_t RT_ERROR
Definition: rt.hpp:82
constexpr response_t RT_THREAD_LIST_UPDATE
Definition: rt.hpp:71
constexpr response_t RT_WIDGET_STATE_UPDATE
Definition: rt.hpp:80
constexpr response_t RT_SHUTDOWN
Definition: rt.hpp:74
Definition: fifo.cpp:31
struct RT::block_connection_t block_connection_t
std::variant< std::monostate, int64_t, int64_t *, uint64_t, double, RT::Thread *, std::vector< RT::Thread * > *, RT::Device *, std::vector< RT::Device * > *, IO::Block *, RT::block_connection_t, Widgets::Component *, State::state_t, std::string > command_param_t
Definition: rt.hpp:394
Definition: rt.hpp:35
response_t type
Definition: rt.hpp:98
Event::Object * cmd
Definition: rt.hpp:99
bool operator==(const block_connection_t &rhs) const
Definition: rt.hpp:192
IO::Block * dest
Definition: rt.hpp:190
IO::Block * src
Definition: rt.hpp:187
IO::flags_t src_port_type
Definition: rt.hpp:188
bool operator!=(const block_connection_t &rhs) const
Definition: rt.hpp:198