Wiselib
wiselib.testing/external_interface/trisos/trisos_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_TRISOS_RADIOMODEL_H
00020 #define CONNECTOR_TRISOS_RADIOMODEL_H
00021 
00022 #include "external_interface/trisos/trisos_types.h"
00023 #include "util/delegates/delegate.hpp"
00024 #include "util/serialization/simple_types.h"
00025 #include <string.h>
00026 extern "C" {
00027 #include "src/sys/sys.h"
00028 #include "src/hw/rf/rf.h"
00029 }
00030 
00031 namespace wiselib
00032 {
00033 
00034    typedef delegate3<void, uint16_t, uint8_t, uint8_t*> trisos_radio_delegate_t;
00035    // -----------------------------------------------------------------------
00036    typedef void (*trisos_radio_fp)( uint8_t data_length, uint8_t *data );
00037    extern trisos_radio_fp trisos_internal_radio_callback;
00038    // -----------------------------------------------------------------------
00039    void init_trisos_radio( void );
00040    int  trisos_radio_add_receiver( trisos_radio_delegate_t& delegate );
00041    void trisos_radio_del_receiver( int idx );
00042    void trisos_notify_receivers( uint8_t, uint8_t* );
00043    // -----------------------------------------------------------------------
00044    // -----------------------------------------------------------------------
00045    // -----------------------------------------------------------------------
00052    template<typename OsModel_P>
00053    class TriSOSRadio
00054    {
00055    public:
00056       typedef OsModel_P OsModel;
00057 
00058       typedef TriSOSRadio<OsModel> self_type;
00059       typedef self_type* self_pointer_t;
00060 
00061       typedef uint16_t node_id_t;
00062       typedef uint8_t  block_data_t;
00063       typedef uint8_t  size_t;
00064       typedef uint8_t  message_id_t;
00065 
00066       typedef trisos_radio_delegate_t radio_delegate_t;
00067       // --------------------------------------------------------------------
00068       enum ErrorCodes
00069       {
00070          SUCCESS = OsModel::SUCCESS,
00071          ERR_UNSPEC = OsModel::ERR_UNSPEC
00072       };
00073       // --------------------------------------------------------------------
00074       enum SpecialNodeIds {
00075          BROADCAST_ADDRESS = 0xffff, 
00076          NULL_NODE_ID      = 0       
00077       };
00078       // --------------------------------------------------------------------
00079       enum Restrictions {
00080          MAX_MESSAGE_LENGTH = 256 
00081       };
00082       // --------------------------------------------------------------------
00083       void init()
00084       {
00085          init_trisos_radio();
00086       }
00087       // --------------------------------------------------------------------
00088       int send( node_id_t id, size_t len, block_data_t *data );
00089       // --------------------------------------------------------------------
00090       int enable_radio()
00091       {
00092          // TODO: Also HW enable
00093          trisos_internal_radio_callback = trisos_notify_receivers;
00094          return SUCCESS;
00095       }
00096       // --------------------------------------------------------------------
00097       int disable_radio()
00098       {
00099          // TODO: Also HW disable
00100          trisos_internal_radio_callback = 0;
00101          return SUCCESS;
00102       }
00103       // --------------------------------------------------------------------
00104       node_id_t id()
00105       {
00106          uint16_t addr = sys_get_id();
00107          return addr;
00108       }
00109       // --------------------------------------------------------------------
00110       template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)>
00111       int reg_recv_callback( T *obj_pnt );
00112       // --------------------------------------------------------------------
00113       int unreg_recv_callback( int idx )
00114       {
00115          trisos_radio_del_receiver( idx );
00116          return SUCCESS;
00117       }
00118    };
00119    // --------------------------------------------------------------------
00120    // -----------------------------------------------------------------------
00121    // -----------------------------------------------------------------------
00122    template<typename OsModel_P>
00123    int
00124    TriSOSRadio<OsModel_P>::
00125 	send( node_id_t dest, size_t len, block_data_t *data )
00126    {
00127       if( !trisos_internal_radio_callback ) 
00128       {
00129          // Radio enabled?
00130          return ERR_UNSPEC;
00131       }
00132 
00133       uint8_t buf[MAX_MESSAGE_LENGTH];
00134 
00135       // wirte own id and destination in first 4 bytes of buffer
00136       uint16_t addr = id();
00137       write<OsModel, block_data_t, node_id_t>( buf, addr );
00138       write<OsModel, block_data_t, node_id_t>( buf + sizeof(node_id_t), dest );
00139       // write payload
00140       memcpy( buf + 2*sizeof(node_id_t), data, len );
00141 
00142       rf_state_type state = rf_send_data( 2 * sizeof(node_id_t) + len, buf );
00143 
00144       if( state == rf_SUCCESS )
00145       {
00146          return SUCCESS;
00147       }
00148       else
00149       {
00150          return ERR_UNSPEC;
00151       }
00152    }
00153    // -----------------------------------------------------------------------
00154    template<typename OsModel_P>
00155    template<class T,
00156            void (T::*TMethod)( typename TriSOSRadio<OsModel_P>::node_id_t,
00157                                typename TriSOSRadio<OsModel_P>::size_t,
00158                                typename TriSOSRadio<OsModel_P>::block_data_t*)>
00159    int
00160    TriSOSRadio<OsModel_P>::
00161    reg_recv_callback( T *obj_pnt )
00162    {
00163       trisos_radio_delegate_t delegate =
00164          trisos_radio_delegate_t::from_method<T, TMethod>( obj_pnt );
00165       return trisos_radio_add_receiver( delegate );
00166    }
00167 }
00168 
00169 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines