main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <getopt.h>
  4. #include <sprog.h>
  5. #include <serial.h>
  6. #include <lpc.h>
  7. struct option sprog_options[] = {
  8. {"port", required_argument, NULL, 'p'},
  9. {"family", required_argument, NULL, 'f'},
  10. {"baud", required_argument, NULL, 'b'},
  11. {"exec", required_argument, NULL, 'e'},
  12. {"version", no_argument, NULL, 'v'},
  13. {"help", no_argument, NULL, 'h'},
  14. {NULL, 0, NULL, 0}
  15. };
  16. void usage(const char *name);
  17. void print_version(void);
  18. int main(int argc, char *argv[]) {
  19. const struct sprog_family *fam;
  20. struct sprog_data d;
  21. int opt;
  22. int option_index;
  23. int baud;
  24. char port[128];
  25. char path[128];
  26. port[0] = 0;
  27. path[0] = 0;
  28. fam = NULL;
  29. while((opt = getopt_long(argc, argv, "p:b:f:e:h", sprog_options, &option_index))!=-1) {
  30. switch(opt) {
  31. case 'p':
  32. strncpy(port, optarg, sizeof(port));
  33. break;
  34. case 'e':
  35. strncpy(path, optarg, sizeof(path));
  36. break;
  37. case 'f':
  38. if(strcmp(optarg, "lpc")==0)
  39. fam = &lpc_family;
  40. else
  41. sprog_error("Family '%s' not recognized\n", optarg);
  42. break;
  43. case 'b':
  44. if(sscanf(optarg, "%d", &baud)!=1) {
  45. sprog_error("Invalid value for baud rate: '%s'\n", optarg);
  46. return 1;
  47. }
  48. break;
  49. case 'v':
  50. print_version();
  51. return 0;
  52. break;
  53. case 'h':
  54. usage(argv[0]);
  55. return 0;
  56. break;
  57. default:
  58. usage(argv[0]);
  59. return 1;
  60. }
  61. }
  62. if(!port[0]) {
  63. sprog_error("Serial port not specified!\n");
  64. usage(argv[0]);
  65. return 1;
  66. }
  67. if(!fam) {
  68. sprog_error("Family not specified!\n");
  69. usage(argv[0]);
  70. return 1;
  71. }
  72. if(path[0]) {
  73. d.data = NULL;
  74. d.size = 0;
  75. sprog_load(&d, path);
  76. sprog_communicate(fam, &d, port, baud);
  77. } else
  78. sprog_communicate(fam, NULL, port, baud);
  79. return 0;
  80. }
  81. void usage(const char *name) {
  82. printf("%s -p <port> -f <family> [options]\n", name);
  83. printf("Following options are supported:\n");
  84. printf(" -p <port>, --port <port> specify the serial port, eg. /dev/ttyS0\n");
  85. printf(" -f <family>, --family <family> specify the microcontroller family, eg. lpc\n");
  86. printf(" -b <baud>, --baud <baud> specify the baud rate\n");
  87. printf(" -e <file>, --exec <file> execute the given file\n");
  88. printf(" -h, --help display this help\n");
  89. printf(" --version print version and exit\n");
  90. }
  91. void print_version(void) {
  92. printf("SProg v%s (built on " __DATE__ ")\n", sprog_version);
  93. }