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, ...) if (verbose == true) printf(fmt, ## __VA_ARGS__)
35 * Video device to capture from. Sane default for GNU/Linux systems.
37 static char* device = "/dev/video0";
42 static int verbose = false;
47 static int silent = false;
52 static long unsigned int exit_code = 1;
55 * Helper process we started.
57 static struct GNUNET_OS_Process *p;
61 * Pipe used to communicate child death via signal.
63 static struct GNUNET_DISK_PipeHandle *sigpipe;
67 * Task triggered whenever we receive a SIGCHLD (child
68 * process died) or when user presses CTRL-C.
70 * @param cls closure, NULL
73 maint_child_death (void *cls)
75 enum GNUNET_OS_ProcessStatusType type;
78 GNUNET_OS_process_status (p, &type, &exit_code)) ||
79 (type != GNUNET_OS_PROCESS_EXITED) )
80 GNUNET_break (0 == GNUNET_OS_process_kill (p, GNUNET_TERM_SIG));
81 GNUNET_OS_process_destroy (p);
86 * Dispatch URIs to the appropriate GNUnet helper process
89 * @param uri uri to dispatch
90 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
91 * @param cfg configuration
94 gnunet_uri (void *cls,
97 const struct GNUNET_CONFIGURATION_Handle *cfg)
103 struct GNUNET_SCHEDULER_Task * rt;
106 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://"))) {
108 _("Invalid URI: does not start with `%s'\n"),
112 uri += strlen ("gnunet://");
113 if (NULL == (slash = strchr (uri, '/')))
116 _("Invalid URI: fails to specify subsystem\n"));
119 subsystem = GNUNET_strndup (uri, slash - uri);
121 GNUNET_CONFIGURATION_get_value_string (cfg,
127 _("No handler known for subsystem `%s'\n"),
129 GNUNET_free (subsystem);
132 GNUNET_free (subsystem);
133 rt = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
134 GNUNET_DISK_pipe_handle (sigpipe,
135 GNUNET_DISK_PIPE_END_READ),
136 &maint_child_death, NULL);
137 p = GNUNET_OS_start_process (GNUNET_NO, 0,
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;
163 zbar_processor_parse_config (proc, "enable"))
169 /* initialize the Processor */
171 (rc = zbar_processor_init(proc, device, 1)))
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 "Failed to open device `%s': %d\n",
180 /* enable the preview window */
181 if ( (0 != (rc = zbar_processor_set_visible (proc, 1))) ||
182 (0 != (rc = zbar_processor_set_active(proc, 1))) )
188 /* read at least one barcode (or until window closed) */
190 n = zbar_process_one (proc, -1);
192 /* hide the preview window */
193 (void) zbar_processor_set_active (proc, 0);
194 (void) zbar_processor_set_visible (proc, 0);
196 return NULL; /* likely user closed the window */
197 LOG ("Got %i images\n",
199 /* extract results */
200 symbols = zbar_processor_get_results (proc);
206 return zbar_symbol_set_first_symbol (symbols);
211 * Run zbar QR code parser.
213 * @return NULL on error, otherwise the URI that we found
218 zbar_processor_t *proc;
221 const zbar_symbol_t* symbol;
223 /* configure the Processor */
224 proc = zbar_processor_create (1);
231 symbol = get_symbol (proc);
234 zbar_processor_destroy (proc);
237 data = zbar_symbol_get_data (symbol);
241 zbar_processor_destroy (proc);
244 LOG ("Found %s \"%s\"\n",
245 zbar_get_symbol_name (zbar_symbol_get_type(symbol)),
247 ret = GNUNET_strdup (data);
249 zbar_processor_destroy(proc);
255 * Main function that will be run by the scheduler.
258 * @param args remaining command-line arguments
259 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
260 * @param cfg configuration
266 const struct GNUNET_CONFIGURATION_Handle *cfg)
279 printf ("Failed to add URI %s\n",
284 printf ("Added URI %s\n",
292 main (int argc, char *const *argv)
295 struct GNUNET_GETOPT_CommandLineOption options[] = {
296 GNUNET_GETOPT_option_string ('d', "device", "DEVICE",
297 gettext_noop ("use video-device DEVICE (default: /dev/video0"),
299 GNUNET_GETOPT_option_flag ('\0', "verbose",
300 gettext_noop ("be verbose"),
302 GNUNET_GETOPT_option_flag ('s', "silent",
303 gettext_noop ("do not show preview windows"),
305 GNUNET_GETOPT_OPTION_END
308 ret = GNUNET_PROGRAM_run (argc,
311 gettext_noop ("Scan a QR code using a video device and import the uri read"),
312 options, &run, NULL);
313 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;