Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "config/compiler.h"
00038
00039 #include "core/include/frames.h"
00040 #include "core/include/definitions.h"
00041 #include "core/interface/wmp_interface.h"
00042 #include "core/include/wmp_misc.h"
00043 #include "core/include/ml_com.h"
00044 #include "core/include/ll_com.h"
00045 #include "core/include/frame_compress.h"
00046 #include "core/include/wmp_utils.h"
00047 #include "core/include/global.h"
00048
00049 static int wack = 0;
00050 static int filter_type = 0, filter_param = 0;
00051
00052 void closeMiddleLevelCom() {
00053 closeLowLevelCom();
00054 }
00055
00056 int initMiddleLevelCom() {
00057 return initLowLevelCom();
00058 }
00059 void ml_set_ett(int _use_ett, int _ett_mult, int _ett_max) {
00060 status.use_ett = _use_ett;
00061 }
00062
00063 int ml_calculate_duration(int rate, int size) {
00064 return 1;
00065 }
00066
00067
00068 void ml_set_filter(char * ftype, int param){
00069 filter_param = param;
00070 if (strcmp(ftype,"chain") == 0){
00071 filter_type = 1;
00072 }else if (strcmp(ftype,"star") == 0){
00073 filter_type = 2;
00074 }
00075 }
00076
00077 void wmpForceTopology(char * name, int param){
00078 ml_set_filter(name, param);
00079 }
00080
00081 static int ml_filter(char from){
00082 switch (filter_type){
00083 case 1:{
00084 if (abs(from-wmpGetNodeId())>1){
00085 return 1;
00086 }else{
00087 return 0;
00088 }
00089 }
00090 case 2:{
00091 if (wmpGetNodeId()==filter_param){
00092 return 0;
00093 }else{
00094 if (from == filter_param){
00095 return 0;
00096 }
00097 }
00098 return 1;
00099 }
00100 default:
00101 return 0;
00102 }
00103 }
00104
00105
00106
00107 static int rx_error_type = 0, last_discarded = 0, rx_error_param = 1,last_discarded_type = 0;
00108 void ml_set_rx_error(char * ftype, int param){
00109 rx_error_param = param;
00110 if (strcmp(ftype,"all") == 0){
00111 rx_error_type = 1;
00112 }else if (strcmp(ftype,"messages") == 0){
00113 rx_error_type = 2;
00114 }else if (strcmp(ftype,"unconfirm") == 0){
00115 rx_error_type = 3;
00116 }
00117 }
00118
00119 void wmpSetRxError(char * name, int rate){
00120 if (rate == 0){
00121 rx_error_param = 1;
00122 }
00123 ml_set_rx_error(name, rate);
00124 }
00125
00126 static int ml_rx_error(wmpFrame * p){
00127 int discard = 0;
00128 if (rx_error_type == 0 || rx_error_param == 0){
00129 return 0;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 switch (rx_error_type){
00140 case 1:
00141 discard = ((rand() % rx_error_param) == 0);
00142 break;
00143 case 2:
00144 if (p->hdr.type == MESSAGE){
00145 discard = ((rand() % rx_error_param) == 0);
00146 }
00147 break;
00148 case 3:
00149 discard = 0;
00150 if ((rand() % rx_error_param) == 0){
00151 mBitsUnset(p->hdr.ack,rand() % wmpGetNumOfNodes());
00152 }
00153 break;
00154 }
00155
00156
00157 if (discard){
00158 last_discarded = p->hdr.serial;
00159 last_discarded_type = p->hdr.type;
00160 }
00161 return discard;
00162 }
00163
00164
00165
00166 int ml_send(wmpFrame * p, int size) {
00167 int ret = UNDEF;
00168
00169 if (p->hdr.type != DROP_TOKEN && p->hdr.type != ACK) {
00170
00171 wack = 1;
00172 }
00173 p->hdr.net_id = status.net_id;
00174
00175 ret = cmp_send((char *) p, size);
00176
00177 return ret;
00178 }
00179
00180 int ml_receive(wmpFrame *f, int timeout) {
00181 rxInfo ret;
00182 struct timespec before, after;
00183 unsigned long long elapsed_ms = 0, tot_elapsed = 0;
00184
00185 while (timeout > 0) {
00186
00187 GETNSTIMEOFDAY(&before);
00188 ret = cmp_receive((char*) f, timeout);
00189 GETNSTIMEOFDAY(&after);
00190
00191 elapsed_ms = wmp_elapsed_ms(&before, &after);
00192 tot_elapsed+= elapsed_ms;
00193
00194 if (ret.error) {
00195 return EXPIRED;
00196 }
00197
00198 timeout = (timeout - elapsed_ms) > 0 ? (timeout - elapsed_ms) : 0;
00199
00200 if (ret.proto == 0x6969 || ret.proto == 0x6970) {
00201
00202 if (f->hdr.from == status.id) {
00203
00204 continue;
00205 }
00206
00207 if (ml_filter(f->hdr.from)){
00208
00209 }
00210
00211 if (ml_rx_error(f)){
00212 continue;
00213 }
00214
00215 if (f->hdr.net_id == status.net_id) {
00216 if (ret.has_lq) {
00217 f->hdr.rssi = ret.rssi;
00218 f->hdr.noise = ret.noise;
00219 }
00220 return RECEIVE_OK;
00221 } else {
00222 WMP_MSG(stderr, "*** Warning ::: Foreign network detected (id:%d)\n", f->hdr.net_id);
00223 continue;
00224 }
00225 } else{
00226
00227
00228
00229
00230 continue;
00231 }
00232 }
00233
00234 return EXPIRED;
00235 }
00236
00237
00238