00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <math.h>
00040 #include "../main/anemuras.h"
00041 #include "../main/event.h"
00042 #include "../main/packet.h"
00043 #include "../main/fileio.h"
00044 #include "../channel1/channel1.h"
00045
00046
00047
00048
00049
00050 #define UMTS_SLOT_TIME 0.000666
00051
00052 #define UMTS_RTT_VALUE 0.21
00053
00054 #define HANDOVER_TH_DB 1.2
00055
00056
00057 #define ACCEPT_POW_TH 10e-3
00058
00059
00060
00061 #define N_UPDATE_HANDOVER 150
00062
00063 #define SF_INIT_VALUE 16
00064
00065 #define POWER_MAX_UP -16
00066
00067 #define POWER_MIN_UP -96
00068
00069 #define POWER_MAX_DOWN -5
00070
00071 #define DELTA 0.5
00072
00073 #define POWER_CONTROL_DELAY 1
00074
00075 #define SIR_TARGET_DB 4.5
00076
00077 #define RX_POW_VALID_TIME 1e-6
00078
00079 #define ZETA 2.72
00080
00081 extern time clock;
00082 extern ANnode *globalPtr;
00083 extern char OUT_FILE[STRING_MAX_LENGTH];
00084 extern const ANid NODES_NUM;
00085 extern const ANid AP_NODES_MAC1_NUM;
00086 extern const ANid AP_NODES_MAC2_NUM;
00087 extern const ANid AP_NODES_NUM;
00088
00089 extern flag Mac2Available(ANid id);
00090
00091
00092 ANid AP_MAC2_TOT_NUM;
00093 unsigned int BS_ACTIVE_NUM;
00094
00095 typedef struct ANidList{
00096 ANid id;
00097 struct ANidList *toNextPtr;
00098 }ANidList;
00099 ANidList *nodesWantSync;
00100 ANid *BSlist;
00101 double po0 = 49.0/225.0;
00102 double f0 = 1;
00103 double No = 1e-32;
00104
00105
00106
00107
00108 unsigned int PHY2_SYMBOL_TEST;
00109
00110
00111 typedef enum{
00112 TX,
00113 RX,
00114 IDLE
00115 } PBState;
00116
00117 typedef enum{
00118
00119 WAIT_SYNC,
00120
00121
00122 PB_IDLE,
00123 } PBSubState;
00124
00125 typedef struct{
00126 unsigned long int txNo;
00127 unsigned long int txBits;
00128 } PBStats;
00129
00130 typedef struct{
00131 unsigned long int rxNo;
00132 unsigned long int rxBits;
00133 }BSStats;
00134
00135 typedef struct{
00136 flag isInCell;
00137 flag pktInFlyNow;
00138 ANpacket* pktInFly;
00139 ANsize currentBlockSize;
00140 time endRxPkt;
00141 ANsize bitCount;
00142 int fadingErrorNum;
00143 flag contextTransferDuringRx;
00144 ANtx_power rxPowLin;
00145 ANtx_power SIR;
00146 ANposition distance;
00147 time propDelay;
00148 }PBArrayEl;
00149
00150 typedef struct{
00151 PBArrayEl *PBarray;
00152 time lastRxPowUpdate;
00153 BSStats stats;
00154 }BSStruct;
00155
00156 typedef struct{
00157 ANpacket *TxPkt;
00158 ANid *BSlist;
00159 unsigned int sf;
00160 ANtx_power *BSgain;
00161 PBState state;
00162 PBSubState subState;
00163 PBStats stats;
00164 ANtx_power txPower;
00165 }PBStruct;
00166
00167
00168
00169
00170
00171 time Phy2NextBlockSymbolTest(ANid id,ANpacket *pkt){
00172 BSStruct *BSptr;
00173 double nextTest;
00174
00175 BSptr = (BSStruct *)NODEPTR(id)->Phy2StructPtr;
00176 if ((BSptr->PBarray[pkt->txId].bitCount + PHY2_SYMBOL_TEST) > pkt->size){
00177
00178 BSptr->PBarray[pkt->txId].currentBlockSize =
00179 pkt->size - BSptr->PBarray[pkt->txId].bitCount;
00180 nextTest = BSptr->PBarray[pkt->txId].endRxPkt;
00181 }else
00182 nextTest = clock + (PHY2_SYMBOL_TEST * (1/pkt->txRate));
00183
00184 return(nextTest);
00185 }
00186
00187
00188
00189
00190
00191 void Phy2UpdatePostMobility(ANid id){
00192 ANid i;
00193 BSStruct *BSptr;
00194 ANid *BStemp;
00195
00196 if (NODEPTR(id)->node_type==PAN_BAN){
00197 BStemp = BSlist;
00198
00199 for(i=0; i<AP_MAC2_TOT_NUM; i++){
00200 BSptr = (BSStruct *)NODEPTR(BStemp[i])->Phy2StructPtr;
00201 BSptr->PBarray[id].distance = Distance(BStemp[i],id);
00202 BSptr->PBarray[id].propDelay = Channel1PropDelay(BStemp[i],id);
00203 }
00204 BStemp = BSlist;
00205
00206
00207
00208
00209
00210
00211
00212
00213 }
00214 }
00215
00216
00217
00218
00219 double sf2datarate(unsigned int sf, linkType link)
00220 {
00221 if (link==DOWNLINK) {
00222 switch (sf)
00223 {
00224 case 4: return(1966080);
00225 case 8: return(983040);
00226 case 16: return(491520);
00227 case 32: return(245760);
00228 case 64: return(122880);
00229 case 128: return(61440);
00230 case 256: return(30720);
00231 case 512: return(15360);
00232 }
00233 } else {
00234 switch (sf)
00235 {
00236 case 4: return(983040);
00237 case 8: return(491520);
00238 case 16: return(245760);
00239 case 32: return(122880);
00240 case 64: return(61440);
00241 case 128: return(30720);
00242 case 256: return(15360);
00243 }
00244 }
00245 }
00246
00247
00248
00249
00250
00251 void Phy2RxPowUpdateUP(ANid BS){
00252 BSStruct *BSptr;
00253 PBStruct *PBptr;
00254 int i;
00255
00256 if (BS==0){
00257
00258 for(i=0;i<AP_MAC2_TOT_NUM; i++){
00259 BSptr = (BSStruct *)NODEPTR(BSlist[i])->Phy2StructPtr;
00260 if (BSptr->lastRxPowUpdate+RX_POW_VALID_TIME < clock){
00261 Phy2RxPowUpdateUP(BSlist[i]);
00262 }
00263 }
00264 }else{
00265 #ifdef control
00266 if ((Mac2Avaiable(BS)==FALSE) && (NODEPTR(BS)->node_type!=ACCESS_PROVIDER)){
00267 puts("Error Phy2RxPowUpdate, uncorrect BS");
00268 exit(1);
00269 }
00270 #endif
00271
00272
00273 BSptr = (BSStruct *)NODEPTR(BS)->Phy2StructPtr;
00274 for(i=1; i<=NODES_NUM;i++){
00275 if ((Mac2Available(i)==TRUE)&& (NODEPTR(i)->node_type==PAN_BAN)){
00276 PBptr = (PBStruct *)NODEPTR(i)->Phy2StructPtr;
00277 BSptr->PBarray[i].rxPowLin = FromDbToLinear(Channel1RxPower(i,BS,PBptr->txPower));
00278
00279
00280 }
00281 }
00282 BSptr->lastRxPowUpdate = clock;
00283 }
00284 }
00285
00286
00287
00288
00289 ANtx_power Phy2ComputeSIR(ANid BS, ANid PB){
00290 BSStruct *BSptr;
00291 PBStruct *PBptr;
00292 int i;
00293 ANtx_power util, interIntra, interExtra,rxPow,SIR;
00294
00295 #ifdef control
00296 if ( (Mac2Available(BS)==FALSE) && (NODEPTR(BS)->node_type!=ACCESS_PROVIDER)){
00297 puts("Error Phy2RxPowUpdate, uncorrect BS");
00298 exit(1);
00299 }
00300 if ((Mac2Available==FALSE) && (NODEPTR(PB)->node_type!=PAN_BAN)){
00301 puts("Error Phy2RxPowUpdate, uncorrect PB");
00302 exit(1);
00303 }
00304 #endif
00305 BSptr = (BSStruct *)NODEPTR(BS)->Phy2StructPtr;
00306 PBptr = (PBStruct *)NODEPTR(PB)->Phy2StructPtr;
00307
00308 if (BSptr->lastRxPowUpdate+RX_POW_VALID_TIME < clock)
00309 Phy2RxPowUpdateUP(BS);
00310
00311 util = BSptr->PBarray[PB].rxPowLin * (1./(1.+po0));
00312 interIntra = 0.0;
00313 interExtra = 0.0;
00314 for (i=1;i<NODES_NUM;i++){
00315 if ((Mac2Available(i)==TRUE) && (NODEPTR(i)->node_type==PAN_BAN) && (i!=PB) ){
00316 rxPow = BSptr->PBarray[i].rxPowLin;
00317 if (BSptr->PBarray[i].pktInFlyNow==FALSE)
00318
00319 rxPow *= (po0/(1.+po0));
00320 if (BSptr->PBarray[i].isInCell==TRUE)
00321
00322 interIntra += rxPow;
00323 else
00324
00325 interExtra += rxPow;
00326 }
00327 }
00328 SIR = (PBptr->sf * util) / (f0*interIntra + interExtra + No);
00329
00330
00331 return(SIR);
00332 }
00333
00334
00335
00336
00337 void Phy2GetUmtsSync(ANid id){
00338 ANidList *ptr;
00339 ANidList *temp;
00340
00341 ptr = nodesWantSync;
00342
00343 if (nodesWantSync==NULL){
00344
00345 ptr = (ANidList *)malloc(sizeof(ANidList));
00346 if (ptr==NULL){
00347 puts("Error Phy2GetUmtsSync, ptr malloc error");
00348 exit(1);
00349 }
00350 ptr->id = id;
00351 ptr->toNextPtr = NULL;
00352 nodesWantSync = ptr;
00353 }else {
00354
00355 while(ptr->toNextPtr!=NULL) ptr = ptr->toNextPtr;
00356 temp = (ANidList *)malloc(sizeof(ANidList));
00357 if (temp==NULL){
00358 puts("Error Phy2GetUmtsSync, temp malloc error");
00359 exit(1);
00360 }
00361 temp->id = id;
00362 temp->toNextPtr = NULL;
00363 ptr->toNextPtr = temp;
00364 }
00365 return;
00366 }
00367
00368
00369
00370
00371 void Phy2Synchronization(ANid id){
00372 ANidList *ptr;
00373 ANidList *last;
00374 int counter=0;
00375
00376 ptr = nodesWantSync;
00377 while(ptr!=NULL){
00378 EventNew(ptr->id, PHY2, UMTS_SLOT_BEGIN, clock, (void *)NULL);
00379 last = ptr;
00380 ptr = ptr->toNextPtr;
00381 free(last);
00382 }
00383 nodesWantSync = NULL;
00384 EventNew(id, PHY2, UMTS_SYNC,clock + UMTS_SLOT_TIME, (void *) NULL);
00385 }
00386
00387
00388 void PrintfList(ANid *BSlist, ANtx_power *gainlist){
00389 int i;
00390
00391 for(i=0; i<AP_MAC2_TOT_NUM;i++)
00392 printf("( %d, %2.1f)", BSlist[i],gainlist[i]);
00393 printf("\n");
00394 }
00395
00396
00397
00398 void Phy2HandoverUpdate(ANid id){
00399 PBStruct *PBptr;
00400 ANid *BStemp;
00401 BSStruct *BSptr;
00402 ANid idtemp;
00403 ANtx_power gaintemp;
00404 ANtx_power *BSgainTemp;
00405 double *vectValue;
00406 int i,j,k,dum,counter;
00407 HandoverStruct *HSptr;
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 for(i=1; i<=NODES_NUM; i++){
00419 PBptr = (PBStruct *)NODEPTR(i)->Phy2StructPtr;
00420 if ((Mac2Available(i)==TRUE) && (NODEPTR(i)->node_type==PAN_BAN)){
00421 vectValue = (double *)malloc(sizeof(double)*AP_MAC2_TOT_NUM);
00422 if (vectValue==NULL){
00423 puts("Error Phy2UpdateBS, vaectValue malloc error");
00424 exit(1);
00425 }
00426 for(j=0; j<AP_MAC2_TOT_NUM; j++)
00427 vectValue[j] = Phy2ComputeSIR(BSlist[j],i);
00428
00429
00430 if (PBptr->BSlist==NULL){
00431
00432
00433 for(j=0; j<AP_MAC2_TOT_NUM -1; j++)
00434 for(k=j; k<AP_MAC2_TOT_NUM; k++)
00435 if (vectValue[j]<vectValue[k]){
00436 gaintemp = vectValue[k];
00437 idtemp = BSlist[k];
00438 vectValue[k] = vectValue[j];
00439 BSlist[k] = BSlist[j];
00440 vectValue[j] = gaintemp;
00441 BSlist[j] = idtemp;
00442 }
00443
00444
00445 BStemp = (ANid *)malloc(sizeof(ANid)*AP_MAC2_TOT_NUM);
00446 BSgainTemp = (ANtx_power *)malloc(sizeof(ANtx_power)*AP_MAC2_TOT_NUM);
00447 if ((BStemp==NULL)||(BSgainTemp==NULL)){
00448 puts("Error Phy2UpdateBs, BStemp BSgainTemp malloc error");
00449 exit(1);
00450 }
00451 for(j=0; j<AP_MAC2_TOT_NUM; j++){
00452 BStemp[j] = BSlist[j];
00453 BSgainTemp[j] = vectValue[j];
00454 }
00455 PBptr->BSlist = BStemp;
00456 PBptr->BSgain = BSgainTemp;
00457
00458
00459
00460 for(j=0;j<BS_ACTIVE_NUM;j++){
00461 BSptr = (BSStruct *)NODEPTR(PBptr->BSlist[j])->Phy2StructPtr;
00462 BSptr->PBarray[i].isInCell = TRUE;
00463 }
00464 }else{
00465
00466 BStemp = (ANid *)malloc(sizeof(ANid)*AP_MAC2_TOT_NUM);
00467 if ((BStemp==NULL)||(BSgainTemp==NULL)){
00468 puts("Error Phy2UpdateBs, BStemp BSgainTemp malloc error");
00469 exit(1);
00470 }
00471
00472
00473
00474
00475 for(j=0;j<AP_MAC2_TOT_NUM;j++){
00476 dum = 0;
00477 for(k=0;k<AP_MAC2_TOT_NUM;k++)
00478 if ((PBptr->BSlist[k]==BSlist[j])&&(k<BS_ACTIVE_NUM)) {
00479 dum=1;
00480 break;
00481 }
00482 if (dum)
00483
00484 vectValue[j] = vectValue[j] + HANDOVER_TH_DB;
00485 }
00486
00487 for(j=0; j<AP_MAC2_TOT_NUM -1; j++)
00488 for(k=j; k<AP_MAC2_TOT_NUM; k++)
00489 if (vectValue[j]<vectValue[k]){
00490 gaintemp = vectValue[k];
00491 idtemp = BSlist[k];
00492 vectValue[k] = vectValue[j];
00493 BSlist[k] = BSlist[j];
00494 vectValue[j] = gaintemp;
00495 BSlist[j] = idtemp;
00496 }
00497
00498
00499
00500 counter = 0;
00501 for(j=0; j<BS_ACTIVE_NUM; j++){
00502 dum = 0;
00503 for(k=0; k<BS_ACTIVE_NUM;k++)
00504 if (PBptr->BSlist[j]==BSlist[k]){
00505 dum = 1;
00506 break;
00507 }
00508 if (!dum){
00509
00510 BStemp[counter] = PBptr->BSlist[j];
00511 counter++;
00512 }
00513 }
00514 while(counter>0){
00515 for(j=0; j<BS_ACTIVE_NUM; j++){
00516 dum = 0;
00517 for(k=0; k<BS_ACTIVE_NUM;k++)
00518 if(BSlist[j]==PBptr->BSlist[k]){
00519 dum = 1;
00520 break;
00521 }
00522 if (!dum){
00523
00524 HSptr = (HandoverStruct *)malloc(sizeof(HandoverStruct));
00525 HSptr->oldBS = BStemp[counter-1];
00526 HSptr->newBS = BSlist[j];
00527 HSptr->node = i;
00528 EventNew(BSlist[j], MAC2L, CONTEXT_TRANSFER, clock, (void *)HSptr);
00529
00530 #ifdef debug
00531 printf("Context Transfer nodo %d from AP %d to AP %d\n",i,
00532 BStemp[counter-1],BSlist[j]);
00533 #endif
00534
00535 BSptr = (BSStruct *)NODEPTR(BStemp[counter - 1])->Phy2StructPtr;
00536 BSptr->PBarray[i].isInCell = FALSE;
00537 BSptr = (BSStruct *)NODEPTR(BSlist[j])->Phy2StructPtr;
00538 BSptr->PBarray[i].isInCell = TRUE;
00539 counter--;
00540 }
00541 }
00542 }
00543 free(BStemp);
00544
00545 for(j=0; j<AP_MAC2_TOT_NUM; j++)
00546 PBptr->BSlist[j] = BSlist[j];
00547 }
00548 }
00549 }
00550
00551 EventNew(id, PHY2, HANDOVER_UPDATE, clock + ((double)(N_UPDATE_HANDOVER*UMTS_SLOT_TIME)),
00552 (void *)NULL);
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 return;
00563 }
00564
00565
00566
00567 void Phy2Init(){
00568 ANid i,j,k;
00569 BSStruct *BSptr;
00570 PBStruct *PBptr;
00571 PBArrayEl *array;
00572 ANid *AParray;
00573 int counter;
00574
00575
00576 if(ReadUInt("BS_ACTIVE_NUM",&(BS_ACTIVE_NUM))==FALSE) {
00577 puts("UMTS_MacInit: BS_ACTIVE_NUM not defined in configuration file");
00578 exit(1);
00579 }
00580 AP_MAC2_TOT_NUM = AP_NODES_NUM - AP_NODES_MAC1_NUM;
00581 if (BS_ACTIVE_NUM>AP_MAC2_TOT_NUM){
00582 puts("Error Phy2Init, BS_ACTIVE_NUM>AP_MAC2_TOT_NUM");
00583 exit(1);
00584 }
00585 if ((AP_MAC2_TOT_NUM>0)&&(BS_ACTIVE_NUM<=0)){
00586 puts("Error Phy2Init, there's mac2 node but any BS");
00587 exit(1);
00588 }
00589 if(ReadUInt("PHY2_SYMBOL_TEST",&(PHY2_SYMBOL_TEST))==FALSE)
00590 PHY2_SYMBOL_TEST = 1;
00591 if (PHY2_SYMBOL_TEST < 1){
00592 puts("Error Phy2Init, PHY2_SYMBOL_TEST must be an integer equal or greater than 1");
00593 exit(1);
00594 }
00595 printf("PHY2_SYMBOL_TEST = %d\n",PHY2_SYMBOL_TEST);
00596 nodesWantSync = NULL;
00597
00598 AParray = (ANid *)malloc(sizeof(ANid)*AP_MAC2_TOT_NUM);
00599 if (AParray==NULL){
00600 puts("Error Phy2Init, AParray malloc error");
00601 exit(1);
00602 }
00603 counter = 0;
00604 i = 1;
00605 while ((counter<AP_MAC2_TOT_NUM)&&(i<=NODES_NUM)){
00606 if ((NODEPTR(i)->node_type==ACCESS_PROVIDER) && (Mac2Available(i)==TRUE) ){
00607 AParray[counter] = i;
00608 counter ++;
00609 }
00610 i++;
00611 }
00612 if (counter!=AP_MAC2_TOT_NUM){
00613 puts("Error Phy2Init, doesn't find all the mac2 Access Provider");
00614 exit(1);
00615 }
00616 BSlist = AParray;
00617 #ifdef debug
00618 printf("Generata lista AP:");
00619 for(i=0; i<AP_MAC2_TOT_NUM; i++)
00620 printf(" %d",BSlist[i]);
00621 printf("\n");
00622 #endif
00623
00624 for(i=1; i<=NODES_NUM; i++){
00625 if (Mac2Available(i)==TRUE){
00626 switch(NODEPTR(i)->node_type){
00627 case PAN_BAN:
00628 PBptr = (PBStruct *)malloc(sizeof(PBStruct));
00629 if (PBptr==NULL){
00630 puts("Error Phy2Init, PBptr malloc error");
00631 exit(1);
00632 }
00633 PBptr->TxPkt = NULL;
00634 PBptr->state = IDLE;
00635 PBptr->subState = PB_IDLE;
00636 PBptr->BSlist = NULL;
00637 PBptr->BSgain = NULL;
00638 PBptr->txPower = POWER_MAX_UP;
00639 PBptr->sf = SF_INIT_VALUE;
00640 PBptr->stats.txNo = 0;
00641 PBptr->stats.txBits = 0;
00642 NODEPTR(i)->Phy2StructPtr = (void *)PBptr;
00643 break;
00644 case ACCESS_PROVIDER:
00645 BSptr = (BSStruct *)malloc(sizeof(BSStruct));
00646 if (BSptr==NULL){
00647 puts("Error Phy2Init, BSptr malloc error");
00648 exit(1);
00649 }
00650
00651
00652 array = (PBArrayEl *)malloc(sizeof(PBArrayEl)*(NODES_NUM+1));
00653 if (array==NULL){
00654 puts("Phy2Init: array malloc error");
00655 exit(1);
00656 }
00657 for(j=1; j<=NODES_NUM;j++){
00658 array[j].isInCell = FALSE;
00659 array[j].pktInFlyNow = FALSE;
00660 array[j].pktInFly = NULL;
00661 array[j].bitCount = 0;
00662 array[j].currentBlockSize = PHY2_SYMBOL_TEST;
00663 array[j].endRxPkt = 0.0;
00664 array[j].rxPowLin = 0.0;
00665 array[j].contextTransferDuringRx = FALSE;
00666 array[j].SIR = 0.0;
00667 array[j].distance = Distance(i,j);
00668 array[j].propDelay = Channel1PropDelay(i,j);
00669 }
00670 BSptr->PBarray = array;
00671
00672
00673 BSptr->lastRxPowUpdate = -1.0;
00674 BSptr->stats.rxNo = 0;
00675 BSptr->stats.rxBits = 0;
00676 NODEPTR(i)->Phy2StructPtr = (void *)BSptr;
00677 break;
00678 default:
00679 puts("Error Phy2Init, node type not recognized");
00680 exit(1);
00681 }
00682
00683 }
00684 }
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717 if (AP_MAC2_TOT_NUM>0){
00718 #ifdef debug
00719 printf("SYNC generato da AP %d\n",AParray[0]);
00720 #endif
00721 EventNew(AParray[0], PHY2, UMTS_SYNC,clock + UMTS_SLOT_TIME, (void *) NULL);
00722 EventNew(AParray[0], PHY2, HANDOVER_UPDATE, clock, (void *)NULL);
00723 EventNew(AParray[0], PHY2, POWER_CONTROL, clock +
00724 ((double)(POWER_CONTROL_DELAY*UMTS_SLOT_TIME)), (void *)NULL);
00725 }
00726 return;
00727 }
00728
00729
00730
00731
00732 void Phy2PowerControl(ANid id){
00733 PBStruct *PBptr;
00734 ANid *qptr;
00735 int deltaSum,i,j;
00736 double SIR;
00737 static int count=0;
00738 FILE *fd;
00739 char filename[STRING_MAX_LENGTH];
00740
00741 Phy2RxPowUpdateUP(0);
00742 #ifdef debug
00743 count++;
00744 #endif
00745
00746
00747
00748
00749
00750
00751 for(i=1;i<=NODES_NUM;i++){
00752 if ((Mac2Available(i)==TRUE) &&(NODEPTR(i)->node_type==PAN_BAN)){
00753 PBptr = (PBStruct *)NODEPTR(i)->Phy2StructPtr;
00754 #ifdef debug
00755 if (count==500) printf("POW CONTROL PB %d ",i);
00756 #endif
00757 deltaSum = 0;
00758 for(j=0;j<BS_ACTIVE_NUM;j++){
00759 if ((SIR=Phy2ComputeSIR(PBptr->BSlist[j],i))>SIR_TARGET_DB)
00760 deltaSum--;
00761 else deltaSum++;
00762
00763 }
00764 if (deltaSum>0){
00765 if (PBptr->txPower<POWER_MAX_UP) PBptr->txPower += DELTA;
00766 }else
00767 if (PBptr->txPower>POWER_MIN_UP) PBptr->txPower -= DELTA;
00768
00769
00770
00771
00772 #ifdef debug
00773 if (count==500) printf(" txPow %2.2f\n",PBptr->txPower);
00774 #endif
00775 }
00776 }
00777
00778
00779
00780
00781
00782
00783 EventNew(id, PHY2, POWER_CONTROL, clock +
00784 ((double)(POWER_CONTROL_DELAY*UMTS_SLOT_TIME)), (void *)NULL);
00785 #ifdef debug
00786 if (count==500) count=0;
00787 #endif
00788 }
00789
00790
00791
00792 void Phy2UmtsSlotBegin(ANid id){
00793 PBStruct *temp;
00794 ANid *AParray;
00795 ANpacket *pktDup;
00796 BSStruct *BSptr;
00797 double endTx;
00798 int i;
00799
00800 temp = (PBStruct *)NODEPTR(id)->Phy2StructPtr;
00801 #ifdef control
00802 if ((temp->state!=TX)||(temp->subState!=WAIT_SYNC)){
00803 puts("Error Phy2UmtsSlotBegin, uncorrect state");
00804 exit(1);
00805 }
00806 #endif
00807
00808 #ifdef debug
00809 printf("2- Node %d in sending umts pkt %d to all AP time %.10f\n",id, temp->TxPkt->UMTSpktId, clock);
00810 #endif
00811 for(i=0; i<AP_MAC2_TOT_NUM; i++){
00812 pktDup = PacketDuplicate(temp->TxPkt);
00813 pktDup->nextHop = temp->BSlist[i];
00814 BSptr = (BSStruct *)NODEPTR(BSlist[i])->Phy2StructPtr;
00815 EventNew(temp->BSlist[i], PHY2, BS_RX_UMTS_PKT, clock +
00816 BSptr->PBarray[pktDup->txId].propDelay,(void *)pktDup);
00817 }
00818 endTx = ((double)(pktDup->size))/pktDup->txRate;
00819 EventNew(id, MAC2L, PACKET_TXED, clock + endTx, (void *)NULL);
00820 PacketFree(temp->TxPkt);
00821 temp->TxPkt = NULL;
00822 }
00823
00824
00825
00826
00827 void Phy2PBSendPacket(ANid id){
00828 ANpacket *pktPtr;
00829 PBStruct *temp;
00830
00831 pktPtr = (ANpacket *)NODEPTR(id)->eventQPtr->extraStructPtr;
00832 temp = (PBStruct *)NODEPTR(id)->Phy2StructPtr;
00833
00834 temp->state = TX;
00835 temp->subState = WAIT_SYNC;
00836 temp->TxPkt = pktPtr;
00837
00838 Phy2GetUmtsSync(id);
00839 return;
00840 }
00841
00842
00843
00844
00845 void Phy2SendPacketInfo(ANid id){
00846 ANpacket *pktPtr;
00847 PBStruct *temp;
00848 ANpacket *pktDup;
00849 int i;
00850
00851 pktPtr = (ANpacket *)NODEPTR(id)->eventQPtr->extraStructPtr;
00852 temp = (PBStruct *)NODEPTR(id)->Phy2StructPtr;
00853 for(i=0; i<BS_ACTIVE_NUM; i++){
00854 pktDup = PacketDuplicate(pktPtr);
00855 #ifdef debug
00856 printf("2- Node %d in sending info abuot umts pkt %d to AP %d, time %.10f\n",id, pktDup->UMTSpktId,
00857 temp->BSlist[i], clock);
00858 #endif
00859 pktDup->nextHop = temp->BSlist[i];
00860 EventNew(temp->BSlist[i], MAC2L, RX_PACKET_INFO, clock ,(void *)pktDup);
00861 }
00862 PacketFree(pktPtr);
00863 }
00864
00865
00866
00867 void Phy2EndRxPacket(ANid id){
00868 BSStruct *BSptr;
00869 ANid *sorgPtr;
00870 ANid sorg;
00871 ANpacket *pktRx;
00872 unsigned long int *pktIdptr;
00873
00874 sorgPtr = (ANid *)NODEPTR(id)->eventQPtr->extraStructPtr;
00875 sorg = *sorgPtr;
00876 free(sorgPtr);
00877 BSptr = (BSStruct *)NODEPTR(id)->Phy2StructPtr;
00878 BSptr->PBarray[sorg].pktInFlyNow = FALSE;
00879 if (BSptr->PBarray[sorg].contextTransferDuringRx==TRUE){
00880 pktRx = BSptr->PBarray[sorg].pktInFly;
00881 if (pktRx==NULL)
00882 printf("BS %d nothing to discared...\n",id);
00883
00884
00885 else {
00886 printf("BS %d discared pkt %d.%d,%d for context transfer\n",id,pktRx->sourceId,
00887 pktRx->packetId,pktRx->UMTSpktId);
00888 PacketFree(BSptr->PBarray[sorg].pktInFly);
00889 BSptr->PBarray[sorg].pktInFly = NULL;
00890 }
00891 BSptr->PBarray[sorg].contextTransferDuringRx=FALSE;
00892 return;
00893 }
00894 if (BSptr->PBarray[sorg].pktInFly!=NULL){
00895
00896 pktRx = BSptr->PBarray[sorg].pktInFly;
00897 pktRx->lastTech = MAC2;
00898 if (pktRx->fadingErrorNum==0){
00899
00900 if (pktRx->nextHop!=id){
00901
00902 printf("BS %d rx pkt not for me from %d pkt (%d.%d)\n",id, pktRx->txId,
00903 pktRx->sourceId,pktRx->packetId);
00904 PacketFree(pktRx);
00905 return;
00906 }
00907 if (BSptr->PBarray[sorg].isInCell==FALSE){
00908
00909 printf("BS %d rx pkt from other cell from %d pkt (%d.%d)\n",id, pktRx->txId,
00910 pktRx->sourceId,pktRx->packetId);
00911 PacketFree(pktRx);
00912 return;
00913 }
00914
00915 #ifdef debug
00916 printf("BS %d rx pkt pkt %d.%d(%d)\n",id, pktRx->sourceId,
00917 pktRx->packetId, pktRx->UMTSpktId);
00918 #endif
00919 EventNew(id, MAC2L, RX_UMTS_PACKET, clock, (void *)pktRx);
00920 }else {
00921
00922 if ((pktRx->nextHop==id)&&(BSptr->PBarray[sorg].isInCell==TRUE)){
00923
00924 pktIdptr = (unsigned long int *)malloc(sizeof(unsigned long int));
00925 *pktIdptr = pktRx->UMTSpktId;
00926 EventNew(pktRx->txId, MAC2L, NACK_ARRIVED, clock + UMTS_RTT_VALUE, (void *)pktIdptr);
00927 #ifdef debug
00928 printf("BS %d rx corrupt pkt %d.%d,%d at %.10f\n",id, pktRx->sourceId,
00929 pktRx->packetId, pktRx->UMTSpktId, clock);
00930 #endif
00931 }
00932 PacketFree(pktRx);
00933 }
00934 BSptr->PBarray[sorg].pktInFly = NULL;
00935 }
00936 return;
00937 }
00938
00939
00940
00941
00942 void Phy2TestPktSymbol(ANid id){
00943 BSStruct *BSptr;
00944 ANid *sorgPtr;
00945 ANid sorg;
00946 ANtx_power SIR;
00947 ANpacket *pkt;
00948 double random,Pe,PeBlock;
00949 unsigned int error=0;
00950 double endRx,nextTest;
00951
00952 sorgPtr = (ANid *)NODEPTR(id)->eventQPtr->extraStructPtr;
00953 sorg = *sorgPtr;
00954 BSptr = (BSStruct *)NODEPTR(id)->Phy2StructPtr;
00955 BSptr->PBarray[sorg].bitCount += BSptr->PBarray[sorg].currentBlockSize;
00956 if (BSptr->lastRxPowUpdate+RX_POW_VALID_TIME < clock)
00957 Phy2RxPowUpdateUP(id);
00958 SIR = Phy2ComputeSIR(id,sorg);
00959
00960 Pe = 0.5*erfc(sqrt(pow(SIR,ZETA)));
00961 random = ((float)rand())/((float)RAND_MAX);
00962 if (BSptr->PBarray[sorg].pktInFly==NULL){
00963 printf("Error Phy2TestPktSymbol, doesn't find pkt to test in structure\n");
00964 exit(1);
00965 }
00966 if (BSptr->PBarray[sorg].currentBlockSize==1)
00967
00968 error = (random<Pe?1:0);
00969 else{
00970
00971 PeBlock = 1 - pow(1 - Pe,BSptr->PBarray[sorg].currentBlockSize);
00972 error = (random<PeBlock?1:0);
00973 }
00974 if (error==1){
00975 BSptr->PBarray[sorg].pktInFly->fadingErrorNum++;
00976 endRx = clock + ( ((double)(BSptr->PBarray[sorg].pktInFly->size - BSptr->PBarray[sorg].bitCount)) *
00977 (1/BSptr->PBarray[sorg].pktInFly->txRate) );
00978
00979
00980
00981 EventNew(id, PHY2, END_RX_PACKET, endRx, (void *)sorgPtr);
00982 return;
00983 }
00984
00985
00986
00987
00988
00989
00990
00991 if (BSptr->PBarray[sorg].bitCount==BSptr->PBarray[sorg].pktInFly->size){
00992
00993 EventNew(id, PHY2, END_RX_PACKET, clock, (void *)sorgPtr);
00994
00995
00996 }else{
00997 if (BSptr->PBarray[sorg].currentBlockSize==1)
00998
00999 EventNew(id, PHY2, TEST_PKT_SYMBOL, clock +
01000 (1/BSptr->PBarray[sorg].pktInFly->txRate), (void *)sorgPtr);
01001 else {
01002 nextTest = Phy2NextBlockSymbolTest(id,BSptr->PBarray[sorg].pktInFly);
01003 EventNew(id, PHY2, TEST_PKT_SYMBOL, nextTest, (void *)sorgPtr);
01004
01005
01006 }
01007 }
01008 }
01009
01010
01011
01012
01013 void Phy2BSRxUmtsPkt(ANid id){
01014 BSStruct *BSptr;
01015 ANpacket *pktptr;
01016 ANid *sorgPtr;
01017 time endRx;
01018 double nextTest;
01019
01020 #ifdef control
01021 if ((Mac2Available(i)==FALSE) || (NODEPTR(id)->node_type!=ACCESS_PROVIDER)){
01022 puts("Error Phy2BSRxUmtsPkt, the node isn't a BS");
01023 exit(1);
01024 }
01025 #endif
01026 BSptr = (BSStruct *)NODEPTR(id)->Phy2StructPtr;
01027 pktptr = (ANpacket *)NODEPTR(id)->eventQPtr->extraStructPtr;
01028 #ifdef control
01029 if (BSptr->PBarray[pktptr->txId].pktInFlyNow==TRUE){
01030 puts("Error Phy2BSRxUmtsPkt, BS is receiving two pkt from the same PB");
01031 exit(1);
01032 }
01033 #endif
01034 pktptr->fadingErrorNum = 0;
01035 if (BSptr->PBarray[pktptr->txId].isInCell==TRUE){
01036
01037 BSptr->PBarray[pktptr->txId].pktInFly = pktptr;
01038 BSptr->PBarray[pktptr->txId].bitCount = 0;
01039 BSptr->PBarray[pktptr->txId].currentBlockSize = PHY2_SYMBOL_TEST;
01040 BSptr->PBarray[pktptr->txId].endRxPkt = clock + ((double)(pktptr->size))/pktptr->txRate;
01041
01042 sorgPtr = (ANid *)malloc(sizeof(ANid));
01043 *sorgPtr = pktptr->txId;
01044 if (PHY2_SYMBOL_TEST==1)
01045 EventNew(id, PHY2, TEST_PKT_SYMBOL, clock + (1.0/pktptr->txRate),
01046 (void *)sorgPtr);
01047 else{
01048 nextTest = Phy2NextBlockSymbolTest(id,
01049 BSptr->PBarray[pktptr->txId].pktInFly);
01050 EventNew(id, PHY2, TEST_PKT_SYMBOL, nextTest, (void *)sorgPtr);
01051 }
01052 }else {
01053
01054 endRx = ((double)(pktptr->size))/pktptr->txRate;
01055 sorgPtr = (ANid *)malloc(sizeof(ANid));
01056 *sorgPtr = pktptr->txId;
01057 EventNew(id, PHY2, END_RX_PACKET, clock + endRx, (void *)sorgPtr);
01058 }
01059 BSptr->PBarray[pktptr->txId].pktInFlyNow = TRUE;
01060 }
01061
01062
01063
01064
01065 void Phy2ContextTransfer(ANid id){
01066 BSStruct *BSptr;
01067 ANid *nodePtr;
01068 ANid node;
01069
01070 BSptr = (BSStruct *)NODEPTR(id)->Phy2StructPtr;
01071 nodePtr = (ANid *)NODEPTR(id)->eventQPtr->extraStructPtr;
01072 node = *nodePtr;
01073 free(nodePtr);
01074 BSptr->PBarray[node].contextTransferDuringRx = TRUE;
01075 }
01076
01077
01078
01079
01080 void Phy2SendSink(ANid id){
01081 ANpacket *pktPtr;
01082 BSStruct *temp;
01083 ANpacket *pktDup;
01084 int i;
01085
01086 #ifdef control
01087 if ((Mac2Available(i)==FALSE) || (NODEPTR(id)->node_type!=ACCESS_PROVIDER)){
01088 puts("Error Phy2SendSink, the node isn't a BS");
01089 exit(1);
01090 }
01091 #endif
01092 pktPtr = (ANpacket *)NODEPTR(id)->eventQPtr->extraStructPtr;
01093 pktPtr->txId = id;
01094 pktPtr->lastTech = MAC2;
01095 temp = (BSStruct *)NODEPTR(id)->Phy2StructPtr;
01096 for(i=1; i<=NODES_NUM; i++){
01097
01098 if (temp->PBarray[i].isInCell==TRUE){
01099 pktDup = PacketDuplicate(pktPtr);
01100 #ifdef debug
01101 printf("2- BS %d in sending SINK pkt %d to PB %d, time %.10f\n",id, pktDup->UMTSpktId,
01102 i, clock);
01103 #endif
01104 pktDup->nextHop = i;
01105
01106 EventNew(i, ROUTING, NEW_SINK_FOR_ROUTING, clock ,(void *)pktDup);
01107 }
01108 }
01109 PacketFree(pktPtr);
01110 }
01111
01112
01113
01114
01115
01116 void Phy2EventHandler(ANid id) {
01117
01118 if (Mac2Available(id)==TRUE){
01119 switch(NODEPTR(id)->eventQPtr->eventType) {
01120 case PB_SEND_PACKET:
01121 #ifdef debug
01122 printf("Nodo %d PHY2, event PB_SEND_PACKET time:%.10f\n", id, clock);
01123 #endif
01124 Phy2PBSendPacket(id);
01125 break;
01126 case UMTS_SYNC:
01127 #ifdef debug
01128
01129 #endif
01130 Phy2Synchronization(id);
01131 break;
01132 case UMTS_SLOT_BEGIN:
01133 #ifdef debug
01134 printf("Nodo %d PHY2, event UMTS_SLOT_BEGIN time:%.10f\n", id, clock);
01135 #endif
01136 Phy2UmtsSlotBegin(id);
01137 break;
01138 case BS_RX_UMTS_PKT:
01139 #ifdef debug
01140
01141 #endif
01142 Phy2BSRxUmtsPkt(id);
01143 break;
01144 case TEST_PKT_SYMBOL:
01145 #ifdef debug
01146
01147 #endif
01148 Phy2TestPktSymbol(id);
01149 break;
01150 case END_RX_PACKET:
01151 #ifdef debug
01152 printf("Nodo %d PHY2, event END_RX_PACKET time:%.10f\n", id, clock);
01153 #endif
01154 Phy2EndRxPacket(id);
01155 break;
01156 case HANDOVER_UPDATE:
01157 #ifdef debug
01158 printf("Nodo %d PHY2, event HANDOVER_UPDATE time:%.10f\n", id, clock);
01159 #endif
01160 Phy2HandoverUpdate(id);
01161 break;
01162 case UPDATE_POST_MOBILITY:
01163 #ifdef debug
01164
01165 #endif
01166 Phy2UpdatePostMobility(id);
01167 break;
01168 case POWER_CONTROL:
01169 #ifdef debug
01170 printf("Nodo %d PHY2, event POWER_CONTROL time:%.10f\n", id, clock);
01171 #endif
01172 Phy2PowerControl(id);
01173 break;
01174 case SEND_PACKET_INFO:
01175 #ifdef debug
01176 printf("Nodo %d PHY2, event SEND_PACKET_INFO:%.10f\n", id, clock);
01177 #endif
01178 Phy2SendPacketInfo(id);
01179 break;
01180 case CONTEXT_TRANSFER:
01181 #ifdef debug
01182 printf("Nodo %d PHY2, event CONTEXT_TRANSFER:%.10f\n", id, clock);
01183 #endif
01184 Phy2ContextTransfer(id);
01185 break;
01186 case SEND_SINK:
01187 #ifdef debug
01188 printf("Nodo %d PHY2, event SEND_SINK:%.10f\n", id, clock);
01189 #endif
01190 Phy2SendSink(id);
01191 break;
01192 default:
01193 puts("Error Phy2EventHandler, unreocgnized type of event");
01194 exit(1);
01195 }
01196 }
01197 }
01198
01199
01200
01201
01202
01203
01204 double Phy2SARAReadParameters(ANid id){
01205 BSStruct *BSptr;
01206 double Beff,Pe,SIR;
01207 int i,howManyInCell;
01208
01209 if (NODEPTR(id)->node_type!=ACCESS_PROVIDER){
01210 puts("Error Phy2SARAReadParameters, request node isn't AP");
01211 exit(1);
01212 }
01213 BSptr = (BSStruct *)NODEPTR(id)->Phy2StructPtr;
01214
01215 if (BSptr->lastRxPowUpdate+RX_POW_VALID_TIME < clock)
01216 Phy2RxPowUpdateUP(id);
01217 Beff = 0.0;
01218 howManyInCell = 0;
01219 for (i=1;i<NODES_NUM;i++){
01220 if ((Mac2Available(i)==TRUE) && (NODEPTR(i)->node_type==PAN_BAN) ){
01221 if (BSptr->PBarray[i].isInCell==TRUE){
01222 howManyInCell++;
01223 SIR = Phy2ComputeSIR(id, i);
01224 Pe = 0.5*erfc(sqrt(pow(SIR,ZETA)));
01225
01226 Beff += ((1-Pe)*sf2datarate(SF_INIT_VALUE,UPLINK));
01227 }
01228 }
01229 }
01230
01231 Beff = floor(Beff/((double)howManyInCell));
01232 return(Beff);
01233 }
01234
01235
01236
01237 ANid Phy2ReadBestBs(ANid id){
01238 PBStruct *PBptr;
01239
01240 PBptr = (PBStruct *)NODEPTR(id)->Phy2StructPtr;
01241
01242 return(PBptr->BSlist[0]);
01243 }
01244
01245
01246
01247
01248 void Phy2Finalize() {
01249 PBStruct *PBptr;
01250 int i;
01251
01252 #ifdef debug
01253 for(i=1;i<=NODES_NUM;i++){
01254 if ((Mac2Available(i)==TRUE) && (NODEPTR(i)->node_type==PAN_BAN)){
01255 PBptr = (PBStruct *)NODEPTR(i)->Phy2StructPtr;
01256 printf("PB %d has tx power %f\n",i,PBptr->txPower);
01257 }
01258 }
01259 #endif
01260 }