A small USV for ALIX boards with i2c support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.9KB

  1. /***************************************************************************
  2. * Copyright (C) 03/2009 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <getopt.h>
  24. #include "logging.h"
  25. #include "unixsocket.h"
  26. #define DEFAULT_SOCKET "alix-usvd.sock"
  27. static struct option opts[] = {
  28. {"command", 1, 0, 'c'},
  29. {"help", 0, 0, 'h'},
  30. {"socket", 1, 0, 's'},
  31. {0, 0, 0, 0}
  32. };
  33. int main(int argc, char *argv[])
  34. {
  35. char *socket_path = DEFAULT_SOCKET;
  36. char *cmd = NULL;
  37. int code, arg = 0;
  38. do {
  39. code = getopt_long(argc, argv, "c:hs:", opts, &arg);
  40. switch (code) {
  41. case 'c': /* command */
  42. if (strncasecmp(optarg, "IDLE", 4) == 0)
  43. cmd = optarg;
  44. else if (strncasecmp(optarg, "TEST", 4) == 0)
  45. cmd = optarg;
  46. else if (strncasecmp(optarg, "CHARGE", 6) == 0)
  47. cmd = optarg;
  48. else if (strncasecmp(optarg, "POWEROFF", 8) == 0)
  49. cmd = optarg;
  50. else if (strncasecmp(optarg, "STATUS", 6) == 0)
  51. cmd = optarg;
  52. break;
  53. case 's': /* socket */
  54. socket_path = optarg;
  55. break;
  56. case 'h': /* help */
  57. printf("Usage: alix-usv [-s socket] -c <status|idle|charge|test|poweroff>\n\n");
  58. exit(0);
  59. break;
  60. case '?': /* error */
  61. exit(1);
  62. break;
  63. default: /* unknown / all options parsed */
  64. break;
  65. }
  66. } while (code != -1);
  67. if (cmd == NULL) {
  68. log_print(LOG_ERROR, "invalid command: %s", cmd);
  69. return -1;
  70. }
  71. int sock = unix_connect(socket_path);
  72. if (sock < 0)
  73. return -1;
  74. write(sock, cmd, strlen(cmd));
  75. char buf[64];
  76. int len = read(sock, buf, sizeof(buf));
  77. if (len <= 0) {
  78. log_print(LOG_ERROR, "no answer from server");
  79. return -1;
  80. }
  81. log_print(LOG_INFO, "%s", buf);
  82. close(sock);
  83. return 0;
  84. }