File: ether6.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 (438 lines) | stat: -rw-r--r-- 9,620 bytes parent folder | download | duplicates (2)
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
/*
 *  Hamlib Ether6 backend - main file
 *  Copyright (c) 2001-2009 by Stephane Fillod
 *  Copyright (c) 2013 by Jonny Röker <Jonny.Roeker@t-online.de>
 *
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library 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
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; 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>  /* String function definitions */

#include <hamlib/rotator.h>
#include "serial.h"
#include "register.h"
#include "idx_builtin.h"

#include "ether6.h"

#define CMD_MAX 32
#define BUF_MAX 64

#define ETHER_LEVELS ROT_LEVEL_SPEED

/*
 * Helper function with protocol return code parsing
 */
static int ether_transaction(ROT *rot, char *cmd, int len, char *buf)
{
    int ret;
    hamlib_port_t *rotp = ROTPORT(rot);

    ret = write_block(rotp, (unsigned char *) cmd, len);
    rig_debug(RIG_DEBUG_VERBOSE, "function %s(1): ret=%d || send=%s\n", __func__,
              ret, cmd);

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

    ret = read_string(rotp, (unsigned char *) buf, BUF_MAX,
                      "\n", sizeof("\n"), 0, 1);
    rig_debug(RIG_DEBUG_VERBOSE, "function %s(2): ret=%d || receive=%s\n", __func__,
              ret, buf);

    if (ret < 0)
    {
        return ret;
    }

    if (!memcmp(buf, ROTORCTL_RET, strlen(ROTORCTL_RET)))
    {
        rig_debug(RIG_DEBUG_VERBOSE, "function %s(2a): receive=%s\n", __func__, buf);
        return RIG_OK;
    }

    if (!memcmp(buf, NETROTCTL_RET, strlen(NETROTCTL_RET)))
    {
        int rv = atoi(buf + strlen(NETROTCTL_RET));
        rig_debug(RIG_DEBUG_VERBOSE, "function %s(2): ret=%d || receive=%d\n", __func__,
                  ret, rv);
        return atoi(buf + strlen(NETROTCTL_RET));
    }

    return ret;
}

static int ether_rot_open(ROT *rot)
{
    int ret;
    int sval;
    float min_az, max_az, min_el, max_el;
    struct rot_state *rs = ROTSTATE(rot);

    char cmd[CMD_MAX];
    char buf[BUF_MAX];

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);
    /* elevation not need */

    SNPRINTF(cmd, sizeof(cmd), "rotor state\n");
    /*-180/180 0/90*/

    ret = ether_transaction(rot, cmd, strlen(cmd), buf);

    if (ret <= 0)
    {
        return (ret < 0) ? ret : -RIG_EPROTO;
    }

    sval = sscanf(buf, "%f/%f %f/%f", &min_az, &max_az, &min_el, &max_el);
    rs->min_az = min_az;
    rs->max_az = max_az;
    rs->min_el = min_el;
    rs->max_el = max_el;
    rig_debug(RIG_DEBUG_VERBOSE, "ret(%d)%f/%f %f/%f\n", sval, rs->min_az,
              rs->max_az, rs->min_el, rs->max_el);

    return RIG_OK;
}

static int ether_rot_close(ROT *rot)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    /* clean signoff, no read back */
    write_block(ROTPORT(rot), (unsigned char *) "\n", 1);

    return RIG_OK;
}

static int ether_rot_set_position(ROT *rot, azimuth_t az, elevation_t el)
{
    int ret;
    char cmd[CMD_MAX];
    char buf[BUF_MAX];

    rig_debug(RIG_DEBUG_VERBOSE, "%s called: %f %f\n", __func__,
              az, el);

    SNPRINTF(cmd, sizeof(cmd), "rotor move %d %d\n", (int)az, (int)el);

    ret = ether_transaction(rot, cmd, strlen(cmd), buf);

    if (ret > 0)
    {
        return -RIG_EPROTO;
    }
    else
    {
        return ret;
    }
}

static int ether_rot_get_position(ROT *rot, azimuth_t *az, elevation_t *el)
{
    int ret, sval, speed, adv;
    char cmd[CMD_MAX];
    char buf[BUF_MAX];
    char mv[5];

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    SNPRINTF(cmd, sizeof(cmd), "rotor status\n");

    ret = ether_transaction(rot, cmd, strlen(cmd), buf);

    if (ret <= 0)
    {
        return (ret < 0) ? ret : -RIG_EPROTO;
    }

    // example "hold,az=87,el=0,v=8,ad0=346"
    sval = sscanf(buf, "%4s az=%f el=%f v=%d ad0=%d", mv, az, el, &speed, &adv);
    rig_debug(RIG_DEBUG_VERBOSE, "az=%f el=%f mv=%s ad(az)=%d\n", *az, *el, mv,
              adv);

    if (sval == 5)
    {
        return RIG_OK;
    }
    else
    {
        return -RIG_EPROTO;
    }
}

/**
* stop the rotor
*/
static int ether_rot_stop(ROT *rot)
{
    int ret;
    char cmd[CMD_MAX];
    char buf[BUF_MAX];

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    SNPRINTF(cmd, sizeof(cmd), "rotor stop\n");

    ret = ether_transaction(rot, cmd, strlen(cmd), buf);

    if (ret > 0)
    {
        return -RIG_EPROTO;
    }
    else
    {
        return ret;
    }
}


