File: radio-conf.c

package info (click to toggle)
gpredict 2.3-72-gc596101-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,264 kB
  • sloc: ansic: 39,918; makefile: 469; python: 143; sh: 85
file content (300 lines) | stat: -rw-r--r-- 9,283 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
    Gpredict: Real-time satellite tracking and orbit prediction program

    Copyright (C)  2001-2015  Alexandru Csete.

    Authors: Alexandru Csete <oz9aec@gmail.com>

    Comments, questions and bugreports should be submitted via
    http://sourceforge.net/projects/gpredict/
    More details can be found at the project home page:

            http://gpredict.oz9aec.net/
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    along with this program; if not, visit http://www.fsf.org/
*/

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "sat-log.h"
#include "compat.h"
#include "gpredict-utils.h"

#include "radio-conf.h"

#define GROUP           "Radio"
#define KEY_HOST        "Host"
#define KEY_PORT        "Port"
#define KEY_CYCLE       "Cycle"
#define KEY_LO          "LO"
#define KEY_LOUP        "LO_UP"
#define KEY_TYPE        "Type"
#define KEY_PTT         "PTT"
#define KEY_VFO_DOWN    "VFO_DOWN"
#define KEY_VFO_UP      "VFO_UP"
#define KEY_SIG_AOS     "SIGNAL_AOS"
#define KEY_SIG_LOS     "SIGNAL_LOS"

#define DEFAULT_CYCLE_MS    1000

/**
 * \brief Read radio configuration.
 * \param conf Pointer to a radio_conf_t structure where the data will be
 *             stored.
 * \return TRUE if the configuration was read successfully, FALSE if an
 *         error has occurred.
 * 
 * This function reads a radio configuration from a .rig file into conf.
 * conf->name must contain the file name of the configuration (no path, just
 * file name).
 */
gboolean radio_conf_read(radio_conf_t * conf)
{
    GKeyFile       *cfg = NULL;
    gchar          *confdir;
    gchar          *fname;
    GError         *error = NULL;


    if (conf->name == NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: NULL configuration name!"), __func__);
        return FALSE;
    }

    confdir = get_hwconf_dir();
    fname = g_strconcat(confdir, G_DIR_SEPARATOR_S, conf->name, ".rig", NULL);
    g_free(confdir);

    /* open .rig file */
    cfg = g_key_file_new();
    g_key_file_load_from_file(cfg, fname, 0, NULL);

    if (cfg == NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Could not load file %s\n"), __func__, fname);
        g_free(fname);

        return FALSE;
    }

    g_free(fname);

    /* read parameters */
    conf->host = g_key_file_get_string(cfg, GROUP, KEY_HOST, &error);
    if (error != NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Error reading radio conf from %s (%s)."),
                    __func__, conf->name, error->message);
        g_clear_error(&error);
        g_key_file_free(cfg);
        return FALSE;
    }

    conf->port = g_key_file_get_integer(cfg, GROUP, KEY_PORT, &error);
    if (error != NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Error reading radio conf from %s (%s)."),
                    __func__, conf->name, error->message);
        g_clear_error(&error);
        g_key_file_free(cfg);
        return FALSE;
    }

    /* cycle period is only saved if not default */
    if (g_key_file_has_key(cfg, GROUP, KEY_CYCLE, NULL))
    {
        conf->cycle = g_key_file_get_integer(cfg, GROUP, KEY_CYCLE, &error);
        if (error != NULL)
        {
            sat_log_log(SAT_LOG_LEVEL_ERROR,
                        _("%s: Error reading radio conf from %s (%s)."),
                        __func__, conf->name, error->message);
            g_clear_error(&error);
            g_key_file_free(cfg);
            return FALSE;
        }
        if (conf->cycle < 10)
            conf->cycle = 10;
    }
    else
    {
        conf->cycle = DEFAULT_CYCLE_MS;
    }

    /* KEY_LO is optional */
    if (g_key_file_has_key(cfg, GROUP, KEY_LO, NULL))
    {
        conf->lo = g_key_file_get_double(cfg, GROUP, KEY_LO, &error);
        if (error != NULL)
        {
            sat_log_log(SAT_LOG_LEVEL_ERROR,
                        _("%s: Error reading radio conf from %s (%s)."),
                        __func__, conf->name, error->message);
            g_clear_error(&error);
            g_key_file_free(cfg);
            return FALSE;
        }
    }
    else
    {
        conf->lo = 0.0;
    }

    /* KEY_LOUP is optional */
    if (g_key_file_has_key(cfg, GROUP, KEY_LOUP, NULL))
    {
        conf->loup = g_key_file_get_double(cfg, GROUP, KEY_LOUP, &error);
        if (error != NULL)
        {
            sat_log_log(SAT_LOG_LEVEL_ERROR,
                        _("%s: Error reading radio conf from %s (%s)."),
                        __func__, conf->name, error->message);
            g_clear_error(&error);
            g_key_file_free(cfg);
            return FALSE;
        }
    }
    else
    {
        conf->loup = 0.0;
    }

    /* Radio type */
    conf->type = g_key_file_get_integer(cfg, GROUP, KEY_TYPE, &error);
    if (error != NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Error reading radio conf from %s (%s)."),
                    __func__, conf->name, error->message);
        g_clear_error(&error);
        g_key_file_free(cfg);
        return FALSE;
    }

    /* PTT Type */
    conf->ptt = g_key_file_get_integer(cfg, GROUP, KEY_PTT, &error);
    if (error != NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: Error reading radio conf from %s (%s)."),
                    __func__, conf->name, error->message);
        g_clear_error(&error);
        g_key_file_free(cfg);
        return FALSE;
    }

    /* VFO up and down, only if radio is full-duplex */
    if (conf->type == RIG_TYPE_DUPLEX)
    {
        /* downlink */
        if (g_key_file_has_key(cfg, GROUP, KEY_VFO_DOWN, NULL))
        {
            conf->vfoDown =
                g_key_file_get_integer(cfg, GROUP, KEY_VFO_DOWN, &error);
            if (error != NULL)
            {
                sat_log_log(SAT_LOG_LEVEL_ERROR,
                            _("%s: Error reading radio conf from %s (%s)."),
                            __func__, conf->name, error->message);
                g_clear_error(&error);
                conf->vfoDown = VFO_SUB;
            }
        }

        /* uplink */
        if (g_key_file_has_key(cfg, GROUP, KEY_VFO_UP, NULL))
        {
            conf->vfoUp =
                g_key_file_get_integer(cfg, GROUP, KEY_VFO_UP, &error);
            if (error != NULL)
            {
                sat_log_log(SAT_LOG_LEVEL_ERROR,
                            _("%s: Error reading radio conf from %s (%s)."),
                            __func__, conf->name, error->message);
                g_clear_error(&error);
                conf->vfoUp = VFO_MAIN;
            }
        }
    }

    /* Signal AOS and LOS */
    conf->signal_aos = g_key_file_get_boolean(cfg, GROUP, KEY_SIG_AOS, NULL);
    conf->signal_los = g_key_file_get_boolean(cfg, GROUP, KEY_SIG_LOS, NULL);

    g_key_file_free(cfg);
    sat_log_log(SAT_LOG_LEVEL_INFO,
                _("%s: Read radio configuration %s"), __func__, conf->name);

    return TRUE;
}

