Merged r4354:4390 from developer branch jcorgan/digital into trunk.
authorjcorgan <jcorgan@221aa14e-8319-0410-a670-987f0aec2ac5>
Tue, 6 Feb 2007 00:10:32 +0000 (00:10 +0000)
committerjcorgan <jcorgan@221aa14e-8319-0410-a670-987f0aec2ac5>
Tue, 6 Feb 2007 00:10:32 +0000 (00:10 +0000)
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@4391 221aa14e-8319-0410-a670-987f0aec2ac5

gnuradio-core/src/lib/general/gr_framer_sink_1.cc
gnuradio-core/src/lib/general/gr_framer_sink_1.h
gnuradio-core/src/python/gnuradio/blksimpl/pkt.py
gnuradio-core/src/python/gnuradio/packet_utils.py
gnuradio-examples/python/digital/Makefile.am
gnuradio-examples/python/digital/README
gnuradio-examples/python/digital/benchmark_tx.py
gnuradio-examples/python/digital/run_length.py [new file with mode: 0755]
gnuradio-examples/python/digital/transmit_path.py

index d6f87df1e71e5808d36adb335d4d350f0e507d42..1d939adbddfb4107b186a425467d048b66f3a6c0 100644 (file)
@@ -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
index dab34675509ab39dacd2479bd448ab59de46becb..ba50184b88580876c5c0e339303c1c5be8e02147 100644 (file)
@@ -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:
index a1ece893dfe5cb51a4cb03a727c307a52665869b..96b48657f684886c08fa1c9fa0847a25aa47f981 100644 (file)
@@ -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)
index 59b1353662086cff8c7e12fe4b642afeb3cca252..182c80cdc85de794a248aecf16383f16c4f2d3a9 100644 (file)
@@ -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:
index ad2fa4d688b8321882ec4e8eaa9dbe9022947968..93f1bafea822817a57ae628b68c76015671c8810 100644 (file)
@@ -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             
index 61801f73d1a18fa40a77922233e0304f50a0e4a4..5382498f55f91d65262c57a82cdc0267ea923d94 100644 (file)
@@ -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.
+
+
index 55f309322e891ae276596c279adb4577b256f766..27534a69ae577c18b905cd9e48f67f7b5c0f91ad 100755 (executable)
@@ -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 (executable)
index 0000000..dcf86e8
--- /dev/null
@@ -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()
+
+    
index 5c4017362b7100cd24e779502a54825b031b51e3..753f59999a507a8bd9297d3e62528b2244818d76 100644 (file)
@@ -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)