| 1 | /*****************************************************************************/ |
| 2 | |
| 3 | /* |
| 4 | * smdiag.c -- kernel soundcard radio modem driver diagnostics 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 26.06.1996 Started |
| 30 | * 0.2 11.05.1997 introduced hdrvcomm.h |
| 31 | * 0.3 05.01.2000 glibc compile fixes |
| 32 | */ |
| 33 | |
| 34 | /*****************************************************************************/ |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <stdio.h> |
| 38 | #include <errno.h> |
| 39 | #include <sys/time.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <unistd.h> |
| 44 | #include <string.h> |
| 45 | #include <limits.h> |
| 46 | #include <net/if.h> |
| 47 | #include <X11/X.h> |
| 48 | #include <X11/Xlib.h> |
| 49 | #include <X11/Xutil.h> |
| 50 | #include "hdrvcomm.h" |
| 51 | |
| 52 | #include <config.h> |
| 53 | #include <scm-version.h> |
| 54 | |
| 55 | /* ---------------------------------------------------------------------- */ |
| 56 | |
| 57 | static char *progname; |
| 58 | static Display *display; |
| 59 | static Window window; |
| 60 | static Pixmap pixmap; |
| 61 | static unsigned long col_zeroline; |
| 62 | static unsigned long col_background; |
| 63 | static unsigned long col_trace; |
| 64 | static GC gr_context; |
| 65 | static int xmul; |
| 66 | |
| 67 | /* ---------------------------------------------------------------------- */ |
| 68 | |
| 69 | static int x_error_handler(Display *disp, XErrorEvent *evt) |
| 70 | { |
| 71 | char err_buf[256], mesg[256], number[256]; |
| 72 | char *mtype = "XlibMessage"; |
| 73 | |
| 74 | XGetErrorText(disp, evt->error_code, err_buf, sizeof(err_buf)); |
| 75 | fprintf(stderr, "X Error: %s\n", err_buf); |
| 76 | XGetErrorDatabaseText(disp, mtype, "MajorCode", "Request Major code %d", |
| 77 | mesg, sizeof(mesg)); |
| 78 | fprintf(stderr, mesg, evt->request_code); |
| 79 | sprintf(number, "%d", evt->request_code); |
| 80 | XGetErrorDatabaseText(disp, "XRequest", number, "", err_buf, |
| 81 | sizeof(err_buf)); |
| 82 | fprintf(stderr, " (%s)\n", err_buf); |
| 83 | abort(); |
| 84 | } |
| 85 | |
| 86 | /* ---------------------------------------------------------------------- */ |
| 87 | |
| 88 | #define WIDTH 512 |
| 89 | #define HEIGHT (constell ? 512 : 256) |
| 90 | |
| 91 | static int openwindow(char *disp, int constell, int samplesperbit) |
| 92 | { |
| 93 | XSetWindowAttributes attr; |
| 94 | XGCValues gr_values; |
| 95 | XColor color, dummy; |
| 96 | XSizeHints sizehints; |
| 97 | |
| 98 | display = XOpenDisplay(NULL); |
| 99 | if (!display) |
| 100 | return -1; |
| 101 | XSetErrorHandler(x_error_handler); |
| 102 | XAllocNamedColor(display, DefaultColormap(display, 0), "red", |
| 103 | &color, &dummy); |
| 104 | col_zeroline = color.pixel; |
| 105 | col_background = WhitePixel(display, 0); |
| 106 | col_trace = BlackPixel(display, 0); |
| 107 | attr.background_pixel = col_background; |
| 108 | window = XCreateWindow(display, XRootWindow(display, 0), 200, 200, |
| 109 | WIDTH, HEIGHT, 5, DefaultDepth(display, 0), |
| 110 | InputOutput, DefaultVisual(display, 0), |
| 111 | CWBackPixel, &attr); |
| 112 | if (!window) { |
| 113 | fprintf(stderr, "smdiag: unable to open X window\n"); |
| 114 | exit(1); |
| 115 | } |
| 116 | pixmap = XCreatePixmap(display, window, WIDTH, HEIGHT, |
| 117 | DefaultDepth(display, 0)); |
| 118 | if (!pixmap) { |
| 119 | fprintf(stderr, "smdiag: unable to open offscreen pixmap\n"); |
| 120 | exit(1); |
| 121 | } |
| 122 | xmul = WIDTH / (2*(samplesperbit > 0 ? samplesperbit : 1)); |
| 123 | XSelectInput(display, window, KeyPressMask | StructureNotifyMask |
| 124 | | ExposureMask) ; |
| 125 | XAllocNamedColor(display, DefaultColormap(display, 0), "red", |
| 126 | &color, &dummy); |
| 127 | gr_values.foreground = col_trace; |
| 128 | gr_values.line_width = 1; |
| 129 | gr_values.line_style = LineSolid; |
| 130 | gr_context = XCreateGC(display, window, GCForeground | GCLineWidth | |
| 131 | GCLineStyle, &gr_values); |
| 132 | XStoreName(display, window, "diagnostics"); |
| 133 | /* |
| 134 | * Do not allow the window to be resized |
| 135 | */ |
| 136 | memset(&sizehints, 0, sizeof(sizehints)); |
| 137 | sizehints.min_width = sizehints.max_width = WIDTH; |
| 138 | sizehints.min_height = sizehints.max_height = HEIGHT; |
| 139 | sizehints.flags = PMinSize | PMaxSize; |
| 140 | XSetWMNormalHints(display, window, &sizehints); |
| 141 | XMapWindow(display, window); |
| 142 | XSynchronize(display, 1); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | #undef WIDTH |
| 147 | #undef HEIGHT |
| 148 | |
| 149 | /* ---------------------------------------------------------------------- */ |
| 150 | |
| 151 | static void closewindow(void) |
| 152 | { |
| 153 | if (!display) |
| 154 | return; |
| 155 | XDestroyWindow(display, window); |
| 156 | XCloseDisplay(display); |
| 157 | display = NULL; |
| 158 | } |
| 159 | |
| 160 | /* ---------------------------------------------------------------------- */ |
| 161 | |
| 162 | #define XCOORD(x) ((x) * xm) |
| 163 | #define YCOORD(y) ((SHRT_MAX - (int)(y)) * winattrs.height / USHRT_MAX) |
| 164 | |
| 165 | static void drawdata(short *data, int len, int replace, int xm) |
| 166 | { |
| 167 | int cnt; |
| 168 | GC gc; |
| 169 | XGCValues gcv; |
| 170 | XWindowAttributes winattrs; |
| 171 | |
| 172 | if (!display || !pixmap) |
| 173 | return; |
| 174 | XGetWindowAttributes(display, window, &winattrs); |
| 175 | gcv.line_width = 1; |
| 176 | gcv.line_style = LineSolid; |
| 177 | gc = XCreateGC(display, pixmap, GCLineWidth | GCLineStyle, &gcv); |
| 178 | XSetState(display, gc, col_background, col_background, GXcopy, |
| 179 | AllPlanes); |
| 180 | if (replace) |
| 181 | XFillRectangle(display, pixmap, gc, 0, 0, |
| 182 | winattrs.width, winattrs.height); |
| 183 | else |
| 184 | XCopyArea(display, window, pixmap, gr_context, 0, 0, |
| 185 | winattrs.width, winattrs.height, 0, 0); |
| 186 | XSetForeground(display, gc, col_trace); |
| 187 | for (cnt = 0; cnt < len-1; cnt++) |
| 188 | XDrawLine(display, pixmap, gc, XCOORD(cnt), YCOORD(data[cnt]), |
| 189 | XCOORD(cnt+1), YCOORD(data[cnt+1])); |
| 190 | XSetForeground(display, gc, col_zeroline); |
| 191 | XDrawLine(display, pixmap, gc, 0, YCOORD(0), winattrs.width, |
| 192 | YCOORD(0)); |
| 193 | XCopyArea(display, pixmap, window, gr_context, 0, 0, winattrs.width, |
| 194 | winattrs.height, 0, 0); |
| 195 | XFreeGC(display, gc); |
| 196 | XSync(display, 0); |
| 197 | } |
| 198 | |
| 199 | #undef XCOORD |
| 200 | #undef YCOORD |
| 201 | |
| 202 | /* ---------------------------------------------------------------------- */ |
| 203 | |
| 204 | #define XCOORD(x) ((SHRT_MAX - (int)(x)) * winattrs.width / USHRT_MAX) |
| 205 | #define YCOORD(y) ((SHRT_MAX - (int)(y)) * winattrs.height / USHRT_MAX) |
| 206 | |
| 207 | static void drawconstell(short *data, int len) |
| 208 | { |
| 209 | int cnt; |
| 210 | GC gc; |
| 211 | XGCValues gcv; |
| 212 | XWindowAttributes winattrs; |
| 213 | |
| 214 | if (!display || !pixmap) |
| 215 | return; |
| 216 | XGetWindowAttributes(display, window, &winattrs); |
| 217 | gcv.line_width = 1; |
| 218 | gcv.line_style = LineSolid; |
| 219 | gc = XCreateGC(display, pixmap, GCLineWidth | GCLineStyle, &gcv); |
| 220 | XSetState(display, gc, col_background, col_background, GXcopy, |
| 221 | AllPlanes); |
| 222 | XCopyArea(display, window, pixmap, gr_context, 0, 0, |
| 223 | winattrs.width, winattrs.height, 0, 0); |
| 224 | XSetForeground(display, gc, col_trace); |
| 225 | for (cnt = 0; cnt < len-1; cnt += 2) |
| 226 | XDrawPoint(display, pixmap, gc, |
| 227 | XCOORD(data[cnt]), YCOORD(data[cnt+1])); |
| 228 | XSetForeground(display, gc, col_zeroline); |
| 229 | XDrawLine(display, pixmap, gc, 0, YCOORD(0), winattrs.width, YCOORD(0)); |
| 230 | XDrawLine(display, pixmap, gc, XCOORD(0), 0, XCOORD(0), winattrs.height); |
| 231 | XCopyArea(display, pixmap, window, gr_context, 0, 0, winattrs.width, |
| 232 | winattrs.height, 0, 0); |
| 233 | XFreeGC(display, gc); |
| 234 | XSync(display, 0); |
| 235 | } |
| 236 | |
| 237 | #undef XCOORD |
| 238 | #undef YCOORD |
| 239 | |
| 240 | /* ---------------------------------------------------------------------- */ |
| 241 | |
| 242 | static void clearwindow(void) |
| 243 | { |
| 244 | XWindowAttributes winattrs; |
| 245 | GC gc; |
| 246 | XGCValues gcv; |
| 247 | |
| 248 | if (!display || !pixmap) |
| 249 | return; |
| 250 | XGetWindowAttributes(display, window, &winattrs); |
| 251 | gcv.line_width = 1; |
| 252 | gcv.line_style = LineSolid; |
| 253 | gc = XCreateGC(display, pixmap, GCLineWidth | GCLineStyle, &gcv); |
| 254 | XSetState(display, gc, col_background, col_background, GXcopy, |
| 255 | AllPlanes); |
| 256 | XFillRectangle(display, pixmap, gc, 0, 0, |
| 257 | winattrs.width, winattrs.height); |
| 258 | XSetForeground(display, gc, col_zeroline); |
| 259 | XClearArea(display, window, 0, 0, 0, 0, False); |
| 260 | XCopyArea(display, pixmap, window, gr_context, 0, 0, winattrs.width, |
| 261 | winattrs.height, 0, 0); |
| 262 | XFreeGC(display, gc); |
| 263 | } |
| 264 | |
| 265 | /* ---------------------------------------------------------------------- */ |
| 266 | |
| 267 | static Bool predicate(Display *display, XEvent *event, char *arg) |
| 268 | { |
| 269 | return True; |
| 270 | } |
| 271 | |
| 272 | /* ---------------------------------------------------------------------- */ |
| 273 | |
| 274 | static char *getkey(void) |
| 275 | { |
| 276 | XWindowAttributes winattrs; |
| 277 | XEvent evt; |
| 278 | static char kbuf[32]; |
| 279 | int i; |
| 280 | |
| 281 | if (!display) |
| 282 | return NULL; |
| 283 | XSync(display, 0); |
| 284 | while (XCheckIfEvent(display, &evt, predicate, NULL)) { |
| 285 | switch (evt.type) { |
| 286 | case KeyPress: |
| 287 | i = XLookupString((XKeyEvent *)&evt, kbuf, sizeof(kbuf)-1, |
| 288 | NULL, NULL); |
| 289 | if (!i) |
| 290 | return NULL; |
| 291 | kbuf[i] = 0; |
| 292 | return kbuf; |
| 293 | case DestroyNotify: |
| 294 | XCloseDisplay(display); |
| 295 | display = NULL; |
| 296 | return NULL; |
| 297 | case Expose: |
| 298 | XGetWindowAttributes(display, window, &winattrs); |
| 299 | XCopyArea(display, pixmap, window, gr_context, 0, 0, |
| 300 | winattrs.width, winattrs.height, 0, 0); |
| 301 | break; |
| 302 | default: |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | /* ---------------------------------------------------------------------- */ |
| 310 | |
| 311 | static void printmode(unsigned int mode, unsigned int trigger) |
| 312 | { |
| 313 | printf("Source: %s%s\n", (mode == SM_DIAGMODE_DEMOD) ? |
| 314 | "demodulator (eye diagram)" : "input (oscilloscope)", |
| 315 | (trigger & SM_DIAGFLAG_DCDGATE) ? " gated with DCD" : ""); |
| 316 | } |
| 317 | |
| 318 | /* ---------------------------------------------------------------------- */ |
| 319 | |
| 320 | static const char *usage_str = |
| 321 | "[-d display] [-i smif] [-c] [-e]\n" |
| 322 | " -d: display host\n" |
| 323 | " -i: specify the name of the baycom kernel driver interface\n" |
| 324 | " -c: toggle carrier trigger\n" |
| 325 | " -e: eye diagram mode\n\n" |
| 326 | " -p: constellation plot\n\n"; |
| 327 | |
| 328 | int main(int argc, char *argv[]) |
| 329 | { |
| 330 | char *disp = NULL; |
| 331 | unsigned int mode = HDRVC_DIAGMODE_INPUT; |
| 332 | unsigned int trigger = 0; |
| 333 | unsigned int ifflags; |
| 334 | short data[256]; |
| 335 | char *cp; |
| 336 | int ret; |
| 337 | unsigned int samplesperbit; |
| 338 | |
| 339 | progname = *argv; |
| 340 | printf("%s: Version " FULL_VER "; (C) 1996-1997 by Thomas Sailer HB9JNX/AE4WA\n", *argv); |
| 341 | hdrvc_args(&argc, argv, "sm0"); |
| 342 | while ((ret = getopt(argc, argv, "d:ecp")) != -1) { |
| 343 | switch (ret) { |
| 344 | case 'd': |
| 345 | disp = optarg; |
| 346 | break; |
| 347 | case 'e': |
| 348 | mode = HDRVC_DIAGMODE_DEMOD; |
| 349 | trigger = HDRVC_DIAGFLAG_DCDGATE; |
| 350 | break; |
| 351 | case 'c': |
| 352 | trigger ^= HDRVC_DIAGFLAG_DCDGATE; |
| 353 | break; |
| 354 | case 'p': |
| 355 | mode = HDRVC_DIAGMODE_CONSTELLATION; |
| 356 | break; |
| 357 | default: |
| 358 | printf("usage: %s %s", *argv, usage_str); |
| 359 | exit(-1); |
| 360 | } |
| 361 | } |
| 362 | hdrvc_init(); |
| 363 | ifflags = hdrvc_get_ifflags(); |
| 364 | if (!(ifflags & IFF_UP)) { |
| 365 | fprintf(stderr, "interface %s down\n", hdrvc_ifname()); |
| 366 | exit(1); |
| 367 | } |
| 368 | if (!(ifflags & IFF_RUNNING)) { |
| 369 | fprintf(stderr, "interface %s not running\n", hdrvc_ifname()); |
| 370 | exit(1); |
| 371 | } |
| 372 | printmode(mode, trigger); |
| 373 | for (;;) { |
| 374 | ret = hdrvc_diag2(mode, trigger, data, |
| 375 | sizeof(data) / sizeof(short), |
| 376 | &samplesperbit); |
| 377 | if (ret < 0) { |
| 378 | perror("hdrvc_diag2"); |
| 379 | exit(1); |
| 380 | } |
| 381 | if (ret > 0) { |
| 382 | if (!display) { |
| 383 | openwindow(disp, mode == HDRVC_DIAGMODE_CONSTELLATION, |
| 384 | samplesperbit); |
| 385 | clearwindow(); |
| 386 | } |
| 387 | if (mode == SM_DIAGMODE_CONSTELLATION) |
| 388 | drawconstell(data, ret); |
| 389 | else |
| 390 | drawdata(data, ret, mode == HDRVC_DIAGMODE_INPUT, |
| 391 | mode == HDRVC_DIAGMODE_INPUT ? 5:xmul); |
| 392 | } else |
| 393 | usleep(100000L); |
| 394 | if (display) { |
| 395 | cp = getkey(); |
| 396 | if (cp) { |
| 397 | for (; *cp; cp++) { |
| 398 | printf("char pressed: '%c'\n", *cp); |
| 399 | switch (*cp) { |
| 400 | case 'c': |
| 401 | case 'C': |
| 402 | clearwindow(); |
| 403 | printmode(mode, trigger); |
| 404 | break; |
| 405 | case 'q': |
| 406 | case 'Q': |
| 407 | closewindow(); |
| 408 | break; |
| 409 | case 'i': |
| 410 | case 'I': |
| 411 | if (mode == HDRVC_DIAGMODE_CONSTELLATION) |
| 412 | break; |
| 413 | mode = HDRVC_DIAGMODE_INPUT; |
| 414 | clearwindow(); |
| 415 | printmode(mode, trigger); |
| 416 | break; |
| 417 | case 'e': |
| 418 | case 'E': |
| 419 | if (mode == HDRVC_DIAGMODE_CONSTELLATION) |
| 420 | break; |
| 421 | mode = HDRVC_DIAGMODE_DEMOD; |
| 422 | clearwindow(); |
| 423 | printmode(mode, trigger); |
| 424 | break; |
| 425 | case 'd': |
| 426 | case 'D': |
| 427 | trigger ^= HDRVC_DIAGFLAG_DCDGATE; |
| 428 | clearwindow(); |
| 429 | printmode(mode, trigger); |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | if (!display) |
| 435 | exit(0); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* ---------------------------------------------------------------------- */ |