Index: /vtcross/branches/sriram/dsa.py
===================================================================
--- /vtcross/branches/sriram/dsa.py	(revision 383)
+++ /vtcross/branches/sriram/dsa.py	(revision 384)
@@ -8,4 +8,5 @@
 from numpy import random
 import random, time, struct, sys, math
+from datetime import datetime
 
 # from current dir
@@ -31,11 +32,22 @@
 def main():
 	print "inside main"
-   	global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts
+   	global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag
     	n_rcvd = 0
     	n_right = 0
 	n_attempts = 5
+	return_flag = 0
     	def send_pkt(self, payload='', eof=False):
         	return self.txpath.send_pkt(payload, eof)
-
+	
+	def get_freq(hop_freq,probe_level,absent_time):
+
+		channel = int(random.choice([1,7,8,14]))
+	    	if channel < 8:
+			hop_freq = float(1e6 * (462.5625+(channel-1)*0.025))#setting the centre freq frequency for sending packets
+	 	else:
+			hop_freq = float(1e6 * (467.5625+(channel-8)*0.025))#setting the centre freq frequency for sending packets	
+
+		return channel,hop_freq #returning the channel number and hop frequency
+	
 
 	def rx_callback(ok, payload):
@@ -129,5 +141,5 @@
 	
     	############# Setting some default values for tx side of the block
-    	options_tx.tx_freq = 470e6#462.5625e6
+    	options_tx.tx_freq = 462.5625e6
     	options_tx.samples_per_symbol =  2
     	options_tx.modulation = 'dbpsk'
@@ -169,5 +181,5 @@
         	sys.exit(1)
     	############# Setting some default values for rx side of the block
-    	options_rx.rx_freq = 470e6#462.5625e6 #setting default rx_freq value
+    	options_rx.rx_freq = 462.5625e6 #setting default rx_freq value
     	options_rx.samples_per_symbol =  2
     	options_rx.modulation = 'dbpsk'
@@ -200,36 +212,30 @@
     	global ch
 	running = True
-    
+    	ch_energy = tb.rxpath.probe.level() #setting initial value
+	hop_freq = options_tx.tx_freq #  = options_rx.rx_freq...same for tx and rx side
    	while running:
-
 
 		################################################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)
+	    			
+				if return_flag == 0:
+					ch,hop_freq = get_freq(hop_freq,ch_energy,0)
+				else:
+					ch,hop_freq = get_freq(hop_freq,ch_energy,elapsed_time)
+					return_flag = 0
+
+				tb.txpath.set_freq(hop_freq)
 	    			tb.rxpath.set_freq(hop_freq)
-        		
-				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) > 2e7: #if primary user is there then dont transmit on this channel
+      		
+				ch_energy = tb.rxpath.probe.level() #check if primary user is present
+				
+				print "printing hop frequency ",hop_freq,"and energy is ",ch_energy,"\n"
+				if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel
 					#while 
 					continue
 					#print "present"
-			
-				nbytes = 5 #int(1e6 * .0003)
+				
+	    			nbytes = 5 #int(1e6 * .0003)
 				pkt_size = 5
 	    			n = 0
@@ -262,4 +268,5 @@
 				traffic_flag = False
 				sync_status="False"
+				start_time = datetime.now() #measuring the time for which the primary user is away
 	
 		################################################end of sync mode####################################
@@ -297,5 +304,5 @@
                 		data_pktno += 1
 	    			#print "before sleeping for 10 seconds and value of resend count is",resend_count
-				time.sleep(0.1 + 0.05*int(random.choice([0,1,2,3])))
+				time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3])))
 
 				if traffic_flag != True:
@@ -307,6 +314,10 @@
 				ch_energy = tb.rxpath.probe.level() #check if primary user is present
 				print "channel energy is ",ch_energy,"\n"
-            			if int(ch_energy) > 2e7: #if primary user is there then dont transmit on this channel
+            			if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel
+					stop_time = datetime.now()	
+					_elapsed_time  = start_time - stop_time
+					elapsed_time = _elapsed_time.seconds
 					mode = "sync"
+					return_flag = 1
                 
 
