GNUnet  0.17.6
Macros | Functions | Variables
gnunet-qr.c File Reference
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <zbar.h>
#include "platform.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:63
@ GNUNET_ERROR_TYPE_INFO

Macro to handle verbosity when printing messages.

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

Function Documentation

◆ shutdown_program()

static void shutdown_program ( void *  cls)
static

Executed when program is terminating.

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

97 {
98  if (NULL != waitchildproc)
99  {
101  }
102  if (NULL != childproc)
103  {
104  /* A bit brutal, but this process is terminating so we're out of time */
106  }
107 }
struct GNUNET_OS_Process * childproc
Child process handle.
Definition: gnunet-qr.c:68
static struct GNUNET_ChildWaitHandle * waitchildproc
Child process handle for waiting.
Definition: gnunet-qr.c:73
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:217

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 117 of file gnunet-qr.c.

120 {
122  childproc = NULL;
123  waitchildproc = NULL;
124 
125  char *uri = cls;
126 
127  if (0 != exit_code)
128  {
129  fprintf (stdout, _("Failed to add URI %s\n"), uri);
130  }
131  else
132  {
133  fprintf (stdout, _("Added URI %s\n"), uri);
134  }
135 
136  GNUNET_free (uri);
137 
139 }
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:42
#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:280
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:533
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

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 150 of file gnunet-qr.c.

154 {
155  const char *cursor = uri;
156 
157  if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
158  {
159  fprintf (stderr,
160  _("Invalid URI: does not start with `gnunet://'\n"));
161  exit_code = 1;
162  return;
163  }
164 
165  cursor += strlen ("gnunet://");
166 
167  const char *slash = strchr (cursor, '/');
168  if (NULL == slash)
169  {
170  fprintf (stderr, _("Invalid URI: fails to specify a subsystem\n"));
171  exit_code = 1;
172  return;
173  }
174 
175  char *subsystem = GNUNET_strndup (cursor, slash - cursor);
176  char *program = NULL;
177 
178  if (GNUNET_OK !=
180  {
181  fprintf (stderr, _("No known handler for subsystem `%s'\n"), subsystem);
183  exit_code = 1;
184  return;
185  }
186 
188 
189  char **childargv = NULL;
190  unsigned int childargc = 0;
191 
192  for (const char *token=strtok (program, " ");
193  NULL!=token;
194  token=strtok(NULL, " "))
195  {
196  GNUNET_array_append (childargv, childargc, GNUNET_strdup (token));
197  }
198  GNUNET_array_append (childargv, childargc, GNUNET_strdup (uri));
199  GNUNET_array_append (childargv, childargc, NULL);
200 
202  NULL,
203  NULL,
204  NULL,
205  childargv[0],
206  childargv);
207  for (size_t i=0; i<childargc-1; ++i)
208  {
209  GNUNET_free (childargv[i]);
210  }
211 
212  GNUNET_array_grow (childargv, childargc, 0);
213 
214  if (NULL == childproc)
215  {
217  _("Unable to start child process `%s'\n"),
218  program);
219  GNUNET_free (program);
220  exit_code = 1;
221  return;
222  }
223 
225 }
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:117
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
Definition: gnunet_common.h:99
@ 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:598
@ 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 234 of file gnunet-qr.c.

235 {
236  if (0 != zbar_processor_parse_config (proc, "enable"))
237  {
238  GNUNET_break (0);
239  return NULL;
240  }
241 
242  int r = zbar_processor_init (proc, device, 1);
243  if (0 != r)
244  {
246  _("Failed to open device: `%s': %d\n"),
247  device,
248  r);
249  return NULL;
250  }
251 
252  r = zbar_processor_set_visible (proc, 1);
253  r += zbar_processor_set_active (proc, 1);
254  if (0 != r)
255  {
256  GNUNET_break (0);
257  return NULL;
258  }
259 
260  LOG (_("Capturing...\n"));
261 
262  int n = zbar_process_one (proc, -1);
263 
264  zbar_processor_set_active (proc, 0);
265  zbar_processor_set_visible (proc, 0);
266 
267  if (-1 == n)
268  {
269  LOG (_("No captured images\n"));
270  return NULL;
271  }
272 
273  LOG(_("Got %d images\n"), n);
274 
275  const zbar_symbol_set_t *symbols = zbar_processor_get_results (proc);
276  if (NULL == symbols)
277  {
278  GNUNET_break (0);
279  return NULL;
280  }
281 
282  return zbar_symbol_set_first_symbol (symbols);
283 }
#define LOG(fmt,...)
Macro to handle verbosity when printing messages.
Definition: gnunet-qr.c:78
static char * device
Video device to capture from.
Definition: gnunet-qr.c:49
#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 291 of file gnunet-qr.c.

292 {
293  zbar_processor_t *proc = zbar_processor_create (1);
294  if (NULL == proc)
295  {
296  GNUNET_break (0);
297  return NULL;
298  }
299 
300  if (NULL == device)
301  {
302  device = GNUNET_strdup ("/dev/video0");
303  }
304 
305  const zbar_symbol_t *symbol = get_symbol (proc);
306  if (NULL == symbol)
307  {
308  zbar_processor_destroy (proc);
309  return NULL;
310  }
311 
312  const char *data = zbar_symbol_get_data (symbol);
313  if (NULL == data)
314  {
315  GNUNET_break (0);
316  zbar_processor_destroy (proc);
317  return NULL;
318  }
319 
320  LOG (_("Found %s: \"%s\"\n"),
321  zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
322  data);
323 
324  char *copy = GNUNET_strdup (data);
325 
326  zbar_processor_destroy (proc);
328 
329  return copy;
330 }
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:234

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 523 of file gnunet-qr.c.

527 {
528  char *data = NULL;
529 
531 
532 #if HAVE_PNG
533  if (NULL != pngfilename)
534  {
535  data = run_png_reader ();
536  }
537  else
538 #endif
539  {
540  data = run_zbar ();
541  }
542 
543  if (NULL == data)
544  {
545  LOG (_("No data found\n"));
546  exit_code = 1;
548  return;
549  }
550 
551  handle_uri (cls, data, cfgfile, cfg);
552 
553  if (0 != exit_code)
554  {
555  fprintf (stdout, _("Failed to add URI %s\n"), data);
556  GNUNET_free (data);
558  return;
559  }
560 
561  LOG (_("Dispatching the URI\n"));
562 }
static char * run_zbar(void)
Run the zbar QR code parser.
Definition: gnunet-qr.c:291
static void shutdown_program(void *cls)
Executed when program is terminating.
Definition: gnunet-qr.c:96
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:150
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:1316

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 565 of file gnunet-qr.c.

566 {
569  'd',
570  "device",
571  "DEVICE",
572  gettext_noop ("use the video device DEVICE (defaults to /dev/video0)"),
573  &device),
574 #if HAVE_PNG
576  'f',
577  "file",
578  "FILE",
579  gettext_noop ("read from the PNG-encoded file FILE"),
580  &pngfilename),
581 #endif
584  };
585 
587  GNUNET_PROGRAM_run (argc,
588  argv,
589  "gnunet-qr",
590  gettext_noop ("Scan a QR code and import the URI read"),
591  options,
592  &run,
593  NULL);
594 
595  return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
596 }
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:69
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:523
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.
Definition: gnunet_common.h:96
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:399
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 42 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 49 of file gnunet-qr.c.

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

◆ verbosity

unsigned int verbosity = 0
static

Requested verbosity.

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

Referenced by main().

◆ childproc

struct GNUNET_OS_Process* childproc = NULL

Child process handle.

Definition at line 68 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 73 of file gnunet-qr.c.

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