gnunet-qr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013-2019 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/gnunet-qr.c
  18. * @author Hartmut Goebel (original implementation)
  19. * @author Martin Schanzenbach (integrate gnunet-uri)
  20. * @author Christian Grothoff (error handling)
  21. */
  22. #include <stdio.h>
  23. #include <zbar.h>
  24. #include <stdbool.h>
  25. #include "platform.h"
  26. #include "gnunet_util_lib.h"
  27. #define LOG(fmt, ...) \
  28. if (verbose == true) \
  29. printf (fmt, ## __VA_ARGS__)
  30. /**
  31. * Video device to capture from. Sane default for GNU/Linux systems.
  32. */
  33. static char *device = "/dev/video0";
  34. /**
  35. * --verbose option
  36. */
  37. static int verbose = false;
  38. /**
  39. * --silent option
  40. */
  41. static int silent = false;
  42. /**
  43. * Handler exit code
  44. */
  45. static long unsigned int exit_code = 1;
  46. /**
  47. * Helper process we started.
  48. */
  49. static struct GNUNET_OS_Process *p;
  50. /**
  51. * Pipe used to communicate child death via signal.
  52. */
  53. static struct GNUNET_DISK_PipeHandle *sigpipe;
  54. /**
  55. * Task triggered whenever we receive a SIGCHLD (child
  56. * process died) or when user presses CTRL-C.
  57. *
  58. * @param cls closure, NULL
  59. */
  60. static void
  61. maint_child_death (void *cls)
  62. {
  63. enum GNUNET_OS_ProcessStatusType type;
  64. if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) ||
  65. (type != GNUNET_OS_PROCESS_EXITED))
  66. GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
  67. GNUNET_OS_process_destroy (p);
  68. }
  69. /**
  70. * Dispatch URIs to the appropriate GNUnet helper process
  71. *
  72. * @param cls closure
  73. * @param uri uri to dispatch
  74. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  75. * @param cfg configuration
  76. */
  77. static void
  78. gnunet_uri (void *cls,
  79. const char *uri,
  80. const char *cfgfile,
  81. const struct GNUNET_CONFIGURATION_Handle *cfg)
  82. {
  83. const char *orig_uri;
  84. const char *slash;
  85. char *subsystem;
  86. char *program;
  87. struct GNUNET_SCHEDULER_Task *rt;
  88. orig_uri = uri;
  89. if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
  90. {
  91. fprintf (stderr,
  92. _ ("Invalid URI: does not start with `%s'\n"),
  93. "gnunet://");
  94. return;
  95. }
  96. uri += strlen ("gnunet://");
  97. if (NULL == (slash = strchr (uri, '/')))
  98. {
  99. fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
  100. return;
  101. }
  102. subsystem = GNUNET_strndup (uri, slash - uri);
  103. if (GNUNET_OK !=
  104. GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
  105. {
  106. fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
  107. GNUNET_free (subsystem);
  108. return;
  109. }
  110. GNUNET_free (subsystem);
  111. rt = GNUNET_SCHEDULER_add_read_file (
  112. GNUNET_TIME_UNIT_FOREVER_REL,
  113. GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
  114. &maint_child_death,
  115. NULL);
  116. p = GNUNET_OS_start_process (GNUNET_NO,
  117. 0,
  118. NULL,
  119. NULL,
  120. NULL,
  121. program,
  122. program,
  123. orig_uri,
  124. NULL);
  125. GNUNET_free (program);
  126. if (NULL == p)
  127. GNUNET_SCHEDULER_cancel (rt);
  128. }
  129. /**
  130. * Obtain QR code 'symbol' from @a proc.
  131. *
  132. * @param proc zbar processor to use
  133. * @return NULL on error
  134. */
  135. static const zbar_symbol_t *
  136. get_symbol (zbar_processor_t *proc)
  137. {
  138. const zbar_symbol_set_t *symbols;
  139. int rc;
  140. int n;
  141. if (0 != zbar_processor_parse_config (proc, "enable"))
  142. {
  143. GNUNET_break (0);
  144. return NULL;
  145. }
  146. /* initialize the Processor */
  147. if (0 != (rc = zbar_processor_init (proc, device, 1)))
  148. {
  149. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  150. "Failed to open device `%s': %d\n",
  151. device,
  152. rc);
  153. return NULL;
  154. }
  155. /* enable the preview window */
  156. if ((0 != (rc = zbar_processor_set_visible (proc, 1))) ||
  157. (0 != (rc = zbar_processor_set_active (proc, 1))))
  158. {
  159. GNUNET_break (0);
  160. return NULL;
  161. }
  162. /* read at least one barcode (or until window closed) */
  163. LOG ("Capturing\n");
  164. n = zbar_process_one (proc, -1);
  165. /* hide the preview window */
  166. (void) zbar_processor_set_active (proc, 0);
  167. (void) zbar_processor_set_visible (proc, 0);
  168. if (-1 == n)
  169. return NULL; /* likely user closed the window */
  170. LOG ("Got %i images\n", n);
  171. /* extract results */
  172. symbols = zbar_processor_get_results (proc);
  173. if (NULL == symbols)
  174. {
  175. GNUNET_break (0);
  176. return NULL;
  177. }
  178. return zbar_symbol_set_first_symbol (symbols);
  179. }
  180. /**
  181. * Run zbar QR code parser.
  182. *
  183. * @return NULL on error, otherwise the URI that we found
  184. */
  185. static char *
  186. run_zbar ()
  187. {
  188. zbar_processor_t *proc;
  189. const char *data;
  190. char *ret;
  191. const zbar_symbol_t *symbol;
  192. /* configure the Processor */
  193. proc = zbar_processor_create (1);
  194. if (NULL == proc)
  195. {
  196. GNUNET_break (0);
  197. return NULL;
  198. }
  199. symbol = get_symbol (proc);
  200. if (NULL == symbol)
  201. {
  202. zbar_processor_destroy (proc);
  203. return NULL;
  204. }
  205. data = zbar_symbol_get_data (symbol);
  206. if (NULL == data)
  207. {
  208. GNUNET_break (0);
  209. zbar_processor_destroy (proc);
  210. return NULL;
  211. }
  212. LOG ("Found %s \"%s\"\n",
  213. zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
  214. data);
  215. ret = GNUNET_strdup (data);
  216. /* clean up */
  217. zbar_processor_destroy (proc);
  218. return ret;
  219. }
  220. /**
  221. * Main function that will be run by the scheduler.
  222. *
  223. * @param cls closure
  224. * @param args remaining command-line arguments
  225. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  226. * @param cfg configuration
  227. */
  228. static void
  229. run (void *cls,
  230. char *const *args,
  231. const char *cfgfile,
  232. const struct GNUNET_CONFIGURATION_Handle *cfg)
  233. {
  234. char *data;
  235. data = run_zbar ();
  236. if (NULL == data)
  237. return;
  238. gnunet_uri (cls, data, cfgfile, cfg);
  239. if (exit_code != 0)
  240. {
  241. printf ("Failed to add URI %s\n", data);
  242. }
  243. else
  244. {
  245. printf ("Added URI %s\n", data);
  246. }
  247. GNUNET_free (data);
  248. };
  249. int
  250. main (int argc, char *const *argv)
  251. {
  252. int ret;
  253. struct GNUNET_GETOPT_CommandLineOption options[] =
  254. { GNUNET_GETOPT_option_string (
  255. 'd',
  256. "device",
  257. "DEVICE",
  258. gettext_noop ("use video-device DEVICE (default: /dev/video0"),
  259. &device),
  260. GNUNET_GETOPT_option_flag ('\0',
  261. "verbose",
  262. gettext_noop ("be verbose"),
  263. &verbose),
  264. GNUNET_GETOPT_option_flag ('s',
  265. "silent",
  266. gettext_noop ("do not show preview windows"),
  267. &silent),
  268. GNUNET_GETOPT_OPTION_END };
  269. ret = GNUNET_PROGRAM_run (
  270. argc,
  271. argv,
  272. "gnunet-qr",
  273. gettext_noop (
  274. "Scan a QR code using a video device and import the uri read"),
  275. options,
  276. &run,
  277. NULL);
  278. return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
  279. }