Fix doxygen
[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_error_e net_nfc_get_target_info_from_device_type(net_nfc_target_handle_h handle, net_nfc_target_type_e device_type, net_nfc_target_info_s** target_info)
459 {
460         if (handle == NULL)
461                 return NET_NFC_NULL_PARAMETER;
462
463         if (net_nfc_client_tag_is_connected() == FALSE)
464                 return NET_NFC_OPERATION_FAIL;
465
466         *target_info = net_nfc_client_tag_get_client_target_info();
467         if (*target_info == NULL)
468                 return NET_NFC_NO_DATA_FOUND;
469
470         if ((*target_info)->devType != NET_NFC_FELICA_PICC) {
471                 DEBUG_CLIENT_MSG("only felica tag is available");
472                 return NET_NFC_NOT_ALLOWED_OPERATION;
473         }
474
475         return NET_NFC_OK;
476 }
477
478 net_nfc_error_e net_nfc_get_tag_info_value_from_key(net_nfc_target_info_s* target_info, const char* key, int valid_length, data_h* data)
479 {
480         if (net_nfc_get_tag_info_value((net_nfc_target_info_h)target_info,
481                 key,
482                 data) != NET_NFC_OK) {
483                 return NET_NFC_NO_DATA_FOUND;
484         }
485
486         if (((data_s*)*data)->length != valid_length)
487                 return NET_NFC_OUT_OF_BOUND;
488
489         return NET_NFC_OK;
490 }
491
492 NET_NFC_EXPORT_API
493 net_nfc_error_e net_nfc_client_tag_is_tag_connected_sync(
494                                         net_nfc_target_type_e *dev_type)
495 {
496         net_nfc_target_info_s *info;
497         net_nfc_error_e result = NET_NFC_OK;
498         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
499         gboolean out_is_connected = false;
500         GError *error = NULL;
501
502         if (tag_proxy == NULL)
503                 return NET_NFC_NOT_INITIALIZED;
504
505         /* prevent executing daemon when nfc is off */
506         if (net_nfc_client_manager_is_activated() == false)
507                 return NET_NFC_NOT_ACTIVATED;
508
509         info = net_nfc_client_tag_get_client_target_info();
510         if (info == NULL) {
511                 /* try to request target information from server */
512                 if (net_nfc_gdbus_tag_call_is_tag_connected_sync(tag_proxy,
513                                         &result,
514                                         &out_is_connected,
515                                         (gint *)&out_dev_type,
516                                         NULL,
517                                         &error) == FALSE) {
518                         if (error != NULL) {
519                                 DEBUG_ERR_MSG("Can not get is_tag_connected result: %s", error->message);
520                                 g_error_free(error);
521                         }
522
523                         result = NET_NFC_IPC_FAIL;
524
525                         return result;
526                 }
527
528                 if (out_is_connected == true) {
529                         if (dev_type)
530                                 *dev_type = out_dev_type;
531
532                         result = NET_NFC_OK;
533                 } else {
534                         result = NET_NFC_NOT_CONNECTED;
535                 }
536         } else {
537                 /* target was connected */
538                 if (dev_type != NULL)
539                         *dev_type = info->devType;
540
541                 result = NET_NFC_OK;
542         }
543
544         return result;
545 }
546
547 NET_NFC_EXPORT_API
548 net_nfc_error_e net_nfc_client_barcode_get_barcode_sync(
549                                                 unsigned char **barcode, int* barcode_len)
550 {
551         data_s *data;
552         GError *error = NULL;
553         GVariant *out_barcode = NULL;
554         net_nfc_error_e result = NET_NFC_OK;
555
556         if (tag_proxy == NULL)
557                 return NET_NFC_NOT_INITIALIZED;
558
559         /* prevent executing daemon when nfc is off */
560         if (net_nfc_client_manager_is_activated() == false)
561                 return NET_NFC_NOT_ACTIVATED;
562
563         if (net_nfc_gdbus_tag_call_get_barcode_sync(tag_proxy,
564                                 &result,
565                                 &out_barcode,
566                                 NULL,
567                                 &error) == FALSE) {
568                 if (error != NULL) {
569                         DEBUG_ERR_MSG("Can no get barcode result: %s", error->message);
570                         g_error_free(error);
571                 }
572
573                 result = NET_NFC_IPC_FAIL;
574
575                 return result;
576         }
577
578         if (result == NET_NFC_OK) {
579                 data = g_new0(data_s, 1);
580                 net_nfc_util_gdbus_variant_to_data_s(out_barcode, data);
581
582                 if (data == NULL)
583                         return NET_NFC_OPERATION_FAIL;
584
585                 if (data->buffer == NULL || data->length == 0) {
586                         net_nfc_util_free_data(data);
587
588                         return NET_NFC_OPERATION_FAIL;
589                 }
590
591                 /* alloc barcode */
592                 *barcode_len = data->length;
593                 *barcode = (unsigned char *)calloc(*barcode_len, sizeof(unsigned char));
594                 memcpy(*barcode, data->buffer, *barcode_len);
595
596                 if (data != NULL)
597                         net_nfc_util_free_data(data);
598         }
599
600         return result;
601 }
602
603 NET_NFC_EXPORT_API
604 net_nfc_error_e net_nfc_client_tag_get_current_tag_info_sync(
605                                                 net_nfc_target_info_h *info)
606 {
607         net_nfc_error_e result = NET_NFC_OK;
608         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
609         gboolean out_is_connected = false;
610         gboolean out_is_ndef_supported = false;
611         guchar out_ndef_card_state = 0;
612         guint out_handle = 0;
613         guint out_max_data_size = 0;
614         guint out_actual_data_size = 0;
615         guint out_number_of_keys = 0;
616         GVariant *out_target_info_values = NULL;
617         GVariant *out_raw_data = NULL;
618         GError *error = NULL;
619
620         if (tag_proxy == NULL)
621                 return NET_NFC_NOT_INITIALIZED;
622
623         /* prevent executing daemon when nfc is off */
624         if (net_nfc_client_manager_is_activated() == false)
625                 return NET_NFC_NOT_ACTIVATED;
626
627         if (net_nfc_client_tag_get_client_target_info() == NULL) {
628                 /* try to request target information from server */
629                 if (net_nfc_gdbus_tag_call_get_current_tag_info_sync(tag_proxy,
630                                         &result,
631                                         &out_is_connected,
632                                         &out_handle,
633                                         (gint *)&out_dev_type,
634                                         &out_is_ndef_supported,
635                                         &out_ndef_card_state,
636                                         &out_max_data_size,
637                                         &out_actual_data_size,
638                                         &out_number_of_keys,
639                                         &out_target_info_values,
640                                         &out_raw_data,
641                                         NULL,
642                                         &error) == FALSE) {
643                         if (error != NULL) {
644                                 DEBUG_ERR_MSG("Can no get current_tag_info result: %s", error->message);
645                                 g_error_free(error);
646                         }
647
648                         result = NET_NFC_IPC_FAIL;
649
650                         return result;
651                 }
652
653                 if (result == NET_NFC_OK) {
654                         if (out_is_connected == true) {
655                                 if (tag_check_filter(out_dev_type) == true) {
656                                         tag_get_target_info(out_handle,
657                                                         out_dev_type,
658                                                         out_is_ndef_supported,
659                                                         out_ndef_card_state,
660                                                         out_max_data_size,
661                                                         out_actual_data_size,
662                                                         out_number_of_keys,
663                                                         out_target_info_values,
664                                                         out_raw_data,
665                                                         &client_target_info);
666
667                                         result = NET_NFC_OK;
668                                 } else {
669                                         DEBUG_CLIENT_MSG("The detected target is filtered out");
670
671                                         result = NET_NFC_NOT_CONNECTED;
672                                 }
673                         } else {
674                                 result = NET_NFC_NOT_CONNECTED;
675                         }
676                 }
677         } else {
678                 result = NET_NFC_OK;
679         }
680
681         if (result == NET_NFC_OK && info != NULL)
682                 *info = client_target_info;
683
684         return result;
685 }
686
687 NET_NFC_EXPORT_API
688 net_nfc_error_e net_nfc_client_tag_get_current_target_handle_sync(
689                                         net_nfc_target_handle_h *handle)
690 {
691         net_nfc_target_info_s *info;
692         net_nfc_error_e result;
693         net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
694         gboolean out_is_connected = false;
695         guint out_handle = 0;
696         GError *error = NULL;
697
698         if (tag_proxy == NULL)
699                 return NET_NFC_NOT_INITIALIZED;
700
701         /* prevent executing daemon when nfc is off */
702         if (net_nfc_client_manager_is_activated() == false)
703                 return NET_NFC_NOT_ACTIVATED;
704
705         info = net_nfc_client_tag_get_client_target_info();
706         if (info == NULL) {
707                 if (net_nfc_gdbus_tag_call_get_current_target_handle_sync(
708                         tag_proxy,
709                         &result,
710                         &out_is_connected,
711                         &out_handle,
712                         (gint *)&out_dev_type,
713                         NULL,
714                         &error) == FALSE) {
715                         if (error != NULL) {
716                                 DEBUG_ERR_MSG("Can no get current_target_handle result: %s", error->message);
717                                 g_error_free(error);
718                         }
719
720                         result = NET_NFC_IPC_FAIL;
721
722                         return result;
723                 }
724
725                 if (result == NET_NFC_OK) {
726                         if (out_is_connected == true) {
727                                 if (handle)
728                                         *handle = GUINT_TO_POINTER(out_handle);
729
730                                 result = NET_NFC_OK;
731                         } else {
732                                 result = NET_NFC_NOT_CONNECTED;
733                         }
734                 }
735         } else if (info->devType == NET_NFC_NFCIP1_INITIATOR ||
736                 info->devType == NET_NFC_NFCIP1_TARGET) {
737                 if (handle)
738                         *handle = info->handle;
739
740                 result = NET_NFC_OK;
741         } else {
742                 result = NET_NFC_NOT_CONNECTED;
743         }
744
745         return result;
746 }
747
748 NET_NFC_EXPORT_API
749 void net_nfc_client_tag_set_tag_discovered(
750                         net_nfc_client_tag_tag_discovered callback,
751                         void *user_data)
752 {
753         if (callback == NULL)
754                 return;
755
756         if (tag_proxy == NULL) {
757                 if (net_nfc_client_tag_init() != NET_NFC_OK) {
758                         DEBUG_ERR_MSG("tag_proxy fail");
759                         /* FIXME : return result of this error */
760                         return;
761                 }
762         }
763
764         tag_discovered_func_data.callback = (gpointer)callback;
765         tag_discovered_func_data.user_data = user_data;
766 }
767
768 NET_NFC_EXPORT_API
769 void net_nfc_client_tag_unset_tag_discovered(void)
770 {
771         tag_discovered_func_data.callback = NULL;
772         tag_discovered_func_data.user_data = NULL;
773 }
774
775 NET_NFC_EXPORT_API
776 void net_nfc_client_tag_set_tag_detached(
777                         net_nfc_client_tag_tag_detached callback,
778                         void *user_data)
779 {
780         if (callback == NULL)
781                 return;
782
783         if (tag_proxy == NULL) {
784                 if (net_nfc_client_tag_init() != NET_NFC_OK) {
785                         DEBUG_ERR_MSG("tag_proxy fail");
786                         /* FIXME : return result of this error */
787                         return;
788                 }
789         }
790
791         tag_detached_func_data.callback = (gpointer)callback;
792         tag_detached_func_data.user_data = user_data;
793 }
794
795 NET_NFC_EXPORT_API
796 void net_nfc_client_tag_unset_tag_detached(void)
797 {
798         tag_detached_func_data.callback = NULL;
799         tag_detached_func_data.user_data = NULL;
800 }
801
802 NET_NFC_EXPORT_API
803 void net_nfc_client_tag_set_filter(net_nfc_event_filter_e filter)
804 {
805         client_filter = filter;
806 }
807
808 NET_NFC_EXPORT_API
809 net_nfc_event_filter_e net_nfc_client_tag_get_filter(void)
810 {
811         return client_filter;
812 }
813
814 net_nfc_error_e net_nfc_client_tag_init(void)
815 {
816         GError *error = NULL;
817
818         if (tag_proxy) {
819                 DEBUG_CLIENT_MSG("Alrady initialized");
820
821                 return NET_NFC_OK;
822         }
823
824         if (client_target_info) {
825                 net_nfc_release_tag_info(
826                                 (net_nfc_target_info_h)client_target_info);
827                 client_target_info = NULL;
828         }
829
830         client_filter = NET_NFC_ALL_ENABLE;
831
832         tag_proxy = net_nfc_gdbus_tag_proxy_new_for_bus_sync(
833                                 G_BUS_TYPE_SYSTEM,
834                                 G_DBUS_PROXY_FLAGS_NONE,
835                                 "org.tizen.NetNfcService",
836                                 "/org/tizen/NetNfcService/Tag",
837                                 NULL,
838                                 &error);
839         if (tag_proxy == NULL) {
840                 if (error != NULL) {
841                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
842                         g_error_free(error);
843                 }
844
845                 return NET_NFC_UNKNOWN_ERROR;
846         }
847
848         g_signal_connect(tag_proxy, "tag-discovered",
849                         G_CALLBACK(tag_tag_discovered), NULL);
850
851         g_signal_connect(tag_proxy, "tag-detached",
852                         G_CALLBACK(tag_tag_detached), NULL);
853
854         return NET_NFC_OK;
855 }
856
857 void net_nfc_client_tag_deinit(void)
858 {
859         client_filter = NET_NFC_ALL_ENABLE;
860
861         net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
862         client_target_info = NULL;
863
864         net_nfc_client_tag_unset_tag_discovered();
865         net_nfc_client_tag_unset_tag_detached();
866
867         if (tag_proxy) {
868                 g_object_unref(tag_proxy);
869                 tag_proxy = NULL;
870         }
871 }
872
873 /* LCOV_EXCL_STOP */
874