csma.h

00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        csma.h
00003  *
00004   * author:     Jerome Rousselot, Marcel Steine, Amre El-Hoiydi,
00005  *        Marc Loebbers, Yosia Hadisusanto
00006  *
00007  * copyright: (C) 2007-2009 CSEM SA
00008  *        (C) 2009 T.U. Eindhoven
00009  *        (C) 2004 Telecommunication Networks Group (TKN) at
00010  *              Technische Universitaet Berlin, Germany.
00011  *
00012  *              This program is free software; you can redistribute it
00013  *              and/or modify it under the terms of the GNU General Public
00014  *              License as published by the Free Software Foundation; either
00015  *              version 2 of the License, or (at your option) any later
00016  *              version.
00017  *              For further information see file COPYING
00018  *              in the top level directory
00019  *
00020  * Funding: This work was partially financed by the European Commission under the
00021  * Framework 6 IST Project "Wirelessly Accessible Sensor Populations"
00022  * (WASP) under contract IST-034963.
00023  ***************************************************************************
00024  * part of:    Modifications to the MF-2 framework by CSEM
00025  **************************************************************************/
00026 
00027 
00028 #ifndef csma_H
00029 #define csma_H
00030 
00031 
00032 #include <string>
00033 #include <sstream>
00034 #include <vector>
00035 #include <list>
00036 #include "BaseMacLayer.h"
00037 #include <DroppedPacket.h>
00038 #include <MacPkt_m.h>
00039 
00054 class  csma : public BaseMacLayer
00055 {
00056   public:
00057 
00058 
00059   ~csma();
00060 
00062     virtual void initialize(int);
00063 
00065     virtual void finish();
00066 
00068     virtual void handleLowerMsg(cMessage*);
00069 
00071     virtual void handleUpperMsg(cMessage*);
00072 
00074     virtual void handleSelfMsg(cMessage*);
00075 
00077     virtual void handleLowerControl(cMessage *msg);
00078 
00079   protected:
00080     typedef std::list<MacPkt*> MacQueue;
00081 
00084   long nbTxFrames;
00085   long nbRxFrames;
00086   long nbMissedAcks;
00087   long nbRecvdAcks;
00088   long nbDroppedFrames;
00089   long nbTxAcks;
00090   long nbDuplicates;
00091   long nbBackoffs;
00092   double backoffValues;
00096   bool stats;
00098   bool trace;
00099 
00103     enum t_mac_states {
00104         IDLE_1=1,
00105         BACKOFF_2,
00106         CCA_3,
00107         TRANSMITFRAME_4,
00108         WAITACK_5,
00109         WAITSIFS_6,
00110         TRANSMITACK_7
00111 
00112     };
00113 
00114     /*************************************************************/
00115     /****************** TYPES ************************************/
00116     /*************************************************************/
00117 
00119     enum t_mac_timer {
00120       TIMER_NULL=0,
00121       TIMER_BACKOFF,
00122       TIMER_CCA,
00123       TIMER_SIFS,
00124       TIMER_RX_ACK,
00125     };
00126 
00129     cMessage * backoffTimer, * ccaTimer, * txTimer, * sifsTimer, * rxAckTimer;
00134     enum t_mac_event {
00135       EV_SEND_REQUEST=1,                   // 1, 11, 20, 21, 22
00136       EV_TIMER_BACKOFF,                    // 2, 7, 14, 15
00137       EV_FRAME_TRANSMITTED,                // 4, 19
00138       EV_ACK_RECEIVED,                     // 5
00139       EV_ACK_TIMEOUT,                      // 12
00140       EV_FRAME_RECEIVED,                   // 15, 26
00141       EV_DUPLICATE_RECEIVED,
00142       EV_TIMER_SIFS,                       // 17
00143       EV_BROADCAST_RECEIVED,       // 23, 24
00144       EV_TIMER_CCA
00145     };
00146 
00148     enum t_csma_frame_types {
00149       DATA,
00150       ACK
00151     };
00152 
00153     enum t_mac_carrier_sensed {
00154       CHANNEL_BUSY=1,
00155       CHANNEL_FREE
00156     } ;
00157 
00158     enum t_mac_status {
00159       STATUS_OK=1,
00160       STATUS_ERROR,
00161       STATUS_RX_ERROR,
00162       STATUS_RX_TIMEOUT,
00163       STATUS_FRAME_TO_PROCESS,
00164       STATUS_NO_FRAME_TO_PROCESS,
00165       STATUS_FRAME_TRANSMITTED
00166     };
00167 
00169     enum backoff_methods {
00171       CONSTANT = 0,
00173       LINEAR,
00175       EXPONENTIAL,
00176     };
00177 
00179     t_mac_states macState;
00180     t_mac_status status;
00181 
00187     simtime_t sifs;
00188 
00190     simtime_t macAckWaitDuration;
00191 
00192     bool transmissionAttemptInterruptedByRx;
00194     simtime_t ccaDetectionTime;
00196     simtime_t rxSetupTime;
00198     simtime_t aTurnaroundTime;
00200     int macMaxCSMABackoffs;
00202     unsigned int macMaxFrameRetries;
00204     simtime_t aUnitBackoffPeriod;
00206     bool useMACAcks;
00207 
00209     backoff_methods backoffMethod;
00210 
00215   int macMinBE;
00220   int macMaxBE;
00221 
00224   double initialCW;
00225 
00227     double txPower;
00228 
00230     int NB;
00231 
00234     MacQueue macQueue;
00235 
00237     unsigned int queueLength;
00238 
00243     unsigned int txAttempts;
00244 
00246     double bitrate;
00247 
00249     DroppedPacket droppedPacket;
00250 
00252     int catDroppedPacket;
00253 
00255     int nicId;
00256 
00258     int ackLength;
00259 
00261     //int macaddress;
00262 
00263 
00264 protected:
00265   // FSM functions
00266   void fsmError(t_mac_event event, cMessage *msg);
00267   void executeMac(t_mac_event event, cMessage *msg);
00268   void updateStatusIdle(t_mac_event event, cMessage *msg);
00269   void updateStatusBackoff(t_mac_event event, cMessage *msg);
00270   void updateStatusCCA(t_mac_event event, cMessage *msg);
00271   void updateStatusTransmitFrame(t_mac_event event, cMessage *msg);
00272   void updateStatusWaitAck(t_mac_event event, cMessage *msg);
00273   void updateStatusSIFS(t_mac_event event, cMessage *msg);
00274   void updateStatusTransmitAck(t_mac_event event, cMessage *msg);
00275   void updateStatusNotIdle(cMessage *msg);
00276   void manageQueue();
00277   void updateMacState(t_mac_states newMacState);
00278 
00279   void attachSignal(MacPkt* mac, simtime_t startTime);
00280   void manageMissingAck(t_mac_event event, cMessage *msg);
00281   void startTimer(t_mac_timer timer);
00282 
00283   virtual simtime_t scheduleBackoff();
00284 
00285   virtual cPacket *decapsMsg(MacPkt * macPkt);
00286   MacPkt * ackMessage;
00287 
00288   //sequence number for sending, map for the general case with more senders
00289   //also in initialisation phase multiple potential parents
00290   std::map<int, unsigned long> SeqNrParent; //parent -> sequence number
00291 
00292   //sequence numbers for receiving
00293   std::map<int, unsigned long> SeqNrChild; //child -> sequence number
00294 
00295 };
00296 
00297 #endif
00298