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