Wiselib
wiselib.testing/external_interface/isense/isense_extended_txradio_isensestyle.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_EXTENDEDISENSESTYLETXRADIOMODEL_H
00020 #define CONNECTOR_ISENSE_EXTENDEDISENSESTYLETXRADIOMODEL_H
00021 
00022 #include "external_interface/isense/isense_types.h"
00023 #include "util/delegates/delegate.hpp"
00024 #include <isense/os.h>
00025 #include <isense/radio.h>
00026 #include <isense/hardware_radio.h>
00027 #include <isense/dispatcher.h>
00028 #include <isense/time.h>
00029 
00030 namespace wiselib
00031 {
00032 
00033    template<typename OsModel_P>
00034    class iSenseExtendediSenseStyleTxRadioModel
00035    : public isense::Receiver
00036    {
00037    public:
00038       class ExtendedData
00039       {
00040          public:
00041             ExtendedData(){}
00042 
00043             uint16 get_rssi() const
00044          { return rssi_; };
00045 
00046          void set_rssi( uint16 rssi )
00047          { rssi_ = rssi; };
00048 
00049          uint16 get_lqi() const
00050          { return lqi_; };
00051 
00052          void set_lqi( uint16 lqi )
00053          { lqi_ = lqi; };
00054 
00055        private:
00056          uint16 rssi_;
00057          uint16 lqi_;
00058       };
00059       // --------------------------------------------------------------------
00060       typedef OsModel_P OsModel;
00061 
00062       typedef iSenseExtendediSenseStyleTxRadioModel<OsModel> self_type;
00063       typedef self_type* self_pointer_t;
00064 
00065 #ifdef ISENSE_RADIO_ADDR_TYPE
00066       typedef ISENSE_RADIO_ADDR_TYPE node_id_t;
00067 #else
00068       typedef uint16 node_id_t;
00069 #endif
00070       typedef uint8  block_data_t;
00071       typedef uint8  size_t;
00072       typedef uint8  message_id_t;
00073 
00074       typedef delegate3<void, node_id_t, size_t, block_data_t*> isense_radio_delegate_t;
00075       typedef delegate5<void, node_id_t, size_t, block_data_t*,uint8,uint8> extended_radio_delegate_iss_t;
00076       typedef isense_radio_delegate_t radio_delegate_t;
00077       // --------------------------------------------------------------------
00078       enum ErrorCodes
00079       {
00080          SUCCESS = OsModel::SUCCESS,
00081          ERR_UNSPEC = OsModel::ERR_UNSPEC,
00082          ERR_NOTIMPL = OsModel::ERR_NOTIMPL
00083       };
00084       // --------------------------------------------------------------------
00085       enum { MAX_INTERNAL_RECEIVERS = 10 };
00086       enum { MAX_EXTENDED_RECEIVERS = MAX_INTERNAL_RECEIVERS };
00087       // --------------------------------------------------------------------
00088       enum SpecialNodeIds {
00089          BROADCAST_ADDRESS = 0xffff, 
00090          NULL_NODE_ID      = 0       
00091       };
00092       // --------------------------------------------------------------------
00093       enum Restrictions {
00094          MAX_MESSAGE_LENGTH = 116    
00095       };
00096       // --------------------------------------------------------------------
00097       class TxPower;
00098       // --------------------------------------------------------------------
00099       iSenseExtendediSenseStyleTxRadioModel( isense::Os& os )
00100          : os_(os)
00101       {
00102          os_.dispatcher().add_receiver( this );
00103       }
00104       // --------------------------------------------------------------------
00105       int
00106       send( node_id_t id, size_t len, block_data_t *data )
00107       {
00108          os().radio().send( id, len, data, 0, 0 );
00109          return SUCCESS;
00110       }
00111       // --------------------------------------------------------------------
00112       int enable_radio()
00113       {
00114          os().radio().enable();
00115          return SUCCESS;
00116       }
00117       // --------------------------------------------------------------------
00118       int disable_radio()
00119       {
00120          os().radio().disable();
00121          return SUCCESS;
00122       }
00123       // --------------------------------------------------------------------
00124       node_id_t id()
00125       {
00126          return os().id();
00127       }
00128       // --------------------------------------------------------------------
00129       //---------- From concept VariablePowerRadio ------------
00130       int set_power(TxPower p);
00131       //-------------------------------------------------------
00132       TxPower power();
00133       //-------------------------------------------------------
00134       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00135       int reg_recv_callback( T *obj_pnt )
00136       {
00137          for ( int i = 0; i < MAX_INTERNAL_RECEIVERS; i++ )
00138          {
00139             if ( !isense_radio_callbacks_[i] )
00140             {
00141                isense_radio_callbacks_[i] =
00142                   isense_radio_delegate_t::template from_method<T, TMethod>( obj_pnt );
00143                return i;
00144             }
00145          }
00146          return -1;
00147       }
00148       // --------------------------------------------------------------------
00149       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*,uint8,uint8)>
00150       int reg_recv_callback( T *obj_pnt )
00151       {
00152          for ( int i = 0; i < MAX_EXTENDED_RECEIVERS; i++ )
00153          {
00154             if ( !isense_ext_radio_callbacks_[i] )
00155             {
00156                isense_ext_radio_callbacks_[i]=
00157                 extended_radio_delegate_iss_t::template from_method<T, TMethod>( obj_pnt );
00158                return i;
00159             }
00160          }
00161          return -1;
00162       }
00163       // --------------------------------------------------------------------
00164       int unreg_recv_callback( int idx )
00165       {
00166          return ERR_NOTIMPL;
00167       }
00168       // --------------------------------------------------------------------
00169 #ifdef ISENSE_RADIO_ADDR_TYPE
00170       virtual void receive( uint8 len, const uint8 *buf,
00171          ISENSE_RADIO_ADDR_TYPE src_addr, ISENSE_RADIO_ADDR_TYPE dest_addr,
00172          uint16 signal_strength, uint16 signal_quality, uint8 sequence_no,
00173          uint8 interface, isense::Time rx_time )
00174 #else
00175       virtual void receive( uint8 len, const uint8 *buf,
00176                               uint16 src_addr, uint16 dest_addr,
00177                               uint16 lqi, uint8 seq_no, uint8 interface )
00178 #endif
00179       {
00180          for ( int i = 0; i < MAX_INTERNAL_RECEIVERS; i++ )
00181          {
00182             if ( isense_radio_callbacks_[i] )
00183                isense_radio_callbacks_[i]( src_addr, len, const_cast<uint8*>(buf) );
00184          }
00185 
00186          for ( int i = 0; i < MAX_EXTENDED_RECEIVERS; i++ )
00187          {
00188             if ( isense_ext_radio_callbacks_[i] )
00189                isense_ext_radio_callbacks_[i]( src_addr, len, const_cast<uint8*>(buf), (uint8)signal_strength, (uint8)signal_quality );
00190          }
00191       }
00192 
00193    private:
00194       isense::Os& os()
00195       { return os_; }
00196       // --------------------------------------------------------------------
00197       isense::Os& os_;
00198       isense_radio_delegate_t isense_radio_callbacks_[MAX_INTERNAL_RECEIVERS];
00199       extended_radio_delegate_iss_t isense_ext_radio_callbacks_[MAX_EXTENDED_RECEIVERS];
00200    };
00201    // --------------------------------------------------------------------
00207    template<typename OsModel_P>
00208    class iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower {
00209    public:
00210       TxPower();
00211       TxPower(TxPower const &);
00212 
00213       TxPower &operator=(TxPower const &);
00214       bool operator==(TxPower) const;
00215       bool operator!=(TxPower) const;
00216       bool operator<=(TxPower) const;
00217       bool operator>=(TxPower) const;
00218       bool operator<(TxPower) const;
00219       bool operator>(TxPower) const;
00220       TxPower operator++();
00221       TxPower operator++(int);
00222       TxPower operator--();
00223       TxPower operator--(int);
00224 
00225       static TxPower from_ratio(int);
00226       void set_ratio(int);
00227       int to_ratio() const;
00228       static TxPower from_dB(int);
00229       void set_dB(int);
00230       int to_dB() const;
00231 
00232       static TxPower const MIN;
00233       static TxPower const MAX;
00234 
00235    private:
00236       TxPower(int);
00237 
00238       int value;
00239 
00240       friend class iSenseExtendediSenseStyleTxRadioModel<OsModel_P>;
00241    };
00242 
00243    template<typename OsModel_P>
00244    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower const  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::MIN=-30;
00245 
00246    template<typename OsModel_P>
00247    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower const  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::MAX=0;
00248 
00249    template<typename OsModel_P>
00250    inline  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::TxPower(int v):value(v){}
00251 
00252    template<typename OsModel_P>
00253    inline  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::TxPower():value(0){}
00254 
00255    template<typename OsModel_P>
00256    inline typename iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower &iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator=(TxPower const &p) {
00257       value=p.value;
00258       return *this;
00259    }
00260 
00261    template<typename OsModel_P>
00262    inline iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::TxPower(TxPower const &power):value(power.value){}
00263 
00264    template<typename OsModel_P>
00265    inline bool  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator==(TxPower p) const {
00266       return value==p.value;
00267    }
00268 
00269    template<typename OsModel_P>
00270    inline bool  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator!=(TxPower p) const {
00271       return value!=p.value;
00272    }
00273 
00274    template<typename OsModel_P>
00275    inline bool  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator<=(TxPower p) const {
00276       return value<=p.value;
00277    }
00278 
00279    template<typename OsModel_P>
00280    inline bool  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator>=(TxPower p) const {
00281       return value>=p.value;
00282    }
00283 
00284    template<typename OsModel_P>
00285    inline bool  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator<(TxPower p) const {
00286       return value<p.value;
00287    }
00288 
00289    template<typename OsModel_P>
00290    inline bool  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator>(TxPower p) const {
00291       return value>p.value;
00292    }
00293 
00294    template<typename OsModel_P>
00295    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator++(){
00296       value+=6;
00297       return *this;
00298    }
00299 
00300    template<typename OsModel_P>
00301    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator++(int){
00302       TxPower p=*this;
00303       value+=6;
00304       return p;
00305    }
00306 
00307    template<typename OsModel_P>
00308    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator--(){
00309       value-=6;
00310       return *this;
00311    }
00312 
00313    template<typename OsModel_P>
00314    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::operator--(int){
00315       TxPower p=*this;
00316       value-=6;
00317       return p;
00318    }
00319 
00320    template<typename OsModel_P>
00321    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::from_ratio(int ratio){
00322       if(ratio<=1)
00323          return MIN;
00324       else if(ratio<=4)
00325          return TxPower(-24);
00326       else if(ratio<=16)
00327          return TxPower(-18);
00328       else if(ratio<=63)
00329          return TxPower(-12);
00330       else if(ratio<=251)
00331          return TxPower(-6);
00332       else
00333          return MAX;
00334    }
00335 
00336    template<typename OsModel_P>
00337    void  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::set_ratio(int ratio){
00338       if(ratio<=1)
00339          value=-30;
00340       else if(ratio<=4)
00341          value=-24;
00342       else if(ratio<=16)
00343          value=-18;
00344       else if(ratio<=63)
00345          value=-12;
00346       else if(ratio<=251)
00347          value=-6;
00348       else
00349          value=0;
00350    }
00351 
00352    template<typename OsModel_P>
00353    int  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::to_ratio() const {
00354       switch(value){
00355       case 0:
00356          return 1000;
00357       case -6:
00358          return 251;
00359       case -12:
00360          return 63;
00361       case -18:
00362          return 16;
00363       case -24:
00364          return 4;
00365       default:
00366          return 1;
00367       }
00368    }
00369 
00370    template<typename OsModel_P>
00371    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::from_dB(int db){
00372       if(db<=-30)
00373          return MIN;
00374       else if(db<=-24)
00375          return TxPower(-24);
00376       else if(db<=-18)
00377          return TxPower(-18);
00378       else if(db<=-12)
00379          return TxPower(-12);
00380       else if(db<=-6)
00381          return TxPower(-6);
00382       else
00383          return MAX;
00384    }
00385 
00386    template<typename OsModel_P>
00387    void  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::set_dB(int db){
00388       if(db<=-30)
00389          value=-30;
00390       else if(db<=-24)
00391          value=-24;
00392       else if(db<=-18)
00393          value=-18;
00394       else if(db<=-12)
00395          value=-12;
00396       else if(db<=-6)
00397          value=-6;
00398       else
00399          value=0;
00400 //Another way: value=-(((-db)/6)*6);
00401    }
00402 
00403    template<typename OsModel_P>
00404    inline int  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower::to_dB() const {
00405       return value;
00406    }
00407    // --------------------------------------------------------------------
00408    template<typename OsModel_P>
00409    int  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::set_power(TxPower p){
00410       os().radio().hardware_radio().set_power(p.value);
00411       return SUCCESS;
00412    }
00413 
00414    template<typename OsModel_P>
00415    typename  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::TxPower  iSenseExtendediSenseStyleTxRadioModel<OsModel_P>::power(){
00416      return TxPower(os().radio().hardware_radio().power());
00417    }
00418    //-------------------------------------------------------
00419 }
00420 
00421 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines