Debian Codegraph @

protolib/src/sim/ns/ns233/cmu-trace.cc

raw
1 /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
2 /*
3 * Copyright (c) 1997 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * Ported from CMU/Monarch's code, appropriate copyright applies.
35 * nov'98 -Padma.
36 *
37 * $Header: /cvsroot/nsnam/ns-2/trace/cmu-trace.cc,v 1.92 2008/02/18 03:39:03 tom_henderson Exp $
38 */
39
40 #include <packet.h>
41 #include <ip.h>
42 #include <tcp.h>
43 #include <sctp.h>
44 #include <rtp.h>
45 #include <arp.h>
46 #include <dsr/hdr_sr.h> // DSR
47 #include <mac.h>
48 #include <mac-802_11.h>
49 #include <smac.h>
50 #include <address.h>
51 #include <tora/tora_packet.h> //TORA
52 #include <imep/imep_spec.h> // IMEP
53 #include <aodv/aodv_packet.h> //AODV
54 #include <nsProtoManetKernel.h>
55 #include <cmu-trace.h>
56 #include <mobilenode.h>
57 #include <simulator.h>
58 //<zheng: add for 802.15.4>
59 #include "wpan/p802_15_4pkt.h"
60 #include "wpan/p802_15_4trace.h"
61 #include "wpan/p802_15_4nam.h"
62 //</zheng: add for 802.15.4>
63
64 #include "diffusion/diff_header.h" // DIFFUSION -- Chalermek
65
66
67 PacketTracer::PacketTracer() : next_(0)
68 {
69 }
70 void PacketTracer::setNext(PacketTracer *next)
71 {
72 next_ = next;
73 }
74
75 PacketTracer::~PacketTracer()
76 {
77 }
78
79 PacketTracer *PacketTracer::getNext()
80 {
81 return next_;
82 }
83
84 int PacketTracer::format_unknow(Packet *p, int offset, BaseTrace *pt, int newtrace)
85 {
86 return (format(p, offset, pt, newtrace) || (next_ && next_->format_unknow(p, offset, pt, newtrace)));
87 }
88
89 PacketTracer *CMUTrace::pktTrc_ = 0;
90
91 void CMUTrace::addPacketTracer(PacketTracer *pt)
92 {
93 if(!pt)
94 return;
95 pt->setNext(pktTrc_);
96 pktTrc_ = pt;
97 }
98
99
100 //#define LOG_POSITION
101
102 //extern char* pt_names[];
103
104 static class CMUTraceClass : public TclClass {
105 public:
106 CMUTraceClass() : TclClass("CMUTrace") { }
107 TclObject* create(int, const char*const* argv) {
108 return (new CMUTrace(argv[4], *argv[5]));
109 }
110 } cmutrace_class;
111
112
113 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
114 double CMUTrace::bradius = 0.0;
115 double CMUTrace::radius_scaling_factor_ = 0.0;
116 double CMUTrace::duration_scaling_factor_ = 0.0;
117 //</zheng>
118
119 CMUTrace::CMUTrace(const char *s, char t) : Trace(t)
120 {
121 bzero(tracename, sizeof(tracename));
122 strncpy(tracename, s, MAX_ID_LEN);
123
124 if(strcmp(tracename, "RTR") == 0) {
125 tracetype = TR_ROUTER;
126 }
127 else if(strcmp(tracename, "TRP") == 0) {
128 tracetype = TR_ROUTER;
129 }
130 else if(strcmp(tracename, "PHY") == 0) {
131 tracetype = TR_PHY;
132 }
133 else if(strcmp(tracename, "MAC") == 0) {
134 tracetype = TR_MAC;
135 }
136 else if(strcmp(tracename, "IFQ") == 0) {
137 tracetype = TR_IFQ;
138 }
139 else if(strcmp(tracename, "AGT") == 0) {
140 tracetype = TR_AGENT;
141 }
142 else {
143 fprintf(stderr, "CMU Trace Initialized with invalid type\n");
144 exit(1);
145 }
146 // change wrt Mike's code
147 // assert(type_ == DROP || type_ == SEND || type_ == RECV);
148 assert(type_ == DROP || type_ == SEND || type_ == RECV
149 || ((type_ == EOT) && (tracetype == TR_MAC)));
150
151
152
153 newtrace_ = 0;
154 for (int i=0 ; i < MAX_NODE ; i++)
155 nodeColor[i] = 3 ;
156 node_ = 0;
157 }
158
159 void
160 CMUTrace::format_mac_common(Packet *p, const char *why, int offset)
161 {
162 struct hdr_cmn *ch = HDR_CMN(p);
163 struct hdr_ip *ih = HDR_IP(p);
164 struct hdr_mac802_11 *mh;
165 struct hdr_smac *sh;
166 char mactype[SMALL_LEN];
167
168 strcpy(mactype, Simulator::instance().macType());
169 if (strcmp (mactype, "Mac/SMAC") == 0)
170 sh = HDR_SMAC(p);
171 else
172 mh = HDR_MAC802_11(p);
173
174 double x = 0.0, y = 0.0, z = 0.0;
175
176 char op = (char) type_;
177 Node* thisnode = Node::get_node_by_address(src_);
178 double energy = -1;
179 if (thisnode) {
180 if (thisnode->energy_model()) {
181 energy = thisnode->energy_model()->energy();
182 }
183 }
184
185 // hack the IP address to convert pkt format to hostid format
186 // for now until port ids are removed from IP address. -Padma.
187
188 int src = Address::instance().get_nodeaddr(ih->saddr());
189
190 if(tracetype == TR_ROUTER && type_ == SEND) {
191 if(src_ != src)
192 op = FWRD;
193 }
194
195 // use tagged format if appropriate
196 if (pt_->tagged()) {
197 int next_hop = -1 ;
198 Node* nextnode = Node::get_node_by_address(ch->next_hop_);
199 if (nextnode) next_hop = nextnode->nodeid();
200
201 node_->getLoc(&x, &y, &z);
202
203 if (op == DROP) op = 'd';
204 if (op == SEND) op = '+';
205 if (op == FWRD) op = 'h';
206
207 sprintf(pt_->buffer() + offset,
208 "%c "TIME_FORMAT" -s %d -d %d -p %s -k %3s -i %d "
209 "-N:loc {%.2f %.2f %.2f} -N:en %f ",
210
211 op, // event type
212 Scheduler::instance().clock(), // time
213 src_, // this node
214 next_hop, // next hop
215 packet_info.name(ch->ptype()), // packet type
216 tracename, // trace level
217 ch->uid(), // event id
218 x, y, z, // location
219 energy); // energy
220
221 offset = strlen(pt_->buffer());
222 if (strcmp (mactype, "Mac/SMAC") == 0) {
223 format_smac(p, offset);
224 } else {
225 format_mac(p, offset);
226 }
227 return;
228 }
229
230
231 // Use new ns trace format to replace the old cmu trace format)
232 if (newtrace_) {
233
234 node_->getLoc(&x, &y, &z);
235 // consistence
236 if ( op == DROP ) { op = 'd';}
237
238 // basic trace infomation + basic exenstion
239
240 sprintf(pt_->buffer() + offset,
241 "%c -t %.9f -Hs %d -Hd %d -Ni %d -Nx %.2f -Ny %.2f -Nz %.2f -Ne %f -Nl %3s -Nw %s ",
242 op, // event type
243 Scheduler::instance().clock(), // time
244 src_, // this node
245 ch->next_hop_, // next hop
246 src_, // this node
247 x, // x coordinate
248 y, // y coordinate
249 z, // z coordinate
250 energy, // energy, -1 = not existing
251 tracename, // trace level
252 why); // reason
253
254 // mac layer extension
255
256 offset = strlen(pt_->buffer());
257 if (strcmp(mactype, "Mac/SMAC") == 0) {
258 format_smac(p, offset);
259 } else {
260 format_mac(p, offset);
261 }
262 return;
263 }
264
265
266 #ifdef LOG_POSITION
267 x = 0.0, y = 0.0, z = 0.0;
268 node_->getLoc(&x, &y, &z);
269 #endif
270 sprintf(pt_->buffer() + offset,
271 #ifdef LOG_POSITION
272 "%c %.9f %d (%6.2f %6.2f) %3s %4s %d %s %d ",
273 #else
274 "%c %.9f _%d_ %3s %4s %d %s %d",
275 #endif
276 op,
277 Scheduler::instance().clock(),
278 src_, // this node
279 #ifdef LOG_POSITION
280 x,
281 y,
282 #endif
283 tracename,
284 why,
285
286 ch->uid(), // identifier for this event
287
288 ((ch->ptype() == PT_MAC) ? (
289 (mh->dh_fc.fc_type == MAC_Type_Control) ? (
290 (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS" :
291 (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS" :
292 (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK":
293 //<zheng: add for 802.15.4>
294 (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN" : //Beacon
295 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1" : //CMD: Association request
296 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2" : //CMD: Association response
297 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3" : //CMD: Disassociation notification
298 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4" : //CMD: Data request
299 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5" : //CMD: PAN ID conflict notification
300 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6" : //CMD: Orphan notification
301 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7" : //CMD: Beacon request
302 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8" : //CMD: Coordinator realignment
303 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9" : //CMD: GTS request
304 "UNKN") :
305 (mh->dh_fc.fc_type == MAC_Type_Management) ? (
306 (mh->dh_fc.fc_subtype == MAC_Subtype_80211_Beacon) ? "BCN" :
307 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocReq) ? "ACRQ" :
308 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocRep) ? "ACRP" :
309 (mh->dh_fc.fc_subtype == MAC_Subtype_Auth) ? "AUTH" :
310 (mh->dh_fc.fc_subtype == MAC_Subtype_ProbeReq) ? "PRRQ" :
311 (mh->dh_fc.fc_subtype == MAC_Subtype_ProbeRep) ? "PRRP" :
312 "UNKN") :
313 "UNKN") :
314 (ch->ptype() == PT_SMAC) ? (
315 (sh->type == RTS_PKT) ? "RTS" :
316 (sh->type == CTS_PKT) ? "CTS" :
317 (sh->type == ACK_PKT) ? "ACK" :
318 (sh->type == SYNC_PKT) ? "SYNC" :
319 "UNKN") :
320 packet_info.name(ch->ptype())),
321 ch->size());
322
323 offset = strlen(pt_->buffer());
324
325 if(tracetype == TR_PHY) {
326 format_phy(p, offset);
327 offset = strlen(pt_->buffer());
328 return;
329 }
330
331 if (strncmp (mactype, "Mac/SMAC", 8) == 0) {
332 format_smac(p, offset);
333 } else {
334 format_mac(p, offset);
335 }
336
337 offset = strlen(pt_->buffer());
338
339 if (thisnode) {
340 if (thisnode->energy_model()) {
341 // log detailed energy consumption
342 // total energy and breakdown in idle, sleep, transmit and receive modes
343 sprintf(pt_->buffer() + offset,
344 "[energy %f ei %.3f es %.3f et %.3f er %.3f] ",
345 thisnode->energy_model()->energy(),
346 thisnode->energy_model()->ei(),
347 thisnode->energy_model()->es(),
348 thisnode->energy_model()->et(),
349 thisnode->energy_model()->er());
350 }
351 }
352 }
353
354 void
355 CMUTrace::format_phy(Packet *p, int offset)
356 {
357 sprintf(pt_->buffer() + offset, " ");
358 }
359
360
361 void
362 CMUTrace::format_mac(Packet *p, int offset)
363 {
364 struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
365
366 if (pt_->tagged()) {
367 sprintf(pt_->buffer() + offset,
368 "-M:dur %x -M:s %x -M:d %x -M:t %x ",
369 mh->dh_duration, // MAC: duration
370
371 // change wrt Mike's code
372 //ETHER_ADDR(mh->dh_da), // MAC: source
373 //ETHER_ADDR(mh->dh_sa), // MAC: destination
374 ETHER_ADDR(mh->dh_ra), // MAC: source
375 ETHER_ADDR(mh->dh_ta), // MAC: destination
376
377
378 GET_ETHER_TYPE(mh->dh_body)); // MAC: type
379 } else if (newtrace_) {
380 sprintf(pt_->buffer() + offset,
381 "-Ma %x -Md %x -Ms %x -Mt %x ",
382 mh->dh_duration,
383
384 // change wrt Mike's code
385 //ETHER_ADDR(mh->dh_da),
386 //ETHER_ADDR(mh->dh_sa),
387
388 ETHER_ADDR(mh->dh_ra),
389 ETHER_ADDR(mh->dh_ta),
390
391 GET_ETHER_TYPE(mh->dh_body));
392 } else {
393 sprintf(pt_->buffer() + offset,
394 " [%x %x %x %x] ",
395 //*((u_int16_t*) &mh->dh_fc),
396 mh->dh_duration,
397
398 // change wrt Mike's code
399 //ETHER_ADDR(mh->dh_da),
400 //ETHER_ADDR(mh->dh_sa),
401 ETHER_ADDR(mh->dh_ra),
402 ETHER_ADDR(mh->dh_ta),
403
404
405 GET_ETHER_TYPE(mh->dh_body));
406 }
407 }
408
409 void
410 CMUTrace::format_smac(Packet *p, int offset)
411 {
412 struct hdr_smac *sh = HDR_SMAC(p);
413 sprintf(pt_->buffer() + offset,
414 " [%.2f %d %d] ",
415 sh->duration,
416 sh->dstAddr,
417 sh->srcAddr);
418 }
419
420
421 void
422 CMUTrace::format_ip(Packet *p, int offset)
423 {
424 struct hdr_cmn *ch = HDR_CMN(p);
425 struct hdr_ip *ih = HDR_IP(p);
426
427 // hack the IP address to convert pkt format to hostid format
428 // for now until port ids are removed from IP address. -Padma.
429 int src = Address::instance().get_nodeaddr(ih->saddr());
430 int dst = Address::instance().get_nodeaddr(ih->daddr());
431
432 if (pt_->tagged()) {
433 sprintf(pt_->buffer() + offset,
434 "-IP:s %d -IP:sp %d -IP:d %d -IP:dp %d -p %s -e %d "
435 "-c %d -i %d -IP:ttl %d ",
436 src, // packet src
437 ih->sport(), // src port
438 dst, // packet dest
439 ih->dport(), // dst port
440 packet_info.name(ch->ptype()), // packet type
441 ch->size(), // packet size
442 ih->flowid(), // flow id
443 ch->uid(), // unique id
444 ih->ttl_ // ttl
445 );
446 } else if (newtrace_) {
447 sprintf(pt_->buffer() + offset,
448 "-Is %d.%d -Id %d.%d -It %s -Il %d -If %d -Ii %d -Iv %d ",
449 src, // packet src
450 ih->sport(), // src port
451 dst, // packet dest
452 ih->dport(), // dst port
453 packet_info.name(ch->ptype()), // packet type
454 ch->size(), // packet size
455 ih->flowid(), // flow id
456 ch->uid(), // unique id
457 ih->ttl_); // ttl
458 } else {
459 sprintf(pt_->buffer() + offset, "------- [%d:%d %d:%d %d %d] ",
460 src, ih->sport(),
461 dst, ih->dport(),
462 ih->ttl_, (ch->next_hop_ < 0) ? 0 : ch->next_hop_);
463 }
464 }
465
466 // Note: HDLC format (format_hdlc()) has moved to satellite tracing
467
468 void
469 CMUTrace::format_arp(Packet *p, int offset)
470 {
471 struct hdr_arp *ah = HDR_ARP(p);
472
473 if (pt_->tagged()) {
474 sprintf(pt_->buffer() + offset,
475 "-arp:op %s -arp:ms %d -arp:s %d -arp:md %d -arp:d %d ",
476 ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY",
477 ah->arp_sha,
478 ah->arp_spa,
479 ah->arp_tha,
480 ah->arp_tpa);
481 } else if (newtrace_) {
482 sprintf(pt_->buffer() + offset,
483 "-P arp -Po %s -Pms %d -Ps %d -Pmd %d -Pd %d ",
484 ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY",
485 ah->arp_sha,
486 ah->arp_spa,
487 ah->arp_tha,
488 ah->arp_tpa);
489 } else {
490
491 sprintf(pt_->buffer() + offset,
492 "------- [%s %d/%d %d/%d]",
493 ah->arp_op == ARPOP_REQUEST ? "REQUEST" : "REPLY",
494 ah->arp_sha,
495 ah->arp_spa,
496 ah->arp_tha,
497 ah->arp_tpa);
498 }
499 }
500
501 void
502 CMUTrace::format_dsr(Packet *p, int offset)
503 {
504 hdr_sr *srh = hdr_sr::access(p);
505
506 if (pt_->tagged()) {
507 sprintf(pt_->buffer() + offset,
508 "-dsr:h %d -dsr:q %d -dsr:s %d -dsr:p %d -dsr:n %d "
509 "-dsr:l %d -dsr:e {%d %d} -dsr:w %d -dsr:m %d -dsr:c %d "
510 "-dsr:b {%d %d} ",
511 srh->num_addrs(),
512 srh->route_request(),
513 srh->rtreq_seq(),
514 srh->route_reply(),
515 srh->rtreq_seq(),
516 srh->route_reply_len(),
517 srh->reply_addrs()[0].addr,
518 srh->reply_addrs()[srh->route_reply_len()-1].addr,
519 srh->route_error(),
520 srh->num_route_errors(),
521 srh->down_links()[srh->num_route_errors() - 1].tell_addr,
522 srh->down_links()[srh->num_route_errors() - 1].from_addr,
523 srh->down_links()[srh->num_route_errors() - 1].to_addr);
524 return;
525 } else if (newtrace_) {
526 sprintf(pt_->buffer() + offset,
527 "-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 ",
528 srh->num_addrs(), // how many nodes travered
529
530 srh->route_request(),
531 srh->rtreq_seq(),
532
533 srh->route_reply(),
534 srh->rtreq_seq(),
535 srh->route_reply_len(),
536 // the dest of the src route
537 srh->reply_addrs()[0].addr,
538 srh->reply_addrs()[srh->route_reply_len()-1].addr,
539
540 srh->route_error(),
541 srh->num_route_errors(),
542 srh->down_links()[srh->num_route_errors() - 1].tell_addr,
543 srh->down_links()[srh->num_route_errors() - 1].from_addr,
544 srh->down_links()[srh->num_route_errors() - 1].to_addr);
545
546 return;
547 }
548 sprintf(pt_->buffer() + offset,
549 "%d [%d %d] [%d %d %d %d->%d] [%d %d %d %d->%d]",
550 srh->num_addrs(),
551
552 srh->route_request(),
553 srh->rtreq_seq(),
554
555 srh->route_reply(),
556 srh->rtreq_seq(),
557 srh->route_reply_len(),
558 // the dest of the src route
559 srh->reply_addrs()[0].addr,
560 srh->reply_addrs()[srh->route_reply_len()-1].addr,
561
562 srh->route_error(),
563 srh->num_route_errors(),
564 srh->down_links()[srh->num_route_errors() - 1].tell_addr,
565 srh->down_links()[srh->num_route_errors() - 1].from_addr,
566 srh->down_links()[srh->num_route_errors() - 1].to_addr);
567 }
568
569 void
570 CMUTrace::format_msg(Packet *, int)
571 {
572 }
573
574 void
575 CMUTrace::format_tcp(Packet *p, int offset)
576 {
577 struct hdr_cmn *ch = HDR_CMN(p);
578 struct hdr_tcp *th = HDR_TCP(p);
579
580 if (pt_->tagged()) {
581 sprintf(pt_->buffer() + offset,
582 "-tcp:s %d -tcp:a %d -tcp:f %d -tcp:o %d ",
583 th->seqno_,
584 th->ackno_,
585 ch->num_forwards(),
586 ch->opt_num_forwards());
587 } else if (newtrace_) {
588 sprintf(pt_->buffer() + offset,
589 "-Pn tcp -Ps %d -Pa %d -Pf %d -Po %d ",
590 th->seqno_,
591 th->ackno_,
592 ch->num_forwards(),
593 ch->opt_num_forwards());
594
595 } else {
596 sprintf(pt_->buffer() + offset,
597 "[%d %d] %d %d",
598 th->seqno_,
599 th->ackno_,
600 ch->num_forwards(),
601 ch->opt_num_forwards());
602 }
603 }
604
605 /* Armando L. Caro Jr. <acaro@@cis,udel,edu> 6/5/2002
606 * (with help from Florina Almenrez <florina@@it,uc3m,es>)
607 */
608 void
609 CMUTrace::format_sctp(Packet* p,int offset)
610 {
611 struct hdr_cmn *ch = HDR_CMN(p);
612 struct hdr_sctp *sh = HDR_SCTP(p);
613 //struct hdr_ip *ih = HDR_IP(p);
614 char cChunkType;
615
616 for(u_int i = 0; i < sh->NumChunks(); i++) {
617 switch(sh->SctpTrace()[i].eType) {
618 case SCTP_CHUNK_INIT:
619 case SCTP_CHUNK_INIT_ACK:
620 case SCTP_CHUNK_COOKIE_ECHO:
621 case SCTP_CHUNK_COOKIE_ACK:
622 cChunkType = 'I'; // connection initialization
623 break;
624
625 case SCTP_CHUNK_DATA:
626 cChunkType = 'D';
627 break;
628
629 case SCTP_CHUNK_SACK:
630 cChunkType = 'S';
631 break;
632
633 case SCTP_CHUNK_FORWARD_TSN:
634 cChunkType = 'R';
635 break;
636
637 case SCTP_CHUNK_HB:
638 cChunkType = 'H';
639 break;
640
641 case SCTP_CHUNK_HB_ACK:
642 cChunkType = 'B';
643 break;
644 default:
645 // quiet compiler
646 cChunkType = ' ';
647 assert (false);
648 break;
649 }
650
651 if( newtrace_ ) {
652 sprintf(pt_->buffer() + offset,
653 "-Pn sctp -Pnc %d -Pct %c "
654 "-Ptsn %d -Psid %d -Pssn %d "
655 "-Pf %d -Po %d ",
656 sh->NumChunks(),
657 cChunkType,
658 sh->SctpTrace()[i].uiTsn,
659 sh->SctpTrace()[i].usStreamId,
660 sh->SctpTrace()[i].usStreamSeqNum,
661 ch->num_forwards(),
662 ch->opt_num_forwards());
663 }
664 else {
665 sprintf(pt_->buffer() + offset,
666 "[%d %c %d %d %d] %d %d",
667 sh->NumChunks(),
668 cChunkType,
669 sh->SctpTrace()[i].uiTsn,
670 sh->SctpTrace()[i].usStreamId,
671 sh->SctpTrace()[i].usStreamSeqNum,
672 ch->num_forwards(),
673 ch->opt_num_forwards());
674 }
675 }
676 }
677
678 void
679 CMUTrace::format_rtp(Packet *p, int offset)
680 {
681 struct hdr_cmn *ch = HDR_CMN(p);
682 struct hdr_rtp *rh = HDR_RTP(p);
683 struct hdr_ip *ih = HDR_IP(p);
684 Node* thisnode = Node::get_node_by_address(src_);
685
686 //hacking, needs to change later,
687 int dst = Address::instance().get_nodeaddr(ih->daddr());
688
689 if (dst == src_){
690 // I just received a cbr data packet
691 if (thisnode->energy_model() &&
692 thisnode->energy_model()->powersavingflag()) {
693 thisnode->energy_model()->set_node_state(EnergyModel::INROUTE);
694 }
695 }
696
697 if (pt_->tagged()) {
698 sprintf(pt_->buffer() + offset,
699 "-cbr:s %d -cbr:f %d -cbr:o %d ",
700 rh->seqno_,
701 ch->num_forwards(),
702 ch->opt_num_forwards());
703 } else if (newtrace_) {
704 sprintf(pt_->buffer() + offset,
705 "-Pn cbr -Pi %d -Pf %d -Po %d ",
706 rh->seqno_,
707 ch->num_forwards(),
708 ch->opt_num_forwards());
709 } else {
710 sprintf(pt_->buffer() + offset,
711 "[%d] %d %d",
712 rh->seqno_,
713 ch->num_forwards(),
714 ch->opt_num_forwards());
715 }
716 }
717
718 void
719 CMUTrace::format_imep(Packet *p, int offset)
720 {
721 struct hdr_imep *im = HDR_IMEP(p);
722
723 #define U_INT16_T(x) *((u_int16_t*) &(x))
724
725 if (pt_->tagged()) {
726 sprintf(pt_->buffer() + offset,
727 "-imep:a %c -imep:h %c -imep:o %c -imep:l %04x ",
728 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
729 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
730 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
731 U_INT16_T(im->imep_length));
732 } else if (newtrace_) {
733 sprintf(pt_->buffer() + offset,
734 "-P imep -Pa %c -Ph %c -Po %c -Pl 0x%04x ] ",
735 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
736 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
737 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
738 U_INT16_T(im->imep_length));
739 } else {
740 sprintf(pt_->buffer() + offset,
741 "[%c %c %c 0x%04x] ",
742 (im->imep_block_flags & BLOCK_FLAG_ACK) ? 'A' : '-',
743 (im->imep_block_flags & BLOCK_FLAG_HELLO) ? 'H' : '-',
744 (im->imep_block_flags & BLOCK_FLAG_OBJECT) ? 'O' : '-',
745 U_INT16_T(im->imep_length));
746 }
747 #undef U_INT16_T
748 }
749
750
751 void
752 CMUTrace::format_tora(Packet *p, int offset)
753 {
754 struct hdr_tora *th = HDR_TORA(p);
755 struct hdr_tora_qry *qh = HDR_TORA_QRY(p);
756 struct hdr_tora_upd *uh = HDR_TORA_UPD(p);
757 struct hdr_tora_clr *ch = HDR_TORA_CLR(p);
758
759 switch(th->th_type) {
760
761 case TORATYPE_QRY:
762
763 if (pt_->tagged()) {
764 sprintf(pt_->buffer() + offset,
765 "-tora:t %x -tora:d %d -tora:c QUERY",
766 qh->tq_type, qh->tq_dst);
767 } else if (newtrace_) {
768 sprintf(pt_->buffer() + offset,
769 "-P tora -Pt 0x%x -Pd %d -Pc QUERY ",
770 qh->tq_type, qh->tq_dst);
771
772 } else {
773
774 sprintf(pt_->buffer() + offset, "[0x%x %d] (QUERY)",
775 qh->tq_type, qh->tq_dst);
776 }
777 break;
778
779 case TORATYPE_UPD:
780
781 if (pt_->tagged()) {
782 sprintf(pt_->buffer() + offset,
783 "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
784 "-tora:r %d -tora:e %d -tora:i %d -tora:c UPDATE",
785 uh->tu_type,
786 uh->tu_dst,
787 uh->tu_tau,
788 uh->tu_oid,
789 uh->tu_r,
790 uh->tu_delta,
791 uh->tu_id);
792 } else if (newtrace_) {
793 sprintf(pt_->buffer() + offset,
794 "-P tora -Pt 0x%x -Pd %d (%f %d %d %d %d) -Pc UPDATE ",
795 uh->tu_type,
796 uh->tu_dst,
797 uh->tu_tau,
798 uh->tu_oid,
799 uh->tu_r,
800 uh->tu_delta,
801 uh->tu_id);
802 } else {
803 sprintf(pt_->buffer() + offset,
804 "-Pt 0x%x -Pd %d -Pa %f -Po %d -Pr %d -Pe %d -Pi %d -Pc UPDATE ",
805 uh->tu_type,
806 uh->tu_dst,
807 uh->tu_tau,
808 uh->tu_oid,
809 uh->tu_r,
810 uh->tu_delta,
811 uh->tu_id);
812 }
813 break;
814
815 case TORATYPE_CLR:
816 if (pt_->tagged()) {
817 sprintf(pt_->buffer() + offset,
818 "-tora:t %x -tora:d %d -tora:a %f -tora:o %d "
819 "-tora:c CLEAR ",
820 ch->tc_type,
821 ch->tc_dst,
822 ch->tc_tau,
823 ch->tc_oid);
824 } else if (newtrace_) {
825 sprintf(pt_->buffer() + offset,
826 "-P tora -Pt 0x%x -Pd %d -Pa %f -Po %d -Pc CLEAR ",
827 ch->tc_type,
828 ch->tc_dst,
829 ch->tc_tau,
830 ch->tc_oid);
831 } else {
832 sprintf(pt_->buffer() + offset, "[0x%x %d %f %d] (CLEAR)",
833 ch->tc_type,
834 ch->tc_dst,
835 ch->tc_tau,
836 ch->tc_oid);
837 }
838 break;
839 }
840 }
841
842 void
843 CMUTrace::format_aodv(Packet *p, int offset)
844 {
845 struct hdr_aodv *ah = HDR_AODV(p);
846 struct hdr_aodv_request *rq = HDR_AODV_REQUEST(p);
847 struct hdr_aodv_reply *rp = HDR_AODV_REPLY(p);
848
849
850 switch(ah->ah_type) {
851 case AODVTYPE_RREQ:
852
853 if (pt_->tagged()) {
854 sprintf(pt_->buffer() + offset,
855 "-aodv:t %x -aodv:h %d -aodv:b %d -aodv:d %d "
856 "-aodv:ds %d -aodv:s %d -aodv:ss %d "
857 "-aodv:c REQUEST ",
858 rq->rq_type,
859 rq->rq_hop_count,
860 rq->rq_bcast_id,
861 rq->rq_dst,
862 rq->rq_dst_seqno,
863 rq->rq_src,
864 rq->rq_src_seqno);
865 } else if (newtrace_) {
866
867 sprintf(pt_->buffer() + offset,
868 "-P aodv -Pt 0x%x -Ph %d -Pb %d -Pd %d -Pds %d -Ps %d -Pss %d -Pc REQUEST ",
869 rq->rq_type,
870 rq->rq_hop_count,
871 rq->rq_bcast_id,
872 rq->rq_dst,
873 rq->rq_dst_seqno,
874 rq->rq_src,
875 rq->rq_src_seqno);
876
877
878 } else {
879
880 sprintf(pt_->buffer() + offset,
881 "[0x%x %d %d [%d %d] [%d %d]] (REQUEST)",
882 rq->rq_type,
883 rq->rq_hop_count,
884 rq->rq_bcast_id,
885 rq->rq_dst,
886 rq->rq_dst_seqno,
887 rq->rq_src,
888 rq->rq_src_seqno);
889 }
890 break;
891
892 case AODVTYPE_RREP:
893 case AODVTYPE_HELLO:
894 case AODVTYPE_RERR:
895
896 if (pt_->tagged()) {
897 sprintf(pt_->buffer() + offset,
898 "-aodv:t %x -aodv:h %d -aodv:d %d -adov:ds %d "
899 "-aodv:l %f -aodv:c %s ",
900 rp->rp_type,
901 rp->rp_hop_count,
902 rp->rp_dst,
903 rp->rp_dst_seqno,
904 rp->rp_lifetime,
905 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
906 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
907 "HELLO"));
908 } else if (newtrace_) {
909
910 sprintf(pt_->buffer() + offset,
911 "-P aodv -Pt 0x%x -Ph %d -Pd %d -Pds %d -Pl %f -Pc %s ",
912 rp->rp_type,
913 rp->rp_hop_count,
914 rp->rp_dst,
915 rp->rp_dst_seqno,
916 rp->rp_lifetime,
917 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
918 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
919 "HELLO"));
920 } else {
921
922 sprintf(pt_->buffer() + offset,
923 "[0x%x %d [%d %d] %f] (%s)",
924 rp->rp_type,
925 rp->rp_hop_count,
926 rp->rp_dst,
927 rp->rp_dst_seqno,
928 rp->rp_lifetime,
929 rp->rp_type == AODVTYPE_RREP ? "REPLY" :
930 (rp->rp_type == AODVTYPE_RERR ? "ERROR" :
931 "HELLO"));
932 }
933 break;
934
935 default:
936 #ifdef WIN32
937 fprintf(stderr,
938 "CMUTrace::format_aodv: invalid AODV packet type\n");
939 #else
940 fprintf(stderr,
941 "%s: invalid AODV packet type\n", __FUNCTION__);
942 #endif
943 abort();
944 }
945 }
946
947 void
948 CMUTrace::format_protolibmk(Packet *p, int offset) {return;}
949
950 void
951 CMUTrace::nam_format(Packet *p, int offset)
952 {
953 Node* srcnode = 0 ;
954 Node* dstnode = 0 ;
955 Node* nextnode = 0 ;
956 struct hdr_cmn *ch = HDR_CMN(p);
957 struct hdr_ip *ih = HDR_IP(p);
958 char op = (char) type_;
959 char colors[32];
960 int next_hop = -1 ;
961
962 // change wrt Mike's code
963 assert(type_ != EOT);
964
965
966
967 //<zheng: add for 802.15.4>
968
969 //Actually we only need to handle MAC layer for nam (but should display dropping for other layers)
970 //if (strcmp(tracename,"MAC") != 0)
971 //if ((op != 'D')&&(op != 'd'))
972 // return;
973
974 struct hdr_mac802_11 *mh = HDR_MAC802_11(p);
975 char ptype[11];
976 strcpy(ptype,
977 ((ch->ptype() == PT_MAC) ? (
978 (mh->dh_fc.fc_subtype == MAC_Subtype_RTS) ? "RTS" :
979 (mh->dh_fc.fc_subtype == MAC_Subtype_CTS) ? "CTS" :
980 (mh->dh_fc.fc_subtype == MAC_Subtype_ACK) ? "ACK" :
981 (mh->dh_fc.fc_subtype == MAC_Subtype_Beacon) ? "BCN" : //Beacon
982 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocReq) ? "ACRQ" :
983 (mh->dh_fc.fc_subtype == MAC_Subtype_AssocRep) ? "ACRP" :
984 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoReq) ? "CM1" : //CMD: Association request
985 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_AssoRsp) ? "CM2" : //CMD: Association response
986 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DAssNtf) ? "CM3" : //CMD: Disassociation notification
987 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_DataReq) ? "CM4" : //CMD: Data request
988 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_PIDCNtf) ? "CM5" : //CMD: PAN ID conflict notification
989 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_OrphNtf) ? "CM6" : //CMD: Orphan notification
990 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_BconReq) ? "CM7" : //CMD: Beacon request
991 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_CoorRea) ? "CM8" : //CMD: Coordinator realignment
992 (mh->dh_fc.fc_subtype == MAC_Subtype_Command_GTSReq) ? "CM9" : //CMD: GTS request
993 "UNKN"
994 ) : packet_info.name(ch->ptype())));
995 //</zheng: add for 802.15.4>
996 int dst = Address::instance().get_nodeaddr(ih->daddr());
997
998 nextnode = Node::get_node_by_address(ch->next_hop_);
999 if (nextnode) next_hop = nextnode->nodeid();
1000
1001 srcnode = Node::get_node_by_address(src_);
1002 dstnode = Node::get_node_by_address(ch->next_hop_);
1003
1004 double distance = 0;
1005
1006 if ((srcnode) && (dstnode)) {
1007 MobileNode* tmnode = (MobileNode*)srcnode;
1008 MobileNode* rmnode = (MobileNode*)dstnode;
1009
1010 distance = tmnode->propdelay(rmnode) * 300000000 ;
1011 }
1012
1013 double energy = -1;
1014 double initenergy = -1;
1015
1016 //default value for changing node color with respect to energy depletion
1017 double l1 = 0.5;
1018 double l2 = 0.2;
1019
1020 if (srcnode) {
1021 if (srcnode->energy_model()) {
1022 energy = srcnode->energy_model()->energy();
1023 initenergy = srcnode->energy_model()->initialenergy();
1024 l1 = srcnode->energy_model()->level1();
1025 l2 = srcnode->energy_model()->level2();
1026 }
1027 }
1028
1029 int energyLevel = 0 ;
1030 double energyLeft = (double)(energy/initenergy) ;
1031
1032 if ((energyLeft <= 1 ) && (energyLeft >= l1 )) energyLevel = 3;
1033 if ((energyLeft >= l2 ) && (energyLeft < l1 )) energyLevel = 2;
1034 if ((energyLeft > 0 ) && (energyLeft < l2 )) energyLevel = 1;
1035
1036 if (energyLevel == 0)
1037 strcpy(colors,"-c black -o red");
1038 else if (energyLevel == 1)
1039 strcpy(colors,"-c red -o yellow");
1040 else if (energyLevel == 2)
1041 strcpy(colors,"-c yellow -o green");
1042 else if (energyLevel == 3)
1043 strcpy(colors,"-c green -o black");
1044
1045 // A simple hack for scadds demo (fernandez's visit) -- Chalermek
1046 int pkt_color = 0;
1047 if (ch->ptype()==PT_DIFF) {
1048 hdr_cdiff *dfh= HDR_CDIFF(p);
1049 if (dfh->mess_type != DATA) {
1050 pkt_color = 1;
1051 }
1052 }
1053
1054 //<zheng: add for 802.15.4>
1055 if (Nam802_15_4::Nam_Status)
1056 {
1057 ATTRIBUTELINK *attr;
1058 int t_src,t_dst;
1059 if (ch->ptype() == PT_MAC)
1060 {
1061 t_src = p802_15_4macSA(p);
1062 t_dst = p802_15_4macDA(p);;
1063 }
1064 else
1065 {
1066 t_src = HDR_IP(p)->saddr();
1067 t_dst = HDR_IP(p)->daddr();
1068 }
1069 attr = findAttrLink(HDR_CMN(p)->ptype(),t_src,t_dst);
1070 if (attr == NULL)
1071 attr = findAttrLink(HDR_CMN(p)->ptype());
1072 if (attr != NULL)
1073 HDR_LRWPAN(p)->attribute = attr->attribute;
1074 else
1075 HDR_LRWPAN(p)->attribute = 0;
1076 if (HDR_LRWPAN(p)->attribute >= 32)
1077 pkt_color = HDR_LRWPAN(p)->attribute;
1078 }
1079 //</zheng: add for 802.15.4>
1080
1081 // convert to nam format
1082 if (op == 's') op = 'h' ;
1083 if (op == 'D') op = 'd' ;
1084 if (op == 'h') {
1085 sprintf(pt_->nbuffer(),
1086 "+ -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s ",
1087 Scheduler::instance().clock(),
1088 src_, // this node
1089 next_hop,
1090 ptype, //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
1091 ch->size(),
1092 pkt_color,
1093 ch->uid(),
1094 tracename);
1095
1096 offset = strlen(pt_->nbuffer());
1097 pt_->namdump();
1098 sprintf(pt_->nbuffer() ,
1099 "- -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
1100 Scheduler::instance().clock(),
1101 src_, // this node
1102 next_hop,
1103 ptype, //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
1104 ch->size(),
1105 pkt_color,
1106 ch->uid(),
1107 tracename);
1108
1109 offset = strlen(pt_->nbuffer());
1110 pt_->namdump();
1111 }
1112
1113 // if nodes are too far from each other
1114 // nam won't dump SEND event 'cuz it's
1115 // gonna be dropped later anyway
1116 // this value 250 is pre-calculated by using
1117 // two-ray ground refelction model with fixed
1118 // transmission power 3.652e-10
1119 // if ((type_ == SEND) && (distance > 250 )) return ;
1120
1121 if(tracetype == TR_ROUTER && type_ == RECV && dst != -1 ) return ;
1122 if(type_ == RECV && dst == -1 )dst = src_ ; //broadcasting event
1123
1124 if (energy != -1) { //energy model being turned on
1125 if (src_ >= MAX_NODE) {
1126 fprintf (stderr, "node id must be < %d\n",
1127 MAX_NODE);
1128 exit(0);
1129 }
1130 if (nodeColor[src_] != energyLevel ) { //only dump it when node
1131 sprintf(pt_->nbuffer() , //color change
1132 "n -t %.9f -s %d -S COLOR %s",
1133 Scheduler::instance().clock(),
1134 src_, // this node
1135 colors);
1136 offset = strlen(pt_->nbuffer());
1137 pt_->namdump();
1138 nodeColor[src_] = energyLevel ;
1139 }
1140 }
1141
1142 sprintf(pt_->nbuffer() ,
1143 "%c -t %.9f -s %d -d %d -p %s -e %d -c 2 -a %d -i %d -k %3s",
1144 op,
1145 Scheduler::instance().clock(),
1146 src_, // this node
1147 next_hop,
1148 ptype, //<zheng: modify for 802.15.4>packet_info.name(ch->ptype()),
1149 ch->size(),
1150 pkt_color,
1151 ch->uid(),
1152 tracename);
1153
1154 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
1155 if (Nam802_15_4::Nam_Status)
1156 {
1157 if ((strcmp(tracename, "AGT") != 0) || ((u_int32_t)(ih->daddr()) == IP_BROADCAST)) //<zheng: add: next_hop info not available at agent level>
1158 //(doesn't really matter -- seems agent level has no effect on nam)
1159 if (next_hop == -1 && op == 'h') {
1160 // print extra fields for broadcast packets
1161
1162 // bradius is calculated assuming 2-ray ground reflectlon
1163 // model using default settings of Phy/WirelessPhy and
1164 // Antenna/OmniAntenna
1165 if (bradius == 0.0) calculate_broadcast_parameters();
1166
1167 double radius = bradius*radius_scaling_factor_;
1168
1169 // duration is calculated based on the radius and
1170 // the speed of light (299792458 m/s)
1171 double duration = (bradius/299792458.0)*duration_scaling_factor_;
1172 //<zheng: add -- the duration in 802.15.4 could be very small and rounded to 0.0>
1173 if (Nam802_15_4::Nam_Status)
1174 if (duration < 0.000000001)
1175 duration = 0.000000001;
1176 //</zheng: add>
1177 sprintf(pt_->nbuffer() + strlen(pt_->nbuffer()),
1178 " -R %.2f -D %.2f",
1179 radius,
1180 duration);
1181 }
1182 }
1183 //</zheng>
1184
1185 offset = strlen(pt_->nbuffer());
1186 pt_->namdump();
1187 }
1188
1189 void CMUTrace::format(Packet* p, const char *why)
1190 {
1191 hdr_cmn *ch = HDR_CMN(p);
1192 int offset = 0;
1193
1194 /*
1195 * Log the MAC Header
1196 */
1197 format_mac_common(p, why, offset);
1198
1199 if (pt_->namchannel())
1200 nam_format(p, offset);
1201 offset = strlen(pt_->buffer());
1202 switch(ch->ptype()) {
1203 case PT_MAC:
1204 case PT_SMAC:
1205 break;
1206 case PT_ARP:
1207 format_arp(p, offset);
1208 break;
1209 default:
1210 format_ip(p, offset);
1211 offset = strlen(pt_->buffer());
1212 switch(ch->ptype()) {
1213 case PT_PROTOLIBMK:
1214 format_protolibmk(p,offset);
1215 break;
1216 case PT_AODV:
1217 format_aodv(p, offset);
1218 break;
1219 case PT_TORA:
1220 format_tora(p, offset);
1221 break;
1222 case PT_IMEP:
1223 format_imep(p, offset);
1224 break;
1225 case PT_DSR:
1226 format_dsr(p, offset);
1227 break;
1228 case PT_MESSAGE:
1229 case PT_UDP:
1230 format_msg(p, offset);
1231 break;
1232 case PT_TCP:
1233 case PT_ACK:
1234 format_tcp(p, offset);
1235 break;
1236 case PT_SCTP:
1237 /* Armando L. Caro Jr. <acaro@@cis,udel,edu> 6/5/2002
1238 */
1239 format_sctp(p, offset);
1240 break;
1241 case PT_CBR:
1242 format_rtp(p, offset);
1243 break;
1244 case PT_DIFF:
1245 break;
1246 case PT_GAF:
1247 case PT_PING:
1248 break;
1249 default:
1250
1251 if(pktTrc_ && pktTrc_->format_unknow(p, offset, pt_, newtrace_))
1252 break;
1253
1254 /*<zheng: del -- there are many more new packet types added, like PT_EXP (poisson traffic belongs to this type)>
1255 fprintf(stderr, "%s - invalid packet type (%s).\n",
1256 __PRETTY_FUNCTION__, packet_info.name(ch->ptype()));
1257 exit(1);
1258 </zheng: del>*/
1259 break; //zheng: add
1260 }
1261 }
1262 }
1263
1264 int
1265 CMUTrace::command(int argc, const char*const* argv)
1266 {
1267
1268 if(argc == 3) {
1269 if(strcmp(argv[1], "node") == 0) {
1270 node_ = (MobileNode*) TclObject::lookup(argv[2]);
1271 if(node_ == 0)
1272 return TCL_ERROR;
1273 return TCL_OK;
1274 }
1275 if (strcmp(argv[1], "newtrace") == 0) {
1276 newtrace_ = atoi(argv[2]);
1277 return TCL_OK;
1278 }
1279 }
1280 return Trace::command(argc, argv);
1281 }
1282
1283 /*ARGSUSED*/
1284 void
1285 CMUTrace::recv(Packet *p, Handler *h)
1286 {
1287 if (!node_energy()) {
1288 Packet::free(p);
1289 return;
1290 }
1291 assert(initialized());
1292 /*
1293 * Agent Trace "stamp" the packet with the optimal route on
1294 * sending.
1295 */
1296 if (tracetype == TR_AGENT && type_ == SEND) {
1297 God::instance()->stampPacket(p);
1298 }
1299 #if 0
1300 /*
1301 * When the originator of a packet drops the packet, it may or may
1302 * not have been stamped by GOD. Stamp it before logging the
1303 * information.
1304 */
1305 if(src_ == src && type_ == DROP) {
1306 God::instance()->stampPacket(p);
1307 }
1308 #endif
1309 format(p, "---");
1310 pt_->dump();
1311 //namdump();
1312 if(target_ == 0)
1313 Packet::free(p);
1314 else
1315 send(p, h);
1316 }
1317
1318 void
1319 CMUTrace::recv(Packet *p, const char* why)
1320 {
1321 assert(initialized() && type_ == DROP);
1322 if (!node_energy()) {
1323 Packet::free(p);
1324 return;
1325 }
1326 #if 0
1327 /*
1328 * When the originator of a packet drops the packet, it may or may
1329 * not have been stamped by GOD. Stamp it before logging the
1330 * information.
1331 */
1332 if(src_ == ih->saddr()) {
1333 God::instance()->stampPacket(p);
1334 }
1335 #endif
1336 format(p, why);
1337 pt_->dump();
1338 //namdump();
1339 Packet::free(p);
1340 }
1341
1342 int CMUTrace::node_energy()
1343 {
1344 Node* thisnode = Node::get_node_by_address(src_);
1345 double energy = 1;
1346 if (thisnode) {
1347 if (thisnode->energy_model()) {
1348 energy = thisnode->energy_model()->energy();
1349 }
1350 }
1351 if (energy > 0) return 1;
1352 return 0;
1353 }
1354
1355 //<zheng: ns 2.27 removed the following part, but we need it to control the broadcast radius>
1356 void CMUTrace::calculate_broadcast_parameters() {
1357 // Calculate the maximum distance at which a packet can be received
1358 // based on the two-ray reflection model using the current default
1359 // values for Phy/WirelessPhy and Antenna/OmniAntenna.
1360
1361 double P_t, P_r, G_t, G_r, h, L;
1362 Tcl& tcl = Tcl::instance();
1363
1364 tcl.evalc("Phy/WirelessPhy set Pt_");
1365 P_t = atof(tcl.result());
1366 tcl.evalc("Phy/WirelessPhy set RXThresh_");
1367 P_r = atof(tcl.result());
1368 tcl.evalc("Phy/WirelessPhy set L_");
1369 L = atof(tcl.result());
1370 tcl.evalc("Antenna/OmniAntenna set Gt_");
1371 G_t = atof(tcl.result());
1372 tcl.evalc("Antenna/OmniAntenna set Gr_");
1373 G_r = atof(tcl.result());
1374 tcl.evalc("Antenna/OmniAntenna set Z_");
1375 h = atof(tcl.result());
1376 bradius = pow(P_t*G_r*G_t*pow(h,4.0)/(P_r*L), 0.25);
1377 //<zheng: add for 802.15.4>
1378 //the above calculation is not accurate for short distance
1379 double PI,freq,lambda,crossover_dist;
1380 PI = 3.14159265359;
1381 tcl.evalc("Phy/WirelessPhy set freq_");
1382 freq = atof(tcl.result());
1383 lambda = 3.0e8/freq;
1384 crossover_dist = (4 * PI * h * h) / lambda;
1385 if (bradius < crossover_dist) //need re-calculation
1386 bradius = pow(P_t * G_r * G_t * pow(lambda, 2.0)/(P_r * L), 0.5)/(4 * PI);
1387 //</zheng: add for 802.15.4>
1388
1389 // Also get the scaling factors
1390 tcl.evalc("CMUTrace set radius_scaling_factor_");
1391 radius_scaling_factor_ = atof(tcl.result());
1392 tcl.evalc("CMUTrace set duration_scaling_factor_");
1393 duration_scaling_factor_ = atof(tcl.result());
1394 }
1395 //</zheng>