File: hw_pear.c

package info (click to toggle)
gcpegg 5.1-14
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 324 kB
  • ctags: 439
  • sloc: ansic: 3,726; makefile: 97; sh: 33; csh: 21
file content (241 lines) | stat: -rw-r--r-- 5,601 bytes parent folder | download | duplicates (3)
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
/* PROGRAM:     eggsh
 * FILE:        $Header: /home/egg/src/RCS/hw_pear.c,v 1.3 1998/08/01 18:50:39 ghn Exp $
 * PURPOSE:     PEAR (Bradish box/micro-REG) hardware interface
 * AUTHOR:      Greg Nelson
 * DATE:        98-04-12
 *
 * REVISED:     $Log: hw_pear.c,v $
 * REVISED:     Revision 1.3  1998/08/01 18:50:39  ghn
 * REVISED:     Added John's byte-order-independence changes and PSEUDO support.
 * REVISED:
 * REVISED:     Revision 1.2  1998/08/01 17:13:51  ghn
 * REVISED:     Added John's Solaris support and DUMPREG option.
 * REVISED:
 * REVISED:     Revision 1.1  1998/07/21 11:37:41  ghn
 * REVISED:     Initial revision
 * REVISED:
 * Copyright 1998 - Greg Nelson
 * Redistributable under the terms of the GNU Public Licence (GPL)
 */

/* Define this to dump samples from the REG which we actually use into
   a file named dumpreg.dat.  This can be used to debug serial port
   mode problems (for example, setting the character frame to 7 bits,
   or telling it to ignore breaks, which will lose all zero bytes. */
/* #define DUMPREG */

#include <stdio.h>
#define __USE_BSD
#include <termios.h>
#undef __USE_BSD
#include <unistd.h>
#include <fcntl.h>
#define __USE_BSD
#include <errno.h>
#undef __USE_BSD

#include "global.h"
#include "genlib.h"
#include "hwapi.h"

#define MAXDEV	20

int32 oldbits[MAXDEV], bitsleft[MAXDEV];

#ifdef DUMPREG
static FILE *dumpfile;		      /* REG dump file handle */
static unsigned char dumpbuf[1024];   /* REG dump buffer */
static int dumpptr = 0; 	      /* Pointer into dump buffer */
#endif

#ifdef PSEUDORANDOM_TEST
#include <stdlib.h>
static int pseudo = FALSE;	      /* Nonzero if using pseudo-REG */
#endif

int32 OpenDev(DevOpts *opts) {
  char ttydev[30];
  speed_t baudcon;
  struct termios tt;
  int32 TTY_fd, res;

#ifdef DUMPREG
  dumpfile = fopen("dumpreg.dat", "w");
  setbuf(dumpfile, NULL);
  dumpptr = 0;
#endif

#ifdef PSEUDORANDOM_TEST
  if (opts->type == 0xFF) {
    TTY_fd = 1;
    oldbits[TTY_fd] = bitsleft[TTY_fd] = 0;
    srandom(time(NULL));
    pseudo = TRUE;
    fprintf(stderr, "**** WARNING!  This egg has been configured to\n\
               use a pseudorandom sequence generator to\n\
               permit testing on machines not equipped\n\
               with an REG.  Data from this egg should not\n\
               be used for analysis.\n");
    return TTY_fd;
  }
#endif

#ifdef Solaris
  /* Serial ports (at least built-in ones) have names of
     /dev/term/a, /dev/term/b, etc.  Map the port numbers
     from the RC file into this nomenclature, with "1"
     designating /dev/term/a. */
  sprintf(ttydev, "/dev/term/%c", 'a' + (opts->port - 1));
#else
  sprintf(ttydev, "/dev/REG", opts->port);
#endif

  switch(opts->baud) {
  case 1200: baudcon = B1200; break;
  case 2400: baudcon = B2400; break;
  case 4800: baudcon = B4800; break;
  case 9600: baudcon = B9600; break;
  case 19200: baudcon = B19200; break;
  case 38400: baudcon = B38400; break;
#ifndef Irix
  case 115200: baudcon = B115200; break;
#endif
  default:
    printf("%s: Baud rate %ld not supported.\n", pgmname, opts->baud);
    return -1;
  }

  fprintf(stderr, "Opening %s at %ld\n", ttydev, opts->baud);
  if ((TTY_fd = open(ttydev, O_RDWR | O_NDELAY)) < 0) {
    perror(pgmname);
    return -1;
  }

  if (TTY_fd >= MAXDEV) {
    fprintf(stderr, "%s: Too many devices open.\n", pgmname);
    close(TTY_fd);
    return -1;
  }

  res = tcgetattr(TTY_fd, &tt);
#ifdef Solaris
  tt.c_iflag = 0;
  tt.c_oflag = 0;
  tt.c_cflag = baudcon | CS8 | CREAD | CLOCAL;
  tt.c_lflag = 0;
  tt.c_cc[VMIN] = 1;	/* This many chars satisfies reads */
  tt.c_cc[VTIME] = 0;	/* or in this many tenths of seconds */
#else
  res = cfsetospeed(&tt, baudcon); 
  cfmakeraw(&tt);
  tt.c_oflag &= (~(TABDLY | ONLCR));
#endif
  res = tcsetattr(TTY_fd, TCSANOW, &tt);

  oldbits[TTY_fd] = bitsleft[TTY_fd] = 0;
  return TTY_fd;
}

