Wiselib
|
00001 #ifndef __FRONTS_JOIN_DECISION_H_ 00002 #define __FRONTS_JOIN_DECISION_H_ 00003 00004 namespace wiselib { 00005 00011 template<typename OsModel_P, typename Radio_P> 00012 class FrontsJoinDecision { 00013 public: 00014 //TYPEDEFS 00015 typedef OsModel_P OsModel; 00016 // os modules 00017 typedef Radio_P Radio; 00018 typedef typename OsModel::Debug Debug; 00019 00020 // data types 00021 typedef typename Radio::node_id_t node_id_t; 00022 typedef typename Radio::block_data_t block_data_t; 00023 typedef node_id_t cluster_id_t; 00024 00025 /* 00026 * Constructor 00027 * */ 00028 FrontsJoinDecision() : 00029 cluster_id_(-1), hops_(0) { 00030 } 00031 00032 /* 00033 * Destructor 00034 * */ 00035 ~FrontsJoinDecision() { 00036 } 00037 00038 /* 00039 * INIT 00040 * initializes the values of radio and debug 00041 */ 00042 void init(Radio& radio, Debug& debug) { 00043 radio_ = &radio; 00044 debug_ = &debug; 00045 } 00046 00047 /* SET functions */ 00048 00049 //set the cluster id 00050 void set_cluster_id(cluster_id_t cluster_id) { 00051 cluster_id_ = cluster_id; 00052 } 00053 00054 void set_maxhops(int hops) { 00055 maxhops_ = hops; 00056 } 00057 00058 //set the hops from head 00059 void set_hops(int hops) { 00060 hops_ = hops; 00061 } 00062 00063 int hops() { 00064 return hops_; 00065 } 00066 00067 /* GET functions */ 00068 00069 //get the cluster id 00070 00071 cluster_id_t cluster_id(void) { 00072 return cluster_id_; 00073 } 00074 00075 //get the join request payload 00076 JoinClusterMsg<OsModel, Radio> get_join_request_payload() { 00077 JoinClusterMsg<OsModel, Radio> msg; 00078 msg.set_cluster_id(cluster_id_); 00079 msg.set_hops(hops_); 00080 00081 #ifdef DEBUG_PAYLOAD 00082 debug().debug("[%d|%x|%d]\n",JOIN,cluster_id_,hops_); 00083 #endif 00084 return msg; 00085 } 00086 00087 JoinAccClusterMsg<OsModel, Radio> get_join_accept_payload() { 00088 JoinAccClusterMsg<OsModel, Radio> msg; 00089 msg.set_node_id(radio().id()); 00090 00091 #ifdef DEBUG_PAYLOAD 00092 debug().debug("[%d|%x]\n", JOIN_ACCEPT, radio().id()); 00093 #endif 00094 return msg; 00095 } 00096 00097 /* 00098 * JOIN 00099 * respond to an JOIN message received 00100 * either join to a cluster or not 00101 * */ 00102 bool join(uint8_t *payload, uint8_t length) { 00103 JoinClusterMsg<OsModel, Radio> msg; 00104 memcpy(&msg, payload, length); 00105 00106 int mess_hops = msg.hops(); 00107 00108 //if in no cluster yet 00109 if (cluster_id_ == Radio::NULL_NODE_ID) { 00110 if (mess_hops < maxhops_) { 00111 //join the cluster 00112 cluster_id_ = msg.cluster_id(); 00113 //set the hops from head 00114 hops_ = mess_hops + 1; 00115 //return true 00116 return true; 00117 } else { 00118 return false; 00119 } 00120 }//if already in a cluster 00121 else { 00122 cluster_id_t mess_cluster_id = msg.cluster_id(); 00123 //join the cluster 00124 if (mess_hops + 1 < hops_) { 00125 int mess_hops = msg.hops(); 00126 00127 if (mess_cluster_id < cluster_id_) { 00128 //join the cluster 00129 cluster_id_ = msg.cluster_id(); 00130 //set the hops from head 00131 hops_ = mess_hops + 1; 00132 //return true 00133 return true; 00134 } else { 00135 //return false 00136 return false; 00137 } 00138 } else { 00139 return false; 00140 } 00141 } 00142 } 00143 00144 /* 00145 * RESET 00146 * resets the module 00147 * initializes values 00148 * */ 00149 void reset() { 00150 cluster_id_ = -1; 00151 hops_ = 0; 00152 } 00153 00154 private: 00155 cluster_id_t cluster_id_; //id of current CH 00156 int hops_; //hops from current CH 00157 int maxhops_; //Maximum distance from CH 00158 00159 Radio * radio_; //radio module 00160 Debug * debug_; //debug module 00161 00162 Radio& radio() { 00163 return *radio_; 00164 } 00165 00166 Debug& debug() { 00167 return *debug_; 00168 } 00169 }; 00170 } 00171 #endif