p802_15_4transac.cc

Go to the documentation of this file.
00001 /********************************************/
00002 /*     NS2 Simulator for IEEE 802.15.4      */
00003 /*           (per P802.15.4/D18)            */
00004 /*------------------------------------------*/
00005 /* by:        Jianliang Zheng               */
00006 /*        (zheng@ee.ccny.cuny.edu)          */
00007 /*              Myung J. Lee                */
00008 /*          (lee@ccny.cuny.edu)             */
00009 /*        ~~~~~~~~~~~~~~~~~~~~~~~~~         */
00010 /*           SAIT-CUNY Joint Lab            */
00011 /********************************************/
00012 
00013 // File:  p802_15_4transac.cc
00014 // Mode:  C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t
00015 
00016 // $Header: /nfs/jade/vint/CVSROOT/ns-2/wpan/p802_15_4transac.cc,v 1.2 2005/07/13 03:51:34 tomh Exp $
00017 
00018 /*
00019  * Copyright (c) 2003-2004 Samsung Advanced Institute of Technology and
00020  * The City University of New York. All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions
00024  * are met:
00025  * 1. Redistributions of source code must retain the above copyright
00026  *    notice, this list of conditions and the following disclaimer.
00027  * 2. Redistributions in binary form must reproduce the above copyright
00028  *    notice, this list of conditions and the following disclaimer in the
00029  *    documentation and/or other materials provided with the distribution.
00030  * 3. All advertising materials mentioning features or use of this software
00031  *    must display the following acknowledgement:
00032  *  This product includes software developed by the Joint Lab of Samsung 
00033  *      Advanced Institute of Technology and The City University of New York.
00034  * 4. Neither the name of Samsung Advanced Institute of Technology nor of 
00035  *    The City University of New York may be used to endorse or promote 
00036  *    products derived from this software without specific prior written 
00037  *    permission.
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE JOINT LAB OF SAMSUNG ADVANCED INSTITUTE
00040  * OF TECHNOLOGY AND THE CITY UNIVERSITY OF NEW YORK ``AS IS'' AND ANY EXPRESS 
00041  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00042  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
00043  * NO EVENT SHALL SAMSUNG ADVANCED INSTITUTE OR THE CITY UNIVERSITY OF NEW YORK 
00044  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00045  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00046  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00048  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00049  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00050  */
00051 
00052 
00053 #include <scheduler.h>
00054 #include "p802_15_4transac.h"
00055 #include "p802_15_4pkt.h"
00056 
00057 
00058 int addDeviceLink(DEVICELINK **deviceLink1, DEVICELINK **deviceLink2, IE3ADDR addr, UINT_8 cap)
00059 {
00060     DEVICELINK *tmp;
00061     if(*deviceLink2 == NULL)        //not exist yet
00062     {
00063         *deviceLink2 = new DEVICELINK(addr,cap);
00064         if (*deviceLink2 == NULL) return 1;
00065         *deviceLink1 = *deviceLink2;
00066     }
00067     else
00068     {
00069         tmp=new DEVICELINK(addr,cap);
00070         if (tmp == NULL) return 1;
00071         tmp->last = *deviceLink2;
00072         (*deviceLink2)->next = tmp;
00073         *deviceLink2 = tmp;
00074     }
00075     return 0;
00076 }
00077 
00078 int updateDeviceLink(int oper, DEVICELINK **deviceLink1, DEVICELINK **deviceLink2, IE3ADDR addr)
00079 {
00080     DEVICELINK *tmp;
00081     int rt;
00082 
00083     rt = 1;
00084 
00085     tmp = *deviceLink1;
00086     while(tmp != NULL)
00087     {
00088         if(tmp->addr64 == addr)
00089         {
00090             if (oper == tr_oper_del)    //delete an element
00091             {
00092                 if(tmp->last != NULL)
00093                 {
00094                     tmp->last->next = tmp->next;
00095                     if(tmp->next != NULL)
00096                         tmp->next->last = tmp->last;
00097                     else
00098                         *deviceLink2 = tmp->last;
00099                 }
00100                 else if (tmp->next != NULL)
00101                 {
00102                     *deviceLink1 = tmp->next;
00103                     tmp->next->last = NULL;
00104                 }
00105                 else
00106                 {
00107                     *deviceLink1 = NULL;
00108                     *deviceLink2 = NULL;
00109                 }
00110                 delete tmp;
00111             }
00112             rt = 0;
00113             break;
00114         }
00115         tmp = tmp->next;
00116     }
00117     return rt;
00118 }
00119 
00120 int numberDeviceLink(DEVICELINK **deviceLink1)
00121 {
00122     DEVICELINK *tmp;
00123     int num;
00124 
00125     num = 0;
00126     tmp = *deviceLink1;
00127     while(tmp != NULL)
00128     {
00129         num++;
00130         tmp = tmp->next;
00131     }
00132     return num;
00133 }
00134 
00135 int chkAddDeviceLink(DEVICELINK **deviceLink1, DEVICELINK **deviceLink2, IE3ADDR addr, UINT_8 cap)
00136 {
00137         int i;
00138 
00139         i = updateDeviceLink(tr_oper_est, deviceLink1, deviceLink2, addr);
00140         if (i == 0) return 1;
00141         i = addDeviceLink(deviceLink1, deviceLink2, addr, cap);
00142         if (i == 0) return 0;
00143         else return 2;
00144 }
00145 
00146 void emptyDeviceLink(DEVICELINK **deviceLink1, DEVICELINK **deviceLink2)
00147 {
00148     DEVICELINK *tmp, *tmp2;
00149 
00150     if(*deviceLink1 != NULL)
00151     {
00152         tmp = *deviceLink1;
00153         while(tmp != NULL)
00154         {
00155             tmp2 = tmp;
00156             tmp = tmp->next;
00157             delete tmp2;
00158         }
00159         *deviceLink1 = NULL;
00160     }
00161     *deviceLink2 = *deviceLink1;
00162 }
00163 
00164 void dumpDeviceLink(DEVICELINK *deviceLink1, IE3ADDR coorAddr)
00165 {
00166     DEVICELINK *tmp;
00167     int i;
00168 
00169     fprintf(stdout, "[%.2f] --- dump associated device list (by coordinator %d) ---\n", Scheduler::instance().clock(), coorAddr);
00170     tmp = deviceLink1;
00171     i = 1;
00172     while(tmp != NULL)
00173     {
00174         fprintf(stdout, "\t%d:\taddress = 0x%x\n", i,tmp->addr64);
00175         tmp = tmp->next;
00176         i++;
00177     }
00178 }
00179 
00180 //--------------------------------------------------------------------------------------
00181 
00182 void purgeTransacLink(TRANSACLINK **transacLink1, TRANSACLINK **transacLink2)
00183 {
00184     //purge expired transactions
00185     TRANSACLINK *tmp,*tmp2;
00186 
00187     tmp = *transacLink1;
00188     while(tmp != NULL)
00189     {
00190         if (CURRENT_TIME > tmp->expTime)
00191         {
00192             tmp2 = tmp;
00193             if (tmp->next != NULL)
00194                 tmp = tmp->next->next;
00195             else
00196                 tmp = NULL;
00197             //--- delete the transaction ---
00198             //don't try to call updateTransacLink() -- to avoid re-entries of functions
00199             if(tmp2->last != NULL)
00200             {
00201                 tmp2->last->next = tmp2->next;
00202                 if(tmp2->next != NULL)
00203                     tmp2->next->last = tmp2->last;
00204                 else
00205                     *transacLink2 = tmp2->last;
00206             }
00207             else if (tmp2->next != NULL)
00208             {
00209                 *transacLink1 = tmp2->next;
00210                 tmp2->next->last = NULL;
00211             }
00212             else
00213             {
00214                 *transacLink1 = NULL;
00215                 *transacLink2 = NULL;
00216             }
00217             //free the packet first
00218             Packet::free(tmp2->pkt);
00219             delete tmp2;
00220             //--------------------------------
00221         }
00222         else
00223             tmp = tmp->next;
00224     }
00225 }
00226 
00227 int addTransacLink(TRANSACLINK **transacLink1, TRANSACLINK **transacLink2, UINT_8 pendAM, IE3ADDR pendAddr, Packet *p, UINT_8 msduH, double kpTime)
00228 {
00229     TRANSACLINK *tmp;
00230     if(*transacLink2 == NULL)       //not exist yet
00231     {
00232         *transacLink2 = new TRANSACLINK(pendAM,pendAddr,p,msduH,kpTime);
00233         if (*transacLink2 == NULL) return 1;
00234         *transacLink1 = *transacLink2;
00235     }
00236     else
00237     {
00238         tmp=new TRANSACLINK(pendAM,pendAddr,p,msduH,kpTime);
00239         if (tmp == NULL) return 1;
00240         tmp->last = *transacLink2;
00241         (*transacLink2)->next = tmp;
00242         *transacLink2 = tmp;
00243     }
00244     return 0;
00245 }
00246 
00247 Packet *getPktFrTransacLink(TRANSACLINK **transacLink1, UINT_8 pendAM, IE3ADDR pendAddr)
00248 {
00249     TRANSACLINK *tmp;
00250 
00251     tmp = *transacLink1;
00252 
00253     while(tmp != NULL)
00254     {
00255         if((tmp->pendAddrMode == pendAM)
00256         && (((pendAM == defFrmCtrl_AddrMode16)&&((UINT_16)pendAddr == tmp->pendAddr16))
00257          ||((pendAM == defFrmCtrl_AddrMode64)&&(pendAddr == tmp->pendAddr64))))
00258         {
00259             return tmp->pkt;
00260         }
00261         tmp = tmp->next;
00262     }
00263     return NULL;
00264 }
00265 
00266 int updateTransacLink(int oper, TRANSACLINK **transacLink1, TRANSACLINK **transacLink2, UINT_8 pendAM, IE3ADDR pendAddr)
00267 {
00268     TRANSACLINK *tmp;
00269     int rt;
00270 
00271     //purge first if (oper == tr_oper_est)
00272     if (oper == tr_oper_est)
00273         purgeTransacLink(transacLink1,transacLink2);
00274 
00275     rt = 1;
00276 
00277     tmp = *transacLink1;
00278 
00279     if (oper == tr_oper_EST)
00280     {
00281         while(tmp != NULL)
00282         {
00283             if((tmp->pendAddrMode == pendAM)
00284             && (((pendAM == defFrmCtrl_AddrMode16)&&((UINT_16)pendAddr == tmp->pendAddr16))
00285             ||((pendAM == defFrmCtrl_AddrMode64)&&(pendAddr == tmp->pendAddr64))))
00286             {
00287                 rt = 0;
00288                 tmp = tmp->next;
00289                 break;
00290             }
00291             tmp = tmp->next;
00292         }
00293         if (rt) return 1;
00294     }
00295 
00296     rt = 1;
00297 
00298     while(tmp != NULL)
00299     {
00300         if((tmp->pendAddrMode == pendAM)
00301         && (((pendAM == defFrmCtrl_AddrMode16)&&((UINT_16)pendAddr == tmp->pendAddr16))
00302          ||((pendAM == defFrmCtrl_AddrMode64)&&(pendAddr == tmp->pendAddr64))))
00303         {
00304             if (oper == tr_oper_del)    //delete an element
00305             {
00306                 if(tmp->last != NULL)
00307                 {
00308                     tmp->last->next = tmp->next;
00309                     if(tmp->next != NULL)
00310                         tmp->next->last = tmp->last;
00311                     else
00312                         *transacLink2 = tmp->last;
00313                 }
00314                 else if (tmp->next != NULL)
00315                 {
00316                     *transacLink1 = tmp->next;
00317                     tmp->next->last = NULL;
00318                 }
00319                 else
00320                 {
00321                     *transacLink1 = NULL;
00322                     *transacLink2 = NULL;
00323                 }
00324                 //free the packet first
00325                 Packet::free(tmp->pkt);
00326                 delete tmp;
00327             }
00328             rt = 0;
00329             break;
00330         }
00331         tmp = tmp->next;
00332     }
00333     return rt;
00334 }
00335 
00336 int updateTransacLinkByPktOrHandle(int oper, TRANSACLINK **transacLink1, TRANSACLINK **transacLink2, Packet *pkt, UINT_8 msduH)
00337 {
00338     TRANSACLINK *tmp;
00339     int rt;
00340     
00341     //purge first if (oper == tr_oper_est)
00342     if (oper == tr_oper_est)
00343         purgeTransacLink(transacLink1,transacLink2);
00344 
00345     rt = 1;
00346 
00347     tmp = *transacLink1;
00348 
00349     while(tmp != NULL)
00350     {
00351         if(((pkt != NULL)&&(tmp->pkt == pkt))
00352          ||((pkt == NULL)&&(tmp->msduHandle == msduH)))
00353         {
00354             if (oper == tr_oper_del)    //delete an element
00355             {
00356                 if(tmp->last != NULL)
00357                 {
00358                     tmp->last->next = tmp->next;
00359                     if(tmp->next != NULL)
00360                         tmp->next->last = tmp->last;
00361                     else
00362                         *transacLink2 = tmp->last;
00363                 }
00364                 else if (tmp->next != NULL)
00365                 {
00366                     *transacLink1 = tmp->next;
00367                     tmp->next->last = NULL;
00368                 }
00369                 else
00370                 {
00371                     *transacLink1 = NULL;
00372                     *transacLink2 = NULL;
00373                 }
00374                 //free the packet first
00375                 Packet::free(tmp->pkt);
00376                 delete tmp;
00377             }
00378             rt = 0;
00379             break;
00380         }
00381         tmp = tmp->next;
00382     }
00383     return rt;
00384 }
00385 
00386 int numberTransacLink(TRANSACLINK **transacLink1, TRANSACLINK **transacLink2)
00387 {
00388     //return the number of transactions in the link
00389     TRANSACLINK *tmp;
00390     int num;
00391 
00392     //purge first
00393     purgeTransacLink(transacLink1,transacLink2);
00394 
00395     num = 0;
00396     tmp = *transacLink1;
00397     while(tmp != NULL)
00398     {
00399         num++;
00400         tmp = tmp->next;
00401     }
00402     return num;
00403 }
00404 
00405 int chkAddTransacLink(TRANSACLINK **transacLink1, TRANSACLINK **transacLink2, UINT_8 pendAM, IE3ADDR pendAddr, Packet *p, UINT_8 msduH, double kpTime)
00406 {
00407     int i;
00408 
00409     //purge first
00410     purgeTransacLink(transacLink1,transacLink2);
00411 
00412     i = numberTransacLink(transacLink1,transacLink2);
00413     if (i >= maxNumTransactions) return 1;
00414 
00415     i = addTransacLink(transacLink1,transacLink2,pendAM,pendAddr,p,msduH,kpTime);
00416     if (i == 0) return 0;
00417     else return 2;
00418 }
00419 
00420 void emptyTransacLink(TRANSACLINK **transacLink1, TRANSACLINK **transacLink2)
00421 {
00422     TRANSACLINK *tmp, *tmp2;
00423 
00424     if(*transacLink1 != NULL)
00425     {
00426         tmp = *transacLink1;
00427         while(tmp != NULL)
00428         {
00429             tmp2 = tmp;
00430             tmp = tmp->next;
00431             //free the packet first
00432             Packet::free(tmp2->pkt);
00433             delete tmp2;
00434         }
00435         *transacLink1 = NULL;
00436     }
00437     *transacLink2 = *transacLink1;
00438 }
00439 
00440 void dumpTransacLink(TRANSACLINK *transacLink1, IE3ADDR coorAddr)
00441 {
00442     TRANSACLINK *tmp;
00443     int i;
00444     char tmpstr[121],tmpstr2[61];
00445     FrameCtrl frmCtrl;
00446 
00447     fprintf(stdout, "[%.2f] --- dump transaction list (by coordinator %d) ---\n", Scheduler::instance().clock(), coorAddr);
00448     tmp = transacLink1;
00449     i = 1;
00450     while(tmp != NULL)
00451     {
00452         sprintf(tmpstr, "\t%d:\t",i);
00453         if (tmp->pendAddrMode == defFrmCtrl_AddrMode16)
00454             sprintf(tmpstr2,"pendAddrMode = 16-bit\tpendAddr = %d\t",tmp->pendAddr16);
00455         else
00456             sprintf(tmpstr2,"pendAddrMode = 64-bit\tpendAddr = %d\t",tmp->pendAddr64);
00457         strcat(tmpstr,tmpstr2);
00458         frmCtrl.FrmCtrl = HDR_LRWPAN(tmp->pkt)->MHR_FrmCtrl;
00459         frmCtrl.parse();
00460         if (frmCtrl.frmType == defFrmCtrl_Type_Data)
00461             strcat(tmpstr,"pktType = data\t");
00462         else
00463             strcat(tmpstr,"pktType = command\t");
00464         sprintf(tmpstr2,"expTime = %f\n",tmp->expTime);
00465         strcat(tmpstr,tmpstr2);
00466         fprintf(stdout, "%s\n", tmpstr);
00467         tmp = tmp->next;
00468         i++;
00469     }
00470 }
00471 
00472 // End of file: p802_15_4transac.cc

Generated on Tue Mar 6 16:47:48 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6