# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Simple control script for the HackRF One.

Important:  You must run this from a GNURadio Command Prompt!!

Usage:  run this from a command prompt:

          > python simple_hackrf_control.py CW -20 1595.0 30 705f
                                            ^   ^    ^    ^    ^
                                            |   |    |    |    |
                                     rfi type   |    |    |   ID
                                                |    |    |
                                       power [dBm]   |   on-time
                                                     |
                                               frequency [MHz]


The interference types are:

    CW
    GAUSSIAN_RFI_1MHz
    GAUSSIAN_RFI_2MHz
    GAUSSIAN_RFI_5MHz
    BANDLIMITED_RFI_400kHz
    BANDLIMITED_RFI_1MHz
    BANDLIMITED_RFI_2MHz
    PULSED_RFI_1msec
    PULSED_RFI_20msec
    PULSED_RFI_500msec

"""


import sys
import time
import hackrf


# 09/23/2024
#
# This is the net gain in the path from the output
# of the HackRF One to the receiver RF input,
# when the MiniCircuits LNA is in the signal path.
NET_GAIN_IN_RF_PATH_dB = +4.0


def set(interference_type, desired_pwr_dBm, center_freq_MHz,
        on_time=-1, device_str='705f'):
    #
    # To calculate the receiver RF input power:
    #
    #     receiver RF input power [dBm] = output power from the HackRF [dBm]
    #                                   + net gain from HackRF to rcvr [dB]
    #
    # Rearranging to calculate the output power from the HackRF:
    #
    #     output power from HackRF [dBm] = receiver RF input power      [dBm]
    #                                    - net gain from HackRF to rcvr [dB]
    #
    rf_power_dBm = desired_pwr_dBm - NET_GAIN_IN_RF_PATH_dB

    if device_str.lower().endswith('a9c3'):
        #
        # 09/05/2024:  I've added a 10 dB attenuator to the output
        #
        rf_power_dBm += 10.0

    print('\nSTARTING TEST!!!\n')

    # Determine the devide ID
    if device_str.lower().endswith('705f'):
        device_id = '0000000000000000a06063c82416705f'
    if device_str.lower().endswith('7e5f'):
        device_id = '0000000000000000a06063c824237e5f'
    if device_str.lower().endswith('a9c3'):
        device_id = '0000000000000000f75461dc2684a9c3'

    # Connect to the HackRF One
    hack_rf = hackrf.device_connect(device_id, interference_type)
    hack_rf.start()

    # Turn on the RF source
    hack_rf.turn_signal_on(center_freq_MHz, rf_power_dBm)

    if on_time < 0:
        time.sleep(1)
        input('\n\nPress <Enter> to halt jamming...')
    else:
        print('\nTURN ON FOR ', on_time, ' [SEC]\n')
        time.sleep(on_time)

    # Turn off the RF source
    hack_rf.turn_signal_off()

    # Disconnect from the HackRF One
    hack_rf.stop()
    hack_rf.wait()

    print('\n\nTEST FINISHED!!!\n')


if __name__ == '__main__':
    if len(sys.argv) != 5:
        print(__doc__)
        print('Ack, not enough input arguments!!')
    else:
        #
        #        RFI type    desired power [dBm]
        #
        set(str(sys.argv[1]), float(sys.argv[2]),
            float(sys.argv[3]), device_str=str(sys.argv[4]))
        #
        #    center freq [MHz]     HackRF ID
        #
