Index: /vtcross/branches/sriram/benchmark_txrxnode2.py
===================================================================
--- /vtcross/branches/sriram/benchmark_txrxnode2.py	(revision 364)
+++ /vtcross/branches/sriram/benchmark_txrxnode2.py	(revision 369)
@@ -8,11 +8,12 @@
 from numpy import random
 import random, time, struct, sys, math
-import traceback
+
 # from current dir
 from transmit_path import transmit_path
 from receive_path import receive_path
-global sync_status,mode
-sync_status=False
-mode = "sync" #default mode of operation...
+global sync_status,mode,traffic_flag,ch
+sync_status="False"
+mode = "sync" #default mode
+traffic_flag = False
 class my_top_block(gr.top_block):
 
@@ -28,10 +29,8 @@
 #//////main////// 
 
-global n_rcvd, n_right,ch,traffic_flag
-traffic_flag = False
+global n_rcvd, n_right
 
 def main():
-    global n_rcvd, n_right,sync_status,sync_pktno,ch,mode,traffic_flag
-    sync_pktno =0
+    global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
     n_rcvd = 0
     n_right = 0
@@ -40,61 +39,36 @@
 
     def rx_callback(ok, payload):
-        global n_rcvd, n_right,sync_status,sync_pktno,ch,mode,traffic_flag
-        #(pktno,) = struct.unpack('!H', payload[0:2])
-        print "inside rx callback"
-	########################## sync ###########################
+        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag
+        ########################## sync ###########################
 	if mode == "sync":
         	if ok == True:
-	    		(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  ",str(sync_signal),"  !!!!!!!!!!!  ",str(sync_signal_red)," !!!!!\n"     
-            		print "printing the channel  ",str(data_channel_red),"  !!!!!!!!!!!  ",str(data_channel_red)," !!!!!\n"
-	    		#if str(sync_signal) == 's' and str(sync_signal_red) == 's' and str(data_channel) == str(data_channel_red):
-	    		if str(data_channel) == str(ch):
-            			#print "inside if"
-            			sync_status = True
-				#tb.stop()
-                		#print "received a sync packet on channel %s\n" %(data_channel)
-	        		data = str('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
-				sync_pktno+=1
-				#print "printing ack packet and sent pkt no ",ack_payload," ",sync_pktno,"\n"
-				#print "printing the 3rd byte ",struct.unpack('!s', ack_payload[2]) ," \n"
-                		#k=0
-                		#while k < 10000:
-				#print "inside while"
-	        		send_pkt(tb,ack_payload) #sending back the acknowledgement
-		                #k=k+1
-				print "ok = %5s  sync_pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
-        			    ok, sync_pktno, n_rcvd, n_right)
- 		    
-        #####################################################
+	    		(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) == 's' and str(data_channel) == str(ch): 
+				sync_status = "True"
+				print "received a sync packet on channel %s\n" %(data_channel)
+	       			data = 'o'
+               			pktno=0
+	       			ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data
+		      		send_pkt(tb,ack_payload) #sending back the acknowledgement
+
+	#####################################################
 	
 	######################### traffic ############################
 	if mode == "traffic":
-		
-		if ok:
-			print "received a packet"
+		if ok: 
 			(data_header,) = struct.unpack('!s', payload[0])
 			if data_header == 'd':
 				traffic_flag = True
-				(comm,) = struct.unpack('!14s', payload[1:15])
-				print "received this ", comm,"\n"
+				comm = struct.unpack('!14s', payload[1:15])
+				print("received this ", comm) 
 				data = 'dI am fine.....' #Sending this message
 				payload = struct.pack('!15s', data)
 				send_pkt(tb,payload)
-		
-		else:
-			print "not ok"
-	
 	##############################################################
 
-
-
-	n_rcvd += 1
+       	n_rcvd += 1
         if ok:
             n_right += 1
@@ -102,7 +76,4 @@
         #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
         #    ok, pktno, n_rcvd, n_right)
-        #print "ok = %5s  n_rcvd = %4d  n_right = %4d" % (
-#            ok, n_rcvd, n_right)
-
 	#print "sync_status is ",sync_status," @@\n"
 
@@ -140,5 +111,5 @@
         sys.exit(1)
     ############# Setting some default values for tx side of the block
-    options_tx.tx_freq = 462.5625e6 
+    options_tx.tx_freq = 470e6#462.5625e6
     options_tx.samples_per_symbol =  2
     options_tx.modulation = 'dbpsk'
@@ -147,4 +118,5 @@
     options_tx.bitrate = 0.0125e6
     #############
+
     if options_tx.tx_freq is None:
         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
@@ -157,13 +129,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"
@@ -187,5 +151,5 @@
         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 = 470e6#462.5625e6 #setting default rx_freq value
     options_rx.samples_per_symbol =  2
     options_rx.modulation = 'dbpsk'
@@ -194,4 +158,5 @@
     options_rx.bitrate = 0.0125e6
     #############
+
     if options_rx.rx_freq is None:
         sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
@@ -199,7 +164,6 @@
         sys.exit(1)
     print "printing rx options",options_rx,"&&&&\n"
-    #options_rx.decim=128    #setting the decimation for receive path
-    
-# build the graph  
+
+# build the graph
 
     tb = my_top_block(mods[options_tx.modulation],
@@ -210,6 +174,8 @@
     if r != gr.RT_OK:
         print "Warning: failed to enable realtime scheduling"
-    print "printing the decimation ",options_rx.decim,"\n"
+    
     tb.start()
+    #tb.rxpath.disconnect(tb.rxpath.u, tb.rxpath.s2v,tb.rxpath.fft, tb.rxpath.c2mag, tb.rxpath.stats)
+    #tb.rxpath.connect(tb.rxpath.u, tb.rxpath.s2v)
     #listening to random frequencies untill a match is found
     running = True
@@ -218,72 +184,56 @@
 	################################################sync mode####################################
     	if mode == "sync":
-		
-    		if sync_status != True:
+    		if sync_status != "True":
         		#print "inside while"
-        		#ch = random.randint(1, 7)
-			ch = int(random.choice('37'))
-			#ch = random.randint(6, 12)
-			#ch = 1
-        		#hop_freq = float(1e6 * (400+(ch-1)*10))#setting the centre freq frequency for sending packets
-			hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))
-			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
-			print "hop freq is",hop_freq,"@@@@\n"
-			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
-			#hop_freq = float(1e6 * (462.5625+(5-1)*0.05))
-        		
+        		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
+        		print "hop freq is",hop_freq,"@@@@\n"
+			
         		tb.txpath.set_freq(hop_freq)
         		tb.rxpath.set_freq(hop_freq)
