Add libtlm-nfc support to gum-example appication
authorAlexander Kanavin <alexander.kanavin@intel.com>
Wed, 18 Dec 2013 15:11:07 +0000 (17:11 +0200)
committerAlexander Kanavin <alexander.kanavin@intel.com>
Wed, 18 Dec 2013 15:11:07 +0000 (17:11 +0200)
configure.ac
docs/building.xml
docs/examples.xml
examples/Makefile.am
examples/gum-example.c

index 2fc27e4..6143f18 100644 (file)
@@ -32,6 +32,13 @@ AC_SUBST(GLIB_CFLAGS)
 AC_SUBST(GLIB_LIBS)
 AC_PATH_PROG(GLIB_MKENUMS, glib-mkenums)
 
+# Checks for libtlm-nfc
+PKG_CHECK_MODULES([LIBTLM_NFC], [libtlm-nfc], [have_libtlm_nfc=yes], [have_libtlm_nfc=no])
+AM_CONDITIONAL(HAVE_LIBTLM_NFC, [test x$have_libtlm_nfc = xyes])
+if test "x$have_libtlm_nfc" = xyes; then
+    AC_DEFINE(HAVE_LIBTLM_NFC, [1], [Define if libtlm-nfc exists.])
+fi
+
 # Check library.
 PKG_CHECK_MODULES([CHECK], [check >= 0.9.4], [have_check=yes], [have_check=no])
 AC_SUBST(CHECK_CFLAGS)
index ef0fde1..546bfa3 100644 (file)
@@ -87,7 +87,7 @@
       </listitem>
       <listitem>
         <para>
-            The 
+            The
             <ulink url="http://www.freedesktop.org/software/systemd/man/">
                 Systemd
             </ulink>
             querying/terminating users sessions e.g. when a user is deleted.
         </para>
       </listitem>
+      <listitem>
+        <para>
+            The optional
+            <ulink url="https://github.com/01org/libtlm-nfc">
+                libtlm-nfc library
+            </ulink>
+            provides support for writing usernames and passwords to NFC tags
+            in <link linkend="gumd-examples">gum-example application</link>.
+            Such tags can be used for logging in with TLM login manager.
+        </para>
+      </listitem>
     </itemizedlist>
 
   </refsect1>
index 38468b6..8453c11 100644 (file)
                         --homephone=&lt;home phone&gt;
                         --homedir=&lt;home dir&gt;
                         --shell=&lt;path to shell exe&gt;
+                        --write-nfc
             </computeroutput>
         </literallayout>
+        --write-nfc flag can be used to write the username and secret to an NFC
+        tag, if gum-example was compiled with <link linkend="um-building">libtlm-nfc
+        </link> support.
      </para>        
   </refsect1>
   
                         --officephone=&lt;office phone&gt;
                         --homephone=&lt;home phone&gt;
                         --shell=&lt;path to shell exe&gt; 
+                        --write-nfc
             </computeroutput>
         </literallayout>
-     </para>        
+        --write-nfc flag has the same meaning as with adding users.
+     </para>
   </refsect1>
 
   <refsect1>
index 6f4c276..b5696be 100644 (file)
@@ -14,3 +14,8 @@ gum_example_LDADD = \
        $(DEPS_LIBS) \
        $(top_builddir)/src/lib/libgum.la \
        $(LIBGUM_LIBS)
+
+if HAVE_LIBTLM_NFC
+gum_example_CPPFLAGS += $(LIBTLM_NFC_CFLAGS)
+gum_example_LDADD += $(LIBTLM_NFC_LIBS)
+endif
\ No newline at end of file
index 93c8ae8..a40edf7 100644 (file)
 #include "gum-group.h"
 #include "common/gum-group-types.h"
 #include "common/gum-defines.h"
