Index: /vtcross/branches/sriram/benchmark_txrxnode1.py
===================================================================
--- /vtcross/branches/sriram/benchmark_txrxnode1.py	(revision 365)
+++ /vtcross/branches/sriram/benchmark_txrxnode1.py	(revision 367)
@@ -4,10 +4,4 @@
 from gnuradio import gr, gru, modulation_utils
 from gnuradio import eng_notation
-##########spectrum sense specific imports
-from gnuradio import optfir, window
-from gnuradio import usrp
-#from gnuradio.eng_option import eng_option
-#from usrpm import usrp_dbid
-##########
 from gnuradio.eng_option import eng_option
 from optparse import OptionParser
@@ -18,14 +12,7 @@
 from transmit_path import transmit_path
 from receive_path import receive_path
-#import spec_sense_path
-
-global sync_status
-sync_status = False #default value is "False"...
-global mode
-#Defining modes of operation
-#sync = synchronizing with other node before data transfer
-#traffic = data transfer
-#spec_sense = sensing spectrum for primary user
-mode = "sync" #default mode of operation...
+global sync_status,mode,ch
+sync_status = "False"
+mode = "sync"#default mode is sync
 class my_top_block(gr.top_block):
 
@@ -38,12 +25,11 @@
 	self.connect(self.txpath);
 	self.connect(self.rxpath);
-
 	
 #//////main//////  
 
-global n_rcvd, n_right,hop_freq
+global n_rcvd, n_right
 
 def main():
-    global n_rcvd, n_right,sync_status,mode,hop_freq
+    global n_rcvd, n_right,sync_status,mode,ch
     n_rcvd = 0
     n_right = 0
@@ -52,56 +38,31 @@
 
     def rx_callback(ok, payload):
-
-	
         print "inside rx callback"
-        global n_rcvd, n_right, sync_status,mode,hop_freq
-	#tb.rxpath.min_freq = hop_freq - 80e3 
-        #tb.rxpath.max_freq = hop_freq + 80e3 
-	#tb.rxpath.get_spec_stats(1e13,20,2)
-        #temp = struct.unpack('!s', payload[3:10])
-	#print "printing the elements of packet",temp,"!!!!!!!!!!!\n"
-        #####################################################
+        global n_rcvd, n_right, sync_status,mode,ch
+	##############################################################
 	if mode == "sync":
 		if ok:
-			(pktno,) = struct.unpack('!H', payload[0:2])
-			(sync_signal,) = struct.unpack('!s', payload[2]) 
-        		(sync_signal_red,) = struct.unpack('!s', payload[3])  #redundant sync bit
-        		(data_channel,) = struct.unpack('!s', payload[4])
-        		(data_channel_red,) = struct.unpack('!s', payload[5])  #redundant channel bit
-        		#print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",sync_signal_red," !!!!!\n"    
-        		#print "printing the channel  ",data_channel_red,"  !!!!!!!!!!!  ",data_channel_red," !!!!!\n"
-        	 	#== True :# and str(sync_signal) == 'o' and str(sync_signal_red) == 'o':
-            		if str(data_channel) == str(ch):
-                    		#print "inside if2"
-                    		sync_status = True
-		    		#tb.stop()
-		    		#print "after tb stop ans sync status is ",sync_status," sync status\n"
-                    		#print "received a sync packet on channel %s\n" %(data_channel)
-                    		#data = 'o'+'o'+str(data_channel)+str(data_channel)
-                    		#print "sending this data",data,"@@@@@\n"
-                    		#sync_pktno=0
-                    		#ack_payload = struct.pack('!H', sync_pktno & 0xffff) + data
-                    		#print "printing the ack packet",ack_payload,"$$$$$\n"
-                    		#k=0
-                    		#while k < 10000:
-                    		#print "inside while"
-                    		#send_pkt(tb,ack_payload) #sending back the acknowledgement
-                    		#k=k+1
-	#####################################################
+        		(pktno,) = struct.unpack('!H', payload[0:2])
+       			(sync_signal,) = struct.unpack('!s', payload[2]) 
+        		(data_channel,) = struct.unpack('!H', payload[3:5])
+        		print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",data_channel," !!!!!\n"    
+        		if str(sync_signal) == 'o' and str(data_channel) == str(ch):
+        			#print "inside if2"
+        			sync_status = "True"
+				#tb.stop()
+				#print "after tb stop ans sync status is ",sync_status," sync status\n"
+                   
+	##############################################################
 	
 	######################### traffic ############################
 	if mode == "traffic":
-		print "Mode is ",mode,"received traffic packet\n"
+		
 		if ok:
 			(data_header,) = struct.unpack('!s', payload[0])
 			if data_header == 'd':
-				(comm,) = struct.unpack('!14s', payload[1:15])
-				print "received this ", comm,"\n"
-		else :
-			print"not ok"
+				comm = struct.unpack('!14s', payload[1:15])
+				print("received this ", comm) 
 	
 	##############################################################
-	
-
 
         n_rcvd += 1
@@ -110,5 +71,5 @@
 
         #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
-            #ok, pktno, n_rcvd, n_right)
+        #    ok, pktno, n_rcvd, n_right)
 
     mods = modulation_utils.type_1_mods()
@@ -139,16 +100,16 @@
 
     (options_tx, args_tx) = parser_tx.parse_args ()
-
+    ############# Setting some default values for tx side of the block
+    options_tx.tx_freq = 480e6#462.5625e6 
+    options_tx.samples_per_symbol =  2
+    options_tx.modulation = 'dbpsk'
+    #options_tx.fusb_block_size = 4096
+    #options_tx.fusb_nblocks = 16
+    options_tx.bitrate = 0.0125e6
+    #############
     if len(args_tx) != 0:
         parser_tx.print_help()
         sys.exit(1)
-    ############# Setting some default values for tx side of the block
-    options_tx.tx_freq = 462.5625e6 
-    options_tx.samples_per_symbol =  2
-    options_tx.modulation = 'dbpsk'
-    options_tx.fusb_block_size = 4096
-    options_tx.fusb_nblocks = 16
-    options_tx.bitrate = 0.0125e6
-    #############
+
     if options_tx.tx_freq is None:
         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
@@ -161,13 +122,5 @@
     # build the graph
     print "printing tx options",options_tx,"&&&&\n"
-    #tb = my_top_block(mods[options_tx.modulation], options_tx)
-
-    #r = gr.enable_realtime_scheduling()
-    #if r != gr.RT_OK:
-    #   print "Warning: failed to enable realtime scheduling"
-
-    #setting up rx options parser
-    
-    # Create Options Parser:
+
     parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
     print "printing parser_rx",parser_rx,"!!!!!\n"
@@ -191,9 +144,9 @@
         sys.exit(1)
     ############# Setting some default values for rx side of the block
-    options_rx.rx_freq = 462.5625e6 #setting default rx_freq value
+    options_rx.rx_freq = 480e6#462.5625e6 #setting default rx_freq value
     options_rx.samples_per_symbol =  2
     options_rx.modulation = 'dbpsk'
-    options_rx.fusb_block_size = 4096
-    options_rx.fusb_nblocks = 16
+    #options_rx.fusb_block_size = 4096
+    #options_rx.fusb_nblocks = 16
     options_rx.bitrate = 0.0125e6
     #############
@@ -204,27 +157,21 @@
     print "printing rx options",options_rx,"&&&&\n"
 
-
-    # build the graph
+# build the graph
 
     tb = my_top_block(mods[options_tx.modulation],
-                     demods[options_rx.modulation],
-                     rx_callback,options_tx,
-                     options_rx)
+                      demods[options_rx.modulation],
+                      rx_callback,options_tx,
+                      options_rx)
     r = gr.enable_realtime_scheduling()
     if r != gr.RT_OK:
-       print "Warning: failed to enable realtime scheduling"
+        print "Warning: failed to enable realtime scheduling"
 
     tb.start()
-    #tb.rxpath.get_spec_stats(10) #10 is the number of fft frames that we are looking at to determine the average power
-    # generate and send packets
-    #print "printing megamytes",options_tx.megabytes,"  $$$$\n"
-    
-    #n = 0
-    #pktno = 0
-    #pkt_size = int(options_tx.size)
-    #options_tx.megabytes = 0.000048
-    
+    #tb.rxpath.disconnect(tb.rxpath.u, tb.rxpath.s2v,tb.rxpath.fft, tb.rxpath.c2mag, tb.rxpath.stats)
+
+    nbytes = 5 #int(1e6 * .0003)
     #options_tx.size = 6 #2 bytes for packet size,2 bytes for the synchronization request(including redundant bit) and 2 bits to specify the channel number(including redundant bit)
-    
+    pkt_size = 5
+    print "printing packet size",pkt_size,"pkt size\n"
     
     myfile = file("logfile.txt", 'w')
@@ -232,94 +179,71 @@
     #sending sync packet-format 
     global ch
-    check =True
-    #ch = random.randint(1, 7)#which channel to send information on...this is the second data packet of the synchronization frame
-    #resend = 5; #number of times a batch of sync packets is sent on a channel before trying a different channel
     running = True
-    #resend_count= 0 #initializing resend count
-    sync_pktno = 0
-    #ch = int(random.choice('17'))
+    
     while running:
-  	################################################sync mode####################################
-    	if mode == "sync":
-    		#nbytes = int(1e6 * .0003)
-		#nbytes = int(1e6 * .0003)
-		nbytes = 6
-    		pkt_size = 6
-		data_pktno = 0 
-    		#print "printing packet size",pkt_size,"pkt size\n"
-    		while sync_status != True:
-			#ch = random.randint(1, 7)
-			ch = int(random.choice('37'))
-			
-			#ch = random.randint(6,12)
-			#ch = 1
-		    	hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets
-			print "hop freq is ",hop_freq
-			tb.rxpath.min_freq = hop_freq - 80e3 
-    		    	tb.rxpath.max_freq = hop_freq + 80e3 
-			is_present = tb.rxpath.get_spec_stats(1e13,1) #check if primary user is present
-			if is_present == True: #if primary user is there then dont transmit on this channel
-				print "Cant use this channel..Primary user detected\n"
-				continue 
-					
-			#print "printing status ",status,"\n"
-			#print "primary user present = ",is_present,"\n"
-			#tb.rxpath.get_spec_stats(10)  
-			
-			
-			#hop_freq = float(1e6 * (462.5625+(5-1)*0.05))#setting the centre freq frequency for sending packets
+	
+	################################################sync mode####################################
+	if mode == "sync":
+		if sync_status != "True":
+	    		ch = int(random.choice([1,7,8,14]))
+	    		if ch < 8:
+				hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))#setting the centre freq frequency for sending packets
+	    		else:
+				hop_freq = float(1e6 * (467.5625+(ch-8)*0.025))#setting the centre freq frequency for sending packets
+	    		
+	    
 	    		tb.txpath.set_freq(hop_freq)
 	    		tb.rxpath.set_freq(hop_freq)
-        		#print "inside while pppp printing sync channel",sync_status,"  done"
-			#if sync_status == "True":
-            		#print "synchronization done..data channel is found"
-	    		#break
-			#else:
-	    		#print "inside else1"
+        		
+			k = 0
+			total_energy = 0 
+			while k <  1:
+				ch_energy = tb.rxpath.probe.level() #check if primary user is present
+				total_energy = total_energy + ch_energy
+				k+=1
+			
+			avg_energy = total_energy/1
+			print "printing hop frequency ",hop_freq,"and energy is ",avg_energy,"\n"
+			if int(avg_energy) > 1.50e8: #if primary user is there then dont transmit on this channel
+				#while 
+				continue
+				#print "present"
+			
+
 	    		n = 0
