Wiselib
wiselib.testing/radio/sunspot_radio/sunspot_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 __RADIO_SUNSPOT_H__
00020 #define __RADIO_SUNSPOT_H__
00021 
00022 #include "util/base_classes/radio_base.h"
00023 #include "util/serialization/simple_types.h"
00024 #include "config.h"
00025 #include "algorithms/neighbor_discovery/echo.h"
00026 
00027 namespace wiselib {
00028 
00033     template<typename OsModel_P,
00034              typename Radio_P,
00035              typename Timer_P,
00036              typename Debug_P>
00037     class SunSpotRadio
00038     : public RadioBase<OsModel_P,
00039                        typename Radio_P::node_id_t,
00040                        typename Radio_P::size_t,
00041                        typename Radio_P::block_data_t>
00042     {
00043     public:
00044         // Type definitions
00045         typedef OsModel_P OsModel;
00046 
00047         typedef Radio_P Radio;
00048         typedef typename Radio::node_id_t node_id_t;
00049         typedef typename Radio::size_t size_t;
00050         typedef typename Radio::block_data_t block_data_t;
00051         typedef typename Radio::message_id_t message_id_t;
00052 
00053         typedef Timer_P Timer;
00054         typedef Debug_P Debug;
00055 
00056 //        typedef delegate4<void, uint8_t, node_id_t, size_t, block_data_t*> event_notifier_delegate_t;
00057         typedef SunSpotRadio<OsModel_P, Radio_P, Timer_P, Debug_P> self_t;
00058 
00059         // --------------------------------------------------------------------
00060 
00061         enum Restrictions {
00062             HEADER_LENGHT = 3,
00063             MAX_MESSAGE_LENGTH = Radio::MAX_MESSAGE_LENGTH-HEADER_LENGHT
00065         };
00066         // --------------------------------------------------------------------
00067 
00068         void init (Radio& radio , Timer& timer , Debug& debug){
00069             radio_ = &radio;
00070             timer_ = &timer;
00071             debug_ = &debug;
00072             portNum = 110;
00073             HEADER[0] = 0x7f;
00074             HEADER[1] = 0x69;
00075             HEADER[2] = portNum;
00076         };
00077 
00078         SunSpotRadio() {
00079         };
00080 
00081         ~SunSpotRadio() {
00082         };
00083 
00084         void enable_radio( void ) {
00085             radio().enable_radio();
00086             radio().template reg_recv_callback<self_t, &self_t::receive>( this );
00087             #ifdef SUNSPOT_RADIO_DEBUG
00088             debug().debug( "SunSpotRadio: Boot for %i\n", radio().id() );
00089             #endif
00090             #ifdef SUNSPOT_RADIO_DEBUG
00091              debug().debug( "SunSpotRadio: Start as ordinary node\n" );
00092             #endif
00093         }
00094 
00095 
00096         void set_port(uint8_t port) {
00097             portNum = port;
00098             HEADER[2] = portNum;
00099         }
00100 
00101         node_id_t get_port( void ) {
00102             return portNum;
00103         }
00104 
00105         void send(node_id_t id, size_t len, block_data_t *data) {
00106 
00107             if ( len > MAX_MESSAGE_LENGTH )
00108                 return;
00109 
00110             memcpy(data,HEADER,HEADER_LENGHT);
00111             memcpy(data+HEADER_LENGHT,data,len);
00112 
00113             radio().send(id,len+HEADER_LENGHT,data);
00114         }
00115 
00116 
00117         void receive(node_id_t source, size_t len, block_data_t *data ) {
00118             if ( data[0] == 0x7f && data[1]== 0x69 && data[1]== portNum ) {
00119                 notify_receivers(source, len-HEADER_LENGHT, data+HEADER_LENGHT);
00120             }
00121         }
00122 
00123    private:
00124       Radio& radio()
00125       { return *radio_; }
00126 
00127       Timer& timer()
00128       { return *timer_; }
00129 
00130       Debug& debug()
00131       { return *debug_; }
00132 
00133       typename Radio::self_pointer_t radio_;
00134       typename Timer::self_pointer_t timer_;
00135       typename Debug::self_pointer_t debug_;
00136 
00137       uint8_t portNum;
00138       block_data_t HEADER[HEADER_LENGHT];
00139 //      block_data_t as[HEADER_LENGHT] = {1,2,3};
00140         // --------------------------------------------------------------------
00141     };
00142 
00143 }
00144 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines