AlohaMacLayer.cc

00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        AlohaMacLayer.cc
00003  *
00004  * author:      Jerome Rousselot <jerome.rousselot@csem.ch>
00005  *
00006  * copyright:   (C) 2008 Centre Suisse d'Electronique et Microtechnique (CSEM) SA
00007  *        Systems Engineering
00008  *              Real-Time Software and Networking
00009  *              Jaquet-Droz 1, CH-2002 Neuchatel, Switzerland.
00010  *
00011  *              This program is free software; you can redistribute it
00012  *              and/or modify it under the terms of the GNU General Public
00013  *              License as published by the Free Software Foundation; either
00014  *              version 2 of the License, or (at your option) any later
00015  *              version.
00016  *              For further information see file COPYING
00017  *              in the top level directory
00018  * description: this class implements the Aloha MAC protocol for an UWB-IR
00019  *        IEEE 802.15.4A transceiver.
00020  ***************************************************************************/
00021 
00022 #include "AlohaMacLayer.h"
00023 #include <iostream>
00024 #include <NetwToMacControlInfo.h>
00025 
00026 using namespace std;
00027 
00028 Define_Module(AlohaMacLayer);
00029 
00030 void AlohaMacLayer::initialize(int stage) {
00031   UWBIRMac::initialize(stage);
00032   if(stage == 1 && myMacAddr != 0) {
00033             phy->setRadioState(Radio::TX);
00034     } else if(stage == 1 && myMacAddr == 0) {
00035             phy->setRadioState(Radio::RX);
00036     }
00037 }
00038 
00039 void AlohaMacLayer::finish() {
00040     if(stats) {
00041         recordScalar("Erroneous bits", errRxBits);
00042         recordScalar("Total received bits", totalRxBits);
00043         recordScalar("Average BER", errRxBits/totalRxBits);
00044         recordScalar("nbReceivedPacketsRS", nbReceivedPacketsRS);
00045         recordScalar("nbReceivedPacketsnoRS", nbReceivedPacketsNoRS);
00046         if(rsDecoder) {
00047           recordScalar("nbReceivedPackets", nbReceivedPacketsRS);
00048         } else {
00049           recordScalar("nbReceivedPackets", nbReceivedPacketsNoRS);
00050         }
00051 
00052         recordScalar("nbSentPackets", nbSentPackets);
00053     }
00054 }
00055 
00056 MacPkt* AlohaMacLayer::encapsMsg(cPacket *msg) {
00057     UWBIRMacPkt* encaps = new UWBIRMacPkt(msg->getName(), msg->getKind());
00058     encaps->setByteLength(headerLength);
00059 
00060     // copy dest address from the Control Info attached to the network
00061     // mesage by the network layer
00062     NetwToMacControlInfo* cInfo = static_cast<NetwToMacControlInfo*>(msg->removeControlInfo());
00063 
00064     debugEV <<"CInfo removed, mac addr="<< cInfo->getNextHopMac()<<endl;
00065     encaps->setDestAddr(cInfo->getNextHopMac());
00066 
00067     //delete the control info
00068     delete cInfo;
00069 
00070     //set the src address to own mac address
00071     encaps->setSrcAddr(myMacAddr);
00072 
00073     //encapsulate the network packet
00074     encaps->encapsulate(msg);
00075 
00076     prepareData(encaps);
00077 
00078     nbSentPackets++;
00079 
00080   return encaps;
00081 }
00082 
00083 /*
00084 void AlohaMacLayer::handleUpperMsg(cMessage *msg) {
00085     MacPkt* packet = encapsMsg(msg);
00086 
00087 }
00088 */
00089 void AlohaMacLayer::handleLowerMsg(cMessage *msg) {
00090     UWBIRMacPkt *mac = static_cast<UWBIRMacPkt *>(msg);
00091 
00092     if(validatePacket(mac)) {
00093         int dest = mac->getDestAddr();
00094         int src = mac->getSrcAddr();
00095         if ((dest == myMacAddr)) {
00096           debugEV << "message with mac addr " << src
00097                     << " for me (dest=" << dest
00098                     << ") -> forward packet to upper layer\n";
00099             sendUp(decapsMsg(mac));
00100         } else {
00101           debugEV << "message with mac addr " << src
00102                     << " not for me (dest=" << dest
00103                     << ") -> delete (my MAC=" << myMacAddr << ")\n";
00104             delete mac;
00105         }
00106     } else {
00107       debugEV << "Errors in message ; dropping mac packet." << endl;
00108         delete mac;
00109     }
00110 }
00111