[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / net_nfc_client_manager.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
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_util_internal.h"
22 #include "net_nfc_client_context.h"
23 #include "net_nfc_client_manager.h"
24
25 #ifndef NET_NFC_EXPORT_API
26 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
27 #endif
28
29 #define DEACTIVATE_DELAY        500 /* ms */
30 #define ACTIVATE_DELAY 100 /* ms */
31
32 typedef struct _ManagerFuncData ManagerFuncData;
33
34 struct _ManagerFuncData
35 {
36         gpointer callback;
37         gpointer user_data;
38         net_nfc_error_e result;
39 };
40
41 static NetNfcGDbusManager *manager_proxy = NULL;
42 static NetNfcGDbusManager *auto_start_proxy = NULL;
43 static ManagerFuncData activated_func_data;
44 static int is_activated = -1;
45 static guint timeout_id[2];
46
47 static void manager_call_set_active_callback(GObject *source_object,
48                                         GAsyncResult *res,
49                                         gpointer user_data);
50
51 static void manager_call_get_server_state_callback(GObject *source_object,
52                                                 GAsyncResult *res,
53                                                 gpointer user_data);
54
55
56 static void manager_activated(NetNfcGDbusManager *manager,
57                                 gboolean activated,
58                                 gpointer user_data);
59
60
61 static gboolean _set_activate_time_elapsed_callback(gpointer user_data)
62 {
63         ManagerFuncData *func_data = (ManagerFuncData *)user_data;
64         net_nfc_client_manager_set_active_completed callback;
65
66         if (timeout_id[0] > 0) {
67                 g_assert(func_data != NULL);
68
69                 g_source_remove(timeout_id[0]);
70                 timeout_id[0] = 0;
71
72                 callback = (net_nfc_client_manager_set_active_completed)func_data->callback;
73
74                 callback(func_data->result, func_data->user_data);
75
76                 g_free(func_data);
77         }
78
79         return false;
80 }
81
82 static void manager_call_set_active_callback(GObject *source_object,
83                                         GAsyncResult *res,
84                                         gpointer user_data)
85 {
86         ManagerFuncData *func_data = (ManagerFuncData *)user_data;
87         net_nfc_error_e result;
88         GError *error = NULL;
89
90         g_assert(user_data != NULL);
91
92         if (net_nfc_gdbus_manager_call_set_active_finish(
93                                 NET_NFC_GDBUS_MANAGER(source_object),
94                                 &result,
95                                 res,
96                                 &error) == FALSE)
97         {
98                 DEBUG_ERR_MSG("Can not finish call_set_active: %s",
99                         error->message);
100                 result = NET_NFC_IPC_FAIL;
101
102                 g_error_free(error);
103         }
104
105         func_data->result = result;
106         net_nfc_client_get_nfc_state(&is_activated);
107
108         if (is_activated == false) {
109                 /* FIXME : wait several times */
110                 timeout_id[0] = g_timeout_add(DEACTIVATE_DELAY,
111                         _set_activate_time_elapsed_callback,
112                         func_data);
113         } else {
114                 timeout_id[0] = g_timeout_add(ACTIVATE_DELAY,
115                         _set_activate_time_elapsed_callback,
116                         func_data);
117         }
118 }
119
120 static void manager_call_get_server_state_callback(GObject *source_object,
121                                                 GAsyncResult *res,
122                                                 gpointer user_data)
123 {
124         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
125         net_nfc_error_e result;
126         guint out_state = 0;
127         GError *error = NULL;
128
129         g_assert(user_data != NULL);
130
131         if (net_nfc_gdbus_manager_call_get_server_state_finish(
132                                 NET_NFC_GDBUS_MANAGER(source_object),
133                                 &result,
134                                 &out_state,
135                                 res,
136                                 &error) == FALSE)
137         {
138                 DEBUG_ERR_MSG("Can not finish get_server_state: %s",
139                         error->message);
140                 result = NET_NFC_IPC_FAIL;
141
142                 g_error_free(error);
143         }
144
145         if (func_data->callback != NULL)
146         {
147                 net_nfc_client_manager_get_server_state_completed callback =
148                         (net_nfc_client_manager_get_server_state_completed)func_data->callback;
149
150                 callback(result, out_state, func_data->user_data);
151         }
152
153         g_free(func_data);
154 }
155
156 static gboolean _activated_time_elapsed_callback(gpointer user_data)
157 {
158         net_nfc_client_manager_activated callback =
159                 (net_nfc_client_manager_activated)activated_func_data.callback;
160
161         if (timeout_id[1] > 0) {
162                 g_source_remove(timeout_id[1]);
163                 timeout_id[1] = 0;
164
165                 callback(is_activated, activated_func_data.user_data);
166         }
167
168         return false;
169 }
170
171 static void manager_activated(NetNfcGDbusManager *manager,
172                                 gboolean activated,
173                                 gpointer user_data)
174 {
175         INFO_MSG(">>> SIGNAL arrived");
176         DEBUG_CLIENT_MSG("activated %d", activated);
177
178         /* update current state */
179         is_activated = (int)activated;
180
181         if (activated_func_data.callback != NULL)
182         {
183                 if (is_activated == false) {
184                         /* FIXME : wait several times */
185                         timeout_id[1] = g_timeout_add(DEACTIVATE_DELAY,
186                                 _activated_time_elapsed_callback,
187                                 NULL);
188                 } else {
189                         timeout_id[1] = g_timeout_add(ACTIVATE_DELAY,
190                                 _activated_time_elapsed_callback,
191                                 NULL);
192                 }
193         }
194 }
195
196 NET_NFC_EXPORT_API
197 void net_nfc_client_manager_set_activated(
198                         net_nfc_client_manager_activated callback,
199                         void *user_data)
200 {
201         if (callback == NULL)
202                 return;
203
204         if (manager_proxy == NULL)
205         {
206                 if (net_nfc_client_manager_init() != NET_NFC_OK)
207                 {
208                         DEBUG_ERR_MSG("manager_proxy fail");
209                         /* FIXME : return result of this error */
210                         return;
211                 }
212         }
213
214         activated_func_data.callback = callback;
215         activated_func_data.user_data = user_data;
216 }
217
218 NET_NFC_EXPORT_API
219 void net_nfc_client_manager_unset_activated(void)
220 {
221         activated_func_data.callback = NULL;
222         activated_func_data.user_data = NULL;
223 }
224
225 NET_NFC_EXPORT_API
226 net_nfc_error_e net_nfc_client_manager_set_active(int state,
227                         net_nfc_client_manager_set_active_completed callback,
228                         void *user_data)
229 {
230         gboolean active = FALSE;
231         ManagerFuncData *func_data;
232
233         if (auto_start_proxy == NULL) {
234                 GError *error = NULL;
235
236                 auto_start_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
237                         G_BUS_TYPE_SYSTEM,
238                         G_DBUS_PROXY_FLAGS_NONE,
239                         "org.tizen.NetNfcService",
240                         "/org/tizen/NetNfcService/Manager",
241                         NULL,
242                         &error);
243                 if (auto_start_proxy == NULL)
244                 {
245                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
246                         g_error_free(error);
247
248                         return NET_NFC_UNKNOWN_ERROR;
249                 }
250         }
251
252         /* allow this function even nfc is off */
253
254         func_data = g_try_new0(ManagerFuncData, 1);
255         if (func_data == NULL)
256                 return NET_NFC_ALLOC_FAIL;
257
258         func_data->callback = (gpointer)callback;
259         func_data->user_data = user_data;
260
261         if (state == true)
262                 active = TRUE;
263
264         net_nfc_gdbus_manager_call_set_active(auto_start_proxy,
265                 active,
266                 NULL,
267                 manager_call_set_active_callback,
268                 func_data);
269
270         return NET_NFC_OK;
271 }
272
273 NET_NFC_EXPORT_API
274 net_nfc_error_e net_nfc_client_manager_set_active_sync(int state)
275 {
276         net_nfc_error_e out_result = NET_NFC_OK;
277         GError *error = NULL;
278
279         if (auto_start_proxy == NULL) {
280                 GError *error = NULL;
281
282                 auto_start_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
283                         G_BUS_TYPE_SYSTEM,
284                         G_DBUS_PROXY_FLAGS_NONE,
285                         "org.tizen.NetNfcService",
286                         "/org/tizen/NetNfcService/Manager",
287                         NULL,
288                         &error);
289                 if (auto_start_proxy == NULL)
290                 {
291                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
292                         g_error_free(error);
293
294                         return NET_NFC_UNKNOWN_ERROR;
295                 }
296         }
297
298         /* allow this function even nfc is off */
299
300         if (net_nfc_gdbus_manager_call_set_active_sync(auto_start_proxy,
301                 (gboolean)state,
302                 &out_result,
303                 NULL,
304                 &error) == FALSE)
305         {
306                 DEBUG_ERR_MSG("can not call SetActive: %s",
307                                 error->message);
308                 out_result = NET_NFC_IPC_FAIL;
309
310                 g_error_free(error);
311         }
312
313         return out_result;
314 }
315
316 NET_NFC_EXPORT_API
317 net_nfc_error_e net_nfc_client_manager_get_server_state(
318                 net_nfc_client_manager_get_server_state_completed callback,
319                 void *user_data)
320 {
321         NetNfcCallback *func_data;
322
323         if (manager_proxy == NULL)
324                 return NET_NFC_NOT_INITIALIZED;
325
326         /* prevent executing daemon when nfc is off */
327         if (net_nfc_client_manager_is_activated() == false) {
328                 return NET_NFC_NOT_ACTIVATED;
329         }
330
331         func_data = g_try_new0(NetNfcCallback, 1);
332         if (func_data == NULL)
333                 return NET_NFC_ALLOC_FAIL;
334
335         func_data->callback = (gpointer) callback;
336         func_data->user_data = user_data;
337
338         net_nfc_gdbus_manager_call_get_server_state(manager_proxy,
339                                         NULL,
340                                         manager_call_get_server_state_callback,
341                                         func_data);
342
343         return NET_NFC_OK;
344 }
345
346 NET_NFC_EXPORT_API
347 net_nfc_error_e net_nfc_client_manager_get_server_state_sync(
348                                                         unsigned int *state)
349 {
350         net_nfc_error_e out_result = NET_NFC_OK;
351         guint out_state = 0;
352         GError *error = NULL;
353
354         if (state == NULL)
355                 return NET_NFC_NULL_PARAMETER;
356
357         *state = 0;
358
359         if (manager_proxy == NULL)
360                 return NET_NFC_NOT_INITIALIZED;
361
362         /* prevent executing daemon when nfc is off */
363         if (net_nfc_client_manager_is_activated() == false) {
364                 return NET_NFC_NOT_ACTIVATED;
365         }
366
367         if (net_nfc_gdbus_manager_call_get_server_state_sync(manager_proxy,
368                                         &out_result,
369                                         &out_state,
370                                         NULL,
371                                         &error) == TRUE)
372         {
373                 *state = out_state;
374         }
375         else
376         {
377                 DEBUG_ERR_MSG("can not call GetServerState: %s",
378                                 error->message);
379                 out_result = NET_NFC_IPC_FAIL;
380
381                 g_error_free(error);
382         }
383
384         return out_result;
385
386 }
387
388 net_nfc_error_e net_nfc_client_manager_init(void)
389 {
390         GError *error = NULL;
391
392         if (manager_proxy)
393         {
394                 DEBUG_CLIENT_MSG("Already initialized");
395
396                 return NET_NFC_OK;
397         }
398
399         manager_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
400                 G_BUS_TYPE_SYSTEM,
401                 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
402                 "org.tizen.NetNfcService",
403                 "/org/tizen/NetNfcService/Manager",
404                 NULL,
405                 &error);
406         if (manager_proxy == NULL)
407         {
408                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
409                 g_error_free(error);
410
411                 return NET_NFC_UNKNOWN_ERROR;
412         }
413
414         g_signal_connect(manager_proxy, "activated",
415                         G_CALLBACK(manager_activated), NULL);
416
417         return NET_NFC_OK;
418 }
419
420 void net_nfc_client_manager_deinit(void)
421 {
422         if (manager_proxy)
423         {
424                 int i;
425
426                 for (i = 0; i < 2; i++) {
427                         if (timeout_id[i] > 0) {
428                                 g_source_remove(timeout_id[i]);
429                                 timeout_id[i] = 0;
430                         }
431                 }
432
433                 g_object_unref(manager_proxy);
434                 manager_proxy = NULL;
435         }
436
437         if (auto_start_proxy) {
438                 g_object_unref(auto_start_proxy);
439                 auto_start_proxy = NULL;
440         }
441 }
442
443 /* internal function */
444 bool net_nfc_client_manager_is_activated()
445 {
446         if (is_activated < 0) {
447                 net_nfc_client_get_nfc_state(&is_activated);
448         }
449
450         return is_activated;
451 }