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