Wiselib
|
00001 /* 00002 * File: echomsg.h 00003 * Author: Koninis, Amaxilatis 00004 * 00005 * Created on August 27, 2010, 1:16 PM 00006 */ 00007 00008 #ifndef ECHOMSG_H 00009 #define ECHOMSG_H 00010 00011 #include "pgb_payloads_ids.h" 00012 00013 namespace wiselib { 00014 00015 template 00016 < typename OsModel_P, typename Radio_P> 00017 class EchoMsg { 00018 public: 00019 typedef OsModel_P OsModel; 00020 typedef Radio_P Radio; 00021 00022 typedef typename Radio::node_id_t node_id_t; 00023 typedef typename Radio::size_t size_t; 00024 typedef typename Radio::block_data_t block_data_t; 00025 typedef typename Radio::message_id_t message_id_t; 00026 // message ids 00027 00028 enum { 00029 HELLO_MESSAGE = 42 00030 }; 00031 00032 enum data_positions { 00033 MSG_ID_POS = 0, // message id position inside the message [uint8] 00034 NBS_NUM = 1, 00035 PG_NUM = 2, 00036 PAYLOAD_POS = 3 // position of the payload length 00037 // (the payload starts at +1) 00038 }; 00039 00040 // -------------------------------------------------------------------- 00041 00042 EchoMsg() { 00043 set_msg_id(HELLO_MESSAGE); 00044 set_nearby_list_size(0); 00045 set_payload(0,0); 00046 }; 00047 // -------------------------------------------------------------------- 00048 00049 ~EchoMsg() { 00050 }; 00051 00052 // get the message id 00053 00054 inline message_id_t msg_id() { 00055 return read<OsModel, block_data_t, uint8_t > (buffer + MSG_ID_POS); 00056 }; 00057 // -------------------------------------------------------------------- 00058 00059 // set the message id 00060 00061 inline void set_msg_id(message_id_t id) { 00062 write<OsModel, block_data_t, uint8_t > (buffer + MSG_ID_POS, id); 00063 }; 00064 00065 inline uint8_t * payload() { 00066 return buffer + PAYLOAD_POS + 1 ; 00067 }; 00068 00069 inline uint8_t payload_size() { 00070 return read<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS); 00071 } 00072 00073 inline uint8_t buffer_size() { 00074 return PAYLOAD_POS + 1 + payload_size(); 00075 } 00076 00077 inline block_data_t* data() { 00078 return buffer; 00079 } 00080 00081 // set the message contents 00082 void set_payload(uint8_t len, uint8_t * buf) { 00083 00084 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00085 buffer[PG_NUM]= 0; 00086 // write<OsModel, block_data_t, uint8_t > (buffer + PG_NUM, (uint8_t)0); 00087 if ( buf != 0) 00088 memcpy(buffer + PAYLOAD_POS + 1 , buf, len); 00089 }; 00090 00091 void add_nb_entry(node_id_t nb_id) { 00092 // memcpy(buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, &nb_id, sizeof(node_id_t)); 00093 write<OsModel, block_data_t, node_id_t > 00094 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, nb_id); 00095 00096 buffer[NBS_NUM] += sizeof(node_id_t); 00097 buffer[PAYLOAD_POS] += sizeof(node_id_t); 00098 }; 00099 00100 void add_nb_entry(node_id_t nb_id, uint8_t nb_st) { 00101 00102 // write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00103 // memcpy(buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, &nb_id, sizeof(node_id_t)); 00104 write<OsModel, block_data_t, node_id_t > 00105 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, nb_id); 00106 buffer[NBS_NUM] += sizeof(node_id_t); 00107 buffer[PAYLOAD_POS] += sizeof(node_id_t); 00108 write<OsModel, block_data_t, uint8_t > 00109 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, nb_st); 00110 // memcpy(buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, &nb_st, sizeof(uint8_t)); 00111 buffer[NBS_NUM] += sizeof(uint8_t); 00112 buffer[PAYLOAD_POS] += sizeof(uint8_t); 00113 00114 }; 00115 00116 void add_nb_entry(node_id_t nb_id, uint8_t nb_st, uint16_t nd_st) { 00117 00118 if ( buffer[PAYLOAD_POS] > 95 ) { 00119 return; 00120 } 00121 // write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00122 // memcpy(buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, &nb_id, sizeof(node_id_t)); 00123 write<OsModel, block_data_t, node_id_t > 00124 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, nb_id); 00125 buffer[NBS_NUM] += sizeof(node_id_t); 00126 buffer[PAYLOAD_POS] += sizeof(node_id_t); 00127 write<OsModel, block_data_t, uint8_t > 00128 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, nb_st); 00129 // memcpy(buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, &nb_st, sizeof(uint8_t)); 00130 buffer[NBS_NUM] += sizeof(uint8_t); 00131 buffer[PAYLOAD_POS] += sizeof(uint8_t); 00132 00133 write<OsModel, block_data_t, uint16_t > 00134 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, nd_st); 00135 // memcpy(buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, &nb_st, sizeof(uint8_t)); 00136 buffer[NBS_NUM] += sizeof(uint16_t); 00137 buffer[PAYLOAD_POS] += sizeof(uint16_t); 00138 }; 00139 00140 void add(uint8_t data) { 00141 write<OsModel, block_data_t, uint8_t > 00142 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, data); 00143 buffer[NBS_NUM] += sizeof(uint8_t); 00144 buffer[PAYLOAD_POS] += sizeof(uint8_t); 00145 } 00146 00147 void add(uint16_t data) { 00148 write<OsModel, block_data_t, uint16_t > 00149 (buffer + buffer[NBS_NUM] + PAYLOAD_POS + 1, data); 00150 buffer[NBS_NUM] += sizeof(uint16_t); 00151 buffer[PAYLOAD_POS] += sizeof(uint16_t); 00152 } 00153 00154 void set_nearby_list_size(uint8_t len) { 00155 buffer[NBS_NUM] = len; 00156 }; 00157 00158 void set_nb_list(uint8_t len, uint8_t * buf) { 00159 buffer[NBS_NUM] = len; 00160 }; 00161 00162 uint8_t nb_list_size(void) { 00163 return buffer[NBS_NUM]; 00164 }; 00165 00166 uint8_t get_nb_list(uint8_t * buf) { 00167 buf = buffer + PAYLOAD_POS + 1; 00168 return buffer[NBS_NUM]; 00169 }; 00170 00171 void add_pg_payload(uint8_t app_id, uint8_t *data, uint8_t len) { 00172 00173 buffer[app_id+PAYLOAD_POS+1] = TOTAL_REG_ALG; 00174 }; 00175 00176 void append_payload(uint8_t &app_id, uint8_t *data, uint8_t &len) { 00177 00178 buffer[PG_NUM]++; 00179 write<OsModel, block_data_t, uint8_t > (buffer + buffer_size(), app_id); 00180 write<OsModel, block_data_t, uint8_t > (buffer + buffer_size() + 1, len); 00181 memcpy(buffer + buffer_size() + 2, data, len); 00182 buffer[PAYLOAD_POS] += (len + 2); 00183 // write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00184 }; 00185 00186 uint8_t get_pg_payloads_num(void) { 00187 return buffer[PG_NUM]; 00188 }; 00189 00190 // void append_payload(uint8_t pay) 00191 00192 private: 00193 00194 inline void set_payload_size(uint8_t len) { 00195 write<OsModel, block_data_t, uint8_t > (buffer + PAYLOAD_POS, len); 00196 } 00197 00198 block_data_t buffer[Radio::MAX_MESSAGE_LENGTH]; // buffer for the message data 00199 }; 00200 00201 } 00202 00203 #endif /* ECHOMSG_H */ 00204