//======================================================= // FUNCTION: MobilityProcess // PURPOSE: Process the procedures over all the running time of the // program. //======================================================= void MobilityProcess() { int i; //int j; for (i=0; i<TotNodeNo; i++){ if ((GetPowSwitch(WGNnode(i+1)) == ON)&&(GetEndSign(WGNnode(i+1)) == FALSE)) MobilityUpdate(WGNnode(i+1)); }//end of for }
//======================================================= // FUNCTION: FinalizeNodeLlc //======================================================= void FinalizeNodeLlc(NodeType type) { int id,i; printf("Finalizing logic link control layer parameters ... "); if (type == NORMAL) { for (id=1; id<=TotNodeNo; id++) { //cancel uml->llc connection. conn_close(WGNnode(id)->nodeLlc->inf); //cancel llc->uml connection. pthread_cancel(WGNnode(id)->nodeLlc->buf_thread_id); for (i=0; i<LLC_TO_MAC_BUF_SIZE; i++) { if (WGNnode(id)->nodeLlc->LToMArray[i] != NULL) { free(WGNnode(id)->nodeLlc->LToMArray[i]); WGNnode(id)->nodeLlc->LToMArray[i] = NULL; } } for (i=0; i<LLC_TO_UML_BUF_SIZE; i++) { if (WGNnode(id)->nodeLlc->LToUArray[i] != NULL) { free(WGNnode(id)->nodeLlc->LToUArray[i]); WGNnode(id)->nodeLlc->LToUArray[i] = NULL; } } free(WGNnode(id)->nodeLlc); } } printf("[ OK ]\n"); }
//======================================================= // FUNCTION: IniChannel //======================================================= void IniChannel() { //initialization of the basic functional channel. if (prog_verbosity_level()==1){ PrintSystemInfo ("Initializing the network channel of wireless GINI ... "); printf("[IniChannel]:: Initializing basic functional channel ... "); } Ch_update_remain = SecToNsec(SYS_INFO->time_info->Ch_timeunit); Channel = (ChannelMib *)malloc(sizeof(ChannelMib)); //current version only support the identical bandwidth. (change it in the future) Channel->channelbandwidth = WGNnode(1)->nodePhy->nodeWCard->bandwidth; Channel->channelFadPrintSign = PrintChFadSign; IniChannelLinkStateMatrix(); if (prog_verbosity_level()==1){ printf("[ OK ]\n"); } //initialization of the modules. if (prog_verbosity_level()==2){ PrintSystemInfo ("Initializing the channel modules ... "); } IniChannelAwgn(); IniChannelPropagation(); IniChannelFading() ; }
//======================================================= // FUNCTION: FinalizeNodeS80211DcfMac //======================================================= void FinalizeNodeS80211DcfMac(NodeType type) { NodeId id; if (prog_verbosity_level()==2){ printf("[FinalizeNodeS80211DcfMac]:: Finalizing 802.11 dcf module ... "); } if (type == NORMAL) { for (id=1; id<=TotNodeNo; id++) { if (WGNnode(id)->nodeMac->pktRts != NULL) { Dot11FrameFree(WGNnode(id)->nodeMac->pktRts); } } } if (prog_verbosity_level()==2){ printf("[ OK ]\n"); } }
//======================================================= // FUNCTION: IniNodeMac //======================================================= void IniNodeS80211DcfMac(NodeType type) { NodeId id; if (prog_verbosity_level()==2){ printf("[IniNodeS80211DcfMac]:: Initializing 802.11 dcf module ... "); } if (type == NORMAL) { for (id=1; id<=TotNodeNo; id++) { WGNnode(id)->nodeMac->cw = SYS_INFO->phy_info->CWMin; WGNnode(id)->nodeMac->short_retry_counter = 0; WGNnode(id)->nodeMac->long_retry_counter = 0; WGNnode(id)->nodeMac->pktRts = NULL; } } if (prog_verbosity_level()==2){ printf("[ OK ]\n"); } }
//======================================================= // FUNCTION: LinkStateComp //======================================================= void LinkStateComp() { int sid, did; double tmpPwr; for (sid=1; sid<=TotNodeNo; sid++) { for (did=1; did<=TotNodeNo; did++) { if (sid!=did) { tmpPwr = GetRxPwr(WGNnode(sid), WGNnode(did)); if (IsOverRXThresh(DBToW(tmpPwr), WGNnode(did))==TRUE) Channel->linkmatrix[sid-1][did-1] = 3; else if (IsOverCSThresh(DBToW(tmpPwr), WGNnode(did))==TRUE) Channel->linkmatrix[sid-1][did-1] = 2; else Channel->linkmatrix[sid-1][did-1] = 1; } } } }
//======================================================= // FUNCTION: LlcProcess //======================================================= void LlcProcess() { int ret; int i; //pthread_t thread_id; char *pchSocket; pchSocket = (char *)malloc(sizeof(char)*100); for (i=0; i<TotNodeNo; i++) { // llc to uml connection ret = pthread_create(&(WGNnode(i+1)->nodeLlc->buf_thread_id), NULL, (void *)LlcUpPortSend, WGNnode(i+1)); if (ret != 0) { printf("[LlcProcess]:: The connection from LLC to UML is failed.\n"); } //initialize the node interface WGNnode(i+1)->nodeLlc->inf = (struct interface *)malloc(sizeof(struct interface)); //initialize the uml to llc connection SocketNameGen(i+1, pchSocket); conn_open(WGNnode(i+1), pchSocket); } }
//======================================================= // FUNCTION: FinalizeNodeEnergy //======================================================= void FinalizeNodeEnergy(NodeType type) { int id; if (prog_verbosity_level()==2){ printf("[FinalizeEnergyModel]:: Finalizing energy module ... "); } if (type == NORMAL) { for ( id=1; id<=TotNodeNo; id++) { free(WGNnode(id)->nodePhy->nodeEnergy); } } if (prog_verbosity_level()==2){ printf("[ OK ]\n"); } }
//======================================================= // FUNCTION: LlcUpPortRecv (thread, get pkt from UML) //======================================================= void LlcUpPortRecv(void* psConn) { struct interface* psConnection = (struct interface*) psConn; unsigned char* input_frame; WGN_802_3_Mac_Frame* frame; WGNflag fullArray; MobileNode *node; int byte_no; fullArray=FALSE; node = WGNnode(psConnection->node_id); input_frame = (unsigned char*)malloc(sizeof(char)*RCVBUFSIZE); while(1){ byte_no = vpl_recvfrom(psConnection->iDescriptor, input_frame, RCVBUFSIZE); frame = WGN_802_3_FrameDup(input_frame, byte_no); if (frame == NULL) { verbose(4, "[LlcUpPortRecv]:: Node %d received a NULL packet from the UML.", psConnection->node_id); } else { verbose(4, "[LlcUpPortRecv]:: Node %d receives a packet from the UML.", psConnection->node_id); } pthread_mutex_lock(&(node->nodeLlc->ltom_array_mutex)); if (node->nodeLlc->LToMArray[(node->nodeLlc->ltom_buf_Wcount)]==NULL){ node->nodeLlc->LToMArray[(node->nodeLlc->ltom_buf_Wcount)] = frame; frame = NULL; //update index node->nodeLlc->ltom_buf_Wcount = (node->nodeLlc->ltom_buf_Wcount + 1)%LLC_TO_MAC_BUF_SIZE; } else fullArray=TRUE; node->nodeLlc->ltombufempty = FALSE; pthread_mutex_unlock(&(node->nodeLlc->ltom_array_mutex)); //unlock this module's buffer array if (node->nodeMac->HasPkt == FALSE) { LlcDownPortSend (node); } if(fullArray==TRUE){ verbose(4, "[LlcUpPortRecv]:: NODE %d: LLC to MAC buffer is full, packet thrown.", node->Id); Dot3FrameFree(frame); node->nodeStats->LToMBufDrop += 1; fullArray=FALSE; } } }
//======================================================= // FUNCTION: FinalizeNodeMobility //======================================================= void FinalizeNodeMobility(NodeType type) { int i; if (prog_verbosity_level()==2){ printf("[FinalizeNodeMobility]:: Finalizing mobility module ... "); } if (type == NORMAL) { for ( i=0; i<TotNodeNo; i++) { SetEndSign(WGNnode(i+1), TRUE); free(WGNnode(i+1)->nodePhy->nodeMobility->movStrPtr); free(WGNnode(i+1)->nodePhy->nodeMobility->srcPosition); free(WGNnode(i+1)->nodePhy->nodeMobility->dstPosition); free(WGNnode(i+1)->nodePhy->nodeMobility->curPosition); free(WGNnode(i+1)->nodePhy->nodeMobility->nodeSpd); free(WGNnode(i+1)->nodePhy->nodeMobility); } } if (prog_verbosity_level()==2){ printf("[ OK ]\n"); } }
//======================================================= // FUNCTION: ChannelDelivery //======================================================= void ChannelDelivery(int src_id, int dst_id, WGN_802_11_Mac_Frame *frame) { double tmpPwr; //(db) unsigned int prop_delay; //nanosecond WGNTime link_end_time; verbose(4, "[ChannelDelivery]:: Packet from node %d to node %d is deliveried by channel", src_id, dst_id); //Calculate the rx power by "propagation module" & "fading module" tmpPwr = GetRxPwr(WGNnode(src_id), WGNnode(dst_id)); frame->pseudo_header->dstid = dst_id; SetPktTxPwr(frame, GetTxPwr(WGNnode(src_id))); SetPktRxPwr(frame, tmpPwr); if (IsOverRXThresh(DBToW(tmpPwr), WGNnode(dst_id))==FALSE) { SetFadErrorSign(frame); //mark this frame as fading error } //Marking demodulation error based on noise power by "awgn module" //count all the packets with "collison error" and "pwr too low error" out, //the only packet left should be only affected by "AWGN noise" or //"survived from capture". The AWGNPktHandler can handler the pkt with //"AWGN noise" by theoretical error probability formula. But when handle the //latter packets, error diffenrence will be introduced, because the interference //changes the characters of the channel. Fortunately, the probability that the // latter comes out will be at the order of 10^(-2)*10^(-4~-8) //(P{collison happens}*P{error probabitlity with SNR more than CPThreshold}). //So it will not impact on the system performance. if (GetAwgnSign() == ON) { AWGNPktHandler (frame); } //Emulate the propagation delay. verbose(5, "[ChannelDelivery]:: Packet from node %d to node %d in propagation delay procedure is handled by the channel.", src_id, dst_id); SetLinkStartTime(frame, GetTime()); prop_delay = SecToNsec(GetPropDelay(WGNnode(src_id), WGNnode(dst_id))); link_end_time = LocalTimeAdd (GetTime(), prop_delay); SetLinkEndTime(frame, link_end_time); if (frame == NULL) { printf("[ChannelDelivery]:: Duplicate a NULL packet from node %d to node %d.\n", src_id, dst_id); } AddNewEventToList(dst_id, START_RX, link_end_time, (void*)frame); }
//======================================================= // FUNCTION: IniNodeLlc //======================================================= void IniNodeLlc(NodeType type) { int id; int j; int ret; if (prog_verbosity_level()==1){ printf("[IniNodeLlc]:: Initializing basic functional logical link control layer ... "); } if (type == NORMAL) { for (id=1; id<=TotNodeNo; id++) { WGNnode(id)->nodeLlc = (NodeLlc *)malloc(sizeof(NodeLlc)); //initialize LToMArray for (j=0; j<LLC_TO_MAC_BUF_SIZE; j++) { WGNnode(id)->nodeLlc->LToMArray[j]=NULL; } WGNnode(id)->nodeLlc->ltom_buf_Wcount = 0; WGNnode(id)->nodeLlc->ltom_buf_Rcount = 0; ret = pthread_mutex_init(&(WGNnode(id)->nodeLlc->ltom_array_mutex), NULL); if (ret!=0) printf("[IniNodeLlc]:: fail to init mutex, error %d\n",ret); ret = pthread_cond_init(&(WGNnode(id)->nodeLlc->ltom_nonempty), NULL); if (ret!=0) printf("[IniNodeLlc]:: fail to init cond, error %d\n",ret); ret = pthread_cond_init(&(WGNnode(id)->nodeLlc->ltom_nonfull), NULL); if (ret!=0) printf("[IniNodeLlc]:: fail to init cond nonfull, error %d\n",ret); for (j=0; j<LLC_TO_UML_BUF_SIZE; j++) { WGNnode(id)->nodeLlc->LToUArray[j]=NULL; } WGNnode(id)->nodeLlc->ltou_buf_Wcount = 0; WGNnode(id)->nodeLlc->ltou_buf_Rcount = 0; ret = pthread_mutex_init(&(WGNnode(id)->nodeLlc->ltou_array_mutex), NULL); if (ret!=0) printf("[IniNodeLlc]:: fail to init mutex, error %d\n",ret); ret = pthread_cond_init(&(WGNnode(id)->nodeLlc->ltou_nonempty), NULL); if (ret!=0) printf("[IniNodeLlc]:: fail to init cond, error %d\n",ret); ret = pthread_cond_init(&(WGNnode(id)->nodeLlc->ltou_nonfull), NULL); if (ret!=0) printf("[IniNodeLlc]:: fail to init cond nonfull, error %d\n",ret); }//end of for //establish the connection from llc to uml. printf("Calling LlcProcess... \n"); LlcProcess(); }//end of if if (prog_verbosity_level()==1){ printf("[ OK ]\n"); } }
//======================================================= // FUNCTION: IniEnergyModel // PURPOSE: Initialize the energy module. //======================================================= void IniNodeEnergy(NodeType type) { int id; if (prog_verbosity_level()==2){ printf("[IniNodeEnergy]:: Initializing energy module ... "); } if (type == NORMAL) { for ( id=1; id<=TotNodeNo; id++) { WGNnode(id)->nodePhy->nodeEnergy = (NodeEnergy *)malloc(sizeof(NodeEnergy)); if( WGNnode(id)->nodePhy->nodeEnergy == NULL ){ printf("IniEnergyModel(): nodeEnergy malloc error"); exit(1); } /*set the power switch of the mobile node*/ WGNnode(id)->nodePhy->nodeEnergy->PowSwitch = EnPowSwitchArray[(WGNnode(id)->Id)-1]; if (WGNnode(id)->nodePhy->nodeEnergy->PowSwitch==ON) { WGNnode(id)->nodePhy->nodeEnergy->PSM = EnPSMArray[id-1]; WGNnode(id)->nodePhy->nodeEnergy->TotalEnergy = TotalEnergyArray[id-1]; WGNnode(id)->nodePhy->nodeEnergy->Pt = WGNnode(id)->nodePhy->nodeWCard->Pt; WGNnode(id)->nodePhy->nodeEnergy->Pt_consume = WGNnode(id)->nodePhy->nodeWCard->Pt_consume; WGNnode(id)->nodePhy->nodeEnergy->Pr_consume = WGNnode(id)->nodePhy->nodeWCard->Pr_consume; WGNnode(id)->nodePhy->nodeEnergy->P_idle = WGNnode(id)->nodePhy->nodeWCard->P_idle; WGNnode(id)->nodePhy->nodeEnergy->P_sleep = WGNnode(id)->nodePhy->nodeWCard->P_sleep; WGNnode(id)->nodePhy->nodeEnergy->P_off = WGNnode(id)->nodePhy->nodeWCard->P_off; } else printf ("This mobile node is POWER OFF."); } } if (prog_verbosity_level()==2){ printf("[ OK ]\n"); } }
//======================================================= // FUNCTION: IniNodeMobility // PURPOSE: Initialize the mobility parameters of all nodes. //======================================================= void IniNodeMobility(NodeType type) { int i; if (prog_verbosity_level()==2){ printf("[IniNodeMobility]:: Initializing mobility module ... "); } if (type == NORMAL) { for ( i=0; i<TotNodeNo; i++) { WGNnode(i+1)->nodePhy->nodeMobility = (NodeMobility *)malloc(sizeof(NodeMobility)); if( WGNnode(i+1)->nodePhy->nodeMobility == NULL ){ printf("[IniMobNode]:: nodeMobility malloc error"); exit(1); } /*malloc for movStrPtr*/ if (MobilityTypeArray[i] == RandomWayPoint) { WGNnode(i+1)->nodePhy->nodeMobility->movStrPtr = (RWP_StrPtr *)malloc(sizeof(RWP_StrPtr)); if( WGNnode(i+1)->nodePhy->nodeMobility->movStrPtr == NULL ){ printf("[IniMobNode]:: RWP_StrPtr malloc error"); exit(1); } } else if (MobilityTypeArray[i] == TrajectoryBased) { printf ("[IniMobNode]:: TrajectoryBased is not implemented at this moment.\n"); exit (1); } else if (MobilityTypeArray[i] == Manual) { printf ("[IniMobNode]:: Manual is not implemented at this moment.\n"); exit (1); } else { printf ("[IniMobNode]:: Unrecongnized mobility type.\n"); exit (1); } /*malloc for srcPosition*/ WGNnode(i+1)->nodePhy->nodeMobility->srcPosition = (NodePosition *)malloc(sizeof(NodePosition)); if( WGNnode(i+1)->nodePhy->nodeMobility->srcPosition == NULL ){ printf("[IniMobNode]:: srcPosition malloc error"); exit(1); } /*malloc for dstPosition*/ WGNnode(i+1)->nodePhy->nodeMobility->dstPosition = (NodePosition *)malloc(sizeof(NodePosition)); if( WGNnode(i+1)->nodePhy->nodeMobility->dstPosition == NULL ){ printf("[IniMobNode]:: dstPosition malloc error"); exit(1); } /*malloc for curPosition*/ WGNnode(i+1)->nodePhy->nodeMobility->curPosition = (NodePosition *)malloc(sizeof(NodePosition)); if( WGNnode(i+1)->nodePhy->nodeMobility->curPosition == NULL ){ printf("[IniMobNode]:: curPosition malloc error"); exit(1); } /*malloc for nodeSpd*/ WGNnode(i+1)->nodePhy->nodeMobility->nodeSpd = (NodeSpd *)malloc(sizeof(NodeSpd)); if( WGNnode(i+1)->nodePhy->nodeMobility->srcPosition == NULL ){ printf("[IniMobNode]:: nodeSpd malloc error"); exit(1); } WGNnode(i+1)->nodePhy->nodeMobility->mov_rep_name = NULL; WGNnode(i+1)->nodePhy->nodeMobility->mobilityType = MobilityTypeArray[i]; SetMovStrPtr(WGNnode(i+1)); //chose the movement structure ptr based on the type SetStartPointLoc(WGNnode(i+1)); //set the start location of the node IniNodeCurLoc(WGNnode(i+1)); //set the current loc to be the same as the src loc. WGNnode(i+1)->nodePhy->nodeMobility->MaxSpd = MaxSpdArray[i]; WGNnode(i+1)->nodePhy->nodeMobility->MinSpd = MinSpdArray[i]; } }//end of if if (prog_verbosity_level()==2){ printf("[ OK ]\n"); } }
//======================================================= // FUNCTION: KillJamInt // PURPOSE: Remove all the "jam" interference power from the // specified node //======================================================= void KillJamInt (int id) { WGNnode(id)->nodePhy->nodeAntenna->intPwr -= WGNnode(id)->nodePhy->nodeAntenna->jamInt; WGNnode(id)->nodePhy->nodeAntenna->jamInt = 0; rx_resume(WGNnode(id)); CheckBFTimer(WGNnode(id)); }
//======================================================= // FUNCTION: SetDefJamInt // PURPOSE: Add some interference on the specified node manually //======================================================= void SetJamInt (int id, double interference) { WGNnode(id)->nodePhy->nodeAntenna->intPwr += interference; WGNnode(id)->nodePhy->nodeAntenna->jamInt = interference; }