Wiselib
wiselib.stable/external_interface/isense/isense_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 CONNECTOR_ISENSE_RADIOMODEL_H
00020 #define CONNECTOR_ISENSE_RADIOMODEL_H
00021 
00022 #include "external_interface/isense/isense_os.h"
00023 #include "external_interface/isense/isense_types.h"
00024 #include "util/delegates/delegate.hpp"
00025 #include <isense/os.h>
00026 #include <isense/radio.h>
00027 #include <isense/dispatcher.h>
00028 
00029 namespace wiselib
00030 {
00031 
00040    template<typename OsModel_P>
00041    class iSenseRadioModel
00042       : public isense::Receiver
00043    {
00044    public:
00045       typedef OsModel_P OsModel;
00046 
00047       typedef iSenseRadioModel<OsModel> self_type;
00048       typedef self_type* self_pointer_t;
00049 
00050       typedef uint16 node_id_t;
00051       typedef uint8  block_data_t;
00052       typedef uint8  size_t;
00053       typedef uint8  message_id_t;
00054       // --------------------------------------------------------------------
00055       typedef delegate3<void, node_id_t, size_t, block_data_t*> isense_radio_delegate_t;
00056       typedef isense_radio_delegate_t radio_delegate_t;
00057       // --------------------------------------------------------------------
00058       enum ErrorCodes
00059       {
00060          SUCCESS = OsModel::SUCCESS,
00061          ERR_UNSPEC = OsModel::ERR_UNSPEC
00062       };
00063       // --------------------------------------------------------------------
00064       enum { MAX_INTERNAL_RECEIVERS = 10 };
00065       // --------------------------------------------------------------------
00066       enum SpecialNodeIds {
00067          BROADCAST_ADDRESS = 0xffff, 
00068          NULL_NODE_ID      = 0       
00069       };
00070       // --------------------------------------------------------------------
00071       enum Restrictions {
00072          MAX_MESSAGE_LENGTH = 116    
00073       };
00074       // --------------------------------------------------------------------
00075       iSenseRadioModel( isense::Os& os )
00076          : os_(os)
00077       {
00078          os_.dispatcher().add_receiver( this );
00079       }
00080       // --------------------------------------------------------------------
00081       virtual ~iSenseRadioModel()
00082       {}
00083       // --------------------------------------------------------------------
00084       int
00085       send( node_id_t id, size_t len, block_data_t *data )
00086       {
00087          os_.radio().send( id, len, data, 0, 0 );
00088          return SUCCESS;
00089       }
00090       // --------------------------------------------------------------------
00091       int enable_radio()
00092       {
00093          os_.radio().enable();
00094          return SUCCESS;
00095       }
00096       // --------------------------------------------------------------------
00097       int disable_radio()
00098       {
00099 #ifndef ISENSE_ENABLE_REMOVE_RECEIVERS
00100    #warning ISENSE_ENABLE_REMOVE_RECEIVERS is not enabled. Add to be able to remove receiers.
00101 #endif
00102          os_.radio().disable();
00103          return SUCCESS;
00104       }
00105       // --------------------------------------------------------------------
00106       node_id_t id()
00107       {
00108          return os_.id();
00109       }
00110       // --------------------------------------------------------------------
00111       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00112       int reg_recv_callback(T *obj_pnt )
00113       {
00114          int idx = get_free_receiver_item();
00115          if ( idx < 0 )
00116             return -1;
00117 
00118          isense_radio_callbacks_[idx] =
00119             isense_radio_delegate_t::template from_method<T, TMethod>( obj_pnt );
00120 
00121          return idx;
00122       }
00123       // --------------------------------------------------------------------
00124       void unreg_recv_callback(int idx )
00125       {
00126          isense_radio_callbacks_[idx] = isense_radio_delegate_t();
00127          //return SUCCESS;
00128       }
00129       // --------------------------------------------------------------------
00130 #ifdef ISENSE_RADIO_ADDR_TYPE
00131          virtual void receive( uint8 len, const uint8 *buf,
00132             ISENSE_RADIO_ADDR_TYPE src_addr, ISENSE_RADIO_ADDR_TYPE dest_addr,
00133             uint16 signal_strength, uint16 signal_quality, uint8 sequence_no,
00134             uint8 interface, isense::Time rx_time )
00135 #else
00136          virtual void receive( uint8 len, const uint8 *buf,
00137                                uint16 src_addr, uint16 dest_addr,
00138                                uint16 lqi, uint8 seq_no, uint8 interface )
00139 #endif
00140       {
00141          for ( int i = 0; i < MAX_INTERNAL_RECEIVERS; i++ )
00142          {
00143             if ( isense_radio_callbacks_[i] )
00144                isense_radio_callbacks_[i]( src_addr, len, const_cast<uint8*>(buf) );
00145          }
00146       }
00147 
00148    private:
00149       int get_free_receiver_item()
00150       {
00151          for ( int i = 0; i < MAX_INTERNAL_RECEIVERS; i++ )
00152          {
00153             if ( !isense_radio_callbacks_[i] )
00154                return i;
00155          }
00156          return -1;
00157       }
00158       // --------------------------------------------------------------------
00159       int find_receiver_item( void *obj_ptr )
00160       {
00161          for ( int i = 0; i < MAX_INTERNAL_RECEIVERS; i++ )
00162          {
00163             if ( isense_radio_callbacks_[i].obj_ptr() == obj_ptr )
00164                return i;
00165          }
00166          return -1;
00167       }
00168       // --------------------------------------------------------------------
00169       // --------------------------------------------------------------------
00170       // --------------------------------------------------------------------
00171       isense::Os& os_;
00172       isense_radio_delegate_t isense_radio_callbacks_[MAX_INTERNAL_RECEIVERS];
00173    };
00174 }
00175 
00176 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines