Wiselib
wiselib.testing/external_interface/contiki/contiki_distance.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_CONTIKI_DISTANCE__
00020 #define __EXTERNAL_INTERFACE_CONTIKI_DISTANCE__
00021 
00022 #include "internal_interface/routing_table/routing_table_static_array.h"
00023 #include "external_interface/contiki/contiki_os.h"
00024 
00025 #include "external_interface/contiki/contiki_radio.h"
00026 #include "external_interface/contiki/contiki_types.h"
00027 //#include "util/delegates/delegate.hpp"
00028 //#include <isense/os.h>
00029 //#include <isense/radio.h>
00030 //#include <isense/dispatcher.h>
00031 #include "external_interface/external_interface.h"
00032 extern "C" {
00033 #include "net/rime.h"
00034 }
00035 
00036 
00037 namespace wiselib
00038 {
00039 
00040    template<typename OsModel_P,
00041             typename Radio_P = typename OsModel_P::Radio,
00042             int TABLE_SIZE = 20>
00043    class ContikiDistanceModel
00044    {
00045    public:
00046       typedef OsModel_P OsModel;
00047 
00048       typedef Radio_P Radio;
00049       typedef typename Radio::node_id_t node_id_t;
00050       typedef typename Radio::block_data_t block_data_t;
00051       typedef typename Radio::size_t size_t;
00052       //typedef typename Radio::ExtendedData ExtendedData;
00053 
00054       typedef ContikiDistanceModel<OsModel, Radio, TABLE_SIZE> self_type;
00055       typedef self_type* self_pointer_t;
00056 
00057       typedef double distance_t;
00058       typedef StaticArrayRoutingTable<OsModel, Radio, TABLE_SIZE, distance_t> DistanceMap;
00059       typedef typename DistanceMap::iterator DistanceMapIterator;
00060       // --------------------------------------------------------------------
00061       ContikiDistanceModel()
00062       {}
00063       // --------------------------------------------------------------------
00064       void init( Radio& radio)
00065       {
00066          radio_ = &radio;
00067       }
00068       // --------------------------------------------------------------------
00069       inline void enable()
00070       {
00071        /* pFunc = &iSenseDistanceModel<OsModel, Radio, TABLE_SIZE>::template receive;
00072          radio_->template reg_recv_callback<iSenseDistanceModel<OsModel, Radio, TABLE_SIZE>, pFunc>( this );
00073          */
00074         radio_->template reg_recv_callback<self_type, self_type::receive>( this );
00075       }
00076       // --------------------------------------------------------------------
00077       inline void disable()
00078       {
00079          // TODO: Unregister from radio
00080       }
00081       // --------------------------------------------------------------------
00082       inline distance_t distance( node_id_t to )
00083       {
00084          distance_t distance = -1.0;
00085 
00086          DistanceMapIterator it = distances_.find( to );
00087          if ( it != distances_.end() )
00088             distance = it->second;
00089 
00090          return distance;
00091       };
00092 
00093 
00094 
00095 
00096 
00097    //   void (iSenseDistanceModel::*pFunc)(node_id_t id, size_t len, block_data_t* data, const ExtendedData& exdata );
00098 
00099    private:
00100 
00101       void receive( node_id_t id, size_t len, block_data_t* data )
00102            {
00103               // TODO: Why does the lqi-radio returns (255 - lqi)? hence, we transform it back here...
00104             int rssi = (int) packetbuf_attr(PACKETBUF_ATTR_RSSI);
00105 
00106 
00107               uint16_t lqi = 255 - (uint16_t)rssi;
00108               distance_t distance = 0;
00109 
00110               if ( lqi >= 118 )
00111                  distance = 1.0;
00112               else
00113                  distance = (-0.2 * lqi) + 25;
00114      //          else if ( lqi < 118 && lqi >= 110 )
00115      //             distance = 2.0;
00116      //          else if ( lqi < 110 && lqi >= 104 )
00117      //             distance = 3.0;
00118      //          else if ( lqi < 104 && lqi >= 100 )
00119      //             distance = 4.0;
00120      //          else if ( lqi < 100 && lqi >= 94 )
00121      //             distance = 5.0;
00122      //          else if ( lqi < 94 && lqi >= 88 )
00123      //             distance = 6.0;
00124      //          else if ( lqi < 88 && lqi >= 83 )
00125      //             distance = 7.0;
00126      //          else if ( lqi < 83 && lqi >= 78 )
00127      //             distance = 8.0;
00128      //          else if ( lqi < 78 && lqi >= 73 )
00129      //             distance = 9.0;
00130      //          else
00131      //             distance = 10.0;
00132 
00133               distances_[id] = distance;
00134            }
00135 
00136       DistanceMap distances_;
00137       Radio *radio_;
00138    };
00139 
00140 }
00141 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines