cmu-trace.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the Computer Systems
00017  *  Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * Ported from CMU/Monarch's code, appropriate copyright applies.
00035  * nov'98 -Padma.
00036  *
00037  * $Header: /nfs/jade/vint/CVSROOT/ns-2/trace/cmu-trace.cc,v 1.88 2005/10/08 19:16:16 tomh Exp $
00038  */
00039 
00040 #include <packet.h>
00041 #include <ip.h>
00042 #include <tcp.h>
00043 #include <sctp.h>
00044 #include <rtp.h>
00045 #include <arp.h>
00046 #include <dsr/hdr_sr.h> // DSR
00047 #include <mac.h>
00048 #include <mac-802_11.h>
00049 #include <smac.h>
00050 #include <address.h>
00051 #include <tora/tora_packet.h> //TORA
00052 #include <imep/imep_spec.h>         // IMEP
00053 #include <aodv/aodv_packet.h> //AODV
00054 #include <cmu-trace.h>
00055 #include <mobilenode.h>
00056 #include <simulator.h>
00057 //<zheng: add for 802.15.4>
00058 #include "wpan/p802_15_4pkt.h"
00059 #include "wpan/p802_15_4trace.h"
00060 #include "wpan/p802_15_4nam.h"
00061 //</zheng: add for 802.15.4>
00062 
00063 #include "diffusion/diff_header.h" // DIFFUSION -- Chalermek
00064 
00065 
00066 //#define LOG_POSITION
00067 
00068 //extern char* pt_names[];
00069 
00070 static class CMUTraceClass : public TclClass {
00071 public:
00072     CMUTraceClass() : TclClass("CMUTrace") { }
00073     TclObject* create(int, const char*const* argv) {
00074         return (new CMUTrace(argv[4], *argv[5]));
00075     }
00076 } cmutrace_class;
00077 
00078 
00079 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
00080 double CMUTrace::bradius = 0.0;
00081 double CMUTrace::radius_scaling_factor_ = 0.0;
00082 double CMUTrace::duration_scaling_factor_ = 0.0;
00083 //</zheng>
00084 
00085 CMUTrace::CMUTrace(const char *s, char t) : Trace(t)
00086 {
00087     bzero(tracename, sizeof(tracename));
00088     strncpy(tracename, s, MAX_ID_LEN);
00089 
00090         if(strcmp(tracename, "RTR") == 0) {
00091                 tracetype = TR_ROUTER;
00092         }
00093     else if(strcmp(tracename, "TRP") == 0) {
00094                 tracetype = TR_ROUTER;
00095         }
00096         else if(strcmp(tracename, "MAC") == 0) {
00097                 tracetype = TR_MAC;
00098         }
00099         else if(strcmp(tracename, "IFQ") == 0) {
00100                 tracetype = TR_IFQ;
00101         }
00102         else if(strcmp(tracename, "AGT") == 0) {
00103                 tracetype = TR_AGENT;
00104         }
00105         else {
00106                 fprintf(stderr, "CMU Trace Initialized with invalid type\n");
00107                 exit(1);
00108         }
00109 // change wrt Mike's code
00110 //  assert(type_ == DROP || type_ == SEND || type_ == RECV);
00111     assert(type_ == DROP || type_ == SEND || type_ == RECV
00112                || ((type_ == EOT) && (tracetype == TR_MAC)));
00113 
00114 
00115 
00116     newtrace_ = 0;
00117     for (int i=0 ; i < MAX_NODE ; i++) 
00118         nodeColor[i] = 3 ;
00119         node_ = 0;
00120 }
00121 
00122 void
00123 CMUTrace::format_mac_common(Packet *p, const char *why, int offset)
00124 {
00125     struct hdr_cmn *ch = HDR_CMN(p);
00126     struct hdr_ip *ih = HDR_IP(p);
00127     struct hdr_mac802_11 *mh;
00128     struct hdr_smac *sh;
00129     char mactype[SMALL_LEN];
00130 
00131     strcpy(mactype, Simulator::instance().macType());
00132     if (strcmp (mactype, "Mac/SMAC") == 0)
00133         sh = HDR_SMAC(p);
00134     else
00135         mh = HDR_MAC802_11(p);
00136     
00137     double x = 0.0, y = 0.0, z = 0.0;
00138        
00139     char op = (char) type_;
00140     Node* thisnode = Node::get_node_by_address(src_);
00141     double energy = -1;
00142     if (thisnode) {
00143         if (thisnode->energy_model()) {
00144             energy = thisnode->energy_model()->energy();
00145         }
00146     }
00147 
00148     // hack the IP address to convert pkt format to hostid format
00149     // for now until port ids are removed from IP address. -Padma.
00150 
00151     int src = Address::instance().get_nodeaddr(ih->saddr());
00152 
00153     if(tracetype == TR_ROUTER && type_ == SEND) {
00154         if(src_ != src)
00155             op = FWRD;
00156     }
00157 
00158     // use tagged format if appropriate
00159     if (pt_->tagged()) {
00160         int next_hop = -1 ;
00161         Node* nextnode = Node::get_node_by_address(ch->next_hop_);
00162             if (nextnode) next_hop = nextnode->nodeid(); 
00163 
00164         node_->getLoc(&x, &y, &z);
00165 
00166         if (op == DROP) op = 'd';
00167         if (op == SEND) op = '+';
00168         if (op == FWRD) op = 'h';
00169 
00170         sprintf(pt_->buffer() + offset,
00171             "%c "TIME_FORMAT" -s %d -d %d -p %s -k %3s -i %d "
00172             "-N:loc {%.2f %.2f %.2f} -N:en %f ",
00173             
00174             op,             // event type
00175             Scheduler::instance().clock(),  // time
00176             src_,               // this node
00177             next_hop,           // next hop
00178             packet_info.name(ch->ptype()),  // packet type
00179             tracename,          // trace level
00180             ch->uid(),          // event id
00181             x, y, z,            // location
00182             energy);                // energy
00183 
00184         offset = strlen(pt_->buffer());
00185         if (strcmp (mactype, "Mac/SMAC") == 0) {
00186             format_smac(p, offset);
00187         } else {
00188             format_mac(p, offset);
00189         }
00190         return;
00191     }
00192 
00193 
00194     // Use new ns trace format to replace the old cmu trace format)
00195     if (newtrace_) {
00196         
00197         node_->getLoc(&x, &y, &z);
00198         // consistence
00199         if ( op == DROP ) { op = 'd';}
00200 
00201             // basic trace infomation + basic exenstion
00202 
00203         sprintf(pt_->buffer() + offset,
00204            "%c -t %.9f -Hs %d -Hd %d -Ni %d -Nx %.2f -Ny %.2f -Nz %.2f -Ne %f -Nl %3s -Nw %s ",
00205             op,                       // event type
00206             Scheduler::instance().clock(),  // time
00207             src_,                           // this node
00208                     ch->next_hop_,                  // next hop
00209             src_,                           // this node
00210             x,                              // x coordinate
00211             y,                              // y coordinate
00212             z,                              // z coordinate
00213             energy,                         // energy, -1 = not existing
00214             tracename,                      // trace level
00215                     why);                            // reason
00216 
00217         // mac layer extension
00218 
00219         offset = strlen(pt_->buffer());
00220         if (strcmp(mactype, "Mac/SMAC") == 0) {
00221             format_smac(p, offset);
00222         } else {
00223             format_mac(p, offset);
00224         }
00225         return;
00226     }
00227 
00228 
00229 #ifdef LOG_POSITION
00230         x = 0.0, y = 0.0, z = 0.0;
00231         node_->getLoc(&x, &y, &z);
00232 #endif
00233 
00234     sprintf(pt_->buffer() + offset,
00235 #ifdef LOG_POSITION
00236         "%c %.9f %d (%6.2f %6.2f) %3s %4s %d %s %d ",
00237 #else
00238         "%c %.9f _%d_ %3s %4s %d %s %d",
00239 #endif
00240         op,
00241         Scheduler::instance().clock(),
00242                 src_,                           // this node
00243 #ifdef LOG_POSITION
00244                 x,
00245                 y,
00246 #endif
00247         tracename,
00248         why,
00249         
00250                 ch->uid(),                      // identifier for this event
00251         
00252         ((ch->ptype() == PT_MAC) ? (
00253           (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS"  :
00254           (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS"  :
00255           (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK"  :
00256           //<zheng: add for 802.15.4>
00257           (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN"  :       //Beacon
00258           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1"  :  //CMD: Association request
00259           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2"  :  //CMD: Association response
00260           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3"  :  //CMD: Disassociation notification
00261           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4"  :  //CMD: Data request
00262           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5"  :  //CMD: PAN ID conflict notification
00263           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6"  :  //CMD: Orphan notification
00264           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7"  :  //CMD: Beacon request
00265           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8"  :  //CMD: Coordinator realignment
00266           (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9"  :   //CMD: GTS request
00267           //</zheng: add for 802.15.4>
00268           "UNKN") :
00269          (ch->ptype() == PT_SMAC) ? (
00270           (sh->type == RTS_PKT) ? "RTS" :
00271           (sh->type == CTS_PKT) ? "CTS" :
00272           (sh->type == ACK_PKT) ? "ACK" :
00273           (sh->type == SYNC_PKT) ? "SYNC" :
00274           "UNKN") : 
00275          packet_info.name(ch->ptype())),
00276         ch->size());
00277     
00278     offset = strlen(pt_->buffer());
00279 
00280     if (strncmp (mactype, "Mac/SMAC", 8) == 0) {
00281         format_smac(p, offset);
00282     } else {
00283         format_mac(p, offset);
00284         }
00285     
00286     offset = strlen(pt_->buffer());
00287 
00288     if (thisnode) {
00289         if (thisnode->energy_model()) {
00290             sprintf(pt_->buffer() + offset,
00291                 "[energy %f] ",
00292                 thisnode->energy_model()->energy());
00293         }
00294         }
00295 }
00296 
00297 void
00298 CMUTrace::format_mac(Packet *p, int offset)
00299 {
00300     struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
00301     
00302     if (pt_->tagged()) {
00303         sprintf(pt_->buffer() + offset,
00304             "-M:dur %x -M:s %x -M:d %x -M:t %x ",
00305             mh->dh_duration,        // MAC: duration
00306             
00307             // change wrt Mike's code
00308             //ETHER_ADDR(mh->dh_da),        // MAC: source
00309             //ETHER_ADDR(mh->dh_sa),        // MAC: destination
00310             ETHER_ADDR(mh->dh_ra),          // MAC: source
00311                        ETHER_ADDR(mh->dh_ta),          // MAC: destination
00312 
00313 
00314             GET_ETHER_TYPE(mh->dh_body));   // MAC: type
00315     } else if (newtrace_) {
00316         sprintf(pt_->buffer() + offset, 
00317             "-Ma %x -Md %x -Ms %x -Mt %x ",
00318             mh->dh_duration,
00319             
00320             // change wrt Mike's code
00321             //ETHER_ADDR(mh->dh_da),
00322             //ETHER_ADDR(mh->dh_sa),
00323 
00324             ETHER_ADDR(mh->dh_ra),
00325                        ETHER_ADDR(mh->dh_ta),
00326 
00327             GET_ETHER_TYPE(mh->dh_body));
00328     } else {
00329         sprintf(pt_->buffer() + offset,
00330             " [%x %x %x %x] ",
00331             //*((u_int16_t*) &mh->dh_fc),
00332             mh->dh_duration,
00333             
00334             // change wrt Mike's code
00335             //ETHER_ADDR(mh->dh_da),
00336             //ETHER_ADDR(mh->dh_sa),
00337             ETHER_ADDR(mh->dh_ra),
00338                         ETHER_ADDR(mh->dh_ta),
00339 
00340 
00341             GET_ETHER_TYPE(mh->dh_body));
00342     }
00343 }
00344 
00345 void
00346 CMUTrace::format_smac(Packet *p, int offset)
00347 {
00348     struct hdr_smac *sh = HDR_SMAC(p);
00349     sprintf(pt_->buffer() + offset,
00350         " [%.2f %d %d] ",
00351         sh->duration,
00352         sh->dstAddr,
00353         sh->srcAddr);
00354 }
00355     
00356 
00357 void
00358 CMUTrace::format_ip(Packet *p, int offset)
00359 {
00360         struct hdr_cmn *ch = HDR_CMN(p);
00361     struct hdr_ip *ih = HDR_IP(p);
00362     
00363     // hack the IP address to convert pkt format to hostid format
00364     // for now until port ids are removed from IP address. -Padma.
00365     int src = Address::instance().get_nodeaddr(ih->saddr());
00366     int dst = Address::instance().get_nodeaddr(ih->daddr());
00367 
00368     if (pt_->tagged()) {
00369         sprintf(pt_->buffer() + offset,
00370             "-IP:s %d -IP:sp %d -IP:d %d -IP:dp %d -p %s -e %d "
00371             "-c %d -i %d -IP:ttl %d ",
00372             src,                           // packet src
00373             ih->sport(),                   // src port
00374             dst,                           // packet dest
00375             ih->dport(),                   // dst port
00376             packet_info.name(ch->ptype()), // packet type
00377             ch->size(),                    // packet size
00378             ih->flowid(),                  // flow id
00379             ch->uid(),                     // unique id
00380             ih->ttl_                       // ttl
00381             );
00382     } else if (newtrace_) {
00383         sprintf(pt_->buffer() + offset,
00384             "-Is %d.%d -Id %d.%d -It %s -Il %d -If %d -Ii %d -Iv %d ",
00385             src,                           // packet src
00386             ih->sport(),                   // src port
00387             dst,                           // packet dest
00388             ih->dport(),                   // dst port
00389             packet_info.name(ch->ptype()),  // packet type
00390             ch->size(),                     // packet size
00391             ih->flowid(),                   // flow id
00392             ch->uid(),                      // unique id
00393             ih->ttl_);                      // ttl
00394     } else {
00395         sprintf(pt_->buffer() + offset, "------- [%d:%d %d:%d %d %d] ",
00396         src, ih->sport(),
00397         dst, ih->dport(),
00398         ih->ttl_, (ch->next_hop_ < 0) ? 0 : ch->next_hop_);
00399     }
00400 }
00401 
00402 // Note:  HDLC format (format_hdlc()) has moved to satellite tracing
00403 
00404 void
00405 CMUTrace::format_arp(Packet *p, int offset)
00406 {
00407     struct hdr_arp *ah = HDR_ARP(p);
00408 
00409     if (pt_->tagged()) {
00410         sprintf(pt_->buffer() + offset,
00411             "-arp:op %s -arp:ms %d -arp:s %d -arp:md %d -arp:d %d ",
00412             ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
00413             ah->arp_sha,
00414             ah->arp_spa,
00415             ah->arp_tha,
00416             ah->arp_tpa);
00417     } else if (newtrace_) {
00418         sprintf(pt_->buffer() + offset,
00419             "-P arp -Po %s -Pms %d -Ps %d -Pmd %d -Pd %d ",
00420             ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
00421             ah->arp_sha,
00422             ah->arp_spa,
00423             ah->arp_tha,
00424             ah->arp_tpa);
00425     } else {
00426 
00427         sprintf(pt_->buffer() + offset,
00428         "------- [%s %d/%d %d/%d]",
00429         ah->arp_op == ARPOP_REQUEST ?  "REQUEST" : "REPLY",
00430         ah->arp_sha,
00431         ah->arp_spa,
00432         ah->arp_tha,
00433         ah->arp_tpa);
00434     }
00435 }
00436 
00437 void
00438 CMUTrace::format_dsr(Packet *p, int offset)
00439 {
00440     hdr_sr *srh = hdr_sr::access(p);
00441 
00442     if (pt_->tagged()) {
00443         sprintf(pt_->buffer() + offset,
00444             "-dsr:h %d -dsr:q %d -dsr:s %d -dsr:p %d -dsr:n %d "
00445             "-dsr:l %d -dsr:e {%d %d} -dsr:w %d -dsr:m %d -dsr:c %d "
00446             "-dsr:b {%d %d} ",
00447             srh->num_addrs(),
00448             srh->route_request(),
00449             srh->rtreq_seq(),
00450             srh->route_reply(),
00451             srh->rtreq_seq(),
00452             srh->route_reply_len(),
00453             srh->reply_addrs()[0].addr,
00454             srh->reply_addrs()[srh->route_reply_len()-1].addr,
00455             srh->route_error(),
00456             srh->num_route_errors(),
00457             srh->down_links()[srh->num_route_errors() - 1].tell_addr,
00458             srh->down_links()[srh->num_route_errors() - 1].from_addr,
00459             srh->down_links()[srh->num_route_errors() - 1].to_addr);
00460         return;
00461     } else if (newtrace_) {
00462         sprintf(pt_->buffer() + offset, 
00463         "-P dsr -Ph %d -Pq %d -Ps %d -Pp %d -Pn %d -Pl %d -Pe %d->%d -Pw %d -Pm %d -Pc %d -Pb %d->%d ",
00464             srh->num_addrs(),                   // how many nodes travered
00465 
00466         srh->route_request(),
00467         srh->rtreq_seq(),
00468 
00469         srh->route_reply(),
00470         srh->rtreq_seq(),
00471         srh->route_reply_len(),
00472         // the dest of the src route
00473         srh->reply_addrs()[0].addr,
00474         srh->reply_addrs()[srh->route_reply_len()-1].addr,
00475 
00476         srh->route_error(),
00477         srh->num_route_errors(),
00478         srh->down_links()[srh->num_route_errors() - 1].tell_addr,
00479         srh->down_links()[srh->num_route_errors() - 1].from_addr,
00480         srh->down_links()[srh->num_route_errors() - 1].to_addr);
00481 
00482        return;
00483     }
00484     sprintf(pt_->buffer() + offset, 
00485         "%d [%d %d] [%d %d %d %d->%d] [%d %d %d %d->%d]",
00486         srh->num_addrs(),
00487 
00488         srh->route_request(),
00489         srh->rtreq_seq(),
00490 
00491         srh->route_reply(),
00492         srh->rtreq_seq(),
00493         srh->route_reply_len(),
00494         // the dest of the src route
00495         srh->reply_addrs()[0].addr,
00496         srh->reply_addrs()[srh->route_reply_len()-1].addr,
00497 
00498         srh->route_error(),
00499         srh->num_route_errors(),
00500         srh->down_links()[srh->num_route_errors() - 1].tell_addr,
00501         srh->down_links()[srh->num_route_errors() - 1].from_addr,
00502         srh->down_links()[srh->num_route_errors() - 1].to_addr);
00503 }
00504 
00505 void
00506 CMUTrace::format_msg(Packet *, int)
00507 {
00508 }
00509 
00510 void
00511 CMUTrace::format_tcp(Packet *p, int offset)
00512 {
00513     struct hdr_cmn *ch = HDR_CMN(p);
00514     struct hdr_tcp *th = HDR_TCP(p);
00515     
00516     if (pt_->tagged()) {
00517         sprintf(pt_->buffer() + offset,
00518             "-tcp:s %d -tcp:a %d -tcp:f %d -tcp:o %d ",
00519             th->seqno_,
00520             th->ackno_,
00521             ch->num_forwards(),
00522             ch->opt_num_forwards());
00523     } else if (newtrace_) {
00524         sprintf(pt_->buffer() + offset,
00525         "-Pn tcp -Ps %d -Pa %d -Pf %d -Po %d ",
00526         th->seqno_,
00527         th->ackno_,
00528         ch->num_forwards(),
00529         ch->opt_num_forwards());
00530 
00531     } else {
00532         sprintf(pt_->buffer() + offset,
00533         "[%d %d] %d %d",
00534         th->seqno_,
00535         th->ackno_,
00536         ch->num_forwards(),
00537         ch->opt_num_forwards());
00538     }
00539 }
00540 
00541 /* Armando L. Caro Jr. <acaro@@cis,udel,edu> 6/5/2002
00542  * (with help from Florina Almenárez <florina@@it,uc3m,es>)
00543  */
00544 void
00545 CMUTrace::format_sctp(Packet* p,int offset)
00546 {
00547     struct hdr_cmn *ch = HDR_CMN(p);
00548     struct hdr_sctp *sh = HDR_SCTP(p);
00549     //struct hdr_ip *ih = HDR_IP(p);
00550     char cChunkType;
00551   
00552     for(u_int i = 0; i < sh->NumChunks(); i++) {
00553         switch(sh->SctpTrace()[i].eType) {
00554         case SCTP_CHUNK_INIT:
00555         case SCTP_CHUNK_INIT_ACK:
00556         case SCTP_CHUNK_COOKIE_ECHO:
00557         case SCTP_CHUNK_COOKIE_ACK:
00558             cChunkType = 'I';       // connection initialization
00559             break;
00560       
00561         case SCTP_CHUNK_DATA:
00562             cChunkType = 'D';
00563             break;
00564       
00565         case SCTP_CHUNK_SACK:
00566             cChunkType = 'S';
00567             break;
00568       
00569         case SCTP_CHUNK_FORWARD_TSN:
00570             cChunkType = 'R';
00571             break;
00572 
00573         case SCTP_CHUNK_HB:
00574             cChunkType = 'H';
00575             break;
00576             
00577         case SCTP_CHUNK_HB_ACK:
00578             cChunkType = 'B';
00579             break;
00580         default:
00581             // quiet compiler
00582             cChunkType = ' ';
00583             assert (false);
00584             break;
00585         }
00586     
00587         if( newtrace_ ) {
00588             sprintf(pt_->buffer() + offset,
00589                 "-Pn sctp -Pnc %d -Pct %c "
00590                 "-Ptsn %d -Psid %d -Pssn %d "
00591                 "-Pf %d -Po %d ",
00592                 sh->NumChunks(),
00593                 cChunkType,
00594                 sh->SctpTrace()[i].uiTsn,
00595                 sh->SctpTrace()[i].usStreamId,
00596                 sh->SctpTrace()[i].usStreamSeqNum,
00597                 ch->num_forwards(),
00598                 ch->opt_num_forwards());
00599         }
00600         else {
00601             sprintf(pt_->buffer() + offset,
00602                 "[%d %c %d %d %d] %d %d",
00603                 sh->NumChunks(),
00604                 cChunkType,
00605                 sh->SctpTrace()[i].uiTsn,
00606                 sh->SctpTrace()[i].usStreamId,
00607                 sh->SctpTrace()[i].usStreamSeqNum,
00608                 ch->num_forwards(),
00609                 ch->opt_num_forwards());
00610         }
00611     }
00612 }
00613 
00614 void
00615 CMUTrace::format_rtp(Packet *p, int offset)
00616 {
00617     struct hdr_cmn *ch = HDR_CMN(p);
00618     struct hdr_rtp *rh = HDR_RTP(p);
00619     struct hdr_ip *ih = HDR_IP(p);
00620         Node* thisnode = Node::get_node_by_address(src_);
00621 
00622     //hacking, needs to change later, 
00623         int dst = Address::instance().get_nodeaddr(ih->daddr());
00624     
00625     if (dst == src_){
00626         // I just received a cbr data packet
00627         if (thisnode->energy_model() && 
00628             thisnode->energy_model()->powersavingflag()) {
00629             thisnode->energy_model()->set_node_state(EnergyModel::INROUTE);
00630         }
00631         }
00632 
00633     if (pt_->tagged()) {
00634         sprintf(pt_->buffer() + offset,
00635             "-cbr:s %d -cbr:f %d -cbr:o %d ",
00636             rh->seqno_,
00637             ch->num_forwards(),
00638             ch->opt_num_forwards());
00639     } else if (newtrace_) {
00640         sprintf(pt_->buffer() + offset,
00641             "-Pn cbr -Pi %d -Pf %d -Po %d ",
00642             rh->seqno_,
00643             ch->num_forwards(),
00644             ch->opt_num_forwards());
00645     } else {
00646         sprintf(pt_->buffer() + offset,
00647             "[%d] %d %d",
00648             rh->seqno_,
00649             ch->num_forwards(),
00650             ch->opt_num_forwards());
00651     }
00652 }
00653 
00654 void
00655 CMUTrace::format_imep(Packet *p, int offset)
00656 {
00657         struct hdr_imep *im = HDR_IMEP(p);
00658 
00659 #define U_INT16_T(x)    *((u_int16_t*) &(x))
00660 
00661     if (pt_->tagged()) {
00662         sprintf(pt_->buffer() + offset,
00663             "-imep:a %c -imep:h %c -imep:o %c -imep:l %04x ",
00664             (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
00665                     (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
00666                     (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
00667                     U_INT16_T(im->imep_length));
00668     } else if (newtrace_) {
00669         sprintf(pt_->buffer() + offset,
00670                 "-P imep -Pa %c -Ph %c -Po %c -Pl 0x%04x ] ",
00671                 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
00672                 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
00673                 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
00674                 U_INT16_T(im->imep_length));
00675     } else {
00676             sprintf(pt_->buffer() + offset,
00677                 "[%c %c %c 0x%04x] ",
00678                 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
00679                 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
00680                 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
00681                 U_INT16_T(im->imep_length));
00682     }
00683 #undef U_INT16_T
00684 }
00685 
00686 
00687 void
00688 CMUTrace::format_tora(Packet *p, int offset)
00689 {
00690         struct hdr_tora *th = HDR_TORA(p);
00691         struct hdr_tora_qry *qh = HDR_TORA_QRY(p);
00692         struct hdr_tora_upd *uh = HDR_TORA_UPD(p);
00693         struct hdr_tora_clr *ch = HDR_TORA_CLR(p);
00694 
00695         switch(th->th_type) {
00696 
00697         case TORATYPE_QRY:
00698 
00699         if (pt_->tagged()) {
00700             sprintf(pt_->buffer() + offset,
00701                 "-tora:t %x -tora:d %d -tora:c QUERY",
00702                 qh->tq_type, qh->tq_dst);
00703         } else if (newtrace_) {
00704             sprintf(pt_->buffer() + offset,
00705             "-P tora -Pt 0x%x -Pd %d -Pc QUERY ",
00706                         qh->tq_type, qh->tq_dst);
00707             
00708                 } else {
00709 
00710                     sprintf(pt_->buffer() + offset, "[0x%x %d] (QUERY)",
00711                         qh->tq_type, qh->tq_dst);
00712         }
00713                 break;
00714 
00715         case TORATYPE_UPD:
00716 
00717         if (pt_->tagged()) {
00718             sprintf(pt_->buffer() + offset,
00719                 "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
00720                 "-tora:r %d -tora:e %d -tora:i %d -tora:c UPDATE",
00721                 uh->tu_type,
00722                             uh->tu_dst,
00723                             uh->tu_tau,
00724                             uh->tu_oid,
00725                             uh->tu_r,
00726                             uh->tu_delta,
00727                             uh->tu_id);
00728         } else if (newtrace_) {
00729             sprintf(pt_->buffer() + offset,
00730                         "-P tora -Pt 0x%x -Pd %d (%f %d %d %d %d) -Pc UPDATE ",
00731                         uh->tu_type,
00732                         uh->tu_dst,
00733                         uh->tu_tau,
00734                         uh->tu_oid,
00735                         uh->tu_r,
00736                         uh->tu_delta,
00737                         uh->tu_id);
00738         } else {
00739                     sprintf(pt_->buffer() + offset,
00740                         "-Pt 0x%x -Pd %d -Pa %f -Po %d -Pr %d -Pe %d -Pi %d -Pc UPDATE ",
00741                         uh->tu_type,
00742                         uh->tu_dst,
00743                         uh->tu_tau,
00744                         uh->tu_oid,
00745                         uh->tu_r,
00746                         uh->tu_delta,
00747                         uh->tu_id);
00748         }
00749                 break;
00750 
00751         case TORATYPE_CLR:
00752         if (pt_->tagged()) {
00753             sprintf(pt_->buffer() + offset,
00754                 "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
00755                 "-tora:c CLEAR ",
00756                 ch->tc_type,
00757                             ch->tc_dst,
00758                             ch->tc_tau,
00759                             ch->tc_oid);
00760         } else if (newtrace_) {
00761             sprintf(pt_->buffer() + offset, 
00762             "-P tora -Pt 0x%x -Pd %d -Pa %f -Po %d -Pc CLEAR ",
00763                         ch->tc_type,
00764                         ch->tc_dst,
00765                         ch->tc_tau,
00766                         ch->tc_oid);
00767         } else {
00768                     sprintf(pt_->buffer() + offset, "[0x%x %d %f %d] (CLEAR)",
00769                         ch->tc_type,
00770                         ch->tc_dst,
00771                         ch->tc_tau,
00772                         ch->tc_oid);
00773         }
00774                 break;
00775         }
00776 }
00777 
00778 void
00779 CMUTrace::format_aodv(Packet *p, int offset)
00780 {
00781         struct hdr_aodv *ah = HDR_AODV(p);
00782         struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
00783         struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);
00784 
00785 
00786         switch(ah->ah_type) {
00787         case AODVTYPE_RREQ:
00788 
00789         if (pt_->tagged()) {
00790             sprintf(pt_->buffer() + offset,
00791                 "-aodv:t %x -aodv:h %d -aodv:b %d -aodv:d %d "
00792                 "-aodv:ds %d -aodv:s %d -aodv:ss %d "
00793                 "-aodv:c REQUEST ",
00794                 rq->rq_type,
00795                             rq->rq_hop_count,
00796                             rq->rq_bcast_id,
00797                             rq->rq_dst,
00798                             rq->rq_dst_seqno,
00799                             rq->rq_src,
00800                             rq->rq_src_seqno);
00801         } else if (newtrace_) {
00802 
00803             sprintf(pt_->buffer() + offset,
00804             "-P aodv -Pt 0x%x -Ph %d -Pb %d -Pd %d -Pds %d -Ps %d -Pss %d -Pc REQUEST ",
00805             rq->rq_type,
00806                         rq->rq_hop_count,
00807                         rq->rq_bcast_id,
00808                         rq->rq_dst,
00809                         rq->rq_dst_seqno,
00810                         rq->rq_src,
00811                         rq->rq_src_seqno);
00812 
00813 
00814         } else {
00815 
00816             sprintf(pt_->buffer() + offset,
00817             "[0x%x %d %d [%d %d] [%d %d]] (REQUEST)",
00818             rq->rq_type,
00819                         rq->rq_hop_count,
00820                         rq->rq_bcast_id,
00821                         rq->rq_dst,
00822                         rq->rq_dst_seqno,
00823                         rq->rq_src,
00824                         rq->rq_src_seqno);
00825         }
00826                 break;
00827 
00828         case AODVTYPE_RREP:
00829         case AODVTYPE_HELLO:
00830     case AODVTYPE_RERR:
00831         
00832         if (pt_->tagged()) {
00833             sprintf(pt_->buffer() + offset,
00834                 "-aodv:t %x -aodv:h %d -aodv:d %d -adov:ds %d "
00835                 "-aodv:l %f -aodv:c %s ",
00836                 rp->rp_type,
00837                 rp->rp_hop_count,
00838                 rp->rp_dst,
00839                 rp->rp_dst_seqno,
00840                 rp->rp_lifetime,
00841                 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
00842                 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
00843                  "HELLO"));
00844         } else if (newtrace_) {
00845             
00846             sprintf(pt_->buffer() + offset,
00847                 "-P aodv -Pt 0x%x -Ph %d -Pd %d -Pds %d -Pl %f -Pc %s ",
00848                 rp->rp_type,
00849                 rp->rp_hop_count,
00850                 rp->rp_dst,
00851                 rp->rp_dst_seqno,
00852                 rp->rp_lifetime,
00853                 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
00854                 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
00855                  "HELLO"));
00856             } else {
00857             
00858             sprintf(pt_->buffer() + offset,
00859                 "[0x%x %d [%d %d] %f] (%s)",
00860                 rp->rp_type,
00861                 rp->rp_hop_count,
00862                 rp->rp_dst,
00863                 rp->rp_dst_seqno,
00864                 rp->rp_lifetime,
00865                 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
00866                 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
00867                  "HELLO"));
00868         }
00869                 break;
00870         
00871         default:
00872 #ifdef WIN32
00873                 fprintf(stderr,
00874                 "CMUTrace::format_aodv: invalid AODV packet type\n");
00875 #else
00876         fprintf(stderr,
00877                 "%s: invalid AODV packet type\n", __FUNCTION__);
00878 #endif
00879                 abort();
00880         }
00881 }
00882 
00883 void
00884 CMUTrace::nam_format(Packet *p, int offset)
00885 {
00886     Node* srcnode = 0 ;
00887     Node* dstnode = 0 ;
00888     Node* nextnode = 0 ;
00889         struct hdr_cmn *ch = HDR_CMN(p);
00890     struct hdr_ip *ih = HDR_IP(p);
00891     char op = (char) type_;
00892     char colors[32];
00893     int next_hop = -1 ;
00894 
00895 // change wrt Mike's code
00896     assert(type_ != EOT);
00897 
00898 
00899 
00900     //<zheng: add for 802.15.4>
00901 
00902     //Actually we only need to handle MAC layer for nam (but should display dropping for other layers)
00903     //if (strcmp(tracename,"MAC") != 0)
00904     //if ((op != 'D')&&(op != 'd'))
00905     //  return;
00906 
00907     struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
00908     char ptype[11];
00909     strcpy(ptype,
00910     ((ch->ptype() == PT_MAC) ? (
00911       (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS"  :
00912       (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS"  :
00913       (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK"  :
00914       (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN"  :       //Beacon
00915       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1"  :  //CMD: Association request
00916       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2"  :  //CMD: Association response
00917       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3"  :  //CMD: Disassociation notification
00918       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4"  :  //CMD: Data request
00919       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5"  :  //CMD: PAN ID conflict notification
00920       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6"  :  //CMD: Orphan notification
00921       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7"  :  //CMD: Beacon request
00922       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8"  :  //CMD: Coordinator realignment
00923       (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9"  :   //CMD: GTS request
00924       "UNKN"
00925       ) : packet_info.name(ch->ptype())));
00926     //</zheng: add for 802.15.4>
00927         int dst = Address::instance().get_nodeaddr(ih->daddr());
00928 
00929     nextnode = Node::get_node_by_address(ch->next_hop_);
00930         if (nextnode) next_hop = nextnode->nodeid(); 
00931 
00932     srcnode = Node::get_node_by_address(src_);
00933     dstnode = Node::get_node_by_address(ch->next_hop_);
00934 
00935     double distance = 0;
00936 
00937         if ((srcnode) && (dstnode)) {
00938        MobileNode* tmnode = (MobileNode*)srcnode;
00939        MobileNode* rmnode = (MobileNode*)dstnode;
00940 
00941        distance = tmnode->propdelay(rmnode) * 300000000 ;
00942     }
00943 
00944     double energy = -1;
00945     double initenergy = -1;
00946 
00947     //default value for changing node color with respect to energy depletion
00948     double l1 = 0.5; 
00949     double l2 = 0.2;
00950     
00951     if (srcnode) {
00952         if (srcnode->energy_model()) {
00953             energy = srcnode->energy_model()->energy();
00954             initenergy = srcnode->energy_model()->initialenergy();
00955             l1 = srcnode->energy_model()->level1();
00956             l2 = srcnode->energy_model()->level2();
00957         }
00958     }
00959 
00960         int energyLevel = 0 ;
00961         double energyLeft = (double)(energy/initenergy) ;
00962 
00963         if ((energyLeft <= 1 ) && (energyLeft >= l1 )) energyLevel = 3; 
00964         if ((energyLeft >= l2 ) && (energyLeft < l1 )) energyLevel = 2; 
00965         if ((energyLeft > 0 ) && (energyLeft < l2 )) energyLevel = 1;   
00966 
00967     if (energyLevel == 0) 
00968         strcpy(colors,"-c black -o red");
00969         else if (energyLevel == 1) 
00970         strcpy(colors,"-c red -o yellow");
00971         else if (energyLevel == 2) 
00972         strcpy(colors,"-c yellow -o green");
00973         else if (energyLevel == 3) 
00974         strcpy(colors,"-c green -o black");
00975 
00976     // A simple hack for scadds demo (fernandez's visit) -- Chalermek
00977     int pkt_color = 0;
00978     if (ch->ptype()==PT_DIFF) {
00979         hdr_cdiff *dfh= HDR_CDIFF(p);
00980         if (dfh->mess_type != DATA) {
00981             pkt_color = 1;
00982         }
00983     }
00984 
00985     //<zheng: add for 802.15.4>
00986     if (Nam802_15_4::Nam_Status)
00987     {
00988         ATTRIBUTELINK *attr;
00989         int t_src,t_dst;
00990         if (ch->ptype() == PT_MAC)
00991         {
00992             t_src = p802_15_4macSA(p);
00993             t_dst = p802_15_4macDA(p);;
00994         }
00995         else
00996         {
00997             t_src = HDR_IP(p)->saddr();
00998             t_dst = HDR_IP(p)->daddr();
00999         }
01000         attr = findAttrLink(HDR_CMN(p)->ptype(),t_src,t_dst);
01001         if (attr == NULL)
01002             attr = findAttrLink(HDR_CMN(p)->ptype());
01003         if (attr != NULL)
01004             HDR_LRWPAN(p)->attribute = attr->attribute;
01005         else
01006             HDR_LRWPAN(p)->attribute = 0;
01007         if (HDR_LRWPAN(p)->attribute >= 32)
01008             pkt_color = HDR_LRWPAN(p)->attribute;
01009     }
01010     //</zheng: add for 802.15.4>
01011 
01012     // convert to nam format 
01013     if (op == 's') op = 'h' ;
01014     if (op == 'D') op = 'd' ;
01015     if (op == 'h') {
01016         sprintf(pt_->nbuffer(),
01017             "+ -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s ",
01018             Scheduler::instance().clock(),
01019             src_,                           // this node
01020             next_hop,
01021             ptype,          //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
01022             ch->size(),
01023             pkt_color,   
01024             ch->uid(),
01025             tracename);
01026 
01027         offset = strlen(pt_->nbuffer());
01028         pt_->namdump();
01029         sprintf(pt_->nbuffer() ,
01030             "- -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
01031             Scheduler::instance().clock(),
01032             src_,                           // this node
01033             next_hop,
01034             ptype,          //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
01035             ch->size(),
01036             pkt_color,
01037             ch->uid(),
01038             tracename);
01039         
01040         offset = strlen(pt_->nbuffer());
01041         pt_->namdump();
01042     }
01043 
01044         // if nodes are too far from each other
01045     // nam won't dump SEND event 'cuz it's
01046     // gonna be dropped later anyway
01047     // this value 250 is pre-calculated by using 
01048     // two-ray ground refelction model with fixed
01049     // transmission power 3.652e-10
01050 //  if ((type_ == SEND)  && (distance > 250 )) return ;
01051 
01052     if(tracetype == TR_ROUTER && type_ == RECV && dst != -1 ) return ;
01053     if(type_ == RECV && dst == -1 )dst = src_ ; //broadcasting event
01054 
01055         if (energy != -1) { //energy model being turned on
01056        if (src_ >= MAX_NODE) {
01057            fprintf (stderr, "node id must be < %d\n",
01058                 MAX_NODE);
01059            exit(0);
01060        }
01061        if (nodeColor[src_] != energyLevel ) { //only dump it when node  
01062            sprintf(pt_->nbuffer() ,                    //color change
01063               "n -t %.9f -s %d -S COLOR %s",
01064                Scheduler::instance().clock(),
01065                src_,                           // this node
01066                colors);
01067                offset = strlen(pt_->nbuffer());
01068                pt_->namdump();
01069            nodeColor[src_] = energyLevel ;
01070         }   
01071         }
01072 
01073     sprintf(pt_->nbuffer() ,
01074         "%c -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
01075         op,
01076         Scheduler::instance().clock(),
01077         src_,                           // this node
01078         next_hop,
01079         ptype,          //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
01080         ch->size(),
01081         pkt_color,
01082         ch->uid(),
01083         tracename);
01084 
01085 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
01086 if (Nam802_15_4::Nam_Status)
01087 {
01088     if ((strcmp(tracename, "AGT") != 0) || ((u_int32_t)(ih->daddr()) == IP_BROADCAST))      //<zheng: add: next_hop info not available at agent level>
01089                                             //(doesn't really matter -- seems agent level has no effect on nam)
01090     if (next_hop == -1 && op == 'h') {
01091         // print extra fields for broadcast packets
01092 
01093         // bradius is calculated assuming 2-ray ground reflectlon
01094         // model using default settings of Phy/WirelessPhy and
01095         // Antenna/OmniAntenna
01096         if (bradius == 0.0) calculate_broadcast_parameters();
01097 
01098         double radius = bradius*radius_scaling_factor_; 
01099 
01100         // duration is calculated based on the radius and
01101         // the speed of light (299792458 m/s)
01102         double duration = (bradius/299792458.0)*duration_scaling_factor_;
01103         //<zheng: add -- the duration in 802.15.4 could be very small and rounded to 0.0>
01104         if (Nam802_15_4::Nam_Status)
01105         if (duration < 0.000000001)
01106             duration = 0.000000001;
01107         //</zheng: add>
01108         sprintf(pt_->nbuffer() + strlen(pt_->nbuffer()),
01109             " -R %.2f -D %.2f",
01110             radius,
01111             duration);
01112     }
01113 }
01114 //</zheng>
01115 
01116     offset = strlen(pt_->nbuffer());
01117     pt_->namdump();
01118 }
01119 
01120 void CMUTrace::format(Packet* p, const char *why)
01121 {
01122     hdr_cmn *ch = HDR_CMN(p);
01123     int offset = 0;
01124 
01125     /*
01126      * Log the MAC Header
01127      */
01128     format_mac_common(p, why, offset);
01129 
01130     if (pt_->namchannel()) 
01131         nam_format(p, offset);
01132     offset = strlen(pt_->buffer());
01133     switch(ch->ptype()) {
01134     case PT_MAC:
01135     case PT_SMAC:
01136         break;
01137     case PT_ARP:
01138         format_arp(p, offset);
01139         break;
01140     default:
01141         format_ip(p, offset);
01142         offset = strlen(pt_->buffer());
01143         switch(ch->ptype()) {
01144         case PT_AODV:
01145             format_aodv(p, offset);
01146             break;
01147         case PT_TORA:
01148                         format_tora(p, offset);
01149                         break;
01150                 case PT_IMEP:
01151                         format_imep(p, offset);
01152                         break;
01153         case PT_DSR:
01154             format_dsr(p, offset);
01155             break;
01156         case PT_MESSAGE:
01157         case PT_UDP:
01158             format_msg(p, offset);
01159             break;
01160         case PT_TCP:
01161         case PT_ACK:
01162             format_tcp(p, offset);
01163             break;
01164         case PT_SCTP:
01165             /* Armando L. Caro Jr. <acaro@@cis,udel,edu> 6/5/2002
01166              */
01167             format_sctp(p, offset);
01168             break;
01169         case PT_CBR:
01170             format_rtp(p, offset);
01171             break;
01172             case PT_DIFF:
01173             break;
01174         case PT_GAF:
01175         case PT_PING:
01176             break;
01177         default:
01178         /*<zheng: del -- there are many more new packet types added, like PT_EXP (poisson traffic belongs to this type)>
01179             fprintf(stderr, "%s - invalid packet type (%s).\n",
01180                 __PRETTY_FUNCTION__, packet_info.name(ch->ptype()));
01181             exit(1);
01182         </zheng: del>*/
01183             break;      //zheng: add
01184         }
01185     }
01186 }
01187 
01188 int
01189 CMUTrace::command(int argc, const char*const* argv)
01190 {
01191     
01192         if(argc == 3) {
01193                 if(strcmp(argv[1], "node") == 0) {
01194                         node_ = (MobileNode*) TclObject::lookup(argv[2]);
01195                         if(node_ == 0)
01196                                 return TCL_ERROR;
01197                         return TCL_OK;
01198                 }
01199         if (strcmp(argv[1], "newtrace") == 0) {
01200             newtrace_ = atoi(argv[2]);
01201                 return TCL_OK;
01202         }
01203         }
01204     return Trace::command(argc, argv);
01205 }
01206 
01207 /*ARGSUSED*/
01208 void
01209 CMUTrace::recv(Packet *p, Handler *h)
01210 {
01211     if (!node_energy()) {
01212         Packet::free(p);
01213         return;
01214     }
01215         assert(initialized());
01216         /*
01217          * Agent Trace "stamp" the packet with the optimal route on
01218          * sending.
01219          */
01220         if (tracetype == TR_AGENT && type_ == SEND) {
01221                 God::instance()->stampPacket(p);
01222         }
01223 #if 0
01224         /*
01225          * When the originator of a packet drops the packet, it may or may
01226          * not have been stamped by GOD.  Stamp it before logging the
01227          * information.
01228          */
01229         if(src_ == src && type_ == DROP) {
01230                 God::instance()->stampPacket(p);
01231         }
01232 #endif
01233     format(p, "---");
01234     pt_->dump();
01235     //namdump();
01236     if(target_ == 0)
01237         Packet::free(p);
01238     else
01239         send(p, h);
01240 }
01241 
01242 void
01243 CMUTrace::recv(Packet *p, const char* why)
01244 {
01245         assert(initialized() && type_ == DROP);
01246     if (!node_energy()) {
01247         Packet::free(p);
01248         return;
01249     }
01250 #if 0
01251         /*
01252          * When the originator of a packet drops the packet, it may or may
01253          * not have been stamped by GOD.  Stamp it before logging the
01254          * information.
01255          */
01256         if(src_ == ih->saddr()) {
01257                 God::instance()->stampPacket(p);
01258         }
01259 #endif
01260     format(p, why);
01261     pt_->dump();
01262     //namdump();
01263     Packet::free(p);
01264 }
01265 
01266 int CMUTrace::node_energy()
01267 {
01268     Node* thisnode = Node::get_node_by_address(src_);
01269     double energy = 1;
01270     if (thisnode) {
01271         if (thisnode->energy_model()) {
01272             energy = thisnode->energy_model()->energy();
01273         }
01274     } 
01275     if (energy > 0) return 1;
01276     return 0;
01277 }
01278 
01279 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
01280 void CMUTrace::calculate_broadcast_parameters() {
01281     // Calculate the maximum distance at which a packet can be received
01282     // based on the two-ray reflection model using the current default
01283     // values for Phy/WirelessPhy and Antenna/OmniAntenna.
01284 
01285     double P_t, P_r, G_t, G_r, h, L;
01286     Tcl& tcl = Tcl::instance();
01287 
01288     tcl.evalc("Phy/WirelessPhy set Pt_");
01289     P_t = atof(tcl.result());
01290     tcl.evalc("Phy/WirelessPhy set RXThresh_");
01291     P_r = atof(tcl.result());
01292     tcl.evalc("Phy/WirelessPhy set L_");
01293     L = atof(tcl.result());
01294     tcl.evalc("Antenna/OmniAntenna set Gt_");
01295     G_t = atof(tcl.result());
01296     tcl.evalc("Antenna/OmniAntenna set Gr_");
01297     G_r = atof(tcl.result());
01298     tcl.evalc("Antenna/OmniAntenna set Z_");
01299     h = atof(tcl.result());
01300     bradius = pow(P_t*G_r*G_t*pow(h,4.0)/(P_r*L), 0.25);
01301     //<zheng: add for 802.15.4>
01302     //the above calculation is not accurate for short distance
01303     double PI,freq,lambda,crossover_dist;
01304     PI = 3.14159265359;
01305     tcl.evalc("Phy/WirelessPhy set freq_");
01306     freq = atof(tcl.result());
01307     lambda = 3.0e8/freq;
01308     crossover_dist = (4 * PI * h * h) / lambda;
01309     if (bradius < crossover_dist)   //need re-calculation
01310         bradius = pow(P_t * G_r * G_t * pow(lambda, 2.0)/(P_r * L), 0.5)/(4 * PI);
01311     //</zheng: add for 802.15.4>
01312 
01313     // Also get the scaling factors
01314     tcl.evalc("CMUTrace set radius_scaling_factor_");
01315     radius_scaling_factor_ = atof(tcl.result());
01316     tcl.evalc("CMUTrace set duration_scaling_factor_");
01317     duration_scaling_factor_ = atof(tcl.result());
01318 }
01319 //</zheng>

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