1 /*
2     Gpredict: Real-time satellite tracking and orbit prediction program
3 
4     Copyright (C)  2001-2015  Alexandru Csete.
5 
6     Authors: Alexandru Csete <oz9aec@gmail.com>
7 
8     Comments, questions and bugreports should be submitted via
9     http://sourceforge.net/projects/gpredict/
10     More details can be found at the project home page:
11 
12             http://gpredict.oz9aec.net/
13 
14     This program is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License as published by
16     the Free Software Foundation; either version 2 of the License, or
17     (at your option) any later version.
18 
19     This program is distributed in the hope that it will be useful,
20     but WITHOUT ANY WARRANTY; without even the implied warranty of
21     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     GNU General Public License for more details.
23 
24     You should have received a copy of the GNU General Public License
25     along with this program; if not, visit http://www.fsf.org/
26 */
27 
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30 #include "sat-log.h"
31 #include "compat.h"
32 #include "gpredict-utils.h"
33 
34 #include "radio-conf.h"
35 
36 #define GROUP           "Radio"
37 #define KEY_HOST        "Host"
38 #define KEY_PORT        "Port"
39 #define KEY_LO          "LO"
40 #define KEY_LOUP        "LO_UP"
41 #define KEY_TYPE        "Type"
42 #define KEY_PTT         "PTT"
43 #define KEY_VFO_DOWN    "VFO_DOWN"
44 #define KEY_VFO_UP      "VFO_UP"
45 #define KEY_SIG_AOS     "SIGNAL_AOS"
46 #define KEY_SIG_LOS     "SIGNAL_LOS"
47 
48 /**
49  * \brief Read radio configuration.
50  * \param conf Pointer to a radio_conf_t structure where the data will be
51  *             stored.
52  * \return TRUE if the configuration was read successfully, FALSE if an
53  *         error has occurred.
54  *
55  * This function reads a radio configuration from a .rig file into conf.
56  * conf->name must contain the file name of the configuration (no path, just
57  * file name).
58  */
radio_conf_read(radio_conf_t * conf)59 gboolean radio_conf_read(radio_conf_t * conf)
60 {
61     GKeyFile       *cfg = NULL;
62     gchar          *confdir;
63     gchar          *fname;
64     GError         *error = NULL;
65 
66 
67     if (conf->name == NULL)
68     {
69         sat_log_log(SAT_LOG_LEVEL_ERROR,
70                     _("%s: NULL configuration name!"), __func__);
71         return FALSE;
72     }
73 
74     confdir = get_hwconf_dir();
75     fname = g_strconcat(confdir, G_DIR_SEPARATOR_S, conf->name, ".rig", NULL);
76     g_free(confdir);
77 
78     /* open .rig file */
79     cfg = g_key_file_new();
80     g_key_file_load_from_file(cfg, fname, 0, NULL);
81 
82     if (cfg == NULL)
83     {
84         sat_log_log(SAT_LOG_LEVEL_ERROR,
85                     _("%s: Could not load file %s\n"), __func__, fname);
86         g_free(fname);
87 
88         return FALSE;
89     }
90 
91     g_free(fname);
92 
93     /* read parameters */
94     conf->host = g_key_file_get_string(cfg, GROUP, KEY_HOST, &error);
95     if (error != NULL)
96     {
97         sat_log_log(SAT_LOG_LEVEL_ERROR,
98                     _("%s: Error reading radio conf from %s (%s)."),
99                     __func__, conf->name, error->message);
100         g_clear_error(&error);
101         g_key_file_free(cfg);
102         return FALSE;
103     }
104 
105     conf->port = g_key_file_get_integer(cfg, GROUP, KEY_PORT, &error);
106     if (error != NULL)
107     {
108         sat_log_log(SAT_LOG_LEVEL_ERROR,
109                     _("%s: Error reading radio conf from %s (%s)."),
110                     __func__, conf->name, error->message);
111         g_clear_error(&error);
112         g_key_file_free(cfg);
113         return FALSE;
114     }
115 
116     /* KEY_LO is optional */
117     if (g_key_file_has_key(cfg, GROUP, KEY_LO, NULL))
118     {
119         conf->lo = g_key_file_get_double(cfg, GROUP, KEY_LO, &error);
120         if (error != NULL)
121         {
122             sat_log_log(SAT_LOG_LEVEL_ERROR,
123                         _("%s: Error reading radio conf from %s (%s)."),
124                         __func__, conf->name, error->message);
125             g_clear_error(&error);
126             g_key_file_free(cfg);
127             return FALSE;
128         }
129     }
130     else
131     {
132         conf->lo = 0.0;
133     }
134 
135     /* KEY_LOUP is optional */
136     if (g_key_file_has_key(cfg, GROUP, KEY_LOUP, NULL))
137     {
138         conf->loup = g_key_file_get_double(cfg, GROUP, KEY_LOUP, &error);
139         if (error != NULL)
140         {
141             sat_log_log(SAT_LOG_LEVEL_ERROR,
142                         _("%s: Error reading radio conf from %s (%s)."),
143                         __func__, conf->name, error->message);
144             g_clear_error(&error);
145             g_key_file_free(cfg);
146             return FALSE;
147         }
148     }
149     else
150     {
151         conf->loup = 0.0;
152     }
153 
154     /* Radio type */
155     conf->type = g_key_file_get_integer(cfg, GROUP, KEY_TYPE, &error);
156     if (error != NULL)
157     {
158         sat_log_log(SAT_LOG_LEVEL_ERROR,
159                     _("%s: Error reading radio conf from %s (%s)."),
160                     __func__, conf->name, error->message);
161         g_clear_error(&error);
162         g_key_file_free(cfg);
163         return FALSE;
164     }
165 
166     /* PTT Type */
167     conf->ptt = g_key_file_get_integer(cfg, GROUP, KEY_PTT, &error);
168     if (error != NULL)
169     {
170         sat_log_log(SAT_LOG_LEVEL_ERROR,
171                     _("%s: Error reading radio conf from %s (%s)."),
172                     __func__, conf->name, error->message);
173         g_clear_error(&error);
174         g_key_file_free(cfg);
175         return FALSE;
176     }
177 
178     /* VFO up and down, only if radio is full-duplex */
179     if (conf->type == RIG_TYPE_DUPLEX)
180     {
181         /* downlink */
182         if (g_key_file_has_key(cfg, GROUP, KEY_VFO_DOWN, NULL))
183         {
184             conf->vfoDown =
185                 g_key_file_get_integer(cfg, GROUP, KEY_VFO_DOWN, &error);
186             if (error != NULL)
187             {
188                 sat_log_log(SAT_LOG_LEVEL_ERROR,
189                             _("%s: Error reading radio conf from %s (%s)."),
190                             __func__, conf->name, error->message);
191                 g_clear_error(&error);
192                 conf->vfoDown = VFO_SUB;
193             }
194         }
195 
196         /* uplink */
197         if (g_key_file_has_key(cfg, GROUP, KEY_VFO_UP, NULL))
198         {
199             conf->vfoUp =
200                 g_key_file_get_integer(cfg, GROUP, KEY_VFO_UP, &error);
201             if (error != NULL)
202             {
203                 sat_log_log(SAT_LOG_LEVEL_ERROR,
204                             _("%s: Error reading radio conf from %s (%s)."),
205                             __func__, conf->name, error->message);
206                 g_clear_error(&error);
207                 conf->vfoUp = VFO_MAIN;
208             }
209         }
210     }
211 
212     /* Signal AOS and LOS */
213     conf->signal_aos = g_key_file_get_boolean(cfg, GROUP, KEY_SIG_AOS, NULL);
214     conf->signal_los = g_key_file_get_boolean(cfg, GROUP, KEY_SIG_LOS, NULL);
215 
216     g_key_file_free(cfg);
217     sat_log_log(SAT_LOG_LEVEL_INFO,
218                 _("%s: Read radio configuration %s"), __func__, conf->name);
219 
220     return TRUE;
221 }
222 
223 /**
224  * \brief Save radio configuration.
225  * \param conf Pointer to the radio configuration.
226  *
227  * This function saves the radio configuration stored in conf to a
228  * .rig file. conf->name must contain the file name of the configuration
229  * (no path, just file name).
230  */
radio_conf_save(radio_conf_t * conf)231 void radio_conf_save(radio_conf_t * conf)
232 {
233     GKeyFile       *cfg = NULL;
234     gchar          *confdir;
235     gchar          *fname;
236 
237     if (conf->name == NULL)
238     {
239         sat_log_log(SAT_LOG_LEVEL_ERROR,
240                     _("%s: NULL configuration name!"), __func__);
241         return;
242     }
243 
244     /* create a config structure */
245     cfg = g_key_file_new();
246 
247     g_key_file_set_string(cfg, GROUP, KEY_HOST, conf->host);
248     g_key_file_set_integer(cfg, GROUP, KEY_PORT, conf->port);
249     g_key_file_set_double(cfg, GROUP, KEY_LO, conf->lo);
250     g_key_file_set_double(cfg, GROUP, KEY_LOUP, conf->loup);
251     g_key_file_set_integer(cfg, GROUP, KEY_TYPE, conf->type);
252     g_key_file_set_integer(cfg, GROUP, KEY_PTT, conf->ptt);
253 
254     if (conf->type == RIG_TYPE_DUPLEX)
255     {
256         g_key_file_set_integer(cfg, GROUP, KEY_VFO_UP, conf->vfoUp);
257         g_key_file_set_integer(cfg, GROUP, KEY_VFO_DOWN, conf->vfoDown);
258     }
259 
260     g_key_file_set_boolean(cfg, GROUP, KEY_SIG_AOS, conf->signal_aos);
261     g_key_file_set_boolean(cfg, GROUP, KEY_SIG_LOS, conf->signal_los);
262 
263     confdir = get_hwconf_dir();
264     fname = g_strconcat(confdir, G_DIR_SEPARATOR_S, conf->name, ".rig", NULL);
265     g_free(confdir);
266 
267     gpredict_save_key_file(cfg, fname);
268 
269     g_free(fname);
270     g_key_file_free(cfg);
271 }
272