ut_timer_epoc.cpp

Go to the documentation of this file.
00001 #include "ut_timer.h"
00002 
00003 #include "er_errors.h"
00004 
00005 #include "common/epoc-time.h"
00006 
00007 #include <e32base.h>
00008 
00009 class CUtTimer;
00010 
00011 struct _ut_Timer {
00012   CUtTimer* timer;
00013   void* userdata; 
00014   ut_TimerCallback* cb;
00015 };
00016 
00017 NONSHARABLE_CLASS(CUtTimer) :
00018   public CTimer
00019 {
00020 public:
00021 
00022   static CUtTimer *NewLC(ut_Timer &aInterface, TInt aPriority);
00023 
00024   static CUtTimer *NewL(ut_Timer &aInterface, TInt aPriority);
00025 
00026 private:
00027 
00028   CUtTimer(ut_Timer &aInterface, TInt aPriority);
00029 
00030   virtual void RunL();
00031 
00032   ut_Timer &iInterface;
00033 };
00034 
00035 CUtTimer *CUtTimer::NewLC(ut_Timer &aInterface, TInt aPriority)
00036 {
00037   CUtTimer *object = new (ELeave) CUtTimer(aInterface, aPriority);
00038   CleanupStack::PushL(object);
00039   object->ConstructL();
00040   return object;
00041 }
00042 
00043 CUtTimer *CUtTimer::NewL(ut_Timer &aInterface, TInt aPriority)
00044 {
00045   CUtTimer *object = NewLC(aInterface, aPriority);
00046   CleanupStack::Pop();
00047   return object;
00048 }
00049 
00050 CUtTimer::CUtTimer(ut_Timer &aInterface, TInt aPriority) : 
00051   CTimer(aPriority), iInterface(aInterface)
00052 {
00053   CActiveScheduler::Add(this);
00054 }
00055 
00056 #define make_error(_errCode, _msg)          \
00057   gx_error_new(domain_symbian, _errCode, _msg ": %s (%d)",    \
00058          plat_error_strerror(_errCode), _errCode);
00059 
00060 void CUtTimer::RunL()
00061 {
00062   GError* error = NULL;
00063   TInt errCode = iStatus.Int();
00064   if (errCode) {
00065     error = make_error(errCode, "timer error");
00066   }
00067   (*(iInterface.cb))(iInterface.userdata, error);
00068 }
00069 
00070 extern "C" ut_Timer* ut_Timer_new(void* userdata, ut_TimerCallback* cb, GError** error)
00071 {
00072   ut_Timer* self = g_try_new0(ut_Timer, 1);
00073   if (G_UNLIKELY(!self)) {
00074     if (error) *error = gx_error_no_memory;
00075     return NULL;
00076   }
00077   self->userdata = userdata;
00078   self->cb = cb;
00079 
00080   TRAPD(errCode, self->timer = CUtTimer::NewL(*self, CActive::EPriorityStandard));
00081   if (G_UNLIKELY(errCode)) {
00082     g_free(self);
00083     if (error) *error = make_error(errCode, "failed to create native timer instance");
00084     return NULL;
00085   }
00086 
00087   return self;
00088 }
00089   
00090 extern "C" void ut_Timer_destroy(ut_Timer* self)
00091 {
00092   if (self) {
00093     delete self->timer;
00094     g_free(self);
00095   }
00096 }
00097 
00098 extern "C" gboolean ut_Timer_set_after(ut_Timer* self, int secs, GError** error)
00099 {
00100   ut_Timer_cancel(self);
00101 
00102   TTimeIntervalMicroSeconds32 interval = SecsToUsecs(secs);
00103   //logg("interval timer set to %d secs / %d usecs", secs, interval.Int());
00104 
00105   // Note that these timers should not complete with KErrAbort, since
00106   // a wait for an interval should not be affected by a system time
00107   // change.
00108   self->timer->After(interval);
00109   
00110   return TRUE;
00111 }
00112 
00113 extern "C" void ut_Timer_cancel(ut_Timer* self)
00114 {
00115   self->timer->Cancel();
00116 }
00117 
00118 extern "C" gboolean ut_Timer_is_active(ut_Timer* self)
00119 {
00120   return self->timer->IsActive();
00121 }
00122 
00123 /**
00124 
00125 ut_timer_epoc.cpp
00126 
00127 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00128 and the authors. All rights reserved.
00129 
00130 Authors: Tero Hasu <tero.hasu@hut.fi>
00131 
00132 Permission is hereby granted, free of charge, to any person
00133 obtaining a copy of this software and associated documentation files
00134 (the "Software"), to deal in the Software without restriction,
00135 including without limitation the rights to use, copy, modify, merge,
00136 publish, distribute, sublicense, and/or sell copies of the Software,
00137 and to permit persons to whom the Software is furnished to do so,
00138 subject to the following conditions:
00139 
00140 The above copyright notice and this permission notice shall be
00141 included in all copies or substantial portions of the Software.
00142 
00143 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00144 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00145 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00146 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00147 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00148 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00149 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00150 SOFTWARE.
00151 
00152  **/

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