diff --git a/lib/packet_pad_impl.cc b/lib/packet_pad_impl.cc index a499eb3..936f888 100644 --- a/lib/packet_pad_impl.cc +++ b/lib/packet_pad_impl.cc @@ -26,9 +26,11 @@ using namespace gr::foo; packet_pad_impl::packet_pad_impl(bool debug, unsigned int pad_front, unsigned int pad_tail) : block("packet_pad", gr::io_signature::make(1, 1, sizeof(gr_complex)), gr::io_signature::make(1, 1, sizeof(gr_complex))), - d_debug(debug), d_pad_front(pad_front), - d_pad_tail(pad_tail), d_front_left(0), - d_tail_left(0), d_frame_left(0), d_state(IDLE) { + d_debug(debug), + d_pad_front(pad_front), + d_pad_tail(pad_tail), + d_pad(0), + d_eob(false) { set_relative_rate(1); set_tag_propagation_policy(block::TPP_DONT); @@ -62,146 +64,101 @@ packet_pad_impl::general_work (int noutput_items, gr_vector_int& ninput_items, gr_vector_const_void_star& input_items, gr_vector_void_star& output_items) { - int ninput = ninput_items[0]; - int noutput = noutput_items; - // these 4 values are updated in each iteration const gr_complex *in = (const gr_complex*)input_items[0]; gr_complex *out = (gr_complex*)output_items[0]; - int written = 0; // written symbols - int consumed = 0; // read symbols + int ninput = ninput_items[0]; + int noutput = noutput_items; - // tag handling + dout << "call: pad " << d_pad << " ninput " << ninput << " noutput " << noutput << std::endl; + // insert zeros + if(d_pad) { + int n = std::min(d_pad, noutput); + std::memset(out, 0, n * sizeof(gr_complex)); + d_pad -= n; + + dout << "padded zeros: " << n << std::endl; + + // add end of burst tag + if(!d_pad && d_eob) { + d_eob = false; + add_eob(nitems_written(0) + n - 1); + } + return n; + } + + // search for tags const uint64_t nread = this->nitems_read(0); std::vector tags; + get_tags_in_range(tags, 0, nread, nread + ninput); + std::sort(tags.begin(), tags.end(), tag_t::offset_compare); - dout << "PACKET PAD: input size: " << ninput_items[0] << " output size: " - << noutput_items << std::endl; + uint64_t n = std::min(ninput, noutput); - while(written < noutput) { + if(tags.size()) { + tag_t t = tags[0]; - dout << "PACKET PAD: iteration - consumed: " << consumed << - " written: " << written << std::endl; + dout << "found tag: " << pmt::symbol_to_string(t.key) << std::endl; - switch(d_state) { + uint64_t read = nitems_read(0); + if(t.offset != read) { + dout << "tag does not start at current offset" << std::endl; + n = std::min(n, t.offset - read); - case IDLE: + } else { + if(pmt::equal(t.key, pmt::mp("tx_sob"))) { + dout << "tx_sob tag" << std::endl; + add_sob(nitems_written(0)); + d_pad = d_pad_front; + remove_item_tag(0, t); + return 0; - //assert(consumed < ninput); - if(consumed >= ninput) { - goto out; + + } else if(pmt::equal(t.key, pmt::mp("tx_eob"))) { + dout << "tx_eob tag" << std::endl; + d_pad = d_pad_tail; + d_eob = true; + if(n) { + + if(!d_pad) { + add_eob(nitems_written(0)); + d_eob = false; + } + memcpy(out, in, sizeof(gr_complex)); + consume(0, 1); + return 1; + } + return 0; + + } else { + dout << "unknown tag" << std::endl; + if(tags.size() > 1) { + n = std::min(n, tags[1].offset - read); + } } - - get_tags_in_range(tags, 0, nread + consumed, - nread + (consumed + 1) - 1, - pmt::string_to_symbol("ofdm_start")); - - assert(tags.size() == 1); - - d_front_left = d_pad_front; - d_frame_left = pmt::to_uint64(tags[0].value); - d_tail_left = d_pad_tail; - - add_sob(this->nitems_written(0) + written); - d_state = FRONT; - - dout << "PACKET PAD: new frame - length: " << d_frame_left << std::endl; - - break; - - case FRONT: - - if(!((ninput >= d_frame_left) || (ninput == 102))) { - goto out; - } - - if(!d_front_left) { - d_state = FRAME; - break; - } - - dout << "PACKET PAD: pad front" << std::endl; - - memset(out, 0, sizeof(gr_complex)); - - d_front_left--; - out++; - written++; - - break; - - case FRAME: - - //assert((consumed + 1) <= ninput_items[0]); - if(consumed >= ninput) { - goto out; - } - - dout << "PACKET PAD: copy frame" << std::endl; - - memcpy(out, in, sizeof(gr_complex)); - - d_frame_left--; - consumed++; - written++; - in++; - out++; - - if(!d_frame_left) { - d_state = TAIL; - break; - } - - break; - - case TAIL: - - if(!d_tail_left) { - d_state = IDLE; - add_eob(this->nitems_written(0) + written - 1); - goto out; - } - - dout << "PACKET PAD: pad tail" << std::endl; - - memset(out, 0, sizeof(gr_complex)); - - d_tail_left--; - written++; - out ++; - - break; - - default: - assert(false); - break; } } -out: + dout << "copying : " << n << std::endl; - dout << "PACKET PAD: consumed: " << consumed << " produced: " << written << std::endl; - consume(0, consumed); - return written; + std::memcpy(out, in, n * sizeof(gr_complex)); + consume(0, n); + return n; } void packet_pad_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required) { - if(d_frame_left) { - ninput_items_required[0] = noutput_items; - - } else if(d_state == TAIL) { + if(d_pad) { ninput_items_required[0] = 0; } else { - ninput_items_required[0] = 80; + ninput_items_required[0] = noutput_items; } - dout << "PAD forecast noutput_items: " << noutput_items << " required: " << ninput_items_required[0] << std::endl; - dout << "PAD state " << d_state << std::endl; - dout << "d_frame_left " << d_frame_left << std::endl; + // dout << "PAD forecast noutput_items: " << noutput_items << " required: " << ninput_items_required[0] << std::endl; + // dout << "d_pad " << d_pad << std::endl; } packet_pad::sptr diff --git a/lib/packet_pad_impl.h b/lib/packet_pad_impl.h index 37c8d92..5423610 100644 --- a/lib/packet_pad_impl.h +++ b/lib/packet_pad_impl.h @@ -35,12 +35,8 @@ namespace foo { int d_pad_front; int d_pad_tail; - int d_front_left; - int d_tail_left; - int d_frame_left; - - enum {IDLE, FRONT, FRAME, TAIL} d_state; - + int d_pad; + bool d_eob; public: packet_pad_impl(bool debug, unsigned int pad_front,