Wiselib
|
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_LORIEN_RADIOMODEL_H 00020 #define CONNECTOR_LORIEN_RADIOMODEL_H 00021 00022 #include "external_interface/lorien/lorien_types.h" 00023 #include "util/delegates/delegate.hpp" 00024 #include "util/pstl/vector_static.h" 00025 #include <string.h> 00026 extern "C" { 00027 #include "lorien.h" 00028 #include "interfaces/networking/imx_radio.h" 00029 #include "interfaces/hardware/ihw_control.h" 00030 } 00031 00032 namespace wiselib 00033 { 00034 00035 typedef delegate3<void, TNodeID_Int, uint8_t, uint8_t*> lorien_radio_delegate_t; 00036 00045 template<typename OsModel_P> 00046 class LorienRadioModel 00047 { 00048 public: 00049 typedef OsModel_P OsModel; 00050 00051 typedef LorienRadioModel<OsModel> self_type; 00052 typedef self_type* self_pointer_t; 00053 00054 typedef TNodeID_Int node_id_t; 00055 typedef uint8_t block_data_t; 00056 typedef uint8_t size_t; 00057 typedef uint8_t message_id_t; 00058 00059 typedef lorien_radio_delegate_t radio_delegate_t; 00060 00061 typedef vector_static<OsModel, radio_delegate_t, 10> CallbackVector; 00062 typedef typename CallbackVector::iterator CallbackVectorIterator; 00063 // -------------------------------------------------------------------- 00064 enum ErrorCodes 00065 { 00066 SUCCESS = OsModel::SUCCESS, 00067 ERR_UNSPEC = OsModel::ERR_UNSPEC, 00068 ERR_NOTIMPL = OsModel::ERR_NOTIMPL 00069 }; 00070 // -------------------------------------------------------------------- 00071 enum SpecialNodeIds { 00072 BROADCAST_ADDRESS = BROADCAST_INT, 00073 NULL_NODE_ID = -1 00074 }; 00075 // -------------------------------------------------------------------- 00076 enum Restrictions { 00077 MAX_MESSAGE_LENGTH = 100 00078 }; 00079 // ----------------------------------------------------------------- 00080 void init( Component *comp ) 00081 { 00082 comp_ = comp; 00083 //if ( callbacks_.empty() ) 00084 //callbacks_.assign( 10, radio_delegate_t() ); 00085 } 00086 // -------------------------------------------------------------------- 00087 int send( node_id_t id, size_t len, block_data_t *data ) 00088 { 00089 ((IMXRadio*) ((LXState*) comp_ -> state) -> radio -> userRecp) -> sendTo(((LXState*) comp_ -> state) -> radio -> ifHostComponent, data, len, id, WISELIB_PORT); 00090 return SUCCESS; 00091 } 00092 // -------------------------------------------------------------------- 00093 int enable_radio() 00094 { 00095 ((IHWControl*) ((LXState*) comp_ -> state) -> hardwareControl -> userRecp) -> enable(((LXState*) comp_ -> state) -> hardwareControl -> ifHostComponent); 00096 return SUCCESS; 00097 } 00098 // -------------------------------------------------------------------- 00099 int disable_radio() 00100 { 00101 ((IHWControl*) ((LXState*) comp_ -> state) -> hardwareControl -> userRecp) -> disable(((LXState*) comp_ -> state) -> hardwareControl -> ifHostComponent); 00102 return SUCCESS; 00103 } 00104 // -------------------------------------------------------------------- 00105 node_id_t id() 00106 { 00107 return ((IMXRadio*) ((LXState*) comp_ -> state) -> radio -> userRecp) -> getID(((LXState*) comp_ -> state) -> radio -> ifHostComponent); 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 /*for ( unsigned int i = 0; i < callbacks_.size(); ++i ) 00114 { 00115 if ( callbacks_.at(i) == radio_delegate_t() ) 00116 { 00117 callbacks_.at(i) = radio_delegate_t::template from_method<T, TMethod>( obj_pnt ); 00118 return i; 00119 } 00120 }*/ 00121 00122 return -1; 00123 } 00124 // -------------------------------------------------------------------- 00125 int unreg_recv_callback(int idx ) 00126 { 00127 //callbacks_.at(idx) = radio_delegate_t(); 00128 return SUCCESS; 00129 } 00130 // -------------------------------------------------------------------- 00131 static void received( unsigned char *data, size_t len, unsigned int from ) 00132 { 00133 /*for ( CallbackVectorIterator 00134 it = callbacks_.begin(); 00135 it != callbacks_.end(); 00136 ++it ) 00137 { 00138 if ( *it != radio_delegate_t() ) 00139 (*it)( from, len, data ); 00140 }*/ 00141 } 00142 00143 private: 00144 Component *comp_; 00145 static CallbackVector callbacks_; 00146 }; 00147 // ----------------------------------------------------------------------- 00148 // ----------------------------------------------------------------------- 00149 // ----------------------------------------------------------------------- 00150 /*template<typename OsModel_P> 00151 typename LorienRadioModel<OsModel_P>::CallbackVector 00152 LorienRadioModel<OsModel_P>::callbacks_ = 00153 typename LorienRadioModel<OsModel_P>::CallbackVector();*/ 00154 } 00155 00156 #endif