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 | from datetime import datetime |
---|
11 | |
---|
12 | # from current dir |
---|
13 | from transmit_path import transmit_path |
---|
14 | from receive_path import receive_path |
---|
15 | |
---|
16 | # import cross |
---|
17 | from cross import * |
---|
18 | |
---|
19 | |
---|
20 | global sync_status,mode,ch,traffic_flag,n_rcvd, n_right |
---|
21 | sync_status = False |
---|
22 | #Defining modes of operation |
---|
23 | # sync: the two nodes are trying to rendezvous on a common channel |
---|
24 | # traffic: the two node are communicating information to each other |
---|
25 | mode = "sync" #Default mode is sync |
---|
26 | traffic_flag = False |
---|
27 | class my_top_block(gr.top_block): |
---|
28 | |
---|
29 | def __init__(self, mod_class, demod_class, |
---|
30 | rx_callback, options_tx,options_rx): |
---|
31 | |
---|
32 | gr.top_block.__init__(self) |
---|
33 | self.rxpath = receive_path(demod_class, rx_callback, options_rx) |
---|
34 | self.txpath = transmit_path(mod_class, options_tx) |
---|
35 | self.connect(self.txpath); |
---|
36 | self.connect(self.rxpath); |
---|
37 | |
---|
38 | |
---|
39 | def main(): |
---|
40 | |
---|
41 | global stats_array, count_array, time_array, n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag |
---|
42 | n_rcvd = 0 |
---|
43 | n_right = 0 |
---|
44 | n_attempts = 5 |
---|
45 | return_flag = 0 |
---|
46 | |
---|
47 | count_array = [ 0, 0, 0, 0, 0] |
---|
48 | time_array = [ 0, 0, 0, 0, 0] |
---|
49 | stats_array = [ 0, 0, 0, 0, 0] |
---|
50 | |
---|
51 | |
---|
52 | def send_pkt(self, payload='', eof=False): |
---|
53 | return self.txpath.send_pkt(payload, eof) |
---|
54 | |
---|
55 | def get_real_channel(channel): |
---|
56 | |
---|
57 | real_channel = 1; |
---|
58 | |
---|
59 | if channel == 1: |
---|
60 | real_channel = 1 |
---|
61 | if channel == 2: |
---|
62 | real_channel = 7 |
---|
63 | if channel == 3: |
---|
64 | real_channel = 8 |
---|
65 | if channel == 4: |
---|
66 | real_channel = 14 |
---|
67 | |
---|
68 | return real_channel |
---|
69 | |
---|
70 | def get_average_time(channel,absent_time): |
---|
71 | |
---|
72 | global count_array, time_array |
---|
73 | |
---|
74 | count_array[channel] = count_array[channel] + 1 |
---|
75 | average_time = (time_array[channel] + absent_time) / count_array[channel] |
---|
76 | |
---|
77 | return average_time |
---|
78 | |
---|
79 | def get_freq(hop_freq,probe_level,absent_time): |
---|
80 | |
---|
81 | # Convert hop_freq to our unique channel list |
---|
82 | |
---|
83 | if hop_freq == 462562500: |
---|
84 | channel = 1 |
---|
85 | if hop_freq == 462712500: |
---|
86 | channel = 2 |
---|
87 | if hop_freq == 467562500: |
---|
88 | channel = 3 |
---|
89 | if hop_freq == 467712500: |
---|
90 | channel = 4 |
---|
91 | |
---|
92 | currentParameters = Parameter(1) |
---|
93 | currentParameters[0].name = "channel" |
---|
94 | currentParameters[0].value = channel |
---|
95 | |
---|
96 | o = Observable(2) |
---|
97 | o[0].value = probe_level |
---|
98 | o[0].name = "energy" |
---|
99 | |
---|
100 | o[1].value = absent_time |
---|
101 | o[1].name = "communication_time" |
---|
102 | |
---|
103 | # If time == 0 then we are scanning and we dont want to |
---|
104 | # use this time in the averaging process. |
---|
105 | |
---|
106 | if absent_time != 0: |
---|
107 | UpdateParameterPerformance(currentParameters,1,o,1) |
---|
108 | |
---|
109 | else: |
---|
110 | # Get the average communication time |
---|
111 | average_time = get_average_time(channel, absent_time) |
---|
112 | o[1].value = average_time |
---|
113 | o[1].name = "communication_time" |
---|
114 | UpdateParameterPerformance(currentParameters,1,o,2) |
---|
115 | |
---|
116 | p = Parameter(1) |
---|
117 | p = GetOptimalParameters(o,2,currentParameters,1); |
---|
118 | |
---|
119 | channel = get_real_channel(int(p[0].value)) |
---|
120 | |
---|
121 | if channel < 8: |
---|
122 | hop_freq = float(1e6 * (462.5625+(channel-1)*0.025))#setting the centre freq frequency for sending packets |
---|
123 | else: |
---|
124 | hop_freq = float(1e6 * (467.5625+(channel-8)*0.025))#setting the centre freq frequency for sending packets |
---|
125 | |
---|
126 | stats_array[int(p[0].value)] = stats_array[int(p[0].value)] + 1 |
---|
127 | return channel,hop_freq #returning the channel number and hop frequency |
---|
128 | |
---|
129 | |
---|
130 | def rx_callback(ok, payload): |
---|
131 | |
---|
132 | global n_rcvd, n_right,sync_status,mode,ch,traffic_flag |
---|
133 | ########################## sync #################################### |
---|
134 | if mode == "sync": |
---|
135 | if ok: |
---|
136 | (pktno,) = struct.unpack('!H', payload[0:2]) |
---|
137 | (sync_signal,) = struct.unpack('!s', payload[2]) |
---|
138 | (data_channel,) = struct.unpack('!H', payload[3:5]) |
---|
139 | |
---|
140 | if str(sync_signal) == 'o' and str(data_channel) == str(ch): |
---|
141 | |
---|
142 | sync_status = True |
---|
143 | #tb.stop() |
---|
144 | |
---|
145 | if str(sync_signal) == 's' and str(data_channel) == str(ch): |
---|
146 | |
---|
147 | sync_status = True |
---|
148 | data = 'o' |
---|
149 | pktno=0 |
---|
150 | ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data |
---|
151 | send_pkt(tb,ack_payload) #sending back the acknowledgement |
---|
152 | ################################################################### |
---|
153 | |
---|
154 | ######################### traffic ################################# |
---|
155 | if mode == "traffic": |
---|
156 | if ok: |
---|
157 | (data_header,) = struct.unpack('!s', payload[0]) |
---|
158 | if data_header == 'd': |
---|
159 | traffic_flag = True |
---|
160 | comm = struct.unpack('!14s', payload[1:15]) |
---|
161 | data = 'dI am fine.....' #Sending this message |
---|
162 | payload = struct.pack('!15s', data) |
---|
163 | send_pkt(tb,payload) |
---|
164 | |
---|
165 | ############################################################## |
---|
166 | |
---|
167 | n_rcvd += 1 |
---|
168 | if ok: |
---|
169 | n_right += 1 |
---|
170 | |
---|
171 | mods = modulation_utils.type_1_mods() |
---|
172 | demods = modulation_utils.type_1_demods() |
---|
173 | |
---|
174 | #setting up the tx options parser |
---|
175 | |
---|
176 | parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") |
---|
177 | parser_tx.add_option("", "--vtcross", action="store_true", default=False, |
---|
178 | help="Use the CROSS engine for DSA decisions: default is random.") |
---|
179 | |
---|
180 | parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), |
---|
181 | default='gmsk', |
---|
182 | help="Select modulation from: %s [default=%%default]" |
---|
183 | % (', '.join(mods.keys()),)) |
---|
184 | |
---|
185 | parser_tx.add_option("-s", "--size", type="eng_float", default=1500, |
---|
186 | help="set packet size [default=%default]") |
---|
187 | parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0, |
---|
188 | help="set megabytes to transmit [default=%default]") |
---|
189 | parser_tx.add_option("","--discontinuous", action="store_true", default=False, |
---|
190 | help="enable discontinous transmission (bursts of 5 packets)") |
---|
191 | parser_tx.add_option("","--from-file", default=None, |
---|
192 | help="use file for packet contents") |
---|
193 | |
---|
194 | expert_grp_tx = parser_tx.add_option_group("Expert_tx") |
---|
195 | |
---|
196 | transmit_path.add_options(parser_tx, expert_grp_tx) |
---|
197 | |
---|
198 | for mod in mods.values(): |
---|
199 | mod.add_options(expert_grp_tx) |
---|
200 | |
---|
201 | |
---|
202 | (options_tx, args_tx) = parser_tx.parse_args () |
---|
203 | |
---|
204 | if len(args_tx) != 0: |
---|
205 | parser_tx.print_help() |
---|
206 | sys.exit(1) |
---|
207 | |
---|
208 | ############# Setting some default values for tx side of the block |
---|
209 | options_tx.tx_freq = 462.5625e6 |
---|
210 | options_tx.samples_per_symbol = 2 |
---|
211 | options_tx.modulation = 'dbpsk' |
---|
212 | options_tx.fusb_block_size = 4096 |
---|
213 | options_tx.fusb_nblocks = 16 |
---|
214 | options_tx.bitrate = 0.0125e6 |
---|
215 | ############# |
---|
216 | |
---|
217 | if options_tx.tx_freq is None: |
---|
218 | sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") |
---|
219 | parser_tx.print_help(sys.stderr) |
---|
220 | sys.exit(1) |
---|
221 | |
---|
222 | parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") |
---|
223 | expert_grp_rx = parser_rx.add_option_group("Expert_rx") |
---|
224 | receive_path.add_options(parser_rx, expert_grp_rx) |
---|
225 | |
---|
226 | (options_rx, args_rx) = parser_rx.parse_args () |
---|
227 | |
---|
228 | ############# Setting some default values for rx side of the block |
---|
229 | options_rx.rx_freq = 462.5625e6 #setting default rx_freq value |
---|
230 | options_rx.samples_per_symbol = 2 |
---|
231 | options_rx.modulation = 'dbpsk' |
---|
232 | options_rx.fusb_block_size = 4096 |
---|
233 | options_rx.fusb_nblocks = 16 |
---|
234 | options_rx.bitrate = 0.0125e6 |
---|
235 | ############# |
---|
236 | |
---|
237 | # build the graph |
---|
238 | |
---|
239 | tb = my_top_block(mods[options_tx.modulation], |
---|
240 | demods[options_rx.modulation], |
---|
241 | rx_callback,options_tx, |
---|
242 | options_rx) |
---|
243 | r = gr.enable_realtime_scheduling() |
---|
244 | if r != gr.RT_OK: |
---|
245 | print "Warning: failed to enable realtime scheduling" |
---|
246 | |
---|
247 | tb.start() |
---|
248 | |
---|
249 | #listening to random frequencies untill a match is found |
---|
250 | running = True |
---|
251 | ch_energy = tb.rxpath.probe.level() #setting initial value |
---|
252 | hop_freq = options_tx.tx_freq # = options_rx.rx_freq...same for tx and rx side |
---|
253 | |
---|
254 | |
---|
255 | # Scan all channels first for inital data |
---|
256 | time.sleep(0.1) |
---|
257 | |
---|
258 | print "\n[[ Scanning channels for network nodes ]]\n" |
---|
259 | while running: |
---|
260 | |
---|
261 | ################################################sync mode#################################### |
---|
262 | if mode == "sync": |
---|
263 | if sync_status != True: |
---|
264 | |
---|
265 | if return_flag == 0: |
---|
266 | ch,hop_freq = get_freq(hop_freq,ch_energy,0) |
---|
267 | else: |
---|
268 | ch,hop_freq = get_freq(hop_freq,ch_energy,elapsed_time) |
---|
269 | return_flag = 0 |
---|
270 | |
---|
271 | tb.txpath.set_freq(hop_freq) |
---|
272 | tb.rxpath.set_freq(hop_freq) |
---|
273 | |
---|
274 | ch_energy = tb.rxpath.probe.level() #check if primary user is present |
---|
275 | |
---|
276 | if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel |
---|
277 | continue |
---|
278 | |
---|
279 | nbytes = 5 #int(1e6 * .0003) |
---|
280 | pkt_size = 5 |
---|
281 | n = 0 |
---|
282 | pktno = 0 |
---|
283 | while n < nbytes: |
---|
284 | if options_tx.from_file is None: |
---|
285 | data = 's' |
---|
286 | else: |
---|
287 | data = source_file.read(pkt_size - 2) |
---|
288 | if data == '': |
---|
289 | break; |
---|
290 | |
---|
291 | payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data |
---|
292 | |
---|
293 | send_pkt(tb,payload) |
---|
294 | n += len(payload) |
---|
295 | sys.stderr.write('.') |
---|
296 | if options_tx.discontinuous and pktno % 5 == 4: |
---|
297 | time.sleep(1) |
---|
298 | pktno += 1 |
---|
299 | time.sleep(0.1) |
---|
300 | |
---|
301 | else: |
---|
302 | print "\n\n[[ Network Node Found: Commencing communications on CHANNEL ", ch, " ]]\n"; |
---|
303 | n_attempts_counter = 0 |
---|
304 | mode = "traffic" |
---|
305 | traffic_flag = False |
---|
306 | sync_status="False" |
---|
307 | start_time = datetime.now() #measuring the time for which the primary user is away |
---|
308 | |
---|
309 | ################################################end of sync mode#################################### |
---|
310 | |
---|
311 | ################################################Communications mode################################# |
---|
312 | if mode == "traffic": |
---|
313 | |
---|
314 | nbytes = 15 |
---|
315 | pkt_size = 15 |
---|
316 | data_pktno = 0 |
---|
317 | n = 0 |
---|
318 | while n < nbytes: |
---|
319 | |
---|
320 | if options_tx.from_file is None: |
---|
321 | data = 'dHi how are you' #Sending this message |
---|
322 | |
---|
323 | else: |
---|
324 | data = source_file.read(pkt_size - 2) |
---|
325 | if data == '': |
---|
326 | break; |
---|
327 | |
---|
328 | |
---|
329 | payload = struct.pack('!15s', data) |
---|
330 | |
---|
331 | send_pkt(tb,payload) |
---|
332 | n += len(payload) |
---|
333 | sys.stderr.write('.') |
---|
334 | if options_tx.discontinuous and data_pktno % 5 == 4: |
---|
335 | time.sleep(1) |
---|
336 | data_pktno += 1 |
---|
337 | time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3]))) |
---|
338 | |
---|
339 | if traffic_flag != True: |
---|
340 | n_attempts_counter += 1 |
---|
341 | if n_attempts_counter > n_attempts: #get out of the data channel as it seems that the other node is still trying to rendezvous |
---|
342 | mode = "sync" |
---|
343 | continue |
---|
344 | |
---|
345 | ch_energy = tb.rxpath.probe.level() #check if primary user is present |
---|
346 | |
---|
347 | if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel |
---|
348 | stop_time = datetime.now() |
---|
349 | _elapsed_time = start_time - stop_time |
---|
350 | elapsed_time = _elapsed_time.seconds |
---|
351 | print "\n[[ Primary User Detected: Evacuating Current Channel ]]\n" |
---|
352 | print "\n[[ Scanning channels for network nodes ]]\n" |
---|
353 | mode = "sync" |
---|
354 | return_flag = 1 |
---|
355 | |
---|
356 | |
---|
357 | if __name__ == '__main__': |
---|
358 | try: |
---|
359 | main() |
---|
360 | except KeyboardInterrupt: |
---|
361 | pass |
---|
362 | |
---|
363 | |
---|
364 | |
---|
365 | |
---|
366 | |
---|