GNUnet  0.20.0
gnunet-qr.c File Reference
#include "platform.h"
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <zbar.h>
#include "gnunet_util_lib.h"
Include dependency graph for gnunet-qr.c:

Go to the source code of this file.

Macros

#define LOG(fmt, ...)
 Macro to handle verbosity when printing messages. More...
 

Functions

static void shutdown_program (void *cls)
 Executed when program is terminating. More...
 
static void wait_child (void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int code)
 Callback executed when the child process terminates. More...
 
static void handle_uri (void *cls, const char *uri, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Dispatch URIs to the appropriate GNUnet helper process. More...
 
static const zbar_symbol_t * get_symbol (zbar_processor_t *proc)
 Obtain a QR code symbol from proc. More...
 
static char * run_zbar (void)
 Run the zbar QR code parser. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Main function executed by the scheduler. More...
 
int main (int argc, char *const *argv)
 

Variables

static int exit_code = 0
 Global exit code. More...
 
static char * device = NULL
 Video device to capture from. More...
 
static unsigned int verbosity = 0
 Requested verbosity. More...
 
struct GNUNET_OS_Processchildproc = NULL
 Child process handle. More...
 
static struct GNUNET_ChildWaitHandlewaitchildproc = NULL
 Child process handle for waiting. More...
 

Detailed Description

Author
Hartmut Goebel (original implementation)
Martin Schanzenbach (integrate gnunet-uri)
Christian Grothoff (error handling)

Definition in file gnunet-qr.c.

Macro Definition Documentation

◆ LOG

#define LOG (   fmt,
  ... 
)
Value:
do \
{ \
if (0 < verbosity) \
{ \
GNUNET_log (GNUNET_ERROR_TYPE_INFO, fmt, ##__VA_ARGS__); \
if (verbosity > 1) \
{ \
fprintf (stdout, fmt, ##__VA_ARGS__); \
} \
} \
} \
while (0)
static unsigned int verbosity
Requested verbosity.
Definition: gnunet-qr.c:64
@ GNUNET_ERROR_TYPE_INFO

Macro to handle verbosity when printing messages.

Definition at line 79 of file gnunet-qr.c.

Function Documentation

◆ shutdown_program()

static void shutdown_program ( void *  cls)
static

Executed when program is terminating.

Definition at line 97 of file gnunet-qr.c.

98 {
99  if (NULL != waitchildproc)
100  {
102  }
103  if (NULL != childproc)
104  {
105  /* A bit brutal, but this process is terminating so we're out of time */
107  }
108 }
struct GNUNET_OS_Process * childproc
Child process handle.
Definition: gnunet-qr.c:69
static struct GNUNET_ChildWaitHandle * waitchildproc
Child process handle for waiting.
Definition: gnunet-qr.c:74
void GNUNET_wait_child_cancel(struct GNUNET_ChildWaitHandle *cwh)
Stop waiting on this child.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:210

References childproc, GNUNET_OS_process_kill(), GNUNET_wait_child_cancel(), and waitchildproc.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wait_child()

static void wait_child ( void *  cls,
enum GNUNET_OS_ProcessStatusType  type,
long unsigned int  code 
)
static

Callback executed when the child process terminates.

Parameters
clsclosure
typestatus of the child process
codethe exit code of the child process

Definition at line 118 of file gnunet-qr.c.

121 {
123  childproc = NULL;
124  waitchildproc = NULL;
125 
126  char *uri = cls;
127 
128  if (0 != exit_code)
129  {
130  fprintf (stdout, _("Failed to add URI %s\n"), uri);
131  }
132  else
133  {
134  fprintf (stdout, _("Added URI %s\n"), uri);
135  }
136 
137  GNUNET_free (uri);
138 
140 }
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static int exit_code
Global exit code.
Definition: gnunet-qr.c:43
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:260
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:562
#define _(String)
GNU gettext support macro.
Definition: platform.h:178

References _, childproc, exit_code, GNUNET_free, GNUNET_OS_process_destroy(), GNUNET_SCHEDULER_shutdown(), uri, and waitchildproc.

Referenced by handle_uri().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_uri()

static void handle_uri ( void *  cls,
const char *  uri,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Dispatch URIs to the appropriate GNUnet helper process.

Parameters
clsclosure
uriURI to dispatch
cfgfilename of the configuration file in use
cfgthe configuration in use

Definition at line 151 of file gnunet-qr.c.

155 {
156  const char *cursor = uri;
157 
158  if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
159  {
160  fprintf (stderr,
161  _("Invalid URI: does not start with `gnunet://'\n"));
162  exit_code = 1;
163  return;
164  }
165 
166  cursor += strlen ("gnunet://");
167 
168  const char *slash = strchr (cursor, '/');
169  if (NULL == slash)
170  {
171  fprintf (stderr, _("Invalid URI: fails to specify a subsystem\n"));
172  exit_code = 1;
173  return;
174  }
175 
176  char *subsystem = GNUNET_strndup (cursor, slash - cursor);
177  char *program = NULL;
178 
179  if (GNUNET_OK !=
181  {
182  fprintf (stderr, _("No known handler for subsystem `%s'\n"), subsystem);
184  exit_code = 1;
185  return;
186  }
187 
189 
190  char **childargv = NULL;
191  unsigned int childargc = 0;
192 
193  for (const char *token=strtok (program, " ");
194  NULL!=token;
195  token=strtok(NULL, " "))
196  {
197  GNUNET_array_append (childargv, childargc, GNUNET_strdup (token));
198  }
199  GNUNET_array_append (childargv, childargc, GNUNET_strdup (uri));
200  GNUNET_array_append (childargv, childargc, NULL);
201 
203  NULL,
204  NULL,
205  NULL,
206  childargv[0],
207  childargv);
208  for (size_t i=0; i<childargc-1; ++i)
209  {
210  GNUNET_free (childargv[i]);
211  }
212 
213  GNUNET_array_grow (childargv, childargc, 0);
214 
215  if (NULL == childproc)
216  {
218  _("Unable to start child process `%s'\n"),
219  program);
220  GNUNET_free (program);
221  exit_code = 1;
222  return;
223  }
224 
226 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static void wait_child(void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int code)
Callback executed when the child process terminates.
Definition: gnunet-qr.c:118
static char * subsystem
Set to subsystem that we're going to get stats for (or NULL for all).
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
#define GNUNET_log(kind,...)
struct GNUNET_ChildWaitHandle * GNUNET_wait_child(struct GNUNET_OS_Process *proc, GNUNET_ChildCompletedCallback cb, void *cb_cls)
Starts the handling of the child processes.
@ GNUNET_OK
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:567
@ GNUNET_OS_INHERIT_STD_ALL
Use this option to have all of the standard streams (stdin, stdout and stderror) be inherited.

References _, cfg, childproc, exit_code, GNUNET_array_append, GNUNET_array_grow, GNUNET_CONFIGURATION_get_value_string(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_OS_INHERIT_STD_ALL, GNUNET_OS_start_process_vap(), GNUNET_strdup, GNUNET_strndup, GNUNET_wait_child(), subsystem, uri, wait_child(), and waitchildproc.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_symbol()

static const zbar_symbol_t* get_symbol ( zbar_processor_t *  proc)
static

Obtain a QR code symbol from proc.

Parameters
procthe zbar processor to use
Returns
NULL on error

Definition at line 235 of file gnunet-qr.c.

236 {
237  if (0 != zbar_processor_parse_config (proc, "enable"))
238  {
239  GNUNET_break (0);
240  return NULL;
241  }
242 
243  int r = zbar_processor_init (proc, device, 1);
244  if (0 != r)
245  {
247  _("Failed to open device: `%s': %d\n"),
248  device,
249  r);
250  return NULL;
251  }
252 
253  r = zbar_processor_set_visible (proc, 1);
254  r += zbar_processor_set_active (proc, 1);
255  if (0 != r)
256  {
257  GNUNET_break (0);
258  return NULL;
259  }
260 
261  LOG (_("Capturing...\n"));
262 
263  int n = zbar_process_one (proc, -1);
264 
265  zbar_processor_set_active (proc, 0);
266  zbar_processor_set_visible (proc, 0);
267 
268  if (-1 == n)
269  {
270  LOG (_("No captured images\n"));
271  return NULL;
272  }
273 
274  LOG(_("Got %d images\n"), n);
275 
276  const zbar_symbol_set_t *symbols = zbar_processor_get_results (proc);
277  if (NULL == symbols)
278  {
279  GNUNET_break (0);
280  return NULL;
281  }
282 
283  return zbar_symbol_set_first_symbol (symbols);
284 }
#define LOG(fmt,...)
Macro to handle verbosity when printing messages.
Definition: gnunet-qr.c:79
static char * device
Video device to capture from.
Definition: gnunet-qr.c:50
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.

References _, device, GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, LOG, and GNUNET_ChildWaitHandle::proc.

Referenced by run_zbar().

Here is the caller graph for this function:

◆ run_zbar()

static char* run_zbar ( void  )
static

Run the zbar QR code parser.

Returns
NULL on error

Definition at line 292 of file gnunet-qr.c.

293 {
294  zbar_processor_t *proc = zbar_processor_create (1);
295  if (NULL == proc)
296  {
297  GNUNET_break (0);
298  return NULL;
299  }
300 
301  if (NULL == device)
302  {
303  device = GNUNET_strdup ("/dev/video0");
304  }
305 
306  const zbar_symbol_t *symbol = get_symbol (proc);
307  if (NULL == symbol)
308  {
309  zbar_processor_destroy (proc);
310  return NULL;
311  }
312 
313  const char *data = zbar_symbol_get_data (symbol);
314  if (NULL == data)
315  {
316  GNUNET_break (0);
317  zbar_processor_destroy (proc);
318  return NULL;
319  }
320 
321  LOG (_("Found %s: \"%s\"\n"),
322  zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
323  data);
324 
325  char *copy = GNUNET_strdup (data);
326 
327  zbar_processor_destroy (proc);
329 
330  return copy;
331 }
uint32_t data
The data value.
static const zbar_symbol_t * get_symbol(zbar_processor_t *proc)
Obtain a QR code symbol from proc.
Definition: gnunet-qr.c:235

References _, data, device, get_symbol(), GNUNET_break, GNUNET_free, GNUNET_strdup, LOG, and GNUNET_ChildWaitHandle::proc.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Main function executed by the scheduler.

Parameters
clsclosure
argsremaining command line arguments
cfgfilename of the configuration file being used
cfgthe used configuration

Definition at line 524 of file gnunet-qr.c.

528 {
529  char *data = NULL;
530 
532 
533 #if HAVE_PNG
534  if (NULL != pngfilename)
535  {
536  data = run_png_reader ();
537  }
538  else
539 #endif
540  {
541  data = run_zbar ();
542  }
543 
544  if (NULL == data)
545  {
546  LOG (_("No data found\n"));
547  exit_code = 1;
549  return;
550  }
551 
552  handle_uri (cls, data, cfgfile, cfg);
553 
554  if (0 != exit_code)
555  {
556  fprintf (stdout, _("Failed to add URI %s\n"), data);
557  GNUNET_free (data);
559  return;
560  }
561 
562  LOG (_("Dispatching the URI\n"));
563 }
static char * run_zbar(void)
Run the zbar QR code parser.
Definition: gnunet-qr.c:292
static void shutdown_program(void *cls)
Executed when program is terminating.
Definition: gnunet-qr.c:97
static void handle_uri(void *cls, const char *uri, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Dispatch URIs to the appropriate GNUnet helper process.
Definition: gnunet-qr.c:151
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1334

References _, cfg, data, exit_code, GNUNET_free, GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), handle_uri(), LOG, run_zbar(), and shutdown_program().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

Definition at line 566 of file gnunet-qr.c.

567 {
570  'd',
571  "device",
572  "DEVICE",
573  gettext_noop ("use the video device DEVICE (defaults to /dev/video0)"),
574  &device),
575 #if HAVE_PNG
577  'f',
578  "file",
579  "FILE",
580  gettext_noop ("read from the PNG-encoded file FILE"),
581  &pngfilename),
582 #endif
585  };
586 
588  GNUNET_PROGRAM_run (argc,
589  argv,
590  "gnunet-qr",
591  gettext_noop ("Scan a QR code and import the URI read"),
592  options,
593  &run,
594  NULL);
595 
596  return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
597 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:70
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function executed by the scheduler.
Definition: gnunet-qr.c:524
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the '-V' verbosity option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
GNUNET_GenericReturnValue
Named constants for return values.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
Definition of a command line option.

References device, exit_code, gettext_noop, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_string(), GNUNET_GETOPT_option_verbose(), GNUNET_OK, GNUNET_PROGRAM_run(), options, ret, run(), and verbosity.

Here is the call graph for this function:

Variable Documentation

◆ exit_code

int exit_code = 0
static

Global exit code.

Set to non-zero if an error occurs after the scheduler has started.

Definition at line 43 of file gnunet-qr.c.

Referenced by child_completed_callback(), handle_uri(), main(), maint_child_death(), run(), and wait_child().

◆ device

char* device = NULL
static

Video device to capture from.

Used by default if PNG support is disabled or no PNG file is specified. Defaults to /dev/video0.

Definition at line 50 of file gnunet-qr.c.

Referenced by get_symbol(), main(), and run_zbar().

◆ verbosity

unsigned int verbosity = 0
static

Requested verbosity.

Definition at line 64 of file gnunet-qr.c.

Referenced by main().

◆ childproc

struct GNUNET_OS_Process* childproc = NULL

Child process handle.

Definition at line 69 of file gnunet-qr.c.

Referenced by handle_uri(), shutdown_program(), and wait_child().

◆ waitchildproc

struct GNUNET_ChildWaitHandle* waitchildproc = NULL
static

Child process handle for waiting.

Definition at line 74 of file gnunet-qr.c.

Referenced by handle_uri(), shutdown_program(), and wait_child().