File: simelecraft.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 (454 lines) | stat: -rw-r--r-- 11,915 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
// can run this using rigctl/rigctld and socat pty devices
#define _XOPEN_SOURCE 700
// since we are POSIX here we need this
#if  0
struct ip_mreq
{
    int dummy;
};
#endif

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <hamlib/rig.h>
#include "sim.h"

#define BUFSIZE 256

float freqA = 14074000;
float freqB = 14074500;
int afgain = 180;
int rfgain = 190;
int micgain = 30;
int noiseblanker = 0;
int bandwidthA = 2200;
int bandwidthB = 2400;
int ifshift = 0;
int preampA = 0;
int preampB = 0;
int rxattenuatorA = 0;
int rxattenuatorB = 0;
int keyspd = 20;
int ai = 0;
int dt = 0;
int modea = 2;
int modeb = 2;
int ptt = 0;

// ID 0310 == 310, Must drop leading zero
typedef enum nc_rigid_e
{
    NC_RIGID_NONE            = 0,
    NC_RIGID_FT450           = 241,
    NC_RIGID_FT450D          = 244,
    NC_RIGID_FT950           = 310,
    NC_RIGID_FT891           = 135,
    NC_RIGID_FT991           = 135,
    NC_RIGID_FT2000          = 251,
    NC_RIGID_FT2000D         = 252,
    NC_RIGID_FTDX1200        = 583,
    NC_RIGID_FTDX9000D       = 101,
    NC_RIGID_FTDX9000Contest = 102,
    NC_RIGID_FTDX9000MP      = 103,
    NC_RIGID_FTDX5000        = 362,
    NC_RIGID_FTDX3000        = 460,
    NC_RIGID_FTDX101D        = 681,
    NC_RIGID_FTDX101MP       = 682
} nc_rigid_t;

int
getmyline(int fd, char *buf)
{
    char c;
    int i = 0;
    memset(buf, 0, BUFSIZE);

    while (read(fd, &c, 1) > 0)
    {
        buf[i++] = c;

        if (c == ';') { return strlen(buf); }
    }

    if (strlen(buf) == 0) { hl_usleep(10 * 1000); }

    return strlen(buf);
}

#if defined(WIN32) || defined(_WIN32)
int openPort(char *comport) // doesn't matter for using pts devices
{
    int fd;
    fd = open(comport, O_RDWR);

    if (fd < 0)
    {
        perror(comport);
    }

    return fd;
}

#else
int openPort(char *comport) // doesn't matter for using pts devices
{
    int fd = posix_openpt(O_RDWR);
    char *name = ptsname(fd);

    if (name == NULL)
    {
        perror("ptsname");
        return -1;
    }

    printf("name=%s\n", name);

    if (fd == -1 || grantpt(fd) == -1 || unlockpt(fd) == -1)
    {
        perror("posix_openpt");
        return -1;
    }

    return fd;
}
#endif



