File: rigsmtr.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (512 lines) | stat: -rw-r--r-- 12,320 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * rigsmtr.c - (C) Stephane Fillod 2007
 *
 * This program output S-meter vs. Azimuth curve using Hamlib.
 *
 *
 *   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, write to the Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <getopt.h>

#include <hamlib/rig.h>
#include <hamlib/rotator.h>
#include "misc.h"
#include "riglist.h"
#include "rotlist.h"

/*
 * Prototypes
 */
static void usage();
static void version();
static int set_conf_rig(RIG *rig, char *conf_parms);
static int set_conf_rot(ROT *rot, char *conf_parms);

/*
 * Reminder: when adding long options,
 *   keep up to date SHORT_OPTIONS, usage()'s output and man page. thanks.
 * NB: do NOT use -W since it's reserved by POSIX.
 */
#define SHORT_OPTIONS "m:r:s:c:C:M:R:S:N:vhV"
static struct option long_options[] =
{
    {"model",               1, 0, 'm'},
    {"rig-file",            1, 0, 'r'},
    {"serial-speed",        1, 0, 's'},
    {"civaddr",             1, 0, 'c'},
    {"set-conf",            1, 0, 'C'},
    {"rot-model",           1, 0, 'M'},
    {"rot-file",            1, 0, 'R'},
    {"rot-serial-speed",    1, 0, 'S'},
    {"rot-set-conf",        1, 0, 'N'},
    {"verbose",             0, 0, 'v'},
    {"help",                0, 0, 'h'},
    {"version",             0, 0, 'V'},
    {0, 0, 0, 0}
};

#define MAXCONFLEN 2048


