/* 
 * Copyright (c) 2021 Satlab A/S <satlab@satlab.com>
 * 
 * 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 <gnuradio/io_signature.h>
#include "tag_to_trigger_impl.h"

namespace gr {
    namespace satlab {

        tag_to_trigger::sptr
        tag_to_trigger::make(const std::string& tag)
        {
            return gnuradio::get_initial_sptr(new tag_to_trigger_impl(tag));
        }

        int tag_to_trigger_impl::work(int noutput_items,
                                      gr_vector_const_void_star& input_items,
                                      gr_vector_void_star& output_items)
        {
            std::vector<tag_t> t;
            get_tags_in_range(t, 0, nitems_read(0), nitems_read(0) + noutput_items, pmt::intern(d_tag));

            for (int i = 0; i < t.size(); i++) {
                pmt::pmt_t trigger = pmt::cons(pmt::PMT_NIL, pmt::PMT_NIL);
                message_port_pub(pmt::mp("trigger"), trigger);
            }

            return noutput_items;
        }

        tag_to_trigger_impl::tag_to_trigger_impl(const std::string& tag)
            : gr::sync_block("tag_to_trigger",
                    gr::io_signature::make(1, 1, sizeof(gr_complex)),
                    gr::io_signature::make(0, 0, 0)),
            d_tag(tag)
        {
            message_port_register_out(pmt::mp("trigger"));
        }
    } /* namespace satlab */
} /* namespace gr */
