File: wj.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 (469 lines) | stat: -rw-r--r-- 9,537 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
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
/*
 *  Hamlib Watkins-Johnson backend - main file
 *  Copyright (c) 2004 by Stephane Fillod
 *
 *
 *   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 "hamlib/rig.h"
#include "serial.h"
#include "misc.h"
#include "register.h"

#include "wj.h"


#define CMDSZ 10

const struct confparams wj_cfg_params[] =
{
    {
        TOK_RIGID, "receiver_id", "receiver ID", "receiver ID",
        "0", RIG_CONF_NUMERIC, { .n = { 0, 15, 1 } }
    },
    { RIG_CONF_END, NULL, }
};


/*
 * modes
 */
#define MD_AM   0
#define MD_FM   1
#define MD_CW   2
#define MD_VCW  3   /* BFO variable */
#define MD_ISB  4
#define MD_LSB  5
#define MD_USB  6
#define MD_AMNL 7


/*
 * wj_transaction
 *
 * I'm not sure how monitor protocol works, whether you have
 * to send the full frame, or just the modal byte. --SF
 *
 * TODO: decode the whole reply, and maybe do some caching
 */
static int wj_transaction(RIG *rig, int monitor)
{
    struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;
    hamlib_port_t *rp = RIGPORT(rig);

    unsigned char buf[CMDSZ] = { 0x8, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    unsigned char rxbuf[CMDSZ];
    unsigned char freqbuf[4];
    unsigned wj_agc, wj_mode, wj_width, wj_bfo, wj_rfgain;
    int retval;

    if (monitor)
    {
        buf[1] |= 0x40;    /* Monitor+AGC dump */
    }
    else
    {
        buf[0] |= 0x40;    /* Command */
    }

    buf[0] |= priv->receiver_id & 0x0f;

    /* tuned frequency */
    to_bcd_be(freqbuf, priv->freq / 10, 7);
    buf[1] |= freqbuf[0] & 0x3f;
    buf[2] |= freqbuf[1] >> 1;
    buf[3] |= ((freqbuf[1] & 0x1) << 6) | (freqbuf[2] >> 2);
    buf[4] |= ((freqbuf[2] & 0x2) << 5) | (freqbuf[3] >> 3);

    /* gain mode */
    switch (priv->agc.i)
    {
    case RIG_AGC_SLOW: wj_agc = 0; break;   /* slow, 2s */

    case RIG_AGC_OFF: wj_agc = 1; break;    /* "not used" */

    case RIG_AGC_FAST: wj_agc = 2; break;   /* normal, 0.1s */

    case RIG_AGC_USER: wj_agc = 3; break;   /* manual */

    default: return -RIG_EINVAL;
    }

    buf[4] |= wj_agc & 0x1;
    buf[5] |= (wj_agc & 0x2) << 5;

    /* IF BW */
    switch (priv->width)
    {
    case  200:
    case 1000: wj_width = 0; break; /* spare */

    case  500: wj_width = 1; break;

    case 2000: wj_width = 2; break;

    case 4000: wj_width = 3; break;

    case 8000: wj_width = 4; break;

    case 3000:
    case 6000:
    case 12000:
    case 16000: wj_width = 5; break;    /* spare */

    default:
        return -RIG_EINVAL;
    }

    buf[5] |= (wj_width & 0x7) << 3;

    /* Detection mode */
    switch (priv->mode)
    {
    case RIG_MODE_CW:   wj_mode = (priv->ifshift.i != 0) ? MD_VCW : MD_CW; break;

    case RIG_MODE_USB:  wj_mode = MD_USB; break;

    case RIG_MODE_LSB:  wj_mode = MD_LSB; break;

    case RIG_MODE_FM:   wj_mode = MD_FM; break;

    case RIG_MODE_AM:   wj_mode = MD_AM; break;

    case RIG_MODE_AMS:  wj_mode = MD_ISB; break;

    default:
        rig_debug(RIG_DEBUG_ERR, "%s: unsupported mode %s\n",
                  __func__, rig_strrmode(priv->mode));
        return -RIG_EINVAL;
    }

    buf[5] |= wj_mode & 0x7;

    /* BFO frequency, not sure though */
    wj_bfo = (priv->ifshift.i / 10) + 0x400; /* LSBit is 10Hz, +455kHz */
    buf[6] |= (wj_bfo >> 5) & 0x3f;
    buf[7] |= (wj_bfo & 0x1f) << 2;

    /* RF gain */
    wj_rfgain = (unsigned)(priv->rfgain.f * 0x7f);
    buf[7] |= (wj_rfgain >> 6) & 0x1;
    buf[8] |= (wj_rfgain & 0x3f) << 1;

    /* buf[9]: not used if command byte, but must be transmitted */

    rig_flush(rp);

    retval = write_block(rp, buf, CMDSZ);

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

    if (monitor)
    {
        /*
        * Transceiver sends back ">"
        */
        retval = read_block(rp, rxbuf, CMDSZ);

        if (retval < 0 || retval > CMDSZ)
        {
            return -RIG_ERJCTED;
        }

        /*
         *  TODO: analyze back the reply, and fill in the priv struct
         */
        priv->rawstr.i = rxbuf[9] & 0x7f;
    }

    return retval;
}

int wj_init(RIG *rig)
{
    struct wj_priv_data *priv;

    if (!rig || !rig->caps)
    {
        return -RIG_EINVAL;
    }

    STATE(rig)->priv = (struct wj_priv_data *)calloc(1,
                       sizeof(struct wj_priv_data));

    if (!STATE(rig)->priv)
    {
        /* whoops! memory shortage! */
        return -RIG_ENOMEM;
    }

    priv = STATE(rig)->priv;

    priv->receiver_id = 0;
    priv->freq = kHz(500);
    priv->mode = RIG_MODE_AM;
    priv->width = kHz(8);
    priv->agc.i = RIG_AGC_SLOW;
    priv->rfgain.f = 1;
    priv->ifshift.i = 0;

    return RIG_OK;
}

/*
 */
int wj_cleanup(RIG *rig)
{
    if (!rig)
    {
        return -RIG_EINVAL;
    }

    if (STATE(rig)->priv)
    {
        free(STATE(rig)->priv);
    }

    STATE(rig)->priv = NULL;

    return RIG_OK;
}



/*
 * Assumes rig!=NULL, STATE(rig)->priv!=NULL
 */
int wj_set_conf(RIG *rig, hamlib_token_t token, const char *val)
{
    struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;

    switch (token)
    {
    case TOK_RIGID:
        priv->receiver_id = atoi(val);
        break;

    default:
        return -RIG_EINVAL;
    }

    return RIG_OK;
}

/*
 * assumes rig!=NULL,
 * Assumes rig!=NULL, STATE(rig)->priv!=NULL
 *  and val points to a buffer big enough to hold the conf value.
 */
int wj_get_conf2(RIG *rig, hamlib_token_t token, char *val, int val_len)
{
    const struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;

    switch (token)
    {
    case TOK_RIGID:
        SNPRINTF(val, val_len, "%u", priv->receiver_id);
        break;

    default:
        return -RIG_EINVAL;
    }

    return RIG_OK;
}

int wj_get_conf(RIG *rig, hamlib_token_t token, char *val)
{
    return wj_get_conf2(rig, token, val, 128);
}

/*
 * wj_set_freq
 * Assumes rig!=NULL
 */
int wj_set_freq(RIG *rig, vfo_t vfo, freq_t freq)
{
    struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;

    priv->freq = freq;

    return wj_transaction(rig, 0);
}

/*
 * wj_get_freq
 * Assumes rig!=NULL
 */
int wj_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
{
    const struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;
    int retval;

    retval =  wj_transaction(rig, 1);

    if (retval == RIG_OK)
    {
        return retval;
    }

    *freq = priv->freq;

    return RIG_OK;
}

/*
 * wj_set_mode
 * Assumes rig!=NULL
 */
int wj_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
{
    struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;

    priv->mode = mode;

    if (width != RIG_PASSBAND_NOCHANGE)
    {
        if (width == RIG_PASSBAND_NORMAL)
        {
            width = rig_passband_normal(rig, mode);
        }

        priv->width = width;
    }

    return wj_transaction(rig, 0);
}

/*
 * wj_get_mode
 * Assumes rig!=NULL
 */
int wj_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
{
    const struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;
    int retval;

    retval =  wj_transaction(rig, 1);

    if (retval == RIG_OK)
    {
        return retval;
    }

    *mode = priv->mode;
    *width = priv->width;

    return RIG_OK;
}



/*
 * wj_set_level
 * Assumes rig!=NULL
 */
int wj_set_level(RIG *rig, vfo_t vfo, setting_t level, value_t val)
{
    struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;

    switch (level)
    {
    case RIG_LEVEL_IF:
        priv->ifshift.i = val.i;
        break;

    case RIG_LEVEL_RF:
        priv->rfgain.f = val.f;
        break;

    case RIG_LEVEL_AGC:
        priv->agc.i = val.i;
        break;

    default:
        rig_debug(RIG_DEBUG_ERR, "%s: unsupported %s\n", __func__, rig_strlevel(level));
        return -RIG_EINVAL;
    }

    return wj_transaction(rig, 0);
}

/*
 * wj_get_level
 * Assumes rig!=NULL
 */
int wj_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val)
{
    struct wj_priv_data *priv = (struct wj_priv_data *)STATE(rig)->priv;
    int retval = RIG_OK;

    retval =  wj_transaction(rig, 1);

    if (retval == RIG_OK)
    {
        return retval;
    }

    switch (level)
    {
    case RIG_LEVEL_RAWSTR:
        val->i = priv->rawstr.i;
        break;

    case RIG_LEVEL_IF:
        val->i = priv->ifshift.i;
        break;

    case RIG_LEVEL_RF:
        val->f = priv->rfgain.f;
        break;

    case RIG_LEVEL_AGC:
        val->i = priv->agc.i;
        break;


    default:
        rig_debug(RIG_DEBUG_ERR, "%s: unsupported %s\n", __func__, rig_strlevel(level));
        return -RIG_EINVAL;
    }

    return retval;
}


/*
 * initrigs_wj is called by rig_backend_load
 */
DECLARE_INITRIG_BACKEND(wj)
{
    rig_debug(RIG_DEBUG_VERBOSE, "%s: _init called\n", __func__);

    rig_register(&wj8888_caps);

    return RIG_OK;
}