[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / net_nfc_client_tag.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_typedef_internal.h"
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_util_gdbus_internal.h"
20 #include "net_nfc_util_internal.h"
21 #include "net_nfc_gdbus.h"
22 #include "net_nfc_data.h"
23 #include "net_nfc_target_info.h"
24 #include "net_nfc_client.h"
25 #include "net_nfc_client_util_internal.h"
26 #include "net_nfc_client_manager.h"
27 #include "net_nfc_client_tag.h"
28 #include "net_nfc_client_tag_internal.h"
29
30 #ifndef NET_NFC_EXPORT_API
31 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
32 #endif
33
34 static NetNfcGDbusTag *tag_proxy = NULL;
35
36 static NetNfcCallback tag_discovered_func_data;
37 static NetNfcCallback tag_detached_func_data;
38
39 static net_nfc_target_info_s *client_target_info = NULL;
40 static net_nfc_event_filter_e client_filter = NET_NFC_ALL_ENABLE;
41
42 static gboolean tag_check_filter(net_nfc_target_type_e type);
43
44 static void tag_get_info_list(guint8 *buffer,
45                         gint number_of_keys,
46                         net_nfc_tag_info_s **list);
47
48 static void tag_get_target_info(guint handle,
49                                 guint dev_type,
50                                 gboolean is_ndef_supported,
51                                 guchar ndef_card_state,
52                                 guint max_data_size,
53                                 guint actual_data_size,
54                                 guint number_of_keys,
55                                 GVariant *target_info_values,
56                                 GVariant *raw_data,
57                                 net_nfc_target_info_s **info);
58
59 #if 0
60 /* async callback */
61 static void tag_is_tag_connected(GObject *source_object,
62                                 GAsyncResult *res,
63                                 gpointer user_data);
64
65 static void tag_get_current_tag_info(GObject *source_object,
66                                 GAsyncResult *res,
67                                 gpointer user_data);
68
69 static void tag_get_current_target_handle(GObject *source_object,
70                                         GAsyncResult *res,
71                                         gpointer user_data);
72 #endif
73 /* signal callback */
74 static void tag_tag_discovered(NetNfcGDbusTag *object,
75                         guint arg_handle,
76                         gint arg_dev_type,
77                         gboolean arg_is_ndef_supported,
78                         guchar arg_ndef_card_state,
79                         guint arg_max_data_size,
80                         guint arg_actual_data_size,
81                         guint arg_number_of_keys,
82                         GVariant *arg_target_info_values,
83                         GVariant *arg_raw_data,
84                         gpointer user_data);
85
86 static void tag_tag_detached(NetNfcGDbusTag *object,
87                         guint arg_handle,
88                         gint arg_dev_type,
89                         gpointer user_data);
90
91 static gboolean tag_check_filter(net_nfc_target_type_e type)
92 {
93         net_nfc_event_filter_e converted = NET_NFC_ALL_ENABLE;
94
95         DEBUG_CLIENT_MSG("client filter =  %d", client_filter);
96
97         if (type >= NET_NFC_ISO14443_A_PICC &&
98                 type <= NET_NFC_MIFARE_DESFIRE_PICC)
99         {
100                 converted = NET_NFC_ISO14443A_ENABLE;
101         }
102         else if (type >= NET_NFC_ISO14443_B_PICC &&
103                 type <= NET_NFC_ISO14443_BPRIME_PICC)
104         {
105                 converted = NET_NFC_ISO14443B_ENABLE;
106         }
107         else if (type == NET_NFC_FELICA_PICC)
108         {
109                 converted = NET_NFC_FELICA_ENABLE;
110         }
111         else if (type == NET_NFC_JEWEL_PICC)
112         {
113                 converted = NET_NFC_FELICA_ENABLE;
114         }
115         else if (type == NET_NFC_ISO15693_PICC)
116         {
117                 converted = NET_NFC_ISO15693_ENABLE;
118         }
119
120         if ((converted & client_filter) == 0)
121                 return FALSE;
122
123         return TRUE;
124 }
125
126 static void tag_get_info_list(guint8 *buffer,
127                         gint number_of_keys,
128                         net_nfc_tag_info_s **list)
129 {
130         net_nfc_tag_info_s *tmp_list = NULL;
131         net_nfc_tag_info_s *current = NULL;
132
133         gint i = 0;
134         gint length;
135
136         guint8 *pos = buffer;
137
138         if (buffer == NULL)
139                 return;
140
141         tmp_list = g_new0(net_nfc_tag_info_s, number_of_keys);
142         current = tmp_list;
143
144         while (i < number_of_keys)
145         {
146                 gchar *str = NULL;
147                 data_h value = NULL;
148
149                 /* key */
150                 length = *pos;  /* first values is length of key */
151                 pos++;
152
153                 str = g_new0(gchar, length + 1);
154                 memcpy(str, pos, length);
155
156                 SECURE_MSG("key = [%s]", str);
157
158                 pos += length;
159
160                 current->key = str;
161
162                 /* value */
163                 length = *pos; /* first value is length of value */
164                 pos++;
165
166                 value = NULL;
167                 if (length > 0)
168                 {
169                         net_nfc_create_data(&value, pos, length);
170                         pos += length;
171                 }
172
173                 current->value = value;
174
175                 current++;
176                 i++;
177         }
178
179         *list = tmp_list;
180 }
181
182 static void tag_get_target_info(guint handle,
183                                 guint dev_type,
184                                 gboolean is_ndef_supported,
185                                 guchar ndef_card_state,
186                                 guint max_data_size,
187                                 guint actual_data_size,
188                                 guint number_of_keys,
189                                 GVariant *target_info_values,
190                                 GVariant *raw_data,
191                                 net_nfc_target_info_s **info)
192 {
193         guint8 *buffer = NULL;
194         net_nfc_target_info_s *info_data = NULL;
195         net_nfc_tag_info_s *list = NULL;
196
197         if (info == NULL)
198                 return;
199
200         net_nfc_util_gdbus_variant_to_buffer(target_info_values,
201                         &buffer, NULL);
202
203         tag_get_info_list(buffer, number_of_keys, &list);
204
205         info_data = g_new0(net_nfc_target_info_s, 1);
206
207         info_data->ndefCardState = ndef_card_state;
208         info_data->actualDataSize = actual_data_size;
209         info_data->maxDataSize = max_data_size;
210         info_data->devType = dev_type;
211         info_data->handle = GUINT_TO_POINTER(handle);
212         info_data->is_ndef_supported = (uint8_t)is_ndef_supported;
213         info_data->number_of_keys = number_of_keys;
214         info_data->tag_info_list = list;
215
216         net_nfc_util_gdbus_variant_to_data_s(raw_data,
217                         &info_data->raw_data);
218
219         *info = info_data;
220 }
221 #if 0
222 static void tag_is_tag_connected(GObject *source_object,
223                                 GAsyncResult *res,
224                                 gpointer user_data)
225 {
226         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
227         net_nfc_error_e out_result;
228         gboolean out_is_connected = false;
229         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
230         GError *error = NULL;
231
232         g_assert(user_data != NULL);
233
234         if (net_nfc_gdbus_tag_call_is_tag_connected_finish(
235                                 NET_NFC_GDBUS_TAG(source_object),
236                                 &out_result,
237                                 &out_is_connected,
238                                 (gint32 *)&out_dev_type,
239                                 res,
240                                 &error) == FALSE)
241         {
242                 DEBUG_ERR_MSG("Can not finish is_tag_connected: %s",
243                                 error->message);
244                 g_error_free(error);
245
246                 out_result = NET_NFC_IPC_FAIL;
247         }
248
249         if (func_data->callback != NULL)
250         {
251                 net_nfc_client_tag_is_tag_connected_completed callback =
252                         (net_nfc_client_tag_is_tag_connected_completed)func_data->callback;
253
254                 if (out_is_connected == FALSE)
255                         out_result = NET_NFC_NOT_CONNECTED;
256
257                 callback(out_result, out_dev_type, func_data->user_data);
258         }
259
260         g_free(func_data);
261 }
262
263 static void tag_get_current_tag_info(GObject *source_object,
264                                 GAsyncResult *res,
265                                 gpointer user_data)
266 {
267         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
268         net_nfc_error_e out_result = NET_NFC_OK;
269
270         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
271         gboolean out_is_connected = FALSE;
272         gboolean out_is_ndef_supported = FALSE;
273         guchar out_ndef_card_state = 0;
274         guint out_handle = 0;
275         guint out_max_data_size = 0;
276         guint out_actual_data_size = 0;
277         guint out_number_of_keys = 0;
278         GVariant *out_target_info_values = NULL;
279         GVariant *out_raw_data = NULL;
280
281         GError *error = NULL;
282
283         g_assert(user_data != NULL);
284
285         if (net_nfc_gdbus_tag_call_get_current_tag_info_finish (
286                                         NET_NFC_GDBUS_TAG(source_object),
287                                         &out_result,
288                                         &out_is_connected,
289                                         &out_handle,
290                                         (gint *)&out_dev_type,
291                                         &out_is_ndef_supported,
292                                         &out_ndef_card_state,
293                                         &out_max_data_size,
294                                         &out_actual_data_size,
295                                         &out_number_of_keys,
296                                         &out_target_info_values,
297                                         &out_raw_data,
298                                         res,
299                                         &error) == FALSE)
300         {
301                 out_result = NET_NFC_IPC_FAIL;
302
303                 DEBUG_ERR_MSG("Can not finish get_current_tag_info: %s",
304                                 error->message);
305                 g_error_free(error);
306         }
307
308         if (out_result == NET_NFC_OK && out_is_connected == true) {
309                 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
310                 client_target_info = NULL;
311
312                 if (tag_check_filter(out_dev_type) == true) {
313                         tag_get_target_info(out_handle,
314                                         out_dev_type,
315                                         out_is_ndef_supported,
316                                         out_ndef_card_state,
317                                         out_max_data_size,
318                                         out_actual_data_size,
319                                         out_number_of_keys,
320                                         out_target_info_values,
321                                         out_raw_data,
322                                         &client_target_info);
323                 } else {
324                         INFO_MSG("The detected target is filtered out, type [%d]", out_dev_type);
325
326                         out_is_connected = false;
327                 }
328         }
329
330         if (func_data->callback != NULL)
331         {
332                 net_nfc_client_tag_get_current_tag_info_completed callback =
333                         (net_nfc_client_tag_get_current_tag_info_completed)func_data->callback;
334
335                 if (out_result == NET_NFC_OK && out_is_connected == false)
336                         out_result = NET_NFC_NOT_CONNECTED;
337
338                 callback(out_result, client_target_info, func_data->user_data);
339         }
340
341         g_free(func_data);
342 }
343
344 static void tag_get_current_target_handle(GObject *source_object,
345                                         GAsyncResult *res,
346                                         gpointer user_data)
347 {
348         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
349         net_nfc_error_e out_result = NET_NFC_OK;
350         net_nfc_target_handle_h out_handle = NULL;
351         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
352         gboolean out_is_connected = false;
353         GError *error = NULL;
354
355         g_assert(user_data != NULL);
356
357         if (net_nfc_gdbus_tag_call_get_current_target_handle_finish(
358                                         NET_NFC_GDBUS_TAG(source_object),
359                                         &out_result,
360                                         &out_is_connected,
361                                         (guint *)&out_handle,
362                                         (gint *)&out_dev_type,
363                                         res,
364                                         &error) == FALSE)
365         {
366                 DEBUG_ERR_MSG("Can not finish get_current_target_handle: %s",
367                                 error->message);
368                 g_error_free(error);
369
370                 out_result = NET_NFC_IPC_FAIL;
371         }
372
373         if (func_data->callback != NULL)
374         {
375                 net_nfc_client_tag_get_current_target_handle_completed callback =
376                         (net_nfc_client_tag_get_current_target_handle_completed)func_data->callback;
377
378                 if (out_result == NET_NFC_OK && out_is_connected == FALSE)
379                         out_result = NET_NFC_NOT_CONNECTED;
380
381                 callback(out_result,
382                         GUINT_TO_POINTER(out_handle),
383                         func_data->user_data);
384         }
385
386         g_free(func_data);
387 }
388 #endif
389 static void tag_tag_discovered(NetNfcGDbusTag *object,
390                         guint arg_handle,
391                         gint arg_dev_type,
392                         gboolean arg_is_ndef_supported,
393                         guchar arg_ndef_card_state,
394                         guint arg_max_data_size,
395                         guint arg_actual_data_size,
396                         guint arg_number_of_keys,
397                         GVariant *arg_target_info_values,
398                         GVariant *arg_raw_data,
399                         gpointer user_data)
400 {
401         INFO_MSG(">>> SIGNAL arrived");
402
403         net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
404         client_target_info = NULL;
405
406         if (tag_check_filter(arg_dev_type) == FALSE) {
407                 DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
408
409                 return;
410         }
411
412         tag_get_target_info(arg_handle,
413                         arg_dev_type,
414                         arg_is_ndef_supported,
415                         arg_ndef_card_state,
416                         arg_max_data_size,
417                         arg_actual_data_size,
418                         arg_number_of_keys,
419                         arg_target_info_values,
420                         arg_raw_data,
421                         &client_target_info);
422
423         if (tag_discovered_func_data.callback != NULL) {
424                 net_nfc_client_tag_tag_discovered callback =
425                         (net_nfc_client_tag_tag_discovered)tag_discovered_func_data.callback;
426
427                 callback(client_target_info,
428                         tag_discovered_func_data.user_data);
429         }
430 }
431
432 static void tag_tag_detached(NetNfcGDbusTag *object,
433                         guint arg_handle,
434                         gint arg_dev_type,
435                         gpointer user_data)
436 {
437         INFO_MSG(">>> SIGNAL arrived");
438
439         if (tag_check_filter(arg_dev_type) == TRUE) {
440                 if (tag_detached_func_data.callback != NULL) {
441                         net_nfc_client_tag_tag_detached callback =
442                                 (net_nfc_client_tag_tag_detached)tag_detached_func_data.callback;
443
444                         callback(tag_detached_func_data.user_data);
445                 }
446         } else {
447                 DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
448         }
449
450         net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
451         client_target_info = NULL;
452 }
453
454 /* internal function */
455 gboolean net_nfc_client_tag_is_connected(void)
456 {
457         if (client_target_info == NULL)
458                 return FALSE;
459
460         if (client_target_info->handle == NULL)
461                 return FALSE;
462
463         return TRUE;
464 }
465
466 net_nfc_target_info_s *net_nfc_client_tag_get_client_target_info(void)
467 {
468         return client_target_info;
469 }
470
471 NET_NFC_EXPORT_API
472 net_nfc_error_e net_nfc_client_tag_is_tag_connected_sync(
473                                         net_nfc_target_type_e *dev_type)
474 {
475         net_nfc_target_info_s *info;
476         net_nfc_error_e result = NET_NFC_OK;
477         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
478         gboolean out_is_connected = false;
479         GError *error = NULL;
480
481         if (tag_proxy == NULL)
482                 return NET_NFC_NOT_INITIALIZED;
483
484         /* prevent executing daemon when nfc is off */
485         if (net_nfc_client_manager_is_activated() == false) {
486                 return NET_NFC_NOT_ACTIVATED;
487         }
488
489         info = net_nfc_client_tag_get_client_target_info();
490         if (info == NULL) {
491                 /* try to request target information from server */
492                 if (net_nfc_gdbus_tag_call_is_tag_connected_sync(tag_proxy,
493                                         &result,
494                                         &out_is_connected,
495                                         (gint *)&out_dev_type,
496                                         NULL,
497                                         &error) == FALSE)
498                 {
499                         DEBUG_ERR_MSG("Can not get is_tag_connected result: %s",
500                                         error->message);
501                         result = NET_NFC_IPC_FAIL;
502
503                         g_error_free(error);
504
505                         return result;
506                 }
507
508                 if (out_is_connected == true) {
509                         if (dev_type)
510                                 *dev_type = out_dev_type;
511
512                         result = NET_NFC_OK;
513                 } else {
514                         result = NET_NFC_NOT_CONNECTED;
515                 }
516         } else {
517                 /* target was connected */
518                 if (dev_type != NULL) {
519                         *dev_type = info->devType;
520                 }
521
522                 result = NET_NFC_OK;
523         }
524
525         return result;
526 }
527
528 NET_NFC_EXPORT_API
529 net_nfc_error_e net_nfc_client_barcode_get_barcode_sync(
530                                                 unsigned char **barcode, int* barcode_len)
531 {
532         data_s *data;
533         GError *error = NULL;
534         GVariant *out_barcode = NULL;
535         net_nfc_error_e result = NET_NFC_OK;
536
537         if (tag_proxy == NULL)
538                 return NET_NFC_NOT_INITIALIZED;
539
540         /* prevent executing daemon when nfc is off */
541         if (net_nfc_client_manager_is_activated() == false) {
542                 return NET_NFC_NOT_ACTIVATED;
543         }
544
545         if (net_nfc_gdbus_tag_call_get_barcode_sync(tag_proxy,
546                                 &result,
547                                 &out_barcode,
548                                 NULL,
549                                 &error) == FALSE)
550         {
551                 DEBUG_ERR_MSG("Can no get barcode result: %s", error->message);
552                 result = NET_NFC_IPC_FAIL;
553
554                 g_error_free(error);
555
556                 return result;
557         }
558
559         if(result == NET_NFC_OK)
560         {
561                 data = g_new0(data_s, 1);
562                 net_nfc_util_gdbus_variant_to_data_s(out_barcode, data);
563
564                 if(data == NULL || data->buffer == NULL || data->length == 0)
565                         return NET_NFC_OPERATION_FAIL;
566
567                 //alloc barcode
568                 *barcode_len = data->length;
569                 *barcode = (unsigned char *)calloc(*barcode_len, sizeof(unsigned char));
570                 memcpy(*barcode, data->buffer, *barcode_len);
571
572                 if (data != NULL) {
573                         net_nfc_util_free_data(data);
574                 }
575         }
576
577         return result;
578 }
579
580 NET_NFC_EXPORT_API
581 net_nfc_error_e net_nfc_client_tag_get_current_tag_info_sync(
582                                                 net_nfc_target_info_h *info)
583 {
584         net_nfc_error_e result = NET_NFC_OK;
585         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
586         gboolean out_is_connected = false;
587         gboolean out_is_ndef_supported = false;
588         guchar out_ndef_card_state = 0;
589         guint out_handle = 0;
590         guint out_max_data_size = 0;
591         guint out_actual_data_size = 0;
592         guint out_number_of_keys = 0;
593         GVariant *out_target_info_values = NULL;
594         GVariant *out_raw_data = NULL;
595         GError *error = NULL;
596
597         if (tag_proxy == NULL)
598                 return NET_NFC_NOT_INITIALIZED;
599
600         /* prevent executing daemon when nfc is off */
601         if (net_nfc_client_manager_is_activated() == false) {
602                 return NET_NFC_NOT_ACTIVATED;
603         }
604
605         if (net_nfc_client_tag_get_client_target_info() == NULL) {
606                 /* try to request target information from server */
607                 if (net_nfc_gdbus_tag_call_get_current_tag_info_sync(tag_proxy,
608                                         &result,
609                                         &out_is_connected,
610                                         &out_handle,
611                                         (gint *)&out_dev_type,
612                                         &out_is_ndef_supported,
613                                         &out_ndef_card_state,
614                                         &out_max_data_size,
615                                         &out_actual_data_size,
616                                         &out_number_of_keys,
617                                         &out_target_info_values,
618                                         &out_raw_data,
619                                         NULL,
620                                         &error) == FALSE)
621                 {
622                         DEBUG_ERR_MSG("Can no get current_tag_info result: %s",
623                                         error->message);
624                         result = NET_NFC_IPC_FAIL;
625
626                         g_error_free(error);
627
628                         return result;
629                 }
630
631                 if (result == NET_NFC_OK) {
632                         if (out_is_connected == true) {
633                                 if (tag_check_filter(out_dev_type) == true) {
634                                         tag_get_target_info(out_handle,
635                                                         out_dev_type,
636                                                         out_is_ndef_supported,
637                                                         out_ndef_card_state,
638                                                         out_max_data_size,
639                                                         out_actual_data_size,
640                                                         out_number_of_keys,
641                                                         out_target_info_values,
642                                                         out_raw_data,
643                                                         &client_target_info);
644
645                                         result = NET_NFC_OK;
646                                 } else {
647                                         DEBUG_CLIENT_MSG("The detected target is filtered out");
648
649                                         result = NET_NFC_NOT_CONNECTED;
650                                 }
651                         } else {
652                                 result = NET_NFC_NOT_CONNECTED;
653                         }
654                 }
655         } else {
656                 result = NET_NFC_OK;
657         }
658
659         if (result == NET_NFC_OK && info != NULL) {
660                 *info = client_target_info;
661         }
662
663         return result;
664 }
665
666 NET_NFC_EXPORT_API
667 net_nfc_error_e net_nfc_client_tag_get_current_target_handle_sync(
668                                         net_nfc_target_handle_h *handle)
669 {
670         net_nfc_target_info_s *info;
671         net_nfc_error_e result;
672         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
673         gboolean out_is_connected = false;
674         guint out_handle = 0;
675         GError *error = NULL;
676
677         if (tag_proxy == NULL)
678                 return NET_NFC_NOT_INITIALIZED;
679
680         /* prevent executing daemon when nfc is off */
681         if (net_nfc_client_manager_is_activated() == false) {
682                 return NET_NFC_NOT_ACTIVATED;
683         }
684
685         info = net_nfc_client_tag_get_client_target_info();
686         if (info == NULL) {
687                 if (net_nfc_gdbus_tag_call_get_current_target_handle_sync(
688                         tag_proxy,
689                         &result,
690                         &out_is_connected,
691                         &out_handle,
692                         (gint *)&out_dev_type,
693                         NULL,
694                         &error) == FALSE)
695                 {
696                         DEBUG_ERR_MSG("Can no get current_target_handle result: %s",
697                                         error->message);
698                         result = NET_NFC_IPC_FAIL;
699
700                         g_error_free(error);
701
702                         return result;
703                 }
704
705                 if (result == NET_NFC_OK) {
706                         if (out_is_connected == true) {
707                                 if (handle)
708                                         *handle = GUINT_TO_POINTER(out_handle);
709
710                                 result = NET_NFC_OK;
711                         } else {
712                                 result = NET_NFC_NOT_CONNECTED;
713                         }
714                 }
715         } else if (info->devType == NET_NFC_NFCIP1_INITIATOR ||
716                 info->devType == NET_NFC_NFCIP1_TARGET) {
717                 if (handle)
718                         *handle = info->handle;
719
720                 result = NET_NFC_OK;
721         } else {
722                 result = NET_NFC_NOT_CONNECTED;
723         }
724
725         return result;
726 }
727
728 NET_NFC_EXPORT_API
729 void net_nfc_client_tag_set_tag_discovered(
730                         net_nfc_client_tag_tag_discovered callback,
731                         void *user_data)
732 {
733         if (callback == NULL)
734                 return;
735
736         if (tag_proxy == NULL)
737         {
738                 if (net_nfc_client_tag_init() != NET_NFC_OK)
739                 {
740                         DEBUG_ERR_MSG("tag_proxy fail");
741                         /* FIXME : return result of this error */
742                         return;
743                 }
744         }
745
746         tag_discovered_func_data.callback = (gpointer)callback;
747         tag_discovered_func_data.user_data = user_data;
748 }
749
750 NET_NFC_EXPORT_API
751 void net_nfc_client_tag_unset_tag_discovered(void)
752 {
753         tag_discovered_func_data.callback = NULL;
754         tag_discovered_func_data.user_data = NULL;
755 }
756
757 NET_NFC_EXPORT_API
758 void net_nfc_client_tag_set_tag_detached(
759                         net_nfc_client_tag_tag_detached callback,
760                         void *user_data)
761 {
762         if (callback == NULL)
763                 return;
764
765         if (tag_proxy == NULL)
766         {
767                 if (net_nfc_client_tag_init() != NET_NFC_OK)
768                 {
769                         DEBUG_ERR_MSG("tag_proxy fail");
770                         /* FIXME : return result of this error */
771                         return;
772                 }
773         }
774
775         tag_detached_func_data.callback = (gpointer)callback;
776         tag_detached_func_data.user_data = user_data;
777 }
778
779 NET_NFC_EXPORT_API
780 void net_nfc_client_tag_unset_tag_detached(void)
781 {
782         tag_detached_func_data.callback = NULL;
783         tag_detached_func_data.user_data = NULL;
784 }
785
786 NET_NFC_EXPORT_API
787 void net_nfc_client_tag_set_filter(net_nfc_event_filter_e filter)
788 {
789         client_filter = filter;
790 }
791
792 NET_NFC_EXPORT_API
793 net_nfc_event_filter_e net_nfc_client_tag_get_filter(void)
794 {
795         return client_filter;
796 }
797
798 net_nfc_error_e net_nfc_client_tag_init(void)
799 {
800         GError *error = NULL;
801
802         if (tag_proxy)
803         {
804                 DEBUG_CLIENT_MSG("Alrady initialized");
805
806                 return NET_NFC_OK;
807         }
808
809         if (client_target_info)
810         {
811                 net_nfc_release_tag_info(
812                                 (net_nfc_target_info_h)client_target_info);
813                 client_target_info = NULL;
814         }
815
816         client_filter = NET_NFC_ALL_ENABLE;
817
818         tag_proxy = net_nfc_gdbus_tag_proxy_new_for_bus_sync(
819                                 G_BUS_TYPE_SYSTEM,
820                                 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
821                                 "org.tizen.NetNfcService",
822                                 "/org/tizen/NetNfcService/Tag",
823                                 NULL,
824                                 &error);
825         if (tag_proxy == NULL)
826         {
827                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
828                 g_error_free(error);
829
830                 return NET_NFC_UNKNOWN_ERROR;
831         }
832
833         g_signal_connect(tag_proxy, "tag-discovered",
834                         G_CALLBACK(tag_tag_discovered), NULL);
835
836         g_signal_connect(tag_proxy, "tag-detached",
837                         G_CALLBACK(tag_tag_detached), NULL);
838
839         return NET_NFC_OK;
840 }
841
842 void net_nfc_client_tag_deinit(void)
843 {
844         client_filter = NET_NFC_ALL_ENABLE;
845
846         net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
847         client_target_info = NULL;
848
849         net_nfc_client_tag_unset_tag_discovered();
850         net_nfc_client_tag_unset_tag_detached();
851
852         if (tag_proxy)
853         {
854                 g_object_unref(tag_proxy);
855                 tag_proxy = NULL;
856         }
857 }