GNUnet 0.26.2-32-gd298f7855
 
Loading...
Searching...
No Matches
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.
 

Functions

static void shutdown_program (void *cls)
 Executed when program is terminating.
 
static void wait_child (void *cls, enum GNUNET_OS_ProcessStatusType type, long unsigned int code)
 Callback executed when the child process terminates.
 
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.
 
static const zbar_symbol_t * get_symbol (zbar_processor_t *proc)
 Obtain a QR code symbol from proc.
 
static char * run_zbar (void)
 Run the zbar QR code parser.
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Main function executed by the scheduler.
 
int main (int argc, char *const *argv)
 

Variables

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

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.

81 { \
82 if (0 < verbosity) \
83 { \
84 GNUNET_log (GNUNET_ERROR_TYPE_INFO, fmt, ## __VA_ARGS__); \
85 if (verbosity > 1) \
86 { \
87 fprintf (stdout, fmt, ## __VA_ARGS__); \
88 } \
89 } \
90 } \
91 while (0)

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 */
108 SIGKILL));
109 }
110}
struct GNUNET_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.
@ GNUNET_OK
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
enum GNUNET_GenericReturnValue GNUNET_process_kill(struct GNUNET_Process *proc, int sig)
Sends a signal to the process.
Definition os_process.c:302

References childproc, GNUNET_break, GNUNET_OK, GNUNET_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 121 of file gnunet-qr.c.

124{
125 char *uri = cls;
126
128 childproc = NULL;
129 waitchildproc = NULL;
130 if (0 != exit_code)
131 {
132 fprintf (stdout,
133 _ ("Failed to add URI %s\n"),
134 uri);
135 }
136 else
137 {
138 fprintf (stdout,
139 _ ("Added URI %s\n"),
140 uri);
141 }
144}
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_process_destroy(struct GNUNET_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition os_process.c:350
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:572
#define _(String)
GNU gettext support macro.
Definition platform.h:179

References _, childproc, exit_code, GNUNET_free, GNUNET_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 156 of file gnunet-qr.c.

160{
161 const char *cursor = uri;
162 const char *slash;
163 char *program;
164
165 if (0 != strncasecmp ("gnunet://",
166 uri,
167 strlen ("gnunet://")))
168 {
169 fprintf (stderr,
170 _ ("Invalid URI: does not start with `gnunet://'\n"));
171 exit_code = 1;
172 return;
173 }
174
175 cursor += strlen ("gnunet://");
176
177 slash = strchr (cursor,
178 '/');
179 if (NULL == slash)
180 {
181 fprintf (stderr,
182 _ ("Invalid URI: fails to specify a subsystem\n"));
183 exit_code = 1;
184 return;
185 }
186
187 {
188 char *subsystem;
189
190 subsystem = GNUNET_strndup (cursor,
191 slash - cursor);
192 if (GNUNET_OK !=
194 "uri",
195 subsystem,
196 &program))
197 {
198 fprintf (stderr,
199 _ ("No known handler for subsystem `%s'\n"),
200 subsystem);
202 exit_code = 1;
203 return;
204 }
206 }
207
208 {
209 char *fullcmd;
210
211 GNUNET_asprintf (&fullcmd,
212 "%s -- %s",
213 program,
214 uri);
218 childproc,
221 if ( (GNUNET_OK !=
223 fullcmd)) ||
224 (GNUNET_OK !=
226 {
228 _ ("Unable to start child process `%s'\n"),
229 program);
230 }
231 GNUNET_free (fullcmd);
232 }
233 GNUNET_free (program);
234 if (NULL == childproc)
235 {
236 exit_code = 1;
237 return;
238 }
240 &wait_child,
241 (void *) uri);
242}
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:121
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_Process *proc, GNUNET_ChildCompletedCallback cb, void *cb_cls)
Starts the handling of the child processes.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
struct GNUNET_Process * GNUNET_process_create(void)
Create a process handle.
Definition os_process.c:446
enum GNUNET_GenericReturnValue GNUNET_process_set_command(struct GNUNET_Process *p, const char *command)
Set the command to start a process.
Definition os_process.c:925
#define GNUNET_process_option_std_inheritance(flags)
Set flags about standard inheritance options.
enum GNUNET_GenericReturnValue GNUNET_process_start(struct GNUNET_Process *proc)
Start a process.
Definition os_process.c:563
#define GNUNET_process_set_options(proc,...)
Set the requested options for the process.
@ 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_asprintf(), GNUNET_assert, GNUNET_CONFIGURATION_get_value_string(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_OS_INHERIT_STD_ALL, GNUNET_process_create(), GNUNET_process_option_std_inheritance, GNUNET_process_set_command(), GNUNET_process_set_options, GNUNET_process_start(), 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 252 of file gnunet-qr.c.

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

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

314{
315 zbar_processor_t *proc = zbar_processor_create (1);
316 const zbar_symbol_t *symbol;
317 const char *data;
318 char *copy;
319
320 if (NULL == proc)
321 {
322 GNUNET_break (0);
323 return NULL;
324 }
325
326 if (NULL == device)
327 {
328 device = GNUNET_strdup ("/dev/video0");
329 }
330
331 symbol = get_symbol (proc);
332 if (NULL == symbol)
333 {
334 zbar_processor_destroy (proc);
335 return NULL;
336 }
337
338 data = zbar_symbol_get_data (symbol);
339 if (NULL == data)
340 {
341 GNUNET_break (0);
342 zbar_processor_destroy (proc);
343 return NULL;
344 }
345
346 LOG (_ ("Found %s: \"%s\"\n"),
347 zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
348 data);
349
350 copy = GNUNET_strdup (data);
351
352 zbar_processor_destroy (proc);
354
355 return copy;
356}
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:252
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.

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

557{
558 char *data = NULL;
559
561
562#if HAVE_PNG
563 if (NULL != pngfilename)
564 {
565 data = run_png_reader ();
566 }
567 else
568#endif
569 {
570 data = run_zbar ();
571 }
572
573 if (NULL == data)
574 {
575 LOG (_ ("No data found\n"));
576 exit_code = 1;
578 return;
579 }
580
581 handle_uri (cls, data, cfgfile, cfg);
582
583 if (0 != exit_code)
584 {
585 fprintf (stdout, _ ("Failed to add URI %s\n"), data);
588 return;
589 }
590
591 LOG (_ ("Dispatching the URI\n"));
592}
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:156
static char * run_zbar(void)
Run the zbar QR code parser.
Definition gnunet-qr.c:313
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:1345

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

597{
600 'd',
601 "device",
602 "DEVICE",
603 gettext_noop ("use the video device DEVICE (defaults to /dev/video0)"),
604 &device),
605#if HAVE_PNG
607 'f',
608 "file",
609 "FILE",
610 gettext_noop ("read from the PNG-encoded file FILE"),
611 &pngfilename),
612#endif
615 };
616
619 argc,
620 argv,
621 "gnunet-qr",
622 gettext_noop ("Scan a QR code and import the URI read"),
623 options,
624 &run,
625 NULL);
626
627 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
628}
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:553
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
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
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(), child_completed_callback(), handle_uri(), main(), maint_child_death(), run(), and wait_child().

◆ device

char* device
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
static

Requested verbosity.

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

Referenced by main().

◆ childproc

struct GNUNET_Process* childproc

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
static

Child process handle for waiting.

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

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