2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* standard library header */
23 #include <glib-object.h>
29 #if defined(USE_AUTOSTART) && !defined(USE_GDBUS)
30 #include <dbus/dbus-glib.h>
31 #include <dbus/dbus-glib-bindings.h>
34 /* SLP library header */
38 #include "ServerIPC.h"
39 #include "ServerResource.h"
41 #include "smartcard-service-gdbus.h"
42 #include "ServerGDBus.h"
45 #include "SmartcardDbus.h"
46 #include "smartcard-service-binding.h"
52 using namespace smartcard_service_api;
55 GMainLoop *main_loop = NULL;
56 #if defined(USE_AUTOSTART) && !defined(USE_GDBUS)
57 GObject *object = NULL;
58 DBusGConnection *connection = NULL;
62 static void daemonize(void)
66 /* already a daemon */
70 /* Fork off the parent process */
79 exit(EXIT_SUCCESS); /*Killing the Parent Process*/
82 /* At this point we are executing as the child process */
85 /* Create a new SID for the child process */
92 /* Change the current working directory. */
100 close(STDERR_FILENO);
105 static void _bus_acquired_cb(GDBusConnection *connection,
106 const gchar *path, gpointer user_data)
108 _DBG("bus path : %s", path);
110 ServerResource::getInstance();
112 ServerGDBus::getInstance().init();
115 static void _name_acquired_cb(GDBusConnection *connection,
116 const gchar *name, gpointer user_data)
118 _DBG("name : %s", name);
121 static void _name_lost_cb(GDBusConnection *connnection,
122 const gchar *name, gpointer user_data)
124 _DBG("name : %s", name);
126 ServerGDBus::getInstance().deinit();
131 G_DEFINE_TYPE(Smartcard_Service, smartcard_service, G_TYPE_OBJECT)
133 /* Just Check the assert and set the error message */
134 #define __G_ASSERT(test, return_val, error, domain, error_code)\
137 if G_LIKELY (!(test)) { \
138 g_set_error (error, domain, error_code, #test); \
139 return (return_val); \
144 GQuark smartcard_service_error_quark(void)
146 _DBG("smartcard_service_error_quark entered");
148 return g_quark_from_static_string("smartcard_service_error");
151 static void smartcard_service_init(Smartcard_Service *smartcard_service)
153 _DBG("smartcard_service_init entered");
156 static void smartcard_service_class_init(Smartcard_ServiceClass *smartcard_service_class)
158 _DBG("smartcard_service_class_init entered");
160 dbus_g_object_type_install_info(SMARTCARD_SERVICE_TYPE, &dbus_glib_smartcard_service_object_info);
163 gboolean smartcard_service_launch(Smartcard_Service *smartcard_service, guint *result_val, GError **error)
165 _DBG("smartcard_service_launch entered");
170 static void _initialize_dbus()
172 GError *error = NULL;
173 DBusGProxy *proxy = NULL;
180 connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
183 object = (GObject *)g_object_new(SMARTCARD_SERVICE_TYPE, NULL);
184 dbus_g_connection_register_g_object(connection, SMARTCARD_SERVICE_PATH, object);
186 /* register service */
187 proxy = dbus_g_proxy_new_for_name(connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
190 if (!org_freedesktop_DBus_request_name(proxy, SMARTCARD_SERVICE_NAME, 0, &ret, &error))
192 _ERR("Unable to register service: %s", error->message);
196 g_object_unref (proxy);
200 _ERR("dbus_g_proxy_new_for_name failed");
205 _ERR("ERROR: Can't get on system bus [%s]", error->message);
212 static void _finalize_dbus()
216 dbus_g_connection_unregister_g_object(connection, object);
217 g_object_unref(object);
224 static void __sighandler(int sig)
226 _DBG("signal!! [%d]", sig);
236 int main(int argc, char *argv[])
241 signal(SIGTERM, &__sighandler);
243 #ifndef USE_AUTOSTART
247 if (!g_thread_supported()) {
253 main_loop = g_main_new(TRUE);
256 id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
257 "org.tizen.SmartcardService",
258 G_BUS_NAME_OWNER_FLAGS_NONE,
265 ServerIPC::getInstance()->createListenSocket();
270 g_main_loop_run(main_loop);
274 g_bus_unown_name(id);
280 /* release secure element.. (pure virtual function problem..) */
281 ServerResource::getInstance().unloadSecureElements();
286 void smartcard_daemon_exit()
288 g_main_loop_quit(main_loop);