-	    		
-			print >> myfile, "out"
+	    		pktno = 0
     	    		while n < nbytes:
-                	        #print >> myfile, "inside while"
+                	
                 		if options_tx.from_file is None:
-                	    		#data = (pkt_size - 2) * chr(sync_pktno & 0xff) #0xff is 255
-                	    		data = 's'+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel
-	        	    		#print >> myfile, data
-                	    		#print "printing data",data,"****\n"
-                		else:
-            		    		data = source_file.read(pkt_size - 2)
-            		    		if data == '':
-                	        		break;
-	
-                		payload = struct.pack('!H', sync_pktno & 0xffff) + data
-	       			#print "printing payload",payload,"**\n"
-				
+                    			#data = (pkt_size - 2) * chr(pktno & 0xff) #0xff is 255
+                    			data = 's'#+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel
+	        		else:
+            	    			data = source_file.read(pkt_size - 2)
+            	    			if data == '':
+                        			break;
+
+                		payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
+	        		#print "printing payload",payload,"**\n"
+	
                 		send_pkt(tb,payload)
-				#print "printing payload",payload,"**\n"
                 		n += len(payload)
                 		sys.stderr.write('.')
-                		if options_tx.discontinuous and sync_pktno % 5 == 4:
-                	    		time.sleep(1)
-                		sync_pktno += 1
-	    			#print "before sleeping for 10 seconds and value of resend count is",resend_count
-	
-
-	            	time.sleep(0.05)
-			#time.sleep(2)
-	            	#resend_count += 1 
-	    
-		print "sync channel found! and it is channel ",ch,"\n"
-	    	mode = "traffic"
-	   	 #send_pkt(tb,eof=True)
-	    	#myfile.close()
-	    	#tb.wait()
+                		if options_tx.discontinuous and pktno % 5 == 4:
+                    			time.sleep(1)
+                			pktno += 1
+	    		time.sleep(0.05)
+            	#resend_count += 1 
+		else:
+			print "sync channel found..channel ",ch,"\n"  
+			mode = "traffic"
+			sync_status = "False"
 	################################################end of sync mode####################################
 
-	################################################Communications mode####################################
+    	################################################Communications mode####################################
     	if mode == "traffic":
 		print "inside traffic mode \n"
 		nbytes = 15
     	    	pkt_size = 15
-		sync_pktno = 0
+		data_pktno = 0
 		n = 0
 		while n < nbytes:
@@ -340,5 +264,4 @@
 				
                 	send_pkt(tb,payload)
-			time.sleep(0.5)
 			#print "printing payload",payload,"**\n"
                 	n += len(payload)
@@ -348,15 +271,13 @@
                 	data_pktno += 1
 	    		#print "before sleeping for 10 seconds and value of resend count is",resend_count
-			
-			tb.rxpath.min_freq = hop_freq - 80e3 
-    		    	tb.rxpath.max_freq = hop_freq + 80e3 
-			is_present = tb.rxpath.get_spec_stats(1e13,1) #check if primary user is present
-			if is_present == True: #if primary user is present then get out of this communication channel, back to sync mode.
+			time.sleep(0.2)
+			ch_energy = tb.rxpath.probe.level() #check if primary user is present
+			if int(ch_energy) > 1.50e8: #if primary user is there then dont transmit on this channel
 				mode = "sync"
-				sync_status = False
-				print "Moving out of this channel..Primary user detected\n"
+				continue
+				
 	################################################ end of Communications mode####################################		
-
-
+	
+    
 
 if __name__ == '__main__':
@@ -392,19 +313,4 @@
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
     
 
