diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index d776de7..75c02d8 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -18,5 +18,7 @@ # Boston, MA 02110-1301, USA. install(FILES - DESTINATION share/gnuradio/grc/blocks + ccs_packet_dropper.xml + ccs_wireshark_connector.xml + DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/ccs_packet_dropper.xml b/grc/ccs_packet_dropper.xml new file mode 100644 index 0000000..72e1962 --- /dev/null +++ b/grc/ccs_packet_dropper.xml @@ -0,0 +1,30 @@ + + + Packet Dropper + ccs_packet_dropper + CCS + import ccs + ccs.packet_dropper($drop_rate, $seed) + + + Packet drop rate + drop_rate + real + + + + Random seed + seed + int + + + + in + message + + + + out + message + + diff --git a/grc/ccs_wireshark_connector.xml b/grc/ccs_wireshark_connector.xml new file mode 100644 index 0000000..6a9d9e0 --- /dev/null +++ b/grc/ccs_wireshark_connector.xml @@ -0,0 +1,34 @@ + + + Wireshark Connector + ccs_wireshark_connector + CCS + import ccs + ccs.wireshark_connector($debug) + + + Debug + debug + False + bool + + + + + + + in + message + + + out + byte + 1 + + diff --git a/include/ccs/CMakeLists.txt b/include/ccs/CMakeLists.txt index 6e074e7..2f37528 100644 --- a/include/ccs/CMakeLists.txt +++ b/include/ccs/CMakeLists.txt @@ -21,6 +21,8 @@ # Install public header files ######################################################################## install(FILES - api.h - DESTINATION include/ccs + api.h + packet_dropper.h + wireshark_connector.h + DESTINATION include/ccs ) diff --git a/include/ccs/packet_dropper.h b/include/ccs/packet_dropper.h new file mode 100644 index 0000000..b08e4a2 --- /dev/null +++ b/include/ccs/packet_dropper.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2013 Christoph Leitner + * + * 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_GR_CCS_PACKET_DROPPER_H +#define INCLUDED_GR_CCS_PACKET_DROPPER_H + +#include +#include + +namespace gr { +namespace ccs { + + class CCS_API packet_dropper : virtual public gr::block + { + public: + typedef boost::shared_ptr sptr; + static sptr make(double drop_rate, unsigned long seed); + }; +} // namespace ccs +} // namespace gr + +#endif /* INCLUDED_GR_CCS_PACKET_DROPPER_H */ diff --git a/include/ccs/wireshark_connector.h b/include/ccs/wireshark_connector.h new file mode 100644 index 0000000..371919b --- /dev/null +++ b/include/ccs/wireshark_connector.h @@ -0,0 +1,37 @@ +/* + * 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_GR_CCS_WIRESHARK_CONNECTOR_H +#define INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_H + +#include +#include + +namespace gr { +namespace ccs { + +class CCS_API wireshark_connector : virtual public gr::block +{ +public: + + typedef boost::shared_ptr sptr; + static sptr make(bool debug= false); +}; + +} // namespace ccs +} // namespace gr + +#endif /* INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_H */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 47063f5..bc6e993 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -26,6 +26,8 @@ include_directories(${Boost_INCLUDE_DIR}) link_directories(${Boost_LIBRARY_DIRS}) list(APPEND ccs_sources + packet_dropper_impl.cc + wireshark_connector_impl.cc ) add_library(gnuradio-ccs SHARED ${ccs_sources}) diff --git a/lib/packet_dropper_impl.cc b/lib/packet_dropper_impl.cc new file mode 100644 index 0000000..849154d --- /dev/null +++ b/lib/packet_dropper_impl.cc @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2013 Christoph Leitner + * + * 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 "packet_dropper_impl.h" + +using namespace gr::ccs; + +packet_dropper_impl::packet_dropper_impl(double drop_rate, unsigned long seed) + : d_drop_rate(drop_rate), d_generator(seed), d_distribution(0.0,1.0), + block("rime_stack", gr::io_signature::make(0, 0, 0), + gr::io_signature::make(0, 0, 0)) +{ + if(d_drop_rate >= 1){ + throw std::out_of_range("drop rate has to be < 1"); + } + message_port_register_in(pmt::mp("in")); + set_msg_handler(pmt::mp("in"), boost::bind(&packet_dropper_impl::msg_handler, this, _1)); + message_port_register_out(pmt::mp("out")); +} + +void +packet_dropper_impl::msg_handler(pmt::pmt_t msg) +{ + if(d_distribution(d_generator) <= d_drop_rate){ + return; + } + message_port_pub(pmt::mp("out"), msg); +} + +packet_dropper::sptr +packet_dropper::make(double drop_rate, unsigned long seed) +{ + return gnuradio::get_initial_sptr(new packet_dropper_impl(drop_rate, seed)); +} + diff --git a/lib/packet_dropper_impl.h b/lib/packet_dropper_impl.h new file mode 100644 index 0000000..75c309c --- /dev/null +++ b/lib/packet_dropper_impl.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2013 Christoph Leitner + * + * 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_GR_CCS_PACKET_DROPPER_IMPL_H +#define INCLUDED_GR_CCS_PACKET_DROPPER_IMPL_H + +#include + +namespace gr { +namespace ccs { + class CCS_API packet_dropper_impl : public packet_dropper { + private: + double d_drop_rate; + std::default_random_engine d_generator; + std::uniform_real_distribution d_distribution; + void msg_handler(pmt::pmt_t msg); + public: + packet_dropper_impl(double drop_rate, unsigned long seed); + }; +} +} + +#endif /* INCLUDED_GR_CCS_PACKET_DROPPER_IMPL_H */ diff --git a/lib/wireshark_connector_impl.cc b/lib/wireshark_connector_impl.cc new file mode 100644 index 0000000..4fa082a --- /dev/null +++ b/lib/wireshark_connector_impl.cc @@ -0,0 +1,128 @@ +/* + * 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 "wireshark_connector_impl.h" +#include +#include + +#include +#include +#include + +using namespace gr::ccs; + + +class wireshark_connector_impl : public wireshark_connector { +public: + +#define dout d_debug && std::cout + +wireshark_connector_impl(bool debug) : + block ("wireshark_connector", + gr::io_signature::make(0, 0, 0), + gr::io_signature::make(1, 1, sizeof(uint8_t))), + d_msg_offset(0), + d_debug(debug) { + + message_port_register_in(pmt::mp("in")); + + pcap_global *hdr = (pcap_global*)d_msg; + hdr->magic_number = 0xa1b2c3d4; + hdr->version_major = 2; + hdr->version_minor = 4; + hdr->thiszone = 0; + hdr->sigfigs = 0; + hdr->snaplen = 65535; + hdr->network = ZIGBEE; + d_msg_len = sizeof(pcap_global); +} + +~wireshark_connector_impl(void) { +} + +void copy_message(const char *buf, int len) { + + struct timeval t; + gettimeofday(&t, NULL); + + pcap_pkt hdr; + hdr.ts_sec = t.tv_sec; + hdr.ts_usec = t.tv_usec; + hdr.incl_len = len; + hdr.orig_len = len; + + memcpy(d_msg, &hdr, sizeof(pcap_pkt)); + memcpy(d_msg + sizeof(pcap_pkt), buf, len); + d_msg_len = sizeof(pcap_pkt) + len; +} + +int general_work(int noutput, gr_vector_int& ninput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items ) { + + gr_complex *out = (gr_complex*)output_items[0]; + + while(!d_msg_len) { + pmt::pmt_t msg(delete_head_blocking(pmt::mp("in"))); + + if(pmt::is_eof_object(msg)) { + dout << "WIRESHARK: exiting" << std::endl; + return -1; + } else if(pmt::is_blob(msg)) { + dout << "WIRESHARK: received new message" << std::endl; + dout << "message length " << pmt::blob_length(msg) << std::endl; + + copy_message((const char*)pmt::blob_data(msg), pmt::blob_length(msg)); + break; + } else if(pmt::is_pair(msg)) { + pmt::pmt_t blob = pmt::cdr(msg); + dout << "WIRESHARK: received new message" << std::endl; + dout << "message length " << pmt::blob_length(blob) << std::endl; + copy_message((const char*)pmt::blob_data(blob), pmt::blob_length(blob)); + break; + } + } + + int to_copy = std::min((d_msg_len - d_msg_offset), noutput); + memcpy(out, d_msg + d_msg_offset, to_copy); + + dout << "WIRESHARK: d_msg_offset: " << d_msg_offset << + " to_copy: " << to_copy << + " d_msg_len " << d_msg_len << std::endl; + + d_msg_offset += to_copy; + + if(d_msg_offset == d_msg_len) { + d_msg_offset = 0; + d_msg_len = 0; + } + + dout << "WIRESHARK: output size: " << noutput << + " produced items: " << to_copy << std::endl; + return to_copy; +} + +private: + bool d_debug; + int d_msg_offset; + int d_msg_len; + char d_msg[256]; +}; + +wireshark_connector::sptr +wireshark_connector::make(bool debug) { + return gnuradio::get_initial_sptr(new wireshark_connector_impl(debug)); +} diff --git a/lib/wireshark_connector_impl.h b/lib/wireshark_connector_impl.h new file mode 100644 index 0000000..a4d98c4 --- /dev/null +++ b/lib/wireshark_connector_impl.h @@ -0,0 +1,49 @@ +/* + * 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_GR_CCS_WIRESHARK_CONNECTOR_IMPL_H +#define INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_IMPL_H + +#include + +namespace gr { +namespace ccs { + +enum LINK_LAYER_PROTOCOL { + ZIGBEE = 195 +}; + +struct pcap_global { + uint32_t magic_number; /* magic number */ + uint16_t version_major; /* major version number */ + uint16_t version_minor; /* minor version number */ + int32_t thiszone; /* GMT to local correction */ + uint32_t sigfigs; /* accuracy of timestamps */ + uint32_t snaplen; /* max length of captured packets, in octets */ + uint32_t network; /* data link type */ +}__attribute__((packed)); + +struct pcap_pkt { + uint32_t ts_sec; /* timestamp seconds */ + uint32_t ts_usec; /* timestamp microseconds */ + uint32_t incl_len; /* number of octets of packet saved in file */ + uint32_t orig_len; /* actual length of packet */ +}__attribute__((packed)); + +} // namespace ccs +} // namespace gr + +#endif /* INCLUDED_GR_CCS_WIRESHARK_CONNECTOR_IMPL_H */ diff --git a/swig/ccs_swig.i b/swig/ccs_swig.i index 1b30f36..43b4624 100644 --- a/swig/ccs_swig.i +++ b/swig/ccs_swig.i @@ -1,13 +1,18 @@ -/* -*- c++ -*- */ - #define CCS_API -%include "gnuradio.i" // the common stuff +%include "gnuradio.i" //load generated python docstrings %include "ccs_swig_doc.i" %{ +#include "ccs/packet_dropper.h" +#include "ccs/wireshark_connector.h" %} +%include "ccs/packet_dropper.h" +%include "ccs/wireshark_connector.h" + +GR_SWIG_BLOCK_MAGIC2(ccs, packet_dropper); +GR_SWIG_BLOCK_MAGIC2(ccs, wireshark_connector);