source: multiio/pcmmio/original/main_pcmmio_dout.c @ 69f7221

Last change on this file since 69f7221 was 69f7221, checked in by Joel Sherrill <joel.sherrill@…>, on 07/21/09 at 23:27:42

2009-07-21 Joel Sherrill <joel.sherrill@…>

  • Makefile, README, main_pcmmio_dac.c, main_pcmmio_dout.c: Added string2XXX routines to RTEMS so we have ability to do heavy error checking on numeric inputs.
  • Property mode set to 100644
File size: 2.7 KB
Line 
1/*
2 *  pcmmio_dout command
3 *
4 *  COPYRIGHT (c) 1989-2009.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#include "pcmmio_commands.h"
15#include <rtems/string2.h>
16
17#define __need_getopt_newlib
18#include <getopt.h>
19
20#if defined(TESTING)
21  #define dio_write_bit(_bit, _value) \
22    printf( "Testing: Write %d to to bit %d\n", _value, _bit );
23#endif
24
25char pcmmio_dout_usage[] =
26  "Usage: %s [-v] bit value\n"
27  "Where: bit must be 0-47\n"
28  "       value must be 0 or 1\n";
29
30#define PRINT_USAGE() \
31   printf( pcmmio_dout_usage, argv[0] );
32
33int main_pcmmio_dout(int argc, char **argv)
34{
35  char   ch;
36  bool   verbose;
37  struct getopt_data getopt_reent;
38  int    bit;
39  int    value;
40
41  /*
42   * Parse arguments here
43   */
44  verbose = false;
45
46  memset(&getopt_reent, 0, sizeof(getopt_data));
47  while ((ch = getopt_r(argc, argv, "v", &getopt_reent)) != -1) {
48    switch (ch) {
49      case 'v': /* verbose*/
50        verbose = true;
51        break;
52      default:
53        PRINT_USAGE();
54        return -1;
55    }
56  }
57
58  if ( (argc - getopt_reent.optind) != 2 ) {
59    printf( "Incorrect number of arguments\n" );
60    PRINT_USAGE();
61    return -1;
62  }
63
64  /*
65   *  Convert the string arguments into number values
66   */
67  if ( !rtems_string_to_int( argv[getopt_reent.optind], &bit, NULL, 0 ) ) {
68    printf( "Bit (%s) is not a number\n", argv[getopt_reent.optind] );
69    PRINT_USAGE();
70    return -1;
71  }
72
73  if ( !rtems_string_to_int(argv[getopt_reent.optind + 1], &value, NULL, 0) ) {
74    printf( "Value (%s) is not a number\n", argv[getopt_reent.optind] );
75    PRINT_USAGE();
76    return -1;
77  }
78
79  /*
80   *  Validate the output bit and value.
81   */
82  if ( bit < 0 || bit > 47 ) {
83    printf( "Bit number must be 0-47\n" );
84    PRINT_USAGE();
85    return -1;
86  }
87
88  if ( value != 0 && value != 1 ) {
89    printf( "Value must be 0 or 1\n" );
90    PRINT_USAGE();
91    return -1;
92  }
93
94  /*
95   *  Now write the value
96   */
97  if ( verbose )
98    printf( "Write %d to to bit %d\n", value, bit );
99  dio_write_bit(bit, value);
100
101  return 0;
102}
103
104rtems_shell_cmd_t Shell_PCMMIO_DOUT_Command = {
105  "pcmmio_dout",                                   /* name */
106  "Write PCMMIO Discrete Outputs",                 /* usage */
107  "pcmmio",                                        /* topic */
108  main_pcmmio_dout,                                /* command */
109  NULL,                                            /* alias */
110  NULL                                             /* next */
111};
112
113rtems_shell_alias_t Shell_PCMMIO_DOUT_Alias = {
114  "pcmmio_dout",         /* command */
115  "dout"                 /* alias */
116};
117
Note: See TracBrowser for help on using the repository browser.