root / gnuradio-core / src / lib / general / gr_packet_sink.h @ f914499f
History | View | Annotate | Download (3.3 kB)
| 1 | /* -*- c++ -*- */
|
|---|---|
| 2 | /*
|
| 3 | * Copyright 2005 Free Software Foundation, Inc. |
| 4 | * |
| 5 | * This file is part of GNU Radio |
| 6 | * |
| 7 | * GNU Radio is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * GNU Radio is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Radio; see the file COPYING. If not, write to |
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef INCLUDED_GR_PACKET_SINK_H
|
| 24 | #define INCLUDED_GR_PACKET_SINK_H
|
| 25 | |
| 26 | #include <gr_core_api.h> |
| 27 | #include <gr_sync_block.h> |
| 28 | #include <gr_msg_queue.h> |
| 29 | |
| 30 | class gr_packet_sink; |
| 31 | typedef boost::shared_ptr<gr_packet_sink> gr_packet_sink_sptr;
|
| 32 | |
| 33 | GR_CORE_API gr_packet_sink_sptr |
| 34 | gr_make_packet_sink (const std::vector<unsigned char>& sync_vector, |
| 35 | gr_msg_queue_sptr target_queue, |
| 36 | int threshold = -1 // -1 -> use default |
| 37 | ); |
| 38 | /*!
|
| 39 | * \brief process received bits looking for packet sync, header, and process bits into packet |
| 40 | * \ingroup sink_blk |
| 41 | */ |
| 42 | class GR_CORE_API gr_packet_sink : public gr_sync_block |
| 43 | {
|
| 44 | friend GR_CORE_API gr_packet_sink_sptr |
| 45 | gr_make_packet_sink (const std::vector<unsigned char>& sync_vector, |
| 46 | gr_msg_queue_sptr target_queue, |
| 47 | int threshold);
|
| 48 | |
| 49 | private:
|
| 50 | enum state_t {STATE_SYNC_SEARCH, STATE_HAVE_SYNC, STATE_HAVE_HEADER};
|
| 51 | |
| 52 | static const int MAX_PKT_LEN = 4096; |
| 53 | static const int HEADERBITLEN = 32; |
| 54 | |
| 55 | gr_msg_queue_sptr d_target_queue; // where to send the packet when received
|
| 56 | unsigned long long d_sync_vector; // access code to locate start of packet |
| 57 | unsigned int d_threshold; // how many bits may be wrong in sync vector |
| 58 | |
| 59 | state_t d_state; |
| 60 | |
| 61 | unsigned long long d_shift_reg; // used to look for sync_vector |
| 62 | |
| 63 | unsigned int d_header; // header bits |
| 64 | int d_headerbitlen_cnt; // how many so far |
| 65 | |
| 66 | unsigned char d_packet[MAX_PKT_LEN]; // assembled payload |
| 67 | unsigned char d_packet_byte; // byte being assembled |
| 68 | int d_packet_byte_index; // which bit of d_packet_byte we're working on |
| 69 | int d_packetlen; // length of packet |
| 70 | int d_packetlen_cnt; // how many so far |
| 71 | |
| 72 | protected:
|
| 73 | gr_packet_sink(const std::vector<unsigned char>& sync_vector, |
| 74 | gr_msg_queue_sptr target_queue, |
| 75 | int threshold);
|
| 76 | |
| 77 | void enter_search();
|
| 78 | void enter_have_sync();
|
| 79 | void enter_have_header(int payload_len); |
| 80 | |
| 81 | int slice(float x) { return x > 0 ? 1 : 0; } |
| 82 | |
| 83 | bool header_ok()
|
| 84 | {
|
| 85 | // confirm that two copies of header info are identical
|
| 86 | return ((d_header >> 16) ^ (d_header & 0xffff)) == 0; |
| 87 | } |
| 88 | |
| 89 | int header_payload_len()
|
| 90 | {
|
| 91 | // header consists of two 16-bit shorts in network byte order
|
| 92 | int t = (d_header >> 16) & 0xffff; |
| 93 | return t;
|
| 94 | } |
| 95 | |
| 96 | public:
|
| 97 | ~gr_packet_sink(); |
| 98 | |
| 99 | int work(int noutput_items, |
| 100 | gr_vector_const_void_star &input_items, |
| 101 | gr_vector_void_star &output_items); |
| 102 | |
| 103 | |
| 104 | //! return true if we detect carrier
|
| 105 | bool carrier_sensed() const |
| 106 | {
|
| 107 | return d_state != STATE_SYNC_SEARCH;
|
| 108 | } |
| 109 | |
| 110 | }; |
| 111 | |
| 112 | #endif /* INCLUDED_GR_PACKET_SINK_H */ |