1 | #!/usr/bin/env python |
---|
2 | #Transmission and reception, one ata a time on the same antena |
---|
3 | |
---|
4 | from gnuradio import gr, gru, modulation_utils |
---|
5 | from gnuradio import eng_notation |
---|
6 | from gnuradio.eng_option import eng_option |
---|
7 | from optparse import OptionParser |
---|
8 | from numpy import random |
---|
9 | import random, time, struct, sys, math |
---|
10 | import traceback |
---|
11 | # from current dir |
---|
12 | from transmit_path import transmit_path |
---|
13 | from receive_path import receive_path |
---|
14 | global sync_status |
---|
15 | sync_status=False |
---|
16 | |
---|
17 | class 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 | |
---|
30 | global n_rcvd, n_right,ch |
---|
31 | |
---|
32 | def main(): |
---|
33 | global n_rcvd, n_right,sync_status,pktno1,ch |
---|
34 | pktno1 =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,pktno1,ch |
---|
42 | #(pktno,) = struct.unpack('!H', payload[0:2]) |
---|
43 | print "inside rx callback" |
---|
44 | if ok == True: |
---|
45 | (sync_signal,) = struct.unpack('!s', payload[2]) |
---|
46 | (sync_signal_red,) = struct.unpack('!s', payload[3]) #redundant sync bit |
---|
47 | (data_channel,) = struct.unpack('!s', payload[4]) |
---|
48 | (data_channel_red,) = struct.unpack('!s', payload[5]) #redundant channel bit |
---|
49 | print "printing the elements of packet ",str(sync_signal)," !!!!!!!!!!! ",str(sync_signal_red)," !!!!!\n" |
---|
50 | print "printing the channel ",str(data_channel_red)," !!!!!!!!!!! ",str(data_channel_red)," !!!!!\n" |
---|
51 | #if str(sync_signal) == 's' and str(sync_signal_red) == 's' and str(data_channel) == str(data_channel_red): |
---|
52 | if str(data_channel) == str(ch): |
---|
53 | #print "inside if" |
---|
54 | sync_status = True |
---|
55 | #tb.stop() |
---|
56 | #print "received a sync packet on channel %s\n" %(data_channel) |
---|
57 | data = str('o'+'o'+str(data_channel)+str(data_channel)) |
---|
58 | #print "sending this data",data,"@@@@@\n" |
---|
59 | #pktno1=0 |
---|
60 | ack_payload = struct.pack('!H', pktno1 & 0xffff) + data |
---|
61 | pktno1+=1 |
---|
62 | #print "printing ack packet and sent pkt no ",ack_payload," ",pktno1,"\n" |
---|
63 | #print "printing the 3rd byte ",struct.unpack('!s', ack_payload[2]) ," \n" |
---|
64 | #k=0 |
---|
65 | #while k < 10000: |
---|
66 | #print "inside while" |
---|
67 | send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
68 | #send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
69 | #send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
70 | #send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
71 | #send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
72 | #send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
73 | #send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
74 | |
---|
75 | #k=k+1 |
---|
76 | |
---|
77 | n_rcvd += 1 |
---|
78 | if ok: |
---|
79 | n_right += 1 |
---|
80 | |
---|
81 | #print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( |
---|
82 | # ok, pktno, n_rcvd, n_right) |
---|
83 | #print "ok = %5s n_rcvd = %4d n_right = %4d" % ( |
---|
84 | # ok, n_rcvd, n_right) |
---|
85 | |
---|
86 | #print "sync_status is ",sync_status," @@\n" |
---|
87 | |
---|
88 | mods = modulation_utils.type_1_mods() |
---|
89 | #print "printing mods",mods,"$$$$\n" |
---|
90 | demods = modulation_utils.type_1_demods() |
---|
91 | |
---|
92 | #setting up the tx options parser |
---|
93 | parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") |
---|
94 | expert_grp_tx = parser_tx.add_option_group("Expert_tx") |
---|
95 | #print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group" |
---|
96 | parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), |
---|
97 | default='gmsk', |
---|
98 | help="Select modulation from: %s [default=%%default]" |
---|
99 | % (', '.join(mods.keys()),)) |
---|
100 | |
---|
101 | parser_tx.add_option("-s", "--size", type="eng_float", default=1500, |
---|
102 | help="set packet size [default=%default]") |
---|
103 | parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0, |
---|
104 | help="set megabytes to transmit [default=%default]") |
---|
105 | parser_tx.add_option("","--discontinuous", action="store_true", default=False, |
---|
106 | help="enable discontinous transmission (bursts of 5 packets)") |
---|
107 | parser_tx.add_option("","--from-file", default=None, |
---|
108 | help="use file for packet contents") |
---|
109 | print "printing parser_tx",parser_tx,"printing parser_tx" |
---|
110 | transmit_path.add_options(parser_tx, expert_grp_tx) |
---|
111 | |
---|
112 | for mod in mods.values(): |
---|
113 | mod.add_options(expert_grp_tx) |
---|
114 | |
---|
115 | (options_tx, args_tx) = parser_tx.parse_args () |
---|
116 | |
---|
117 | if len(args_tx) != 0: |
---|
118 | parser_tx.print_help() |
---|
119 | sys.exit(1) |
---|
120 | |
---|
121 | if options_tx.tx_freq is None: |
---|
122 | sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") |
---|
123 | parser_tx.print_help(sys.stderr) |
---|
124 | sys.exit(1) |
---|
125 | |
---|
126 | if options_tx.from_file is not None: |
---|
127 | source_file = open(options_tx.from_file, 'r') |
---|
128 | print "printing modulation type",mods[options_tx.modulation],"**" |
---|
129 | # build the graph |
---|
130 | print "printing tx options",options_tx,"&&&&\n" |
---|
131 | #tb = my_top_block(mods[options_tx.modulation], options_tx) |
---|
132 | |
---|
133 | #r = gr.enable_realtime_scheduling() |
---|
134 | #if r != gr.RT_OK: |
---|
135 | # print "Warning: failed to enable realtime scheduling" |
---|
136 | |
---|
137 | #setting up rx options parser |
---|
138 | |
---|
139 | # Create Options Parser: |
---|
140 | parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") |
---|
141 | print "printing parser_rx",parser_rx,"!!!!!\n" |
---|
142 | print "after option parser" |
---|
143 | expert_grp_rx = parser_rx.add_option_group("Expert_rx") |
---|
144 | print "printing expert group rx",expert_grp_rx,"@@@@\n" |
---|
145 | parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(), |
---|
146 | default='gmsk', |
---|
147 | help="Select modulation from: %s [default=%%default]" |
---|
148 | % (', '.join(demods.keys()),)) |
---|
149 | print "inside rx option parser" |
---|
150 | receive_path.add_options(parser_rx, expert_grp_rx) |
---|
151 | |
---|
152 | for mod in demods.values(): |
---|
153 | mod.add_options(expert_grp_rx) |
---|
154 | |
---|
155 | (options_rx, args_rx) = parser_rx.parse_args () |
---|
156 | |
---|
157 | if len(args_rx) != 0: |
---|
158 | parser_rx.print_help(sys.stderr) |
---|
159 | sys.exit(1) |
---|
160 | |
---|
161 | if options_rx.rx_freq is None: |
---|
162 | sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") |
---|
163 | parser_rx.print_help(sys.stderr) |
---|
164 | sys.exit(1) |
---|
165 | print "printing rx options",options_rx,"&&&&\n" |
---|
166 | #options_rx.decim=128 #setting the decimation for receive path |
---|
167 | |
---|
168 | # build the graph |
---|
169 | |
---|
170 | tb = my_top_block(mods[options_tx.modulation], |
---|
171 | demods[options_rx.modulation], |
---|
172 | rx_callback,options_tx, |
---|
173 | options_rx) |
---|
174 | r = gr.enable_realtime_scheduling() |
---|
175 | if r != gr.RT_OK: |
---|
176 | print "Warning: failed to enable realtime scheduling" |
---|
177 | print "printing the decimation ",options_rx.decim,"\n" |
---|
178 | tb.start() |
---|
179 | #listening to random frequencies untill a match is found |
---|
180 | check = True |
---|
181 | global ch |
---|
182 | while check: |
---|
183 | if sync_status != True: |
---|
184 | #print "inside while" |
---|
185 | #ch = random.randint(1, 7) |
---|
186 | ch = int(random.choice('17')) |
---|
187 | #ch = random.randint(6, 12) |
---|
188 | #ch = 1 |
---|
189 | #hop_freq = float(1e6 * (400+(ch-1)*10))#setting the centre freq frequency for sending packets |
---|
190 | hop_freq = float(1e6 * (462.5625+(ch-1)*0.025)) |
---|
191 | #hop_freq = float(1e6 * (462.5625+(5-1)*0.05)) |
---|
192 | print "hop freq is",hop_freq,"@@@@\n" |
---|
193 | tb.txpath.set_freq(hop_freq) |
---|
194 | tb.rxpath.set_freq(hop_freq) |
---|
195 | time.sleep(0.05) |
---|
196 | #time.sleep(2) |
---|
197 | else: |
---|
198 | print "inside lese of while check" |
---|
199 | #tb.stop() |
---|
200 | break |
---|
201 | temp_variable = 2 |
---|
202 | if temp_variable == 2: |
---|
203 | print "success" |
---|
204 | |
---|
205 | print "sync channel is found! and it is channel ",ch," \n" |
---|
206 | #var2 = 2 |
---|
207 | #if var2 ==2: |
---|
208 | # print "just checking this bug" |
---|
209 | |
---|
210 | |
---|
211 | # generate and send packets |
---|
212 | #print "printing megamytes",options_tx.megabytes," $$$$\n" |
---|
213 | #myfile = file("log.txt", 'w') |
---|
214 | #traceback.print_tb(traceback,None,myfile) |
---|
215 | |
---|
216 | #tb.wait() |
---|
217 | tb.stop() |
---|
218 | |
---|
219 | if __name__ == '__main__': |
---|
220 | try: |
---|
221 | main() |
---|
222 | except KeyboardInterrupt: |
---|
223 | pass |
---|
224 | |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|