3 * Copyright 2005 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
23 #ifndef INCLUDED_GR_PACKET_SINK_H
24 #define INCLUDED_GR_PACKET_SINK_H
26 #include <gr_sync_block.h>
27 #include <gr_msg_queue.h>
30 typedef boost::shared_ptr<gr_packet_sink> gr_packet_sink_sptr;
33 gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
34 gr_msg_queue_sptr target_queue,
35 int threshold = -1 // -1 -> use default
38 * \brief process received bits looking for packet sync, header, and process bits into packet
41 class gr_packet_sink : public gr_sync_block
43 friend gr_packet_sink_sptr
44 gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
45 gr_msg_queue_sptr target_queue,
49 enum state_t {STATE_SYNC_SEARCH, STATE_HAVE_SYNC, STATE_HAVE_HEADER};
51 static const int MAX_PKT_LEN = 4096;
52 static const int HEADERBITLEN = 32;
54 gr_msg_queue_sptr d_target_queue; // where to send the packet when received
55 unsigned long long d_sync_vector; // access code to locate start of packet
56 unsigned int d_threshold; // how many bits may be wrong in sync vector
60 unsigned long long d_shift_reg; // used to look for sync_vector
62 unsigned int d_header; // header bits
63 int d_headerbitlen_cnt; // how many so far
65 unsigned char d_packet[MAX_PKT_LEN]; // assembled payload
66 unsigned char d_packet_byte; // byte being assembled
67 int d_packet_byte_index; // which bit of d_packet_byte we're working on
68 int d_packetlen; // length of packet
69 int d_packetlen_cnt; // how many so far
72 gr_packet_sink(const std::vector<unsigned char>& sync_vector,
73 gr_msg_queue_sptr target_queue,
77 void enter_have_sync();
78 void enter_have_header(int payload_len);
80 int slice(float x) { return x > 0 ? 1 : 0; }
84 // confirm that two copies of header info are identical
85 return ((d_header >> 16) ^ (d_header & 0xffff)) == 0;
88 int header_payload_len()
90 // header consists of two 16-bit shorts in network byte order
91 int t = (d_header >> 16) & 0xffff;
98 int work(int noutput_items,
99 gr_vector_const_void_star &input_items,
100 gr_vector_void_star &output_items);
103 //! return true if we detect carrier
104 bool carrier_sensed() const
106 return d_state != STATE_SYNC_SEARCH;
111 #endif /* INCLUDED_GR_PACKET_SINK_H */