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

Revision 358, 10.4 KB (checked in by sriram, 15 years ago)

Adding more changes to incorporate communication signal and spectrum sense

  • 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
16
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                if ok == True:
79                        traffic_flag = True
80                        comm = struct.unpack('!14s', data)
81                        print("received this ", comm)
82       
83        ##############################################################
84
85
86
87        n_rcvd += 1
88        if ok:
89            n_right += 1
90
91        #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % (
92        #    ok, pktno, n_rcvd, n_right)
93        #print "ok = %5s  n_rcvd = %4d  n_right = %4d" % (
94#            ok, n_rcvd, n_right)
95
96        #print "sync_status is ",sync_status," @@\n"
97
98    mods = modulation_utils.type_1_mods()
99    #print "printing mods",mods,"$$$$\n"
100    demods = modulation_utils.type_1_demods()
101
102    #setting up the tx options parser
103    parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve")
104    expert_grp_tx = parser_tx.add_option_group("Expert_tx")
105    #print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group"
106    parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(),
107                      default='gmsk',
108                      help="Select modulation from: %s [default=%%default]"
109                            % (', '.join(mods.keys()),))
110
111    parser_tx.add_option("-s", "--size", type="eng_float", default=1500,
112                      help="set packet size [default=%default]")
113    parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0,
114                      help="set megabytes to transmit [default=%default]")
115    parser_tx.add_option("","--discontinuous", action="store_true", default=False,
116                      help="enable discontinous transmission (bursts of 5 packets)")
117    parser_tx.add_option("","--from-file", default=None,
118                      help="use file for packet contents")
119    print "printing parser_tx",parser_tx,"printing parser_tx"
120    transmit_path.add_options(parser_tx, expert_grp_tx)
121
122    for mod in mods.values():
123        mod.add_options(expert_grp_tx)
124
125    (options_tx, args_tx) = parser_tx.parse_args ()
126
127    if len(args_tx) != 0:
128        parser_tx.print_help()
129        sys.exit(1)
130
131    if options_tx.tx_freq is None:
132        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
133        parser_tx.print_help(sys.stderr)
134        sys.exit(1)
135
136    if options_tx.from_file is not None:
137        source_file = open(options_tx.from_file, 'r')
138    print "printing modulation type",mods[options_tx.modulation],"**"
139    # build the graph
140    print "printing tx options",options_tx,"&&&&\n"
141    #tb = my_top_block(mods[options_tx.modulation], options_tx)
142
143    #r = gr.enable_realtime_scheduling()
144    #if r != gr.RT_OK:
145    #   print "Warning: failed to enable realtime scheduling"
146
147    #setting up rx options parser
148   
149    # Create Options Parser:
150    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve")
151    print "printing parser_rx",parser_rx,"!!!!!\n"
152    print "after option parser"
153    expert_grp_rx = parser_rx.add_option_group("Expert_rx")
154    print "printing expert group rx",expert_grp_rx,"@@@@\n"
155    parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(),
156                      default='gmsk',
157                      help="Select modulation from: %s [default=%%default]"
158                            % (', '.join(demods.keys()),))
159    print "inside rx option parser"
160    receive_path.add_options(parser_rx, expert_grp_rx)
161
162    for mod in demods.values():
163        mod.add_options(expert_grp_rx)
164
165    (options_rx, args_rx) = parser_rx.parse_args ()
166
167    if len(args_rx) != 0:
168        parser_rx.print_help(sys.stderr)
169        sys.exit(1)
170
171    if options_rx.rx_freq is None:
172        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n")
173        parser_rx.print_help(sys.stderr)
174        sys.exit(1)
175    print "printing rx options",options_rx,"&&&&\n"
176    #options_rx.decim=128    #setting the decimation for receive path
177   
178# build the graph 
179
180    tb = my_top_block(mods[options_tx.modulation],
181                      demods[options_rx.modulation],
182                      rx_callback,options_tx,
183                      options_rx)
184    r = gr.enable_realtime_scheduling()
185    if r != gr.RT_OK:
186        print "Warning: failed to enable realtime scheduling"
187    print "printing the decimation ",options_rx.decim,"\n"
188    tb.start()
189    #listening to random frequencies untill a match is found
190    running = True
191    global ch
192    while running:
193        ################################################sync mode####################################
194        if mode == "sync":
195                if sync_status != True:
196                        #print "inside while"
197                        #ch = random.randint(1, 7)
198                        ch = int(random.choice('17'))
199                        #ch = random.randint(6, 12)
200                        #ch = 1
201                        #hop_freq = float(1e6 * (400+(ch-1)*10))#setting the centre freq frequency for sending packets
202                        hop_freq = float(1e6 * (462.5625+(ch-1)*0.025))
203                        #hop_freq = float(1e6 * (462.5625+(5-1)*0.05))
204                        print "hop freq is",hop_freq,"@@@@\n"
205                        tb.txpath.set_freq(hop_freq)
206                        tb.rxpath.set_freq(hop_freq)
207                        time.sleep(0.05)
208                        #time.sleep(2)
209                else:
210                        print "inside lese of while check"
211                        #tb.stop()
212                        break
213                temp_variable = 2
214                if temp_variable == 2:
215                        print "success"
216   
217                print "sync channel is found! and it is channel ",ch," \n" 
218                mode = "traffic"
219       
220        ################################################end of sync mode####################################
221
222        ################################################Communications mode####################################
223        if mode == "traffic":
224                time.sleep(0.4)
225                if traffic_flag != True
226                        mode = "sync"
227                nbytes = 14
228                pkt_size = 14
229                sync_pktno = 0
230                while n < nbytes:
231                        #print >> myfile, "inside while"
232                        if options_tx.from_file is None:
233                                #data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255
234                                data = 'I am fine.....' #Sending this message
235                                #print >> myfile, data
236                                #print "printing data",data,"****\n"
237                        else:
238                                data = source_file.read(pkt_size - 2)
239                                if data == '':
240                                        break;
241       
242                       
243                        payload = struct.pack('!14s', data)
244                        #print "printing payload",payload,"**\n"
245                               
246                        send_pkt(tb,payload)
247                        #print "printing payload",payload,"**\n"
248                        n += len(payload)
249                        sys.stderr.write('.')
250                        if options_tx.discontinuous and data_pktno % 5 == 4:
251                                time.sleep(1)
252                        data_pktno += 1
253                        #print "before sleeping for 10 seconds and value of resend count is",resend_count
254                        #time.sleep(0.1)
255                        tb.rxpath.min_freq = hop_freq - 80e3
256                        tb.rxpath.max_freq = hop_freq + 80e3
257                        is_present = tb.rxpath.get_spec_stats(1e13,10) #check if primary user is present
258                        if is_present == True: #if primary user is present then get out of this communication channel, back to sync mode.
259                                mode = "sync"
260       
261        ################################################ end of Communications mode####################################         
262
263
264
265    #var2  = 2
266    #if var2 ==2:
267    #    print "just checking this bug"
268   
269 
270    # generate and send packets
271    #print "printing megamytes",options_tx.megabytes,"  $$$$\n"
272    #myfile = file("log.txt", 'w')
273    #traceback.print_tb(traceback,None,myfile)
274
275    #tb.wait()
276    #tb.stop()
277
278if __name__ == '__main__':
279    try:
280        main()
281    except KeyboardInterrupt:
282        pass
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310   
311
312
313
314
Note: See TracBrowser for help on using the browser.