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_CONTIKI_RADIOMODEL_H 00020 #define CONNECTOR_CONTIKI_RADIOMODEL_H 00021 00022 #include "external_interface/contiki/contiki_types.h" 00023 #include "util/delegates/delegate.hpp" 00024 #include "util/serialization/simple_types.h" 00025 #include <string.h> 00026 extern "C" { 00027 #include "contiki.h" 00028 #include "net/rime.h" 00029 } 00030 00031 namespace wiselib 00032 { 00033 00034 typedef delegate3<void, uint16_t, uint8_t, uint8_t*> contiki_radio_delegate_t; 00035 // ----------------------------------------------------------------------- 00036 typedef void (*contiki_radio_fp)( struct abc_conn *c ); 00037 extern contiki_radio_fp contiki_internal_radio_callback; 00038 extern abc_conn contiki_radio_conn; 00039 // ----------------------------------------------------------------------- 00040 void init_contiki_radio( void ); 00041 int contiki_radio_add_receiver( contiki_radio_delegate_t& delegate ); 00042 void contiki_radio_del_receiver( int idx ); 00043 void contiki_notify_receivers( struct abc_conn *c ); 00044 // ----------------------------------------------------------------------- 00045 // ----------------------------------------------------------------------- 00046 // ----------------------------------------------------------------------- 00055 template<typename OsModel_P> 00056 class ContikiRadio 00057 { 00058 public: 00059 typedef OsModel_P OsModel; 00060 00061 typedef ContikiRadio<OsModel> self_type; 00062 typedef self_type* self_pointer_t; 00063 00065 typedef uint16_t node_id_t; 00066 typedef uint8_t block_data_t; 00067 typedef uint8_t size_t; 00068 typedef uint8_t message_id_t; 00069 00070 typedef contiki_radio_delegate_t radio_delegate_t; 00071 // -------------------------------------------------------------------- 00072 enum ErrorCodes 00073 { 00074 SUCCESS = OsModel::SUCCESS, 00075 ERR_UNSPEC = OsModel::ERR_UNSPEC 00076 }; 00077 // -------------------------------------------------------------------- 00078 enum SpecialNodeIds { 00079 BROADCAST_ADDRESS = 0xffff, 00080 NULL_NODE_ID = 0 00081 }; 00082 // -------------------------------------------------------------------- 00083 enum Restrictions { 00084 MAX_MESSAGE_LENGTH = PACKETBUF_SIZE 00085 }; 00086 // -------------------------------------------------------------------- 00087 void init() 00088 { 00089 init_contiki_radio(); 00090 } 00091 // -------------------------------------------------------------------- 00092 int send( node_id_t id, size_t len, block_data_t *data ); 00093 // -------------------------------------------------------------------- 00094 int enable_radio() 00095 { 00096 contiki_internal_radio_callback = contiki_notify_receivers; 00097 return SUCCESS; 00098 } 00099 // -------------------------------------------------------------------- 00100 int disable_radio() 00101 { 00102 contiki_internal_radio_callback = 0; 00103 return SUCCESS; 00104 } 00105 // -------------------------------------------------------------------- 00106 node_id_t id() 00107 { 00108 uint16_t addr = rimeaddr_node_addr.u8[0] | 00109 (rimeaddr_node_addr.u8[1] << 8); 00110 return addr; 00111 } 00112 // -------------------------------------------------------------------- 00113 template<class T, void (T::*TMethod)(node_id_t, size_t, block_data_t*)> 00114 int reg_recv_callback( T *obj_pnt ); 00115 // -------------------------------------------------------------------- 00116 int unreg_recv_callback( int idx ) 00117 { 00118 contiki_radio_del_receiver( idx ); 00119 return SUCCESS; 00120 } 00121 }; 00122 // -------------------------------------------------------------------- 00123 // ----------------------------------------------------------------------- 00124 // ----------------------------------------------------------------------- 00125 template<typename OsModel_P> 00126 int 00127 ContikiRadio<OsModel_P>:: 00128 send( node_id_t dest, size_t len, block_data_t *data ) 00129 { 00130 uint8_t buf[PACKETBUF_SIZE]; 00131 00132 // wirte own id and destination in first 4 bytes of buffer 00133 uint16_t addr = id(); 00134 write<OsModel, block_data_t, node_id_t>( buf, addr ); 00135 write<OsModel, block_data_t, node_id_t>( buf + sizeof(node_id_t), dest ); 00136 // write payload 00137 memcpy( buf + 2*sizeof(node_id_t), data, len ); 00138 00139 packetbuf_clear(); 00140 packetbuf_copyfrom( buf, len + 2*sizeof(node_id_t) ); 00141 abc_send( &contiki_radio_conn ); 00142 return SUCCESS; 00143 } 00144 // ----------------------------------------------------------------------- 00145 template<typename OsModel_P> 00146 template<class T, 00147 void (T::*TMethod)( typename ContikiRadio<OsModel_P>::node_id_t, 00148 typename ContikiRadio<OsModel_P>::size_t, 00149 typename ContikiRadio<OsModel_P>::block_data_t*)> 00150 int 00151 ContikiRadio<OsModel_P>:: 00152 reg_recv_callback( T *obj_pnt ) 00153 { 00154 contiki_radio_delegate_t delegate = 00155 contiki_radio_delegate_t::from_method<T, TMethod>( obj_pnt ); 00156 return contiki_radio_add_receiver( delegate ); 00157 } 00158 } 00159 00160 #endif