gnunet-sensor.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. This file is part of GNUnet.
  3. (C)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file sensor/gnunet-sensor.c
  19. * @brief sensor tool
  20. * @author Omar Tarabai
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_sensor_service.h"
  25. static int ret;
  26. /**
  27. * option '-a'
  28. */
  29. static int get_all;
  30. /**
  31. * option '-g'
  32. */
  33. static char *get_sensor;
  34. /**
  35. * option '-f'
  36. */
  37. static char *force_anomaly;
  38. /*
  39. * Handle to sensor service
  40. */
  41. static struct GNUNET_SENSOR_Handle *sensor_handle;
  42. /**
  43. * Run on shutdown
  44. *
  45. * @param cls unused
  46. * @param tc scheduler context
  47. */
  48. static void
  49. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  50. {
  51. if (NULL != sensor_handle)
  52. {
  53. GNUNET_SENSOR_disconnect (sensor_handle);
  54. sensor_handle = NULL;
  55. }
  56. }
  57. /**
  58. * Callback for getting sensor info from service
  59. *
  60. * @param cls not used
  61. * @param sensor brief information about sensor (NULL means end of transmission)
  62. * @param err_msg contains error string if any
  63. */
  64. void
  65. print_sensor_info (void *cls, const struct SensorInfoShort *sensor,
  66. const char *err_msg)
  67. {
  68. if (NULL != err_msg)
  69. {
  70. printf ("Error: %s\n", err_msg);
  71. GNUNET_SCHEDULER_shutdown ();
  72. return;
  73. }
  74. if (NULL == sensor) /* no more sensors from service */
  75. {
  76. GNUNET_SCHEDULER_shutdown ();
  77. return;
  78. }
  79. printf ("Name: %s\nVersion: %d.%d\n", sensor->name, sensor->version_major,
  80. sensor->version_minor);
  81. if (NULL != sensor->description)
  82. printf ("Description: %s\n", sensor->description);
  83. printf ("\n");
  84. }
  85. /**
  86. * Continuation called after a force anomaly request is sent.
  87. *
  88. * @param cls Closure (unused)
  89. * @param emsg Error message, NULL of no error
  90. */
  91. void
  92. force_anomaly_cont (void *cls, const char *emsg)
  93. {
  94. if (NULL != emsg)
  95. printf ("Error: %s\n", emsg);
  96. GNUNET_SCHEDULER_shutdown ();
  97. }
  98. /**
  99. * Main function that will be run by the scheduler.
  100. *
  101. * @param cls closure
  102. * @param args remaining command-line arguments
  103. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  104. * @param cfg configuration
  105. */
  106. static void
  107. run (void *cls, char *const *args, const char *cfgfile,
  108. const struct GNUNET_CONFIGURATION_Handle *cfg)
  109. {
  110. sensor_handle = NULL;
  111. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  112. NULL);
  113. sensor_handle = GNUNET_SENSOR_connect (cfg);
  114. GNUNET_assert (NULL != sensor_handle);
  115. if (GNUNET_YES == get_all)
  116. {
  117. GNUNET_SENSOR_iterate (sensor_handle, GNUNET_TIME_UNIT_FOREVER_REL, NULL,
  118. &print_sensor_info, NULL);
  119. }
  120. else if (NULL != get_sensor)
  121. {
  122. GNUNET_SENSOR_iterate (sensor_handle, GNUNET_TIME_UNIT_FOREVER_REL,
  123. get_sensor, &print_sensor_info, NULL);
  124. }
  125. else if (NULL != force_anomaly)
  126. {
  127. GNUNET_SENSOR_force_anomaly (sensor_handle, "nse", GNUNET_YES,
  128. &force_anomaly_cont, NULL);
  129. }
  130. ret = 0;
  131. }
  132. /**
  133. * The main function to sensor.
  134. *
  135. * @param argc number of arguments from the command line
  136. * @param argv command line arguments
  137. * @return 0 ok, 1 on error
  138. */
  139. int
  140. main (int argc, char *const *argv)
  141. {
  142. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  143. {'a', "all", NULL,
  144. gettext_noop ("Retrieve information about all defined sensors"),
  145. 0, &GNUNET_GETOPT_set_one, &get_all},
  146. {'g', "get-sensor", NULL,
  147. gettext_noop ("Retrieve information about a single sensor"),
  148. 1, &GNUNET_GETOPT_set_string, &get_sensor},
  149. {'f', "force-anomaly", NULL,
  150. gettext_noop ("Force an anomaly on a sensor, use only for testing"),
  151. 1, &GNUNET_GETOPT_set_string, &force_anomaly},
  152. GNUNET_GETOPT_OPTION_END
  153. };
  154. return (GNUNET_OK ==
  155. GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor [options [value]]",
  156. gettext_noop ("sensor"), options, &run,
  157. NULL)) ? ret : 1;
  158. }
  159. /* end of gnunet-sensor.c */