sa_sensor_timer.c

Go to the documentation of this file.
00001 // This implementation is presently only intended for testing. It is a
00002 // simple timer sensor for producing "dummy" sensor events at random
00003 // time intervals. It is implemented based on the internal,
00004 // platform-independent ut_Timer API.
00005 
00006 #include "sa_sensor_timer.h"
00007 
00008 #if __TIMER_ENABLED__
00009 
00010 #include "er_errors.h"
00011 #include "ld_logging.h"
00012 #include "sa_sensor_list_log_db.h"
00013 #include "ut_timer.h"
00014 
00015 #include "common/utilities.h"
00016 
00017 #include <errno.h>
00018 #include <stdlib.h>
00019 #include <sys/time.h>
00020 #include <time.h>
00021 
00022 struct _sa_Sensor_timer {
00023   ut_Timer* timer;
00024   LogDb* log; // not owned, no refcounting
00025 };
00026 
00027 static void timerCallback(void* userdata, GError* timerError);
00028 
00029 static void setTimer(sa_Sensor_timer* self)
00030 {
00031   int wait_secs = ((rand() % 30) + 1);
00032   logg("waiting for %d secs", wait_secs);
00033   
00034   GError* timerError = NULL;
00035   if (!ut_Timer_set_after(self->timer, wait_secs, &timerError)) {
00036     er_log_gerror(er_FATAL|er_FREE, timerError, 
00037       "error setting timer in timer sensor");
00038     return;
00039   }
00040 }
00041 
00042 // A non-NULL timerError indicates an error.
00043 // Caller takes ownership of any timerError.
00044 static void timerCallback(void* userdata, GError* timerError)
00045 {
00046   sa_Sensor_timer* self = (sa_Sensor_timer*)userdata;
00047 
00048   if (timerError) {
00049     er_log_gerror(er_FATAL|er_FREE, timerError, 
00050       "timer error event in timer sensor");
00051     return;
00052   }
00053 
00054   // Log "sensor" event.
00055   GError* localError = NULL;
00056   if (!log_db_log_timer(self->log, &localError)) {
00057     er_log_gerror(er_FATAL|er_FREE, localError, 
00058       "logging error in timer sensor");
00059     return;
00060   }
00061 
00062   guilogf("timer: event");
00063 
00064   // Schedule next timer event.
00065   setTimer(self);
00066 }
00067 
00068 EXTERN_C sa_Sensor_timer* sa_Sensor_timer_new(LogDb* log, GError** error)
00069 {
00070   sa_Sensor_timer* self = g_try_new0(sa_Sensor_timer, 1);
00071   if (G_UNLIKELY(!self)) {
00072     if (error) *error = gx_error_no_memory;
00073     return NULL;
00074   }
00075   self->log = log;
00076   self->timer = ut_Timer_new(self, timerCallback /*xxx*/, error);
00077   if (G_UNLIKELY(!(self->timer))) {
00078     g_free(self);
00079     return NULL;
00080   }
00081   return self;
00082 }
00083 
00084 EXTERN_C void sa_Sensor_timer_destroy(sa_Sensor_timer* self)
00085 {
00086   if (self) {
00087     ut_Timer_destroy(self->timer);
00088     g_free(self);
00089   }
00090 }
00091 
00092 EXTERN_C gboolean sa_Sensor_timer_start(sa_Sensor_timer* self, GError** error)
00093 {
00094   (void)error;
00095   if (!sa_Sensor_timer_is_active(self)) {
00096     setTimer(self);
00097   }
00098   return TRUE;
00099 }
00100 
00101 EXTERN_C void sa_Sensor_timer_stop(sa_Sensor_timer* self)
00102 {
00103   ut_Timer_cancel(self->timer);
00104 }
00105 
00106 EXTERN_C gboolean sa_Sensor_timer_is_active(sa_Sensor_timer* self)
00107 {
00108   return ut_Timer_is_active(self->timer);
00109 }
00110 
00111 #endif /* __TIMER_ENABLED__ */
00112 
00113 /**
00114 
00115 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00116 and the authors. All rights reserved.
00117 
00118 Authors: Tero Hasu <tero.hasu@hut.fi>
00119 
00120 Permission is hereby granted, free of charge, to any person
00121 obtaining a copy of this software and associated documentation files
00122 (the "Software"), to deal in the Software without restriction,
00123 including without limitation the rights to use, copy, modify, merge,
00124 publish, distribute, sublicense, and/or sell copies of the Software,
00125 and to permit persons to whom the Software is furnished to do so,
00126 subject to the following conditions:
00127 
00128 The above copyright notice and this permission notice shall be
00129 included in all copies or substantial portions of the Software.
00130 
00131 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00132 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00133 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00134 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00135 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00136 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00137 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00138 SOFTWARE.
00139 
00140  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:56 2011 by Doxygen 1.6.1