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 | ##########spectrum sense specific imports |
---|
7 | from gnuradio import optfir, window |
---|
8 | from gnuradio import usrp |
---|
9 | #from gnuradio.eng_option import eng_option |
---|
10 | #from usrpm import usrp_dbid |
---|
11 | ########## |
---|
12 | from gnuradio.eng_option import eng_option |
---|
13 | from optparse import OptionParser |
---|
14 | from numpy import random |
---|
15 | import random, time, struct, sys, math |
---|
16 | |
---|
17 | # from current dir |
---|
18 | from transmit_path import transmit_path |
---|
19 | from receive_path import receive_path |
---|
20 | import spec_sense_path |
---|
21 | |
---|
22 | global sync_status |
---|
23 | sync_status = False #default value is "False"... |
---|
24 | global mode |
---|
25 | #Defining modes of operation |
---|
26 | #sync = synchronizing with other node before data transfer |
---|
27 | #traffic = data transfer |
---|
28 | #spec_sense = sensing spectrum for primary user |
---|
29 | mode = "sync" #default mode of operation... |
---|
30 | class my_top_block(gr.top_block): |
---|
31 | |
---|
32 | def __init__(self, mod_class, demod_class, |
---|
33 | rx_callback, options_tx,options_rx): |
---|
34 | |
---|
35 | gr.top_block.__init__(self) |
---|
36 | self.rxpath = receive_path(demod_class, rx_callback, options_rx) |
---|
37 | self.txpath = transmit_path(mod_class, options_tx) |
---|
38 | |
---|
39 | ########connect for the spectrum sense######### |
---|
40 | default_min_freq = 462.4825e6 #80 Khz to the left of channel 1 (462.5625e6) in frs band |
---|
41 | default_max_freq = 462.6425e6 #80 Khz to the right of channel 1 (462.5625e6) in frs band |
---|
42 | self.sp_sense_path = spec_sense_path.spec_sense_top_block(default_min_freq,default_min_freq) |
---|
43 | self.connect(self.sp_sense_path.u, self.sp_sense_path.s2v, self.sp_sense_path.fft, self.sp_sense_path.c2mag, self.sp_sense_path.stats) |
---|
44 | #self.connect(self.txpath); |
---|
45 | #self.connect(self.rxpath); |
---|
46 | |
---|
47 | |
---|
48 | #//////main////// |
---|
49 | |
---|
50 | global n_rcvd, n_right |
---|
51 | |
---|
52 | def main(): |
---|
53 | global n_rcvd, n_right,sync_status,mode |
---|
54 | n_rcvd = 0 |
---|
55 | n_right = 0 |
---|
56 | def send_pkt(self, payload='', eof=False): |
---|
57 | return self.txpath.send_pkt(payload, eof) |
---|
58 | |
---|
59 | def rx_callback(ok, payload): |
---|
60 | #print "inside rx callback" |
---|
61 | global n_rcvd, n_right, sync_status,mode |
---|
62 | (pktno,) = struct.unpack('!H', payload[0:2]) |
---|
63 | #temp = struct.unpack('!s', payload[3:10]) |
---|
64 | #print "printing the elements of packet",temp,"!!!!!!!!!!!\n" |
---|
65 | if mode == "sync": |
---|
66 | ##################################################### |
---|
67 | (sync_signal,) = struct.unpack('!s', payload[2]) |
---|
68 | (sync_signal_red,) = struct.unpack('!s', payload[3]) #redundant sync bit |
---|
69 | (data_channel,) = struct.unpack('!s', payload[4]) |
---|
70 | (data_channel_red,) = struct.unpack('!s', payload[5]) #redundant channel bit |
---|
71 | #print "printing the elements of packet ",sync_signal," !!!!!!!!!!! ",sync_signal_red," !!!!!\n" |
---|
72 | #print "printing the channel ",data_channel_red," !!!!!!!!!!! ",data_channel_red," !!!!!\n" |
---|
73 | if ok: #== True :# and str(sync_signal) == 'o' and str(sync_signal_red) == 'o': |
---|
74 | #print "inside if" |
---|
75 | if ok: #== True: #str(data_channel) == str(data_channel_red): |
---|
76 | #print "inside if1$$$$$" |
---|
77 | if str(data_channel) == str(ch): |
---|
78 | #print "inside if2" |
---|
79 | sync_status = True |
---|
80 | tb.stop() |
---|
81 | #print "after tb stop ans sync status is ",sync_status," sync status\n" |
---|
82 | #print "received a sync packet on channel %s\n" %(data_channel) |
---|
83 | #data = 'o'+'o'+str(data_channel)+str(data_channel) |
---|
84 | #print "sending this data",data,"@@@@@\n" |
---|
85 | #pktno=0 |
---|
86 | #ack_payload = struct.pack('!H', pktno & 0xffff) + data |
---|
87 | #print "printing the ack packet",ack_payload,"$$$$$\n" |
---|
88 | #k=0 |
---|
89 | #while k < 10000: |
---|
90 | #print "inside while" |
---|
91 | #send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
92 | #k=k+1 |
---|
93 | ##################################################### |
---|
94 | |
---|
95 | n_rcvd += 1 |
---|
96 | if ok: |
---|
97 | n_right += 1 |
---|
98 | |
---|
99 | #print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( |
---|
100 | #ok, pktno, n_rcvd, n_right) |
---|
101 | |
---|
102 | mods = modulation_utils.type_1_mods() |
---|
103 | demods = modulation_utils.type_1_demods() |
---|
104 | |
---|
105 | #setting up the tx options parser |
---|
106 | parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") |
---|
107 | expert_grp_tx = parser_tx.add_option_group("Expert_tx") |
---|
108 | print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group" |
---|
109 | parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), |
---|
110 | default='gmsk', |
---|
111 | help="Select modulation from: %s [default=%%default]" |
---|
112 | % (', '.join(mods.keys()),)) |
---|
113 | |
---|
114 | parser_tx.add_option("-s", "--size", type="eng_float", default=1500, |
---|
115 | help="set packet size [default=%default]") |
---|
116 | parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0, |
---|
117 | help="set megabytes to transmit [default=%default]") |
---|
118 | parser_tx.add_option("","--discontinuous", action="store_true", default=False, |
---|
119 | help="enable discontinous transmission (bursts of 5 packets)") |
---|
120 | parser_tx.add_option("","--from-file", default=None, |
---|
121 | help="use file for packet contents") |
---|
122 | print "printing parser_tx",parser_tx,"printing parser_tx" |
---|
123 | transmit_path.add_options(parser_tx, expert_grp_tx) |
---|
124 | |
---|
125 | for mod in mods.values(): |
---|
126 | mod.add_options(expert_grp_tx) |
---|
127 | |
---|
128 | (options_tx, args_tx) = parser_tx.parse_args () |
---|
129 | |
---|
130 | if len(args_tx) != 0: |
---|
131 | parser_tx.print_help() |
---|
132 | sys.exit(1) |
---|
133 | ############# Setting some default values for tx side of the block |
---|
134 | options_tx.tx_freq = 462.5625e6 |
---|
135 | options_tx.samples_per_symbol = 2 |
---|
136 | options_tx.modulation = 'dbpsk' |
---|
137 | options_tx.fusb_block_size = 4096 |
---|
138 | options_tx.fusb_nblocks = 16 |
---|
139 | ############# |
---|
140 | if options_tx.tx_freq is None: |
---|
141 | sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") |
---|
142 | parser_tx.print_help(sys.stderr) |
---|
143 | sys.exit(1) |
---|
144 | |
---|
145 | if options_tx.from_file is not None: |
---|
146 | source_file = open(options_tx.from_file, 'r') |
---|
147 | print "printing modulation type",mods[options_tx.modulation],"**" |
---|
148 | # build the graph |
---|
149 | print "printing tx options",options_tx,"&&&&\n" |
---|
150 | #tb = my_top_block(mods[options_tx.modulation], options_tx) |
---|
151 | |
---|
152 | #r = gr.enable_realtime_scheduling() |
---|
153 | #if r != gr.RT_OK: |
---|
154 | # print "Warning: failed to enable realtime scheduling" |
---|
155 | |
---|
156 | #setting up rx options parser |
---|
157 | |
---|
158 | # Create Options Parser: |
---|
159 | parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") |
---|
160 | print "printing parser_rx",parser_rx,"!!!!!\n" |
---|
161 | print "after option parser" |
---|
162 | expert_grp_rx = parser_rx.add_option_group("Expert_rx") |
---|
163 | print "printing expert group rx",expert_grp_rx,"@@@@\n" |
---|
164 | parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(), |
---|
165 | default='gmsk', |
---|
166 | help="Select modulation from: %s [default=%%default]" |
---|
167 | % (', '.join(demods.keys()),)) |
---|
168 | print "inside rx option parser" |
---|
169 | receive_path.add_options(parser_rx, expert_grp_rx) |
---|
170 | |
---|
171 | for mod in demods.values(): |
---|
172 | mod.add_options(expert_grp_rx) |
---|
173 | |
---|
174 | (options_rx, args_rx) = parser_rx.parse_args () |
---|
175 | |
---|
176 | if len(args_rx) != 0: |
---|
177 | parser_rx.print_help(sys.stderr) |
---|
178 | sys.exit(1) |
---|
179 | ############# Setting some default values for rx side of the block |
---|
180 | options_rx.rx_freq = 462.5625e6 #setting default rx_freq value |
---|
181 | options_rx.samples_per_symbol = 2 |
---|
182 | options_rx.modulation = 'dbpsk' |
---|
183 | options_rx.fusb_block_size = 4096 |
---|
184 | options_rx.fusb_nblocks = 16 |
---|
185 | ############# |
---|
186 | if options_rx.rx_freq is None: |
---|
187 | sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") |
---|
188 | parser_rx.print_help(sys.stderr) |
---|
189 | sys.exit(1) |
---|
190 | print "printing rx options",options_rx,"&&&&\n" |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | ######################## adding another top block for usrp spectrum sense ######################## |
---|
195 | trials = 10 |
---|
196 | |
---|
197 | #tb_spec_sense = spec_sense_top_block(default_min_freq,default_min_freq) |
---|
198 | #tb_spec_sense.subdev.select_rx_antenna('RX2') |
---|
199 | #tb_spec_sense.start() # start executing flow graph in another thread... |
---|
200 | #avg = tb_spec_sense.get_avg_power(trials) |
---|
201 | #print "printing the average power ",avg, "\n" |
---|
202 | #tb_spec_sense.stop() |
---|
203 | |
---|
204 | |
---|
205 | ################################################################################################## |
---|
206 | |
---|
207 | # build the graph |
---|
208 | |
---|
209 | tb = my_top_block(mods[options_tx.modulation], |
---|
210 | demods[options_rx.modulation], |
---|
211 | rx_callback,options_tx, |
---|
212 | options_rx) |
---|
213 | r = gr.enable_realtime_scheduling() |
---|
214 | if r != gr.RT_OK: |
---|
215 | print "Warning: failed to enable realtime scheduling" |
---|
216 | |
---|
217 | #tb.start() |
---|
218 | # generate and send packets |
---|
219 | #print "printing megamytes",options_tx.megabytes," $$$$\n" |
---|
220 | |
---|
221 | #n = 0 |
---|
222 | #pktno = 0 |
---|
223 | #pkt_size = int(options_tx.size) |
---|
224 | #options_tx.megabytes = 0.000048 |
---|
225 | |
---|
226 | #options_tx.size = 6 #2 bytes for packet size,2 bytes for the synchronization request(including redundant bit) and 2 bits to specify the channel number(including redundant bit) |
---|
227 | |
---|
228 | |
---|
229 | myfile = file("logfile.txt", 'w') |
---|
230 | myfile.write("") |
---|
231 | #sending sync packet-format |
---|
232 | global ch |
---|
233 | check =True |
---|
234 | #ch = random.randint(1, 7)#which channel to send information on...this is the second data packet of the synchronization frame |
---|
235 | #resend = 5; #number of times a batch of sync packets is sent on a channel before trying a different channel |
---|
236 | running = True |
---|
237 | #resend_count= 0 #initializing resend count |
---|
238 | #while running |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | if __name__ == '__main__': |
---|
244 | try: |
---|
245 | main() |
---|
246 | except KeyboardInterrupt: |
---|
247 | pass |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | |
---|
288 | |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | |
---|