/**
 * \brief Save radio configuration.
 * \param conf Pointer to the radio configuration.
 * 
 * This function saves the radio configuration stored in conf to a
 * .rig file. conf->name must contain the file name of the configuration
 * (no path, just file name).
 */
void radio_conf_save(radio_conf_t * conf)
{
    GKeyFile       *cfg = NULL;
    gchar          *confdir;
    gchar          *fname;

    if (conf->name == NULL)
    {
        sat_log_log(SAT_LOG_LEVEL_ERROR,
                    _("%s: NULL configuration name!"), __func__);
        return;
    }

    /* create a config structure */
    cfg = g_key_file_new();

    g_key_file_set_string(cfg, GROUP, KEY_HOST, conf->host);
    g_key_file_set_integer(cfg, GROUP, KEY_PORT, conf->port);
    g_key_file_set_double(cfg, GROUP, KEY_LO, conf->lo);
    g_key_file_set_double(cfg, GROUP, KEY_LOUP, conf->loup);
    g_key_file_set_integer(cfg, GROUP, KEY_TYPE, conf->type);
    g_key_file_set_integer(cfg, GROUP, KEY_PTT, conf->ptt);

    if (conf->cycle == DEFAULT_CYCLE_MS)
        g_key_file_remove_key(cfg, GROUP, KEY_CYCLE, NULL);
    else
        g_key_file_set_integer(cfg, GROUP, KEY_CYCLE, conf->cycle);

    if (conf->type == RIG_TYPE_DUPLEX)
    {
        g_key_file_set_integer(cfg, GROUP, KEY_VFO_UP, conf->vfoUp);
        g_key_file_set_integer(cfg, GROUP, KEY_VFO_DOWN, conf->vfoDown);
    }

    g_key_file_set_boolean(cfg, GROUP, KEY_SIG_AOS, conf->signal_aos);
    g_key_file_set_boolean(cfg, GROUP, KEY_SIG_LOS, conf->signal_los);

    confdir = get_hwconf_dir();
    fname = g_strconcat(confdir, G_DIR_SEPARATOR_S, conf->name, ".rig", NULL);
    g_free(confdir);

    gpredict_save_key_file(cfg, fname);

    g_free(fname);
    g_key_file_free(cfg);
}