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_SHAWN_TXRADIOMODEL_H 00020 #define CONNECTOR_SHAWN_TXRADIOMODEL_H 00021 00022 #include "external_interface/shawn/shawn_types.h" 00023 #include <cmath> 00024 00025 namespace wiselib 00026 { 00032 template<typename OsModel_P> 00033 class ShawnTxRadioModel 00034 { 00035 public: 00036 typedef OsModel_P OsModel; 00037 00038 typedef ShawnTxRadioModel<OsModel> self_type; 00039 typedef self_type* self_pointer_t; 00040 00041 typedef ExtIfaceProcessor::node_id_t node_id_t; 00042 typedef ExtIfaceProcessor::block_data_t block_data_t; 00043 typedef ExtIfaceProcessor::size_t size_t; 00044 typedef ExtIfaceProcessor::message_id_t message_id_t; 00045 typedef ExtIfaceProcessor::ExtendedData ExtendedData; 00046 00047 typedef delegate3<void, int, long, unsigned char*> radio_delegate_t; 00048 // -------------------------------------------------------------------- 00049 enum ErrorCodes 00050 { 00051 SUCCESS = OsModel::SUCCESS, 00052 ERR_UNSPEC = OsModel::ERR_UNSPEC, 00053 ERR_NOTIMPL = OsModel::ERR_NOTIMPL 00054 }; 00055 // -------------------------------------------------------------------- 00056 enum SpecialNodeIds { 00057 BROADCAST_ADDRESS = 0xffff, 00058 NULL_NODE_ID = -1 00059 }; 00060 // -------------------------------------------------------------------- 00061 enum Restrictions { 00062 MAX_MESSAGE_LENGTH = 0xff 00063 }; 00064 // -------------------------------------------------------------------- 00065 class TxPower; 00066 // -------------------------------------------------------------------- 00067 ShawnTxRadioModel( ShawnOs& os ) 00068 : os_(os) 00069 {} 00070 // -------------------------------------------------------------------- 00071 int send( node_id_t id, size_t len, block_data_t *data ) 00072 { 00073 os().proc->send_wiselib_message( id, len, data ); 00074 return SUCCESS; 00075 }; 00076 // -------------------------------------------------------------------- 00077 int enable_radio() 00078 { return SUCCESS; } 00079 // -------------------------------------------------------------------- 00080 int disable_radio() 00081 { return SUCCESS; } 00082 // -------------------------------------------------------------------- 00083 node_id_t id() 00084 { 00085 return os().proc->id(); 00086 } 00087 // -------------------------------------------------------------------- 00088 //---------- From concept VariablePowerRadio ------------ 00089 int set_power(TxPower p); 00090 // -------------------------------------------------------------------- 00091 TxPower power(); 00092 //------------------------------------------------------- 00093 template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)> 00094 int reg_recv_callback( T *obj_pnt ) 00095 { 00096 return os().proc->template reg_recv_callback<T, TMethod>( obj_pnt ); 00097 } 00098 //------------------------------------------------------- 00099 template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*, ExtendedData const &)> 00100 int reg_recv_callback( T *obj_pnt ) 00101 { 00102 return os().proc->template reg_recv_callback<T, TMethod>( obj_pnt ); 00103 } 00104 // -------------------------------------------------------------------- 00105 int unreg_recv_callback( int idx ) 00106 { return ERR_NOTIMPL; } 00107 00108 private: 00109 ShawnOs& os() 00110 { return os_; } 00111 // -------------------------------------------------------------------- 00112 ShawnOs& os_; 00113 }; 00114 // -------------------------------------------------------------------- 00120 template<typename OsModel_P> 00121 class ShawnTxRadioModel<OsModel_P>::TxPower { 00122 public: 00123 TxPower(); 00124 00125 bool operator==(TxPower) const; 00126 bool operator!=(TxPower) const; 00127 bool operator<=(TxPower) const; 00128 bool operator>=(TxPower) const; 00129 bool operator<(TxPower) const; 00130 bool operator>(TxPower) const; 00131 TxPower operator++(); 00132 TxPower operator++(int); 00133 TxPower operator--(); 00134 TxPower operator--(int); 00135 00136 static TxPower from_ratio(int); 00137 void set_ratio(int); 00138 int to_ratio() const; 00139 static TxPower from_dB(int); 00140 void set_dB(int); 00141 int to_dB() const; 00142 00143 void set_steps(int); 00144 int steps() const; 00145 static void set_default_steps(int); 00146 static int default_steps(); 00147 00148 static TxPower const MIN; 00149 static TxPower const MAX; 00150 00151 private: 00152 TxPower(double); 00153 00154 double value; 00155 int steps_; 00156 static int def_steps; 00157 00158 friend class ShawnTxRadioModel<OsModel_P>; 00159 }; 00160 //------------------------------------------------------------------------ 00161 template<typename OsModel_P> 00162 typename ShawnTxRadioModel<OsModel_P>::TxPower const ShawnTxRadioModel<OsModel_P>::TxPower::MIN=0.0; 00163 //------------------------------------------------------------------------ 00164 template<typename OsModel_P> 00165 typename ShawnTxRadioModel<OsModel_P>::TxPower const ShawnTxRadioModel<OsModel_P>::TxPower::MAX=1.0; 00166 //------------------------------------------------------------------------ 00167 template<typename OsModel_P> 00168 int ShawnTxRadioModel<OsModel_P>::TxPower::def_steps=10; 00169 //------------------------------------------------------------------------ 00170 template<typename OsModel_P> 00171 inline ShawnTxRadioModel<OsModel_P>::TxPower::TxPower(double v): 00172 value(v), 00173 steps_(def_steps){} 00174 //------------------------------------------------------------------------ 00175 template<typename OsModel_P> 00176 inline ShawnTxRadioModel<OsModel_P>::TxPower::TxPower(): 00177 value(1.0), 00178 steps_(def_steps){} 00179 //------------------------------------------------------------------------ 00180 template<typename OsModel_P> 00181 inline bool ShawnTxRadioModel<OsModel_P>::TxPower::operator==(TxPower p) const { 00182 return value==p.value; 00183 } 00184 //------------------------------------------------------------------------ 00185 template<typename OsModel_P> 00186 inline bool ShawnTxRadioModel<OsModel_P>::TxPower::operator!=(TxPower p) const { 00187 return value!=p.value; 00188 } 00189 //------------------------------------------------------------------------ 00190 template<typename OsModel_P> 00191 inline bool ShawnTxRadioModel<OsModel_P>::TxPower::operator<=(TxPower p) const { 00192 return value<=p.value; 00193 } 00194 //------------------------------------------------------------------------ 00195 template<typename OsModel_P> 00196 inline bool ShawnTxRadioModel<OsModel_P>::TxPower::operator>=(TxPower p) const { 00197 return value>=p.value; 00198 } 00199 //------------------------------------------------------------------------ 00200 template<typename OsModel_P> 00201 inline bool ShawnTxRadioModel<OsModel_P>::TxPower::operator<(TxPower p) const { 00202 return value<p.value; 00203 } 00204 //------------------------------------------------------------------------ 00205 template<typename OsModel_P> 00206 inline bool ShawnTxRadioModel<OsModel_P>::TxPower::operator>(TxPower p) const { 00207 return value>p.value; 00208 } 00209 //------------------------------------------------------------------------ 00210 template<typename OsModel_P> 00211 typename ShawnTxRadioModel<OsModel_P>::TxPower ShawnTxRadioModel<OsModel_P>::TxPower::operator++(){ 00212 value=static_cast<int>(value*steps_+0.5)+1.0/steps_; 00213 return *this; 00214 } 00215 //------------------------------------------------------------------------ 00216 template<typename OsModel_P> 00217 typename ShawnTxRadioModel<OsModel_P>::TxPower ShawnTxRadioModel<OsModel_P>::TxPower::operator++(int){ 00218 TxPower p=*this; 00219 value=static_cast<int>(value*steps_+0.5)+1.0/steps_; 00220 return p; 00221 } 00222 //------------------------------------------------------------------------ 00223 template<typename OsModel_P> 00224 typename ShawnTxRadioModel<OsModel_P>::TxPower ShawnTxRadioModel<OsModel_P>::TxPower::operator--(){ 00225 value=static_cast<int>(value*steps_+0.5)-1.0/steps_; 00226 return *this; 00227 } 00228 //------------------------------------------------------------------------ 00229 template<typename OsModel_P> 00230 typename ShawnTxRadioModel<OsModel_P>::TxPower ShawnTxRadioModel<OsModel_P>::TxPower::operator--(int){ 00231 TxPower p=*this; 00232 value=static_cast<int>(value*steps_+0.5)-1.0/steps_; 00233 return p; 00234 } 00235 //------------------------------------------------------------------------ 00236 template<typename OsModel_P> 00237 typename ShawnTxRadioModel<OsModel_P>::TxPower ShawnTxRadioModel<OsModel_P>::TxPower::from_ratio(int ratio){ 00238 return TxPower(ratio/1e3); 00239 } 00240 //------------------------------------------------------------------------ 00241 template<typename OsModel_P> 00242 void ShawnTxRadioModel<OsModel_P>::TxPower::set_ratio(int ratio){ 00243 if(ratio>=0&&ratio<=1000) 00244 value=ratio/1e3; 00245 } 00246 //------------------------------------------------------------------------ 00247 template<typename OsModel_P> 00248 int ShawnTxRadioModel<OsModel_P>::TxPower::to_ratio() const { 00249 return value*1e3+.5; 00250 } 00251 //------------------------------------------------------------------------ 00252 template<typename OsModel_P> 00253 typename ShawnTxRadioModel<OsModel_P>::TxPower ShawnTxRadioModel<OsModel_P>::TxPower::from_dB(int db){ 00254 return TxPower(std::pow(10.0,db/10.0)); 00255 } 00256 //------------------------------------------------------------------------ 00257 template<typename OsModel_P> 00258 void ShawnTxRadioModel<OsModel_P>::TxPower::set_dB(int db){ 00259 if(db<=0) 00260 value=std::pow(10.0,db/10.0); 00261 } 00262 //------------------------------------------------------------------------ 00263 template<typename OsModel_P> 00264 inline int ShawnTxRadioModel<OsModel_P>::TxPower::to_dB() const { 00265 return std::log10(value)*10.0+.5; 00266 } 00267 //------------------------------------------------------------------------ 00268 template<typename OsModel_P> 00269 void ShawnTxRadioModel<OsModel_P>::TxPower::set_steps(int n){ 00270 if(n>0) 00271 steps_=n; 00272 } 00273 //------------------------------------------------------------------------ 00274 template<typename OsModel_P> 00275 int ShawnTxRadioModel<OsModel_P>::TxPower::steps() const { 00276 return steps_; 00277 } 00278 //------------------------------------------------------------------------ 00279 template<typename OsModel_P> 00280 void ShawnTxRadioModel<OsModel_P>::TxPower::set_default_steps(int n){ 00281 if(n>0) 00282 def_steps=n; 00283 } 00284 //------------------------------------------------------------------------ 00285 template<typename OsModel_P> 00286 int ShawnTxRadioModel<OsModel_P>::TxPower::default_steps() { 00287 return def_steps; 00288 } 00289 //------------------------------------------------------------------------ 00290 template<typename OsModel_P> 00291 int ShawnTxRadioModel<OsModel_P>::set_power(TxPower p) 00292 { 00293 os().proc->owner_w().set_transmission_range(p.value); 00294 return SUCCESS; 00295 } 00296 //------------------------------------------------------------------------ 00297 template<typename OsModel_P> 00298 typename ShawnTxRadioModel<OsModel_P>::TxPower ShawnTxRadioModel<OsModel_P>::power() { 00299 return TxPower(os().proc->owner_w().transmission_range()); 00300 } 00301 00302 } 00303 00304 #endif