1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  *
16  * Author: Junling Bu <linlinjavaer@gmail.com>
17  */
18 
19 #include "channel-scheduler.h"
20 #include "ns3/log.h"
21 
22 namespace ns3 {
23 
24 NS_LOG_COMPONENT_DEFINE ("ChannelScheduler");
25 
26 NS_OBJECT_ENSURE_REGISTERED (ChannelScheduler);
27 
28   /**
29    * \brief Get the type ID.
30    * \return the object TypeId
31    */
32 TypeId
GetTypeId(void)33 ChannelScheduler::GetTypeId (void)
34 {
35   static TypeId tid = TypeId ("ns3::ChannelScheduler")
36     .SetParent<Object> ()
37     .SetGroupName ("Wave")
38   ;
39   return tid;
40 }
41 
ChannelScheduler()42 ChannelScheduler::ChannelScheduler ()
43 {
44   NS_LOG_FUNCTION (this);
45 }
~ChannelScheduler()46 ChannelScheduler::~ChannelScheduler ()
47 {
48   NS_LOG_FUNCTION (this);
49 }
50 
51 void
DoInitialize(void)52 ChannelScheduler::DoInitialize (void)
53 {
54   // assign default CCH access when the device is initialized
55   AssignDefaultCchAccess ();
56 }
57 
58 void
SetWaveNetDevice(Ptr<WaveNetDevice> device)59 ChannelScheduler::SetWaveNetDevice (Ptr<WaveNetDevice> device)
60 {
61   NS_LOG_FUNCTION (this << device);
62   m_device = device;
63 }
64 
65 bool
IsChannelAccessAssigned(uint32_t channelNumber) const66 ChannelScheduler::IsChannelAccessAssigned (uint32_t channelNumber) const
67 {
68   NS_LOG_FUNCTION (this << channelNumber);
69   return (GetAssignedAccessType (channelNumber) != NoAccess);
70 }
71 
72 bool
IsCchAccessAssigned(void) const73 ChannelScheduler::IsCchAccessAssigned (void) const
74 {
75   NS_LOG_FUNCTION (this);
76   return (GetAssignedAccessType (CCH) != NoAccess);
77 }
78 
79 bool
IsSchAccessAssigned(void) const80 ChannelScheduler::IsSchAccessAssigned (void) const
81 {
82   NS_LOG_FUNCTION (this);
83   return (GetAssignedAccessType (SCH1) != NoAccess) || (GetAssignedAccessType (SCH2) != NoAccess)
84          || (GetAssignedAccessType (SCH3) != NoAccess) || (GetAssignedAccessType (SCH4) != NoAccess)
85          || (GetAssignedAccessType (SCH5) != NoAccess) || (GetAssignedAccessType (SCH6) != NoAccess);
86 }
87 
88 bool
IsContinuousAccessAssigned(uint32_t channelNumber) const89 ChannelScheduler::IsContinuousAccessAssigned (uint32_t channelNumber) const
90 {
91   NS_LOG_FUNCTION (this << channelNumber);
92   return (GetAssignedAccessType (channelNumber) == ContinuousAccess);
93 }
94 bool
IsAlternatingAccessAssigned(uint32_t channelNumber) const95 ChannelScheduler::IsAlternatingAccessAssigned (uint32_t channelNumber) const
96 {
97   NS_LOG_FUNCTION (this << channelNumber);
98   return (GetAssignedAccessType (channelNumber) == AlternatingAccess);
99 }
100 bool
IsExtendedAccessAssigned(uint32_t channelNumber) const101 ChannelScheduler::IsExtendedAccessAssigned (uint32_t channelNumber) const
102 {
103   NS_LOG_FUNCTION (this << channelNumber);
104   return (GetAssignedAccessType (channelNumber) == ExtendedAccess);
105 }
106 bool
IsDefaultCchAccessAssigned(void) const107 ChannelScheduler::IsDefaultCchAccessAssigned (void) const
108 {
109   NS_LOG_FUNCTION (this);
110   return (GetAssignedAccessType (CCH) == DefaultCchAccess);
111 }
112 bool
StartSch(const SchInfo & schInfo)113 ChannelScheduler::StartSch (const SchInfo & schInfo)
114 {
115   NS_LOG_FUNCTION (this << &schInfo);
116   uint32_t cn = schInfo.channelNumber;
117 
118   if (ChannelManager::IsCch (schInfo.channelNumber))
119     {
120       NS_LOG_DEBUG ("the channel access requirement for CCH is not allowed.");
121       return false;
122     }
123   uint32_t extends = schInfo.extendedAccess;
124   bool immediate = schInfo.immediateAccess;
125   Ptr<OcbWifiMac> mac = m_device->GetMac (cn);
126   for (EdcaParametersI i = schInfo.edcaParameters.begin (); i != schInfo.edcaParameters.end (); ++i)
127     {
128       EdcaParameter edca = i->second;
129       mac->ConfigureEdca (edca.cwmin, edca.cwmax, edca.aifsn, i->first);
130     }
131 
132   if (extends == EXTENDED_CONTINUOUS)
133     {
134       return AssignContinuousAccess (cn, immediate);
135     }
136   else if (extends == EXTENDED_ALTERNATING)
137     {
138       return AssignAlternatingAccess (cn, immediate);
139     }
140   else
141     {
142       return AssignExtendedAccess (cn, extends, immediate);
143     }
144 }
145 
146 bool
StopSch(uint32_t channelNumber)147 ChannelScheduler::StopSch (uint32_t channelNumber)
148 {
149   NS_LOG_FUNCTION (this << channelNumber);
150   if (ChannelManager::IsCch (channelNumber))
151     {
152       NS_LOG_DEBUG ("the channel access for CCH is not allowed to be released.");
153       return false;
154     }
155   if (!IsChannelAccessAssigned (channelNumber))
156     {
157       NS_LOG_DEBUG ("the channel access for channel " << channelNumber << " has already been released.");
158       return true;
159     }
160   return ReleaseAccess (channelNumber);
161 }
162 
163 void
DoDispose(void)164 ChannelScheduler::DoDispose (void)
165 {
166   NS_LOG_FUNCTION (this);
167   m_device = 0;
168 }
169 
170 } // namespace ns3
171