remove common library
[platform/core/connectivity/nfc-manager-neard.git] / daemon / net_nfc_server.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16 #include <glib.h>
17 #include <gio/gio.h>
18 #include <vconf.h>
19
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_gdbus.h"
23 #include "net_nfc_server.h"
24 #include "net_nfc_server_common.h"
25 #include "net_nfc_server_vconf.h"
26 #include "net_nfc_server_manager.h"
27 #include "net_nfc_server_util.h"
28 #include "net_nfc_server_controller.h"
29 #include "net_nfc_server_tag.h"
30 #include "net_nfc_server_ndef.h"
31 #include "net_nfc_server_llcp.h"
32 #include "net_nfc_server_p2p.h"
33 #include "net_nfc_server_transceive.h"
34 #include "net_nfc_server_handover.h"
35 #include "net_nfc_server_se.h"
36 #include "net_nfc_server_snep.h"
37 #include "net_nfc_server_system_handler.h"
38 #include "net_nfc_server_context.h"
39
40 static gboolean use_daemon = FALSE;
41 static GMainLoop *loop = NULL;
42
43 static GDBusConnection *connection = NULL;
44 static guint subscribe_id;
45
46 GOptionEntry option_entries[] = {
47         { "daemon", 'd', 0, G_OPTION_ARG_NONE, &use_daemon,
48                 "Use Daemon mode", NULL },
49         { NULL }
50 };
51
52 pid_t net_nfc_server_gdbus_get_pid(const char *name)
53 {
54         guint pid = 0;
55         GError *error = NULL;
56         GVariant *_ret;
57
58         _ret = g_dbus_connection_call_sync(connection,
59                         "org.freedesktop.DBus",
60                         "/org/freedesktop/DBus",
61                         "org.freedesktop.DBus",
62                         "GetConnectionUnixProcessID",
63                         g_variant_new("(s)", name),
64                         NULL,
65                         G_DBUS_CALL_FLAGS_NONE,
66                         -1,
67                         NULL,
68                         &error);
69         if (_ret != NULL) {
70                 g_variant_get(_ret, "(u)", &pid);
71                 g_variant_unref(_ret);
72         }
73
74         return pid;
75 }
76
77 static void _name_owner_changed(GDBusProxy *proxy,
78                 const gchar *name, const gchar *old_owner,
79                 const gchar *new_owner, void *user_data)
80 {
81         if (name == NULL || old_owner == NULL || new_owner == NULL) {
82                 DEBUG_ERR_MSG("invalid parameter");
83
84                 return;
85         }
86
87         if (strlen(new_owner) == 0) {
88                 if (net_nfc_server_gdbus_check_client_is_running(old_owner)) {
89                         /* unregister service */
90                         net_nfc_server_llcp_unregister_services(old_owner);
91
92                         /* remove client context */
93                         net_nfc_server_gdbus_cleanup_client_context(old_owner);
94                 }
95         }
96 }
97
98 static void _on_name_owner_changed(GDBusConnection *connection,
99                 const gchar *sender_name, const gchar *object_path,
100                 const gchar *interface_name, const gchar *signal_name,
101                 GVariant *parameters, gpointer user_data)
102 {
103         gchar *name;
104         gchar *old_owner;
105         gchar *new_owner;
106
107         g_variant_get(parameters,
108                         "(sss)",
109                         &name,
110                         &old_owner,
111                         &new_owner);
112
113         _name_owner_changed((GDBusProxy *)connection,
114                         name, old_owner, new_owner, user_data);
115 }
116
117 static void _subscribe_name_owner_changed_event()
118 {
119         if (connection == NULL)
120                 return;
121
122         /* subscribe signal */
123         subscribe_id = g_dbus_connection_signal_subscribe(connection,
124                         "org.freedesktop.DBus", /* bus name */
125                         "org.freedesktop.DBus", /* interface */
126                         "NameOwnerChanged", /* member */
127                         "/org/freedesktop/DBus", /* path */
128                         NULL, /* arg0 */
129                         G_DBUS_SIGNAL_FLAGS_NONE,
130                         _on_name_owner_changed,
131                         NULL, NULL);
132 }
133
134 static void _unsubscribe_name_owner_changed_event()
135 {
136         if (connection == NULL)
137                 return;
138
139         /* subscribe signal */
140         if (subscribe_id > 0) {
141                 g_dbus_connection_signal_unsubscribe(connection, subscribe_id);
142         }
143 }
144
145 static void net_nfc_server_gdbus_init(void)
146 {
147         GError *error = NULL;
148
149         if (connection)
150                 g_object_unref(connection);
151
152         connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
153         if (connection == NULL)
154         {
155                 DEBUG_ERR_MSG("Can not get connection %s", error->message);
156                 g_error_free (error);
157                 return;
158         }
159
160         net_nfc_server_gdbus_init_client_context();
161
162         if (net_nfc_server_manager_init(connection) == FALSE)
163         {
164                 DEBUG_ERR_MSG("Can not init manager");
165                 return;
166         }
167
168         if (net_nfc_server_tag_init(connection) == FALSE)
169         {
170                 DEBUG_ERR_MSG("Can not init tag");
171                 return;
172         }
173
174         if (net_nfc_server_ndef_init(connection) == FALSE)
175         {
176                 DEBUG_ERR_MSG("Can not init ndef");
177                 return;
178         }
179
180         if (net_nfc_server_llcp_init(connection) == FALSE)
181         {
182                 DEBUG_ERR_MSG("Can not init llcp");
183                 return;
184         }
185
186         if (net_nfc_server_p2p_init(connection) == FALSE)
187         {
188                 DEBUG_ERR_MSG("Can not init tag");
189                 return;
190         }
191
192         if (net_nfc_server_transceive_init(connection) == FALSE)
193         {
194                 DEBUG_ERR_MSG("Can not initialize transceive");
195                 return;
196         }
197
198         if (net_nfc_server_handover_init(connection) == FALSE)
199         {
200                 DEBUG_ERR_MSG("Can not initialize transceive");
201                 return;
202         }
203
204         if (net_nfc_server_se_init(connection) == FALSE)
205         {
206                 DEBUG_ERR_MSG("Can not init Test");
207                 return;
208         }
209
210         if (net_nfc_server_snep_init(connection) == FALSE)
211         {
212                 DEBUG_ERR_MSG("Can not init controller thread");
213                 return;
214         }
215
216         if (net_nfc_server_system_handler_init(connection) == FALSE)
217         {
218                 DEBUG_ERR_MSG("Can not init controller thread");
219                 return;
220         }
221
222         if (net_nfc_server_controller_thread_init() == FALSE)
223         {
224                 DEBUG_ERR_MSG("Can not init controller thread");
225                 return;
226         }
227
228         _subscribe_name_owner_changed_event();
229 }
230
231 static void net_nfc_server_gdbus_deinit(void)
232 {
233         _unsubscribe_name_owner_changed_event();
234
235         net_nfc_server_manager_deinit();
236         net_nfc_server_tag_deinit();
237         net_nfc_server_ndef_deinit();
238         net_nfc_server_llcp_deinit();
239         net_nfc_server_transceive_deinit();
240         net_nfc_server_handover_deinit();
241         net_nfc_server_se_deinit();
242         net_nfc_server_snep_deinit();
243         net_nfc_server_system_handler_deinit();
244
245         net_nfc_server_gdbus_deinit_client_context();
246
247         net_nfc_server_controller_thread_deinit();
248
249         if (connection)
250         {
251                 g_object_unref(connection);
252                 connection = NULL;
253         }
254 }
255
256 void net_nfc_manager_quit()
257 {
258         DEBUG_MSG("net_nfc_manager_quit kill the nfc-manager daemon!!");
259
260         if (loop != NULL)
261                 g_main_loop_quit(loop);
262 }
263
264 static void on_bus_acquired(GDBusConnection *connection, const gchar *path,
265                 gpointer user_data)
266 {
267         gint state;
268
269         DEBUG_MSG("bus path : %s", path);
270
271         net_nfc_server_gdbus_init();
272
273         net_nfc_server_controller_init();
274
275         if (vconf_get_bool(VCONFKEY_NFC_STATE, &state) != 0)
276         {
277                 DEBUG_MSG("VCONFKEY_NFC_STATE is not exist");
278                 net_nfc_manager_quit();
279
280                 return;
281         }
282
283         net_nfc_server_vconf_init();
284
285         if (state == 1)
286                 net_nfc_server_manager_set_active(TRUE);
287 #ifndef ESE_ALWAYS_ON
288         else if (use_daemon == TRUE)
289                 net_nfc_server_controller_deinit();
290 #endif
291 }
292
293 static void on_name_acquired(GDBusConnection *connection, const gchar *name,
294                 gpointer user_data)
295 {
296         DEBUG_SERVER_MSG("name : %s", name);
297 }
298
299 static void on_name_lost(GDBusConnection *connnection, const gchar *name,
300                 gpointer user_data)
301 {
302         DEBUG_SERVER_MSG("name : %s", name);
303
304         net_nfc_manager_quit();
305 }
306
307
308 int main(int argc, char *argv[])
309 {
310
311         void *handle = NULL;
312         guint id = 0;
313         gboolean use_daemon = FALSE;
314         GOptionContext *option_context;
315         GError *error = NULL;
316
317         net_nfc_change_log_tag();
318
319         option_context = g_option_context_new("Nfc manager");
320         g_option_context_add_main_entries(option_context, option_entries, NULL);
321
322         if (g_option_context_parse(option_context, &argc, &argv, &error) == FALSE)
323         {
324                 DEBUG_ERR_MSG("can not parse option: %s", error->message);
325                 g_error_free(error);
326
327                 g_option_context_free(option_context);
328                 return 0;
329         }
330
331         DEBUG_SERVER_MSG("start nfc manager");
332         DEBUG_SERVER_MSG("use_daemon : %d", use_daemon);
333
334         net_nfc_manager_init_log();
335
336         net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
337
338         handle = net_nfc_controller_onload();
339         if (handle == NULL)
340         {
341                 DEBUG_ERR_MSG("load plugin library is failed");
342
343                 if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_OFF) != 0)
344                         DEBUG_ERR_MSG("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_OFF);
345
346                 if (vconf_set_bool(VCONFKEY_NFC_STATE, 0) != 0)
347                         DEBUG_ERR_MSG("VCONFKEY_NFC_STATE set to %d failed", 0);
348
349                 goto EXIT;
350         }
351
352         if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_ON) != 0)
353                 DEBUG_ERR_MSG("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_ON);
354
355         id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
356                         "org.tizen.NetNfcService",
357                         G_BUS_NAME_OWNER_FLAGS_NONE,
358                         on_bus_acquired,
359                         on_name_acquired,
360                         on_name_lost,
361                         NULL,
362                         NULL);
363
364         loop = g_main_loop_new(NULL, FALSE);
365         g_main_loop_run(loop);
366
367 EXIT :
368         net_nfc_server_vconf_deinit();
369         net_nfc_server_controller_deinit();
370         net_nfc_server_gdbus_deinit();
371
372         if (id)
373                 g_bus_unown_name(id);
374
375         net_nfc_controller_unload(handle);
376
377         net_nfc_manager_fini_log();
378
379         g_option_context_free(option_context);
380
381         return 0;
382 }