main.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. {"version", no_argument, NULL, 'v'},
  12. {"help", no_argument, NULL, 'h'},
  13. {NULL, 0, NULL, 0}
  14. };
  15. void usage(const char *name);
  16. void print_version(void);
  17. int main(int argc, char *argv[]) {
  18. const struct sprog_family *fam;
  19. int opt;
  20. int option_index;
  21. int baud;
  22. char port[128];
  23. port[0] = 0;
  24. fam = NULL;
  25. while((opt = getopt_long(argc, argv, "p:b:f:h", sprog_options, &option_index))!=-1) {
  26. switch(opt) {
  27. case 'p':
  28. strncpy(port, optarg, sizeof(port));
  29. break;
  30. case 'f':
  31. if(strcmp(optarg, "lpc")==0)
  32. fam = &lpc_family;
  33. else
  34. sprog_error("Family '%s' not recognized\n", optarg);
  35. break;
  36. case 'b':
  37. if(sscanf(optarg, "%d", &baud)!=1) {
  38. sprog_error("Invalid value for baud rate: '%s'\n", optarg);
  39. return 1;
  40. }
  41. break;
  42. case 'v':
  43. print_version();
  44. return 0;
  45. break;
  46. case 'h':
  47. usage(argv[0]);
  48. return 0;
  49. break;
  50. default:
  51. usage(argv[0]);
  52. return 1;
  53. }
  54. }
  55. if(!port[0]) {
  56. sprog_error("Serial port not specified!\n");
  57. usage(argv[0]);
  58. return 1;
  59. }
  60. if(!fam) {
  61. sprog_error("Family not specified!\n");
  62. usage(argv[0]);
  63. return 1;
  64. }
  65. sprog_communicate(fam, port, baud);
  66. return 0;
  67. }
  68. void usage(const char *name) {
  69. printf("%s -p <port> -f <family> [options]\n", name);
  70. printf("Following options are supported:\n");
  71. printf(" -p <port>, --port <port> specify the serial port, eg. /dev/ttyS0\n");
  72. printf(" -f <family>, --family <family> specify the serial port, eg. /dev/ttyS0\n");
  73. printf(" -b <baud>, --baud <baud> specify the baud rate\n");
  74. printf(" -h, --help display this help\n");
  75. printf(" --version print version and exit\n");
  76. }
  77. void print_version(void) {
  78. printf("SProg v%s (built on " __DATE__ ")\n", sprog_version);
  79. }