2 * Copyright (C) 2017 Konsulko Group
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "radio_impl.h"
25 #include "radio_output.h"
28 // Structure to describe FM band plans, all values in Hz.
36 static fm_band_plan_t known_fm_band_plans[5] = {
37 { .name = "US", .min = 87900000, .max = 107900000, .step = 200000 },
38 { .name = "JP", .min = 76100000, .max = 89900000, .step = 100000 },
39 { .name = "EU", .min = 87500000, .max = 108000000, .step = 50000 },
40 { .name = "ITU-1", .min = 87500000, .max = 108000000, .step = 50000 },
41 { .name = "ITU-2", .min = 87900000, .max = 107900000, .step = 50000 }
44 static unsigned int bandplan;
47 static uint32_t current_frequency;
49 static void rtl_output_callback(int16_t *result, int result_len, void *ctx)
52 radio_output_write((char*) result, result_len * 2);
55 int radio_impl_init(void)
58 int conf_file_present = 0;
64 // Load settings from configuration file if it exists
65 conf_file = g_key_file_new();
67 g_key_file_load_from_dirs(conf_file,
69 (const gchar**) g_get_system_config_dirs(),
71 G_KEY_FILE_KEEP_COMMENTS,
73 conf_file_present = 1;
75 // Set band plan if it is specified
76 value_str = g_key_file_get_string(conf_file,
83 i < sizeof(known_fm_band_plans) / sizeof(fm_band_plan_t);
85 if(!strcasecmp(value_str, known_fm_band_plans[i].name)) {
92 fprintf(stderr, "Using FM Bandplan: %s\n", known_fm_band_plans[bandplan].name);
94 current_frequency = radio_impl_get_min_frequency(BAND_FM);
95 if(rtl_fm_init(current_frequency, 200000, 48000, rtl_output_callback, NULL) < 0) {
99 if(conf_file_present) {
100 GError *error = NULL;
103 // Allow over-riding scanning parameters just in case a demo
104 // setup needs to do so to work reliably.
105 n = g_key_file_get_integer(conf_file,
107 "scan_squelch_level",
109 //error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND &&
110 //error->code != G_KEY_FILE_ERROR_INVALID_VALUE) {
112 fprintf(stderr, "Scanning squelch level set to %d\n", n);
113 rtl_fm_scan_set_squelch_level(n);
117 n = g_key_file_get_integer(conf_file,
119 "scan_squelch_limit",
122 fprintf(stderr, "Scanning squelch limit set to %d\n", n);
123 rtl_fm_scan_set_squelch_limit(n);
126 g_key_file_free(conf_file);
133 uint32_t radio_impl_get_frequency(void)
135 return current_frequency;
138 void radio_impl_set_frequency(uint32_t frequency)
143 if(frequency < known_fm_band_plans[bandplan].min ||
144 frequency > known_fm_band_plans[bandplan].max)
147 radio_impl_scan_stop();
148 current_frequency = frequency;
149 rtl_fm_set_freq(frequency);
152 void radio_impl_set_frequency_callback(radio_freq_callback_t callback,
155 rtl_fm_set_freq_callback(callback, data);
158 radio_band_t radio_impl_get_band(void)
163 void radio_impl_set_band(radio_band_t band)
165 // We only support FM, so do nothing
168 int radio_impl_band_supported(radio_band_t band)
175 uint32_t radio_impl_get_min_frequency(radio_band_t band)
177 return known_fm_band_plans[bandplan].min;
180 uint32_t radio_impl_get_max_frequency(radio_band_t band)
182 return known_fm_band_plans[bandplan].max;
185 uint32_t radio_impl_get_frequency_step(radio_band_t band)
194 ret = known_fm_band_plans[bandplan].step;
202 void radio_impl_start(void)
208 if(radio_output_start() != 0)
216 void radio_impl_stop(void)
229 void radio_impl_scan_start(radio_scan_direction_t direction,
230 radio_scan_callback_t callback,
233 rtl_fm_scan_start(direction == SCAN_FORWARD ? 0 : 1,
236 radio_impl_get_frequency_step(BAND_FM),
237 radio_impl_get_min_frequency(BAND_FM),
238 radio_impl_get_max_frequency(BAND_FM));
241 void radio_impl_scan_stop(void)
246 radio_stereo_mode_t radio_impl_get_stereo_mode(void)
251 void radio_impl_set_stereo_mode(radio_stereo_mode_t mode)
253 // We only support stereo, so do nothing