Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef CONNECTOR_CONTIKI_TIMER_H 00020 #define CONNECTOR_CONTIKI_TIMER_H 00021 00022 #include "external_interface/contiki/contiki_types.h" 00023 #include "util/delegates/delegate.hpp" 00024 00025 extern "C" { 00026 #include "contiki.h" 00027 #include "sys/etimer.h" 00028 } 00029 00030 namespace wiselib 00031 { 00032 typedef delegate1<void, void*> contiki_timer_delegate_t; 00033 // ----------------------------------------------------------------------- 00034 struct timer_item { 00035 struct timer_item *next; 00036 struct etimer etimer; 00037 struct process *p; 00038 contiki_timer_delegate_t cb; 00039 void *ptr; 00040 }; 00041 // ----------------------------------------------------------------------- 00042 void init_contiki_timer( void ); 00043 timer_item* get_timer_item_ref( void ); 00044 // ----------------------------------------------------------------------- 00045 extern process timer_process; 00046 extern timer_item timer_item_; 00047 // ----------------------------------------------------------------------- 00055 template<typename OsModel_P> 00056 class ContikiTimer 00057 { 00058 public: 00059 typedef OsModel_P OsModel; 00060 00061 typedef ContikiTimer<OsModel> self_type; 00062 typedef self_type* self_pointer_t; 00063 00064 typedef uint32_t millis_t; 00065 // -------------------------------------------------------------------- 00066 enum ErrorCodes 00067 { 00068 SUCCESS = OsModel::SUCCESS, 00069 ERR_UNSPEC = OsModel::ERR_UNSPEC 00070 }; 00071 // -------------------------------------------------------------------- 00072 void init() 00073 { 00074 init_contiki_timer(); 00075 } 00076 // -------------------------------------------------------------------- 00077 template<typename T, void (T::*TMethod)(void*)> 00078 int set_timer( millis_t millis, T *obj_pnt, void *userdata ); 00079 }; 00080 // ----------------------------------------------------------------------- 00081 // ----------------------------------------------------------------------- 00082 // ----------------------------------------------------------------------- 00083 template<typename OsModel_P> 00084 template<typename T, void (T::*TMethod)(void*)> 00085 int 00086 ContikiTimer<OsModel_P>:: 00087 set_timer( millis_t millis, T *obj_pnt, void *userdata ) 00088 { 00089 uint16_t secs = millis >> 10; 00090 if (secs == 0) 00091 secs = 1; 00092 timer_item *item = get_timer_item_ref(); 00093 if ( !item ) 00094 return ERR_UNSPEC; 00095 item->p = PROCESS_CURRENT(); 00096 item->cb = contiki_timer_delegate_t::from_method<T, TMethod>( obj_pnt ); 00097 item->ptr = userdata; 00098 00099 PROCESS_CONTEXT_BEGIN( &timer_process ); 00100 etimer_set( &item->etimer, CLOCK_SECOND * secs ); 00101 PROCESS_CONTEXT_END( &timer_process ); 00102 00103 return SUCCESS; 00104 } 00105 00106 } 00107 00108 #endif