1 | /* |
---|
2 | * Test RTEMS/KA9Q TFTP device driver |
---|
3 | * |
---|
4 | * This program may be distributed and used for any purpose. |
---|
5 | * I ask only that you: |
---|
6 | * 1. Leave this author information intact. |
---|
7 | * 2. Document any changes you make. |
---|
8 | * |
---|
9 | * W. Eric Norum |
---|
10 | * Saskatchewan Accelerator Laboratory |
---|
11 | * University of Saskatchewan |
---|
12 | * Saskatoon, Saskatchewan, CANADA |
---|
13 | * eric@skatter.usask.ca |
---|
14 | */ |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <rtems.h> |
---|
18 | #include <rtems/error.h> |
---|
19 | #include <sys/fcntl.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <bootp.h> |
---|
22 | #include <netuser.h> |
---|
23 | |
---|
24 | #include "../usercfg.h" |
---|
25 | |
---|
26 | static char cbuf[1024]; |
---|
27 | static char *fullname; |
---|
28 | static rtems_interval then, now; |
---|
29 | |
---|
30 | static void |
---|
31 | showRate (unsigned long totalRead) |
---|
32 | { |
---|
33 | int elapsed; |
---|
34 | |
---|
35 | printf ("Read %lu bytes", totalRead); |
---|
36 | elapsed = now - then; |
---|
37 | if (elapsed) { |
---|
38 | rtems_interval ticksPerSecond; |
---|
39 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond); |
---|
40 | printf (" (%ld bytes/sec)", |
---|
41 | (long)(((long long)totalRead * ticksPerSecond) |
---|
42 | / elapsed)); |
---|
43 | } |
---|
44 | printf (".\n"); |
---|
45 | rtems_ka9q_execute_command ("ifconfig rtems"); |
---|
46 | } |
---|
47 | |
---|
48 | static void |
---|
49 | testRawRead (void) |
---|
50 | { |
---|
51 | int fd; |
---|
52 | int nread; |
---|
53 | unsigned long totalRead = 0; |
---|
54 | |
---|
55 | fd = open (fullname, O_RDONLY); |
---|
56 | if (fd < 0) { |
---|
57 | printf ("Open failed: %s\n", strerror (errno)); |
---|
58 | return; |
---|
59 | } |
---|
60 | |
---|
61 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
62 | for (;;) { |
---|
63 | nread = read (fd, cbuf, sizeof cbuf); |
---|
64 | if (nread < 0) { |
---|
65 | printf ("Read failed: %s\n", strerror (errno)); |
---|
66 | close (fd); |
---|
67 | return; |
---|
68 | } |
---|
69 | if (nread == 0) |
---|
70 | break; |
---|
71 | totalRead += nread; |
---|
72 | } |
---|
73 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
74 | close (fd); |
---|
75 | showRate (totalRead); |
---|
76 | } |
---|
77 | |
---|
78 | static void |
---|
79 | testFread (void) |
---|
80 | { |
---|
81 | FILE *fp; |
---|
82 | int nread; |
---|
83 | unsigned long totalRead = 0; |
---|
84 | |
---|
85 | fp = fopen (fullname, "r"); |
---|
86 | if (fp == NULL) { |
---|
87 | printf ("Open failed: %s\n", strerror (errno)); |
---|
88 | return; |
---|
89 | } |
---|
90 | |
---|
91 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then); |
---|
92 | for (;;) { |
---|
93 | nread = fread (cbuf, sizeof cbuf[0], sizeof cbuf, fp); |
---|
94 | if (nread < 0) { |
---|
95 | printf ("Read failed: %s\n", strerror (errno)); |
---|
96 | fclose (fp); |
---|
97 | return; |
---|
98 | } |
---|
99 | if (nread == 0) |
---|
100 | break; |
---|
101 | totalRead += nread; |
---|
102 | } |
---|
103 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
104 | fclose (fp); |
---|
105 | showRate (totalRead); |
---|
106 | } |
---|
107 | |
---|
108 | static int |
---|
109 | makeFullname (rtems_unsigned32 host, const char *file) |
---|
110 | { |
---|
111 | const char *hostname; |
---|
112 | #if !defined(USE_BOOTP) |
---|
113 | static char name[16]; |
---|
114 | #endif |
---|
115 | |
---|
116 | printf( "makeFullname(0x08%x, %s)\n", host, file); |
---|
117 | |
---|
118 | #if (defined (USE_BOOTP)) |
---|
119 | if (host) { |
---|
120 | hostname = rtems_hostname_for_address (BootpHost.s_addr, 0); |
---|
121 | if (hostname == NULL) { |
---|
122 | printf ("No host to read from!\n"); |
---|
123 | return 0; |
---|
124 | } |
---|
125 | } else { |
---|
126 | hostname = ""; |
---|
127 | } |
---|
128 | #else |
---|
129 | if (host) { |
---|
130 | sprintf( name, "%d.%d.%d.%d", host >> 24, (host >> 16) & 0xff, |
---|
131 | (host >> 8) & 0xff, host & 0xff |
---|
132 | ); |
---|
133 | hostname = name; |
---|
134 | } else { |
---|
135 | hostname = ""; |
---|
136 | } |
---|
137 | #endif |
---|
138 | fullname = realloc (fullname, 8 + strlen (file) + strlen (hostname)); |
---|
139 | sprintf (fullname, "/TFTP/%s/%s", hostname, file); |
---|
140 | printf ("Read `%s'.\n", fullname); |
---|
141 | return 1; |
---|
142 | } |
---|
143 | |
---|
144 | void |
---|
145 | testTFTP (void) |
---|
146 | { |
---|
147 | /* |
---|
148 | * Check that invalid file names are reported as such |
---|
149 | */ |
---|
150 | if (makeFullname (0, "")) { |
---|
151 | testRawRead (); |
---|
152 | testFread (); |
---|
153 | } |
---|
154 | |
---|
155 | /* |
---|
156 | * Check that non-existent files are reported as such |
---|
157 | */ |
---|
158 | if (makeFullname (0, "BAD-FILE-NAME")) { |
---|
159 | testRawRead (); |
---|
160 | testFread (); |
---|
161 | } |
---|
162 | |
---|
163 | /* |
---|
164 | * Check read speed |
---|
165 | */ |
---|
166 | #if (defined (USE_BOOTP)) |
---|
167 | if (BootpFileName == NULL) { |
---|
168 | printf ("Nothing to read!\n"); |
---|
169 | return; |
---|
170 | } |
---|
171 | if (makeFullname (BootpHost.s_addr, BootpFileName)) { |
---|
172 | #else |
---|
173 | if (makeFullname (aton (DATA_SOURCE_HOST), DATA_SOURCE_FILE)) { |
---|
174 | #endif |
---|
175 | |
---|
176 | testRawRead (); |
---|
177 | testFread (); |
---|
178 | } |
---|
179 | } |
---|