main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /******************************************************************
  2. *
  3. * uEcho for C
  4. *
  5. * Copyright (C) Satoshi Konno 2015
  6. *
  7. * This is licensed under BSD-style license, see file COPYING.
  8. *
  9. ******************************************************************/
  10. #include <signal.h>
  11. #include <unistd.h>
  12. #include "lighting_dev.h"
  13. void usage()
  14. {
  15. printf("Usage : uecholight\n");
  16. printf(" -v : Enable verbose output\n");
  17. printf(" -m XXXXXX : Set Manifacture code\n");
  18. printf(" -h : Print this message\n");
  19. }
  20. void uecho_lighting_node_messagelitener(uEchoNode* obj, uEchoMessage* msg)
  21. {
  22. uecho_light_printrequest(msg);
  23. }
  24. int main(int argc, char* argv[])
  25. {
  26. bool verbose_mode;
  27. int manifacture_code;
  28. int c;
  29. uEchoNode* node;
  30. uEchoObject* obj;
  31. // Parse options
  32. verbose_mode = false;
  33. manifacture_code = 0;
  34. while ((c = getopt(argc, argv, "vhm:")) != -1) {
  35. switch (c) {
  36. case 'v': {
  37. verbose_mode = true;
  38. } break;
  39. case 'm': {
  40. sscanf(optarg, "%X", &manifacture_code);
  41. } break;
  42. case 'h': {
  43. usage();
  44. return EXIT_SUCCESS;
  45. }
  46. default: {
  47. usage();
  48. return EXIT_FAILURE;
  49. }
  50. }
  51. }
  52. argc -= optind;
  53. argv += optind;
  54. // Start node
  55. node = uecho_node_new();
  56. if (!node)
  57. return EXIT_FAILURE;
  58. if (verbose_mode) {
  59. uecho_node_setmessagelistener(node, uecho_lighting_node_messagelitener);
  60. }
  61. obj = uecho_light_new();
  62. if (!obj) {
  63. uecho_node_delete(node);
  64. return EXIT_FAILURE;
  65. }
  66. if (0 < manifacture_code) {
  67. uecho_node_setmanufacturercode(node, manifacture_code);
  68. }
  69. uecho_node_addobject(node, obj);
  70. if (!uecho_node_start(node)) {
  71. return EXIT_FAILURE;
  72. }
  73. while (uecho_node_isrunning(node)) {
  74. sigset_t sig_set;
  75. if (sigfillset(&sig_set) != 0)
  76. break;
  77. int sig_no;
  78. if (sigwait(&sig_set, &sig_no) != 0)
  79. break;
  80. switch (sig_no) {
  81. case SIGTERM:
  82. case SIGINT:
  83. case SIGKILL: {
  84. uecho_node_stop(node);
  85. } break;
  86. case SIGHUP: {
  87. uecho_node_stop(node);
  88. uecho_node_start(node);
  89. } break;
  90. }
  91. }
  92. uecho_node_delete(node);
  93. return EXIT_SUCCESS;
  94. }