From c4e072e8a58800f2dfd6c1e5e95009dd783061be Mon Sep 17 00:00:00 2001 From: jcorgan Date: Tue, 6 Feb 2007 00:10:32 +0000 Subject: [PATCH] Merged r4354:4390 from developer branch jcorgan/digital into trunk. git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@4391 221aa14e-8319-0410-a670-987f0aec2ac5 --- .../src/lib/general/gr_framer_sink_1.cc | 17 ++-- .../src/lib/general/gr_framer_sink_1.h | 11 ++- .../src/python/gnuradio/blksimpl/pkt.py | 17 ++-- .../src/python/gnuradio/packet_utils.py | 31 ++++--- gnuradio-examples/python/digital/Makefile.am | 1 + gnuradio-examples/python/digital/README | 7 ++ .../python/digital/benchmark_tx.py | 18 +++- .../python/digital/run_length.py | 83 +++++++++++++++++++ .../python/digital/transmit_path.py | 8 +- 9 files changed, 160 insertions(+), 33 deletions(-) create mode 100755 gnuradio-examples/python/digital/run_length.py diff --git a/gnuradio-core/src/lib/general/gr_framer_sink_1.cc b/gnuradio-core/src/lib/general/gr_framer_sink_1.cc index d6f87df1..1d939adb 100644 --- a/gnuradio-core/src/lib/general/gr_framer_sink_1.cc +++ b/gnuradio-core/src/lib/general/gr_framer_sink_1.cc @@ -52,13 +52,14 @@ gr_framer_sink_1::enter_have_sync() } inline void -gr_framer_sink_1::enter_have_header(int payload_len) +gr_framer_sink_1::enter_have_header(int payload_len, int whitener_offset) { if (VERBOSE) - fprintf(stderr, "@ enter_have_header (payload_len = %d)\n", payload_len); + fprintf(stderr, "@ enter_have_header (payload_len = %d) (offset = %d)\n", payload_len, whitener_offset); d_state = STATE_HAVE_HEADER; d_packetlen = payload_len; + d_packet_whitener_offset = whitener_offset; d_packetlen_cnt = 0; d_packet_byte = 0; d_packet_byte_index = 0; @@ -125,11 +126,10 @@ gr_framer_sink_1::work (int noutput_items, // we have a full header, check to see if it has been received properly if (header_ok()){ - int payload_len = header_payload_len(); - if (payload_len <= MAX_PKT_LEN) // reasonable? - enter_have_header(payload_len); // yes. - else - enter_search(); // no. + int payload_len; + int payload_offset; + header_payload(&payload_len, &payload_offset); + enter_have_header(payload_len, payload_offset); } else enter_search(); // no. @@ -151,7 +151,8 @@ gr_framer_sink_1::work (int noutput_items, if (d_packetlen_cnt == d_packetlen){ // packet is filled // build a message - gr_message_sptr msg = gr_make_message(0, 0, 0, d_packetlen_cnt); + // NOTE: passing header field as arg1 is not scalable + gr_message_sptr msg = gr_make_message(0, d_packet_whitener_offset, 0, d_packetlen_cnt); memcpy(msg->msg(), d_packet, d_packetlen_cnt); d_target_queue->insert_tail(msg); // send it diff --git a/gnuradio-core/src/lib/general/gr_framer_sink_1.h b/gnuradio-core/src/lib/general/gr_framer_sink_1.h index dab34675..ba50184b 100644 --- a/gnuradio-core/src/lib/general/gr_framer_sink_1.h +++ b/gnuradio-core/src/lib/general/gr_framer_sink_1.h @@ -70,6 +70,7 @@ class gr_framer_sink_1 : public gr_sync_block unsigned char d_packet_byte; // byte being assembled int d_packet_byte_index; // which bit of d_packet_byte we're working on int d_packetlen; // length of packet + int d_packet_whitener_offset; // offset into whitener string to use int d_packetlen_cnt; // how many so far protected: @@ -77,7 +78,7 @@ class gr_framer_sink_1 : public gr_sync_block void enter_search(); void enter_have_sync(); - void enter_have_header(int payload_len); + void enter_have_header(int payload_len, int whitener_offset); bool header_ok() { @@ -85,11 +86,13 @@ class gr_framer_sink_1 : public gr_sync_block return ((d_header >> 16) ^ (d_header & 0xffff)) == 0; } - int header_payload_len() + void header_payload(int *len, int *offset) { // header consists of two 16-bit shorts in network byte order - int t = (d_header >> 16) & 0xffff; - return t; + // payload length is lower 12 bits + // whitener offset is upper 4 bits + *len = (d_header >> 16) & 0x0fff; + *offset = (d_header >> 28) & 0x000f; } public: diff --git a/gnuradio-core/src/python/gnuradio/blksimpl/pkt.py b/gnuradio-core/src/python/gnuradio/blksimpl/pkt.py index a1ece893..96b48657 100644 --- a/gnuradio-core/src/python/gnuradio/blksimpl/pkt.py +++ b/gnuradio-core/src/python/gnuradio/blksimpl/pkt.py @@ -36,7 +36,7 @@ class mod_pkts(gr.hier_block): Send packets by calling send_pkt """ - def __init__(self, fg, modulator, access_code=None, msgq_limit=2, pad_for_usrp=True): + def __init__(self, fg, modulator, access_code=None, msgq_limit=2, pad_for_usrp=True, use_whitener_offset=False): """ Hierarchical block for sending packets @@ -52,12 +52,15 @@ class mod_pkts(gr.hier_block): @param msgq_limit: maximum number of messages in message queue @type msgq_limit: int @param pad_for_usrp: If true, packets are padded such that they end up a multiple of 128 samples - + @param use_whitener_offset: If true, start of whitener XOR string is incremented each packet + See gmsk_mod for remaining parameters """ self._modulator = modulator self._pad_for_usrp = pad_for_usrp - + self._use_whitener_offset = use_whitener_offset + self._whitener_offset = 0 + if access_code is None: access_code = packet_utils.default_access_code if not packet_utils.is_1_0_string(access_code): @@ -84,9 +87,13 @@ class mod_pkts(gr.hier_block): self._modulator.samples_per_symbol(), self._modulator.bits_per_symbol(), self._access_code, - self._pad_for_usrp) + self._pad_for_usrp, + self._whitener_offset) #print "pkt =", string_to_hex_list(pkt) msg = gr.message_from_string(pkt) + if self._use_whitener_offset is True: + self._whitener_offset = (self._whitener_offset + 1) % 16 + self._pkt_input.msgq().insert_tail(msg) @@ -151,6 +158,6 @@ class _queue_watcher_thread(_threading.Thread): def run(self): while self.keep_running: msg = self.rcvd_pktq.delete_head() - ok, payload = packet_utils.unmake_packet(msg.to_string()) + ok, payload = packet_utils.unmake_packet(msg.to_string(), int(msg.arg1())) if self.callback: self.callback(ok, payload) diff --git a/gnuradio-core/src/python/gnuradio/packet_utils.py b/gnuradio-core/src/python/gnuradio/packet_utils.py index 59b13536..182c80cd 100644 --- a/gnuradio-core/src/python/gnuradio/packet_utils.py +++ b/gnuradio-core/src/python/gnuradio/packet_utils.py @@ -86,22 +86,26 @@ def string_to_hex_list(s): return map(lambda x: hex(ord(x)), s) -def whiten(s): +def whiten(s, o): sa = Numeric.fromstring(s, Numeric.UnsignedInt8) - z = sa ^ random_mask_vec8[0:len(sa)] + z = sa ^ random_mask_vec8[o:len(sa)+o] return z.tostring() -def dewhiten(s): - return whiten(s) # self inverse +def dewhiten(s, o): + return whiten(s, o) # self inverse -def make_header(payload_len): - return struct.pack('!HH', payload_len, payload_len) +def make_header(payload_len, whitener_offset=0): + # Upper nibble is offset, lower 12 bits is len + val = ((whitener_offset & 0xf) << 12) | (payload_len & 0x0fff) + #print "offset =", whitener_offset, " len =", payload_len, " val=", val + return struct.pack('!HH', val, val) def make_packet(payload, samples_per_symbol, bits_per_symbol, - access_code=default_access_code, pad_for_usrp=True): + access_code=default_access_code, pad_for_usrp=True, + whitener_offset=0): """ - Build a packet, given access code and payload. + Build a packet, given access code, payload, and whitener offset @param payload: packet payload, len [0, 4096] @param samples_per_symbol: samples per symbol (needed for padding calculation) @@ -109,6 +113,7 @@ def make_packet(payload, samples_per_symbol, bits_per_symbol, @param bits_per_symbol: (needed for padding calculation) @type bits_per_symbol: int @param access_code: string of ascii 0's and 1's + @param whitener_offset offset into whitener string to use [0-16) Packet will have access code at the beginning, followed by length, payload and finally CRC-32. @@ -116,6 +121,9 @@ def make_packet(payload, samples_per_symbol, bits_per_symbol, if not is_1_0_string(access_code): raise ValueError, "access_code must be a string containing only 0's and 1's (%r)" % (access_code,) + if not whitener_offset >=0 and whitener_offset < 16: + raise ValueError, "whitener_offset must be between 0 and 15, inclusive (%i)" % (whitener_offset,) + (packed_access_code, padded) = conv_1_0_string_to_packed_binary_string(access_code) (packed_preamble, ignore) = conv_1_0_string_to_packed_binary_string(preamble) @@ -127,7 +135,8 @@ def make_packet(payload, samples_per_symbol, bits_per_symbol, if L > MAXLEN: raise ValueError, "len(payload) must be in [0, %d]" % (MAXLEN,) - pkt = ''.join((packed_preamble, packed_access_code, make_header(L), whiten(payload_with_crc), '\x55')) + pkt = ''.join((packed_preamble, packed_access_code, make_header(L, whitener_offset), + whiten(payload_with_crc, whitener_offset), '\x55')) if pad_for_usrp: pkt = pkt + (_npadding_bytes(len(pkt), samples_per_symbol, bits_per_symbol) * '\x55') @@ -156,13 +165,13 @@ def _npadding_bytes(pkt_byte_len, samples_per_symbol, bits_per_symbol): return byte_modulus - r -def unmake_packet(whitened_payload_with_crc): +def unmake_packet(whitened_payload_with_crc, whitener_offset=0): """ Return (ok, payload) @param whitened_payload_with_crc: string """ - payload_with_crc = dewhiten(whitened_payload_with_crc) + payload_with_crc = dewhiten(whitened_payload_with_crc, whitener_offset) ok, payload = gru.check_crc32(payload_with_crc) if 0: diff --git a/gnuradio-examples/python/digital/Makefile.am b/gnuradio-examples/python/digital/Makefile.am index ad2fa4d6..93f1bafe 100644 --- a/gnuradio-examples/python/digital/Makefile.am +++ b/gnuradio-examples/python/digital/Makefile.am @@ -28,6 +28,7 @@ EXTRA_DIST = \ pick_bitrate.py \ receive_path.py \ rx_voice.py \ + run_length.py \ transmit_path.py \ tunnel.py \ tx_voice.py diff --git a/gnuradio-examples/python/digital/README b/gnuradio-examples/python/digital/README index 61801f73..5382498f 100644 --- a/gnuradio-examples/python/digital/README +++ b/gnuradio-examples/python/digital/README @@ -75,3 +75,10 @@ Likewise, on machine B: This now uses a carrier sense MAC, so you should be able to ssh between the machines, web browse, etc. + +* run_length.py: This program takes a single argument '-f FILE' and +outputs the number of runs of similar bits within the file. It is +useful as a diagnostic tool when experimenting with line coding or +whitening algorithms. + + diff --git a/gnuradio-examples/python/digital/benchmark_tx.py b/gnuradio-examples/python/digital/benchmark_tx.py index 55f30932..27534a69 100755 --- a/gnuradio-examples/python/digital/benchmark_tx.py +++ b/gnuradio-examples/python/digital/benchmark_tx.py @@ -71,6 +71,8 @@ def main(): help="set megabytes to transmit [default=%default]") parser.add_option("","--discontinuous", action="store_true", default=False, help="enable discontinous transmission (bursts of 5 packets)") + parser.add_option("","--from-file", default=None, + help="use file for packet contents") transmit_path.add_options(parser, expert_grp) @@ -89,6 +91,9 @@ def main(): parser.print_help(sys.stderr) sys.exit(1) + if options.from_file is not None: + source_file = open(options.from_file, 'r') + # build the graph fg = my_graph(mods[options.modulation], options) @@ -98,7 +103,6 @@ def main(): fg.start() # start flow graph - # generate and send packets nbytes = int(1e6 * options.megabytes) n = 0 @@ -106,8 +110,16 @@ def main(): pkt_size = int(options.size) while n < nbytes: - send_pkt(struct.pack('!H', pktno) + (pkt_size - 2) * chr(pktno & 0xff)) - n += pkt_size + if options.from_file is None: + data = (pkt_size - 2) * chr(pktno & 0xff) + else: + data = source_file.read(pkt_size - 2) + if data == '': + break; + + payload = struct.pack('!H', pktno) + data + send_pkt(payload) + n += len(payload) sys.stderr.write('.') if options.discontinuous and pktno % 5 == 4: time.sleep(1) diff --git a/gnuradio-examples/python/digital/run_length.py b/gnuradio-examples/python/digital/run_length.py new file mode 100755 index 00000000..dcf86e8c --- /dev/null +++ b/gnuradio-examples/python/digital/run_length.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# +# Copyright 2007 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from optparse import OptionParser +import sys + +def main(): + parser = OptionParser() + parser.add_option("-f", "--file", default=None, + help="Choose file to read data from.") + (options, args) = parser.parse_args() + + if options.file == None: + print "Must specify file to read from using '-f'." + sys.exit(1) + print "Using", options.file, "for data." + + f = open(options.file, 'r') + runs = [] + count = 0 + current = 0 + bytes = 0 + bits = 0 + + for ch in f.read(): + x = ord(ch) + bytes = bytes + 1 + for i in range(7,-1,-1): + bits = bits + 1 + t = (x >> i) & 0x1 + if t == current: + count = count + 1 + else: + if count > 0: + if len(runs) < count: + for j in range(count - len(runs)): + runs.append(0); + runs[count-1] = runs[count-1] + 1 + + current = 1-current; + count = 1 + + # Deal with last run at EOF + if len(runs) < count and count > 0: + for j in range(count - len(runs)): + runs.append(0); + runs[count-1] = runs[count-1] + 1 + + chk = 0 + print "Bytes read: ", bytes + print "Bits read: ", bits + print + for i in range(len(runs)): + chk = chk + runs[i]*(i+1) + print "Runs of length", i+1, ":", runs[i] + print + print "Sum of runs:", chk, "bits" + print + print "Maximum run length is", len(runs), "bits" + +if __name__ == "__main__": + main() + + diff --git a/gnuradio-examples/python/digital/transmit_path.py b/gnuradio-examples/python/digital/transmit_path.py index 5c401736..753f5999 100644 --- a/gnuradio-examples/python/digital/transmit_path.py +++ b/gnuradio-examples/python/digital/transmit_path.py @@ -50,7 +50,8 @@ class transmit_path(gr.hier_block): self._samples_per_symbol = options.samples_per_symbol # desired samples/baud self._fusb_block_size = options.fusb_block_size # usb info for USRP self._fusb_nblocks = options.fusb_nblocks # usb info for USRP - + self._use_whitener_offset = options.use_whitener_offset # increment start of whitener XOR data + self._modulator_class = modulator_class # the modulator_class we are using if self._tx_freq is None: @@ -80,7 +81,8 @@ class transmit_path(gr.hier_block): self._modulator_class(fg, **mod_kwargs), access_code=None, msgq_limit=4, - pad_for_usrp=True) + pad_for_usrp=True, + use_whitener_offset=options.use_whitener_offset) # Set the USRP for maximum transmit gain @@ -200,6 +202,8 @@ class transmit_path(gr.hier_block): help="set fpga interpolation rate to INTERP [default=%default]") expert.add_option("", "--log", action="store_true", default=False, help="Log all parts of flow graph to file (CAUTION: lots of data)") + expert.add_option("","--use-whitener-offset", action="store_true", default=False, + help="make sequential packets use different whitening") # Make a static method to call before instantiation add_options = staticmethod(add_options) -- 2.30.2