iobridge.cpp
Go to the documentation of this file.
1/** @file iobridge.cpp Test utility for IO devices
2
3
4This tutorial demonstrates how to use elementary access to
5external signals for a bridging device, ie inputs of one device
6are mapped to outputs of the other device and vice versa.
7This addresses the situation where digital io events are to be
8transmitted to or receviced from a network device without running
9a simulation.
10
11@ingroup Tutorials
12
13@include iobridge.cpp
14
15*/
16
17#include "libfaudes.h"
18#include <signal.h>
19
20
21using namespace faudes;
22
23// iobridge clean-up on exit
24void iobridge_exit(void);
25
26// signal handler recursion flag
27volatile sig_atomic_t signal_in_progress = 0;
28
29// signal handler to stop devices
30void 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
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
45void iobridge_exit(void) {
46 // stop all devices
48}
49
50
51// iobridge
52int 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
133
134 // never happens
135 return 0;
136}
137
138
139
int main()
void faudes_termsignal(void(*sighandler)(int))
const char * faudes_strsignal(int sig)
virtual void Compile(void)
virtual std::string EStr(Idx ev)
static void StopAll(void)
virtual void Start(void)
virtual void WriteOutput(Idx output)=0
static vDevice * FromFile(const std::string &rFileName)
virtual Idx ReadInput(void)
void Name(const std::string &rName)
void UseCondition(faudes_mutex_t *wmutex, faudes_cond_t *wcond)
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
uint32_t Idx
std::string VersionString()

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen