libFAUDES

Sections

Index

iobridge.cpp File Reference

Test utility for IO devices. More...

#include "libfaudes.h"
#include "iop_vdevice.h"
#include <signal.h>

Go to the source code of this file.

Functions

void iobridge_exit (void)
void catch_signal (int sig)
int main (int argc, char *argv[])

Variables

volatile sig_atomic_t signal_in_progress = 0

Detailed Description

Test utility for IO devices.

This tutorial demonstrates how to use elementary access to external signals for a bridging device, ie sensors of one device are mapped to actuators of the other device and vice versa. This addresses the situation where digital io events are to be transmitted to or receviced from a network device without running a simulation.

/** @file iobridge.cpp  Test utility for IO devices


This tutorial demonstrates how to use elementary access to 
external signals for a bridging device, ie sensors of one device
are mapped to actuators of the other device and vice versa.
This addresses the situation where digital io events are to be
transmitted to or receviced from a network device without running
a simulation.

@ingroup Tutorials

@include iobridge.cpp

*/

#include "libfaudes.h"
#include "iop_vdevice.h"
#include <signal.h>


// fix windows/mingw definition                                                          
#ifdef FAUDES_WINEXTRA
#define sleep(sec) Sleep((sec) * 1000)
#define usleep(usec) Sleep((usec) / 1000)
#define SIGQUIT SIGBREAK
#define SIGHUP SIGBREAK
#define strsignal(sig) "unknown"
#endif

using namespace faudes;

// iobridge clean-up on exit
void iobridge_exit(void);

// signal handler recursion flag
volatile sig_atomic_t signal_in_progress = 0;

// signal handler to stop devices
void catch_signal(int sig) {
  // detect recursion, pass on
  if(signal_in_progress) raise(sig);
  signal_in_progress = 1;
  // report
  std::cerr << "iobridge: signal: " << strsignal(sig) << std::endl;
  // call my exit function
  iobridge_exit();
  // re-install default handler
  signal(sig, SIG_DFL);
  // pass on signal
  raise(sig);
}

// iobridge clean-up on exit
void iobridge_exit(void) {
  // stop all devices
  vDevice::StopAll();
}


// iobridge
int main(int argc, char* argv[]) {

  // install my signal handler
  signal(SIGTERM, catch_signal);
  signal(SIGINT, catch_signal);
  signal(SIGQUIT, catch_signal);
  signal(SIGHUP, catch_signal);
  signal(SIGABRT, catch_signal);

  // install my exit fnct
  atexit(iobridge_exit);


  // first two arguments must be the device files
  if(argc!=3) {
    std::cerr << "iobridge: " << FDVersionString()  << std::endl;
    std::cerr << "usage: iobridge <one device-file> <other device-file>" << std::endl;
    return(-1);
  }

  //initialize vDevice A
  FD_DH("Initialize vDevice A");
  vDevice* adev;
  adev=vDevice::FromFile(std::string(argv[1]));

  //initialize vDevice A
  FD_DH("Initialize vDevice B");
  vDevice* bdev;
  bdev=vDevice::FromFile(std::string(argv[2]));

  // have mutex/condition for common wait
  pthread_mutex_t wmutex;
  pthread_cond_t wcond;
  pthread_mutex_init(&wmutex,NULL);
  pthread_cond_init(&wcond,NULL);
  adev->UseCondition(&wmutex,&wcond);
  bdev->UseCondition(&wmutex,&wcond);

  // set up devices
  adev->Compile();
  bdev->Compile();

  // start devices
  adev->Start();
  bdev->Start();

  // loop forever
  while(1) {
   
    std::cout << "% ################ iobridge: waiting for sensor events" << std::endl;

    // lock waiting
    pthread_mutex_lock(&wmutex);

    // wait for sensors
    pthread_cond_wait(&wcond,&wmutex);

    // test sensors
    while(Idx ev =adev->ReadSensor()) {
      std::cout << "% ################ iobridge: sensed " << adev->Name() << "-sensor " << adev->EStr(ev) << std::endl;
      bdev->WriteActuator(ev);
    } 
    while(Idx ev =bdev->ReadSensor()) {
      std::cout << "% ################ iobridge: sensed " << bdev->Name() <<  "-sensor " << bdev->EStr(ev) << std::endl;
      adev->WriteActuator(ev);
    } 

    // unlock waiting
    pthread_mutex_unlock(&wmutex);

  }

  // never happens
  iobridge_exit();

  // never happens  
  return 0;
}



Definition in file iobridge.cpp.


Function Documentation

void catch_signal ( int  sig  ) 

Definition at line 40 of file iobridge.cpp.

void iobridge_exit ( void   ) 

Definition at line 55 of file iobridge.cpp.

int main ( int  argc,
char *  argv[] 
)

Definition at line 62 of file iobridge.cpp.


Variable Documentation

volatile sig_atomic_t signal_in_progress = 0

Definition at line 37 of file iobridge.cpp.

libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3