⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ramimo_proc.pr.c

📁 用OPNET实现802.11MAC协议DCF协议
💻 C
📖 第 1 页 / 共 5 页
字号:
				}
				/*If transmission of an ACK packet*/
				else if(ACK)
				{
				    /*Create an ACK packet*/
				    pktptr = op_pk_create_fmt("ACK_pkt");
				    
					/*set the appropriate size of the packet - An ACK packet is 14 bytes*/
					op_pk_total_size_set(pktptr, ackpktsize);
				
					/*Set the fields of the ACK packet*/
					op_pk_nfd_set(pktptr, "SRC", userid);
					op_pk_nfd_set(pktptr, "DEST", commnodeid);
					op_pk_nfd_set(pktptr, "number", sequencenum);
					
					#ifdef PRINT_TX
				    	printf("Transmitting an ACK Packet\n");
					    printf("Packet Size = %d\n", op_pk_total_size_get(pktptr));
					    op_pk_nfd_get(pktptr, "SRC", &srcid);
					    printf("Packet Source = %d\n", srcid);
					    op_pk_nfd_get(pktptr, "DEST", &destid);
					    printf("Packet Destination = %d\n", destid);
					 	op_pk_nfd_get(pktptr, "number", &tempdouble);
					    printf("Packet Sequence Number = %lf\n", tempdouble);	
				    #endif
				}
				
				
				/*Reset the transmission required variable - now that the transmission is taking place*/
				txreq = 0;
				
				/*Reset the random backoff interval*/
				rob = -1.0;
				
				/*Transmit the packet*/
				op_pk_send(pktptr, TX_STRM);
				
				op_prg_odb_bkpt("TX");
				op_prg_odb_bkpt("TEST");
				}
				FSM_PROFILE_SECTION_OUT (state4_enter_exec)

			/** state (TX) exit executives **/
			FSM_STATE_EXIT_FORCED (4, "TX", "ramimo_proc [TX exit execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [TX exit execs]", state4_exit_exec)
				{
				}
				FSM_PROFILE_SECTION_OUT (state4_exit_exec)


			/** state (TX) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;, "default", "", "TX", "WAIT")
				/*---------------------------------------------------------*/



			/** state (SENSE) enter executives **/
			FSM_STATE_ENTER_FORCED (5, "SENSE", state5_enter_exec, "ramimo_proc [SENSE enter execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [SENSE enter execs]", state5_enter_exec)
				{
				/******************************************/
				/*Code for physical carrier sensing update*/
				/******************************************/
				if(PCS)
				{
				    /*Read the update from the receiver or transmitter channels*/
				    pcs_state = (int) op_stat_local_read(op_intrpt_stat());
						
					#ifdef PRINT_SENSE
					    printf("SENSE STATE\n");
					    printf("Node ID = %d\n", userid);
				     	printf("Updating the Physical Carrier Sensing Status!\n");
					    printf("Updated PCS_STATE = %d\n", pcs_state);
				    #endif
					
					/*If the channel has become busy then any pending transmission or rob calculation interrupts need to be cancelled*/
					if(pcs_state == 1)
					{
						/*Cancel the transmission event if scheduled and not for a DATA or ACK packet transmission*/
						/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
						if(op_ev_valid(nexttxevent) == OPC_TRUE)
						{
				        	/*Update the backoff time if one has been set*/
				    		if(rob != -1.0)
					   		{
					    	    #ifdef PRINT_SENSE
					                printf("Updating backoff time!\n");
				                    printf("Original backoff time was %lfs\n", rob);
				                #endif
				
					    	    rob = op_ev_time(nexttxevent) - op_sim_time();
				       	   		
					 	        #ifdef PRINT_SENSE
				          	        printf("New backoff time is %lfs\n", rob);
				                #endif
					         }	
				    	
							 op_ev_cancel(nexttxevent);
							
				     		 #ifdef PRINT_SENSE
				                 printf("Cancelling a Scheduled Transmission Event!\n");
				             #endif
						}
						
						/*Cancel the transmission event if it is not for a DATA or ACK packet transmission*/
						/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
						if(op_ev_valid(robevent) == OPC_TRUE)
						{
						    op_ev_cancel(robevent);
							
				   			#ifdef PRINT_SENSE
				           	     printf("Cancelling a Scheduled ROB Event!\n");
				               #endif
						}
					}
				}
				/******************************************************/
				/*Code for virtual carrier sensing busy channel update*/
				/******************************************************/
				else if(VCS_busy)
				{
				    #ifdef PRINT_SENSE
					    printf("SENSE STATE\n");
					    printf("Node ID = %d\n", userid);
				        printf("Channel is sensed BUSY via VCS\n");
				    #endif
				 	
					/*Cancel the transmission event if scheduled and not for a DATA or ACK packet transmission*/
					/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
					if(op_ev_valid(nexttxevent) == OPC_TRUE)
					{
				       	/*Update the backoff time if one has been set*/
				    	if(rob != -1.0)
						{
					   	    #ifdef PRINT_SENSE
					            printf("Updating backoff time!\n");
				                printf("Original backoff time was %lfs\n", rob);
				            #endif
				
					  	    rob = op_ev_time(nexttxevent) - op_sim_time();
				      	   		
					        #ifdef PRINT_SENSE
				       	        printf("New backoff time is %lfs\n", rob);
				             #endif
					     }	
				    	
						 op_ev_cancel(nexttxevent);
							
				         #ifdef PRINT_SENSE
				             printf("Cancelling a Scheduled Transmission Event!\n");
				         #endif
				    }
						
					/*Cancel the transmission event if it is not for a DATA or ACK packet transmission*/
					/*Remember that no regard is given to the state of the channel when tx a DATA or ACK packet*/
					if(op_ev_valid(robevent) == OPC_TRUE)
					{
					    op_ev_cancel(robevent);
							
				   		#ifdef PRINT_SENSE
				            printf("Cancelling a Scheduled ROB Event!\n");
				        #endif
				    }
				    
					/*Determine if the channel is already sensed busy*/
				    if(vcs_state == 1)
					{
				        #ifdef PRINT_SENSE
					        printf("Channel was Already Sensed BUSY via VCS!\n");
							printf("Time of Previous Free Interrupt = %lf\n", op_ev_time(chfreeint));
							printf("New Time for Free Channel = %lf\n", op_sim_time() + vcs_duration);
				        #endif
						
					    /*Reschedule the channel free interrupt if the new duration is later than the previous*/
					    if((op_sim_time() + vcs_duration) > op_ev_time(chfreeint)) 
				        {
						    op_ev_cancel(chfreeint);
							chfreeint = op_intrpt_schedule_self(op_sim_time() + vcs_duration, VCS_FREE_INT);
							
							 #ifdef PRINT_SENSE
					            printf("Rescheduling Channel Free Time\n");
					    		printf("Channel Free Interrupt Scheduled for %lf seconds\n", op_sim_time() + vcs_duration);
				            #endif
						}	
					}	
					else
					{	
					    chfreeint = op_intrpt_schedule_self(op_sim_time() + vcs_duration, VCS_FREE_INT);
				  	    
						#ifdef PRINT_SENSE
					        printf("Channel was not already sensed BUSY via VCS!\n");
							printf("Time for Free Channel = %lf\n", op_sim_time() + vcs_duration);
				        #endif
				     }
						
				     /*Update the vcs state variable*/
				     vcs_state = 1;
				}
				/******************************************************/
				/*Code for virtual carrier sensing free channel update*/
				/******************************************************/
				else if(VCS_free)
				{
				    #ifdef PRINT_SENSE
					    printf("SENSE STATE\n");
					    printf("Node ID = %d\n", userid);
				        printf("Channel is sensed FREE via VCS!\n");
				    #endif
				
					/*Update the vcs state variable*/
				    vcs_state = 0;
					
				}
				
				/************************************/
				/*Code for the channel becoming free*/
				/************************************/
				
				/*If the channel is now free then a random backoff interrupt may need to be scheduled, either for a retransmission*/
				/*attempt or a new transmission*/
				if((pcs_state == 0) && (vcs_state == 0))
				{
				  	#ifdef PRINT_SENSE
				        printf("Channel has just become FREE!\n");
				    #endif
					
					/*Reschedule a transmission interrupt if required - based on the random backoff procedure*/
				  	if( (busy == 1) && (txreq != 0) )
					{	
					    /*Schedule ROB interrupt in eifs time*/
				   	   	robevent = op_intrpt_schedule_self(op_sim_time() + difs, ROB_INT);
					
				   		#ifdef PRINT_SENSE
				            printf("Scheduled a Random Backoff Interrupt for an Expected Transmission at %lfs\n", op_ev_time(robevent));
				        #endif
					}
					else if( (op_subq_empty(SRC_QUEUE) == OPC_FALSE) && (busy == 0) )
					{	
					    /*Set the destination node ID to be associated with the tx interrupt*/
				        pktptr = op_subq_pk_access(SRC_QUEUE, OPC_QPOS_HEAD); 
						op_pk_nfd_get(pktptr, "nexthop", &commnodeid);
						op_pk_nfd_get(pktptr, "number", &sequencenum);
						
						/*Schedule an ROB interrupt in DIFS seconds time*/
				 	   	robevent = op_intrpt_schedule_self(op_sim_time() + difs, ROB_INT);
						txreq = 1;
				   	   	txstate = RTS_INT;
						busy = 1;
						
						/*Reset the random backoff interval if this is a new transmission*/
						rob = -1.0;
					
						
						#ifdef PRINT_SENSE
				            printf("Scheduled a Random Backoff Interrupt for an New Packet Transmission at %lfs\n", op_ev_time(robevent));
				     		op_pk_nfd_get(pktptr, "SRC", &srcid);   
				   	  	    printf("Packet Source = %d\n", srcid);
				            op_pk_nfd_get(pktptr, "DEST", &destid);   
				   	 	    printf("Packet Destination = %d\n", destid);
				      		printf("Will be communicating with node %d\n", commnodeid);
				     	    printf("Packet Sequence Number = %lf\n", sequencenum);
				         #endif
					}
					
				
				}
				
				op_prg_odb_bkpt("SENSE");
					op_prg_odb_bkpt("TEST");
				}
				FSM_PROFILE_SECTION_OUT (state5_enter_exec)

			/** state (SENSE) exit executives **/
			FSM_STATE_EXIT_FORCED (5, "SENSE", "ramimo_proc [SENSE exit execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [SENSE exit execs]", state5_exit_exec)
				{
				}
				FSM_PROFILE_SECTION_OUT (state5_exit_exec)


			/** state (SENSE) transition processing **/
			FSM_TRANSIT_FORCE (1, state1_enter_exec, ;, "default", "", "SENSE", "WAIT")
				/*---------------------------------------------------------*/



			/** state (RXARVL) enter executives **/
			FSM_STATE_ENTER_FORCED (6, "RXARVL", state6_enter_exec, "ramimo_proc [RXARVL enter execs]")
				FSM_PROFILE_SECTION_IN ("ramimo_proc [RXARVL enter execs]", state6_enter_exec)
				{
#ifdef PRINT_RXARVL
				    printf("RXARVL STATE\n");    
					printf("Node %d\n", userid);
#endif
				
				/******************************************************************/
				/*Determine whether the incoming packet can be correctly received */
				/******************************************************************/
				/*Get the packet from the incoming stream*/
				pktptr = op_pk_get(RX_STRM);
				
				/*Determine the packet type from its name*/
				op_pk_format(pktptr, &pktname);
				
				if(strcmp(pktname, "DATA_pkt") == 0)
				{
				    /*Read the user ID of the source node*/
				    op_pk_nfd_get(pktptr, "prevhop", &srcid);
				}
				else
				{
					/*Read the user ID of the source node*/
				    op_pk_nfd_get(pktptr, "SRC", &srcid);
				}
				
				/*Obtain the source nodes object ID from the user ID*/
				other_node_id = op_id_from_userid(subnet_id, OPC_OBJTYPE_NDMOB, srcid); 
				
				/*Determine the location of the source node*/
				op_ima_obj_attr_get(other_node_id, "x position", &other_xpos);
				op_ima_obj_attr_get(other_node_id, "y position", &other_ypos);
				
				/*Work out the distance between nodes*/
				distance = sqrt((other_xpos - xpos)*(other_xpos - xpos) + (other_ypos - ypos)*(other_ypos - ypos));
				
#ifdef PRINT_RX_ARVL
				    printf("Packet Heard from Node %d\n", srcid);
				    printf("Distance Between Tx and Rx Nodes = %lf\n", distance);
				    printf("Transmitting Range Over Which Packets Can Be Received = %lf\n", tx_range);
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -