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