/* -*- c++ -*- */ /* * Copyright 2019 Thomas Kolb. * * This 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, or (at your option) * any later version. * * This software 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 software; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, * Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include "async_scrambler_impl.h" namespace gr { namespace hamnet70 { async_scrambler::sptr async_scrambler::make(uint8_t start, uint8_t polynom) { return gnuradio::get_initial_sptr (new async_scrambler_impl(start, polynom)); } /* * The private constructor */ async_scrambler_impl::async_scrambler_impl(uint8_t start, uint8_t polynom) : gr::block("async_scrambler", gr::io_signature::make(0, 0, 0), gr::io_signature::make(0, 0, 0)), d_start(start), d_polynom(polynom) { d_in_port = pmt::mp("pdu_in"); d_out_port = pmt::mp("pdu_out"); message_port_register_in(d_in_port); message_port_register_out(d_out_port); set_msg_handler(d_in_port, boost::bind(&async_scrambler_impl::scramble, this ,_1) ); } /* * Our virtual destructor. */ async_scrambler_impl::~async_scrambler_impl() { } void async_scrambler_impl::scramble(pmt::pmt_t pdu) { // extract input pdu pmt::pmt_t meta(pmt::car(pdu)); pmt::pmt_t bytes(pmt::cdr(pdu)); unsigned int crc; size_t pkt_len(0); const uint8_t* bytes_in = pmt::u8vector_elements(bytes, pkt_len); uint8_t* bytes_out = (uint8_t*)volk_malloc(pkt_len*sizeof(uint8_t), volk_get_alignment()); uint8_t lfsr = d_start; for(size_t i = 0; i < pkt_len; i++) { bytes_out[i] = bytes_in[i] ^ lfsr; if(lfsr & 0x1) { lfsr = (lfsr >> 1) ^ d_polynom; } else { lfsr >>= 1; } } pmt::pmt_t output = pmt::init_u8vector(pkt_len, bytes_out); // this copies the values from bytes_out into the u8vector pmt::pmt_t msg_pair = pmt::cons(meta, output); message_port_pub(d_out_port, msg_pair); volk_free(bytes_out); } } /* namespace hamnet70 */ } /* namespace gr */