File: client.c

package info (click to toggle)
baycomusb 0.10-15.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,884 kB
  • sloc: ansic: 49,182; asm: 17,572; sh: 2,442; makefile: 542; pascal: 183; sed: 93; perl: 31
file content (339 lines) | stat: -rw-r--r-- 9,189 bytes parent folder | download | duplicates (9)
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
/*****************************************************************************/

/*
 *      client.c  --  Transceiver control client.
 *
 *      Copyright (C) 2000, 2001
 *          Thomas Sailer (t.sailer@alumni.ethz.ch)
 *
 *      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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Please note that the GPL allows you to use the driver, NOT the radio.
 *  In order to use the radio, you need a license from the communications
 *  authority of your country.
 *
 *  History:
 *   0.1  19.09.2000  Created
 *
 */

/*****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "trxapi.h"

#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

/* --------------------------------------------------------------------- */

#if defined(WIN32)
#define UINT64_FORMAT "I64u"
#else
#define UINT64_FORMAT "llu"
#endif

/* --------------------------------------------------------------------- */

static void list_trxobjects(void)
{
	unsigned int index = 0, i;
	struct trxapi *trx;
	const struct trxapi_description *desc;
	struct trxapi_state state;

	for (;; index++) {
		if (!(trx = trxapi_open_byindex(index)))
			break;
		printf("Transceiver Index %u\n", index);
		desc = trxapi_get_description(trx);
		if (!desc) {
			printf("  trxapi_get_description error\n");
			trxapi_close(trx);
			continue;
		}
		printf("  Manufacturer           : %s\n"
		       "  Model                  : %s\n"
		       "  Copyright              : %s\n"
		       "  Instance ID (serial no): %s\n"
		       "  RSSI range             : %gdB..%gdB\n"
		       "  Receive Bands:\n",
		       desc->manufacturer, desc->model, desc->copyright, desc->instid,
		       desc->rssimin, desc->rssimax);
		for (i = 0; i < desc->nrrxbands; i++)
			printf("    Frequency range %10" UINT64_FORMAT "-%10" UINT64_FORMAT " stepsize %10" UINT64_FORMAT "\n",
			       desc->rxbands[i].low, desc->rxbands[i].high, desc->rxbands[i].step);
		printf("  Transmit Bands:\n");
		for (i = 0; i < desc->nrtxbands; i++)
			printf("    Frequency range %10" UINT64_FORMAT "-%10" UINT64_FORMAT " stepsize %10" UINT64_FORMAT "\n",
			       desc->txbands[i].low, desc->txbands[i].high, desc->txbands[i].step);
		if (trxapi_get_state(trx, &state)) {
			printf("  trxapi_get_state error\n");
			trxapi_close(trx);
			continue;
		}
		printf("  RX Frequency  : %10" UINT64_FORMAT "\n"
		       "  TX Frequency  : %10" UINT64_FORMAT "\n",
                       state.rxfreq, state.txfreq);
                printf("  PTT           : %s\n"
		       "  DCD           : %s\n"
		       "  RSSI          : %gdB\n",
		       state.ptt ? "on" : "off",
		       state.dcd ? "on" : "off", state.rssi);
		trxapi_close(trx);
	}
	if (!index)
		printf("No Transceivers found\n");
}

/* --------------------------------------------------------------------- */

static int periodic_trxstate(unsigned int index)
{
	unsigned int i;
	struct trxapi *trx;
	const struct trxapi_description *desc;
	struct trxapi_state state;

	if (!(trx = trxapi_open_byindex(index))) {
		fprintf(stderr, "Cannot find transceiver index %u\n", index);
		return 1;
	}
	printf("Transceiver Index %u\n", index);
	desc = trxapi_get_description(trx);
	if (!desc) {
		printf("  trxapi_get_description error\n");
		trxapi_close(trx);
		return 1;
	}
	printf("  Manufacturer           : %s\n"
	       "  Model                  : %s\n"
	       "  Copyright              : %s\n"
	       "  Instance ID (serial no): %s\n"
	       "  RSSI range             : %gdB..%gdB\n"
	       "  Receive Bands:\n",
	       desc->manufacturer, desc->model, desc->copyright, desc->instid,
	       desc->rssimin, desc->rssimax);
	for (i = 0; i < desc->nrrxbands; i++)
		printf("    Frequency range %10" UINT64_FORMAT "-%10" UINT64_FORMAT " stepsize %10" UINT64_FORMAT "\n",
		       desc->rxbands[i].low, desc->rxbands[i].high, desc->rxbands[i].step);
	printf("  Transmit Bands:\n");
	for (i = 0; i < desc->nrtxbands; i++)
		printf("    Frequency range %10" UINT64_FORMAT "-%10" UINT64_FORMAT " stepsize %10" UINT64_FORMAT "\n",
		       desc->txbands[i].low, desc->txbands[i].high, desc->txbands[i].step);
	for (;;) {
		if (trxapi_get_state(trx, &state)) {
			printf("  trxapi_get_state error\n");
			trxapi_close(trx);
			return 1;
		}
		printf("RX %10" UINT64_FORMAT " TX %10" UINT64_FORMAT " PTT %c DCD %c RSSI %8.2fdB\n", 
		       state.rxfreq, state.txfreq, state.ptt ? 'P' : '-',
		       state.dcd ? 'D' : '-', state.rssi);
		sleep(1);
	}
	trxapi_close(trx);
}

