summaryrefslogtreecommitdiff
path: root/tftpTest/test.c
blob: 1d40af8f6334924b36eca1d0e450f8275e861e0b (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
171
172
173
174
175
176
177
178
179
180
181
/*
 * Test RTEMS/KA9Q TFTP device driver
 *
 * This program may be distributed and used for any purpose.
 * I ask only that you:
 *      1. Leave this author information intact.
 *      2. Document any changes you make.
 *
 * W. Eric Norum
 * Saskatchewan Accelerator Laboratory
 * University of Saskatchewan
 * Saskatoon, Saskatchewan, CANADA
 * eric@skatter.usask.ca
 */

#include <stdio.h>
#include <rtems.h>
#include <rtems/error.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <bootp.h>
#include <netuser.h>

#include "../usercfg.h"

static char cbuf[1024];
static char *fullname;
static rtems_interval then, now;

static void
showRate (unsigned long totalRead)
{
	int elapsed;

	printf ("Read %lu bytes", totalRead);
	elapsed = now - then;
	if (elapsed) {
		rtems_interval ticksPerSecond;
		rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond);
		printf (" (%ld bytes/sec)",
				(long)(((long long)totalRead * ticksPerSecond)
								/ elapsed));
	}
	printf (".\n");
rtems_ka9q_execute_command ("ifconfig rtems");
}

static void
testRawRead (void)
{
	int fd;
	int nread;
	unsigned long totalRead = 0;

	fd = open (fullname, O_RDONLY);
	if (fd < 0) {
		printf ("Open failed: %s\n", strerror (errno));
		return;
	}

	rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
	for (;;) {
		nread = read (fd, cbuf, sizeof cbuf);
		if (nread < 0) {
			printf ("Read failed: %s\n", strerror (errno));
			close (fd);
			return;
		}
		if (nread == 0)
			break;
		totalRead += nread;
	}
	rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
	close (fd);
	showRate (totalRead);
}

static void
testFread (void)
{
	FILE *fp;
	int nread;
	unsigned long totalRead = 0;

	fp = fopen (fullname, "r");
	if (fp == NULL) {
		printf ("Open failed: %s\n", strerror (errno));
		return;
	}

	rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
	for (;;) {
		nread = fread (cbuf, sizeof cbuf[0], sizeof cbuf, fp);
		if (nread < 0) {
			printf ("Read failed: %s\n", strerror (errno));
			fclose (fp);
			return;
		}
		if (nread == 0)
			break;
		totalRead += nread;
	}
	rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
	fclose (fp);
	showRate (totalRead);
}

static int
makeFullname (rtems_unsigned32 host, const char *file)
{
	const char *hostname;
#if !defined(USE_BOOTP)
	static char name[16];
#endif

#if 0
	printf( "makeFullname(0x08%x, %s)\n", host, file);
#endif

#if (defined (USE_BOOTP))
	if (host) {
		hostname = rtems_hostname_for_address (BootpHost.s_addr, 0);
		if (hostname == NULL) {
			printf ("No host to read from!\n");
			return 0;
		}
	} else {
		hostname = "";
	}
#else
	if (host) {
		sprintf( name, "%d.%d.%d.%d", host >> 24, (host >> 16) & 0xff, 
			(host >> 8) & 0xff, host & 0xff
		);
		hostname = name;
	} else {
		hostname = "";
	}
#endif
	fullname = realloc (fullname, 8 + strlen (file) + strlen (hostname));
	sprintf (fullname, "/TFTP/%s/%s", hostname, file);
	printf ("Read `%s'.\n", fullname);
	return 1;
}

void
testTFTP (void)
{
	/*
	 * Check that invalid file names are reported as such
	 */
	if (makeFullname (0, "")) {
		testRawRead ();
		testFread ();
	}

	/*
	 * Check that non-existent files are reported as such
	 */
	if (makeFullname (0, "BAD-FILE-NAME")) {
		testRawRead ();
		testFread ();
	}

	/*
	 * Check read speed
	 */
#if (defined (USE_BOOTP))
	if (BootpFileName == NULL) {
		printf ("Nothing to read!\n");
		return;
	}
	if (makeFullname (BootpHost.s_addr, BootpFileName)) {
#else
	if (makeFullname (aton (DATA_SOURCE_HOST), DATA_SOURCE_FILE)) {
#endif

		testRawRead ();
		testFread ();
	}
}