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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
/*****************************************************************************/
/*
* stlstats.c -- stallion intelligent multiport stats display.
*
* Copyright (c) 1994-1996 Greg Ungerer (gerg@stallion.oz.au).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Greg Ungerer.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*****************************************************************************/
#ifndef lint
static const char rcsid[] =
"$FreeBSD$";
#endif /* not lint */
#include <err.h>
#include <fcntl.h>
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <machine/cdk.h>
#include <machine/comstats.h>
/*****************************************************************************/
char *version = "1.0.0";
char *defdevice = "/dev/staliomem0";
char *ctrldevice;
int ctrlfd;
int displaybrdnr = 0;
int displaypanelnr = 0;
int displayportnr = 0;
int displayportbank = 0;
#define MAXBRDS 8
#define MAXPORTS 32
combrd_t brdstats;
comstats_t stats[MAXPORTS];
char *line = " ";
/*****************************************************************************/
/*
* Declare internal function prototypes here.
*/
static void usage(void);
void useportdevice(char *devname);
void localexit(int nr);
void menuport();
void displayport();
void menuallports();
void displayallports();
void getallstats();
void getbrdstats();
void clearportstats();
void clearallstats();
/*****************************************************************************/
static void usage()
{
fprintf(stderr, "%s\n%s\n",
"usage: stlstats [-hVi] [-c control-device] [-b board-number]",
" [-p port-number] [-d port-device]");
exit(0);
}
/*****************************************************************************/
void useportdevice(char *devname)
{
struct stat statinfo;
int portnr, portcnt;
int i;
if (stat(devname, &statinfo) < 0)
errx(1, "port device %s does not exist", devname);
if ((statinfo.st_mode & S_IFMT) != S_IFCHR)
errx(1, "port device %s is not a char device", devname);
displaybrdnr = (statinfo.st_rdev & 0x00700000) >> 20;
portnr = (statinfo.st_rdev & 0x1f) |
((statinfo.st_rdev & 0x00010000) >> 11);
getbrdstats();
if (brdstats.ioaddr == 0)
errx(1, "device %s does not exist", devname);
for (portcnt = 0, i = 0; (i < brdstats.nrpanels); i++) {
if ((portnr >= portcnt) &&
(portnr < (portcnt + brdstats.panels[i].nrports)))
break;
portcnt += brdstats.panels[i].nrports;
}
if (i >= brdstats.nrpanels)
errx(1, "device %s does not exist", devname);
displaypanelnr = i;
displayportnr = portnr - portcnt;
if (displayportnr >= 16)
displayportbank = 16;
}
/*****************************************************************************/
/*
* Get the board stats for the current display board.
*/
void getbrdstats()
{
brdstats.brd = displaybrdnr;
if (ioctl(ctrlfd, COM_GETBRDSTATS, &brdstats) < 0)
memset((combrd_t *) &brdstats, 0, sizeof(combrd_t));
}
/*****************************************************************************/
/*
* Zero out stats for the current display port.
*/
void clearportstats()
{
stats[displayportnr].brd = displaybrdnr;
stats[displayportnr].panel = displaypanelnr;
stats[displayportnr].port = displayportnr;
ioctl(ctrlfd, COM_CLRPORTSTATS, &stats[displayportnr]);
}
/*****************************************************************************/
/*
* Zero out all stats for all ports on all boards.
*/
void clearallstats()
{
int brdnr, panelnr, portnr;
for (brdnr = 0; (brdnr < MAXBRDS); brdnr++) {
for (panelnr = 0; (panelnr < COM_MAXPANELS); panelnr++) {
for (portnr = 0; (portnr < MAXPORTS); portnr++) {
stats[0].brd = brdnr;
stats[0].panel = panelnr;
stats[0].port = portnr;
ioctl(ctrlfd, COM_CLRPORTSTATS, &stats[0]);
}
}
}
}
/*****************************************************************************/
/*
* Get the stats for the current display board/panel.
*/
void getallstats()
{
int i;
for (i = 0; (i < brdstats.panels[displaypanelnr].nrports); i++) {
stats[i].brd = displaybrdnr;
stats[i].panel = displaypanelnr;
stats[i].port = i;
if (ioctl(ctrlfd, COM_GETPORTSTATS, &stats[i]) < 0) {
warn("ioctl(COM_GETPORTSTATS) failed");
localexit(1);
}
}
}
/*****************************************************************************/
/*
* Display the per ports stats screen.
*/
void displayport()
{
mvprintw(0, 0, "STALLION SERIAL PORT STATISTICS");
mvprintw(2, 0,
"Board=%d Type=%d HwID=%02x State=%06x TotalPorts=%d",
displaybrdnr, brdstats.type, brdstats.hwid, brdstats.state,
brdstats.nrports);
mvprintw(3, 0, "Panel=%d HwID=%02x Ports=%d", displaypanelnr,
brdstats.panels[displaypanelnr].hwid,
brdstats.panels[displaypanelnr].nrports);
attron(A_REVERSE);
mvprintw(5, 0, line);
mvprintw(5, 0, "Port=%d ", displayportnr);
attroff(A_REVERSE);
mvprintw(7, 0, "STATE: State=%08x", stats[displayportnr].state);
mvprintw(7, 29, "Tty=%08x", stats[displayportnr].ttystate);
mvprintw(7, 47, "Flags=%08x", stats[displayportnr].flags);
mvprintw(7, 65, "HwID=%02x", stats[displayportnr].hwid);
mvprintw(8, 0, "CONFIG: Cflag=%08x", stats[displayportnr].cflags);
mvprintw(8, 29, "Iflag=%08x", stats[displayportnr].iflags);
mvprintw(8, 47, "Oflag=%08x", stats[displayportnr].oflags);
mvprintw(8, 65, "Lflag=%08x", stats[displayportnr].lflags);
mvprintw(10, 0, "TX DATA: Total=%d", stats[displayportnr].txtotal);
mvprintw(10, 29, "Buffered=%d ", stats[displayportnr].txbuffered);
mvprintw(11, 0, "RX DATA: Total=%d", stats[displayportnr].rxtotal);
mvprintw(11, 29, "Buffered=%d ", stats[displayportnr].rxbuffered);
mvprintw(12, 0, "RX ERRORS: Parity=%d", stats[displayportnr].rxparity);
mvprintw(12, 29, "Framing=%d", stats[displayportnr].rxframing);
mvprintw(12, 47, "Overrun=%d", stats[displayportnr].rxoverrun);
mvprintw(12, 65, "Lost=%d", stats[displayportnr].rxlost);
mvprintw(14, 0, "FLOW TX: Xoff=%d", stats[displayportnr].txxoff);
mvprintw(14, 29, "Xon=%d", stats[displayportnr].txxon);
#if 0
mvprintw(14, 47, "CTSoff=%d", stats[displayportnr].txctsoff);
mvprintw(14, 65, "CTSon=%d", stats[displayportnr].txctson);
#endif
mvprintw(15, 0, "FLOW RX: Xoff=%d", stats[displayportnr].rxxoff);
mvprintw(15, 29, "Xon=%d", stats[displayportnr].rxxon);
mvprintw(15, 47, "RTSoff=%d", stats[displayportnr].rxrtsoff);
mvprintw(15, 65, "RTSon=%d", stats[displayportnr].rxrtson);
mvprintw(17, 0, "OTHER: TXbreaks=%d",
stats[displayportnr].txbreaks);
mvprintw(17, 29, "RXbreaks=%d", stats[displayportnr].rxbreaks);
mvprintw(17, 47, "Modem=%d", stats[displayportnr].modem);
mvprintw(19, 0, "SIGNALS: DCD=%d DTR=%d CTS=%d RTS=%d "
"DSR=%d RI=%d",
(stats[displayportnr].signals & TIOCM_CD) ? 1 : 0,
(stats[displayportnr].signals & TIOCM_DTR) ? 1 : 0,
(stats[displayportnr].signals & TIOCM_CTS) ? 1 : 0,
(stats[displayportnr].signals & TIOCM_RTS) ? 1 : 0,
(stats[displayportnr].signals & TIOCM_DSR) ? 1 : 0,
(stats[displayportnr].signals & TIOCM_RI) ? 1 : 0);
attron(A_REVERSE);
mvprintw(22, 0, line);
attroff(A_REVERSE);
mvprintw(24, 19, "(q=Quit,0123456789abcdef=Port,Z=ZeroStats)");
refresh();
}
/*****************************************************************************/
/*
* Continuously update and display the per ports stats screen.
* Also checks for keyboard input, and processes it as appropriate.
*/
void menuport()
{
int ch, done;
clear();
done = 0;
while ((ch = getch()) != 27) {
switch (ch) {
case ERR:
break;
case '':
refresh();
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
ch = (ch - 'a' + '0' + 10);
/* fall thru */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
ch -= '0';
if (ch >= brdstats.panels[displaypanelnr].nrports) {
beep();
} else {
displayportnr = displayportbank + ch;
clear();
}
break;
case 'Z':
clearportstats();
clear();
break;
case 'q':
done = 1;
break;
default:
beep();
break;
}
if (done)
break;
getallstats();
displayport();
}
}
/*****************************************************************************/
/*
* Display the all ports stats screen.
*/
void displayallports()
{
int i, nrports, portnr;;
nrports = brdstats.panels[displaypanelnr].nrports;
mvprintw(0, 0, "STALLION SERIAL PORT STATISTICS");
mvprintw(2, 0, "Board=%d Type=%d HwID=%02x State=%06x TotalPorts=%d",
displaybrdnr, brdstats.type, brdstats.hwid, brdstats.state,
brdstats.nrports);
mvprintw(3, 0, "Panel=%d HwID=%02x Ports=%d", displaypanelnr,
brdstats.panels[displaypanelnr].hwid, nrports);
attron(A_REVERSE);
mvprintw(5, 0, "Port State Tty Flags Cflag Iflag Oflag Lflag "
"Sigs TX Total RX Total ");
attroff(A_REVERSE);
if (nrports > 0) {
if (nrports > 16)
nrports = 16;
portnr = displayportbank;
for (i = 0; (i < nrports); i++, portnr++) {
mvprintw((6 + i), 1, "%2d", portnr);
mvprintw((6 + i), 5, "%06x", stats[portnr].state);
mvprintw((6 + i), 12, "%06x", stats[portnr].ttystate);
mvprintw((6 + i), 19, "%08x", stats[portnr].flags);
mvprintw((6 + i), 28, "%05x", stats[portnr].cflags);
mvprintw((6 + i), 34, "%05x", stats[portnr].iflags);
mvprintw((6 + i), 40, "%05x", stats[portnr].oflags);
mvprintw((6 + i), 46, "%05x", stats[portnr].lflags);
mvprintw((6 + i), 52, "%04x", stats[portnr].signals);
mvprintw((6 + i), 58, "%10d", stats[portnr].txtotal);
mvprintw((6 + i), 69, "%10d", stats[portnr].rxtotal);
}
} else {
mvprintw(12, 32, "NO BOARD %d FOUND", displaybrdnr);
i = 16;
}
attron(A_REVERSE);
mvprintw((6 + i), 0, line);
attroff(A_REVERSE);
mvprintw(24, 14,
"(q=Quit,01234567=Board,n=Panels,p=Ports,Z=ZeroStats)");
refresh();
}
/*****************************************************************************/
/*
* Continuously update and display the all ports stats screen.
* Also checks for keyboard input, and processes it as appropriate.
*/
void menuallports()
{
int ch, done;
clear();
getbrdstats();
done = 0;
while ((ch = getch()) != 27) {
switch (ch) {
case ERR:
break;
case '':
refresh();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
displaybrdnr = ch - '0';
displaypanelnr = 0;
getbrdstats();
if (brdstats.state == 0)
beep();
clear();
break;
case 'n':
if (brdstats.panels[displaypanelnr].nrports > 16) {
if (displayportbank == 0) {
displayportbank = 16;
clear();
break;
}
}
displayportbank = 0;
displaypanelnr++;
if (displaypanelnr >= brdstats.nrpanels)
displaypanelnr = 0;
clear();
break;
case 'p':
if (brdstats.panels[displaypanelnr].nrports > 0) {
displayportnr = displayportbank;
menuport();
clear();
} else {
beep();
}
break;
case 'Z':
clearallstats();
clear();
break;
case 'q':
done = 1;
break;
default:
beep();
break;
}
if (done)
break;
getallstats();
displayallports();
}
}
/*****************************************************************************/
/*
* A local exit routine - shuts down curses before exiting.
*/
void localexit(int nr)
{
refresh();
endwin();
exit(nr);
}
/*****************************************************************************/
int main(int argc, char *argv[])
{
struct stat statinfo;
int c, useport;
char *portdev;
ctrldevice = defdevice;
useport = 0;
while ((c = getopt(argc, argv, "hvVb:p:d:c:")) != -1) {
switch (c) {
case 'V':
printf("stlstats version %s\n", version);
exit(0);
break;
case 'h':
usage();
break;
case 'b':
displaybrdnr = atoi(optarg);
break;
case 'p':
displaypanelnr = atoi(optarg);
break;
case 'd':
useport++;
portdev = optarg;
break;
case 'c':
ctrldevice = optarg;
break;
case '?':
default:
usage();
break;
}
}
/*
* Check that the control device exits and is a character device.
*/
if (stat(ctrldevice, &statinfo) < 0)
errx(1, "control device %s does not exist", ctrldevice);
if ((statinfo.st_mode & S_IFMT) != S_IFCHR)
errx(1, "control device %s is not a char device", ctrldevice);
if ((ctrlfd = open(ctrldevice, O_RDWR)) < 0)
errx(1, "open of %s failed", ctrldevice);
/*
* Validate the panel number supplied by user. We do this now since we
* need to have parsed the entire command line first.
*/
getbrdstats();
if (displaypanelnr >= brdstats.nrpanels)
displaypanelnr = 0;
if (useport)
useportdevice(portdev);
/*
* Everything is now ready, lets go!
*/
initscr();
cbreak();
halfdelay(5);
noecho();
clear();
if (useport) {
menuport();
clear();
}
menuallports();
refresh();
endwin();
close(ctrlfd);
printf("\n");
exit(0);
}
/*****************************************************************************/
|