1 /*
2  * Copyright (C) 2019 Jared Boone, ShareBrained Technology, Inc.
3  *
4  * This file is part of HackRF.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "hackrf_ui.h"
23 
24 #include "ui_portapack.h"
25 #include "ui_rad1o.h"
26 
27 #include <stddef.h>
28 
29 #define UNUSED(x) (void)(x)
30 
31 /* Stub functions for null UI function table */
hackrf_ui_init_null(void)32 void hackrf_ui_init_null(void) { }
hackrf_ui_deinit_null(void)33 void hackrf_ui_deinit_null(void) { }
hackrf_ui_set_frequency_null(uint64_t frequency)34 void hackrf_ui_set_frequency_null(uint64_t frequency) { UNUSED(frequency); }
hackrf_ui_set_sample_rate_null(uint32_t sample_rate)35 void hackrf_ui_set_sample_rate_null(uint32_t sample_rate) { UNUSED(sample_rate); }
hackrf_ui_set_direction_null(const rf_path_direction_t direction)36 void hackrf_ui_set_direction_null(const rf_path_direction_t direction) { UNUSED(direction); }
hackrf_ui_set_filter_bw_null(uint32_t bandwidth)37 void hackrf_ui_set_filter_bw_null(uint32_t bandwidth) { UNUSED(bandwidth); }
hackrf_ui_set_lna_power_null(bool lna_on)38 void hackrf_ui_set_lna_power_null(bool lna_on) { UNUSED(lna_on); }
hackrf_ui_set_bb_lna_gain_null(const uint32_t gain_db)39 void hackrf_ui_set_bb_lna_gain_null(const uint32_t gain_db) { UNUSED(gain_db); }
hackrf_ui_set_bb_vga_gain_null(const uint32_t gain_db)40 void hackrf_ui_set_bb_vga_gain_null(const uint32_t gain_db) { UNUSED(gain_db); }
hackrf_ui_set_bb_tx_vga_gain_null(const uint32_t gain_db)41 void hackrf_ui_set_bb_tx_vga_gain_null(const uint32_t gain_db) { UNUSED(gain_db); }
hackrf_ui_set_first_if_frequency_null(const uint64_t frequency)42 void hackrf_ui_set_first_if_frequency_null(const uint64_t frequency) { UNUSED(frequency); }
hackrf_ui_set_filter_null(const rf_path_filter_t filter)43 void hackrf_ui_set_filter_null(const rf_path_filter_t filter) { UNUSED(filter); }
hackrf_ui_set_antenna_bias_null(bool antenna_bias)44 void hackrf_ui_set_antenna_bias_null(bool antenna_bias) { UNUSED(antenna_bias); }
hackrf_ui_set_clock_source_null(clock_source_t source)45 void hackrf_ui_set_clock_source_null(clock_source_t source) { UNUSED(source); }
hackrf_ui_operacake_gpio_compatible_null(void)46 bool hackrf_ui_operacake_gpio_compatible_null(void) { return true; }
47 
48 /* Null UI function table, used if there's no hardware UI detected. Eliminates the
49  * need to check for null UI before calling a function in the table.
50  */
51 static const hackrf_ui_t hackrf_ui_null = {
52 	&hackrf_ui_init_null,
53 	&hackrf_ui_deinit_null,
54 	&hackrf_ui_set_frequency_null,
55 	&hackrf_ui_set_sample_rate_null,
56 	&hackrf_ui_set_direction_null,
57 	&hackrf_ui_set_filter_bw_null,
58 	&hackrf_ui_set_lna_power_null,
59 	&hackrf_ui_set_bb_lna_gain_null,
60 	&hackrf_ui_set_bb_vga_gain_null,
61 	&hackrf_ui_set_bb_tx_vga_gain_null,
62 	&hackrf_ui_set_first_if_frequency_null,
63 	&hackrf_ui_set_filter_null,
64 	&hackrf_ui_set_antenna_bias_null,
65 	&hackrf_ui_set_clock_source_null,
66 	&hackrf_ui_operacake_gpio_compatible_null
67 };
68 
69 static const hackrf_ui_t* ui = NULL;
70 static bool ui_enabled = true;
71 
hackrf_ui(void)72 const hackrf_ui_t* hackrf_ui(void) {
73 	/* Detect on first use. If no UI hardware is detected, use a stub function table. */
74 	if( ui == NULL && ui_enabled ) {
75 #ifdef HACKRF_ONE
76 		if( portapack_hackrf_ui_init ) {
77 			ui = portapack_hackrf_ui_init();
78 		}
79 #endif
80 #ifdef RAD1O
81 		if( rad1o_ui_setup ) {
82 			ui = rad1o_ui_setup();
83 		}
84 #endif
85 	}
86 
87 	if( ui == NULL ) {
88 		ui = &hackrf_ui_null;
89 	}
90 	return ui;
91 }
92 
hackrf_ui_set_enable(bool enabled)93 void hackrf_ui_set_enable(bool enabled) {
94 	if (ui_enabled != enabled) {
95 		ui_enabled = enabled;
96 		hackrf_ui()->deinit();
97 		ui = NULL;
98 		if (enabled) {
99 			hackrf_ui()->init();
100 		}
101 	}
102 }
103