GNU Radio 3.5.3.2 C++ API
gr_packet_sink.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2005 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_PACKET_SINK_H
00024 #define INCLUDED_GR_PACKET_SINK_H
00025 
00026 #include <gr_core_api.h>
00027 #include <gr_sync_block.h>
00028 #include <gr_msg_queue.h>
00029 
00030 class gr_packet_sink;
00031 typedef boost::shared_ptr<gr_packet_sink> gr_packet_sink_sptr;
00032 
00033 GR_CORE_API gr_packet_sink_sptr 
00034 gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
00035                      gr_msg_queue_sptr target_queue,
00036                      int threshold = -1                 // -1 -> use default
00037                      );
00038 /*!
00039  * \brief process received  bits looking for packet sync, header, and process bits into packet
00040  * \ingroup sink_blk
00041  */
00042 class GR_CORE_API gr_packet_sink : public gr_sync_block
00043 {
00044   friend GR_CORE_API gr_packet_sink_sptr 
00045   gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
00046                        gr_msg_queue_sptr target_queue,
00047                        int threshold);
00048 
00049  private:
00050   enum state_t {STATE_SYNC_SEARCH, STATE_HAVE_SYNC, STATE_HAVE_HEADER};
00051 
00052   static const int MAX_PKT_LEN    = 4096;
00053   static const int HEADERBITLEN   = 32;
00054 
00055   gr_msg_queue_sptr  d_target_queue;            // where to send the packet when received
00056   unsigned long long d_sync_vector;             // access code to locate start of packet
00057   unsigned int       d_threshold;               // how many bits may be wrong in sync vector
00058 
00059   state_t            d_state;
00060 
00061   unsigned long long d_shift_reg;               // used to look for sync_vector
00062 
00063   unsigned int       d_header;                  // header bits
00064   int                d_headerbitlen_cnt;        // how many so far
00065 
00066   unsigned char      d_packet[MAX_PKT_LEN];     // assembled payload
00067   unsigned char      d_packet_byte;             // byte being assembled
00068   int                d_packet_byte_index;       // which bit of d_packet_byte we're working on
00069   int                d_packetlen;               // length of packet
00070   int                d_packetlen_cnt;           // how many so far
00071 
00072  protected:
00073   gr_packet_sink(const std::vector<unsigned char>& sync_vector, 
00074                  gr_msg_queue_sptr target_queue,
00075                  int threshold);
00076 
00077   void enter_search();
00078   void enter_have_sync();
00079   void enter_have_header(int payload_len);
00080   
00081   int slice(float x) { return x > 0 ? 1 : 0; }
00082 
00083   bool header_ok()
00084   {
00085     // confirm that two copies of header info are identical
00086     return ((d_header >> 16) ^ (d_header & 0xffff)) == 0;
00087   }
00088 
00089   int header_payload_len()
00090   {
00091     // header consists of two 16-bit shorts in network byte order
00092     int t = (d_header >> 16) & 0xffff;
00093     return t;
00094   }
00095 
00096  public:
00097   ~gr_packet_sink();
00098 
00099   int work(int noutput_items,
00100            gr_vector_const_void_star &input_items,
00101            gr_vector_void_star &output_items);
00102 
00103 
00104   //! return true if we detect carrier
00105   bool carrier_sensed() const
00106   {
00107     return d_state != STATE_SYNC_SEARCH;
00108   }
00109 
00110 };
00111 
00112 #endif /* INCLUDED_GR_PACKET_SINK_H */