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