p802_15_4nam.cc

Go to the documentation of this file.
00001 /********************************************/
00002 /*     NS2 Simulator for IEEE 802.15.4      */
00003 /*           (per P802.15.4/D18)            */
00004 /*------------------------------------------*/
00005 /* by:        Jianliang Zheng               */
00006 /*        (zheng@ee.ccny.cuny.edu)          */
00007 /*              Myung J. Lee                */
00008 /*          (lee@ccny.cuny.edu)             */
00009 /*        ~~~~~~~~~~~~~~~~~~~~~~~~~         */
00010 /*           SAIT-CUNY Joint Lab            */
00011 /********************************************/
00012 
00013 // File:  p802_15_4nam.cc
00014 // Mode:  C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t
00015 
00016 // $Header: /nfs/jade/vint/CVSROOT/ns-2/wpan/p802_15_4nam.cc,v 1.1 2005/01/24 18:34:25 haldar Exp $
00017 
00018 /*
00019  * Copyright (c) 2003-2004 Samsung Advanced Institute of Technology and
00020  * The City University of New York. All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions
00024  * are met:
00025  * 1. Redistributions of source code must retain the above copyright
00026  *    notice, this list of conditions and the following disclaimer.
00027  * 2. Redistributions in binary form must reproduce the above copyright
00028  *    notice, this list of conditions and the following disclaimer in the
00029  *    documentation and/or other materials provided with the distribution.
00030  * 3. All advertising materials mentioning features or use of this software
00031  *    must display the following acknowledgement:
00032  *  This product includes software developed by the Joint Lab of Samsung 
00033  *      Advanced Institute of Technology and The City University of New York.
00034  * 4. Neither the name of Samsung Advanced Institute of Technology nor of 
00035  *    The City University of New York may be used to endorse or promote 
00036  *    products derived from this software without specific prior written 
00037  *    permission.
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE JOINT LAB OF SAMSUNG ADVANCED INSTITUTE
00040  * OF TECHNOLOGY AND THE CITY UNIVERSITY OF NEW YORK ``AS IS'' AND ANY EXPRESS 
00041  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00042  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
00043  * NO EVENT SHALL SAMSUNG ADVANCED INSTITUTE OR THE CITY UNIVERSITY OF NEW YORK 
00044  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00045  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00046  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00048  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00049  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00050  */
00051 
00052 
00053 #include "p802_15_4mac.h"
00054 #include "p802_15_4nam.h"
00055 
00056 
00057 MACLINK *macLink1 = NULL;
00058 MACLINK *macLink2 = NULL;
00059 
00060 int addMacLink(int ad,Mac802_15_4 *m)
00061 {
00062     MACLINK *tmp;
00063 
00064     if (macLink2 == NULL)       //not exist yet
00065     {
00066         macLink2 = new MACLINK(ad,m);
00067         if (macLink2 == NULL) return 1;
00068         macLink1 = macLink2;
00069     }
00070     else
00071     {
00072         tmp=new MACLINK(ad,m);
00073         if (tmp == NULL) return 1;
00074         tmp->last = macLink2;
00075         macLink2->next = tmp;
00076         macLink2 = tmp;
00077     }
00078     return 0;
00079 }
00080 
00081 int updateMacLink(int oper,int ad)
00082 {
00083     MACLINK *tmp;
00084     int rt;
00085 
00086     rt = 1;
00087 
00088     tmp = macLink1;
00089     while(tmp != NULL)
00090     {
00091         if (tmp->addr == ad)
00092         {
00093             if (oper == mac_oper_del)   //delete an element
00094             {
00095                 if(tmp->last != NULL)
00096                 {
00097                     tmp->last->next = tmp->next;
00098                     if(tmp->next != NULL)
00099                         tmp->next->last = tmp->last;
00100                     else
00101                         macLink2 = tmp->last;
00102                 }
00103                 else if (tmp->next != NULL)
00104                 {
00105                     macLink1 = tmp->next;
00106                     tmp->next->last = NULL;
00107                 }
00108                 else
00109                 {
00110                     macLink1 = NULL;
00111                     macLink2 = NULL;
00112                 }
00113                 delete tmp;
00114             }
00115             rt = 0;
00116             break;
00117         }
00118         tmp = tmp->next;
00119     }
00120     return rt;
00121 }
00122 
00123 Mac802_15_4 *getMacLink(int ad)
00124 {
00125     MACLINK *tmp;
00126 
00127     tmp = macLink1;
00128     while(tmp != NULL)
00129     {
00130         if(tmp->addr == ad)
00131             return tmp->mac;
00132         tmp = tmp->next;
00133     }
00134 
00135     return NULL;
00136 }
00137 
00138 int chkAddMacLink(int ad,Mac802_15_4 *m)
00139 {
00140         int i;
00141 
00142         i = updateMacLink(at_oper_est,ad);
00143         if (i == 0) return 1;
00144         i = addMacLink(ad,m);
00145         if (i == 0) return 0;
00146         else return 2;
00147 }
00148 
00149 //-------------------------------------------------------------------
00150 
00151 ATTRIBUTELINK *attrLink1 = NULL;
00152 ATTRIBUTELINK *attrLink2 = NULL;
00153 int ATTRIBUTE_SN = 32;
00154 
00155 packet_t nam_pktName2Type(const char *name)
00156 {
00157     //not all types included
00158 
00159     return (strcmp(packet_info.name(PT_TCP),name) == 0)?PT_TCP:
00160                (strcmp(packet_info.name(PT_UDP),name) == 0)?PT_UDP: 
00161                (strcmp(packet_info.name(PT_CBR),name) == 0)?PT_CBR: 
00162                (strcmp(packet_info.name(PT_ACK),name) == 0)?PT_ACK: 
00163                (strcmp(packet_info.name(PT_PRUNE),name) == 0)?PT_PRUNE: 
00164                (strcmp(packet_info.name(PT_GRAFT),name) == 0)?PT_GRAFT: 
00165                (strcmp(packet_info.name(PT_GRAFTACK),name) == 0)?PT_GRAFTACK: 
00166                (strcmp(packet_info.name(PT_RTCP),name) == 0)?PT_RTCP: 
00167                (strcmp(packet_info.name(PT_RTP),name) == 0)?PT_RTP: 
00168                (strcmp(packet_info.name(PT_SRM),name) == 0)?PT_SRM: 
00169                (strcmp(packet_info.name(PT_TELNET),name) == 0)?PT_TELNET: 
00170                (strcmp(packet_info.name(PT_FTP),name) == 0)?PT_FTP: 
00171                (strcmp(packet_info.name(PT_HTTP),name) == 0)?PT_HTTP: 
00172                (strcmp(packet_info.name(PT_MFTP),name) == 0)?PT_MFTP: 
00173                (strcmp(packet_info.name(PT_ARP),name) == 0)?PT_ARP: 
00174                (strcmp(packet_info.name(PT_MAC),name) == 0)?PT_MAC: 
00175                (strcmp(packet_info.name(PT_TORA),name) == 0)?PT_TORA: 
00176                (strcmp(packet_info.name(PT_DSR),name) == 0)?PT_DSR: 
00177                (strcmp(packet_info.name(PT_AODV),name) == 0)?PT_AODV: 
00178                (strcmp(packet_info.name(PT_IMEP),name) == 0)?PT_IMEP: 
00179                (strcmp(packet_info.name(PT_PING),name) == 0)?PT_PING: 
00180                (strcmp(packet_info.name(PT_LDP),name) == 0)?PT_LDP: 
00181                (strcmp(packet_info.name(PT_GAF),name) == 0)?PT_GAF: 
00182                (strcmp(packet_info.name(PT_PGM),name) == 0)?PT_PGM:PT_NTYPE;
00183 }
00184 
00185 int addAttrLink(packet_t pt,char *clr,int s,int d)
00186 {
00187     ATTRIBUTELINK *tmp;
00188 
00189     if (attrLink2 == NULL)      //not exist yet
00190     {
00191         attrLink2 = new ATTRIBUTELINK(pt,clr,s,d);
00192         if (attrLink2 == NULL) return 1;
00193         attrLink1 = attrLink2;
00194     }
00195     else
00196     {
00197         tmp=new ATTRIBUTELINK(pt,clr,s,d);
00198         if (tmp == NULL) return 1;
00199         tmp->last = attrLink2;
00200         attrLink2->next = tmp;
00201         attrLink2 = tmp;
00202     }
00203     return 0;
00204 }
00205 
00206 int updateAttrLink(int oper,packet_t pt,int s,int d)
00207 {
00208     ATTRIBUTELINK *tmp;
00209     int rt;
00210 
00211     rt = 1;
00212 
00213     tmp = attrLink1;
00214     while(tmp != NULL)
00215     {
00216         if ((tmp->ptype == pt)&&(tmp->src == s)&&(tmp->dst == d))
00217         {
00218             if (oper == at_oper_del)    //delete an element
00219             {
00220                 if(tmp->last != NULL)
00221                 {
00222                     tmp->last->next = tmp->next;
00223                     if(tmp->next != NULL)
00224                         tmp->next->last = tmp->last;
00225                     else
00226                         attrLink2 = tmp->last;
00227                 }
00228                 else if (tmp->next != NULL)
00229                 {
00230                     attrLink1 = tmp->next;
00231                     tmp->next->last = NULL;
00232                 }
00233                 else
00234                 {
00235                     attrLink1 = NULL;
00236                     attrLink2 = NULL;
00237                 }
00238                 delete tmp;
00239             }
00240             rt = 0;
00241             break;
00242         }
00243         tmp = tmp->next;
00244     }
00245     return rt;
00246 }
00247 
00248 int chkAddAttrLink(packet_t pt,char *clr,int s,int d)
00249 {
00250         int i;
00251 
00252         i = updateAttrLink(at_oper_est,pt,s,d);
00253         if (i == 0) return 1;
00254         i = addAttrLink(pt,clr,s,d);
00255         if (i == 0) return 0;
00256         else return 2;
00257 }
00258 
00259 ATTRIBUTELINK *findAttrLink(packet_t pt,int s,int d)
00260 {
00261     ATTRIBUTELINK *tmp;
00262 
00263     tmp = attrLink1;
00264     while(tmp != NULL)
00265     {
00266         if((tmp->ptype == pt)&&(tmp->src == s)&&(tmp->dst == d))
00267             return tmp;
00268         tmp = tmp->next;
00269     }
00270 
00271     return NULL;
00272 }
00273 
00274 //---------------------------------------------------------------------------
00275 
00276 bool Nam802_15_4::Nam_Status = false;       
00277 bool Nam802_15_4::emStatus = false;     
00278 bool Nam802_15_4::emHandling = true;
00279 char Nam802_15_4::def_PANCoor_clr[] = "tomato";
00280 char Nam802_15_4::def_Coor_clr[] = "blue";
00281 char Nam802_15_4::def_Dev_clr[] = "green3";
00282 char Nam802_15_4::def_Node_clr[] = "black";
00283 char Nam802_15_4::def_Mark_clr[] = "black";
00284 char Nam802_15_4::def_ColFlash_clr[] = "gold";
00285 char Nam802_15_4::def_NodeFail_clr[] = "grey";
00286 char Nam802_15_4::def_LinkFailFlash_clr[] = "red";
00287 
00288 Nam802_15_4::Nam802_15_4(const char *ncolor,const char *mcolor,Mac802_15_4 *m)
00289 {
00290     strncpy(nodeColor,ncolor,20);
00291     nodeColor[20] = 0;
00292     strcpy(nodePreColor,nodeColor);
00293     strncpy(markColor,mcolor,20);
00294     markColor[20] = 0;
00295     strcpy(label,"\"\"");
00296     mac = m;
00297 }
00298 
00299 void Nam802_15_4::flowAttribute(int clrID,const char *clrName)
00300 {
00301     Tcl& tcl = Tcl::instance();
00302 
00303     if (!Nam_Status) return;
00304 
00305     tcl.evalf("[Simulator instance] puts-nam-traceall {c -t * -i %d -n %s}",
00306     clrID,clrName);
00307 }
00308 
00309 void Nam802_15_4::changePlaybackRate(double atTime,const char *stepLen)
00310 {
00311     Tcl& tcl = Tcl::instance();
00312 
00313     if (!Nam_Status) return;
00314 
00315     if (atTime > 0.0)
00316         atTime += 0.000000001;      
00317     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {v -t %.9f -e set_rate_ext %s 1}}",
00318     atTime,atTime,stepLen);
00319 }
00320 
00321 void Nam802_15_4::changeNodeColor(double atTime,const char *newColor,bool save)
00322 {
00323     char t_newColor[21];
00324     Tcl& tcl = Tcl::instance();
00325 
00326     if (!Nam_Status) return;
00327 
00328     if (emStatus&&emHandling) return;
00329 
00330     if (atTime > 0.0)
00331         atTime += 0.000000001;      
00332     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S COLOR -c %s -o %s}}",
00333         atTime,atTime,mac->index_,newColor,nodeColor);
00334     if (save)
00335     {
00336         strncpy(t_newColor,newColor,20);
00337         t_newColor[20] = 0;
00338         strcpy(nodePreColor,nodeColor); 
00339         strncpy(nodeColor,t_newColor,20);
00340         nodeColor[20] = 0;
00341     }
00342 }
00343 
00344 void Nam802_15_4::changeBackNodeColor(double atTime)
00345 {
00346     changeNodeColor(atTime,nodePreColor);
00347 }
00348 
00349 void Nam802_15_4::flashNodeColor(double atTime,const char *flashColor)
00350 {
00351     char t_flashColor[21];
00352     Tcl& tcl = Tcl::instance();
00353 
00354     if (!Nam_Status) return;
00355 
00356     if (emStatus&&emHandling) return;
00357 
00358     if (flashColor[0] == 0)
00359         strcpy(t_flashColor,Nam802_15_4::def_ColFlash_clr);
00360     else
00361     {
00362         strncpy(t_flashColor,flashColor,20);
00363         t_flashColor[20] = 0;
00364     }
00365 
00366     atTime += 0.000000001;      
00367     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S COLOR -c %s -o %s}}",
00368         atTime,atTime,mac->index_,t_flashColor,nodeColor);
00369     atTime += 0.010000000;
00370     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S COLOR -c %s -o %s}}",
00371         atTime,atTime,mac->index_,"grey",t_flashColor);
00372     atTime += 0.010000000;
00373     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S COLOR -c %s -o %s}}",
00374         atTime,atTime,mac->index_,nodeColor,"grey");
00375 }
00376 
00377 void Nam802_15_4::changeMarkColor(double atTime,const char *newColor)
00378 {
00379     Tcl& tcl = Tcl::instance();
00380 
00381     if (!Nam_Status) return;
00382 
00383     if (atTime > 0.0)
00384         atTime += 0.000000001;      
00385     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S COLOR -i %s -I %s -c %s -o %s}}",
00386         atTime,atTime,mac->index_,newColor,markColor,nodeColor,nodeColor);
00387     strncpy(markColor,newColor,20);
00388     markColor[20] = 0;
00389 }
00390 
00391 void Nam802_15_4::flashNodeMark(double atTime)
00392 {
00393     Tcl& tcl = Tcl::instance();
00394 
00395     if (!Nam_Status) return;
00396 
00397     if (emStatus&&emHandling) return;
00398 
00399     atTime += 0.010000000;
00400     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S COLOR -i %s -I %s -c %s -o %s}}",
00401         atTime,atTime,mac->index_,"LightGrey",markColor,nodeColor,nodeColor);
00402     atTime += 0.010000000;
00403     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S COLOR -i %s -I %s -c %s -o %s}}",
00404         atTime,atTime,mac->index_,markColor,"LightGrey",nodeColor,nodeColor);
00405 }
00406 
00407 void Nam802_15_4::changeLabel(double atTime,const char *lb)
00408 {
00409     Tcl& tcl = Tcl::instance();
00410 
00411     if (!Nam_Status) return;
00412 
00413     if (atTime > 0.0)
00414         atTime += 0.000000001;      
00415     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S DLABEL -l %s -L %s}}",
00416         atTime,atTime,mac->index_,lb,label);
00417     strncpy(label,lb,80);
00418     label[80] = 0;
00419 }
00420 
00421 void Nam802_15_4::flashLinkFail(double atTime,int dst,const char *flashColor)
00422 {
00423     char t_flashColor[21];
00424     Tcl& tcl = Tcl::instance();
00425 
00426     if (!Nam_Status) return;
00427 
00428     if (flashColor[0] == 0)
00429         strcpy(t_flashColor,Nam802_15_4::def_LinkFailFlash_clr);
00430     else
00431     {
00432         strncpy(t_flashColor,flashColor,20);
00433         t_flashColor[20] = 0;
00434     }
00435     
00436     atTime += 0.000000001;      
00437     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S DLABEL -l \" ~ %d\" -L %s}}",
00438         atTime,atTime,mac->index_,dst,label);
00439     atTime += 0.020000000;
00440     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S DLABEL -l \" \" -L \" ~ %d\"}}",
00441         atTime,atTime,mac->index_,dst);
00442     atTime += 0.010000000;
00443     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] puts-nam-traceall {n -t %.9f -s %d -S DLABEL -l %s -L \" ~ %d\"}}",
00444         atTime,atTime,mac->index_,label,dst);
00445 }
00446 
00447 void Nam802_15_4::annotate(double atTime,const char *note)
00448 {
00449     Tcl& tcl = Tcl::instance();
00450 
00451     atTime += 0.000000001;      
00452     tcl.evalf("[Simulator instance] at %.9f {[Simulator instance] trace-annotate %s}",atTime,note);
00453 }
00454 
00455 // End of file: p802_15_4nam.cc

Generated on Tue Mar 6 16:47:48 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6