06-26-04 03:11 PM
Hello,
Did anyone succeed in using the timer API with Linux ?
I tried to use my code to create a repeated timer but it run only once :-(
Any hint ?
--
Fabien
--------------------------------------------------------------------------
#ifndef __TimerClass_
#define __TimerClass_
//<SECTION> Standard Include =============================
#include <iostream.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <stdexcept>
//<SECTION> Local Include =============================
#include "utils.h"
//<SECTION> Constant Definitions ===============================
typedef void (*SIGFUNC) (int, siginfo_t*, void *);
//<SECTION> Class Declaration ========================================
class TimerClass
{
/*
* Attributes
*/
private:
// Timer id
timer_t tmTimerId;
// Indicates if the timer is enabled
bool tmTimerEnabled;
// Indicates if the timer is triggered once
bool tmSingleShot;
// Timer interval in {second, nanosecond}
struct itimerspec tmTimerInterval;
/*
* Operations
*/
public:
/*<FUNCTION> TimerClass ****************************************
****
*
* Description: Constructor
*
* Input parameters:
* - sigNb: Signal triggered by the timer
* - p_function: Function triggered by the timer
* - tag: Tag used to differentiate the timers
* - singleShot: Indicates if the timer triggers only once (defaulted to no)
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
TimerClass(int sigNb, SIGFUNC p_function, int tag, bool singleShot = false);
/*<FUNCTION> ~TimerClass ****************************************
****
*
* Description: Destructor
*
* Input parameters:
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
~TimerClass();
/*<FUNCTION> setEnabled ****************************************
****
*
* Description: This function enables or disables the timer
*
* Input parameters:
* - b_value: Indicates the required state
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
void
setEnabled(bool b_value);
/*<FUNCTION> setInterval ****************************************
****
*
* Description: This function updates the timer interval
*
* Input parameters:
* - tmi: Interval in {seconds, nanoseconds}
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
void
setInterval(struct itimerspec tmi);
};
#endif
-------------------------------------------------------------------------
//<SECTION> Local Include =============================
#include "TimerClass.h"
/*<FUNCTION> TimerClass ****************************************
****
*
* Description: Constructor
*
* Input parameters:
* - sigNb: Signal triggered by the timer
* - p_function: Function triggered by the timer
* - tag: Tag used to differentiate the timers
* - singleShot: Indicates if the timer triggers only once (defaulted to no)
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
TimerClass::TimerClass(int sigNb, SIGFUNC p_function, int tag, bool singleSh
ot)
{
try
{
tmSingleShot = singleShot;
// The timer is suspended
tmTimerEnabled = false;
// The interval is defaulted to 1 second
tmTimerInterval.it_value.tv_sec = 1;
tmTimerInterval.it_value.tv_nsec = 0;
// Defines the timer action
struct sigaction sAction;
sigemptyset(&sAction.sa_mask);
sAction.sa_flags = SA_SIGINFO;
sAction.sa_sigaction = p_function;
int i_res = sigaction(sigNb, &sAction, 0);
if (i_res == -1)
{
perror("TimerClass::TimerClass:sigaction");
}
// Defines the signal associated to the timer
struct sigevent sEvent;
sEvent.sigev_notify = SIGEV_SIGNAL;
sEvent.sigev_signo = sigNb;
sEvent.sigev_value.sival_int = tag;
// Creates the timer
i_res = timer_create(CLOCK_REALTIME, &sEvent, &tmTimerId);
if (i_res == -1)
{
perror("TimerClass::TimerClass:timer_create");
}
}
catch(std::exception& e)
{
std::string s_timeString = getCurrentTime();
std::cout <<"\n"<<s_timeString.c_str()
<< ":Error TimerClass::TimerClass"
<< e.what() <<std::flush;
}
}
/*<FUNCTION> ~TimerClass ****************************************
****
*
* Description: Destructor
*
* Input parameters:
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
TimerClass::~TimerClass()
{
try
{
// Suspends the timer
setEnabled(false);
// Destroys the timer
int i_res = timer_delete(tmTimerId);
if (i_res == -1)
{
perror("TimerClass::~TimerClass:timer_delete");
}
}
catch(std::exception& e)
{
std::string s_timeString = getCurrentTime();
std::cout <<"\n"<<s_timeString.c_str()
<< ":Error TimerClass::~TimerClass"
<< e.what() <<std::flush;
}
}
/*<FUNCTION> setEnabled ****************************************
****
*
* Description: This function enables or disables the timer
*
* Input parameters:
* - b_value: Indicates the required state
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
void
TimerClass::setEnabled(bool b_value)
{
try
{
tmTimerEnabled = b_value;
int i_res;
switch(b_value)
{
case true:
{
// Starts the timer
i_res = timer_settime(tmTimerId, 0, &tmTimerInterval, 0);
if (i_res == -1)
{
perror("TimerClass::setEnabled true:timer_settime");
}
break;
}
case false:
{
// Stops the timer
struct itimerspec tmi;
tmi.it_value.tv_sec = 0;
tmi.it_value.tv_nsec = 0;
tmi.it_interval.tv_sec = 0;
tmi.it_interval.tv_nsec = 0;
i_res = timer_settime(tmTimerId, 0, &tmi, 0);
if (i_res == -1)
{
perror("TimerClass::setEnabled false:timer_settime");
}
break;
}
}
}
catch(std::exception& e)
{
std::string s_timeString = getCurrentTime();
std::cout <<"\n"<<s_timeString.c_str()
<< ":Error TimerClass::setEnabled"
<< e.what() <<std::flush;
}
}
/*<FUNCTION> setInterval ****************************************
****
*
* Description: This function updates the timer interval
*
* Input parameters:
* - tmi: Interval in {seconds, nanoseconds}
*
* Output parameters:
*
* Return value:
*
****************************************
***********************************/
void
TimerClass::setInterval(struct itimerspec tmi)
{
tmTimerInterval.it_value.tv_sec = tmi.it_value.tv_sec;
tmTimerInterval.it_value.tv_nsec = tmi.it_value.tv_nsec;
// Checks if the timer triggers once
if (tmSingleShot)
{
tmTimerInterval.it_interval.tv_sec = 0;
tmTimerInterval.it_interval.tv_nsec = 0;
}
else
{
tmTimerInterval.it_interval.tv_sec = tmi.it_interval.tv_sec;
tmTimerInterval.it_interval.tv_nsec = tmi.it_interval.tv_nsec;
}
}
/*==================== End of file =======================*/
[ Post a follow-up to this message ]
|