-
Notifications
You must be signed in to change notification settings - Fork 0
/
WifiChannel.cpp
97 lines (77 loc) · 2.28 KB
/
WifiChannel.cpp
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
//
// Created by zeronsix on 5/12/19.
//
#include "WifiChannel.h"
#include "Node.h"
#include <limits>
WifiChannel::WifiChannel():
m_state{State::Idle},
m_prevState{State::Idle},
m_lastStateChangeTime{0},
m_lastIdleChangeTime{0}
{
}
void WifiChannel::SetState(WifiChannel::State state) {
LOG_FUNCTION("channel set to " << static_cast<int>(state) << );
if (state == m_state) {
return;
}
for (auto node : m_listeners) {
//Simulator::Schedule(0, std::bind(&Node::NotifyChannelStateChange, node, state, m_state));
node->NotifyChannelStateChange(state, m_state);
}
m_prevState = m_state;
m_state = state;
m_lastStateChangeTime = Simulator::Now();
switch (state) {
case State::Idle:
m_lastIdleChangeTime = Simulator::Now();
break;
default:
break;
}
}
WifiChannel::State WifiChannel::GetState() const {
return m_state;
}
Time WifiChannel::GetLastStateChangeTime() const {
return m_lastStateChangeTime;
}
bool WifiChannel::WasIdleDuring(Time time) const {
//LOG_FUNCTION();
if (Simulator::Now() == 0) {
return true;
}
if (m_state == State::Idle && (Simulator::Now() - m_lastIdleChangeTime) >= time) {
return true;
}
else if (m_prevState == State::Idle && (Simulator::Now() == m_lastStateChangeTime) &&
(Simulator::Now() - m_lastIdleChangeTime) >= time) {
return true;
}
return false;
}
void WifiChannel::Send(FramePtr frame) {
LOG_FUNCTION(" send type:" << (int)frame->GetType()<< );
if (m_state == WifiChannel::State::Idle) {
SetState(WifiChannel::State::Busy);
Simulator::Schedule(frame->GetDuration(frame->GetSender()->GetMcs()),
std::bind(&WifiChannel::EndTx, this, frame));
}
else if (m_state == WifiChannel::State::Busy) {
SetState(WifiChannel::State::Collision);
}
}
void WifiChannel::EndTx(FramePtr frame) {
LOG_FUNCTION();
SetState(WifiChannel::State::Idle);
if (m_prevState == WifiChannel::State::Busy) {
frame->GetReceiver()->Receive(frame);
}
}
void WifiChannel::AddListener(Node *node) {
m_listeners.push_back(node);
}
Time WifiChannel::GetLastIdleChangeTime() const {
return m_lastIdleChangeTime;
}