Wiselib
wiselib.testing/algorithms/gke/keylevels_message.h
Go to the documentation of this file.
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 KEYLEVELS_MESSAGE_H
00020 #define KEYLEVELS_MESSAGE_H
00021 
00022 #include "util/serialization/simple_types.h"
00023 
00024 namespace wiselib {
00025 enum KeylevelsMessageTypes {
00026    KEY_ACK = 90, GROUP_KEY, NEIGHBORHOOD_SEEK, NEIGHBORHOOD_ACK, TTL_MESSAGE
00027 };
00028 
00029 template<typename OsModel_P, typename Radio_P = typename OsModel_P::Radio>
00030 class KeylevelsMessage {
00031 public:
00032    typedef OsModel_P OsModel;
00033    typedef Radio_P Radio;
00034    typedef typename Radio::block_data_t block_data_t;
00035    typedef typename Radio::size_t size_t;
00036    typedef typename Radio::node_id_t node_id_t;
00037    typedef typename Radio::message_id_t message_id_t;
00038    // --------------------------------------------------------------------
00039    inline message_id_t message_type() {
00040       return read<OsModel, block_data_t, message_id_t> (buffer);
00041    }
00042    // --------------------------------------------------------------------
00043    inline void set_message_type(message_id_t type) {
00044       write<OsModel, block_data_t, message_id_t> (buffer, type);
00045    }
00046    // --------------------------------------------------------------------
00047    inline node_id_t destination() {
00048       return read<OsModel, block_data_t, node_id_t> (buffer + DESTINATION);
00049    }
00050    // --------------------------------------------------------------------
00051    inline void set_destination(node_id_t dst) {
00052       write<OsModel, block_data_t, node_id_t> (buffer + DESTINATION, dst);
00053    }
00054    // --------------------------------------------------------------------
00055    inline node_id_t source() {
00056       return read<OsModel, block_data_t, node_id_t> (buffer + SOURCE);
00057    }
00058    // --------------------------------------------------------------------
00059    inline void set_source(node_id_t src) {
00060       write<OsModel, block_data_t, node_id_t> (buffer + SOURCE, src);
00061    }
00062    // --------------------------------------------------------------------
00063    inline node_id_t get_cluster() {
00064       return read<OsModel, block_data_t, node_id_t> (buffer + CLUSTER_POS);
00065    }
00066    //---------------------------------------------------------------------
00067    inline void set_cluster(node_id_t clstr) {
00068       write<OsModel, block_data_t, node_id_t> (buffer + CLUSTER_POS, clstr);
00069    }
00070    // --------------------------------------------------------------------
00071    inline uint8_t ttl() {
00072       return read<OsModel, block_data_t, uint8_t> (buffer + TTL);
00073    }
00074    // --------------------------------------------------------------------
00075    inline void set_ttl(uint8_t ttl) {
00076       write<OsModel, block_data_t, uint8_t> (buffer + TTL, ttl);
00077    }
00078    //---------------------------------------------------------------------
00079    inline node_id_t message_id() {
00080       return read<OsModel, block_data_t, node_id_t> (buffer + MESSAGE_ID);
00081    }
00082    // --------------------------------------------------------------------
00083    inline void set_message_id(node_id_t message_id) {
00084       write<OsModel, block_data_t, node_id_t> (buffer + MESSAGE_ID,
00085             message_id);
00086    }
00087    // --------------------------------------------------------------------
00088    inline size_t payload_length() {
00089       return read<OsModel, block_data_t, size_t> (buffer + PAYLOAD_SIZE);
00090    }
00091    //---------------------------------------------------------------------
00092    inline block_data_t* payload() {
00093       return buffer + PAYLOAD_POS;
00094    }
00095    // --------------------------------------------------------------------
00096    inline void set_payload(size_t len, block_data_t *buf) {
00097       set_payload_length(len);
00098       memcpy(buffer + PAYLOAD_POS, buf, len);
00099    }
00100    // --------------------------------------------------------------------
00101    inline size_t buffer_size() {
00102       return sizeof(message_id_t) + sizeof(node_id_t) + sizeof(node_id_t)
00103             + sizeof(node_id_t) + sizeof(uint8_t) + sizeof(node_id_t)
00104             + sizeof(size_t) + payload_length();
00105    }
00106 
00107 private:
00108 
00109    inline void set_payload_length(size_t len) {
00110       write<OsModel, block_data_t, size_t> (buffer + PAYLOAD_SIZE, len);
00111    }
00112    // --------------------------------------------------------------------
00113 
00114    enum msgdata_positions {
00115       COMMAND_TYPE = 0,
00116       DESTINATION = sizeof(message_id_t),
00117       SOURCE = DESTINATION + sizeof(node_id_t),
00118       CLUSTER_POS = SOURCE + sizeof(node_id_t),
00119       TTL = CLUSTER_POS + sizeof(node_id_t),
00120       MESSAGE_ID = TTL + sizeof(uint8_t),
00121       PAYLOAD_SIZE = MESSAGE_ID + sizeof(node_id_t),
00122       PAYLOAD_POS = PAYLOAD_SIZE + sizeof(node_id_t)
00123    };
00124    // --------------------------------------------------------------------
00125    block_data_t buffer[Radio_P::MAX_MESSAGE_LENGTH];
00126 };
00127 
00128 }
00129 
00130 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines