Wiselib
wiselib.stable/external_interface/tinyos/tinyos_radio.h
Go to the documentation of this file.
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 EXTERNAL_INTERFACE_TINYOS_RADIOMODEL_H
00020 #define EXTERNAL_INTERFACE_TINYOS_RADIOMODEL_H
00021 
00022 extern "C" {
00023 #include "external_interface/tinyos/tinyos_wiselib_glue.h"
00024 }
00025 #include "external_interface/tinyos/tinyos_types.h"
00026 #include "util/delegates/delegate.hpp"
00027 
00028 namespace wiselib
00029 {
00030 
00031    namespace tinyos
00032    {
00033       class ExtendedData
00034       {
00035          public:
00036             ExtendedData(){}
00037 
00038             uint16_t link_metric() const
00039             { return link_metric_; };
00040 
00041             void set_link_metric( uint16_t lm )
00042             { link_metric_ = lm; };
00043 
00044          private:
00045             uint16_t link_metric_;
00046       };
00047       // --------------------------------------------------------------------
00048       typedef delegate3<void, uint16_t, uint8_t, uint8_t*> radio_delegate_t;
00049       typedef delegate4<void, uint16_t, uint8_t, uint8_t*, const ExtendedData&> extended_radio_delegate_t;
00050       // --------------------------------------------------------------------
00051       void tinyos_init_wiselib_radio( void );
00052       int tinyos_radio_add_receiver( tinyos::radio_delegate_t& delegate );
00053       int tinyos_radio_add_extended_receiver( tinyos::extended_radio_delegate_t& delegate );
00054       bool tinyos_radio_del_receiver( int idx );
00055    }
00056    // -----------------------------------------------------------------------
00066    template<typename OsModel_P>
00067    class TinyOsRadioModel
00068    {
00069    public:
00070       typedef OsModel_P OsModel;
00071 
00072       typedef TinyOsRadioModel<OsModel> self_type;
00073       typedef self_type* self_pointer_t;
00074 
00075       typedef uint16_t node_id_t;
00076       typedef uint8_t  block_data_t;
00077       typedef uint8_t  size_t;
00078       typedef uint8_t  message_id_t;
00079 
00080       typedef tinyos::ExtendedData ExtendedData;
00081       typedef tinyos::radio_delegate_t radio_delegate_t;
00082       typedef tinyos::extended_radio_delegate_t extended_radio_delegate_t;
00083       // --------------------------------------------------------------------
00084       enum ErrorCodes
00085       {
00086          SUCCESS = OsModel::SUCCESS,
00087          ERR_UNSPEC = OsModel::ERR_UNSPEC,
00088          ERR_NOTIMPL = OsModel::ERR_NOTIMPL
00089       };
00090       // --------------------------------------------------------------------
00091       enum SpecialNodeIds {
00092          BROADCAST_ADDRESS = 0xffff, 
00093          NULL_NODE_ID      = 0       
00094       };
00095       // --------------------------------------------------------------------
00096       enum Restrictions {
00097          MAX_MESSAGE_LENGTH = 28  
00098       };
00099       // --------------------------------------------------------------------
00102       int send( node_id_t id, size_t len, block_data_t *data )
00103       {
00104          if ( tinyos_send_message( id, len, data ) == TINYOS_RADIO_SUCCESS )
00105             return SUCCESS;
00106 
00107          // TODO: tinyos_send_message returns more error codes - use them!
00108          return ERR_UNSPEC;
00109       }
00110       // --------------------------------------------------------------------
00111       int enable_radio()
00112       {
00113          return SUCCESS;
00114       }
00115       // --------------------------------------------------------------------
00116       int disable_radio()
00117       {
00118          return SUCCESS;
00119       }
00120       // --------------------------------------------------------------------
00121       node_id_t id()
00122       {
00123          return tinyos_get_nodeid();
00124       }
00125       // --------------------------------------------------------------------
00126       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00127       int reg_recv_callback( T *obj_pnt )
00128       {
00129          radio_delegate_t delegate =
00130                radio_delegate_t::from_method<T, TMethod>( obj_pnt );
00131          return tinyos::tinyos_radio_add_receiver( delegate );
00132       }
00133       // --------------------------------------------------------------------
00134       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*,const ExtendedData&)>
00135       int reg_recv_callback( T *obj_pnt )
00136       {
00137          extended_radio_delegate_t delegate =
00138                extended_radio_delegate_t::from_method<T, TMethod>( obj_pnt );
00139          return tinyos::tinyos_radio_add_extended_receiver( delegate );
00140       }
00141       // --------------------------------------------------------------------
00142       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00143       int unreg_recv_callback( int idx )
00144       {
00145          if (tinyos::tinyos_radio_del_receiver( idx ))
00146             return SUCCESS;
00147          else
00148             return ERR_UNSPEC;
00149       }
00150    };
00151 
00152 
00153 }
00154 
00155 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines