iobridge.cpp

Go to the documentation of this file.
00001 /** @file iobridge.cpp  Test utility for IO devices
00002 
00003 
00004 This tutorial demonstrates how to use elementary access to 
00005 external signals for a bridging device, ie inputs of one device
00006 are mapped to outputs of the other device and vice versa.
00007 This addresses the situation where digital io events are to be
00008 transmitted to or receviced from a network device without running
00009 a simulation.
00010 
00011 @ingroup Tutorials
00012 
00013 @include iobridge.cpp
00014 
00015 */
00016 
00017 #include "libfaudes.h"
00018 #include <signal.h>
00019 
00020 
00021 using namespace faudes;
00022 
00023 // iobridge clean-up on exit
00024 void iobridge_exit(void);
00025 
00026 // signal handler recursion flag
00027 volatile sig_atomic_t signal_in_progress = 0;
00028 
00029 // signal handler to stop devices
00030 void catch_signal(int sig) {
00031   // detect recursion, pass on
00032   if(signal_in_progress) raise(sig);
00033   signal_in_progress = 1;
00034   // report
00035   std::cerr << "iobridge: signal: " << faudes_strsignal(sig) << std::endl;
00036   // call my exit function
00037   iobridge_exit();
00038   // re-install default handler
00039   signal(sig, SIG_DFL);
00040   // pass on signal
00041   raise(sig);
00042 }
00043 
00044 // iobridge clean-up on exit
00045 void iobridge_exit(void) {
00046   // stop all devices
00047   vDevice::StopAll();
00048 }
00049 
00050 
00051 // iobridge
00052 int main(int argc, char* argv[]) {
00053 
00054   // install my signal handler
00055   faudes_termsignal(catch_signal);
00056 
00057   // install my exit fnct
00058   atexit(iobridge_exit);
00059 
00060 
00061   // first two arguments must be the device files
00062   if(argc!=3) {
00063     std::cerr << "iobridge: " << VersionString()  << std::endl;
00064     std::cerr << "usage: iobridge <one device-file> <other device-file>" << std::endl;
00065     return(-1);
00066   }
00067 
00068 #ifdef FAUDES_NETWORK
00069 #ifdef FAUDES_WINDOWS
00070   // initialise winsocks
00071   WSADATA wsaData;
00072   if(WSAStartup(MAKEWORD(2,2), &wsaData)!=0) {
00073     std::cerr << "iobridge: failed to initialize winsocks" << std::endl;
00074     return -1;
00075   }
00076 #endif
00077 #endif
00078 
00079   //initialize vDevice A
00080   FD_DH("Initialize vDevice A");
00081   vDevice* adev;
00082   adev=vDevice::FromFile(std::string(argv[1]));
00083 
00084   //initialize vDevice A
00085   FD_DH("Initialize vDevice B");
00086   vDevice* bdev;
00087   bdev=vDevice::FromFile(std::string(argv[2]));
00088 
00089   // have mutex/condition for common wait
00090   faudes_mutex_t wmutex;
00091   faudes_cond_t wcond;
00092   faudes_mutex_init(&wmutex);
00093   faudes_cond_init(&wcond);
00094   adev->UseCondition(&wmutex,&wcond);
00095   bdev->UseCondition(&wmutex,&wcond);
00096 
00097   // set up devices
00098   adev->Compile();
00099   bdev->Compile();
00100 
00101   // start devices
00102   adev->Start();
00103   bdev->Start();
00104 
00105   // loop forever
00106   while(1) {
00107    
00108     std::cout << "% ################ iobridge: waiting for input events" << std::endl;
00109 
00110     // lock waiting
00111     faudes_mutex_lock(&wmutex);
00112 
00113     // wait for inputs
00114     faudes_cond_wait(&wcond,&wmutex);
00115 
00116     // test inputs
00117     while(Idx ev =adev->ReadInput()) {
00118       std::cout << "% ################ iobridge: sensed " << adev->Name() << "-input " << adev->EStr(ev) << std::endl;
00119       bdev->WriteOutput(ev);
00120     } 
00121     while(Idx ev =bdev->ReadInput()) {
00122       std::cout << "% ################ iobridge: sensed " << bdev->Name() <<  "-input " << bdev->EStr(ev) << std::endl;
00123       adev->WriteOutput(ev);
00124     } 
00125 
00126     // unlock waiting
00127     faudes_mutex_unlock(&wmutex);
00128 
00129   }
00130 
00131   // never happens
00132   iobridge_exit();
00133 
00134   // never happens  
00135   return 0;
00136 }
00137 
00138 
00139 

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen