# Copyright (c) 2021 Satlab A/S <satlab@satlab.com>
# SPDX-License-Identifier: GPL-3.0

from gnuradio import satlab


class SRS3Radio(satlab.SRSRadio):
    def __init__(self,
                 rx_freq=2229500000,
                 rx_rate=512000,
                 rx_size=217,
                 rx_rs=True,
                 rx_cc=True,
                 rx_key=b'\x00' * 32,
                 rx_auth=False,
                 rx_decrypt=False,
                 rx_id=0,
                 rx_gain=20,
                 tx_freq=2029500000,
                 tx_rate=128000,
                 tx_size=217,
                 tx_rs=True,
                 tx_cc=True,
                 tx_key=b'\x00' * 32,
                 tx_auth=False,
                 tx_encrypt=False,
                 tx_id=0,
                 tx_gain=50,
                 tx_idleframes=0,
                 tx_max_in_flight=100,
                 tun=None,
                 udp_bind='0.0.0.0',
                 udp_port=52002,
                 clock_source='internal',
                 gpio_control=False,
                 xmlrpc_enable=False,
                 xmlrpc_bind='0.0.0.0',
                 xmlrpc_port=8080,
                 zmq_enable=False,
                 zmq_port=52344,
                 pdu_logger=False,
                 fft_websocket_enable=False,
                 fft_collect_enable=False,
                 init_gated=False):

        # Variables
        self.samp_rate = 2.56e6
        self.clock_rate = 4 * self.samp_rate

        # Blocks
        self.transmitter = satlab.srs3_tx(
            samp_rate=self.samp_rate,
            rate=int(tx_rate),
            size=tx_size,
            idnum=tx_id,
            cc=tx_cc,
            rs=tx_rs,
            crc=True,
            rand=True,
            key=tx_key,
            encrypt=tx_encrypt,
            auth=tx_auth,
            v1compat=False,
            idleframes=tx_idleframes,
            max_in_flight=tx_max_in_flight)

        self.receiver = satlab.srs3_rx(
            samp_rate=self.samp_rate,
            rate=int(rx_rate),
            size=rx_size,
            idnum=rx_id,
            cc=rx_cc,
            rs=rx_rs,
            crc=True,
            rand=True,
            key=rx_key,
            auth=rx_auth,
            decrypt=rx_decrypt,
            v1compat=False)

        super().__init__(
            rx_freq=rx_freq,
            rx_bw=rx_rate * 1.5,
            rx_size=rx_size,
            rx_gain=rx_gain,
            tx_freq=tx_freq,
            tx_bw=tx_rate * 1.5,
            tx_size=tx_size,
            tx_gain=tx_gain,
            tun=tun,
            udp_bind=udp_bind,
            udp_port=udp_port,
            clock_source=clock_source,
            gpio_control=gpio_control,
            xmlrpc_enable=xmlrpc_enable,
            xmlrpc_bind=xmlrpc_bind,
            xmlrpc_port=xmlrpc_port,
            zmq_enable=zmq_enable,
            zmq_port=zmq_port,
            pdu_logger=pdu_logger,
            fft_websocket_enable=fft_websocket_enable,
            fft_collect_enable=fft_collect_enable,
            init_gated=init_gated,
            tsb_tag_name='packet_len')


class SRS3v1Radio(SRS3Radio):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.receiver.set_v1compat(True)
        self.transmitter.set_v1compat(True)
