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_SCW_RADIOMODEL_H 00020 #define CONNECTOR_SCW_RADIOMODEL_H 00021 00022 #include "external_interface/scw/scw_types.h" 00023 #include "util/delegates/delegate.hpp" 00024 extern "C" { 00025 #include <ScatterWeb.System.h> 00026 #include <ScatterWeb.Net.h> 00027 } 00028 00029 namespace wiselib 00030 { 00031 typedef delegate3<void, netaddr_t, uint8_t, uint8_t*> scw_radio_delegate_t; 00032 // -------------------------------------------------------------------------- 00033 void scw_radio_init( void ); 00034 void scw_radio_add_receiver( scw_radio_delegate_t receiver ); 00035 void scw_radio_del_receiver( scw_radio_delegate_t receiver ); 00036 // -------------------------------------------------------------------------- 00037 // -------------------------------------------------------------------------- 00038 // -------------------------------------------------------------------------- 00048 template<typename OsModel_P> 00049 class ScwRadioModel 00050 { 00051 public: 00052 typedef OsModel_P OsModel; 00053 00054 typedef ScwRadioModel<OsModel> self_type; 00055 typedef self_type* self_pointer_t; 00056 00057 typedef netaddr_t node_id_t; 00058 typedef uint8_t block_data_t; 00059 typedef uint8_t size_t; 00060 typedef uint8_t message_id_t; 00061 00062 typedef scw_radio_delegate_t radio_delegate_t; 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 = 0xff, 00073 NULL_NODE_ID = 0 00074 }; 00075 // -------------------------------------------------------------------- 00076 enum Restrictions { 00077 MAX_MESSAGE_LENGTH = 100 00078 }; 00079 // -------------------------------------------------------------------- 00080 int send( node_id_t id, size_t len, block_data_t *data ); 00081 // -------------------------------------------------------------------- 00082 int enable_radio() 00083 { 00084 Net_on(); 00085 return SUCCESS; 00086 } 00087 // -------------------------------------------------------------------- 00088 int disable_radio() 00089 { 00090 Net_off(); 00091 return SUCCESS; 00092 } 00093 // -------------------------------------------------------------------- 00094 node_id_t id() 00095 { 00096 return Configuration.id; 00097 } 00098 // -------------------------------------------------------------------- 00099 template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)> 00100 int reg_recv_callback( T *obj_pnt ) 00101 { 00102 scw_radio_delegate_t delegate = 00103 scw_radio_delegate_t::from_method<T, TMethod>( obj_pnt ); 00104 scw_radio_add_receiver( delegate ); 00105 00106 return 0; 00107 } 00108 // -------------------------------------------------------------------- 00111 int unreg_recv_callback( int idx ) 00112 { 00113 #warning ScwRadio TODO: Implement unreg_recv_callback! 00114 return ERR_NOTIMPL; 00115 } 00116 }; 00117 // -------------------------------------------------------------------- 00118 template<typename OsModel_P> 00119 int 00120 ScwRadioModel<OsModel_P>:: 00121 send( node_id_t id, size_t len, block_data_t *data ) 00122 { 00123 netpacket_send_args_t npsargs; 00124 // fill structure with default values 00125 Net_sendArgsInit( &npsargs ); 00126 00127 // fill network header custom values 00128 npsargs.netheader.to = BROADCAST_ADDRESS; // receiver's address 00129 npsargs.netheader.type = USERDEFINED_PACKET; // application protocol 00130 npsargs.netheader.flags = 0; // network layer config flags (CRC 00131 00132 netpacket_send_buffer buf; 00133 buf.size = len; 00134 buf.buffer = data; 00135 npsargs.payload[0] = buf; 00136 00137 // queue packet for sending 00138 Net_send( &npsargs ); 00139 00140 return SUCCESS; 00141 } 00142 } 00143 00144 #endif