-
Notifications
You must be signed in to change notification settings - Fork 221
/
BaseMacLayer.cc
291 lines (243 loc) · 9.2 KB
/
BaseMacLayer.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
/***************************************************************************
* file: BaseMacLayer.cc
*
* author: Daniel Willkomm
*
* copyright: (C) 2004 Telecommunication Networks Group (TKN) at
* Technische Universitaet Berlin, Germany.
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later
* version.
* For further information see file COPYING
* in the top level directory
***************************************************************************
* part of: framework implementation developed by tkn
* description: basic MAC layer class
* subclass to create your own MAC layer
**************************************************************************/
#include "veins/base/modules/BaseMacLayer.h"
#include <cassert>
#include <sstream>
#include "veins/base/phyLayer/Mapping.h"
#include "veins/base/phyLayer/Signal_.h"
#include "veins/base/phyLayer/MacToPhyInterface.h"
#include "veins/base/utils/MacToNetwControlInfo.h"
#include "veins/base/utils/NetwToMacControlInfo.h"
#include "veins/base/phyLayer/MacToPhyControlInfo.h"
#include "veins/base/modules/AddressingInterface.h"
#include "veins/base/connectionManager/ChannelAccess.h"
#include "veins/base/utils/FindModule.h"
#include "veins/base/messages/MacPkt_m.h"
using Veins::ChannelAccess;
Define_Module(BaseMacLayer);
/**
* First we have to initialize the module from which we derived ours,
* in this case BaseLayer.
*
**/
void BaseMacLayer::initialize(int stage)
{
BaseLayer::initialize(stage);
if(stage==0)
{
// get handle to phy layer
if ((phy = FindModule<MacToPhyInterface*>::findSubModule(getParentModule())) == NULL) {
error("Could not find a PHY module.");
}
headerLength = par("headerLength");
phyHeaderLength = phy->getPhyHeaderLength();
hasPar("coreDebug") ? coreDebug = par("coreDebug").boolValue() : coreDebug = false;
}
if (myMacAddr == LAddress::L2NULL()) {
// see if there is an addressing module available
// otherwise use NIC modules id as MAC address
AddressingInterface* addrScheme = FindModule<AddressingInterface*>::findSubModule(findHost());
if(addrScheme) {
myMacAddr = addrScheme->myMacAddr(this);
} else {
const std::string addressString = par("address").stringValue();
if (addressString.empty() || addressString == "auto")
myMacAddr = LAddress::L2Type(getParentModule()->getId());
else
myMacAddr = LAddress::L2Type(addressString.c_str());
// use streaming operator for string conversion, this makes it more
// independent from the myMacAddr type
std::ostringstream oSS; oSS << myMacAddr;
par("address").setStringValue(oSS.str());
}
registerInterface();
}
}
void BaseMacLayer::registerInterface()
{
}
/**
* Decapsulates the network packet from the received MacPkt
**/
cPacket* BaseMacLayer::decapsMsg(MacPkt* msg)
{
cPacket *m = msg->decapsulate();
setUpControlInfo(m, msg->getSrcAddr());
// delete the macPkt
delete msg;
coreEV << " message decapsulated " << endl;
return m;
}
/**
* Encapsulates the received NetwPkt into a MacPkt and set all needed
* header fields.
**/
MacPkt* BaseMacLayer::encapsMsg(cPacket *netwPkt)
{
MacPkt *pkt = new MacPkt(netwPkt->getName(), netwPkt->getKind());
pkt->setBitLength(headerLength);
// copy dest address from the Control Info attached to the network
// message by the network layer
cObject* cInfo = netwPkt->removeControlInfo();
coreEV <<"CInfo removed, mac addr="<< getUpperDestinationFromControlInfo(cInfo) << endl;
pkt->setDestAddr(getUpperDestinationFromControlInfo(cInfo));
//delete the control info
delete cInfo;
//set the src address to own mac address (nic module getId())
pkt->setSrcAddr(myMacAddr);
//encapsulate the network packet
pkt->encapsulate(netwPkt);
coreEV <<"pkt encapsulated\n";
return pkt;
}
/**
* Redefine this function if you want to process messages from upper
* layers before they are send to lower layers.
*
* To forward the message to lower layers after processing it please
* use @ref sendDown. It will take care of anything needed
**/
void BaseMacLayer::handleUpperMsg(cMessage *mac)
{
assert(dynamic_cast<cPacket*>(mac));
sendDown(encapsMsg(static_cast<cPacket*>(mac)));
}
/**
* This basic implementation just forwards all message that are
* broadcast (destAddr = L2BROADCAST) or destined for this node
* (destAddr = nic module getId()) to the network layer
*
* @sa sendUp
**/
void BaseMacLayer::handleLowerMsg(cMessage *msg)
{
MacPkt* mac = static_cast<MacPkt *>(msg);
LAddress::L2Type dest = mac->getDestAddr();
LAddress::L2Type src = mac->getSrcAddr();
//only foward to upper layer if message is for me or broadcast
if((dest == myMacAddr) || LAddress::isL2Broadcast(dest)) {
coreEV << "message with mac addr " << src
<< " for me (dest=" << dest
<< ") -> forward packet to upper layer\n";
sendUp(decapsMsg(mac));
}
else{
coreEV << "message with mac addr " << src
<< " not for me (dest=" << dest
<< ") -> delete (my MAC="<<myMacAddr<<")\n";
delete mac;
}
}
void BaseMacLayer::handleLowerControl(cMessage* msg)
{
switch (msg->getKind())
{
case MacToPhyInterface::TX_OVER:
msg->setKind(TX_OVER);
sendControlUp(msg);
break;
default:
EV << "BaseMacLayer does not handle control messages of this type (name was "<<msg->getName()<<")\n";
delete msg;
break;
}
}
Signal* BaseMacLayer::createSimpleSignal(simtime_t_cref start, simtime_t_cref length, double power, double bitrate)
{
simtime_t end = start + length;
//create signal with start at current simtime and passed length
Signal* s = new Signal(start, length);
//create and set tx power mapping
Mapping* txPowerMapping = createRectangleMapping(start, end, power);
s->setTransmissionPower(txPowerMapping);
//create and set bitrate mapping
Mapping* bitrateMapping = createConstantMapping(start, end, bitrate);
s->setBitrate(bitrateMapping);
return s;
}
Mapping* BaseMacLayer::createConstantMapping(simtime_t_cref start, simtime_t_cref end, Argument::mapped_type_cref value)
{
//create mapping over time
Mapping* m = MappingUtils::createMapping(Argument::MappedZero(), DimensionSet::timeDomain(), Mapping::LINEAR);
//set position Argument
Argument startPos(start);
//set mapping at position
m->setValue(startPos, value);
//set position Argument
Argument endPos(end);
//set mapping at position
m->setValue(endPos, value);
return m;
}
Mapping* BaseMacLayer::createRectangleMapping(simtime_t_cref start, simtime_t_cref end, Argument::mapped_type_cref value)
{
//create mapping over time
Mapping* m = MappingUtils::createMapping(DimensionSet::timeDomain(), Mapping::LINEAR);
//set position Argument
Argument startPos(start);
//set discontinuity at position
MappingUtils::addDiscontinuity(m, startPos, Argument::MappedZero(), MappingUtils::post(start), value);
//set position Argument
Argument endPos(end);
//set discontinuity at position
MappingUtils::addDiscontinuity(m, endPos, Argument::MappedZero(), MappingUtils::pre(end), value);
return m;
}
ConstMapping* BaseMacLayer::createSingleFrequencyMapping(simtime_t_cref start,
simtime_t_cref end,
Argument::mapped_type_cref centerFreq,
Argument::mapped_type_cref halfBandwidth,
Argument::mapped_type_cref value)
{
Mapping* res = MappingUtils::createMapping(Argument::MappedZero(), DimensionSet::timeFreqDomain(), Mapping::LINEAR);
Argument pos(DimensionSet::timeFreqDomain());
pos.setArgValue(Dimension::frequency(), centerFreq - halfBandwidth);
pos.setTime(start);
res->setValue(pos, value);
pos.setTime(end);
res->setValue(pos, value);
pos.setArgValue(Dimension::frequency(), centerFreq + halfBandwidth);
res->setValue(pos, value);
pos.setTime(start);
res->setValue(pos, value);
return res;
}
BaseConnectionManager* BaseMacLayer::getConnectionManager() {
cModule* nic = getParentModule();
return ChannelAccess::getConnectionManager(nic);
}
const LAddress::L2Type& BaseMacLayer::getUpperDestinationFromControlInfo(const cObject *const pCtrlInfo) {
return NetwToMacControlInfo::getDestFromControlInfo(pCtrlInfo);
}
/**
* Attaches a "control info" (MacToNetw) structure (object) to the message pMsg.
*/
cObject *const BaseMacLayer::setUpControlInfo(cMessage *const pMsg, const LAddress::L2Type& pSrcAddr)
{
return MacToNetwControlInfo::setControlInfo(pMsg, pSrcAddr);
}
/**
* Attaches a "control info" (MacToPhy) structure (object) to the message pMsg.
*/
cObject *const BaseMacLayer::setDownControlInfo(cMessage *const pMsg, Signal *const pSignal)
{
return MacToPhyControlInfo::setControlInfo(pMsg, pSignal);
}