#define SAMP_PERIOD	1000	/* msec */
#define MARGIN		.95	/* how much to headroom to allow in
				   speed measurement */

int32 EvalSpeed(int32 dd) {
  struct timeval start, end;
  int32 bitct, samp;

  if (dd < 0) return -1;

#ifdef PSEUDORANDOM_TEST
  if (pseudo) {
    return 500;
  }
#endif

  gettimeofday(&start, NULL);
  bitct = 0;
  while (1) {
    gettimeofday(&end, NULL);
    if (deltams(&end, &start) >= SAMP_PERIOD) break;
    samp = Sample(dd, 1);
    bitct++;
  }

  return (int32)(bitct * MARGIN);
}

int32 Sample(int32 dd, uint16 bits) {
  int32 bc, sum, n1;
  uint8 c1;

  if (dd < 0) return -1;

  sum = bc = 0;
  while (bc < bits) {
    if (bitsleft[dd]) {
      sum += (oldbits[dd] & 0x01);
      oldbits[dd] >>= 1;
      bitsleft[dd]--;
      bc++;
    } else {
      do {
#ifdef PSEUDORANDOM_TEST
	if (pseudo) {
	    n1 = 1;
	    c1 = (uint8) ((random() >> 5) & 0xFF);
	} else
#endif
	n1 = read(dd, &c1, 1);
      } while (n1 == 0 || (n1 == -1 && errno == EAGAIN));
      if (n1 == -1) {
	/* Fatal error occurred, die now? */
	return -1;
      }
#ifdef DUMPREG
      dumpbuf[dumpptr++] = c1;
      if (dumpptr >= sizeof(dumpbuf)) {
	fwrite(dumpbuf, sizeof(dumpbuf), 1, dumpfile);
	dumpptr = 0;
      }
#endif
      oldbits[dd] = c1;
      bitsleft[dd] = 8;
    }
  }

  return sum;
}

int32 Discard(int32 dd) {
  int32 disc, n1;
  uint8 c1;
  
  if (dd < 0) return -1;

  disc = bitsleft[dd];
  bitsleft[dd] = 0;

#ifdef PSEUDORANDOM_TEST
  if (pseudo) {
    return 32;
  }
#endif

  do {
    n1 = read(dd, &c1, 1);
    if (n1 == 1) disc++;
  } while (n1 == 1);

  return disc;
}

int32 CloseDev(int32 dd) {
  if (dd < 0) return -1;

#ifdef PSEUDORANDOM_TEST
  if (!pseudo)
#endif
  close(dd);
  return 0;
}