2 This file is part of GNUnet.
3 Copyright (C) 2013-2019 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/gnunet-qr.c
22 * @author Hartmut Goebel (original implementation)
23 * @author Martin Schanzenbach (integrate gnunet-uri)
24 * @author Christian Grothoff (error handling)
30 #include "gnunet_util_lib.h"
32 #define LOG(fmt, ...) \
33 if (verbose == true) \
34 printf (fmt, ##__VA_ARGS__)
37 * Video device to capture from. Sane default for GNU/Linux systems.
39 static char *device = "/dev/video0";
44 static int verbose = false;
49 static int silent = false;
54 static long unsigned int exit_code = 1;
57 * Helper process we started.
59 static struct GNUNET_OS_Process *p;
63 * Pipe used to communicate child death via signal.
65 static struct GNUNET_DISK_PipeHandle *sigpipe;
69 * Task triggered whenever we receive a SIGCHLD (child
70 * process died) or when user presses CTRL-C.
72 * @param cls closure, NULL
75 maint_child_death (void *cls)
77 enum GNUNET_OS_ProcessStatusType type;
79 if ((GNUNET_OK != GNUNET_OS_process_status (p, &type, &exit_code)) ||
80 (type != GNUNET_OS_PROCESS_EXITED))
81 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
82 GNUNET_OS_process_destroy (p);
87 * Dispatch URIs to the appropriate GNUnet helper process
90 * @param uri uri to dispatch
91 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
92 * @param cfg configuration
95 gnunet_uri (void *cls,
98 const struct GNUNET_CONFIGURATION_Handle *cfg)
100 const char *orig_uri;
104 struct GNUNET_SCHEDULER_Task *rt;
107 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
110 _ ("Invalid URI: does not start with `%s'\n"),
114 uri += strlen ("gnunet://");
115 if (NULL == (slash = strchr (uri, '/')))
117 fprintf (stderr, _ ("Invalid URI: fails to specify subsystem\n"));
120 subsystem = GNUNET_strndup (uri, slash - uri);
122 GNUNET_CONFIGURATION_get_value_string (cfg, "uri", subsystem, &program))
124 fprintf (stderr, _ ("No handler known for subsystem `%s'\n"), subsystem);
125 GNUNET_free (subsystem);
128 GNUNET_free (subsystem);
129 rt = GNUNET_SCHEDULER_add_read_file (
130 GNUNET_TIME_UNIT_FOREVER_REL,
131 GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ),
134 p = GNUNET_OS_start_process (GNUNET_NO,
143 GNUNET_free (program);
145 GNUNET_SCHEDULER_cancel (rt);
150 * Obtain QR code 'symbol' from @a proc.
152 * @param proc zbar processor to use
153 * @return NULL on error
155 static const zbar_symbol_t *
156 get_symbol (zbar_processor_t *proc)
158 const zbar_symbol_set_t *symbols;
162 if (0 != zbar_processor_parse_config (proc, "enable"))
168 /* initialize the Processor */
169 if (0 != (rc = zbar_processor_init (proc, device, 1)))
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 "Failed to open device `%s': %d\n",
178 /* enable the preview window */
179 if ((0 != (rc = zbar_processor_set_visible (proc, 1))) ||
180 (0 != (rc = zbar_processor_set_active (proc, 1))))
186 /* read at least one barcode (or until window closed) */
188 n = zbar_process_one (proc, -1);
190 /* hide the preview window */
191 (void) zbar_processor_set_active (proc, 0);
192 (void) zbar_processor_set_visible (proc, 0);
194 return NULL; /* likely user closed the window */
195 LOG ("Got %i images\n", n);
196 /* extract results */
197 symbols = zbar_processor_get_results (proc);
203 return zbar_symbol_set_first_symbol (symbols);
208 * Run zbar QR code parser.
210 * @return NULL on error, otherwise the URI that we found
215 zbar_processor_t *proc;
218 const zbar_symbol_t *symbol;
220 /* configure the Processor */
221 proc = zbar_processor_create (1);
228 symbol = get_symbol (proc);
231 zbar_processor_destroy (proc);
234 data = zbar_symbol_get_data (symbol);
238 zbar_processor_destroy (proc);
241 LOG ("Found %s \"%s\"\n",
242 zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
244 ret = GNUNET_strdup (data);
246 zbar_processor_destroy (proc);
252 * Main function that will be run by the scheduler.
255 * @param args remaining command-line arguments
256 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
257 * @param cfg configuration
263 const struct GNUNET_CONFIGURATION_Handle *cfg)
270 gnunet_uri (cls, data, cfgfile, cfg);
273 printf ("Failed to add URI %s\n", data);
277 printf ("Added URI %s\n", data);
284 main (int argc, char *const *argv)
287 struct GNUNET_GETOPT_CommandLineOption options[] =
288 {GNUNET_GETOPT_option_string (
292 gettext_noop ("use video-device DEVICE (default: /dev/video0"),
294 GNUNET_GETOPT_option_flag ('\0',
296 gettext_noop ("be verbose"),
298 GNUNET_GETOPT_option_flag ('s',
300 gettext_noop ("do not show preview windows"),
302 GNUNET_GETOPT_OPTION_END};
304 ret = GNUNET_PROGRAM_run (
309 "Scan a QR code using a video device and import the uri read"),
313 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;