/**
* park the rotor
*/
static int ether_rot_park(ROT *rot)
{
    int ret;
    char cmd[CMD_MAX];
    char buf[BUF_MAX];

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    SNPRINTF(cmd, sizeof(cmd), "rotor park\n");

    ret = ether_transaction(rot, cmd, strlen(cmd), buf);

    if (ret > 0)
    {
        return -RIG_EPROTO;
    }
    else
    {
        return ret;
    }
}

static int ether_rot_reset(ROT *rot, rot_reset_t reset)
{
    int ret;
    char cmd[CMD_MAX];
    char buf[BUF_MAX];

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    SNPRINTF(cmd, sizeof(cmd), "reset\n");

    ret = ether_transaction(rot, cmd, strlen(cmd), buf);

    if (ret > 0)
    {
        return -RIG_EPROTO;
    }
    else
    {
        return ret;
    }
}

/**
* call rotor cw or rotor ccw
* if direction value 0 turn cw and if direction value 1 turn ccw
*/
static int ether_rot_move(ROT *rot, int direction, int speed)
{
    struct rot_state *rs = ROTSTATE(rot);
    int ret;
    char cmd[CMD_MAX];
    char buf[BUF_MAX];
    int ether_speed;

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    if (speed == ROT_SPEED_NOCHANGE)
    {
        ether_speed = rs->current_speed;
    }
    else
    {
        if (speed < 1 || speed > 100)
        {
            rig_debug(RIG_DEBUG_ERR, "%s: Invalid speed value (1-100)! (%d)\n", __func__,
                      speed);
            return -RIG_EINVAL;
        }

        rs->current_speed = speed;
        ether_speed = speed;
    }

    if (direction == 0)
    {
        SNPRINTF(cmd, sizeof(cmd), "rotor cw %d\n", ether_speed);
    }
    else
    {
        SNPRINTF(cmd, sizeof(cmd), "rotor ccw %d\n", ether_speed);
    }

    ret = ether_transaction(rot, cmd, strlen(cmd), buf);

    if (ret > 0)
    {
        return -RIG_EPROTO;
    }
    else
    {
        return ret;
    }
}


static int ether_rot_get_level(ROT *rot, setting_t level, value_t *val)
{
    const struct rot_state *rs = ROTSTATE(rot);

    rig_debug(RIG_DEBUG_VERBOSE, "%s called: %s\n", __func__, rot_strlevel(level));

    switch (level)
    {
    case ROT_LEVEL_SPEED:
        val->i = rs->current_speed;
        break;

    default:
        return -RIG_ENAVAIL;
    }

    return RIG_OK;
}


static int ether_rot_set_level(ROT *rot, setting_t level, value_t val)
{
    struct rot_state *rs = ROTSTATE(rot);

    rig_debug(RIG_DEBUG_VERBOSE, "%s called: %s\n", __func__, rot_strlevel(level));

    switch (level)
    {
    case ROT_LEVEL_SPEED:
    {
        int speed = val.i;

        if (speed < 1)
        {
            speed = 1;
        }
        else if (speed > 100)
        {
            speed = 100;
        }

        rs->current_speed = speed;
        break;
    }

    default:
        return -RIG_ENAVAIL;
    }

    return RIG_OK;
}



static const char *ether_rot_get_info(ROT *rot)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    return "ip rotator via ethersex";
}


static int ether_rot_init(ROT *rot)
{
    struct rot_state *rs = ROTSTATE(rot);

    rig_debug(RIG_DEBUG_VERBOSE, "%s called\n", __func__);

    // Set default speed to half of maximum
    rs->current_speed = 00;

    return RIG_OK;
}


/*
 * Dummy rotator capabilities.
 */

const struct rot_caps ether6_rot_caps =
{
    ROT_MODEL(ROT_MODEL_ETHER6),
    .model_name =     "Ether6 (via ethernet)",
    .mfg_name =       "DG9OAA",
    .version =        "20220109.0",
    .copyright =      "LGPL",
    .status =         RIG_STATUS_STABLE,
    .rot_type =       ROT_FLAG_AZIMUTH,
    .port_type =      RIG_PORT_NETWORK,
    .timeout = 5000,
    .retry =   3,

    .min_az =     0.,
    .max_az =     360,
    .min_el =     0,
    .max_el =     90,

    .priv =  NULL,    /* priv */

    .has_get_level =  ETHER_LEVELS,
    .has_set_level =  ROT_LEVEL_SET(ETHER_LEVELS),

    .level_gran =      { [ROT_LVL_SPEED] = { .min = { .i = 0 }, .max = { .i = 9999 }, .step = { .i = 1 } } },

    .rot_init     =  ether_rot_init,
    .rot_cleanup  =  NULL,

    .rot_open     =  ether_rot_open,
    .rot_close    =  ether_rot_close,

    .set_position =  ether_rot_set_position,
    .get_position =  ether_rot_get_position,
    .park         =  ether_rot_park,
    .stop         =  ether_rot_stop,
    .reset        =  ether_rot_reset,
    .move         =  ether_rot_move,
    .get_level    =  ether_rot_get_level,
    .set_level    =  ether_rot_set_level,

    .get_info     =  ether_rot_get_info,
};

DECLARE_INITROT_BACKEND(ether6)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s: _init called\n", __func__);

    rot_register(&ether6_rot_caps);

    return RIG_OK;
}