Debian Codegraph @

hdlcutil/sethdlc.c

raw symbols SCIP
1 /*****************************************************************************/
2
3 /*
4 * sethdlc.c -- kernel HDLC radio modem driver setup utility.
5 *
6 * Copyright (C) 1996,1997,2000 Thomas Sailer ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *
23 * Please note that the GPL allows you to use the driver, NOT the radio.
24 * In order to use the radio, you need a license from the communications
25 * authority of your country.
26 *
27 *
28 * History:
29 * 0.1 16.10.1996 Adapted from setbaycom.c and setsm.c
30 * 0.2 20.11.1996 New mode set/query code
31 * 0.5 11.05.1997 introduced hdrvcomm.h
32 * 0.6 05.01.2000 glibc compile fixes
33 */
34
35 /*****************************************************************************/
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <errno.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <net/if.h>
47 #include "hdrvcomm.h"
48
49 #include <config.h>
50 #include <scm-version.h>
51
52 /* ---------------------------------------------------------------------- */
53
54 static unsigned char trace_stat;
55 static char *progname;
56
57 /* ---------------------------------------------------------------------- */
58
59 static void display_packet(char *bp, unsigned int len)
60 {
61 unsigned char v1=1,cmd=0;
62 unsigned char i,j;
63
64 if (!bp || !len)
65 return;
66 if (len < 8)
67 return;
68 if (bp[1] & 1) {
69 /*
70 * FlexNet Header Compression
71 */
72 v1 = 0;
73 cmd = (bp[1] & 2) != 0;
74 printf("fm ? to ");
75 i = (bp[2] >> 2) & 0x3f;
76 if (i)
77 printf("%c",i+0x20);
78 i = ((bp[2] << 4) | ((bp[3] >> 4) & 0xf)) & 0x3f;
79 if (i)
80 printf("%c",i+0x20);
81 i = ((bp[3] << 2) | ((bp[4] >> 6) & 3)) & 0x3f;
82 if (i)
83 printf("%c",i+0x20);
84 i = bp[4] & 0x3f;
85 if (i)
86 printf("%c",i+0x20);
87 i = (bp[5] >> 2) & 0x3f;
88 if (i)
89 printf("%c",i+0x20);
90 i = ((bp[5] << 4) | ((bp[6] >> 4) & 0xf)) & 0x3f;
91 if (i)
92 printf("%c",i+0x20);
93 printf("-%u QSO Nr %u", bp[6] & 0xf, (bp[0] << 6) |
94 (bp[1] >> 2));
95 bp += 7;
96 len -= 7;
97 } else {
98 /*
99 * normal header
100 */
101 if (len < 15)
102 return;
103 if ((bp[6] & 0x80) != (bp[13] & 0x80)) {
104 v1 = 0;
105 cmd = (bp[6] & 0x80);
106 }
107 printf("fm ");
108 for (i = 7; i < 13; i++)
109 if ((bp[i] &0xfe) != 0x40)
110 printf("%c",bp[i] >> 1);
111 printf("-%u to ",(bp[13] >> 1) & 0xf);
112 for (i = 0; i < 6; i++)
113 if ((bp[i] &0xfe) != 0x40)
114 printf("%c", bp[i] >> 1);
115 printf("-%u", (bp[6] >> 1) & 0xf);
116 bp += 14;
117 len -= 14;
118 if ((!(bp[-1] & 1)) && (len >= 7))
119 printf(" via ");
120 while ((!(bp[-1] & 1)) && (len >= 7)) {
121 for (i = 0; i < 6; i++)
122 if ((bp[i] &0xfe) != 0x40)
123 printf("%c",bp[i] >> 1);
124 printf("-%u",(bp[6] >> 1) & 0xf);
125 bp += 7;
126 len -= 7;
127 if ((!(bp[-1] & 1)) && (len >= 7))
128 printf(",");
129 }
130 }
131 if (!len)
132 return;
133 i = *bp++;
134 len--;
135 j = v1 ? ((i & 0x10) ? '!' : ' ') :
136 ((i & 0x10) ? (cmd ? '+' : '-') : (cmd ? '^' : 'v'));
137 if (!(i & 1)) {
138 /*
139 * Info frame
140 */
141 printf(" I%u%u%c",(i >> 5) & 7,(i >> 1) & 7,j);
142 } else if (i & 2) {
143 /*
144 * U frame
145 */
146 switch (i & (~0x10)) {
147 case 0x03:
148 printf(" UI%c",j);
149 break;
150 case 0x2f:
151 printf(" SABM%c",j);
152 break;
153 case 0x43:
154 printf(" DISC%c",j);
155 break;
156 case 0x0f:
157 printf(" DM%c",j);
158 break;
159 case 0x63:
160 printf(" UA%c",j);
161 break;
162 case 0x87:
163 printf(" FRMR%c",j);
164 break;
165 default:
166 printf(" unknown U (0x%x)%c",i & (~0x10),j);
167 break;
168 }
169 } else {
170 /*
171 * supervisory
172 */
173 switch (i & 0xf) {
174 case 0x1:
175 printf(" RR%u%c",(i >> 5) & 7,j);
176 break;
177 case 0x5:
178 printf(" RNR%u%c",(i >> 5) & 7,j);
179 break;
180 case 0x9:
181 printf(" REJ%u%c",(i >> 5) & 7,j);
182 break;
183 default:
184 printf(" unknown S (0x%x)%u%c", i & 0xf,
185 (i >> 5) & 7, j);
186 break;
187 }
188 }
189 if (!len) {
190 printf("\n");
191 return;
192 }
193 printf(" pid=%02X\n", *bp++);
194 len--;
195 j = 0;
196 while (len) {
197 i = *bp++;
198 if ((i >= 32) && (i < 128))
199 printf("%c",i);
200 else if (i == 13) {
201 if (j)
202 printf("\n");
203 j = 0;
204 } else
205 printf(".");
206 if (i >= 32)
207 j = 1;
208 len--;
209 }
210 if (j)
211 printf("\n");
212 }
213
214 /* ---------------------------------------------------------------------- */
215
216 static void print_bits(int (*bitproc)(void))
217 {
218 int ret;
219 int i;
220 char str[9];
221 char *cp;
222
223 for (;;) {
224 ret = bitproc();
225 if (ret < 0) {
226 if (errno == EAGAIN)
227 return;
228 fprintf(stderr, "%s: Error %s (%i), cannot ioctl\n",
229 progname, strerror(errno), errno);
230 return;
231 }
232 strcpy(cp = str, "00000000");
233 for (i = 0; i < 8; i++, cp++, ret >>= 1)
234 *cp += (ret & 1);
235 printf("%s", str);
236 }
237 }
238
239 /* ---------------------------------------------------------------------- */
240
241
242 static void do_set_params(int argc, char **argv)
243 {
244 struct hdlcdrv_ioctl drvname, curm, newm, mlist, curp, newp, mpmask;
245 char set = 0;
246 int ret;
247 int mask;
248
249 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_DRIVERNAME, &drvname);
250 if (ret < 0) {
251 perror("ioctl (HDLCDRVCTL_DRIVERNAME)");
252 exit(1);
253 }
254 printf("driver name: %s\n", drvname.data.drivername);
255
256 mask = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_MODEMPARMASK, &mpmask);
257 if (mask < 0)
258 mask = ~0;
259
260 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_GETMODEMPAR, &curp);
261 if (ret < 0) {
262 perror("ioctl (HDLCDRVCTL_GETMODEMPAR)");
263 exit(1);
264 }
265 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_GETMODE, &curm);
266 if (ret < 0) {
267 perror("ioctl (HDLCDRVCTL_GETMODE)");
268 exit(1);
269 }
270 newm = curm;
271 newp = curp;
272 while (argc >= 2) {
273 if (!strcasecmp(argv[0], "mode")) {
274 strncpy(newm.data.modename, argv[1],
275 sizeof(newm.data.modename));
276 set |= 1;
277 } else if (!strcasecmp(argv[0], "io") && mask & HDLCDRV_PARMASK_IOBASE) {
278 newp.data.mp.iobase = strtoul(argv[1], NULL, 0);
279 set |= 2;
280 } else if (!strcasecmp(argv[0], "irq") && mask & HDLCDRV_PARMASK_IRQ) {
281 newp.data.mp.irq = strtoul(argv[1], NULL, 0);
282 set |= 2;
283 } else if (!strcasecmp(argv[0], "dma") && mask & HDLCDRV_PARMASK_DMA) {
284 newp.data.mp.dma = strtoul(argv[1], NULL, 0);
285 set |= 2;
286 } else if (!strcasecmp(argv[0], "dma2") && mask & HDLCDRV_PARMASK_DMA2) {
287 newp.data.mp.dma2 = strtoul(argv[1], NULL, 0);
288 set |= 2;
289 } else if (!strcasecmp(argv[0], "serio") && mask & HDLCDRV_PARMASK_SERIOBASE) {
290 newp.data.mp.seriobase = strtol(argv[1], NULL, 0);
291 set |= 2;
292 } else if (!strcasecmp(argv[0], "pario") && mask & HDLCDRV_PARMASK_PARIOBASE) {
293 newp.data.mp.pariobase = strtol(argv[1], NULL, 0);
294 set |= 2;
295 } else if (!strcasecmp(argv[0], "midiio") && mask & HDLCDRV_PARMASK_MIDIIOBASE) {
296 newp.data.mp.midiiobase = strtol(argv[1], NULL, 0);
297 set |= 2;
298 } else {
299 fprintf(stderr, "%s: invalid parameter type '%s', "
300 "valid: mode%s%s%s%s%s%s%s\n",
301 progname, argv[0],
302 (mask & HDLCDRV_PARMASK_IOBASE) ? " io" : "",
303 (mask & HDLCDRV_PARMASK_IRQ) ? " irq" : "",
304 (mask & HDLCDRV_PARMASK_DMA) ? " dma" : "",
305 (mask & HDLCDRV_PARMASK_DMA2) ? " dma2" : "",
306 (mask & HDLCDRV_PARMASK_SERIOBASE) ? " serio" : "",
307 (mask & HDLCDRV_PARMASK_PARIOBASE) ? " pario" : "",
308 (mask & HDLCDRV_PARMASK_MIDIIOBASE) ? " midiio" : "");
309 }
310 argv += 2;
311 argc -= 2;
312 }
313 if (argc >= 1)
314 fprintf(stderr, "%s: orphaned parameter type '%s', no data\n",
315 progname, argv[0]);
316 printf("current parameters: mode %s", curm.data.modename);
317 if (mask & HDLCDRV_PARMASK_IOBASE)
318 printf(" io 0x%x", curp.data.mp.iobase);
319 if (mask & HDLCDRV_PARMASK_IRQ)
320 printf(" irq %u", curp.data.mp.irq);
321 if (mask & HDLCDRV_PARMASK_DMA)
322 printf(" dma %u", curp.data.mp.dma);
323 if (mask & HDLCDRV_PARMASK_DMA2)
324 printf(" dma2 %u", curp.data.mp.dma2);
325 if (mask & HDLCDRV_PARMASK_SERIOBASE)
326 printf(" serio 0x%x", curp.data.mp.seriobase);
327 if (mask & HDLCDRV_PARMASK_PARIOBASE)
328 printf(" pario 0x%x", curp.data.mp.pariobase);
329 if (mask & HDLCDRV_PARMASK_MIDIIOBASE)
330 printf(" midiio 0x%x", curp.data.mp.midiiobase);
331 printf("\n");
332
333 if (set & 1)
334 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_SETMODE, &newm);
335 if (ret < 0) {
336 perror("ioctl (HDLCDRVCTL_SETMODE)");
337 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_MODELIST, &mlist);
338 if (ret < 0) {
339 perror("ioctl (HDLCDRVCTL_MODELIST)");
340 exit(1);
341 }
342 printf("driver supported modes: %s\n", mlist.data.modename);
343 exit(1);
344 }
345 if (set & 2) {
346 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_SETMODEMPAR, &newp);
347 if (ret < 0) {
348 perror("ioctl (HDLCDRVCTL_SETMODEMPAR)");
349 fprintf(stderr, "%s: trying to restore old "
350 "parameters\n", progname);
351 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_SETMODEMPAR, &curp);
352 if (ret < 0)
353 perror("ioctl (HDLCDRVCTL_SETMODEMPAR)");
354 exit(1);
355 }
356 }
357 if (set & 3) {
358 printf("new parameters: mode %s", newm.data.modename);
359 if (mask & HDLCDRV_PARMASK_IOBASE)
360 printf(" io 0x%x", newp.data.mp.iobase);
361 if (mask & HDLCDRV_PARMASK_IRQ)
362 printf(" irq %u", newp.data.mp.irq);
363 if (mask & HDLCDRV_PARMASK_DMA)
364 printf(" dma %u", newp.data.mp.dma);
365 if (mask & HDLCDRV_PARMASK_DMA2)
366 printf(" dma2 %u", newp.data.mp.dma2);
367 if (mask & HDLCDRV_PARMASK_SERIOBASE)
368 printf(" serio 0x%x", newp.data.mp.seriobase);
369 if (mask & HDLCDRV_PARMASK_PARIOBASE)
370 printf(" pario 0x%x", newp.data.mp.pariobase);
371 if (mask & HDLCDRV_PARMASK_MIDIIOBASE)
372 printf(" midiio 0x%x", newp.data.mp.midiiobase);
373 printf("\n");
374 }
375 exit(0);
376 }
377
378
379 /* ---------------------------------------------------------------------- */
380
381 static void display_channel_params(const struct hdrvc_channel_params *par)
382 {
383 printf("TX delay %ums, TX tail %ums, slottime %ums, p-persistence "
384 " %u/256, %s duplex\n", 10*par->tx_delay, 10*par->tx_tail,
385 10*par->slottime, par->ppersist,
386 par->fulldup ? "Full" : "Half");
387 }
388
389 /* ---------------------------------------------------------------------- */
390
391 static void do_set_channel_params(int argc, char **argv)
392 {
393 struct hdrvc_channel_params par1, par2;
394 char set = 0;
395 int ret;
396
397 ret = hdrvc_get_channel_access_param(&par1);
398 if (ret < 0) {
399 perror("hdrvc_get_channel_access_param");
400 exit(1);
401 }
402 par2 = par1;
403 while (argc > 0) {
404 if (argc >= 2 && !strcasecmp(argv[0], "txd")) {
405 par2.tx_delay = strtoul(argv[1], NULL, 0) / 10;
406 set = 1;
407 argv += 2;
408 argc -= 2;
409 } else if (argc >= 2 && !strcasecmp(argv[0], "txtail")) {
410 par2.tx_tail = strtoul(argv[1], NULL, 0) / 10;
411 set = 1;
412 argv += 2;
413 argc -= 2;
414 } else if (argc >= 2 && !strcasecmp(argv[0], "slot")) {
415 par2.slottime = strtoul(argv[1], NULL, 0) / 10;
416 set = 1;
417 argv += 2;
418 argc -= 2;
419 } else if (argc >= 2 && !strcasecmp(argv[0], "ppersist")) {
420 par2.ppersist = strtoul(argv[1], NULL, 0);
421 set = 1;
422 argv += 2;
423 argc -= 2;
424 } else if (!strcasecmp(argv[0], "half")) {
425 par2.fulldup = 0;
426 set = 1;
427 argv += 1;
428 argc -= 1;
429 } else if (!strcasecmp(argv[0], "full")) {
430 par2.fulldup = 1;
431 set = 1;
432 argv += 1;
433 argc -= 1;
434 } else {
435
436 argv += 1;
437 argc -= 1;
438 }
439 }
440 printf("current parameters: ");
441 display_channel_params(&par1);
442 if (set) {
443 ret = hdrvc_set_channel_access_param(par2);
444 if (ret < 0) {
445 perror("hdrvc_set_channel_access_param");
446 exit(1);
447 }
448 printf("new parameters: ");
449 display_channel_params(&par2);
450 }
451 exit(0);
452 }
453
454 /* ---------------------------------------------------------------------- */
455
456 static const char *usage_str =
457 "[-b] [-i] [-d] [-i <baycomif>] [-h] [-c <cal>]\n"
458 "[-p] [hw <hw>] [type <type>] [io <iobase>] [irq <irq>] [dma <dma>]\n"
459 " [options <opt>] [serio <serio>] [pario <pario>] [midiio <midiio>]\n"
460 "[-a] [txd <txdelay>] [txtail <txtail>] [slot <slottime>]\n"
461 " [ppersist <ppersistence>] [full] [half]\n"
462 " -a: set or display channel access parameters\n"
463 " -b: trace demodulated bits\n"
464 " -s: trace sampled input from tcm3105 (ser12 only)\n"
465 " -d: trace dcd and ptt status on stdout\n"
466 " -i: specify the name of the baycom kernel driver interface\n"
467 " -c: calibrate (i.e. send calibration pattern) for cal seconds\n"
468 " -p: set or display interface parameters\n"
469 " -h: this help\n\n";
470
471 int main(int argc, char *argv[])
472 {
473 int ret;
474 fd_set fds_read;
475 fd_set fds_write;
476 struct timeval tm;
477 char getsetparams = 0;
478 char cal = 0;
479 int cal_val = 0;
480 struct sm_ioctl bsi;
481 struct baycom_ioctl bbi;
482 struct hdrvc_channel_state chst;
483 char pktbuf[2048];
484
485 progname = *argv;
486 printf("%s: Version " FULL_VER "; (C) 1996-1997 by Thomas Sailer HB9JNX/AE4WA\n", *argv);
487 hdrvc_args(&argc, argv, "bc0");
488 while ((ret = getopt(argc, argv, "bsdhpc:a")) != -1) {
489 switch (ret) {
490 case 'b':
491 trace_stat = 2;
492 break;
493 case 's':
494 trace_stat = 3;
495 break;
496 case 'd':
497 trace_stat = 1;
498 break;
499 case 'p':
500 getsetparams = 1;
501 break;
502 case 'a':
503 getsetparams = 2;
504 break;
505 case 'c':
506 cal = 1;
507 cal_val = strtoul(optarg, NULL, 0);
508 break;
509 default:
510 printf("usage: %s %s", *argv, usage_str);
511 exit(-1);
512 }
513 }
514 hdrvc_init();
515 if (getsetparams == 1) {
516 do_set_params(argc - optind, argv+optind);
517 exit(0);
518 }
519 if (getsetparams == 2) {
520 do_set_channel_params(argc - optind, argv+optind);
521 exit(0);
522 }
523 if (cal) {
524 ret = hdrvc_calibrate(cal_val);
525 if (ret < 0) {
526 perror("hdrvc_calibrate");
527 exit(1);
528 }
529 fprintf(stdout, "%s: calibrating for %i seconds\n", *argv,
530 cal_val);
531 exit(0);
532 }
533 for (;;) {
534 while ((ret = hdrvc_recvpacket(pktbuf, sizeof(pktbuf))) > 1)
535 display_packet(pktbuf+1, ret-1);
536 switch (trace_stat) {
537 case 1:
538 ret = hdrvc_get_channel_state(&chst);
539 if (ret < 0)
540 perror("hdrvc_get_channel_state");
541 printf("%c%c rx: %lu tx: %lu rxerr: %lu txerr: %lu",
542 chst.dcd ? 'D' : '-', chst.ptt ? 'P' : '-',
543 chst.rx_packets, chst.tx_packets, chst.rx_errors,
544 chst.tx_errors);
545 if (hdrvc_sm_ioctl(SMCTL_GETDEBUG, &bsi) >= 0) {
546 printf(" intrate: %u modcyc: %u "
547 "demodcyc: %u dmares: %u",
548 bsi.data.dbg.int_rate,
549 bsi.data.dbg.mod_cycles,
550 bsi.data.dbg.demod_cycles,
551 bsi.data.dbg.dma_residue);
552 }
553 if (hdrvc_baycom_ioctl(BAYCOMCTL_GETDEBUG, &bbi) >= 0) {
554 printf(" dbg1: %lu dbg2: %lu dbg3: %li",
555 bbi.data.dbg.debug1, bbi.data.dbg.debug2,
556 bbi.data.dbg.debug3);
557 }
558 printf("\n");
559 break;
560 case 2:
561 print_bits(hdrvc_get_bits);
562 printf("\n");
563 break;
564 case 3:
565 print_bits(hdrvc_get_samples);
566 printf("\n");
567 break;
568 default:
569 break;
570 }
571 tm.tv_usec = 500000L;
572 tm.tv_sec = 0L;
573 if (hdrvc_getfd() >= 0) {
574 FD_ZERO(&fds_read);
575 FD_ZERO(&fds_write);
576 FD_SET(hdrvc_getfd(), &fds_read);
577 ret = select(hdrvc_getfd()+1, &fds_read, &fds_write, NULL, &tm);
578 } else
579 ret = select(0, NULL, NULL, NULL, &tm);
580 if (ret < 0) {
581 fprintf(stderr, "%s: Error %s (%i) in select\n", *argv,
582 strerror(errno), errno);
583 exit(-2);
584 }
585 }
586 }
587
588 /* ---------------------------------------------------------------------- */