Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <getopt.h>
  4. #include <string.h>
  5. #include "ihex.h"
  6. #include "actions.h"
  7. #include "interface.h"
  8. #include <can.h>
  9.  
  10. unsigned char buf[8192];
  11. long start_addr = 0, len = 0;
  12.  
  13. /* Flag set by `--verbose'. */
  14. static int verbosity;
  15.  
  16. int (*actionfuncs[5])(actions_t* action) ;
  17.  
  18. #define MAXACTIONS 16
  19.  
  20. actions_t actions[MAXACTIONS+1];
  21.  
  22. const char* usage = "Usage:\n\n"
  23.                     "canman <options> <actions>\n\n"
  24.                     "  -v\n"
  25.                     "  --verbose               Increase verbosity\n\n"
  26.                     "  -q\n"
  27.                     "  --quiet                 Decrease verbosity\n\n"
  28.                     "  -i <interface>\n"
  29.                     "  --interface=<interface> Select canManager interface\n\n"
  30.                     "  -p <device>\n"
  31.                     "  --port=<device>         Set interface port\n"
  32.                     ;
  33.  
  34. char* const default_port = "/dev/ttyS0";
  35.  
  36. int main (int argc, char* argv[]) {
  37.     int c;
  38.     int n = 0;
  39.     interface_t interface = unset;
  40.     int node_id = 0;
  41.     char* port = default_port;
  42.     Can_Message_t txMsg;
  43.     txMsg.DataLength = 3;
  44.    
  45.     actionfuncs[upload] = do_upload;
  46.     actionfuncs[reset] = do_reset;
  47.     actionfuncs[start] = do_start;
  48.     actionfuncs[dump] = do_dump;
  49.     actionfuncs[send] = do_send;
  50.    
  51.     while (1) {
  52.         static struct option long_options[] =
  53.         {
  54.             {"verbose",   no_argument,       0, 'v'},
  55.             {"quiet",     no_argument,       0, 'q'},
  56.             {"interface", required_argument, 0, 'i'},
  57.             {"port",      required_argument, 0, 'p'},
  58.             {"node",      required_argument, 0, 'n'},
  59.             /* These options are actions */
  60.             {"upload", required_argument, 0, 'u'},
  61.             {"reset",  no_argument,       0, 'r'},
  62.             {"start",  no_argument,       0, 's'},
  63.             {"dump",   no_argument,       0, 'd'},
  64.             {"send",   required_argument, 0, 'x'},
  65.             /* End of options */
  66.             {0, 0, 0, 0}
  67.         };
  68.         /* getopt_long stores the option index here. */
  69.         int option_index = 0;
  70.        
  71.         c = getopt_long (argc, argv, "vqi:p:n:u:rsdx:",
  72.                          long_options, &option_index);
  73.        
  74.         /* Detect the end of the options. */
  75.         if (c == -1)
  76.             break;
  77.        
  78.         switch (c) {
  79.             case 0:
  80.                 /* If this option set a flag, do nothing else now. */
  81.                 if (long_options[option_index].flag != 0)
  82.                     break;
  83.                 //printf ("option %s", long_options[option_index].name);
  84.                 //if (optarg)
  85.                 //  printf (" with arg %s", optarg);
  86.                 //printf ("\n");
  87.                 break;
  88.                
  89.             case 'v': // --verbose
  90.                 verbosity++;
  91.                 break;
  92.                
  93.             case 'q': // --quiet
  94.                 verbosity--;
  95.                 break;
  96.                
  97.             case 'i': // --interface <type>
  98.                 if (interface == unset)
  99.                     if (strcasecmp(optarg, "udp") == 0)
  100.                         interface = udp;
  101.                     else if (strcasecmp(optarg, "can2serial") == 0)
  102.                         interface = can2serial;
  103.                     else {
  104.                         fprintf (stderr, "interface %s not supported.\n", optarg);
  105.                         exit (0);
  106.                     }
  107.                 else {
  108.                     fprintf (stderr, "multiple -i options not allowed.\n");
  109.                     exit (0);
  110.                 }
  111.                 break;
  112.                
  113.             case 'p': // --port <device>
  114.                 port = optarg;
  115.                 break;
  116.                
  117.             case 'n': // --node <id>
  118.                 sscanf (optarg, "%i", &node_id);
  119.                 break;
  120.                
  121.             case 'u': // --upload <hexfile>
  122.             case 'r': // --reset
  123.             case 's': // --start
  124.             case 'd': // --dump
  125.             case 'x': // --send
  126.                 if (node_id>0)
  127.                     actions[n].node_id = node_id;
  128.                 else {
  129.                     puts ("no node id selected for action");
  130.                     exit (1);
  131.                 }
  132.                
  133.                 actions[n].parameter = optarg; // null if not used
  134.  
  135.                 if (c=='u')
  136.                     actions[n].action = upload;
  137.                 else if (c=='r')
  138.                     actions[n].action = reset;
  139.                 else if (c=='s')
  140.                     actions[n].action = start;
  141.                 else if (c=='d')
  142.                     actions[n].action = dump;
  143.                 else if (c=='x')
  144.                     actions[n].action = send;
  145.                    
  146.                 if (n++ >= MAXACTIONS) {
  147.                     puts ("too many actions specified");
  148.                     exit (1);
  149.                 }
  150.                 break;
  151.                
  152.             case '?':
  153.                 /* getopt_long already printed an error message. */
  154.                 puts (usage);
  155.                 exit (1);
  156.                 break;
  157.                
  158.             default:
  159.                 abort ();
  160.         }
  161.     }
  162.    
  163.     /* Print any remaining command line arguments (not options). */
  164.     if (optind < argc) {
  165.         printf ("invalid argument: %s\n", argv[optind]);
  166.         exit (1);
  167.     }
  168.     printf ("interface = %d, port = %s\n", interface, port);
  169.     int i;
  170.     for (i=0; i<n; i++) {
  171.         printf ("node = %d, action = %d, parameter = %s\n",
  172.                 actions[i].node_id,
  173.                 actions[i].action,
  174.                 actions[i].parameter);
  175.         actionfuncs[actions[i].action](&actions[i]);
  176.     }
  177.     exit (0);
  178.    
  179. }
  180.