ml_com.c
Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *---------------------           RT-WMP              --------------------
00003  *------------------------------------------------------------------------
00004  *                                                         V7.0B  11/05/10
00005  *
00006  *
00007  *  File: ./src/core/ml_com.c
00008  *  Authors: Danilo Tardioli
00009  *  ----------------------------------------------------------------------
00010  *  Copyright (C) 2000-2010, Universidad de Zaragoza, SPAIN
00011  *
00012  *  Contact Addresses: Danilo Tardioli                   dantard@unizar.es
00013  *
00014  *  RT-WMP is free software; you can  redistribute it and/or  modify it
00015  *  under the terms of the GNU General Public License  as published by the
00016  *  Free Software Foundation;  either  version 2, or (at  your option) any
00017  *  later version.
00018  *
00019  *  RT-WMP  is distributed  in the  hope  that  it will be   useful, but
00020  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
00021  *  MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
00022  *  General Public License for more details.
00023  *
00024  *  You should have received  a  copy of  the  GNU General Public  License
00025  *  distributed with RT-WMP;  see file COPYING.   If not,  write to the
00026  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
00027  *  02111-1307, USA.
00028  *
00029  *  As a  special exception, if you  link this  unit  with other  files to
00030  *  produce an   executable,   this unit  does  not  by  itself cause  the
00031  *  resulting executable to be covered by the  GNU General Public License.
00032  *  This exception does  not however invalidate  any other reasons why the
00033  *  executable file might be covered by the GNU Public License.
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 /* FILTER */
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:{ /* chain */
00084                 if (abs(from-wmpGetNodeId())>1){
00085                         return 1;
00086                 }else{
00087                         return 0;
00088                 }
00089         }
00090         case 2:{ /* star */
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 /* RX_ERROR */
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 //      if (p->hdr.to != wmpGetNodeId()){
00133 //              return 0;
00134 //      }
00135 
00136 //      if (p->hdr.serial == last_discarded && p->hdr.type == last_discarded_type){
00137 //              return 1;
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 /* TX and RX functions */
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                 /* every time I send something, I wait an ack */
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; //, orig_timeout=timeout;
00184 
00185         while (timeout > 0) {
00186                 /* wait TIMEOUT for acknoledwge frame or for net inactivity */
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                                 /* discard sent frame if present*/
00204                                 continue;
00205                         }
00206 
00207                         if (ml_filter(f->hdr.from)){
00208                                 //continue;
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 //                      if (timeout < 5){
00227 //                              timeout+=2;
00228 //                      }
00229 //                      WMP_MSG(stderr, "*** Warning ::: Foreign frame detected (proto:%d) restart read with TO %d (elapsed:%d)\n", ret.proto, timeout, elapsed_ms);
00230                         continue;
00231                 }
00232         }
00233 
00234         return EXPIRED;
00235 }
00236 
00237 
00238 


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Fri Jan 3 2014 12:07:55