+#include "config.h"
+
+#ifdef HAVE_LIBTLM_NFC
+#include <gtlm-nfc.h>
+#endif
 
 typedef struct {
     uid_t uid;
@@ -210,9 +215,73 @@ _print_user_prop (
     _free_test_user (user);
 }
 
+#ifdef HAVE_LIBTLM_NFC
+static void _on_tag_found(GTlmNfc* tlm_nfc,
+                          const gchar* tag_path,
+                          gpointer user_data)
+{
+    g_print("Tag %s found\n", tag_path);
+
+    g_print("Waiting 5 seconds due to https://01.org/jira/browse/NFC-57\n");
+    sleep(5);
+
+    GumUser* user = GUM_USER(user_data);
+
+    gchar* user_name;
+    gchar* secret;
+
+    g_object_get (G_OBJECT (user), "username", &user_name, NULL);
+    g_object_get (G_OBJECT (user), "secret", &secret, NULL);
+
+    GError* error = NULL;
+    g_print("Writing to tag...");
+    gtlm_nfc_write_username_password(tlm_nfc,
+                                     tag_path,
+                                     user_name,
+                                     secret,
+                                     &error);
+
+    if (!error)
+        g_print("success!\n");
+    else
+        g_print("error: %s\n", error->message);
+
+    g_free(user_name);
+    g_free(secret);
+
+    _stop_mainloop();
+}
+
+static void
+_handle_write_nfc (
+        GumUser *user)
+{
+    g_print("Writing the username and password to NFC tag; please place a tag next \
+to the NFC adapter\n");
+
+    GTlmNfc* tlm_nfc = g_object_new(G_TYPE_TLM_NFC, NULL);
+    G_IS_TLM_NFC(tlm_nfc);
+    g_signal_connect(tlm_nfc, "tag-found", G_CALLBACK(_on_tag_found), user);
+
+    _run_mainloop();
+
+    g_object_unref(tlm_nfc);
+
+}
+#else
+static void
+_handle_write_nfc (
+        GumUser *user)
+{
+    g_print("gum-example was compiled without libtlm-nfc support\n");
+}
+#endif
+
 static void
 _handle_user_add (
-        TestUser *user)
+        TestUser *user,
+        gboolean write_nfc
+                 )
 {
     GumUser *guser = NULL;
     guser = gum_user_create_sync ();
@@ -230,6 +299,10 @@ _handle_user_add (
 
     DBG ("User added successfully");
 
+    if (write_nfc) {
+        _handle_write_nfc(guser);
+    }
+
     g_object_unref (guser);
 }
 
@@ -255,7 +328,9 @@ _handle_user_del (
 
 static void
 _handle_user_up (
-               TestUser *user)
+               TestUser *user,
+                gboolean write_nfc
+                )
 {
     GumUser *guser = NULL;
 
@@ -287,6 +362,10 @@ _handle_user_up (
         DBG ("User updated successfully");
     }
 
+    if (write_nfc) {
+        _handle_write_nfc(guser);
+    }
+
     g_object_unref (guser);
 }
 
@@ -570,6 +649,7 @@ main (int argc, char *argv[])
     gboolean is_group_up_op = FALSE, is_group_get_op = FALSE;
     gboolean is_group_get_by_name_op = FALSE, is_group_add_mem_op = FALSE;
     gboolean is_group_del_mem_op = FALSE;
+    gboolean is_write_nfc = FALSE;
     GOptionGroup* group_option = NULL;
     TestGroup *group = NULL;
 
@@ -609,6 +689,8 @@ main (int argc, char *argv[])
                 "group add member -- gid and mem_uid are mandatory", NULL},
         { "delete-member", 'n', 0, G_OPTION_ARG_NONE, &is_group_del_mem_op,
                 "group delete member -- gid and mem_uid are mandatory", NULL},
+        { "write-nfc", 0, 0, G_OPTION_ARG_NONE, &is_write_nfc,
+                "write username and secret to an NFC tag when creating or updating a user", NULL},
         { NULL }
     };
     
@@ -684,11 +766,11 @@ main (int argc, char *argv[])
     _create_mainloop ();
     
     if (is_user_add_op) {
-        _handle_user_add (user);
+        _handle_user_add (user, is_write_nfc);
     } else if (is_user_del_op) {
        _handle_user_del (user);
     } else if (is_user_up_op) {
-       _handle_user_up (user);
+       _handle_user_up (user, is_write_nfc);
     } else if (is_user_get_op) {
        _handle_user_get (user);
     } else if (is_user_get_by_name_op) {