GNUnet 0.22.2
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 119 of file gnunet-qr.c.

122{
123 char *uri = cls;
125 childproc = NULL;
126 waitchildproc = NULL;
127
128
129 if (0 != exit_code)
130 {
131 fprintf (stdout, _ ("Failed to add URI %s\n"), uri);
132 }
133 else
134 {
135 fprintf (stdout, _ ("Added URI %s\n"), uri);
136 }
137
139
141}
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:567
#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 153 of file gnunet-qr.c.

157{
158 const char *cursor = uri;
159 const char *slash;
160 char *subsystem;
161 char *program = NULL;
162
163 if (0 != strncasecmp ("gnunet://", uri, strlen ("gnunet://")))
164 {
165 fprintf (stderr,
166 _ ("Invalid URI: does not start with `gnunet://'\n"));
167 exit_code = 1;
168 return;
169 }
170
171 cursor += strlen ("gnunet://");
172
173 slash = strchr (cursor, '/');
174 if (NULL == slash)
175 {
176 fprintf (stderr, _ ("Invalid URI: fails to specify a subsystem\n"));
177 exit_code = 1;
178 return;
179 }
180
181 subsystem = GNUNET_strndup (cursor, slash - cursor);
182 program = NULL;
183
184 if (GNUNET_OK !=
186 {
187 fprintf (stderr, _ ("No known handler for subsystem `%s'\n"), subsystem);
189 exit_code = 1;
190 return;
191 }
192
194
195 {
196 char **childargv = NULL;
197 unsigned int childargc = 0;
198
199 for (const char *token = strtok (program, " ");
200 NULL!=token;
201 token = strtok (NULL, " "))
202 {
203 GNUNET_array_append (childargv, childargc, GNUNET_strdup (token));
204 }
205 GNUNET_array_append (childargv, childargc, GNUNET_strdup (uri));
206 GNUNET_array_append (childargv, childargc, NULL);
207
209 NULL,
210 NULL,
211 NULL,
212 childargv[0],
213 childargv);
214 for (size_t i = 0; i<childargc - 1; ++i)
215 {
216 GNUNET_free (childargv[i]);
217 }
218
219 GNUNET_array_grow (childargv, childargc, 0);
220
221 if (NULL == childproc)
222 {
224 _ ("Unable to start child process `%s'\n"),
225 program);
226 GNUNET_free (program);
227 exit_code = 1;
228 return;
229 }
230
232 }
233}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
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:119
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 243 of file gnunet-qr.c.

244{
245 const zbar_symbol_set_t *symbols;
246 int r, n;
247
248 if (0 != zbar_processor_parse_config (proc, "enable"))
249 {
250 GNUNET_break (0);
251 return NULL;
252 }
253
254 r = zbar_processor_init (proc, device, 1);
255 if (0 != r)
256 {
258 _ ("Failed to open device: `%s': %d\n"),
259 device,
260 r);
261 return NULL;
262 }
263
264 r = zbar_processor_set_visible (proc, 1);
265 r += zbar_processor_set_active (proc, 1);
266 if (0 != r)
267 {
268 GNUNET_break (0);
269 return NULL;
270 }
271
272 LOG (_ ("Capturing...\n"));
273
274 n = zbar_process_one (proc, -1);
275
276 zbar_processor_set_active (proc, 0);
277 zbar_processor_set_visible (proc, 0);
278
279 if (-1 == n)
280 {
281 LOG (_ ("No captured images\n"));
282 return NULL;
283 }
284
285 LOG (_ ("Got %d images\n"), n);
286
287 symbols = zbar_processor_get_results (proc);
288 if (NULL == symbols)
289 {
290 GNUNET_break (0);
291 return NULL;
292 }
293
294 return zbar_symbol_set_first_symbol (symbols);
295}
#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 304 of file gnunet-qr.c.

305{
306 zbar_processor_t *proc = zbar_processor_create (1);
307 const zbar_symbol_t *symbol;
308 const char *data;
309 char *copy;
310
311 if (NULL == proc)
312 {
313 GNUNET_break (0);
314 return NULL;
315 }
316
317 if (NULL == device)
318 {
319 device = GNUNET_strdup ("/dev/video0");
320 }
321
322 symbol = get_symbol (proc);
323 if (NULL == symbol)
324 {
325 zbar_processor_destroy (proc);
326 return NULL;
327 }
328
329 data = zbar_symbol_get_data (symbol);
330 if (NULL == data)
331 {
332 GNUNET_break (0);
333 zbar_processor_destroy (proc);
334 return NULL;
335 }
336
337 LOG (_ ("Found %s: \"%s\"\n"),
338 zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
339 data);
340
341 copy = GNUNET_strdup (data);
342
343 zbar_processor_destroy (proc);
345
346 return copy;
347}
static char * data
The data to insert into the dht.
static const zbar_symbol_t * get_symbol(zbar_processor_t *proc)
Obtain a QR code symbol from proc.
Definition: gnunet-qr.c:243

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

548{
549 char *data = NULL;
550
552
553#if HAVE_PNG
554 if (NULL != pngfilename)
555 {
556 data = run_png_reader ();
557 }
558 else
559#endif
560 {
561 data = run_zbar ();
562 }
563
564 if (NULL == data)
565 {
566 LOG (_ ("No data found\n"));
567 exit_code = 1;
569 return;
570 }
571
572 handle_uri (cls, data, cfgfile, cfg);
573
574 if (0 != exit_code)
575 {
576 fprintf (stdout, _ ("Failed to add URI %s\n"), data);
579 return;
580 }
581
582 LOG (_ ("Dispatching the URI\n"));
583}
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:153
static char * run_zbar(void)
Run the zbar QR code parser.
Definition: gnunet-qr.c:304
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:1339

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

588{
591 'd',
592 "device",
593 "DEVICE",
594 gettext_noop ("use the video device DEVICE (defaults to /dev/video0)"),
595 &device),
596#if HAVE_PNG
598 'f',
599 "file",
600 "FILE",
601 gettext_noop ("read from the PNG-encoded file FILE"),
602 &pngfilename),
603#endif
606 };
607
610 argc,
611 argv,
612 "gnunet-qr",
613 gettext_noop ("Scan a QR code and import the URI read"),
614 options,
615 &run,
616 NULL);
617
618 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
619}
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:74
static int ret
Final status code.
Definition: gnunet-arm.c:93
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:544
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.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, 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:407
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_OS_project_data_gnunet(), 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().