/* --------------------------------------------------------------------- */

static int setfreq(unsigned int index, unsigned int rx, unsigned int tx)
{
	struct trxapi *trx;

	if (!(trx = trxapi_open_byindex(index))) {
		fprintf(stderr, "Cannot find transceiver index %u\n", index);
		return 1;
	}	
	if (trxapi_set_frequency(trx, rx, tx)) {
		printf("  trxapi_set_frequency error\n");
		trxapi_close(trx);
		return 1;
	}
	trxapi_close(trx);
	return 0;
}

/* --------------------------------------------------------------------- */

static int rxuart(unsigned int index)
{
	unsigned long fifoptr = 0;
	struct trxapi *trx;
	char buf[256];
	char *cp;

	if (!(trx = trxapi_open_byindex(index))) {
		fprintf(stderr, "Cannot find transceiver index %u\n", index);
		return 1;
	}	
	for (;;) {
		if (trxapi_uart_receive(trx, &fifoptr, buf, sizeof(buf))) {
			printf("  trxapi_uart_receive error\n");
			trxapi_close(trx);
			return 1;
		}
		cp = buf;
		while ((cp = strchr(cp, '\r')))
			*cp++ = '\n';
		printf("%s", buf);
		fflush(stdout);
		sleep(1);
	}
	trxapi_close(trx);
	return 0;
}

/* --------------------------------------------------------------------- */

static int txuart(unsigned int index, const char *str)
{
	struct trxapi *trx;
	char buf[256];

	if (!(trx = trxapi_open_byindex(index))) {
		fprintf(stderr, "Cannot find transceiver index %u\n", index);
		return 1;
	}	
	snprintf(buf, sizeof(buf), "%s\r", str);
	if (trxapi_uart_send(trx, buf)) {
		printf("  trxapi_uart_send error\n");
		trxapi_close(trx);
		return 1;
	}
	trxapi_close(trx);
	return 0;
}

/* --------------------------------------------------------------------- */

static int setptt(unsigned int index, unsigned int ptt)
{
	struct trxapi *trx;

	if (!(trx = trxapi_open_byindex(index))) {
		fprintf(stderr, "Cannot find transceiver index %u\n", index);
		return 1;
	}	
	if (trxapi_set_ptt(trx, ptt)) {
		printf("  trxapi_set_ptt error\n");
		trxapi_close(trx);
		return 1;
	}
	trxapi_close(trx);
	return 0;
}

/* --------------------------------------------------------------------- */

int main (int argc, char *argv[])
{
        static const struct option long_options[] = {
		{ "list", 0, 0, 'l' },
		{ "setfreq", 0, 0, 's' },
		{ "rxuart", 0, 0, 'r' },
		{ "txuart", 0, 0, 't' },
 		{ "ptt", 0, 0, 'p' },
                { "verbose", 0, 0, 'v' },
		{ "help", 0, 0, 'h' },
                { 0, 0, 0, 0 }
        };
        int c, err = 0;
	unsigned int index = 0, mode = 0;

        printf("trxclient v" VERSION " (c) 2000 by Thomas Sailer, HB9JNX/AE4WA\n");
	/* init CORBA */
	if (trxapi_init(&argc, argv))
		exit(1);
	while ((c = getopt_long(argc, argv, "vli:srpt", long_options, NULL)) != EOF) {
                switch (c) {
                case 'v':
                        verboselevel++;
                        break;

		case 'l':
			mode = 1;
			break;

		case 's':
			mode = 2;
			break;

		case 'r':
			mode = 3;
			break;

		case 'p':
			mode = 4;
			break;

		case 't':
			mode = 5;
			break;

		case 'i':
			index = strtoul(optarg, NULL, 0);
			break;

                default:
                        err++;
                        break;
                }
        }
        if (err) {
                fprintf(stderr, "usage: %s [-v] [-l]\n", argv[0]);
		exit(1);
        }
	switch (mode) {
	default:
		exit(periodic_trxstate(index));

	case 1:
		list_trxobjects();
		exit(0);

	case 2:
		if (optind + 2 > argc) {
			fprintf(stderr, "usage: %s -s <rx> <tx>\n", argv[0]);
			exit(1);
		}
		exit(setfreq(index, strtoul(argv[optind], NULL, 0), strtoul(argv[optind+1], NULL, 0)));

	case 3:
		exit(rxuart(index));

	case 4:
		if (optind + 1 > argc) {
			fprintf(stderr, "usage: %s -s <rx> <tx>\n", argv[0]);
			exit(1);
		}
		exit(setptt(index, strtoul(argv[optind], NULL, 0)));

	case 5:
		if (optind + 1 > argc) {
			fprintf(stderr, "usage: %s -s <rx> <tx>\n", argv[0]);
			exit(1);
		}
		exit(txuart(index, argv[optind]));

	}
	return 0;
}