ncdudevmonitor_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file ncdudevmonitor_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <system/BTime.h>
  31. #include <base/BLog.h>
  32. #include <system/BReactor.h>
  33. #include <system/BSignal.h>
  34. #include <system/BProcess.h>
  35. #include <system/BNetwork.h>
  36. #include <udevmonitor/NCDUdevMonitor.h>
  37. BReactor reactor;
  38. BProcessManager manager;
  39. NCDUdevMonitor monitor;
  40. static void signal_handler (void *user);
  41. static void monitor_handler_event (void *unused);
  42. static void monitor_handler_error (void *unused, int is_error);
  43. int main (int argc, char **argv)
  44. {
  45. int ret = 1;
  46. if (argc < 2 || (strcmp(argv[1], "monitor") && strcmp(argv[1], "info"))) {
  47. fprintf(stderr, "Usage: %s <monitor/info>\n", argv[0]);
  48. goto fail0;
  49. }
  50. int is_info_mode = !strcmp(argv[1], "info");
  51. if (!BNetwork_GlobalInit()) {
  52. DEBUG("BNetwork_GlobalInit failed");
  53. goto fail0;
  54. }
  55. BTime_Init();
  56. BLog_InitStdout();
  57. if (!BReactor_Init(&reactor)) {
  58. DEBUG("BReactor_Init failed");
  59. goto fail1;
  60. }
  61. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  62. DEBUG("BSignal_Init failed");
  63. goto fail2;
  64. }
  65. if (!BProcessManager_Init(&manager, &reactor)) {
  66. DEBUG("BProcessManager_Init failed");
  67. goto fail3;
  68. }
  69. if (!NCDUdevMonitor_Init(&monitor, &reactor, &manager, is_info_mode, NULL,
  70. monitor_handler_event,
  71. monitor_handler_error
  72. )) {
  73. DEBUG("NCDUdevMonitor_Init failed");
  74. goto fail4;
  75. }
  76. ret = BReactor_Exec(&reactor);
  77. NCDUdevMonitor_Free(&monitor);
  78. fail4:
  79. BProcessManager_Free(&manager);
  80. fail3:
  81. BSignal_Finish();
  82. fail2:
  83. BReactor_Free(&reactor);
  84. fail1:
  85. BLog_Free();
  86. fail0:
  87. DebugObjectGlobal_Finish();
  88. return ret;
  89. }
  90. void signal_handler (void *user)
  91. {
  92. DEBUG("termination requested");
  93. BReactor_Quit(&reactor, 1);
  94. }
  95. void monitor_handler_event (void *unused)
  96. {
  97. // accept event
  98. NCDUdevMonitor_Done(&monitor);
  99. if (NCDUdevMonitor_IsReadyEvent(&monitor)) {
  100. printf("ready\n");
  101. return;
  102. }
  103. printf("event\n");
  104. int num_props = NCDUdevMonitor_GetNumProperties(&monitor);
  105. for (int i = 0; i < num_props; i++) {
  106. const char *name;
  107. const char *value;
  108. NCDUdevMonitor_GetProperty(&monitor, i, &name, &value);
  109. printf(" %s=%s\n", name, value);
  110. }
  111. }
  112. void monitor_handler_error (void *unused, int is_error)
  113. {
  114. if (is_error) {
  115. DEBUG("monitor error");
  116. } else {
  117. DEBUG("monitor finished");
  118. }
  119. BReactor_Quit(&reactor, (is_error ? 1 : 0));
  120. }