GNUnet 0.26.2-106-g126384b46
 
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:307

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:363
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);
216 if (GNUNET_OK !=
218 fullcmd))
219 {
221 _ ("Unable to start child process `%s'\n"),
222 program);
223 }
224 GNUNET_free (fullcmd);
225 }
226 GNUNET_free (program);
227 if (NULL == childproc)
228 {
229 exit_code = 1;
230 return;
231 }
233 &wait_child,
234 (void *) uri);
235}
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.
@ 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_.
enum GNUNET_GenericReturnValue GNUNET_process_run_command(struct GNUNET_Process *p, const char *command)
Set the command and start a process.
Definition os_process.c:921
struct GNUNET_Process * GNUNET_process_create(enum GNUNET_OS_InheritStdioFlags std_inheritance)
Create a process handle.
Definition os_process.c:462
@ 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_CONFIGURATION_get_value_string(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log, GNUNET_OK, GNUNET_OS_INHERIT_STD_ALL, GNUNET_process_create(), GNUNET_process_run_command(), 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 245 of file gnunet-qr.c.

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

307{
308 zbar_processor_t *proc = zbar_processor_create (1);
309 const zbar_symbol_t *symbol;
310 const char *data;
311 char *copy;
312
313 if (NULL == proc)
314 {
315 GNUNET_break (0);
316 return NULL;
317 }
318
319 if (NULL == device)
320 {
321 device = GNUNET_strdup ("/dev/video0");
322 }
323
324 symbol = get_symbol (proc);
325 if (NULL == symbol)
326 {
327 zbar_processor_destroy (proc);
328 return NULL;
329 }
330
331 data = zbar_symbol_get_data (symbol);
332 if (NULL == data)
333 {
334 GNUNET_break (0);
335 zbar_processor_destroy (proc);
336 return NULL;
337 }
338
339 LOG (_ ("Found %s: \"%s\"\n"),
340 zbar_get_symbol_name (zbar_symbol_get_type (symbol)),
341 data);
342
343 copy = GNUNET_strdup (data);
344
345 zbar_processor_destroy (proc);
347
348 return copy;
349}
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:245
#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 546 of file gnunet-qr.c.

550{
551 char *data = NULL;
552
554
555#if HAVE_PNG
556 if (NULL != pngfilename)
557 {
558 data = run_png_reader ();
559 }
560 else
561#endif
562 {
563 data = run_zbar ();
564 }
565
566 if (NULL == data)
567 {
568 LOG (_ ("No data found\n"));
569 exit_code = 1;
571 return;
572 }
573
574 handle_uri (cls, data, cfgfile, cfg);
575
576 if (0 != exit_code)
577 {
578 fprintf (stdout, _ ("Failed to add URI %s\n"), data);
581 return;
582 }
583
584 LOG (_ ("Dispatching the URI\n"));
585}
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:306
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 589 of file gnunet-qr.c.

590{
593 'd',
594 "device",
595 "DEVICE",
596 gettext_noop ("use the video device DEVICE (defaults to /dev/video0)"),
597 &device),
598#if HAVE_PNG
600 'f',
601 "file",
602 "FILE",
603 gettext_noop ("read from the PNG-encoded file FILE"),
604 &pngfilename),
605#endif
608 };
609
612 argc,
613 argv,
614 "gnunet-qr",
615 gettext_noop ("Scan a QR code and import the URI read"),
616 options,
617 &run,
618 NULL);
619
620 return ((GNUNET_OK == ret) && (0 == exit_code)) ? 0 : 1;
621}
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:546
#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().