Add simple dbus IPC API to zbarcam.
Subsystems (2)
PATCH 1/1 Add simple dbus IPC API to zbarcam.
From: James Hilliard <[email protected]>
This is useful for running zbarcam as a systemd service so that other applications can receive scan messages through dbus.
--- Makefile.am | 6 ++++ configure.ac | 30 ++++++++++++++++++++ dbus/org.linuxtv.Zbar.conf | 19 +++++++++++++ zbarcam/Makefile.am.inc | 4 +++ zbarcam/zbarcam.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 dbus/org.linuxtv.Zbar.conf
Makefile.am +6 -0
@@ -50,6 +50,12 @@ if HAVE_DOC include $(srcdir)/doc/Makefile.am.inc endif +if HAVE_DBUS +dbusconfdir = @DBUS_CONFDIR@ +dbusconf_DATA = $(srcdir)/dbus/org.linuxtv.Zbar.conf +EXTRA_DIST += $(dbusconf_DATA) +endif + EXTRA_DIST += zbar.ico zbar.nsi EXTRA_DIST += examples/barcode.png examples/upcrpc.py examples/upcrpc.pl \
configure.ac +30 -0
@@ -291,6 +291,35 @@ specify XV_LIBS or configure --without-xv to disable the extension])], ]) AM_CONDITIONAL([HAVE_XV], [test "x$with_xv" = "xyes"]) +dnl dbus +AC_ARG_WITH([dbus], + [AS_HELP_STRING([--without-dbus], + [disable support for dbus])], + [], + [with_dbus="check"]) + +AS_IF([test "x$with_dbus" != "xno"], + [PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.0, have_dbus=yes, have_dbus=no) + AS_IF([test "x$have_dbusx$with_dbus" = "xnoxyes"], + [AC_MSG_FAILURE([DBus development libraries not found])], + [with_dbus="$have_dbus"]) +]) +AM_CONDITIONAL([HAVE_DBUS], [test "x$with_dbus" = "xyes"]) + +AS_IF([test "x$with_dbus" = "xyes"], + [CPPFLAGS="$CPPFLAGS $DBUS_CFLAGS" + AC_ARG_VAR([DBUS_LIBS], [linker flags for building dbus]) + AC_DEFINE([HAVE_DBUS], [1], [Define to 1 to use dbus]) + AC_ARG_WITH(dbusconfdir, AC_HELP_STRING([--with-dbusconfdir=PATH], + [path to D-Bus config directory]), + [path_dbusconf=$withval], + [path_dbusconf="`$PKG_CONFIG --variable=sysconfdir dbus-1`"]) + AS_IF([test -z "$path_dbusconf"], + DBUS_CONFDIR="$sysconfdir/dbus-1/system.d", + DBUS_CONFDIR="$path_dbusconf/dbus-1/system.d") + AC_SUBST(DBUS_CONFDIR) +]) + dnl libjpeg AC_ARG_WITH([jpeg], [AS_HELP_STRING([--without-jpeg],
@@ -617,6 +646,7 @@ AS_IF([test "x$enable_video" != "xyes"], [echo " => zbarcam video scanner will *NOT* be built"]) AS_IF([test "x$have_libv4l" != "xyes"], [echo " => libv4l will *NOT* be used"]) +echo "dbus --with-dbus=$with_dbus" echo "jpeg --with-jpeg=$with_jpeg" AS_IF([test "x$with_jpeg" != "xyes"], [echo " => JPEG image conversions will *NOT* be supported"])
dbus/org.linuxtv.Zbar.conf new file +19 -0
@@ -0,0 +1,19 @@ +<!DOCTYPE busconfig PUBLIC + "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" + "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> +<busconfig> + + <policy user="root"> + <allow own="org.linuxtv.Zbar"/> + </policy> + + <policy context="default"> + <allow send_destination="org.linuxtv.Zbar"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.linuxtv.ZbarCam1"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.freedesktop.DBus.Properties"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.freedesktop.DBus.Introspectable"/> + </policy> +</busconfig>
zbarcam/Makefile.am.inc +4 -0
@@ -4,6 +4,10 @@ zbarcam_zbarcam_LDADD = zbar/libzbar.la # automake bug in "monolithic mode"? CLEANFILES += zbarcam/.libs/zbarcam zbarcam/moc_zbarcam_qt.h +if HAVE_DBUS +zbarcam_zbarcam_LDADD += $(DBUS_LIBS) +endif + if HAVE_GTK bin_PROGRAMS += zbarcam/zbarcam-gtk check_PROGRAMS += zbarcam/zbarcam-gtk
zbarcam/zbarcam.c +70 -0
88 lines not on this page · raw message
Signature · 1 line
2.7.4
Hi James,
Em Wed, 16 Jan 2019 16:54:55 +0800
[email protected] escreveu:
Quoted 4 lines
From: James Hilliard <[email protected]>
This is useful for running zbarcam as a systemd service so that other applications can receive scan messages through dbus.
Nice approach!
Yet, I would try to write it on a different way, making sure that it could also be using by zbarimg.
I mean, if you add the dbus bindings inside the zbar core, it shouldn't matter if the source image comes via a webcam or via a scanned image. Both will be able to send the scancodes via dbus.
As a future approach, we may even think on making the interface duplex in the future, e. g. allowing any camera application to send an image via dbus and let zbar to decode it and return the decoded bar codes.
Quoted 201 lines
Signed-off-by James Hilliard <[email protected]>--- Makefile.am | 6 ++++ configure.ac | 30 ++++++++++++++++++++ dbus/org.linuxtv.Zbar.conf | 19 +++++++++++++ zbarcam/Makefile.am.inc | 4 +++ zbarcam/zbarcam.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 dbus/org.linuxtv.Zbar.confMakefile.am +6
-0@@ -50,6 +50,12 @@ if HAVE_DOC include $(srcdir)/doc/Makefile.am.inc endif +if HAVE_DBUS +dbusconfdir = @DBUS_CONFDIR@ +dbusconf_DATA = $(srcdir)/dbus/org.linuxtv.Zbar.conf +EXTRA_DIST += $(dbusconf_DATA) +endif + EXTRA_DIST += zbar.ico zbar.nsi EXTRA_DIST += examples/barcode.png examples/upcrpc.py examples/upcrpc.pl \configure.ac +30
-0@@ -291,6 +291,35 @@ specify XV_LIBS or configure --without-xv to disable the extension])], ]) AM_CONDITIONAL([HAVE_XV], [test "x$with_xv" = "xyes"]) +dnl dbus +AC_ARG_WITH([dbus], + [AS_HELP_STRING([--without-dbus], + [disable support for dbus])], + [], + [with_dbus="check"]) + +AS_IF([test "x$with_dbus" != "xno"], + [PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.0, have_dbus=yes, have_dbus=no) + AS_IF([test "x$have_dbusx$with_dbus" = "xnoxyes"], + [AC_MSG_FAILURE([DBus development libraries not found])], + [with_dbus="$have_dbus"]) +]) +AM_CONDITIONAL([HAVE_DBUS], [test "x$with_dbus" = "xyes"]) + +AS_IF([test "x$with_dbus" = "xyes"], + [CPPFLAGS="$CPPFLAGS $DBUS_CFLAGS" + AC_ARG_VAR([DBUS_LIBS], [linker flags for building dbus]) + AC_DEFINE([HAVE_DBUS], [1], [Define to 1 to use dbus]) + AC_ARG_WITH(dbusconfdir, AC_HELP_STRING([--with-dbusconfdir=PATH], + [path to D-Bus config directory]), + [path_dbusconf=$withval], + [path_dbusconf="`$PKG_CONFIG --variable=sysconfdir dbus-1`"]) + AS_IF([test -z "$path_dbusconf"], + DBUS_CONFDIR="$sysconfdir/dbus-1/system.d", + DBUS_CONFDIR="$path_dbusconf/dbus-1/system.d") + AC_SUBST(DBUS_CONFDIR) +]) + dnl libjpeg AC_ARG_WITH([jpeg], [AS_HELP_STRING([--without-jpeg],@@ -617,6 +646,7 @@ AS_IF([test "x$enable_video" != "xyes"], [echo " => zbarcam video scanner will *NOT* be built"]) AS_IF([test "x$have_libv4l" != "xyes"], [echo " => libv4l will *NOT* be used"]) +echo "dbus --with-dbus=$with_dbus" echo "jpeg --with-jpeg=$with_jpeg" AS_IF([test "x$with_jpeg" != "xyes"], [echo " => JPEG image conversions will *NOT* be supported"])dbus/org.linuxtv.Zbar.conf new file +19
-0@@ -0,0 +1,19 @@ +<!DOCTYPE busconfig PUBLIC + "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" + "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> +<busconfig> + + <policy user="root"> + <allow own="org.linuxtv.Zbar"/> + </policy> + + <policy context="default"> + <allow send_destination="org.linuxtv.Zbar"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.linuxtv.ZbarCam1"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.freedesktop.DBus.Properties"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.freedesktop.DBus.Introspectable"/> + </policy> +</busconfig>zbarcam/Makefile.am.inc +4
-0@@ -4,6 +4,10 @@ zbarcam_zbarcam_LDADD = zbar/libzbar.la # automake bug in "monolithic mode"? CLEANFILES += zbarcam/.libs/zbarcam zbarcam/moc_zbarcam_qt.h +if HAVE_DBUS +zbarcam_zbarcam_LDADD += $(DBUS_LIBS) +endif + if HAVE_GTK bin_PROGRAMS += zbarcam/zbarcam-gtk check_PROGRAMS += zbarcam/zbarcam-gtkzbarcam/zbarcam.c +66
-0@@ -33,6 +33,10 @@#include <zbar.h> +#ifdef HAVE_DBUS +#include <dbus/dbus.h> +#endif +#define BELL "\a" static const char *note_usage =@@ -92,6 +96,68 @@ static inline int parse_config (const char *cfgstr, int i, int n, char *arg)return (0 ); } +#ifdef HAVE_DBUS +void send_dbus (const char * sigvalue) +{ +DBusMessage * msg; +DBusMessageIter args; +DBusConnection * conn; +DBusError err; +int ret; +dbus_uint32_t serial =0 ; + +// initialise the error value +dbus_error_init (&err); + +// connect to the DBUS system bus, and check for errors + conn =dbus_bus_get (DBUS_BUS_SYSTEM , &err); +if (dbus_error_is_set (&err)) { +fprintf (stderr,"Connection Error (%s)\n" , err.message); +dbus_error_free (&err); + } +if (NULL == conn) { +exit (1 ); + } + +// register our name on the bus, and check for errors + ret =dbus_bus_request_name (conn,"org.linuxtv.Zbar" ,DBUS_NAME_FLAG_REPLACE_EXISTING , &err); +if (dbus_error_is_set (&err)) { +fprintf (stderr,"Name Error (%s)\n" , err.message); +dbus_error_free (&err); + } +if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { +exit (1 ); + } + +// create a signal & check for errors + msg =dbus_message_new_signal ("/org/linuxtv/ZbarCam1" ,// object name of the signal +"org.linuxtv.ZbarCam1" ,// interface name of the signal +"ZbarCam1" );// name of the signal +if (NULL == msg) + { +fprintf (stderr,"Message Null\n" ); +exit (1 ); + } + +// append arguments onto signal +dbus_message_iter_init_append (msg, &args); +if (!dbus_message_iter_append_basic (&args,DBUS_TYPE_STRING , &sigvalue)) { +fprintf (stderr,"Out Of Memory!\n" ); +exit (1 ); + } + +// send the message and flush the connection +if (!dbus_connection_send (conn, msg, &serial)) { +fprintf (stderr,"Out Of Memory!\n" ); +exit (1 ); + } +dbus_connection_flush (conn); + +// free the message +dbus_message_unref (msg); +} +#endif +static void data_handler (zbar_image_t *img,const void *userdata) {const zbar_symbol_t *sym =zbar_image_first_symbol (img);
zbarcam/zbarcam.c static void data_handler (zbar_image_t *img, const void *userdata) · line 180
Quoted 10 lines
zbarcam/zbarcam.c +4
-0@@ -105,6 +171,10 @@ static void data_handler (zbar_image_t *img, const void *userdata)if (type ==ZBAR_PARTIAL )continue ; +#ifdef HAVE_DBUS +send_dbus (zbar_symbol_get_data (sym)); +#endif +if (!format) {printf ("%s:" ,zbar_get_symbol_name (type)); if(fwrite(zbar_symbol_get_data(sym),
Thanks,
Mauro
Quoted 11 lines · Mauro Carvalho Chehab
Hi James,
Em Wed, 16 Jan 2019 16:54:55 +0800
[email protected] escreveu:Quoted 4 lines
From: James Hilliard <[email protected]>
This is useful for running zbarcam as a systemd service so that other applications can receive scan messages through dbus.
Quoted 8 lines
Nice approach!
Yet, I would try to write it on a different way, making sure that it could also be using by zbarimg.
I mean, if you add the dbus bindings inside the zbar core, it shouldn't matter if the source image comes via a webcam or via a scanned image. Both will be able to send the scancodes via dbus.
Which function should I call send_dbus() from in that case?
Quoted 5 lines
As a future approach, we may even think on making the interface duplex in the future, e. g. allowing any camera application to send an image via dbus and let zbar to decode it and return the decoded bar codes.
That could be useful, probably too difficult for me to implement myself though(I don't write a lot of c usually).
Quoted 217 lines
Quoted 202 lines
Signed-off-by James Hilliard <[email protected]>--- Makefile.am | 6 ++++ configure.ac | 30 ++++++++++++++++++++ dbus/org.linuxtv.Zbar.conf | 19 +++++++++++++ zbarcam/Makefile.am.inc | 4 +++ zbarcam/zbarcam.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 dbus/org.linuxtv.Zbar.confMakefile.am +6
-0@@ -50,6 +50,12 @@ if HAVE_DOC include $(srcdir)/doc/Makefile.am.inc endif +if HAVE_DBUS +dbusconfdir = @DBUS_CONFDIR@ +dbusconf_DATA = $(srcdir)/dbus/org.linuxtv.Zbar.conf +EXTRA_DIST += $(dbusconf_DATA) +endif + EXTRA_DIST += zbar.ico zbar.nsi EXTRA_DIST += examples/barcode.png examples/upcrpc.py examples/upcrpc.pl \configure.ac +30
-0@@ -291,6 +291,35 @@ specify XV_LIBS or configure --without-xv to disable the extension])], ]) AM_CONDITIONAL([HAVE_XV], [test "x$with_xv" = "xyes"]) +dnl dbus +AC_ARG_WITH([dbus], + [AS_HELP_STRING([--without-dbus], + [disable support for dbus])], + [], + [with_dbus="check"]) + +AS_IF([test "x$with_dbus" != "xno"], + [PKG_CHECK_MODULES(DBUS, dbus-1 >= 1.0, have_dbus=yes, have_dbus=no) + AS_IF([test "x$have_dbusx$with_dbus" = "xnoxyes"], + [AC_MSG_FAILURE([DBus development libraries not found])], + [with_dbus="$have_dbus"]) +]) +AM_CONDITIONAL([HAVE_DBUS], [test "x$with_dbus" = "xyes"]) + +AS_IF([test "x$with_dbus" = "xyes"], + [CPPFLAGS="$CPPFLAGS $DBUS_CFLAGS" + AC_ARG_VAR([DBUS_LIBS], [linker flags for building dbus]) + AC_DEFINE([HAVE_DBUS], [1], [Define to 1 to use dbus]) + AC_ARG_WITH(dbusconfdir, AC_HELP_STRING([--with-dbusconfdir=PATH], + [path to D-Bus config directory]), + [path_dbusconf=$withval], + [path_dbusconf="`$PKG_CONFIG --variable=sysconfdir dbus-1`"]) + AS_IF([test -z "$path_dbusconf"], + DBUS_CONFDIR="$sysconfdir/dbus-1/system.d", + DBUS_CONFDIR="$path_dbusconf/dbus-1/system.d") + AC_SUBST(DBUS_CONFDIR) +]) + dnl libjpeg AC_ARG_WITH([jpeg], [AS_HELP_STRING([--without-jpeg],@@ -617,6 +646,7 @@ AS_IF([test "x$enable_video" != "xyes"], [echo " => zbarcam video scanner will *NOT* be built"]) AS_IF([test "x$have_libv4l" != "xyes"], [echo " => libv4l will *NOT* be used"]) +echo "dbus --with-dbus=$with_dbus" echo "jpeg --with-jpeg=$with_jpeg" AS_IF([test "x$with_jpeg" != "xyes"], [echo " => JPEG image conversions will *NOT* be supported"])dbus/org.linuxtv.Zbar.conf new file +19
-0@@ -0,0 +1,19 @@ +<!DOCTYPE busconfig PUBLIC + "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" + "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> +<busconfig> + + <policy user="root"> + <allow own="org.linuxtv.Zbar"/> + </policy> + + <policy context="default"> + <allow send_destination="org.linuxtv.Zbar"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.linuxtv.ZbarCam1"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.freedesktop.DBus.Properties"/> + <allow send_destination="org.linuxtv.Zbar" + send_interface="org.freedesktop.DBus.Introspectable"/> + </policy> +</busconfig>zbarcam/Makefile.am.inc +4
-0@@ -4,6 +4,10 @@ zbarcam_zbarcam_LDADD = zbar/libzbar.la # automake bug in "monolithic mode"? CLEANFILES += zbarcam/.libs/zbarcam zbarcam/moc_zbarcam_qt.h +if HAVE_DBUS +zbarcam_zbarcam_LDADD += $(DBUS_LIBS) +endif + if HAVE_GTK bin_PROGRAMS += zbarcam/zbarcam-gtk check_PROGRAMS += zbarcam/zbarcam-gtkzbarcam/zbarcam.c +66
-0@@ -33,6 +33,10 @@#include <zbar.h> +#ifdef HAVE_DBUS +#include <dbus/dbus.h> +#endif +#define BELL "\a" static const char *note_usage =@@ -92,6 +96,68 @@ static inline int parse_config (const char *cfgstr, int i, int n, char *arg)return (0 ); } +#ifdef HAVE_DBUS +void send_dbus (const char * sigvalue) +{ +DBusMessage * msg; +DBusMessageIter args; +DBusConnection * conn; +DBusError err; +int ret; +dbus_uint32_t serial =0 ; + +// initialise the error value +dbus_error_init (&err); + +// connect to the DBUS system bus, and check for errors + conn =dbus_bus_get (DBUS_BUS_SYSTEM , &err); +if (dbus_error_is_set (&err)) { +fprintf (stderr,"Connection Error (%s)\n" , err.message); +dbus_error_free (&err); + } +if (NULL == conn) { +exit (1 ); + } + +// register our name on the bus, and check for errors + ret =dbus_bus_request_name (conn,"org.linuxtv.Zbar" ,DBUS_NAME_FLAG_REPLACE_EXISTING , &err); +if (dbus_error_is_set (&err)) { +fprintf (stderr,"Name Error (%s)\n" , err.message); +dbus_error_free (&err); + } +if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { +exit (1 ); + } + +// create a signal & check for errors + msg =dbus_message_new_signal ("/org/linuxtv/ZbarCam1" ,// object name of the signal +"org.linuxtv.ZbarCam1" ,// interface name of the signal +"ZbarCam1" );// name of the signal +if (NULL == msg) + { +fprintf (stderr,"Message Null\n" ); +exit (1 ); + } + +// append arguments onto signal +dbus_message_iter_init_append (msg, &args); +if (!dbus_message_iter_append_basic (&args,DBUS_TYPE_STRING , &sigvalue)) { +fprintf (stderr,"Out Of Memory!\n" ); +exit (1 ); + } + +// send the message and flush the connection +if (!dbus_connection_send (conn, msg, &serial)) { +fprintf (stderr,"Out Of Memory!\n" ); +exit (1 ); + } +dbus_connection_flush (conn); + +// free the message +dbus_message_unref (msg); +} +#endif +static void data_handler (zbar_image_t *img,const void *userdata) {const zbar_symbol_t *sym =zbar_image_first_symbol (img);zbarcam/zbarcam.c static void data_handler (zbar_image_t *img, const void *userdata) · line 180
Quoted 10 lines
zbarcam/zbarcam.c +4
-0@@ -105,6 +171,10 @@ static void data_handler (zbar_image_t *img, const void *userdata)if (type ==ZBAR_PARTIAL )continue ; +#ifdef HAVE_DBUS +send_dbus (zbar_symbol_get_data (sym)); +#endif +if (!format) {printf ("%s:" ,zbar_get_symbol_name (type)); if(fwrite(zbar_symbol_get_data(sym),Thanks,
Mauro
Em Wed, 16 Jan 2019 13:37:58 -0700
James Hilliard <[email protected]> escreveu:
Quoted 15 lines
Quoted 13 lines · Mauro Carvalho Chehab
Hi James,
Em Wed, 16 Jan 2019 16:54:55 +0800
[email protected] escreveu:Quoted 4 lines
From: James Hilliard <[email protected]>
This is useful for running zbarcam as a systemd service so that other applications can receive scan messages through dbus.
Nice approach!
Quoted 7 lines
Quoted 6 lines
Yet, I would try to write it on a different way, making sure that it could also be using by zbarimg.
I mean, if you add the dbus bindings inside the zbar core, it shouldn't matter if the source image comes via a webcam or via a scanned image. Both will be able to send the scancodes via dbus.
Which function should I call
send_dbus()from in that case?
Good question.
Tests required, but I suspect that you could add it at processor.c, inside _zbar_process_image().
I suspect that, if you do something like:
if(nsyms) {
/* FIXME only call after filtering */
_zbar_mutex_lock(&proc->mutex);
_zbar_processor_notify(proc, EVENT_OUTPUT);
_zbar_mutex_unlock(&proc->mutex);
if(proc->handler)
proc->handler(img, proc->userdata);
+ if(proc->is_dbus_enabled)
+ zbar_send_code_via_dbus(proc->userdata);
}should work.
You could easily check if this is working by calling:
$ ./zbarimg/zbarimg ./examples/barcode.png EAN-13:9876543210128 scanned 1 barcode symbols from 1 images in 0.2 seconds
(I would add a flag to allow enabling/disabling it by applications).
Quoted 7 lines
Quoted 5 lines
As a future approach, we may even think on making the interface duplex in the future, e. g. allowing any camera application to send an image via dbus and let zbar to decode it and return the decoded bar codes.
That could be useful, probably too difficult for me to implement myself though(I don't write a lot of c usually).
As I said, this is just an idea for a future possible development.
Not sure how this would actually work, as dbus may not be the best interface for passing images.
Thanks,
Mauro