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 __ALGORITHMS_ROUTING_TORA_ROUTING_MSG_H__ 00020 #define __ALGORITHMS_ROUTING_TORA_ROUTING_MSG_H__ 00021 00022 #include "util/serialization/simple_types.h" 00023 00024 namespace wiselib { 00025 typedef struct { 00026 int16_t time; 00027 int16_t originator_id; 00028 int16_t r; 00029 int16_t delta; 00030 int16_t ID; 00031 } height; 00032 template<typename OsModel_P, 00033 typename Radio_P> 00034 class ToraRoutingMessage { 00035 public: 00036 typedef OsModel_P OsModel; 00037 typedef Radio_P Radio; 00038 typedef typename Radio::block_data_t block_data_t; 00039 00040 00041 // -------------------------------------------------------------------- 00042 inline ToraRoutingMessage(); 00043 // -------------------------------------------------------------------- 00044 00045 inline uint8_t msg_id() { 00046 return read<OsModel, block_data_t, uint8_t > (buffer); 00047 }; 00048 // -------------------------------------------------------------------- 00049 00050 inline void set_msg_id(uint8_t id) { 00051 write<OsModel, block_data_t, uint8_t > (buffer, id); 00052 } 00053 // -------------------------------------------------------------------- 00054 00055 inline uint16_t destination() { 00056 return read<OsModel, block_data_t, uint16_t > (buffer + DEST_POS); 00057 } 00058 // -------------------------------------------------------------------- 00059 00060 inline void set_destination(uint16_t dest) { 00061 write<OsModel, block_data_t, uint16_t > (buffer + DEST_POS, dest); 00062 } 00063 // -------------------------------------------------------------------- 00064 00065 inline uint16_t next_node() { 00066 return read<OsModel, block_data_t, uint16_t > (buffer + NEXT_NOD_POS); 00067 } 00068 // -------------------------------------------------------------------- 00069 00070 inline void set_next_node(uint16_t next_node) { 00071 write<OsModel, block_data_t, uint16_t > (buffer + NEXT_NOD_POS, next_node); 00072 } 00073 // -------------------------------------------------------------------- 00074 00075 inline height get_height() { 00076 height tmp; 00077 tmp.time = read<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS); 00078 tmp.originator_id = read<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 2); 00079 tmp.r = read<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 4); 00080 tmp.delta = read<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 6); 00081 tmp.ID = read<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 8); 00082 return tmp; 00083 } 00084 // -------------------------------------------------------------------- 00085 00086 inline void set_height(height H) { 00087 write<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS, H.time); 00088 write<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 2, H.originator_id); 00089 write<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 4, H.r); 00090 write<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 6, H.delta); 00091 write<OsModel, block_data_t, int16_t > (buffer + HEIGHT_POS + 8, H.ID); 00092 } 00093 // -------------------------------------------------------------------- 00094 00095 inline uint8_t payload_size() { 00096 return read<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS); 00097 } 00098 // -------------------------------------------------------------------- 00099 00100 inline uint8_t* payload() { 00101 return buffer + PAYLOAD_POS + 1; 00102 } 00103 // -------------------------------------------------------------------- 00104 00105 inline void set_payload(uint8_t len, uint8_t *buf) { 00106 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00107 memcpy(buffer + PAYLOAD_POS + 1, buf, len); 00108 } 00109 // -------------------------------------------------------------------- 00110 00111 inline size_t buffer_size() { 00112 return PAYLOAD_POS + 1 + payload_size(); 00113 } 00114 00115 enum data_positions { 00116 MSG_ID_POS = 0, 00117 NEXT_NOD_POS = 2, 00118 DEST_POS = 4, 00119 HEIGHT_POS = 6, 00120 PAYLOAD_POS = 16 00121 }; 00122 00123 private: 00124 00125 inline void set_payload_size(uint8_t len) { 00126 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00127 } 00128 00129 00130 uint8_t buffer[Radio::MAX_MESSAGE_LENGTH]; 00131 }; 00132 // ----------------------------------------------------------------------- 00133 00134 template<typename OsModel_P, 00135 typename Radio_P> 00136 ToraRoutingMessage<OsModel_P, Radio_P>:: 00137 ToraRoutingMessage() { 00138 set_msg_id(0); 00139 set_destination(Radio::NULL_NODE_ID); 00140 set_payload_size(0); 00141 } 00142 00143 } 00144 #endif