Remove unnecessary setting
[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 Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
48 GOptionEntry option_entries[] = {
49         { "daemon", 'd', 0, G_OPTION_ARG_NONE, &use_daemon,
50                 "Use Daemon mode", NULL },
51         { NULL }
52 };
53
54 pid_t net_nfc_server_gdbus_get_pid(const char *name)
55 {
56         GVariant *_ret;
57         guint pid = 0;
58         GError *error = NULL;
59
60         _ret = g_dbus_connection_call_sync(connection,
61                         "org.freedesktop.DBus",
62                         "/org/freedesktop/DBus",
63                         "org.freedesktop.DBus",
64                         "GetConnectionUnixProcessID",
65                         g_variant_new("(s)", name),
66                         NULL,
67                         G_DBUS_CALL_FLAGS_NONE,
68                         -1,
69                         NULL,
70                         &error);
71
72         if (_ret != NULL)
73         {
74                 g_variant_get(_ret, "(u)", &pid);
75                 g_variant_unref(_ret);
76         }
77
78         return pid;
79 }
80
81 void net_nfc_manager_quit()
82 {
83         NFC_DBG("net_nfc_manager_quit kill the nfc-manager daemon!!");
84
85         if (loop != NULL)
86                 g_main_loop_quit(loop);
87 }
88
89 static void _adapter_property_changed_cb(char *name, char *property,
90                                         void *value, void *user_data)
91 {
92         bool powered;
93
94         if (!g_strcmp0(property, "Powered")) {
95                 if ((int *) value == 0)
96                         powered = false;
97                 else
98                         powered = true;
99
100                 NFC_DBG("power changed %d", powered);
101                 if (vconf_set_bool(VCONFKEY_NFC_STATE, powered) != 0)
102                         NFC_DBG("vconf_set_bool failed ");
103         }
104 }
105
106 static bool net_nfc_neard_nfc_support(void)
107 {
108         char **adapters = NULL;
109         neardal_adapter *neard_adapter = NULL;
110         int len;
111         errorCode_t err;
112
113         NFC_INFO("checking nfc support");
114         err = neardal_get_adapters(&adapters, &len);
115         if (err != NEARDAL_SUCCESS)
116                 return false;
117
118         if (!(len > 0 && adapters != NULL))
119                 return false;
120
121         err = neardal_get_adapter_properties(adapters[0], &neard_adapter);
122         if (err == NEARDAL_SUCCESS && neard_adapter != NULL) {
123                 if (vconf_set_bool(VCONFKEY_NFC_STATE,
124                                 neard_adapter->powered) != 0)
125                         NFC_ERR("VCONFKEY_NFC_STATE set to %d failed",
126                                                 neard_adapter->powered);
127
128                 if (neardal_set_cb_adapter_property_changed(
129                         _adapter_property_changed_cb, NULL) != NEARDAL_SUCCESS)
130                         NFC_ERR("Failed to register property changed cb");
131         }
132
133         if (adapters)
134                 neardal_free_array(&adapters);
135
136         if (neard_adapter)
137                 neardal_free_adapter(neard_adapter);
138
139         adapters = NULL;
140         neard_adapter = NULL;
141
142         return true;
143 }
144
145 int main(int argc, char *argv[])
146 {
147         GError *error = NULL;
148         gboolean use_daemon = FALSE;
149         GOptionContext *option_context;
150
151         net_nfc_change_log_tag();
152
153         option_context = g_option_context_new("Nfc manager");
154         g_option_context_add_main_entries(option_context, option_entries, NULL);
155
156         if (g_option_context_parse(option_context, &argc, &argv, &error) == FALSE)
157         {
158                 NFC_ERR("can not parse option: %s", error->message);
159                 g_error_free(error);
160
161                 g_option_context_free(option_context);
162                 return 0;
163         }
164
165         NFC_DBG("start nfc manager");
166         NFC_INFO("use_daemon : %d", use_daemon);
167
168         net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
169
170         if (net_nfc_neard_nfc_support() == false)
171         {
172                 NFC_ERR("failed to detect NFC devices");
173
174                 if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_OFF) != 0)
175                         NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_OFF);
176
177                 goto EXIT;
178         }
179
180         if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_ON) != 0)
181                 NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_ON);
182
183         net_nfc_server_vconf_init();
184
185         loop = g_main_loop_new(NULL, FALSE);
186         g_main_loop_run(loop);
187
188 EXIT :
189         net_nfc_server_vconf_deinit();
190
191         g_option_context_free(option_context);
192
193         return 0;
194 }