int main(int argc, char *argv[])
{
    char buf[256];
    char *pbuf;
    int n;
    int fd = openPort(argv[1]);
    int freqa = 14074000, freqb = 14073500;

    while (1)
    {
        buf[0] = 0;

        if ((n = getmyline(fd, buf)) > 0) { if (strstr(buf, "BW")) printf("Cmd:%s, len=%d\n", buf, n); }
        else {continue; }

        if (strcmp(buf, "RM5;") == 0)
        {
            printf("%s\n", buf);
            hl_usleep(50 * 1000);
            pbuf = "RM5100000;";
            WRITE(fd, pbuf, strlen(pbuf));
        }
        else if (strcmp(buf, "AI;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "AI%d;", ai);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "AI", 2) == 0)
        {
            sscanf(buf, "AI%d", &ai);
        }

        else if (strcmp(buf, "AN0;") == 0)
        {
            printf("%s\n", buf);
            hl_usleep(50 * 1000);
            pbuf = "AN030;";
            WRITE(fd, pbuf, strlen(pbuf));
        }
        else if (strcmp(buf, "IF;") == 0)
        {
            printf("%s\n", buf);
            hl_usleep(50 * 1000);
            //pbuf = "IF059014200000+000000700000;";
            pbuf = strdup("IF00007230000     -000000 00?1000001 ;") ;
            pbuf[28] = ptt == 0 ? '0' : '1';
            WRITE(fd, pbuf, strlen(pbuf));
            free(pbuf);
        }
        else if (strcmp(buf, "ID;") == 0)
        {
            printf("%s\n", buf);
            hl_usleep(50 * 1000);
            int id = 24;
            SNPRINTF(buf, sizeof(buf), "ID%03d;", id);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "PS;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "PS1;");
            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "BW$;") == 0)
        {
            fprintf(stderr, "***** %d\n", __LINE__);
            SNPRINTF(buf, sizeof(buf), "BW$%04d;", bandwidthB);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "BW$", 3) == 0)
        {
            sscanf(buf, "BW$%d", &bandwidthB);
        }
        else if (strcmp(buf, "BW;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "BW%04d;", bandwidthA);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "BW", 2) == 0)
        {
            sscanf(buf, "BW%d", &bandwidthA);
        }
        else if (strcmp(buf, "DT;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "DT%d;", dt);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "DT", 2) == 0)
        {
            sscanf(buf, "DT%d", &dt);
        }
        else if (strcmp(buf, "BN;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "BN03;");
            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "SM;") == 0)
        {
            static int meter = 0;
            SNPRINTF(buf, sizeof(buf), "SM%04d;", meter++);

            if (meter > 15) { meter = 0; }

            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "RG;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "RG%03d;", rfgain);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "RG", 2) == 0)
        {
            sscanf(buf, "RG%d", &rfgain);
        }
        else if (strcmp(buf, "MG;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "MG%03d;", micgain);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "MG", 2) == 0)
        {
            sscanf(buf, "MG%d", &micgain);
        }
        else if (strcmp(buf, "AG;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "MG%03d;", afgain);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "AG", 2) == 0)
        {
            sscanf(buf, "AG%d", &afgain);
        }
        else if (strcmp(buf, "NB;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "NB%d;", noiseblanker);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "NB", 2) == 0)
        {
            sscanf(buf, "NB%d", &noiseblanker);
        }
        else if (strcmp(buf, "IS;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "IS %04d;", ifshift);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "IS", 2) == 0)
        {
            sscanf(buf, "IS %d", &ifshift);
        }


#if 0
        else if (strncmp(buf, "AI", 2) == 0)
        {
            if (strcmp(buf, "AI;"))
            {
                printf("%s\n", buf);
                hl_usleep(50 * 1000);
                n = fprintf(fp, "%s", "AI0;");

                if (n <= 0) { perror("AI"); }
            }
        }

#endif
        else if (strcmp(buf, "VS;") == 0)
        {
            printf("%s\n", buf);
            hl_usleep(50 * 1000);
            pbuf = "VS0;";
            WRITE(fd, pbuf, strlen(pbuf));
        }
        else if (strcmp(buf, "EX032;") == 0)
        {
            static int ant = 0;
            ant = (ant + 1) % 3;
            printf("%s\n", buf);
            hl_usleep(50 * 1000);
            SNPRINTF(buf, sizeof(buf), "EX032%1d;", ant);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "OM;") == 0)
        {
            // KPA3 SNPRINTF(buf, sizeof(buf), "OM AP----L-----;");
            // K4+KPA3
            SNPRINTF(buf, sizeof(buf), "OM AP-S----4---;");
            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "K2;") == 0)
        {
            WRITE(fd, "K20;", 4);
        }
        else if (strcmp(buf, "K3;") == 0)
        {
            WRITE(fd, "K30;", 4);
        }
        else if (strcmp(buf, "RVD;") == 0)
        {
            WRITE(fd, "RVD02.36;", 9);
        }
        else if (strcmp(buf, "RVM;") == 0)
        {
            WRITE(fd, "RVM02.37;", 9);
        }
        else if (strcmp(buf, "MD;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "MD%d;", modea);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "MD$;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "MD$%d;", modeb);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "MD", 2) == 0)
        {
            if (buf[2] == '$') { sscanf(buf, "MD$%d;", &modeb); }
            else { sscanf(buf, "MD%d;", &modea); }
        }
        else if (strcmp(buf, "FA;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "FA%011d;", freqa);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strcmp(buf, "FB;") == 0)
        {
            SNPRINTF(buf, sizeof(buf), "FB%011d;", freqb);
            WRITE(fd, buf, strlen(buf));
        }

        else if (strncmp(buf, "FA", 2) == 0)
        {
            sscanf(buf, "FA%d", &freqa);
        }
        else if (strncmp(buf, "FB", 2) == 0)
        {
            sscanf(buf, "FB%d", &freqb);
        }
        else if (strncmp(buf, "FR;", 3) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "FR0;");
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "FR", 2) == 0)
        {
            // we ignore FR for the K3
        }
        else if (strncmp(buf, "FT;", 3) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "FT0;");
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "KS;", 3) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "KS%03d;", keyspd);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "KS", 2) == 0)
        {
            sscanf(buf, "KS%d", &keyspd);
        }
        else if (strncmp(buf, "TQ;", 3) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "TQ0;");
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "PC;", 3) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "PC0980;");
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "PA;", 3) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "PA%d;", preampA);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "PA$;", 4) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "PA$%d;", preampB);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "PA", 2) == 0)
        {
            sscanf(buf, "PA%d;", &preampA);
        }
        else if (strncmp(buf, "PA$", 3) == 0)
        {
            sscanf(buf, "PA$%d;", &preampB);
        }
        else if (strncmp(buf, "RA;", 3) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "RA%02d;", rxattenuatorA);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "RA$;", 4) == 0)
        {
            SNPRINTF(buf, sizeof(buf), "RA$%02d;", rxattenuatorA);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "RA", 2) == 0)
        {
            sscanf(buf, "RA%d;", &rxattenuatorB);
        }
        else if (strncmp(buf, "RA$", 3) == 0)
        {
            sscanf(buf, "RA$%d;", &rxattenuatorB);
        }
        else if (strncmp(buf, "KY;", 3) == 0)
        {
            int status = 0;
            printf("KY query\n");
            SNPRINTF(buf, sizeof(buf), "KY%d;", status);
            WRITE(fd, buf, strlen(buf));
        }
        else if (strncmp(buf, "KY", 2) == 0)
        {
            printf("Morse: %s\n", buf);
        }
        else if (strncmp(buf, "TX", 2) == 0)
        {
            ptt = 1;
        }
        else if (strncmp(buf, "RX", 2) == 0)
        {
            ptt = 0;
        }
        else if (strlen(buf) > 0)
        {
            fprintf(stderr, "Unknown command: %s\n", buf);
        }
    }

    return 0;
}