| 1 | /*****************************************************************************/ |
| 2 | |
| 3 | /* |
| 4 | * xfsmdiag.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 14.12.1996 Started |
| 30 | * 0.2 11.05.1997 introduced hdrvcomm.h |
| 31 | * 0.3 05.01.2000 upgraded to fltk 1.0.7 |
| 32 | */ |
| 33 | |
| 34 | /*****************************************************************************/ |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <stdio.h> |
| 38 | #include <sys/time.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <unistd.h> |
| 42 | #include <string.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <limits.h> |
| 45 | #include <net/if.h> |
| 46 | #include <X11/X.h> |
| 47 | #include <X11/Xlib.h> |
| 48 | #include <X11/Xutil.h> |
| 49 | #include "hdrvcomm.h" |
| 50 | #include "xfsmdiag.h" |
| 51 | #include <FL/x.H> |
| 52 | #include <FL/fl_draw.H> |
| 53 | |
| 54 | #include <config.h> |
| 55 | #include <scm-version.h> |
| 56 | |
| 57 | /* ---------------------------------------------------------------------- */ |
| 58 | |
| 59 | static char *progname; |
| 60 | static unsigned int drawflags; |
| 61 | |
| 62 | /* ---------------------------------------------------------------------- */ |
| 63 | |
| 64 | scope::scope(int x, int y, int w, int h, const char *l) |
| 65 | : Fl_Box(x, y, w, h, l) |
| 66 | { |
| 67 | box(FL_DOWN_FRAME); |
| 68 | X = x+3; |
| 69 | Y = y+4; |
| 70 | W = w-6; |
| 71 | H = h-8; |
| 72 | drawmode = HDRVC_DIAGMODE_OFF; |
| 73 | pixmalloc = false; |
| 74 | hide(); |
| 75 | } |
| 76 | |
| 77 | scope::~scope() |
| 78 | { |
| 79 | if (pixmalloc) { |
| 80 | XFreePixmap(fl_display, pixmap); |
| 81 | pixmalloc = false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void scope::resize(int xx, int yy, int ww, int hh) |
| 86 | { |
| 87 | if (pixmalloc && (ww != w() || hh != h())) { |
| 88 | XFreePixmap(fl_display, pixmap); |
| 89 | pixmalloc = false; |
| 90 | } |
| 91 | X = xx+3; |
| 92 | Y = yy+4; |
| 93 | W = ww-6; |
| 94 | H = hh-8; |
| 95 | Fl_Box::resize(xx, yy, ww, hh); |
| 96 | } |
| 97 | |
| 98 | void scope::draw() |
| 99 | { |
| 100 | GC gc; |
| 101 | XGCValues gcv; |
| 102 | |
| 103 | /* cannot use draw_box(); as it clears the whole window -> flicker */ |
| 104 | /* from fl_boxtype.C, fl_down_frame */ |
| 105 | if (W > 0 && H > 0) { |
| 106 | fl_color(FL_DARK1); fl_xyline(X-3, Y-4, X+W+1); |
| 107 | fl_color(FL_DARK2); fl_yxline(X-3, Y+H+3, Y-3, X+W+1); |
| 108 | fl_color(FL_DARK3); fl_yxline(X-2, Y+H+2, Y-2, X+W); |
| 109 | fl_color(FL_LIGHT3); fl_xyline(X-1, Y+H+1, X+W+1, Y-2); |
| 110 | fl_color(FL_LIGHT2); fl_xyline(X-2, Y+H+2, X+W+2, Y-4); |
| 111 | fl_color(FL_LIGHT1); fl_xyline(X-3, Y+H+3, X+W+2); |
| 112 | } |
| 113 | draw_label(); |
| 114 | if (pixmalloc) |
| 115 | XCopyArea(fl_display, pixmap, fl_window, fl_gc, 0, 0, W, H, X, Y); |
| 116 | else { |
| 117 | gcv.line_width = 1; |
| 118 | gcv.line_style = LineSolid; |
| 119 | gcv.fill_style = FillSolid; |
| 120 | gc = XCreateGC(fl_display, pixmap, GCLineWidth | GCLineStyle | GCFillStyle, &gcv); |
| 121 | XSetState(fl_display, gc, col_background, col_background, GXcopy, AllPlanes); |
| 122 | XFillRectangle(fl_display, fl_window, gc, X, Y, W, H); |
| 123 | XFreeGC(fl_display, gc); |
| 124 | } |
| 125 | XSync(fl_display, 0); |
| 126 | } |
| 127 | |
| 128 | /* ---------------------------------------------------------------------- */ |
| 129 | |
| 130 | int scope::mode(void) |
| 131 | { |
| 132 | mode(drawmode); |
| 133 | return drawmode; |
| 134 | } |
| 135 | |
| 136 | void scope::mode(int dmode) |
| 137 | { |
| 138 | Fl_Window *wnd; |
| 139 | |
| 140 | if (dmode != drawmode) { |
| 141 | if (pixmalloc) { |
| 142 | XFreePixmap(fl_display, pixmap); |
| 143 | pixmalloc = false; |
| 144 | } |
| 145 | drawmode = dmode; |
| 146 | switch (drawmode) { |
| 147 | case HDRVC_DIAGMODE_OFF: |
| 148 | default: |
| 149 | hide(); |
| 150 | break; |
| 151 | |
| 152 | case HDRVC_DIAGMODE_INPUT: |
| 153 | case HDRVC_DIAGMODE_DEMOD: |
| 154 | size(512+6, 256+8); |
| 155 | show(); |
| 156 | break; |
| 157 | |
| 158 | case HDRVC_DIAGMODE_CONSTELLATION: |
| 159 | size(512+6, 512+8); |
| 160 | show(); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | if (pixmalloc || !visible() || W < 2 || H < 2) |
| 165 | return; |
| 166 | if (!(wnd = window())) |
| 167 | return; |
| 168 | wnd->make_current(); |
| 169 | if (!(pixmap = XCreatePixmap(fl_display, fl_window, W, H, fl_visual->depth))) { |
| 170 | fprintf(stderr, "unable to open offscreen pixmap\n"); |
| 171 | exit(1); |
| 172 | } |
| 173 | pixmalloc = true; |
| 174 | col_zeroline = fl_xpixel(FL_RED); |
| 175 | col_background = fl_xpixel(FL_WHITE); |
| 176 | col_trace = fl_xpixel(FL_BLACK); |
| 177 | clear(); |
| 178 | } |
| 179 | |
| 180 | /* ---------------------------------------------------------------------- */ |
| 181 | |
| 182 | void scope::clear(void) |
| 183 | { |
| 184 | GC gc; |
| 185 | XGCValues gcv; |
| 186 | |
| 187 | if (!pixmalloc) |
| 188 | return; |
| 189 | gcv.line_width = 1; |
| 190 | gcv.line_style = LineSolid; |
| 191 | gcv.fill_style = FillSolid; |
| 192 | gc = XCreateGC(fl_display, pixmap, GCLineWidth | GCLineStyle | GCFillStyle, &gcv); |
| 193 | XSetState(fl_display, gc, col_background, col_background, GXcopy, AllPlanes); |
| 194 | XFillRectangle(fl_display, pixmap, gc, 0, 0, W, H); |
| 195 | XFreeGC(fl_display, gc); |
| 196 | redraw(); |
| 197 | } |
| 198 | |
| 199 | /* ---------------------------------------------------------------------- */ |
| 200 | |
| 201 | #define WIDTH 512 |
| 202 | |
| 203 | /* ---------------------------------------------------------------------- */ |
| 204 | |
| 205 | void scope::drawdata(short data[], int len, int xm) |
| 206 | { |
| 207 | int cnt; |
| 208 | GC gc; |
| 209 | XGCValues gcv; |
| 210 | |
| 211 | mode(); |
| 212 | if (!pixmalloc || (drawmode != HDRVC_DIAGMODE_CONSTELLATION && |
| 213 | drawmode != HDRVC_DIAGMODE_INPUT && |
| 214 | drawmode != HDRVC_DIAGMODE_DEMOD)) |
| 215 | return; |
| 216 | gcv.line_width = 1; |
| 217 | gcv.line_style = LineSolid; |
| 218 | gc = XCreateGC(fl_display, pixmap, GCLineWidth | GCLineStyle, &gcv); |
| 219 | if (drawmode == HDRVC_DIAGMODE_CONSTELLATION) { |
| 220 | #define XCOORD(x) ((SHRT_MAX - (int)(x)) * W / USHRT_MAX) |
| 221 | #define YCOORD(y) ((SHRT_MAX - (int)(y)) * H / USHRT_MAX) |
| 222 | XSetState(fl_display, gc, col_background, col_background, GXcopy, AllPlanes); |
| 223 | XSetForeground(fl_display, gc, col_trace); |
| 224 | for (cnt = 0; cnt < len-1; cnt += 2) |
| 225 | XDrawPoint(fl_display, pixmap, gc, XCOORD(data[cnt]), YCOORD(data[cnt+1])); |
| 226 | XSetForeground(fl_display, gc, col_zeroline); |
| 227 | XDrawLine(fl_display, pixmap, gc, 0, YCOORD(0), W, YCOORD(0)); |
| 228 | XDrawLine(fl_display, pixmap, gc, XCOORD(0), 0, XCOORD(0), H); |
| 229 | #undef XCOORD |
| 230 | #undef YCOORD |
| 231 | } else { |
| 232 | #define XCOORD(x) ((x) * xm) |
| 233 | #define YCOORD(y) ((SHRT_MAX - (int)(y)) * H / USHRT_MAX) |
| 234 | XSetState(fl_display, gc, col_background, col_background, GXcopy, AllPlanes); |
| 235 | if (drawmode == HDRVC_DIAGMODE_INPUT) { |
| 236 | XFillRectangle(fl_display, pixmap, gc, 0, 0, W, H); |
| 237 | xm = 5; |
| 238 | } |
| 239 | XSetForeground(fl_display, gc, col_trace); |
| 240 | for (cnt = 0; cnt < len-1; cnt++) |
| 241 | XDrawLine(fl_display, pixmap, gc, XCOORD(cnt), YCOORD(data[cnt]), |
| 242 | XCOORD(cnt+1), YCOORD(data[cnt+1])); |
| 243 | XSetForeground(fl_display, gc, col_zeroline); |
| 244 | XDrawLine(fl_display, pixmap, gc, 0, YCOORD(0), W, YCOORD(0)); |
| 245 | #undef XCOORD |
| 246 | #undef YCOORD |
| 247 | } |
| 248 | XFreeGC(fl_display, gc); |
| 249 | redraw(); |
| 250 | } |
| 251 | |
| 252 | /* ---------------------------------------------------------------------- */ |
| 253 | |
| 254 | void cb_cleargr(Fl_Button *, long) |
| 255 | { |
| 256 | scdisp->clear(); |
| 257 | } |
| 258 | |
| 259 | /* ---------------------------------------------------------------------- */ |
| 260 | |
| 261 | void cb_mode(Fl_Check_Button *, long which) |
| 262 | { |
| 263 | struct sm_diag_data diag; |
| 264 | short data; |
| 265 | |
| 266 | diag.mode = HDRVC_DIAGMODE_OFF; |
| 267 | diag.flags = 0; |
| 268 | diag.datalen = 1; |
| 269 | diag.data = &data; |
| 270 | hdrvc_diag(&diag); |
| 271 | switch (which) { |
| 272 | case 256: |
| 273 | drawflags ^= HDRVC_DIAGFLAG_DCDGATE; |
| 274 | break; |
| 275 | |
| 276 | case 0: |
| 277 | scdisp->mode(HDRVC_DIAGMODE_OFF); |
| 278 | drawflags = 0; |
| 279 | break; |
| 280 | |
| 281 | case 1: |
| 282 | scdisp->mode(HDRVC_DIAGMODE_INPUT); |
| 283 | drawflags = 0; |
| 284 | break; |
| 285 | |
| 286 | case 2: |
| 287 | scdisp->mode(HDRVC_DIAGMODE_DEMOD); |
| 288 | drawflags = HDRVC_DIAGFLAG_DCDGATE; |
| 289 | break; |
| 290 | |
| 291 | case 3: |
| 292 | scdisp->mode(HDRVC_DIAGMODE_CONSTELLATION); |
| 293 | drawflags = HDRVC_DIAGFLAG_DCDGATE; |
| 294 | break; |
| 295 | } |
| 296 | sm_dcd->value(!!(drawflags & HDRVC_DIAGFLAG_DCDGATE)); |
| 297 | } |
| 298 | |
| 299 | /* ---------------------------------------------------------------------- */ |
| 300 | |
| 301 | void cb_quit(Fl_Button *, long) |
| 302 | { |
| 303 | struct sm_diag_data diag; |
| 304 | short data; |
| 305 | |
| 306 | diag.mode = HDRVC_DIAGMODE_OFF; |
| 307 | diag.flags = 0; |
| 308 | diag.datalen = 1; |
| 309 | diag.data = &data; |
| 310 | hdrvc_diag(&diag); |
| 311 | exit(0); |
| 312 | } |
| 313 | |
| 314 | /* ---------------------------------------------------------------------- */ |
| 315 | |
| 316 | void cb_timer(void *) |
| 317 | { |
| 318 | struct hdrvc_channel_state cs; |
| 319 | int ret; |
| 320 | short data[256]; |
| 321 | unsigned int samplesperbit; |
| 322 | |
| 323 | Fl::add_timeout(0.2, cb_timer); |
| 324 | /* |
| 325 | * display state |
| 326 | */ |
| 327 | ret = hdrvc_get_channel_state(&cs); |
| 328 | if (ret < 0) { |
| 329 | perror("hdrvc_get_channel_state"); |
| 330 | } else { |
| 331 | st_ptt->value(cs.ptt); |
| 332 | st_dcd->value(cs.dcd); |
| 333 | } |
| 334 | /* |
| 335 | * draw scope |
| 336 | */ |
| 337 | if ((ret = hdrvc_diag2(scdisp->mode(), drawflags, data, sizeof(data) / sizeof(short), |
| 338 | &samplesperbit)) < 0) { |
| 339 | perror("hdrvc_diag2"); |
| 340 | exit(1); |
| 341 | } |
| 342 | if (ret > 0) |
| 343 | scdisp->drawdata(data, ret, WIDTH / (2*(samplesperbit > 0 ? samplesperbit : 1))); |
| 344 | } |
| 345 | |
| 346 | /* ---------------------------------------------------------------------- */ |
| 347 | |
| 348 | static const char *usage_str = |
| 349 | "[-i smif]\n" |
| 350 | " -i: specify the name of the baycom kernel driver interface\n\n"; |
| 351 | |
| 352 | /* ---------------------------------------------------------------------- */ |
| 353 | |
| 354 | int main(int argc, char *argv[]) |
| 355 | { |
| 356 | int c, i; |
| 357 | int ret; |
| 358 | unsigned int ifflags; |
| 359 | char name[64]; |
| 360 | |
| 361 | progname = *argv; |
| 362 | printf("%s: Version " FULL_VER "; (C) 1996,1997,2000 by Thomas Sailer HB9JNX/AE4WA\n", *argv); |
| 363 | |
| 364 | hdrvc_args(&argc, argv, "sm0"); |
| 365 | for (i = 1; i < argc; ) { |
| 366 | c = i; |
| 367 | Fl::arg(argc, argv, c); |
| 368 | if (c <= 0) { |
| 369 | i++; |
| 370 | continue; |
| 371 | } |
| 372 | memmove(&argv[i], &argv[i+c], (argc-i) * sizeof(char *)); |
| 373 | argc -= c; |
| 374 | } |
| 375 | #if 0 |
| 376 | while ((ret = getopt(argc, argv, "")) != -1) { |
| 377 | switch (ret) { |
| 378 | default: |
| 379 | printf("usage: %s %s", *argv, usage_str); |
| 380 | exit(-1); |
| 381 | } |
| 382 | } |
| 383 | #endif |
| 384 | hdrvc_init(); |
| 385 | ifflags = hdrvc_get_ifflags(); |
| 386 | if (!(ifflags & IFF_UP)) { |
| 387 | fprintf(stderr, "interface %s down\n", hdrvc_ifname()); |
| 388 | exit(1); |
| 389 | } |
| 390 | if (!(ifflags & IFF_RUNNING)) { |
| 391 | fprintf(stderr, "interface %s not running\n", hdrvc_ifname()); |
| 392 | exit(1); |
| 393 | } |
| 394 | create_the_forms(); |
| 395 | scdisp->hide(); |
| 396 | /* |
| 397 | * set driver and modem name |
| 398 | */ |
| 399 | ret = hdrvc_get_mode_name(name, sizeof(name)); |
| 400 | if (ret < 0) { |
| 401 | perror("hdrvc_get_mode_name"); |
| 402 | modename->hide(); |
| 403 | } else |
| 404 | modename->value(name); |
| 405 | ret = hdrvc_get_driver_name(name, sizeof(name)); |
| 406 | if (ret < 0) { |
| 407 | perror("hdrvc_get_driver_name"); |
| 408 | drivername->hide(); |
| 409 | } else |
| 410 | drivername->value(name); |
| 411 | Fl::add_timeout(0.1, cb_timer); |
| 412 | scopewindow->show(); |
| 413 | Fl::run(); |
| 414 | cb_quit(quit, 0); |
| 415 | exit(0); |
| 416 | } |