int main(int argc, char *argv[])
{
    RIG *rig;       /* handle to rig (instance) */
    ROT *rot;       /* handle to rotator (instance) */
    rig_model_t rig_model = RIG_MODEL_DUMMY;
    rot_model_t rot_model = ROT_MODEL_DUMMY;

    int retcode;        /* generic return code from functions */

    int verbose = 0;
    const char *rig_file = NULL, *rot_file = NULL;
    int serial_rate = 0;
    int rot_serial_rate = 0;
    char *civaddr = NULL;   /* NULL means no need to set conf */
    char rig_conf_parms[MAXCONFLEN] = "";
    char rot_conf_parms[MAXCONFLEN] = "";

    /* int with_rot = 1; */
    azimuth_t azimuth;
    elevation_t elevation;
    unsigned step = 1000000;    /* 1e6 us */

    while (1)
    {
        int c;
        int option_index = 0;
        char dummy[2];

        c = getopt_long(argc, argv, SHORT_OPTIONS, long_options, &option_index);

        if (c == -1)
        {
            break;
        }

        switch (c)
        {
        case 'h':
            usage();
            exit(0);

        case 'V':
            version();
            exit(0);

        case 'm':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            rig_model = atoi(optarg);
            break;

        case 'r':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            rig_file = optarg;
            break;

        case 'c':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            civaddr = optarg;
            break;

        case 's':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            if (sscanf(optarg, "%d%1s", &serial_rate, dummy) != 1)
            {
                fprintf(stderr, "Invalid baud rate of %s\n", optarg);
                exit(1);
            }

            break;

        case 'C':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            if (*rig_conf_parms != '\0')
            {
                strcat(rig_conf_parms, ",");
            }

            if (strlen(rig_conf_parms) + strlen(optarg) > MAXCONFLEN - 24)
            {
                printf("Length of conf_parms exceeds internal maximum of %d\n",
                       MAXCONFLEN - 24);
                return 1;
            }

            strncat(rig_conf_parms, optarg, MAXCONFLEN - strlen(rig_conf_parms) - 1);
            break;

        case 'M':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            rot_model = atoi(optarg);
            break;

        case 'R':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            rot_file = optarg;
            break;

        case 'S':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            rot_serial_rate = atoi(optarg);
            break;

        case 'N':
            if (!optarg)
            {
                usage();    /* wrong arg count */
                exit(1);
            }

            if (*rot_conf_parms != '\0')
            {
                strcat(rot_conf_parms, ",");
            }

            strncat(rot_conf_parms, optarg, MAXCONFLEN - strlen(rot_conf_parms) - 1);
            break;

        case 'v':
            verbose++;
            break;

        default:
            usage();    /* unknown option? */
            exit(1);
        }
    }

    rig_set_debug(verbose < 2 ? RIG_DEBUG_WARN : verbose);

    rig_debug(RIG_DEBUG_VERBOSE, "rigsmtr, %s\n", hamlib_version2);
    rig_debug(RIG_DEBUG_VERBOSE, "%s",
              "Report bugs to <hamlib-developer@lists.sourceforge.net>\n\n");

    /*
     * The radio
     */
    rig = rig_init(rig_model);

    if (!rig)
    {
        fprintf(stderr,
                "Unknown rig num %u, or initialization error.\n",
                rig_model);

        fprintf(stderr, "Please check with --list option.\n");
        exit(2);
    }

    retcode = set_conf_rig(rig, rig_conf_parms);

    if (retcode != RIG_OK)
    {
        fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
        exit(2);
    }

    if (rig_file)
    {
        strncpy(RIGPORT(rig)->pathname, rig_file, HAMLIB_FILPATHLEN - 1);
    }

    /* FIXME: bound checking and port type == serial */
    if (serial_rate != 0)
    {
        RIGPORT(rig)->parm.serial.rate = serial_rate;
    }

    if (civaddr)
    {
        rig_set_conf(rig, rig_token_lookup(rig, "civaddr"), civaddr);
    }


    if (!rig_has_get_level(rig, RIG_LEVEL_STRENGTH))
    {
        fprintf(stderr,
                "rig backend for %s could not get S-Meter"
                "or has insufficient capability\nSorry\n",
                rig->caps->model_name);
        exit(3);
    }

    retcode = rig_open(rig);

    if (retcode != RIG_OK)
    {
        fprintf(stderr, "rig_open: error = %s \n", rigerror(retcode));
        exit(2);
    }

    if (verbose > 0)
    {
        printf("Opened rig model %u, '%s'\n",
               rig->caps->rig_model,
               rig->caps->model_name);
    }

    /*
     * The rotator
     */
    rot = rot_init(rot_model);

    if (!rot)
    {
        fprintf(stderr,
                "Unknown rot num %d, or initialization error.\n",
                rot_model);

        fprintf(stderr, "Please check with --list option.\n");
        exit(2);
    }

    retcode = set_conf_rot(rot, rot_conf_parms);

    if (retcode != RIG_OK)
    {
        fprintf(stderr, "Config parameter error: %s\n", rigerror(retcode));
        exit(2);
    }

    if (rot_file)
    {
        strncpy(ROTPORT(rot)->pathname, rot_file, HAMLIB_FILPATHLEN - 1);
    }

    /* FIXME: bound checking and port type == serial */
    if (rot_serial_rate != 0)
    {
        ROTPORT(rot)->parm.serial.rate = rot_serial_rate;
    }

    retcode = rot_open(rot);

    if (retcode != RIG_OK && rot_model != ROT_MODEL_DUMMY)
    {
        fprintf(stderr, "rot_open: error = %s \n", rigerror(retcode));
        exit(2);
    }

    /* Commenting out to quell "set but not used" warning.
     * Enable when needed for further functionality.  -N0NB
     */
    /* if (rot_model == ROT_MODEL_DUMMY) */
    /*  with_rot = 1; */

    if (verbose > 0)
    {
        printf("Opened rotator model %d, '%s'\n",
               rot->caps->rot_model,
               rot->caps->model_name);
    }

    /*******************************/
    if (optind < argc)
    {
        step = atof(argv[optind]) * 1e6;
    }

    fprintf(stderr, "Setting rotator to azimuth %.1f°\n", ROTSTATE(rot)->min_az);
    rot_set_position(rot, ROTSTATE(rot)->min_az, 0);
    fprintf(stderr, "Wait for rotator to move...\n");
    rot_get_position(rot, &azimuth, &elevation);

    while (fabs(azimuth - ROTSTATE(rot)->min_az) > 1.)
    {
        rot_get_position(rot, &azimuth, &elevation);
        hl_usleep(step);
    }

    fprintf(stderr, "Now initiating full 360° rotation...\n");
    rot_set_position(rot, ROTSTATE(rot)->max_az, 0);

    /* TODO: check CW or CCW */
    /* disable AGC? */

    while (fabs(ROTSTATE(rot)->max_az - azimuth) > 1.)
    {
        value_t strength;

        rig_get_level(rig, RIG_VFO_CURR, RIG_LEVEL_STRENGTH, &strength);

        rot_get_position(rot, &azimuth, &elevation);

        printf("%.1f %d\n", azimuth, strength.i);
    }

    rig_close(rig);
    rot_close(rot);

    return 0;
}



