summaryrefslogtreecommitdiff
path: root/contrib/binreplay.c
blob: ea4b9ba39afd34b3d98a249e3a8b3226c12d6cf2 (plain)
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
/* This file is Copyright (c)2010 by the GPSD project
 * BSD terms apply: see the file COPYING in the distribution root for details.
 */
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#ifndef __GLIBC__
  #include <util.h>
#else
  #include <stdlib.h>
  #include <pty.h>
#endif


#define WRLEN 64
void spinner(int);
void usage(void);

int main( int argc, char **argv){
	struct stat sb;
	struct termios term;
	char *buf, tn[32];
	int ifd, ofd, sfd;
	int dflag = 0, c, sleeptime, len, rate = 0, speed = 0;

	while((c = getopt(argc, argv, "d:r:s:")) != -1) {
		switch(c){
		case 'd':
			dflag = 1;
			strncpy(tn, optarg, sizeof(tn)-1);
			break;
		case 'r':
			rate = atoi(optarg);
			switch (rate) {
			case 230400:
			case 115200:
			case 57600:
			case 38400:
			case 28800:
			case 19200:
			case 14400:
			case 9600:
			case 4800:
				break;
			default:
				fprintf(stderr, "invalid data rate: %d\n", rate);
				return 1;
			}
			break;
		case 's':
			speed = atoi(optarg);
			switch (speed) {
			case 230400:
			case 115200:
			case 57600:
			case 38400:
			case 28800:
			case 19200:
			case 14400:
			case 9600:
			case 4800:
				break;
			default:
				fprintf(stderr, "invalid port speed: %d\n", speed);
				return 1;
			}
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		usage();

	if (rate > speed){
		printf("data rate cannot be higher than port speed.\n");
		return 1;
	}

	if (0 == rate && 0 != speed)
		rate = speed;

	if (0 == rate && 0 == speed)
		rate = speed = 4800;

	printf("opening %s\n", argv[0]);
	if ((ifd = open(argv[0], O_RDONLY, 0444)) == -1)
		err(1, "open");

	if (fstat(ifd, &sb) == -1)
		err(1, "fstat");

	if ((buf = mmap(0, sb.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, ifd, 0)) == MAP_FAILED)
		err(1, "mmap");

	if (dflag){
		if ((ofd = open(tn, O_RDWR|O_NOCTTY, 0644)) == -1)
			err(1, "open");
		tcgetattr(ofd, &term);
		cfmakeraw(&term);
		cfsetispeed(&term, speed);
		cfsetospeed(&term, speed);
#if 0
		term.c_cflag &= ~(PARENB | PARODD | CRTSCTS);
        	term.c_cflag |= CREAD | CLOCAL;
        	term.c_iflag = term.c_oflag = term.c_lflag = (tcflag_t) 0;
#endif
		tcflush(ofd, TCIOFLUSH);
		tcsetattr(ofd, TCSANOW, &term);
		tcflush(ofd, TCIOFLUSH);
	} else {
		cfmakeraw(&term);
		cfsetospeed(&term, speed);
		cfsetispeed(&term, speed);
		if (openpty(&ofd, &sfd, tn, &term, NULL) == -1)
			err(1, "openpty");

		tcsetattr(ofd, TCSANOW, &term);
		tcsetattr(sfd, TCSANOW, &term);
	}

	sleeptime = 1000000 / (speed / (WRLEN * 10));
	printf("configured %s for %dbps - write delay %dus\n", tn, speed, sleeptime);

	for(len = 0; len < sb.st_size; len += WRLEN ){
		write(ofd, buf+len, WRLEN );
	//	tcdrain(ofd);
		if (0 == dflag){
			tcflush(ofd, TCIFLUSH);
	//		tcdrain(sfd);
			tcflush(sfd, TCIFLUSH);
		}
		spinner( len );
		usleep(sleeptime);
	}

	munmap(buf, sb.st_size);
	close(ifd);
	close(ofd);
	fprintf(stderr, "\010\010\010\010\010\010\010\010\010\010\010\010\n");
	return 0;
}

void spinner(int n){
	char *s = "|/-\\";

	if (n % (WRLEN * 4))
		return;

	n /= (WRLEN * 4);

	fprintf(stderr, "\010\010\010\010\010\010\010\010\010\010\010\010\010");
	fprintf(stderr, "%c %d", s[n%4], n);
	fflush(stderr);
}

void usage(void){
	fprintf(stderr, "usage: binreplay [-d <device>] [-r <data_rate>] [-s <port_speed>] <file>\n");
	exit(1);
}