Remove build error in Tizen 3.0
[platform/core/connectivity/nfc-manager-neard.git] / client / net_nfc_client_manager.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
17 #include "net_nfc_debug_internal.h"
18
19 #include "net_nfc_gdbus.h"
20 #include "net_nfc_client.h"
21 #include "net_nfc_client_context.h"
22 #include "net_nfc_client_manager.h"
23 #include "net_nfc_neard.h"
24
25 #define DEACTIVATE_DELAY        500 /* ms */
26
27 typedef struct _ManagerFuncData ManagerFuncData;
28
29 struct _ManagerFuncData
30 {
31         gpointer callback;
32         gpointer user_data;
33         net_nfc_error_e result;
34 };
35
36 static NetNfcGDbusManager *manager_proxy = NULL;
37 static ManagerFuncData activated_func_data;
38 static int is_activated = -1;
39 static guint timeout_id[2];
40
41 static void manager_call_get_server_state_callback(GObject *source_object,
42                 GAsyncResult *res, gpointer user_data)
43 {
44         gboolean ret;
45         guint out_state = 0;
46         GError *error = NULL;
47         net_nfc_error_e result;
48         NetNfcCallback *func_data = user_data;
49
50         g_assert(user_data != NULL);
51
52         ret = net_nfc_gdbus_manager_call_get_server_state_finish(
53                         NET_NFC_GDBUS_MANAGER(source_object),
54                         &result,
55                         &out_state,
56                         res,
57                         &error);
58
59         if (FALSE == ret)
60         {
61                 NFC_ERR("Can not finish get_server_state: %s",
62                                 error->message);
63                 g_error_free(error);
64
65                 result = NET_NFC_IPC_FAIL;
66         }
67
68         if (func_data->callback != NULL)
69         {
70                 net_nfc_client_manager_get_server_state_completed callback =
71                         (net_nfc_client_manager_get_server_state_completed)func_data->callback;
72
73                 callback(result, out_state, func_data->user_data);
74         }
75
76         g_free(func_data);
77 }
78
79 static gboolean _activated_time_elapsed_callback(gpointer user_data)
80 {
81         net_nfc_client_manager_activated callback =
82                 (net_nfc_client_manager_activated)activated_func_data.callback;
83
84         timeout_id[1] = 0;
85
86         callback(is_activated, activated_func_data.user_data);
87
88         return false;
89 }
90
91 static void manager_activated(NetNfcGDbusManager *manager, gboolean activated,
92                 gpointer user_data)
93 {
94         NFC_INFO(">>> SIGNAL arrived : activated %d", activated);
95
96         /* update current state */
97         is_activated = (int)activated;
98
99         if (activated_func_data.callback != NULL)
100         {
101                 if (is_activated == false)
102                 {
103                         /* FIXME : wait several times */
104                         timeout_id[1] = g_timeout_add(DEACTIVATE_DELAY,
105                                         _activated_time_elapsed_callback, NULL);
106                 }
107                 else
108                 {
109                         g_main_context_invoke(NULL, _activated_time_elapsed_callback, NULL);
110                 }
111         }
112 }
113
114 API void net_nfc_client_manager_set_activated(
115                 net_nfc_client_manager_activated callback, void *user_data)
116 {
117         RET_IF(NULL == callback);
118
119         net_nfc_neard_set_activated(callback, user_data);
120 }
121
122 API void net_nfc_client_manager_unset_activated(void)
123 {
124         net_nfc_neard_unset_activated();
125 }
126
127 API net_nfc_error_e net_nfc_client_manager_set_active(int state,
128                 net_nfc_client_manager_set_active_completed callback, void *user_data)
129 {
130         return net_nfc_neard_set_active(state, callback, user_data);
131 }
132
133 API net_nfc_error_e net_nfc_client_manager_set_active_sync(int state)
134 {
135         gboolean ret;
136         GError *error = NULL;
137         net_nfc_error_e out_result = NET_NFC_OK;
138
139         RETV_IF(NULL == manager_proxy, NET_NFC_NOT_INITIALIZED);
140
141         /* allow this function even nfc is off */
142
143         ret = net_nfc_gdbus_manager_call_set_active_sync(manager_proxy,
144                         (gboolean)state,
145                         net_nfc_client_gdbus_get_privilege(),
146                         &out_result,
147                         NULL,
148                         &error);
149
150         if (FALSE == ret)
151         {
152                 NFC_ERR("can not call SetActive: %s", error->message);
153                 g_error_free(error);
154
155                 out_result = NET_NFC_IPC_FAIL;
156         }
157
158         return out_result;
159 }
160
161 API net_nfc_error_e net_nfc_client_manager_get_server_state(
162                 net_nfc_client_manager_get_server_state_completed callback, void *user_data)
163 {
164         NetNfcCallback *func_data;
165
166         RETV_IF(NULL == manager_proxy, NET_NFC_NOT_INITIALIZED);
167
168         /* prevent executing daemon when nfc is off */
169         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
170
171         func_data = g_try_new0(NetNfcCallback, 1);
172         if (func_data == NULL)
173                 return NET_NFC_ALLOC_FAIL;
174
175         func_data->callback = (gpointer) callback;
176         func_data->user_data = user_data;
177
178         net_nfc_gdbus_manager_call_get_server_state(manager_proxy,
179                         net_nfc_client_gdbus_get_privilege(),
180                         NULL,
181                         manager_call_get_server_state_callback,
182                         func_data);
183
184         return NET_NFC_OK;
185 }
186
187 API net_nfc_error_e net_nfc_client_manager_get_server_state_sync(
188                 unsigned int *state)
189 {
190         gboolean ret;
191         guint out_state = 0;
192         GError *error = NULL;
193         net_nfc_error_e out_result = NET_NFC_OK;
194
195         RETV_IF(NULL == state, NET_NFC_NULL_PARAMETER);
196         RETV_IF(NULL == manager_proxy, NET_NFC_NOT_INITIALIZED);
197
198         /* prevent executing daemon when nfc is off */
199         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
200
201         *state = 0;
202
203         ret = net_nfc_gdbus_manager_call_get_server_state_sync(manager_proxy,
204                         net_nfc_client_gdbus_get_privilege(),
205                         &out_result,
206                         &out_state,
207                         NULL,
208                         &error);
209
210         if (TRUE == ret)
211         {
212                 *state = out_state;
213         }
214         else
215         {
216                 NFC_ERR("can not call GetServerState: %s", error->message);
217                 g_error_free(error);
218
219                 out_result = NET_NFC_IPC_FAIL;
220         }
221
222         return out_result;
223 }
224
225 net_nfc_error_e net_nfc_client_manager_init(void)
226 {
227         GError *error = NULL;
228
229         if (manager_proxy)
230         {
231                 NFC_WARN("Already initialized");
232                 return NET_NFC_OK;
233         }
234
235         manager_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
236                         G_BUS_TYPE_SYSTEM,
237                         G_DBUS_PROXY_FLAGS_NONE,
238                         "org.tizen.NetNfcService",
239                         "/org/tizen/NetNfcService/Manager",
240                         NULL,
241                         &error);
242
243         if (NULL == manager_proxy)
244         {
245                 NFC_ERR("Can not create proxy : %s", error->message);
246                 g_error_free(error);
247
248                 return NET_NFC_UNKNOWN_ERROR;
249         }
250
251         g_signal_connect(manager_proxy, "activated", G_CALLBACK(manager_activated), NULL);
252
253         return NET_NFC_OK;
254 }
255
256 void net_nfc_client_manager_deinit(void)
257 {
258         if (manager_proxy)
259         {
260                 int i;
261
262                 for (i = 0; i < 2; i++)
263                 {
264                         if (timeout_id[i] > 0)
265                         {
266                                 g_source_remove(timeout_id[i]);
267                                 timeout_id[i] = 0;
268                         }
269                 }
270
271                 g_object_unref(manager_proxy);
272                 manager_proxy = NULL;
273         }
274 }
275
276 /* internal function */
277 bool net_nfc_client_manager_is_activated()
278 {
279         if (is_activated < 0)
280                 net_nfc_client_get_nfc_state(&is_activated);
281
282         return is_activated;
283 }