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