1 /*
2  * fprintd example to enroll right index finger
3  * Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dbus/dbus-glib-bindings.h>
23 #include "manager-dbus-glue.h"
24 #include "device-dbus-glue.h"
25 #include "marshal.h"
26 
27 #define N_(x) x
28 #define TR(x) x
29 #include "fingerprint-strings.h"
30 
31 static DBusGProxy *manager = NULL;
32 static DBusGConnection *connection = NULL;
33 static char *finger_name = "right-index-finger";
34 static char **usernames = NULL;
35 
create_manager(void)36 static void create_manager(void)
37 {
38 	GError *error = NULL;
39 
40 	connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
41 	if (connection == NULL) {
42 		g_print("Failed to connect to session bus: %s\n", error->message);
43 		exit (1);
44 	}
45 
46 	manager = dbus_g_proxy_new_for_name(connection,
47 		"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
48 		"net.reactivated.Fprint.Manager");
49 }
50 
open_device(const char * username)51 static DBusGProxy *open_device(const char *username)
52 {
53 	GError *error = NULL;
54 	gchar *path;
55 	DBusGProxy *dev;
56 
57 	if (!net_reactivated_Fprint_Manager_get_default_device(manager, &path, &error)) {
58 		g_print("list_devices failed: %s\n", error->message);
59 		exit (1);
60 	}
61 
62 	if (path == NULL) {
63 		g_print("No devices found\n");
64 		exit(1);
65 	}
66 
67 	g_print("Using device %s\n", path);
68 
69 	/* FIXME use for_name_owner?? */
70 	dev = dbus_g_proxy_new_for_name(connection, "net.reactivated.Fprint",
71 		path, "net.reactivated.Fprint.Device");
72 
73 	g_free (path);
74 
75 	if (!net_reactivated_Fprint_Device_claim(dev, username, &error)) {
76 		g_print("failed to claim device: %s\n", error->message);
77 		exit (1);
78 	}
79 	return dev;
80 }
81 
enroll_result(GObject * object,const char * result,gboolean done,void * user_data)82 static void enroll_result(GObject *object, const char *result, gboolean done, void *user_data)
83 {
84 	gboolean *enroll_completed = user_data;
85 	g_print("Enroll result: %s\n", result);
86 	if (done != FALSE)
87 		*enroll_completed = TRUE;
88 }
89 
do_enroll(DBusGProxy * dev)90 static void do_enroll(DBusGProxy *dev)
91 {
92 	GError *error = NULL;
93 	gboolean enroll_completed = FALSE;
94 	gboolean found;
95 	guint i;
96 
97 	dbus_g_proxy_add_signal(dev, "EnrollStatus", G_TYPE_STRING, G_TYPE_BOOLEAN, NULL);
98 	dbus_g_proxy_connect_signal(dev, "EnrollStatus", G_CALLBACK(enroll_result),
99 				    &enroll_completed, NULL);
100 
101 	found = FALSE;
102 	for (i = 0; fingers[i].dbus_name != NULL; i++) {
103 		if (g_strcmp0 (fingers[i].dbus_name, finger_name) == 0) {
104 			found = TRUE;
105 			break;
106 		}
107 	}
108 	if (!found) {
109 		GString *s;
110 
111 		s = g_string_new (NULL);
112 		g_string_append_printf (s, "Invalid finger name '%s'. Name must be one of ", finger_name);
113 		for (i = 0; fingers[i].dbus_name != NULL; i++) {
114 			g_string_append_printf (s, "%s", fingers[i].dbus_name);
115 			if (fingers[i + 1].dbus_name != NULL)
116 				g_string_append (s, ", ");
117 		}
118 		g_warning ("%s", s->str);
119 		g_string_free (s, TRUE);
120 		exit (1);
121 	}
122 
123 	g_print("Enrolling %s finger.\n", finger_name);
124 	if (!net_reactivated_Fprint_Device_enroll_start(dev, finger_name, &error)) {
125 		g_print("EnrollStart failed: %s\n", error->message);
126 		exit (1);
127 	}
128 
129 	while (!enroll_completed)
130 		g_main_context_iteration(NULL, TRUE);
131 
132 	dbus_g_proxy_disconnect_signal(dev, "EnrollStatus",
133 		G_CALLBACK(enroll_result), &enroll_completed);
134 
135 	if (!net_reactivated_Fprint_Device_enroll_stop(dev, &error)) {
136 		g_print("VerifyStop failed: %s\n", error->message);
137 		exit(1);
138 	}
139 }
140 
release_device(DBusGProxy * dev)141 static void release_device(DBusGProxy *dev)
142 {
143 	GError *error = NULL;
144 	if (!net_reactivated_Fprint_Device_release(dev, &error)) {
145 		g_print("ReleaseDevice failed: %s\n", error->message);
146 		exit (1);
147 	}
148 }
149 
150 static const GOptionEntry entries[] = {
151     { "finger", 'f',  0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL },
152     { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" },
153     { NULL }
154 };
155 
main(int argc,char ** argv)156 int main(int argc, char **argv)
157 {
158 	GOptionContext *context;
159 	GError *err = NULL;
160 	DBusGProxy *dev;
161 
162 #if !GLIB_CHECK_VERSION (2, 36, 0)
163 	g_type_init();
164 #endif
165 
166 	dbus_g_object_register_marshaller (fprintd_marshal_VOID__STRING_BOOLEAN,
167 					   G_TYPE_NONE, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INVALID);
168 
169 	context = g_option_context_new ("Enroll a fingerprint");
170 	g_option_context_add_main_entries (context, entries, NULL);
171 
172 	if (g_option_context_parse (context, &argc, &argv, &err) == FALSE) {
173 		g_print ("couldn't parse command-line options: %s\n", err->message);
174 		g_error_free (err);
175 		return 1;
176 	}
177 
178 	create_manager();
179 
180 	dev = open_device(usernames ? usernames[0] : NULL);
181 	do_enroll(dev);
182 	release_device(dev);
183 	return 0;
184 }
185 
186