diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index 7d77284..a57e346 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -20,6 +20,10 @@ install(FILES ccs_burst_tagger.xml ccs_packet_dropper.xml + ccs_packet_pad.xml + ccs_pdu_to_blob.xml + ccs_periodic_msg_source.xml + ccs_string_to_blob.xml ccs_wireshark_connector.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/ccs_packet_pad.xml b/grc/ccs_packet_pad.xml new file mode 100644 index 0000000..3cc1438 --- /dev/null +++ b/grc/ccs_packet_pad.xml @@ -0,0 +1,52 @@ + + + + Packet Pad + ccs_packet_pad + CCS + import ccs + ccs.packet_pad($debug, $pad_front, $pad_tail) + + + Debug + debug + False + bool + + + + + + + Pad Front + pad_front + 0 + int + + + + Pad Tail + pad_tail + 0 + int + + + + in + complex + 1 + + + + out + complex + 1 + + + diff --git a/grc/ccs_pdu_to_blob.xml b/grc/ccs_pdu_to_blob.xml new file mode 100644 index 0000000..77b587f --- /dev/null +++ b/grc/ccs_pdu_to_blob.xml @@ -0,0 +1,19 @@ + + + + PDU to Blob + ccs_pdu_to_blob + CCS + import ccs + ccs.pdu_to_blob() + + + in + message + + + + out + message + + diff --git a/grc/ccs_periodic_msg_source.xml b/grc/ccs_periodic_msg_source.xml new file mode 100644 index 0000000..c9e2106 --- /dev/null +++ b/grc/ccs_periodic_msg_source.xml @@ -0,0 +1,47 @@ + + + + Periodic Message Source + ccs_periodic_msg_source + CCS + import ccs + ccs..periodic_msg_source($msg, $interval, $num, $debug) + + Message PMT + msg + pmt.pmt_intern("Hello World!") + raw + + + Period (ms) + interval + 1000 + real + + + Debug + debug + False + bool + + + + + Num Messages + num + -1 + int + + + + out + message + 1 + + diff --git a/grc/ccs_string_to_blob.xml b/grc/ccs_string_to_blob.xml new file mode 100644 index 0000000..253b082 --- /dev/null +++ b/grc/ccs_string_to_blob.xml @@ -0,0 +1,19 @@ + + + + String to Blob + ccs_string_to_blob + CCS + import cccs + ccs.string_to_blob() + + + in + message + + + + out + message + + diff --git a/include/ccs/CMakeLists.txt b/include/ccs/CMakeLists.txt index 62fef02..4502bee 100644 --- a/include/ccs/CMakeLists.txt +++ b/include/ccs/CMakeLists.txt @@ -24,6 +24,10 @@ install(FILES api.h burst_tagger.h packet_dropper.h + packet_pad.h + pdu_to_blob.h + periodic_msg_source.h + string_to_blob.h wireshark_connector.h DESTINATION include/ccs ) diff --git a/include/ccs/packet_pad.h b/include/ccs/packet_pad.h new file mode 100644 index 0000000..c6a8fad --- /dev/null +++ b/include/ccs/packet_pad.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#ifndef INCLUDED_CCS_PACKET_PAD_H +#define INCLUDED_CCS_PACKET_PAD_H + +#include +#include + +namespace gr { +namespace ccs { + +class CCS_API packet_pad : virtual public gr::block +{ +public: + + typedef boost::shared_ptr sptr; + static sptr make(bool debug = false, int pad_front = 0, int pad_tail = 0); + +}; + +} // namespace ccs +} // namespace gr + +#endif /* INCLUDED_CCS_PACKET_PAD_H */ diff --git a/include/ccs/pdu_to_blob.h b/include/ccs/pdu_to_blob.h new file mode 100644 index 0000000..e0acfe0 --- /dev/null +++ b/include/ccs/pdu_to_blob.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#ifndef INCLUDED_CCS_PDU_TO_BLOB_H +#define INCLUDED_CCS_PDU_TO_BLOB_H + +#include +#include + +namespace gr { +namespace ccs { + +class CCS_API pdu_to_blob : virtual public gr::block +{ +public: + + typedef boost::shared_ptr sptr; + static sptr make(); + +}; + +} // namespace ccs +} // namespace gr + +#endif /* INCLUDED_CCS_PDU_TO_BLOB_H */ diff --git a/include/ccs/periodic_msg_source.h b/include/ccs/periodic_msg_source.h new file mode 100644 index 0000000..6fd2c1d --- /dev/null +++ b/include/ccs/periodic_msg_source.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#ifndef INCLUDED_CCS_PERIODIC_MSG_SOURCE_H +#define INCLUDED_CCS_PERIODIC_MSG_SOURCE_H + +#include +#include + +namespace gr { +namespace ccs { + +class CCS_API periodic_msg_source : virtual public gr::block +{ +public: + + typedef boost::shared_ptr sptr; + static sptr make(pmt::pmt_t msg, float interval, int num_msg = -1, bool debug = false); + +}; + +} // namespace ccs +} // namespace gr + +#endif /* INCLUDED_CCS_PERIODIC_MSG_SOURCE */ diff --git a/include/ccs/string_to_blob.h b/include/ccs/string_to_blob.h new file mode 100644 index 0000000..a72f74c --- /dev/null +++ b/include/ccs/string_to_blob.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#ifndef INCLUDED_CCS_STRING_TO_BLOB_H +#define INCLUDED_CCS_STRING_TO_BLOB_H + +#include +#include + +namespace gr { +namespace ccs { + +class CCS_API string_to_blob : virtual public gr::block +{ +public: + + typedef boost::shared_ptr sptr; + static sptr make(); + +}; + +} // namespace ccs +} // namespace gr + +#endif /* INCLUDED_CCS_STRING_TO_BLOB_H */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 4ab3ab9..9956e51 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -28,6 +28,10 @@ link_directories(${Boost_LIBRARY_DIRS}) list(APPEND ccs_sources burst_tagger_impl.cc packet_dropper_impl.cc + packet_pad_impl.cc + pdu_to_blob_impl.cc + periodic_msg_source_impl.cc + string_to_blob_impl.cc wireshark_connector_impl.cc ) diff --git a/lib/packet_pad_impl.cc b/lib/packet_pad_impl.cc new file mode 100644 index 0000000..f769215 --- /dev/null +++ b/lib/packet_pad_impl.cc @@ -0,0 +1,237 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#include +#include + +#include + +using namespace gr::ccs; + + +class packet_pad_impl : public packet_pad { + +static const int SYMBOL_LENGTH = 80; + +#define dout d_debug && std::cout + +public: +packet_pad_impl(bool debug, int pad_front, 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) { + + assert(pad_front >= 0); + assert(pad_tail >= 0); + + set_relative_rate(1); + set_output_multiple(SYMBOL_LENGTH); + set_tag_propagation_policy(block::TPP_DONT); +} + +~packet_pad_impl(){ +} + +void insert_sob(uint64_t item) { + return; + dout << "OFDM PAD: insert sob at: " << item << std::endl; + + const pmt::pmt_t sob_key = pmt::string_to_symbol("tx_sob"); + const pmt::pmt_t value = pmt::PMT_T; + const pmt::pmt_t srcid = pmt::string_to_symbol(this->name()); + add_item_tag(0, item, sob_key, value, srcid); + add_item_tag(0, item, pmt::string_to_symbol("rel_tx_time"), pmt::from_double(0.05), srcid); +} + +void insert_eob(uint64_t item) { + dout << "OFDM PAD: insert eob at: " << item << std::endl; + + const pmt::pmt_t eob_key = pmt::string_to_symbol("tx_eob"); + const pmt::pmt_t value = pmt::PMT_T; + const pmt::pmt_t srcid = pmt::string_to_symbol(this->name()); + add_item_tag(0, item, eob_key, value, srcid); +} + +int 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] / SYMBOL_LENGTH; // available input symbols + //assert(ninput_items[0] % SYMBOL_LENGTH == 0); + int noutput = noutput_items / SYMBOL_LENGTH; // available output symbols + + + // 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 + + // tag handling + const uint64_t nread = this->nitems_read(0); + std::vector tags; + + dout << "OFDM PAD: input size: " << ninput_items[0] << " output size: " + << noutput_items << std::endl; + + while(written < noutput) { + + // debug + dout << "OFDM PAD: iteration - consumed: " << consumed << + " written: " << written << std::endl; + + switch(d_state) { + + case IDLE: + + //assert(consumed < ninput); + if(consumed >= ninput) { + goto out; + } + + get_tags_in_range(tags, 0, nread + consumed * SYMBOL_LENGTH, + nread + (consumed + 1) * SYMBOL_LENGTH - 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; + + insert_sob(this->nitems_written(0) + written * SYMBOL_LENGTH); + d_state = FRONT; + + dout << "OFDM 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 << "OFDM PAD: pad front" << std::endl; + + memset(out, 0, SYMBOL_LENGTH * sizeof(gr_complex)); + + d_front_left--; + out += SYMBOL_LENGTH; + written++; + + break; + + case FRAME: + + //assert((consumed + 1) * SYMBOL_LENGTH <= ninput_items[0]); + if(consumed >= ninput) { + goto out; + } + + dout << "OFDM PAD: copy frame" << std::endl; + + memcpy(out, in, SYMBOL_LENGTH * sizeof(gr_complex)); + + d_frame_left--; + consumed++; + written++; + in += SYMBOL_LENGTH; + out += SYMBOL_LENGTH; + + if(!d_frame_left) { + d_state = TAIL; + break; + } + + break; + + case TAIL: + + if(!d_tail_left) { + d_state = IDLE; + insert_eob(this->nitems_written(0) + written * SYMBOL_LENGTH - 1); + goto out; + } + + dout << "OFDM PAD: pad tail" << std::endl; + + memset(out, 0, SYMBOL_LENGTH * sizeof(gr_complex)); + + d_tail_left--; + written++; + out += SYMBOL_LENGTH; + + break; + + default: + assert(false); + break; + } + } + +out: + + dout << "OFDM PAD: consumed: " << consumed << " produced: " << written << std::endl; + consume(0, consumed * SYMBOL_LENGTH); + return written * SYMBOL_LENGTH; +} + +void +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) { + ninput_items_required[0] = 0; + } else { + ninput_items_required[0] = 80; + } + + 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; +} + + + +private: + bool d_debug; + + 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; +}; + +packet_pad::sptr +packet_pad::make(bool debug, int pad_front, int pad_tail) { + return gnuradio::get_initial_sptr(new packet_pad_impl(debug, pad_front, pad_tail)); +} diff --git a/lib/pdu_to_blob_impl.cc b/lib/pdu_to_blob_impl.cc new file mode 100644 index 0000000..552e232 --- /dev/null +++ b/lib/pdu_to_blob_impl.cc @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#include + +#include +#include + +using namespace gr::ccs; + +class pdu_to_blob_impl : public pdu_to_blob { + +public: + +pdu_to_blob_impl() : + block("pdu_to_blob", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)) { + + message_port_register_out(pmt::mp("out")); + + message_port_register_in(pmt::mp("in")); + set_msg_handler(pmt::mp("in"), boost::bind(&pdu_to_blob_impl::make_blob, this, _1)); +} + +~pdu_to_blob_impl() { + +} + +void make_blob(pmt::pmt_t msg) { + + assert(pmt::is_pair(msg)); + + pmt::pmt_t vec = pmt::cdr(msg); + assert(pmt::is_u8vector(vec)); + + size_t len; + + const void *data = pmt::uniform_vector_elements(vec, len); + assert(len < 4000); + + std::memcpy(buf, data, len); + + pmt::pmt_t blob = pmt::make_blob(buf, len); + + message_port_pub(pmt::mp("out"), blob); +} + +private: + char buf[4000]; + +}; + +pdu_to_blob::sptr +pdu_to_blob::make() { + return gnuradio::get_initial_sptr(new pdu_to_blob_impl()); +} diff --git a/lib/periodic_msg_source_impl.cc b/lib/periodic_msg_source_impl.cc new file mode 100644 index 0000000..530cac9 --- /dev/null +++ b/lib/periodic_msg_source_impl.cc @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#include + +#include +#include +#include + +using namespace gr::ccs; + +class periodic_msg_source_impl : public periodic_msg_source { + +#define dout d_debug && std::cout + +public: + +periodic_msg_source_impl(pmt::pmt_t msg, float interval, int num_msg, bool debug) : + block("periodic_msg_source", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)), + d_msg(msg), + d_num_msg(num_msg), + d_interval(interval), + d_debug(debug), + d_finished(false) { + + message_port_register_out(pmt::mp("out")); + d_thread = boost::shared_ptr(new boost::thread(boost::bind(&periodic_msg_source_impl::run, this, this))); +} + +~periodic_msg_source_impl() { + d_finished = true; + d_thread->interrupt(); + d_thread->join(); +} + + +void run(periodic_msg_source_impl *instance) { + while(!d_finished) { + + dout << "number of messages left: " << d_num_msg << std::endl; + + if(!d_num_msg || d_finished) { + d_finished = true; + break; + } + + message_port_pub( pmt::mp("out"), d_msg ); + + boost::this_thread::sleep(boost::posix_time::milliseconds(d_interval)); + + if(d_num_msg > 0) { + d_num_msg--; + } + } + + dout << "stopping msg source" << std::endl; + message_port_pub(pmt::mp("out"), pmt::PMT_EOF); +} + +private: + int d_num_msg; + bool d_debug; + bool d_finished; + float d_interval; + pmt::pmt_t d_msg; + boost::shared_ptr d_thread; + +}; + +periodic_msg_source::sptr +periodic_msg_source::make(pmt::pmt_t msg, float interval, int num_msg, bool debug) { + return gnuradio::get_initial_sptr(new periodic_msg_source_impl(msg, interval, num_msg, debug)); +} + diff --git a/lib/string_to_blob_impl.cc b/lib/string_to_blob_impl.cc new file mode 100644 index 0000000..1782edb --- /dev/null +++ b/lib/string_to_blob_impl.cc @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2013 Bastian Bloessl + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ +#include + +#include +#include +#include + +using namespace gr::ccs; + +class string_to_blob_impl : public string_to_blob { + +public: + +string_to_blob_impl() : + block("string_to_blob", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)) { + + message_port_register_out(pmt::mp("out")); + + message_port_register_in(pmt::mp("in")); + set_msg_handler(pmt::mp("in"), boost::bind(&string_to_blob_impl::make_blob, this, _1)); +} + +~string_to_blob_impl() { + +} + +void make_blob(pmt::pmt_t msg) { + + if(pmt::is_eof_object(msg)) { + message_port_pub(pmt::mp("out"), pmt::PMT_EOF); + detail().get()->set_done(true); + return; + } + + assert(pmt::is_symbol(msg)); + + int len = pmt::symbol_to_string(msg).length(); + const char *data = pmt::symbol_to_string(msg).data(); + + pmt::pmt_t blob = pmt::make_blob(data, len); + + message_port_pub(pmt::mp("out"), blob); +} + +}; + +string_to_blob::sptr +string_to_blob::make() { + return gnuradio::get_initial_sptr(new string_to_blob_impl()); +} + + diff --git a/swig/ccs_swig.i b/swig/ccs_swig.i index e48c7ec..09df45b 100644 --- a/swig/ccs_swig.i +++ b/swig/ccs_swig.i @@ -8,14 +8,26 @@ %{ #include "ccs/burst_tagger.h" #include "ccs/packet_dropper.h" +#include "ccs/packet_pad.h" +#include "ccs/pdu_to_blob.h" +#include "ccs/periodic_msg_source.h" +#include "ccs/string_to_blob.h" #include "ccs/wireshark_connector.h" %} %include "ccs/burst_tagger.h" %include "ccs/packet_dropper.h" +%include "ccs/packet_pad.h" +%include "ccs/pdu_to_blob.h" +%include "ccs/periodic_msg_source.h" +%include "ccs/string_to_blob.h" %include "ccs/wireshark_connector.h" GR_SWIG_BLOCK_MAGIC2(ccs, burst_tagger); GR_SWIG_BLOCK_MAGIC2(ccs, packet_dropper); GR_SWIG_BLOCK_MAGIC2(ccs, wireshark_connector); +GR_SWIG_BLOCK_MAGIC2(ccs, packet_pad); +GR_SWIG_BLOCK_MAGIC2(ccs, pdu_to_blob); +GR_SWIG_BLOCK_MAGIC2(ccs, periodic_msg_source); +GR_SWIG_BLOCK_MAGIC2(ccs, string_to_blob);