source: rtems/testsuites/samples/fileio/init.c @ aec29f1

4.104.114.95
Last change on this file since aec29f1 was aec29f1, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 11, 2008 at 9:43:58 PM

2008-01-11 Joel Sherrill <joel.sherrill@…>

  • fileio/init.c, fileio/system.h, hello/init.c: Enable malloc statistics so shell output can be interesting.
  • Property mode set to 100644
File size: 15.3 KB
Line 
1/*  Init
2 *
3 *  This routine is the initialization task for this test program.
4 *  It is called from init_exec and has the responsibility for creating
5 *  and starting the tasks that make up the test.  If the time of day
6 *  clock is required for the test, it should also be set to a known
7 *  value by this function.
8 *
9 *  Input parameters:  NONE
10 *
11 *  Output parameters:  NONE
12 *
13 *  COPYRIGHT (c) 1989-2007.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.com/license/LICENSE.
19 *
20 *  init.c,v 1.11 2000/06/12 15:00:12 joel Exp
21 */
22
23#define CONFIGURE_INIT
24#include "system.h"
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <errno.h>
30#include <rtems.h>
31#include <fcntl.h>
32#include <rtems/error.h>
33#include <rtems/dosfs.h>
34#include <ctype.h>
35#include <rtems/ide_part_table.h>
36#include <rtems/libcsupport.h>
37#include <rtems/fsmount.h>
38
39/*
40 * Table of FAT file systems that will be mounted
41 * with the "fsmount" function.
42 * See cpukit/libmisc/fsmount for definition of fields
43 */
44fstab_t fs_table[] = {
45  {
46    "/dev/hda1","/mnt/hda1",
47    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
48    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
49    0
50  },
51  {
52    "/dev/hda2","/mnt/hda2",
53    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
54    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
55    0
56  },
57  {
58    "/dev/hda3","/mnt/hda3",
59    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
60    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
61    0
62  },
63  {
64    "/dev/hda4","/mnt/hda4",
65    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
66    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
67    0
68  },
69  {
70    "/dev/hdc1","/mnt/hdc1",
71    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
72    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
73    0
74  },
75  {
76    "/dev/hdc2","/mnt/hdc2",
77    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
78    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
79    0
80  },
81  {
82    "/dev/hdc3","/mnt/hdc3",
83    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
84    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
85    0
86  },
87  {
88    "/dev/hdc4","/mnt/hdc4",
89    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
90    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
91    0
92  }
93};
94
95#define MIN(a,b) (((a) > (b)) ? (b) : (a))
96
97#define USE_SHELL
98
99#ifdef USE_SHELL
100#include <rtems/shell.h>
101
102void fileio_start_shell(void)
103{
104  printf(" =========================\n");
105  printf(" starting shell\n");
106  printf(" =========================\n");
107  rtems_shell_init("SHLL",RTEMS_MINIMUM_STACK_SIZE * 4,100,"/dev/console",
108                   0, 0);
109  rtems_task_suspend(RTEMS_SELF);
110}
111
112#endif /* USE_SHELL */
113
114void fileio_print_free_heap(void)
115{
116  printf("--- unused dynamic memory: %lu bytes ---\n",
117         (unsigned long) malloc_free_space());
118}
119
120
121void fileio_part_table_initialize(void)
122{
123  char devname[64];
124  rtems_status_code rc;
125
126  printf(" =========================\n");
127  printf(" Initialize partition table\n");
128  printf(" =========================\n");
129  fileio_print_free_heap();
130  printf(" Enter device to initialize ==>");
131  fflush(stdout);
132  fgets(devname,sizeof(devname)-1,stdin);
133  while (devname[strlen(devname)-1] == '\n') {
134    devname[strlen(devname)-1] = '\0';
135  }
136  /*
137   * call function
138   */
139  rc = rtems_ide_part_table_initialize(devname);
140  printf("result = %d\n",rc);
141  fileio_print_free_heap();
142}
143
144void fileio_fsmount(void)
145{
146  rtems_status_code rc;
147
148  printf(" =========================\n");
149  printf(" Process fsmount table\n");
150  printf(" =========================\n");
151  fileio_print_free_heap();
152  /*
153   * call function
154   */
155  rc = rtems_fsmount( fs_table,
156                      sizeof(fs_table)/sizeof(fs_table[0]),
157                      NULL);
158  printf("result = %d\n",rc);
159  fileio_print_free_heap();
160}
161
162void fileio_list_file(void)
163{
164  char fname[1024];
165  char *buf_ptr = NULL;
166  ssize_t   flen = 0;
167  int fd = -1;
168  ssize_t n;
169  size_t buf_size = 100;
170
171  rtems_interval start_tick,curr_tick,ticks_per_sec;
172
173  printf(" =========================\n");
174  printf(" LIST FILE ...            \n");
175  printf(" =========================\n");
176  fileio_print_free_heap();
177  printf(" Enter filename to list ==>");
178  fflush(stdout);
179  fgets(fname,sizeof(fname)-1,stdin);
180  while (fname[strlen(fname)-1] == '\n') {
181    fname[strlen(fname)-1] = '\0';
182  }
183  /*
184   * allocate buffer of given size
185   */
186  if (buf_size > 0) {
187    buf_ptr = malloc(buf_size);
188  }
189
190  if (buf_ptr != NULL) {
191    printf("\n Trying to open file \"%s\" for read\n",fname);
192    fd = open(fname,O_RDONLY);
193    if (fd < 0) {
194      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
195    }
196  }
197
198  if (fd >= 0) {
199    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
200    do {
201      n = read(fd,buf_ptr,buf_size);
202      if (n > 0) {
203        write(1,buf_ptr,n);
204        flen += n;
205      }
206    } while (n > 0);
207
208    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
209
210    printf("\n ******** End of file reached, flen = %d\n",flen);
211    close(fd);
212
213    rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
214    printf("time elapsed for read:  %g seconds\n",
215           ((double)curr_tick-start_tick)/ticks_per_sec);
216  }
217  /*
218   * free buffer
219   */
220  if (buf_ptr != NULL) {
221    free(buf_ptr);
222  }
223  fileio_print_free_heap();
224}
225
226/*
227 * convert a size string (like 34K or 12M) to actual byte count
228 */
229boolean fileio_str2size(const char *str,uint32_t   *res_ptr)
230{
231  boolean failed = FALSE;
232  unsigned long size;
233  char suffix = ' ';
234
235  if (1 > sscanf(str,"%lu%c",&size,&suffix)) {
236    failed = TRUE;
237  }
238  else if (toupper(suffix) == 'K') {
239    size *= 1024;
240  }
241  else if (toupper(suffix) == 'M') {
242    size *= 1024UL*1024UL;
243  }
244  else if (isalpha(suffix)) {
245    failed = TRUE;
246  }
247
248  if (!failed) {
249    *res_ptr = size;
250  }
251  return failed;
252}
253
254void fileio_write_file(void)
255{
256  char fname[1024];
257  char tmp_str[32];
258  uint32_t   file_size = 0;
259  uint32_t   buf_size  = 0;
260  size_t curr_pos,bytes_to_copy;
261  int fd = -1;
262  ssize_t n;
263  rtems_interval start_tick,curr_tick,ticks_per_sec;
264  char *bufptr = NULL;
265  boolean failed = FALSE;
266  static const char write_test_string[] =
267    "The quick brown fox jumps over the lazy dog\n";
268  static const char write_block_string[] =
269    "\n----- end of write buffer ------\n";
270
271  printf(" =========================\n");
272  printf(" WRITE FILE ...           \n");
273  printf(" =========================\n");
274  fileio_print_free_heap();
275  /*
276   * get number of ticks per second
277   */
278  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
279
280  /*
281   * get path to file to write
282   */
283  if (!failed) {
284    printf("Enter path/filename ==>");
285    fflush(stdout);
286    fgets(fname,sizeof(fname)-1,stdin);
287    while (fname[strlen(fname)-1] == '\n') {
288      fname[strlen(fname)-1] = '\0';
289    }
290    if (0 == strlen(fname)) {
291      printf("*** no filename entered, aborted\n");
292      failed = TRUE;
293    }
294  }
295  /*
296   * get total file size to write
297   */
298  if (!failed) {
299    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
300           "Enter filesize to write ==>");
301    fflush(stdout);
302    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
303    failed = fileio_str2size(tmp_str,&file_size);
304    if (failed) {
305      printf("*** illegal file size, aborted\n");
306    }
307  }
308  /*
309   * get block size to write
310   */
311  if (!failed) {
312    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
313           "Enter block size to use for write calls ==>");
314    fflush(stdout);
315    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
316    failed = fileio_str2size(tmp_str,&buf_size);
317    if (failed) {
318      printf("*** illegal block size, aborted\n");
319    }
320  }
321
322  /*
323   * allocate buffer
324   */
325  if (!failed) {
326    printf("... allocating %lu bytes of buffer for write data\n",
327           (unsigned long)buf_size);
328    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
329    if (bufptr == NULL) {
330      printf("*** malloc failed, aborted\n");
331      failed = TRUE;
332    }
333  }
334  /*
335   * fill buffer with test pattern
336   */
337  if (!failed) {
338    printf("... filling buffer with write data\n");
339    curr_pos = 0;
340    /*
341     * fill buffer with test string
342     */
343    while (curr_pos < buf_size) {
344      bytes_to_copy = MIN(buf_size-curr_pos,
345                          sizeof(write_test_string)-1);
346      memcpy(bufptr+curr_pos,write_test_string,bytes_to_copy);
347      curr_pos += bytes_to_copy;
348    }
349    /*
350     * put "end" mark at end of buffer
351     */
352    bytes_to_copy = sizeof(write_block_string)-1;
353    if (buf_size >= bytes_to_copy) {
354      memcpy(bufptr+buf_size-bytes_to_copy,
355             write_block_string,
356             bytes_to_copy);
357    }
358  }
359  /*
360   * create file
361   */
362  if (!failed) {
363    printf("... creating file \"%s\"\n",fname);
364    fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,S_IREAD|S_IWRITE);
365    if (fd < 0) {
366      printf("*** file create failed, errno = %d(%s)\n",errno,strerror(errno));
367      failed = TRUE;
368    }
369  }
370  /*
371   * write file
372   */
373  if (!failed) {
374    printf("... writing to file\n");
375    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
376    curr_pos = 0;
377    do {
378      bytes_to_copy = buf_size;
379      do {
380        n = write(fd,
381          bufptr + (buf_size-bytes_to_copy),
382                  MIN(bytes_to_copy,file_size-curr_pos));
383        if (n > 0) {
384          bytes_to_copy -= n;
385          curr_pos      += n;
386        }
387      } while ((bytes_to_copy > 0)  && (n > 0));
388    } while ((file_size > curr_pos) && (n > 0));
389    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
390    if (n < 0) {
391      failed = TRUE;
392      printf("*** file write failed, "
393             "%lu bytes written, "
394             "errno = %d(%s)\n",
395             (unsigned long)curr_pos,errno,strerror(errno));
396    }
397    else {
398      printf("time elapsed for write:  %g seconds\n",
399             ((double)curr_tick-start_tick)/ticks_per_sec);
400      printf("write data rate: %g KBytes/second\n",
401             (((double)file_size) / 1024.0 /
402              (((double)curr_tick-start_tick)/ticks_per_sec)));
403    }
404  }
405  if (fd >= 0) {
406    printf("... closing file\n");
407    close(fd);
408  }
409  if (bufptr != NULL) {
410    printf("... deallocating buffer\n");
411    free(bufptr);
412    bufptr = NULL;
413  }
414  printf("\n ******** End of file write\n");
415  fileio_print_free_heap();
416}
417
418void fileio_read_file(void)
419{
420  char fname[1024];
421  char tmp_str[32];
422  uint32_t   buf_size  = 0;
423  size_t curr_pos;
424  int fd = -1;
425  ssize_t n;
426  rtems_interval start_tick,curr_tick,ticks_per_sec;
427  char *bufptr = NULL;
428  boolean failed = FALSE;
429
430  printf(" =========================\n");
431  printf(" READ FILE ...            \n");
432  printf(" =========================\n");
433  fileio_print_free_heap();
434  /*
435   * get number of ticks per second
436   */
437  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
438
439  /*
440   * get path to file to read
441   */
442  if (!failed) {
443    printf("Enter path/filename ==>");
444    fflush(stdout);
445    fgets(fname,sizeof(fname)-1,stdin);
446    while (fname[strlen(fname)-1] == '\n') {
447      fname[strlen(fname)-1] = '\0';
448    }
449    if (0 == strlen(fname)) {
450      printf("*** no filename entered, aborted\n");
451      failed = TRUE;
452    }
453  }
454  /*
455   * get block size to read
456   */
457  if (!failed) {
458    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
459           "Enter block size to use for read calls ==>");
460    fflush(stdout);
461    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
462    failed = fileio_str2size(tmp_str,&buf_size);
463    if (failed) {
464      printf("*** illegal block size, aborted\n");
465    }
466  }
467
468  /*
469   * allocate buffer
470   */
471  if (!failed) {
472    printf("... allocating %lu bytes of buffer for write data\n",
473           (unsigned long)buf_size);
474    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
475    if (bufptr == NULL) {
476      printf("*** malloc failed, aborted\n");
477      failed = TRUE;
478    }
479  }
480  /*
481   * open file
482   */
483  if (!failed) {
484    printf("... opening file \"%s\"\n",fname);
485    fd = open(fname,O_RDONLY);
486    if (fd < 0) {
487      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
488      failed = TRUE;
489    }
490  }
491  /*
492   * read file
493   */
494  if (!failed) {
495    printf("... reading from file\n");
496    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
497    curr_pos = 0;
498    do {
499      n = read(fd,
500               bufptr,
501               buf_size);
502      if (n > 0) {
503        curr_pos      += n;
504      }
505    } while (n > 0);
506    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
507    if (n < 0) {
508      failed = TRUE;
509      printf("*** file read failed, "
510             "%lu bytes read, "
511             "errno = %d(%s)\n",
512             (unsigned long)curr_pos,errno,strerror(errno));
513    }
514    else {
515      printf("%lu bytes read\n",
516             (unsigned long)curr_pos);
517      printf("time elapsed for read:  %g seconds\n",
518             ((double)curr_tick-start_tick)/ticks_per_sec);
519      printf("read data rate: %g KBytes/second\n",
520             (((double)curr_pos) / 1024.0 /
521              (((double)curr_tick-start_tick)/ticks_per_sec)));
522    }
523  }
524  if (fd >= 0) {
525    printf("... closing file\n");
526    close(fd);
527  }
528  if (bufptr != NULL) {
529    printf("... deallocating buffer\n");
530    free(bufptr);
531    bufptr = NULL;
532  }
533  printf("\n ******** End of file read\n");
534  fileio_print_free_heap();
535
536}
537
538void fileio_menu (void)
539{
540  char inbuf[10];
541
542  /*
543   * Wait for characters from console terminal
544   */
545  for (;;) {
546    printf(" =========================\n");
547    printf(" RTEMS FILE I/O Test Menu \n");
548    printf(" =========================\n");
549    printf("   p -> part_table_initialize\n");
550    printf("   f -> mount all disks in fs_table\n");
551    printf("   l -> list  file\n");
552    printf("   r -> read  file\n");
553    printf("   w -> write file\n");
554#ifdef USE_SHELL
555    printf("   s -> start shell\n");
556#endif
557    printf("   Enter your selection ==>");
558    fflush(stdout);
559
560    inbuf[0] = '\0';
561    fgets(inbuf,sizeof(inbuf),stdin);
562    switch (inbuf[0]) {
563    case 'l':
564      fileio_list_file ();
565      break;
566    case 'r':
567      fileio_read_file ();
568      break;
569    case 'w':
570      fileio_write_file ();
571      break;
572    case 'p':
573      fileio_part_table_initialize ();
574      break;
575    case 'f':
576      fileio_fsmount ();
577      break;
578#ifdef USE_SHELL
579    case 's':
580      fileio_start_shell ();
581      break;
582#endif
583    default:
584      printf("Selection `%c` not implemented\n",inbuf[0]);
585      break;
586    }
587
588  }
589  exit (0);
590}
591
592int menu_tid;
593
594/*
595 * RTEMS Startup Task
596 */
597rtems_task
598Init (rtems_task_argument ignored)
599{
600  puts( "\n\n*** FILE I/O SAMPLE AND TEST ***" );
601
602  fileio_menu();
603}
604
605/*
606 *  RTEMS Shell Configuration -- Add a command and an alias for it
607 */
608
609int main_usercmd(int argc, char **argv)
610{
611  int i;
612  printf( "UserCommand: argc=%d\n", argc );
613  for (i=0 ; i<argc ; i++ )
614    printf( "argv[%d]= %s\n", i, argv[i] );
615  return 0;
616}
617
618rtems_shell_cmd_t Shell_USERCMD_Command = {
619  "usercmd",                                       /* name */
620  "usercmd n1 [n2 [n3...]]     # echo arguments",  /* usage */
621  "user",                                          /* topic */
622  main_usercmd,                                    /* command */
623  NULL,                                            /* alias */
624  NULL                                             /* next */
625};
626
627rtems_shell_alias_t Shell_USERECHO_Alias = {
628  "usercmd",                 /* command */
629  "userecho"                 /* alias */
630};
631 
632
633#define CONFIGURE_SHELL_USER_COMMANDS &Shell_USERCMD_Command
634#define CONFIGURE_SHELL_USER_ALIASES &Shell_USERECHO_Alias
635#define CONFIGURE_SHELL_COMMANDS_INIT
636#define CONFIGURE_SHELL_COMMANDS_ALL
637#define CONFIGURE_SHELL_MOUNT_MSDOS
638
639#include <rtems/shellconfig.h>
640
Note: See TracBrowser for help on using the repository browser.