void version()
{
    printf("rigsmtr, %s\n\n", hamlib_version2);
    printf("%s\n", hamlib_copyright);
}


void usage()
{
    printf("Usage: rigsmtr [OPTION]... [time]\n"
           "Input S-Meter vs Azimuth.\n\n");

    printf(
        "  -m, --model=ID                select radio model number. See model list (rigctl -l)\n"
        "  -r, --rig-file=DEVICE         set device of the radio to operate on\n"
        "  -s, --serial-speed=BAUD       set serial speed of the serial port\n"
        "  -c, --civaddr=ID              set CI-V address, decimal (for Icom rigs only)\n"
        "  -C, --set-conf=PARM=VAL[,...] set config parameters\n"
        "  -M, --rot-model=ID            select rotator model number. See model list (rotctl -l)\n"
        "  -R, --rot-file=DEVICE         set device of the rotator to operate on\n"
        "  -S, --rot-serial-speed=BAUD   set serial speed of the serial port\n"
        "  -N, --rot-set-conf=PARM=VAL   set rotator config parameters\n"
        "  -v, --verbose                 set verbose mode, cumulative (-v to -vvvvv)\n"
        "  -h, --help                    display this help and exit\n"
        "  -V, --version                 output version information and exit\n\n"
    );

    printf("\nReport bugs to <hamlib-developer@lists.sourceforge.net>.\n");

}


int set_conf_rig(RIG *rig, char *conf_parms)
{
    char *p;

    p = conf_parms;

    while (p && *p != '\0')
    {
        int ret;
        char *q, *n = NULL;
        /* FIXME: left hand value of = cannot be null */
        q = strchr(p, '=');

        if (!q)
        {
            return RIG_EINVAL;

            *q++ = '\0';
            n = strchr(q, ',');

            if (n)
            {
                *n++ = '\0';
            }
        }

        ret = rig_set_conf(rig, rig_token_lookup(rig, p), q);

        if (ret != RIG_OK)
        {
            return ret;
        }

        p = n;
    }

    return RIG_OK;
}


int set_conf_rot(ROT *rot, char *conf_parms)
{
    char *p;

    p = conf_parms;

    while (p && *p != '\0')
    {
        char *q, *n = NULL;
        int ret;
        /* FIXME: left hand value of = cannot be null */
        q = strchr(p, '=');

        if (q)
        {
            *q++ = '\0';

            n = strchr(q, ',');

            if (n)
            {
                *n++ = '\0';
            }
        }

        ret = rot_set_conf(rot, rot_token_lookup(rot, p), q);

        if (ret != RIG_OK)
        {
            return ret;
        }

        p = n;
    }

    return RIG_OK;
}