uechodump.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <ctype.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include <uecho/uecho.h>
  14. const int UECHO_TEST_SEARCH_WAIT_MTIME = 5000;
  15. void usage()
  16. {
  17. printf("Usage : uechodump [options]\n");
  18. printf(" -n : Disable unicast server\n");
  19. printf(" -h : Print this message\n");
  20. printf(" -d : Enable debug output\n");
  21. }
  22. void key_usage()
  23. {
  24. printf("s : search\n");
  25. printf("q : quit\n");
  26. }
  27. void uecho_print_multicastmessages(uEchoController* ctrl, uEchoMessage* msg)
  28. {
  29. uEchoProperty* prop;
  30. size_t opc, n;
  31. opc = uecho_message_getopc(msg);
  32. printf("%s %1X %1X %02X %03X %03X %02X %ld ",
  33. uecho_message_getsourceaddress(msg),
  34. uecho_message_getehd1(msg),
  35. uecho_message_getehd2(msg),
  36. uecho_message_gettid(msg),
  37. uecho_message_getsourceobjectcode(msg),
  38. uecho_message_getdestinationobjectcode(msg),
  39. uecho_message_getesv(msg),
  40. opc);
  41. for (n = 0; n < opc; n++) {
  42. prop = uecho_message_getproperty(msg, n);
  43. printf("%02X", uecho_property_getcode(prop));
  44. }
  45. printf("\n");
  46. }
  47. int main(int argc, char* argv[])
  48. {
  49. bool nobind_mode;
  50. bool debug_mode;
  51. uEchoController* ctrl;
  52. int c, key;
  53. // Parse options
  54. nobind_mode = false;
  55. debug_mode = false;
  56. while ((c = getopt(argc, argv, "nhd")) != -1) {
  57. switch (c) {
  58. case 'n': {
  59. nobind_mode = true;
  60. } break;
  61. case 'd': {
  62. debug_mode = true;
  63. } break;
  64. case 'h': {
  65. usage();
  66. return EXIT_SUCCESS;
  67. }
  68. default: {
  69. usage();
  70. return EXIT_FAILURE;
  71. }
  72. }
  73. }
  74. argc -= optind;
  75. argv += optind;
  76. // Debug mode
  77. if (debug_mode) {
  78. uecho_log_setlevel(UECHO_LOG_DEBUG);
  79. }
  80. // Start controller
  81. ctrl = uecho_controller_new();
  82. if (!ctrl)
  83. return EXIT_FAILURE;
  84. uecho_controller_setmessagelistener(ctrl, uecho_print_multicastmessages);
  85. if (!uecho_controller_start(ctrl))
  86. return EXIT_FAILURE;
  87. do {
  88. key = getchar();
  89. if (key < 0) {
  90. uecho_sleep(100);
  91. continue;
  92. }
  93. switch (tolower(key)) {
  94. case 's':
  95. uecho_controller_search(ctrl);
  96. break;
  97. default:
  98. key_usage();
  99. }
  100. } while (key != 'q');
  101. uecho_controller_stop(ctrl);
  102. uecho_controller_delete(ctrl);
  103. return EXIT_SUCCESS;
  104. }