root/vtcross/branches/sriram/benchmark_txrxnode2.py @ 359

Revision 359, 10.6 KB (checked in by sriram, 15 years ago)

More changes for getting the sync and data going one after the other

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#Transmission and reception, one ata a time on the same antena
3
4from gnuradio import gr, gru, modulation_utils
5from gnuradio import eng_notation
6from gnuradio.eng_option import eng_option
7from optparse import OptionParser
8from numpy import random
9import random, time, struct, sys, math
10import traceback
11# from current dir
12from transmit_path import transmit_path
13from receive_path import receive_path
14global sync_status,mode
15sync_status=False
16mode = "sync" #default mode of operation...
17class my_top_block(gr.top_block):
18
19    def __init__(self, mod_class, demod_class,
20                 rx_callback, options_tx,options_rx):
21
22        gr.top_block.__init__(self)
23        self.rxpath = receive_path(demod_class, rx_callback, options_rx)
24        self.txpath = transmit_path(mod_class, options_tx)
25        self.connect(self.txpath);
26        self.connect(self.rxpath);
27   
28#//////main//////
29
30global n_rcvd, n_right,ch,traffic_flag
31
32def main():
33    global n_rcvd, n_right,sync_status,sync_pktno,ch,mode,traffic_flag
34    sync_pktno =0
35    n_rcvd = 0
36    n_right = 0
37    def send_pkt(self, payload='', eof=False):
38        return self.txpath.send_pkt(payload, eof)
39
40    def rx_callback(ok, payload):
41        global n_rcvd, n_right,sync_status,sync_pktno,ch,mode,traffic_flag
42        #(pktno,) = struct.unpack('!H', payload[0:2])
43        print "inside rx callback"
44        ########################## sync ###########################
45        if mode == "sync":
46                if ok == True:
47                        (sync_signal,) = struct.unpack('!s', payload[2]) 
48                        (sync_signal_red,) = struct.unpack('!s', payload[3])  #redundant sync bit
49                        (data_channel,) = struct.unpack('!s', payload[4])
50                        (data_channel_red,) = struct.unpack('!s', payload[5])  #redundant channel bit
51                        print "printing the elements of packet  ",str(sync_signal),"  !!!!!!!!!!!  ",str(sync_signal_red)," !!!!!\n"     
52                        print "printing the channel  ",str(data_channel_red),"  !!!!!!!!!!!  ",str(data_channel_red)," !!!!!\n"
53                        #if str(sync_signal) == 's' and str(sync_signal_red) == 's' and str(data_channel) == str(data_channel_red):
54                        if str(data_channel) == str(ch):
55                                #print "inside if"
56                                sync_status = True
57                                #tb.stop()
58                                #print "received a sync packet on channel %s\n" %(data_channel)
59                                data = str('o'+'o'+str(data_channel)+str(data_channel))
60                                #print "sending this data",data,"@@@@@\n"
61                                #sync_pktno=0
62                                ack_payload = struct.pack('!H', sync_pktno & 0xffff) + data
63                                sync_pktno+=1
64                                #print "printing ack packet and sent pkt no ",ack_payload," ",sync_pktno,"\n"
65                                #print "printing the 3rd byte ",struct.unpack('!s', ack_payload[2]) ," \n"
66                                #k=0
67                                #while k < 10000:
68                                #print "inside while"
69                                send_pkt(tb,ack_payload) #sending back the acknowledgement
70                                #k=k+1
71                                print "ok = %5s  sync_pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
72                                    ok, sync_pktno, n_rcvd, n_right)
73                   
74        #####################################################
75       
76        ######################### traffic ############################
77        if mode == "traffic":
78                (data_header,) = struct.unpack('!s', payload[0])
79                if ok and data_header == 'd':
80                        traffic_flag = True
81                        comm = struct.unpack('!14s', data)
82                        print("received this ", comm)
83       
84        ##############################################################
85
86
87
88        n_rcvd += 1
89        if ok:
90            n_right += 1
91
92        #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
93        #    ok, pktno, n_rcvd, n_right)
94        #print "ok = %5s  n_rcvd = %4d  n_right = %4d" % (
95#            ok, n_rcvd, n_right)
96
97        #print "sync_status is ",sync_status," @@\n"
98
99    mods = modulation_utils.type_1_mods()
100    #print "printing mods",mods,"$$$$\n"
101    demods = modulation_utils.type_1_demods()
102
103    #setting up the tx options parser
104    parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
105    expert_grp_tx = parser_tx.add_option_group("Expert_tx")
106    #print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group"
107    parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
108                      default='gmsk',
109                      help="Select modulation from: %s [default=%%default]"
110                            % (', '.join(mods.keys()),))
111
112    parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
113                      help="set packet size [default=%default]")
114    parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
115                      help="set megabytes to transmit [default=%default]")
116    parser_tx.add_option("","--discontinuous", action="store_true", default=False,
117                      help="enable discontinous transmission (bursts of 5 packets)")
118    parser_tx.add_option("","--from-file", default=None,
119                      help="use file for packet contents")
120    print "printing parser_tx",parser_tx,"printing parser_tx"
121    transmit_path.add_options(parser_tx, expert_grp_tx)
122
123    for mod in mods.values():
124        mod.add_options(expert_grp_tx)
125
126    (options_tx, args_tx) = parser_tx.parse_args ()
127
128    if len(args_tx) != 0:
129        parser_tx.print_help()
130        sys.exit(1)
131
132    if options_tx.tx_freq is None:
133        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
134        parser_tx.print_help(sys.stderr)
135        sys.exit(1)
136
137    if options_tx.from_file is not None:
138        source_file = open(options_tx.from_file, 'r')
139    print "printing modulation type",mods[options_tx.modulation],"**"
140    # build the graph
141    print "printing tx options",options_tx,"&&&&\n"
142    #tb = my_top_block(mods[options_tx.modulation], options_tx)
143
144    #r = gr.enable_realtime_scheduling()
145    #if r != gr.RT_OK:
146    #   print "Warning: failed to enable realtime scheduling"
147
148    #setting up rx options parser
149   
150    # Create Options Parser:
151    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
152    print "printing parser_rx",parser_rx,"!!!!!\n"
153    print "after option parser"
154    expert_grp_rx = parser_rx.add_option_group("Expert_rx")
155    print "printing expert group rx",expert_grp_rx,"@@@@\n"
156    parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
157                      default='gmsk',
158                      help="Select modulation from: %s [default=%%default]"
159                            % (', '.join(demods.keys()),))
160    print "inside rx option parser"
161    receive_path.add_options(parser_rx, expert_grp_rx)
162
163    for mod in demods.values():
164        mod.add_options(expert_grp_rx)
165
166    (options_rx, args_rx) = parser_rx.parse_args ()
167
168    if len(args_rx) != 0:
169        parser_rx.print_help(sys.stderr)
170        sys.exit(1)
171
172    if options_rx.rx_freq is None:
173        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
174        parser_rx.print_help(sys.stderr)
175        sys.exit(1)
176    print "printing rx options",options_rx,"&&&&\n"
177    #options_rx.decim=128    #setting the decimation for receive path
178   
179# build the graph 
180
181    tb = my_top_block(mods[options_tx.modulation],
182                      demods[options_rx.modulation],
183                      rx_callback,options_tx,
184                      options_rx)
185    r = gr.enable_realtime_scheduling()
186    if r != gr.RT_OK:
187        print "Warning: failed to enable realtime scheduling"
188    print "printing the decimation ",options_rx.decim,"\n"
189    tb.start()
190    #listening to random frequencies untill a match is found
191    running = True
192    global ch
193    while running:
194        ################################################sync mode####################################
195        if mode == "sync":
196               
197                if sync_status != True:
198                        #print "inside while"
199                        #ch = random.randint(1, 7)
200                        ch = int(random.choice('17'))
201                        #ch = random.randint(6, 12)
202                        #ch = 1
203                        #hop_freq = float(1e6 * (400+(ch-1)*10))#setting the centre freq frequency for sending packets
204                        hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))
205                        #hop_freq = float(1e6 * (462.5625+(5-1)*0.05))
206                        print "hop freq is",hop_freq,"@@@@\n"
207                        tb.txpath.set_freq(hop_freq)
208                        tb.rxpath.set_freq(hop_freq)
209                        time.sleep(0.05)
210                        #time.sleep(2)
211                else:
212                        print "inside lese of while check"
213                        check_sync == 0
214                        print "sync channel is found! and it is channel ",ch," \n" 
215                        mode = "traffic"
216                        #tb.stop()
217                        #break
218                #temp_variable = 2
219                #if temp_variable == 2:
220                #       print "success"
221   
222               
223       
224        ################################################end of sync mode####################################
225
226        ################################################Communications mode####################################
227        if mode == "traffic":
228                print "Inside traffic mode"
229                if check_sync == 0
230                        time.sleep(0.4)
231                        if traffic_flag != True:
232                                mode = "sync"
233                                continue
234                        else:
235                                check_sync == 1
236                nbytes = 15
237                pkt_size = 15
238                sync_pktno = 0
239                while n < nbytes:
240                        #print >> myfile, "inside while"
241                        if options_tx.from_file is None:
242                                #data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255
243                                data = 'dI am fine.....' #Sending this message
244                                #print >> myfile, data
245                                #print "printing data",data,"****\n"
246                        else:
247                                data = source_file.read(pkt_size - 2)
248                                if data == '':
249                                        break;
250       
251                       
252                        payload = struct.pack('!15s', data)
253                        #print "printing payload",payload,"**\n"
254                               
255                        send_pkt(tb,payload)
256                        #print "printing payload",payload,"**\n"
257                        n += len(payload)
258                        sys.stderr.write('.')
259                        if options_tx.discontinuous and data_pktno % 5 == 4:
260                                time.sleep(1)
261                        data_pktno += 1
262                        #print "before sleeping for 10 seconds and value of resend count is",resend_count
263                        #time.sleep(0.1)
264                        tb.rxpath.min_freq = hop_freq - 80e3
265                        tb.rxpath.max_freq = hop_freq + 80e3
266                        is_present = tb.rxpath.get_spec_stats(1e13,10) #check if primary user is present
267                        if is_present == True: #if primary user is present then get out of this communication channel, back to sync mode.
268                                mode = "sync"
269       
270        ################################################ end of Communications mode####################################         
271
272
273
274    #var2  = 2
275    #if var2 ==2:
276    #    print "just checking this bug"
277   
278 
279    # generate and send packets
280    #print "printing megamytes",options_tx.megabytes,"  $$$$\n"
281    #myfile = file("log.txt", 'w')
282    #traceback.print_tb(traceback,None,myfile)
283
284    #tb.wait()
285    #tb.stop()
286
287if __name__ == '__main__':
288    try:
289        main()
290    except KeyboardInterrupt:
291        pass
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319   
320
321
322
323
Note: See TracBrowser for help on using the browser.