+			ch_energy = tb.rxpath.probe.level() #check if primary user is present
+			print "channel energy is ",ch_energy,"\n"
+			if int(ch_energy) > 1.50e8: #if primary user is there then dont transmit on this channel
+				continue
 			time.sleep(0.05)
-			#time.sleep(2)
 		else:
-			print "inside lese of while check"
+			print "sync channel is found...channel ",ch,"\n"
 			check_sync = 0
-			traffic_flag  = False
-			print "sync channel is found! and it is channel ",ch," \n"  
 			mode = "traffic"
-			#tb.stop()
-			#break
-    		#temp_variable = 2
-    		#if temp_variable == 2:
-        	#	print "success"
-    
-    		
-	
-	################################################end of sync mode####################################
-
-	################################################Communications mode####################################	
+			traffic_flag = False
+			sync_status="False"
+			continue
+    
+    ################################################end of sync mode####################################
+
+    ################################################Communications mode####################################	
 	if mode == "traffic":
 		print "Inside traffic mode"
-		##################### for ensuring sync #####################
 		if check_sync == 0:
-			time.sleep(0.4)
+			time.sleep(0.8)
 			if traffic_flag != True:
 				mode = "sync"
-				sync_status=False
 				continue
 			else:
-				check_sync == 1	
-		############################################################
-
+				check_sync = 1	
 		#nbytes = 15
     	    	#pkt_size = 15
-		#data_pktno = 0
-		#n = 0
+		#sync_pktno = 0
+		#n =0
 		#while n < nbytes:
-                #	#print >> myfile, "inside while"
+                	#print >> myfile, "inside while"
                	#	if options_tx.from_file is None:
-                # 		#data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255
-               	# 		data = 'dI am fine.....' #Sending this message
-	        #		#print >> myfile, data
-                #		#print "printing data",data,"****\n"
+                 		#data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255
+                #		data = 'dI am fine.....' #Sending this message
+	        		#print >> myfile, data
+                		#print "printing data",data,"****\n"
                 #	else:
             	#	    	data = source_file.read(pkt_size - 2)
             	#	    	if data == '':
                 #	        	break;
-		#
+	
 		#	
                 #	payload = struct.pack('!15s', data)
@@ -291,5 +241,4 @@
 				
                 #	send_pkt(tb,payload)
-		#	time.sleep(0.5)
 			#print "printing payload",payload,"**\n"
                 #	n += len(payload)
@@ -298,31 +247,17 @@
                 #		time.sleep(1)
                 #	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,10) #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.
+	    	#	#print "before sleeping for 10 seconds and value of resend count is",resend_count
+		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"
-		time.sleep(0.5)	
-	################################################ end of Communications mode####################################		
-
-
-
-    #var2  = 2
-    #if var2 ==2:
-    #    print "just checking this bug"
-    
- 
-    # generate and send packets
-    #print "printing megamytes",options_tx.megabytes,"  $$$$\n"
-    #myfile = file("log.txt", 'w')
-    #traceback.print_tb(traceback,None,myfile)
+                	continue
+	
+    ################################################ end of Communications mode####################################	
+
+
 
     #tb.wait()
-    #tb.stop()
-
+    
 if __name__ == '__main__':
     try:
