Merge remote-tracking branch 'tizen_2.2' into tizen
[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_phdc.h"
26 #include "net_nfc_server_vconf.h"
27 #include "net_nfc_server_manager.h"
28 #include "net_nfc_server_util.h"
29 #include "net_nfc_server_controller.h"
30 #include "net_nfc_server_tag.h"
31 #include "net_nfc_server_ndef.h"
32 #include "net_nfc_server_llcp.h"
33 #include "net_nfc_server_p2p.h"
34 #include "net_nfc_server_transceive.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         GVariant *_ret;
56         guint pid = 0;
57         GError *error = NULL;
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
71         if (_ret != NULL)
72         {
73                 g_variant_get(_ret, "(u)", &pid);
74                 g_variant_unref(_ret);
75         }
76
77         return pid;
78 }
79
80 static void _name_owner_changed(GDBusProxy *proxy,
81                 const gchar *name, const gchar *old_owner,
82                 const gchar *new_owner, void *user_data)
83 {
84         RET_IF(NULL == name);
85         RET_IF(NULL == old_owner);
86         RET_IF(NULL == new_owner);
87
88         if (strlen(new_owner) == 0)
89         {
90                 if (net_nfc_server_gdbus_check_client_is_running(old_owner))
91                 {
92                         /* unregister service */
93                         net_nfc_server_llcp_unregister_services(old_owner);
94
95                         /* remove client context */
96                         net_nfc_server_gdbus_cleanup_client_context(old_owner);
97                 }
98         }
99 }
100
101 static void _on_name_owner_changed(GDBusConnection *connection,
102                 const gchar *sender_name,
103                 const gchar *object_path,
104                 const gchar *interface_name,
105                 const gchar *signal_name,
106                 GVariant *parameters,
107                 gpointer user_data)
108 {
109         gchar *name;
110         gchar *old_owner;
111         gchar *new_owner;
112
113         g_variant_get(parameters, "(sss)", &name, &old_owner, &new_owner);
114
115         _name_owner_changed((GDBusProxy *)connection, name, old_owner, new_owner, user_data);
116 }
117
118 static void _subscribe_name_owner_changed_event()
119 {
120         RET_IF(NULL == connection);
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         RET_IF(NULL == connection);
137
138         /* subscribe signal */
139         if (subscribe_id > 0)
140                 g_dbus_connection_signal_unsubscribe(connection, subscribe_id);
141 }
142
143 static void net_nfc_server_gdbus_init(void)
144 {
145         GError *error = NULL;
146
147         if (connection)
148                 g_object_unref(connection);
149
150         connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
151         if (NULL == connection)
152         {
153                 NFC_ERR("Can not get connection %s", error->message);
154                 g_error_free (error);
155                 return;
156         }
157
158         net_nfc_server_gdbus_init_client_context();
159
160         if (net_nfc_server_manager_init(connection) == FALSE)
161         {
162                 NFC_ERR("Can not init manager");
163                 return;
164         }
165
166         if (net_nfc_server_tag_init(connection) == FALSE)
167         {
168                 NFC_ERR("Can not init tag");
169                 return;
170         }
171
172         if (net_nfc_server_ndef_init(connection) == FALSE)
173         {
174                 NFC_ERR("Can not init ndef");
175                 return;
176         }
177
178         if (net_nfc_server_llcp_init(connection) == FALSE)
179         {
180                 NFC_ERR("Can not init llcp");
181                 return;
182         }
183
184         if (net_nfc_server_p2p_init(connection) == FALSE)
185         {
186                 NFC_ERR("Can not init p2p");
187                 return;
188         }
189
190         if (net_nfc_server_transceive_init(connection) == FALSE)
191         {
192                 NFC_ERR("Can not initialize transceive");
193                 return;
194         }
195
196         if (net_nfc_server_handover_init(connection) == FALSE)
197         {
198                 NFC_ERR("Can not initialize handover");
199                 return;
200         }
201
202         if (net_nfc_server_se_init(connection) == FALSE)
203         {
204                 NFC_ERR("Can not init se");
205                 return;
206         }
207
208         if (net_nfc_server_snep_init(connection) == FALSE)
209         {
210                 NFC_ERR("Can not init snep");
211                 return;
212         }
213
214         if (net_nfc_server_system_handler_init(connection) == FALSE)
215         {
216                 NFC_ERR("Can not init system handler");
217                 return;
218         }
219
220         if (net_nfc_server_phdc_init(connection) == FALSE)
221         {
222                 NFC_ERR("Can not init phdc");
223                 return;
224         }
225
226         if (net_nfc_server_controller_thread_init() == FALSE)
227         {
228                 NFC_ERR("Can not init controller thread");
229                 return;
230         }
231
232         _subscribe_name_owner_changed_event();
233 }
234
235 static void net_nfc_server_gdbus_deinit(void)
236 {
237         _unsubscribe_name_owner_changed_event();
238
239         net_nfc_server_manager_deinit();
240         net_nfc_server_tag_deinit();
241         net_nfc_server_ndef_deinit();
242         net_nfc_server_llcp_deinit();
243         net_nfc_server_transceive_deinit();
244         net_nfc_server_handover_deinit();
245         net_nfc_server_se_deinit();
246         net_nfc_server_snep_deinit();
247         net_nfc_server_system_handler_deinit();
248         net_nfc_server_phdc_deinit();
249         net_nfc_server_gdbus_deinit_client_context();
250
251         net_nfc_server_controller_thread_deinit();
252
253         if (connection)
254         {
255                 g_object_unref(connection);
256                 connection = NULL;
257         }
258 }
259
260 void net_nfc_manager_quit()
261 {
262         NFC_DBG("net_nfc_manager_quit kill the nfc-manager daemon!!");
263
264         if (loop != NULL)
265                 g_main_loop_quit(loop);
266 }
267
268 static void on_bus_acquired(GDBusConnection *connection, const gchar *path,
269                 gpointer user_data)
270 {
271         gint state;
272
273         NFC_DBG("bus path : %s", path);
274
275         net_nfc_server_gdbus_init();
276
277         net_nfc_server_controller_init();
278
279         if (vconf_get_bool(VCONFKEY_NFC_STATE, &state) != 0)
280         {
281                 NFC_DBG("VCONFKEY_NFC_STATE is not exist");
282                 net_nfc_manager_quit();
283
284                 return;
285         }
286
287         net_nfc_server_vconf_init();
288
289         if (state == 1)
290                 net_nfc_server_manager_set_active(TRUE);
291 #ifndef ESE_ALWAYS_ON
292         else if (use_daemon == TRUE)
293                 net_nfc_server_controller_deinit();
294 #endif
295 }
296
297 static void on_name_acquired(GDBusConnection *connection, const gchar *name,
298                 gpointer user_data)
299 {
300         NFC_INFO("name : %s", name);
301 }
302
303 static void on_name_lost(GDBusConnection *connnection, const gchar *name,
304                 gpointer user_data)
305 {
306         NFC_INFO("name : %s", name);
307
308         net_nfc_manager_quit();
309 }
310
311
312 int main(int argc, char *argv[])
313 {
314         guint id = 0;
315         void *handle = NULL;
316         GError *error = NULL;
317         gboolean use_daemon = FALSE;
318         GOptionContext *option_context;
319
320         net_nfc_change_log_tag();
321
322         option_context = g_option_context_new("Nfc manager");
323         g_option_context_add_main_entries(option_context, option_entries, NULL);
324
325         if (g_option_context_parse(option_context, &argc, &argv, &error) == FALSE)
326         {
327                 NFC_ERR("can not parse option: %s", error->message);
328                 g_error_free(error);
329
330                 g_option_context_free(option_context);
331                 return 0;
332         }
333
334         NFC_DBG("start nfc manager");
335         NFC_INFO("use_daemon : %d", use_daemon);
336
337         net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
338
339         handle = net_nfc_controller_onload();
340         if (NULL == handle)
341         {
342                 NFC_ERR("load plugin library is failed");
343
344                 if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_OFF) != 0)
345                         NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_OFF);
346
347                 if (vconf_set_bool(VCONFKEY_NFC_STATE, 0) != 0)
348                         NFC_ERR("VCONFKEY_NFC_STATE set to %d failed", 0);
349
350                 goto EXIT;
351         }
352
353         if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_ON) != 0)
354                 NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_ON);
355
356         id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
357                         "org.tizen.NetNfcService",
358                         G_BUS_NAME_OWNER_FLAGS_NONE,
359                         on_bus_acquired,
360                         on_name_acquired,
361                         on_name_lost,
362                         NULL,
363                         NULL);
364
365         loop = g_main_loop_new(NULL, FALSE);
366         g_main_loop_run(loop);
367
368 EXIT :
369         net_nfc_server_vconf_deinit();
370         net_nfc_server_controller_deinit();
371         net_nfc_server_gdbus_deinit();
372
373         if (id)
374                 g_bus_unown_name(id);
375
376         net_nfc_controller_unload(handle);
377
378         g_option_context_free(option_context);
379
380         return 0;
381 }