-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmulti-bss.cc
3197 lines (2899 loc) · 120 KB
/
multi-bss.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "multi-bss.h"
#include "tgax-residential-propagation-loss-model.h"
#include "ns3/ai-module.h"
#include "ns3/ampdu-subframe-header.h"
#include "ns3/ap-wifi-mac.h"
#include "ns3/application-container.h"
#include "ns3/boolean.h"
#include "ns3/buildings-helper.h"
#include "ns3/buildings-module.h"
#include "ns3/burst-sink-helper.h"
#include "ns3/bursty-helper.h"
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/core-module.h"
#include "ns3/ctrl-headers.h"
#include "ns3/double.h"
#include "ns3/enum.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/frame-exchange-manager.h"
#include "ns3/he-configuration.h"
#include "ns3/he-phy.h"
#include "ns3/ht-phy.h"
#include "ns3/integer.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/ipv4-flow-classifier.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/log.h"
#include "ns3/mobility-helper.h"
#include "ns3/mobility-module.h"
#include "ns3/multi-model-spectrum-channel.h"
#include "ns3/node-list.h"
#include "ns3/on-off-helper.h"
#include "ns3/packet-sink-helper.h"
#include "ns3/packet-sink.h"
#include "ns3/packet-socket-client.h"
#include "ns3/packet-socket-helper.h"
#include "ns3/packet-socket-server.h"
#include "ns3/pointer.h"
#include "ns3/qos-txop.h"
#include "ns3/queue-item.h"
#include "ns3/queue-size.h"
#include "ns3/rng-seed-manager.h"
#include "ns3/seq-ts-size-frag-header.h"
#include "ns3/spectrum-wifi-helper.h"
#include "ns3/spectrum-wifi-phy.h"
#include "ns3/ssid.h"
#include "ns3/sta-wifi-mac.h"
#include "ns3/string.h"
#include "ns3/threshold-preamble-detection-model.h"
#include "ns3/trace-file-burst-generator.h"
#include "ns3/traffic-control-helper.h"
#include "ns3/traffic-control-layer.h"
#include "ns3/udp-client-server-helper.h"
#include "ns3/uinteger.h"
// #include "ns3/v4ping-helper.h"
#include "ns3/wifi-acknowledgment.h"
#include "ns3/wifi-mac-queue.h"
#include "ns3/wifi-net-device.h"
#include "ns3/wifi-psdu.h"
#include "ns3/yans-wifi-channel.h"
#include "ns3/yans-wifi-helper.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <fstream>
#include <map>
#include <sstream>
#include <unordered_map>
#include <vector>
/// Avoid std::numbers::pi because it's C++20
#define PI 3.1415926535
#define N_BSS 4
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("multi-bss");
Ptr<UniformRandomVariable> randomX = CreateObject<UniformRandomVariable>();
Ptr<UniformRandomVariable> randomY = CreateObject<UniformRandomVariable>();
double distance = 0.001; ///< The distance in meters between the AP and the STAs
bool drlCca = false;
double duration = 0;
int totalDropsByOverlap = 0;
uint8_t boxSize = 10;
std::map<int, std::string> configuration;
uint32_t pktSize = 1500; ///< packet size used for the simulation (in bytes)
uint8_t maxMpdus = 0; ///< The maximum number of MPDUs in A-MPDUs (0 to disable MPDU aggregation)
std::string appType("constant");
std::map<Mac48Address, Time>
timeFirstReceived; ///< Map that stores the time at which the first packet was received per AP
///< (and the packet is addressed to that AP)
std::map<Mac48Address, Time>
timeLastReceived; ///< Map that stores the time at which the last packet was received per AP
///< (and the packet is addressed to that AP)
std::map<Mac48Address, uint64_t> packetsReceived; ///< Map that stores the total packets received
///< per AP (and addressed to that AP)
std::map<Mac48Address, uint64_t>
bytesReceived; ///< Map that stores the total bytes received per AP (and addressed to that AP)
std::map<uint32_t, uint64_t> intervalBytesReceived;
std::map<uint32_t, std::vector<double>> intervalEdcaHolSample;
std::map<uint32_t, std::vector<double>> edcaHolSample;
uint32_t networkSize;
NetDeviceContainer apDevices;
NetDeviceContainer staDevices;
NetDeviceContainer devices;
NodeContainer wifiNodes;
NodeContainer apNodes;
NodeContainer staNodes;
int apNodeCount = 0;
double txPower; ///< The transmit power of all the nodes in dBm
double ccaSensitivity;
std::string propagationModel = "";
std::map<uint32_t, std::vector<double>> nodeCw;
std::map<uint32_t, std::vector<double>> nodeBackoff;
std::map<uint64_t, int> dataRateToMcs;
std::map<uint32_t, int> nodeMcs;
std::vector<std::string>
csv_split(std::string source, char delimeter)
{
std::vector<std::string> ret;
std::string word = "";
// int start = 0;
bool inQuote = false;
for (uint32_t i = 0; i < source.size(); ++i)
{
if (!inQuote && source[i] == '"')
{
inQuote = true;
continue;
}
if (inQuote && source[i] == '"')
{
if (source.size() > i && source[i + 1] == '"')
{
++i;
}
else
{
inQuote = false;
continue;
}
}
if (!inQuote && source[i] == delimeter)
{
ret.push_back(word);
word = "";
}
else
{
word += source[i];
}
}
ret.push_back(word);
return ret;
}
// Function object to compute the hash of a MAC address
struct MacAddressHash
{
std::size_t operator()(const Mac48Address& address) const;
};
std::size_t
MacAddressHash::operator()(const Mac48Address& address) const
{
uint8_t buffer[6];
address.CopyTo(buffer);
std::string s(buffer, buffer + 6);
return std::hash<std::string>{}(s);
}
std::unordered_map<Mac48Address, uint32_t, MacAddressHash> m_staMacAddressToNodeId;
const std::map<AcIndex, std::string> m_aciToString = {
{AC_BE, "BE"},
{AC_BK, "BK"},
{AC_VI, "VI"},
{AC_VO, "VO"},
};
struct InFlightPacketInfo
{
Mac48Address m_srcAddress;
Mac48Address m_dstAddress;
AcIndex m_ac;
Ptr<const Packet> m_ptrToPacket;
Time m_edcaEnqueueTime{Seconds(0)}; // time the packet was enqueued into an EDCA queue
Time m_edcaDequeueTime{Seconds(0)}; // time the packet was dequeued from EDCA queue
Time appTypeTxTime{Seconds(0)}; // time the packet was created by app (E2E)
Time appTypeRxTime{Seconds(0)}; // time the packet was received by app (E2E)
Time m_HoLTime{Seconds(0)}; // time the packet became Head of Line
Time m_L2RxTime{Seconds(0)}; // time packet got forwarded up to the Mac Layer (L2 latency =
// L2RxTime - edcaEnqueueTime)
Time m_phyTxTime{Seconds(0)}; // time packet began transmission
bool m_dequeued{false};
};
std::unordered_map<uint64_t /* UID */, std::list<InFlightPacketInfo>> m_inFlightPacketMap;
uint32_t
MacAddressToNodeId(Mac48Address address)
{
for (uint32_t i = 0; i < apDevices.GetN(); i++)
{
if (apDevices.Get(i)->GetAddress() == address)
{
return apNodes.Get(i)->GetId();
}
}
auto it = m_staMacAddressToNodeId.find(address);
if (it != m_staMacAddressToNodeId.end())
{
return it->second;
}
NS_ABORT_MSG("Found no node having MAC address " << address);
}
void
NotifyEdcaEnqueue(Ptr<const WifiMpdu> item)
{
if (!item->GetHeader().IsQosData())
{
return;
}
// std::cout << "Enqueue UID " << item->GetPacket()->GetUid() << " " << Simulator::Now()
// << std::endl;
// init a map entry if the packet's UID is not present
auto mapIt =
m_inFlightPacketMap.insert({item->GetPacket()->GetUid(), std::list<InFlightPacketInfo>()})
.first;
InFlightPacketInfo info;
info.m_srcAddress = item->GetHeader().GetAddr2();
info.m_dstAddress = item->GetHeader().GetAddr1();
info.m_ptrToPacket = item->GetPacket();
info.m_edcaEnqueueTime = Simulator::Now();
mapIt->second.insert(mapIt->second.end(), info);
}
void
NotifyAppTx(Ptr<const Packet> packet, const Address& address)
{
// std::cout << "App Tx UID " << packet->GetUid() << std::endl;
Ptr<const Packet> p = packet;
auto mapIt = m_inFlightPacketMap.find(p->GetUid());
if (mapIt == m_inFlightPacketMap.end())
{
// std::cout << "No packet with UID " << p->GetUid() << " is currently in queue" <<
// std::endl;
return;
}
auto listIt =
std::find_if(mapIt->second.begin(),
mapIt->second.end(),
[&p](const InFlightPacketInfo& info) { return info.m_ptrToPacket == p; });
if (listIt == mapIt->second.end())
{
// std::cout << "Forwarding up a packet that has not been enqueued?" << std::endl;
return;
}
InFlightPacketInfo info;
info.m_srcAddress = listIt->m_srcAddress;
info.m_dstAddress = listIt->m_dstAddress;
info.m_ptrToPacket = packet;
info.m_edcaEnqueueTime = listIt->m_edcaEnqueueTime;
info.m_edcaDequeueTime = listIt->m_edcaDequeueTime;
info.appTypeTxTime = Simulator::Now();
mapIt->second.erase(listIt);
mapIt->second.insert(mapIt->second.end(), info);
}
/**
* Parse context strings of the form "/NodeList/x/DeviceList/x/..." to extract the NodeId
* integer
*
* \param context The context to parse.
* \return the NodeId
*/
uint32_t
ContextToNodeId(std::string context)
{
std::string sub = context.substr(10);
uint32_t pos = sub.find("/Device");
return std::stoi(sub.substr(0, pos));
}
std::unordered_map<uint32_t, std::map<uint64_t, std::vector<Time>>> nodePacketTxTime;
std::unordered_map<uint32_t, std::map<uint64_t, std::vector<Time>>> nodePacketTxEndTime;
int totalTx = 0;
void
NotifyPhyTxBegin(std::string context, Ptr<const Packet> p, double txPowerW)
{
totalTx += 1;
nodePacketTxTime[ContextToNodeId(context)][p->GetUid()].push_back(Simulator::Now());
auto mapIt = m_inFlightPacketMap.find(p->GetUid());
if (mapIt == m_inFlightPacketMap.end())
{
return;
}
auto listIt = std::find_if(mapIt->second.begin(),
mapIt->second.end(),
[&p](const InFlightPacketInfo& info) {
return info.m_ptrToPacket->GetUid() == p->GetUid();
});
if (listIt == mapIt->second.end())
{
// std::cout << "Forwarding up a packet that has not been enqueued?" << std::endl;
return;
}
InFlightPacketInfo info;
info.m_srcAddress = listIt->m_srcAddress;
info.m_dstAddress = listIt->m_dstAddress;
info.m_ptrToPacket = listIt->m_ptrToPacket;
info.m_edcaEnqueueTime = listIt->m_edcaEnqueueTime;
info.appTypeTxTime = listIt->appTypeTxTime;
info.m_phyTxTime = Simulator::Now();
mapIt->second.erase(listIt);
mapIt->second.insert(mapIt->second.end(), info);
}
/**
* PHY TX end trace.
*
* \param context The context.
* \param p The packet.
*/
void
PhyTxDoneTrace(std::string context, Ptr<const Packet> p)
{
nodePacketTxEndTime[ContextToNodeId(context)][p->GetUid()].push_back(Simulator::Now());
}
/**
* PHY TX drop trace.
*
* \param context The context.
* \param p The packet.
*/
void
PhyTxDropTrace(std::string context, Ptr<const Packet> p)
{
nodePacketTxEndTime[ContextToNodeId(context)][p->GetUid()].push_back(Simulator::Now());
}
void
NotifyMacForwardUp(Ptr<const Packet> p)
{
// std::cout << "MacForwardUp UID " << p->GetUid() << std::endl;
// Ptr<const Packet> p = packet;
auto mapIt = m_inFlightPacketMap.find(p->GetUid());
if (mapIt == m_inFlightPacketMap.end())
{
// std::cout << "No packet with UID " << p->GetUid() << " is currently in queue" <<
// std::endl;
return;
}
auto listIt = std::find_if(mapIt->second.begin(),
mapIt->second.end(),
[&p](const InFlightPacketInfo& info) {
return info.m_ptrToPacket->GetUid() == p->GetUid();
});
if (listIt == mapIt->second.end())
{
std::cout << "Forwarding up a packet that has not been enqueued?" << std::endl;
return;
}
if (listIt->m_dstAddress.IsGroup())
{
return;
}
InFlightPacketInfo info;
info.m_srcAddress = listIt->m_srcAddress;
info.m_dstAddress = listIt->m_dstAddress;
info.m_ptrToPacket = listIt->m_ptrToPacket;
info.m_edcaEnqueueTime = listIt->m_edcaEnqueueTime;
info.appTypeTxTime = listIt->appTypeTxTime;
info.m_phyTxTime = listIt->m_phyTxTime;
// info.appTypeRxTime = Simulator::Now();
info.m_L2RxTime = Simulator::Now();
mapIt->second.erase(listIt);
mapIt->second.insert(mapIt->second.end(), info);
}
int appTxrec = 0;
void
NotifyAppRx(Ptr<const Packet> packet, const Address& address)
{
// std::cout << "App Rx UID " << packet->GetUid() << std::endl;
Ptr<const Packet> p = packet;
auto mapIt = m_inFlightPacketMap.find(p->GetUid());
if (mapIt == m_inFlightPacketMap.end())
{
// std::cout << "No packet with UID " << p->GetUid() << " is currently in queue" <<
// std::endl;
return;
}
// mapIt->second.;
auto listIt = std::find_if(mapIt->second.begin(),
mapIt->second.end(),
[&p](const InFlightPacketInfo& info) {
return info.m_ptrToPacket->GetUid() == p->GetUid();
});
if (listIt == mapIt->second.end())
{
std::cout << "Forwarding up a packet that has not been enqueued?" << std::endl;
return;
}
InFlightPacketInfo info;
info.m_srcAddress = listIt->m_srcAddress;
info.m_dstAddress = listIt->m_dstAddress;
info.m_ptrToPacket = listIt->m_ptrToPacket;
info.m_edcaEnqueueTime = listIt->m_edcaEnqueueTime;
info.appTypeTxTime = listIt->appTypeTxTime;
info.m_phyTxTime = listIt->m_phyTxTime;
info.m_L2RxTime = listIt->m_L2RxTime;
info.appTypeRxTime = Simulator::Now();
appTxrec++;
// std::cout << "APRX" << std::endl;
mapIt->second.erase(listIt);
mapIt->second.insert(mapIt->second.end(), info);
}
std::map<uint32_t, Time> dequeueTimes;
void
NotifyMsduDequeuedFromEdcaQueue(Ptr<const WifiMpdu> item)
{
// std::cout << "Dequeue UID " << item->GetPacket()->GetUid() << std::endl;
if (!item->GetHeader().IsQosData() || item->GetHeader().GetAddr1().IsGroup())
{
// the frame is not a unicast QoS data frame or the MSDU lifetime is higher than the
// max queue delay, hence the MSDU has been discarded. Do nothing in this case.
return;
}
Ptr<const Packet> p = item->GetPacket();
uint64_t srcNodeId = MacAddressToNodeId(item->GetHeader().GetAddr2());
auto iter = dequeueTimes.find(srcNodeId);
if (iter == dequeueTimes.end())
{
dequeueTimes.insert(std::make_pair(srcNodeId, Simulator::Now()));
return;
}
if (iter->second == Simulator::Now())
{
// std::cout << "last " << iter->second << " now " << Simulator::Now() << std::endl;
return;
}
auto mapIt = m_inFlightPacketMap.find(p->GetUid());
if (mapIt == m_inFlightPacketMap.end())
{
// std::cout << "No packet with UID " << p->GetUid() << " is currently in queue" <<
// std::endl;
return;
}
auto listIt =
std::find_if(mapIt->second.begin(),
mapIt->second.end(),
[&p](const InFlightPacketInfo& info) { return info.m_ptrToPacket == p; });
if (listIt == mapIt->second.end())
{
// std::cout << "Dequeue a packet that has not been enqueued?" << std::endl;
return;
}
InFlightPacketInfo info;
info.m_srcAddress = item->GetHeader().GetAddr2();
info.m_dstAddress = item->GetHeader().GetAddr1();
info.m_ptrToPacket = item->GetPacket();
info.m_edcaEnqueueTime = listIt->m_edcaEnqueueTime;
info.m_edcaDequeueTime = Simulator::Now();
info.appTypeTxTime = listIt->appTypeTxTime;
info.m_phyTxTime = listIt->m_phyTxTime;
info.appTypeRxTime = listIt->appTypeRxTime;
info.m_L2RxTime = listIt->m_L2RxTime;
info.m_HoLTime = std::max(listIt->m_edcaEnqueueTime, iter->second);
info.m_dequeued = true;
// HERE CALCULATE ALL DELAYS
double newHolSample = (info.m_edcaDequeueTime - info.m_HoLTime).ToDouble(Time::MS);
double queingDelay = (info.m_HoLTime - info.m_edcaEnqueueTime).ToDouble(Time::MS);
double accessDelay = (info.m_phyTxTime - info.m_HoLTime).ToDouble(Time::MS);
double txDelay = (info.m_edcaDequeueTime - info.m_phyTxTime).ToDouble(Time::MS);
auto it = edcaHolSample.find(srcNodeId);
auto ite = intervalEdcaHolSample.find(srcNodeId);
if (ite == intervalEdcaHolSample.end())
{
std::vector<double> tiVec;
tiVec.push_back(newHolSample);
tiVec.push_back(queingDelay);
tiVec.push_back(accessDelay);
tiVec.push_back(txDelay);
intervalEdcaHolSample.insert(std::make_pair(srcNodeId, tiVec));
// std::cout << "first Sample: " << newHolSample << "\n";
}
else
{
ite->second.push_back(newHolSample);
ite->second.push_back(queingDelay);
ite->second.push_back(accessDelay);
ite->second.push_back(txDelay);
}
if (it == edcaHolSample.end())
{
std::vector<double> tiVec;
tiVec.push_back(newHolSample);
tiVec.push_back(queingDelay);
tiVec.push_back(accessDelay);
tiVec.push_back(txDelay);
edcaHolSample.insert(std::make_pair(srcNodeId, tiVec));
// std::cout << "first Sample: " << newHolSample << "\n";
}
else
{
it->second.push_back(newHolSample);
it->second.push_back(queingDelay);
it->second.push_back(accessDelay);
it->second.push_back(txDelay);
}
iter->second = Simulator::Now();
mapIt->second.erase(listIt);
mapIt->second.insert(mapIt->second.end(), info);
}
void
NotifyMsduDroppedAfterDequeueFromEdcaQueue(Ptr<const WifiMpdu> item)
{
// std::cout << "did it work?" << std::endl;
}
void
StartStatistics()
{
for (auto devIt = devices.Begin(); devIt != devices.End(); devIt++)
{
Ptr<WifiNetDevice> dev = DynamicCast<WifiNetDevice>(*devIt);
// trace packets forwarded up by the MAC layer
dev->GetMac()->TraceConnectWithoutContext("MacRx", MakeCallback(&NotifyMacForwardUp));
for (auto& ac : m_aciToString)
{
dev->GetMac()->GetTxopQueue(ac.first)->TraceConnectWithoutContext(
"Enqueue",
MakeBoundCallback(&NotifyEdcaEnqueue));
dev->GetMac()->GetTxopQueue(ac.first)->TraceConnectWithoutContext(
"Dequeue",
MakeCallback(&NotifyMsduDequeuedFromEdcaQueue));
dev->GetMac()->GetTxopQueue(ac.first)->TraceConnectWithoutContext(
"DropAfterDequeue",
MakeCallback(&NotifyMsduDroppedAfterDequeueFromEdcaQueue));
}
}
}
/**
* Incremement the counter for a given address.
*
* \param [out] counter The counter to increment.
* \param addr The address to incremement the counter for.
* \param increment The incremement (1 if omitted).
*/
void
IncrementCounter(std::map<Mac48Address, uint64_t>& counter,
Mac48Address addr,
uint64_t increment = 1)
{
auto it = counter.find(addr);
if (it != counter.end())
{
it->second += increment;
}
else
{
counter.insert(std::make_pair(addr, increment));
}
}
uint32_t associatedStas = 0;
uint32_t deassociatedStas = 0;
/**
* Reset the stats.
*/
void
RestartCalc()
{
bytesReceived.clear();
packetsReceived.clear();
timeFirstReceived.clear();
timeLastReceived.clear();
appTxrec = 0;
if (associatedStas < staNodes.GetN())
{
NS_ABORT_MSG("Not all STA associated. Missing: " << (staNodes.GetN() - associatedStas));
}
}
// void
// TrackTime()
//{
// std::cout << "Time = " << Simulator::Now().GetSeconds() << "s" << std::endl;
// Simulator::Schedule(Seconds(5), &TrackTime);
// }
void
CheckAssociation()
{
if (associatedStas < staNodes.GetN())
{
// NS_ABORT_MSG("Not all STA associated. Missing: " << (staNodes.GetN() -
// associatedStas));
std::cout << "RESTARTED ASSOCIATION" << std::endl;
for (uint32_t i = 0; i < staNodes.GetN(); i++)
{
Ptr<NetDevice> dev = staNodes.Get(i)->GetDevice(0);
Ptr<WifiNetDevice> wifi_dev = DynamicCast<WifiNetDevice>(dev);
Ptr<WifiPhy> dev_phy = wifi_dev->GetPhy();
Ptr<WifiMac> q_mac = wifi_dev->GetMac();
Ptr<StaWifiMac> staMac = StaticCast<StaWifiMac>(q_mac);
if (!(staMac->IsAssociated()))
{
staMac->ScanningTimeout(std::nullopt);
}
}
Simulator::Schedule(Seconds(1), &CheckAssociation);
}
else
{
std::cout << "associated N Sta: " << associatedStas << std::endl;
}
}
/**
* Parse context strings of the form "/NodeList/x/DeviceList/x/..." and fetch the Mac address
*
* \param context The context to parse.
* \return the device MAC address
*/
Mac48Address
ContextToMac(std::string context)
{
std::string sub = context.substr(10);
uint32_t pos = sub.find("/Device");
uint32_t nodeId = std::stoi(sub.substr(0, pos));
Ptr<Node> n = NodeList::GetNode(nodeId);
Ptr<WifiNetDevice> d;
for (uint32_t i = 0; i < n->GetNDevices(); i++)
{
d = n->GetDevice(i)->GetObject<WifiNetDevice>();
if (d)
{
break;
}
}
return Mac48Address::ConvertFrom(d->GetAddress());
}
std::map<uint32_t, std::map<uint32_t, double>> nodeRxPower;
void
GetRxPower(Ptr<TgaxResidentialPropagationLossModel> tgaxPropModel)
{
for (uint32_t i = 0; i < wifiNodes.GetN(); i++)
{
Ptr<NetDevice> dev = wifiNodes.Get(i)->GetDevice(0);
Ptr<WifiNetDevice> wifi_dev = DynamicCast<WifiNetDevice>(dev);
Ptr<Object> object = wifiNodes.Get(i);
Ptr<MobilityModel> model1 = object->GetObject<MobilityModel>();
Ptr<WifiPhy> wifi_phy = wifi_dev->GetPhy();
for (uint32_t x = 0; x < wifiNodes.GetN(); x++)
{
// Skip same nodes
if (i == x)
{
continue;
}
Ptr<NetDevice> dev2 = wifiNodes.Get(x)->GetDevice(0);
Ptr<WifiNetDevice> wifi_dev2 = DynamicCast<WifiNetDevice>(dev2);
Ssid ssid2 = wifi_dev2->GetMac()->GetSsid();
// Receiver must be node in BSS-0
if (!ssid2.IsEqual(Ssid("BSS-0")))
{
continue;
}
Ptr<Object> object2 = wifiNodes.Get(x);
Ptr<MobilityModel> model2 = object2->GetObject<MobilityModel>();
double rxPower = 0;
for (int j = 0; j < 100; j++)
{
rxPower += tgaxPropModel->GetRxPower(wifi_phy->GetTxPowerStart(), model1, model2);
}
nodeRxPower[wifiNodes.Get(i)->GetId()][wifiNodes.Get(x)->GetId()] = (rxPower / 100);
}
}
}
/**
* Print the buildings list in a format that can be used by Gnuplot to draw them.
*
* \param filename The output filename.
*/
void
PrintPythonPlotCSV(std::string filename)
{
// for (uint32_t i = 0; i < bytes__Received.size(); i++)
// {
// std::cout << "Bytes received: " << bytes__Received[i] << std::endl;
// }
std::ofstream outFile;
outFile.open(filename.c_str(), std::ios_base::trunc);
if (!outFile.is_open())
{
NS_LOG_ERROR("Can't open file " << filename);
return;
}
// uint32_t index = 0;
Ptr<Building> building;
for (BuildingList::Iterator it = BuildingList::Begin(); it != BuildingList::End(); ++it)
{
// ++index;
Box box = (*it)->GetBoundaries();
building = *it;
int rowCount = 1;
if (apNodeCount > 2)
{
rowCount = 2;
}
outFile << "box," << box.xMax - box.xMin << "," << box.yMax - box.yMin << "," << rowCount
<< std::endl;
}
double maxDistance;
if (appType != "setup-done")
{ // Formula to draw radius of circle
double max_loss = -(ccaSensitivity - txPower);
// double maxDistance = std::pow(10, (max_loss - 40.05 - 20 * std::log10(5e9 / 2.4e9)) /
// 20);
maxDistance = pow(10, ((max_loss - 40.05 - 20 * std::log10(5e9 / 2.4e9)) / 20));
// maxDistance = (max_loss - 40.05 - 20 * std::log10(5e9 / 2.4e9)) / (5 - 4 *
// std::log10(5));
}
// double maxDistance_log = std::pow(10, max_loss / (10 * 3));
// max_loss = txPower - ccaSensitivity;
// // double maxDistance2 = std::pow(10, (max_loss - 40.05 - 20 * std::log10(5e9 / 2.4e9)) /
// // 20);
// double maxDistance2 =
// (max_loss - 40.05 - 20 * std::log10(5e9 / 2.4e9)) / (5 - 4 * std::log10(5));
for (uint32_t i = 0; i < apNodes.GetN(); i++)
{
Ptr<NetDevice> dev = apNodes.Get(i)->GetDevice(0);
Ptr<WifiNetDevice> wifi_dev = DynamicCast<WifiNetDevice>(dev);
Ptr<Object> object = apNodes.Get(i);
Ptr<MobilityModel> model = object->GetObject<MobilityModel>();
Vector pos = model->GetPosition();
Ssid ssid = wifi_dev->GetMac()->GetSsid();
// Ptr<WifiPhy> wifi_phy = wifi_dev->GetPhy();
// wifi_phy->SetCcaSensitivityThreshold();
if (appType == "setup-done")
{
// std::string configString = configuration[apNodes.Get(i)->GetId()];
std::string configString = configuration[apNodes.Get(i)->GetId()];
std::vector<std::string> configValues = csv_split(configString, ',');
double m_ccaSensitivity = std::stoi(configValues[1]);
double m_txPower = std::stoi(configValues[2]);
// double m_chWidth = std::stoi(configValues[3]);
// double m_chNumber = std::stoi(configValues[4]);
// size_t pos = configString.find(',');
// configString = configString.substr(pos + 1);
// size_t pos2 = configString.find(',');
// std::cout << "CCaSensitivity: " << m_ccaSensitivity << std::endl;
// double m_ccaSensitivity = std::stoi(configString.substr(0, pos2));
// std::cout << "apTxPower: " << m_txPower << std::endl;
// double m_txPower = std::stoi(configString.substr(pos2 + 1));
// Formula to draw radius of circle
double max_loss = -(m_ccaSensitivity - m_txPower); // TODO: fix to specific tx power
// double maxDistance = std::pow(10, (max_loss - 40.05 - 20 * std::log10(5e9
// / 2.4e9)) / 20);
maxDistance =
(max_loss - 40.05 - 20 * std::log10(5e9 / 2.4e9)) / (5 - 4 * std::log10(5));
}
outFile << "AP," << pos.x << "," << pos.y << "," << ssid.PeekString() << "-"
<< apNodes.Get(i)->GetId() << "-"
<< "HeMcs" << nodeMcs[apNodes.Get(i)->GetId()] << "," << maxDistance << std::endl;
}
for (uint32_t i = 0; i < staNodes.GetN(); i++)
{
Ptr<NetDevice> dev = staNodes.Get(i)->GetDevice(0);
Ptr<WifiNetDevice> wifi_dev = DynamicCast<WifiNetDevice>(dev);
Ptr<Object> object = staNodes.Get(i);
Ptr<MobilityModel> model = object->GetObject<MobilityModel>();
Vector pos = model->GetPosition();
Ssid ssid = wifi_dev->GetMac()->GetSsid();
if (appType == "setup-done")
{
std::string configString = configuration[staNodes.Get(i)->GetId()];
std::vector<std::string> configValues = csv_split(configString, ',');
double m_ccaSensitivity = std::stoi(configValues[1]);
double m_txPower = std::stoi(configValues[2]);
// double m_chWidth = std::stoi(configValues[3]);
// double m_chNumber = std::stoi(configValues[4]);
// size_t pos = configString.find(',');
// configString = configString.substr(pos + 1);
// size_t pos2 = configString.find(',');
std::cout << "CCaSensitivity: " << m_ccaSensitivity << std::endl;
// double m_ccaSensitivity = std::stoi(configString.substr(0, pos2));
std::cout << "apTxPower: " << m_txPower << std::endl;
// size_t pos = configString.find(',');
// configString = configString.substr(pos + 1);
// size_t pos2 = configString.find(',');
// std::cout << "CCaSensitivity: " << configString.substr(0, pos2) << std::endl;
// double m_ccaSensitivity = std::stoi(configString.substr(0, pos2));
// std::cout << "apTxPower: " << configString.substr(pos2 + 1) << std::endl;
// double m_txPower = std::stoi(configString.substr(pos2 + 1));
// Formula to draw radius of circle
double max_loss = -(m_ccaSensitivity - m_txPower); // TODO: fix to specific tx power
maxDistance =
(max_loss - 40.05 - 20 * std::log10(5e9 / 2.4e9)) / (5 - 4 * std::log10(5));
}
outFile << "STA," << pos.x << "," << pos.y << "," << ssid.PeekString() << "-"
<< staNodes.Get(i)->GetId() << "-"
<< "HeMcs" << nodeMcs[staNodes.Get(i)->GetId()] << "," << maxDistance << std::endl;
}
}
void
RestartIntervalThroughputHolDelay()
{
intervalBytesReceived.clear();
// std::cout << "Amount of samples " << intervalEdcaHolSample[2].size() << std::endl;
intervalEdcaHolSample.clear();
}
void
MeasureIntervalThroughputHolDelay()
{
Ns3AiMsgInterfaceImpl<Env, Act>* msgInterface =
Ns3AiMsgInterface::Get()->GetInterface<Env, Act>();
// std::cout << "\nInterval T " << Simulator::Now().GetSeconds() << std::endl;
std::map<uint32_t, std::tuple<double, double, double, double>> nodeDelays;
for (auto it : intervalEdcaHolSample)
{
uint32_t srcNodeId = it.first;
double sum1 = 0;
double sum2 = 0;
double sum3 = 0;
double sum4 = 0;
for (double i = 0; i < it.second.size(); i += 4)
{
sum1 += it.second[i];
sum2 += it.second[i + 1];
sum3 += it.second[i + 2];
sum4 += it.second[i + 3];
}
std::get<0>(nodeDelays[srcNodeId]) = sum1 / (it.second.size() / 4); // Avg HoL Delay
std::get<1>(nodeDelays[srcNodeId]) = sum2 / (it.second.size() / 4); // Avg Queuing Delay
std::get<2>(nodeDelays[srcNodeId]) = sum3 / (it.second.size() / 4); // Avg Access Delay
std::get<3>(nodeDelays[srcNodeId]) = sum4 / (it.second.size() / 4); // Avg Tx Delay
}
Ptr<TgaxResidentialPropagationLossModel> propModel =
CreateObject<TgaxResidentialPropagationLossModel>();
GetRxPower(propModel);
msgInterface->CppSendBegin();
for (size_t i = 0; i < wifiNodes.GetN(); i++)
{
uint32_t txNodeId = wifiNodes.Get(i)->GetId();
auto& env_struct = msgInterface->GetCpp2PyVector()->at(txNodeId);
env_struct.txNode = txNodeId;
env_struct.mcs = nodeMcs[txNodeId];
env_struct.holDelay = std::get<0>(nodeDelays[txNodeId]);
if (txNodeId >= N_BSS) // STAs
{
env_struct.throughput = (intervalBytesReceived.find(txNodeId)->second * 8) /
static_cast<double>(Seconds(1).GetMicroSeconds());
}
else
{
// Only count for UL traffic
env_struct.throughput = 0;
}
std::cout << "CPP send: txnode " << txNodeId << " tpt " << env_struct.throughput
<< std::endl;
for (auto rxNodePower : nodeRxPower[txNodeId])
{
NS_ASSERT(rxNodePower.first % N_BSS == 0); // only record rx node in first BSS
if (rxNodePower.first == txNodeId)
{
env_struct.rxPower[rxNodePower.first / N_BSS] = 0;
}
else
{
env_struct.rxPower[rxNodePower.first / N_BSS] = rxNodePower.second;
}
}
}
msgInterface->CppSendEnd();
msgInterface->CppRecvBegin();
double nextCca = msgInterface->GetPy2CppVector()->at(0).newCcaSensitivity;
msgInterface->CppRecvEnd();
std::cout << "At " << Simulator::Now().GetMilliSeconds() << "ms:" << std::endl;
// Change CCA of nodes in first BSS
for (uint32_t i = 0; i < wifiNodes.GetN(); i += N_BSS)
{
uint32_t nodeId = wifiNodes.Get(i)->GetId();
Ptr<NetDevice> dev = wifiNodes.Get(i)->GetDevice(0);
Ptr<WifiNetDevice> wifi_dev = DynamicCast<WifiNetDevice>(dev);
Ptr<WifiPhy> wifi_phy = wifi_dev->GetPhy();
Ssid ssid = wifi_dev->GetMac()->GetSsid();
// Only change CCA for nodes in BSS-0
NS_ASSERT(ssid.IsEqual(Ssid("BSS-0")));
double currentCca = wifi_phy->GetCcaSensitivityThreshold();
Ptr<ThresholdPreambleDetectionModel> preambleCaptureModel =
CreateObject<ThresholdPreambleDetectionModel>();
preambleCaptureModel->SetAttribute("MinimumRssi", DoubleValue(nextCca));
wifi_phy->SetCcaSensitivityThreshold(nextCca);
wifi_phy->SetPreambleDetectionModel(preambleCaptureModel);
std::cout << "-- " << ssid << " Node " << nodeId << " current CCA " << currentCca
<< " next CCA " << nextCca << std::endl;
}