iobridge.cpp
Go to the documentation of this file.
1 /** @file iobridge.cpp Test utility for IO devices
2 
3 
4 This tutorial demonstrates how to use elementary access to
5 external signals for a bridging device, ie inputs of one device
6 are mapped to outputs of the other device and vice versa.
7 This addresses the situation where digital io events are to be
8 transmitted to or receviced from a network device without running
9 a simulation.
10 
11 @ingroup Tutorials
12 
13 @include iobridge.cpp
14 
15 */
16 
17 #include "libfaudes.h"
18 #include <signal.h>
19 
20 
21 using namespace faudes;
22 
23 // iobridge clean-up on exit
24 void iobridge_exit(void);
25 
26 // signal handler recursion flag
27 volatile sig_atomic_t signal_in_progress = 0;
28 
29 // signal handler to stop devices
30 void catch_signal(int sig) {
31  // detect recursion, pass on
32  if(signal_in_progress) raise(sig);
34  // report
35  std::cerr << "iobridge: signal: " << faudes_strsignal(sig) << std::endl;
36  // call my exit function
37  iobridge_exit();
38  // re-install default handler
39  signal(sig, SIG_DFL);
40  // pass on signal
41  raise(sig);
42 }
43 
44 // iobridge clean-up on exit
45 void iobridge_exit(void) {
46  // stop all devices
48 }
49 
50 
51 // iobridge
52 int main(int argc, char* argv[]) {
53 
54  // install my signal handler
56 
57  // install my exit fnct
58  atexit(iobridge_exit);
59 
60 
61  // first two arguments must be the device files
62  if(argc!=3) {
63  std::cerr << "iobridge: " << VersionString() << std::endl;
64  std::cerr << "usage: iobridge <one device-file> <other device-file>" << std::endl;
65  return(-1);
66  }
67 
68 #ifdef FAUDES_NETWORK
69 #ifdef FAUDES_WINDOWS
70  // initialise winsocks
71  WSADATA wsaData;
72  if(WSAStartup(MAKEWORD(2,2), &wsaData)!=0) {
73  std::cerr << "iobridge: failed to initialize winsocks" << std::endl;
74  return -1;
75  }
76 #endif
77 #endif
78 
79  //initialize vDevice A
80  FD_DH("Initialize vDevice A");
81  vDevice* adev;
82  adev=vDevice::FromFile(std::string(argv[1]));
83 
84  //initialize vDevice A
85  FD_DH("Initialize vDevice B");
86  vDevice* bdev;
87  bdev=vDevice::FromFile(std::string(argv[2]));
88 
89  // have mutex/condition for common wait
90  faudes_mutex_t wmutex;
91  faudes_cond_t wcond;
92  faudes_mutex_init(&wmutex);
93  faudes_cond_init(&wcond);
94  adev->UseCondition(&wmutex,&wcond);
95  bdev->UseCondition(&wmutex,&wcond);
96 
97  // set up devices
98  adev->Compile();
99  bdev->Compile();
100 
101  // start devices
102  adev->Start();
103  bdev->Start();
104 
105  // loop forever
106  while(1) {
107 
108  std::cout << "% ################ iobridge: waiting for input events" << std::endl;
109 
110  // lock waiting
111  faudes_mutex_lock(&wmutex);
112 
113  // wait for inputs
114  faudes_cond_wait(&wcond,&wmutex);
115 
116  // test inputs
117  while(Idx ev =adev->ReadInput()) {
118  std::cout << "% ################ iobridge: sensed " << adev->Name() << "-input " << adev->EStr(ev) << std::endl;
119  bdev->WriteOutput(ev);
120  }
121  while(Idx ev =bdev->ReadInput()) {
122  std::cout << "% ################ iobridge: sensed " << bdev->Name() << "-input " << bdev->EStr(ev) << std::endl;
123  adev->WriteOutput(ev);
124  }
125 
126  // unlock waiting
127  faudes_mutex_unlock(&wmutex);
128 
129  }
130 
131  // never happens
132  iobridge_exit();
133 
134  // never happens
135  return 0;
136 }
137 
138 
139 
const char * faudes_strsignal(int sig)
void faudes_termsignal(void(*sighandler)(int))
Virtual base class to define the interface for event io.
Definition: iop_vdevice.h:261
virtual void Compile(void)
Compile inner data-structures.
virtual std::string EStr(Idx ev)
Convenience method.
Definition: iop_vdevice.h:627
static void StopAll(void)
Stop all devices.
virtual void Start(void)
Activate the device.
virtual void WriteOutput(Idx output)=0
Run output command.
static vDevice * FromFile(const std::string &rFileName)
Construct on heap from file.
virtual Idx ReadInput(void)
Read sensed input events.
void Name(const std::string &rName)
Set the device name.
void UseCondition(faudes_mutex_t *wmutex, faudes_cond_t *wcond)
Tell the device which condition to use for waiting.
int main(int argc, char *argv[])
Definition: iobridge.cpp:52
volatile sig_atomic_t signal_in_progress
Definition: iobridge.cpp:27
void iobridge_exit(void)
Definition: iobridge.cpp:45
void catch_signal(int sig)
Definition: iobridge.cpp:30
#define FD_DH(message)
Definition: iop_vdevice.h:27
Includes all libFAUDES headers, incl plugings
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
std::string VersionString()
Return FAUDES_VERSION as std::string.
Definition: cfl_helper.cpp:131

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen