Wiselib
wiselib.testing/algorithms/cluster_radio/cluster_radio_message.h
Go to the documentation of this file.
00001 /*
00002  * File:   cluster_radio_message.h
00003  * Author: Amaxilatis
00004  *
00005  */
00006 
00007 #ifndef CLUSTERRADIOMSG_H
00008 #define  CLUSTERRADIOMSG_H
00009 
00010 namespace wiselib {
00011 
00012     template<typename OsModel_P, typename Radio_P>
00013     class ClusterRadioMsg {
00014     public:
00015         typedef OsModel_P OsModel;
00016         typedef Radio_P Radio;
00017 
00018         typedef typename Radio::node_id_t node_id_t;
00019         typedef typename Radio::size_t size_t;
00020         typedef typename Radio::block_data_t block_data_t;
00021         typedef typename Radio::message_id_t message_id_t;
00022         typedef node_id_t cluster_id_t;
00023 
00024         enum data_positions {
00025             MSG_ID_POS = 0, // message id position inside the message [uint8]
00026             SOURCE_POS = sizeof (message_id_t),
00027             DESTINATION_POS = sizeof (message_id_t) + sizeof (cluster_id_t),
00028             PAYLOAD_SIZE_POS = sizeof (message_id_t) + sizeof (cluster_id_t) + sizeof (cluster_id_t),
00029             PAYLOAD = sizeof (message_id_t) + sizeof (cluster_id_t) + sizeof (cluster_id_t) + sizeof (size_t)
00030         };
00031 
00032         // --------------------------------------------------------------------
00033 
00034         ClusterRadioMsg() {
00035             set_msg_id(CLRADIO_MSG);
00036             set_destination(1);
00037 
00038         }
00039         // --------------------------------------------------------------------
00040 
00041         ~ClusterRadioMsg() {
00042         }
00043 
00044         // get the message id
00045 
00046         inline message_id_t msg_id() {
00047             return read<OsModel, block_data_t, message_id_t > (buffer + MSG_ID_POS);
00048         }
00049 
00050         inline void set_msg_id(message_id_t id) {
00051             write<OsModel, block_data_t, message_id_t > (buffer + MSG_ID_POS, id);
00052         }
00053         // --------------------------------------------------------------------
00054 
00055         inline cluster_id_t source() {
00056             return read<OsModel, block_data_t, node_id_t > (buffer + SOURCE_POS);
00057         }
00058 
00059         inline void set_source(cluster_id_t source) {
00060             write<OsModel, block_data_t, cluster_id_t > (buffer + SOURCE_POS, source);
00061         }
00062         // --------------------------------------------------------------------
00063 
00064         inline cluster_id_t destination() {
00065             return read<OsModel, block_data_t, node_id_t > (buffer + DESTINATION_POS);
00066         }
00067 
00068         inline void set_destination(cluster_id_t destination) {
00069             write<OsModel, block_data_t, cluster_id_t > (buffer + DESTINATION_POS, destination);
00070         }
00071 
00072         // --------------------------------------------------------------------
00073 
00074         void set_payload(uint8_t *data, size_t len) {
00075             write<OsModel, block_data_t, size_t > (buffer + PAYLOAD_SIZE_POS, len);
00076             memcpy(buffer + PAYLOAD, data, len);
00077         }
00078 
00079         void get_payload(uint8_t *data) {
00080             memcpy(data, buffer + PAYLOAD, payload_size());
00081         }
00082 
00083         inline int payload_size() {
00084             return read<OsModel, block_data_t, size_t > (buffer + PAYLOAD_SIZE_POS);
00085         }
00086         // --------------------------------------------------------------------
00087 
00088         inline size_t length() {
00089             return PAYLOAD + payload_size();
00090         }
00091 
00092     private:
00093         block_data_t buffer[Radio::MAX_MESSAGE_LENGTH]; // buffer for the message data
00094     };
00095 }
00096 #endif   /* CLUSTERRADIOMSG_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines