[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / nfc_tag.c
1 /*
2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "nfc_common.h"
18
19 /**
20  * @brief The default factory key.
21  * @details The key is 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
22  * @ingroup CAPI_NETWORK_NFC_TAG_MIFARE_MODULE
23  */
24 const unsigned char NFC_TAG_MIFARE_KEY_DEFAULT[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
25
26 /**
27  * @brief The well-known key for tags formatted according to the MIFARE Application Directory (MAD) specification.
28  * @details The key is 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5
29  * @ingroup CAPI_NETWORK_NFC_TAG_MIFARE_MODULE
30  */
31 const unsigned char NFC_TAG_MIFARE_KEY_APPLICATION_DIRECTORY[6] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
32
33 /**
34  * @brief The well-known key for tags formatted according to the NDEF on Mifare Classic specification.
35  * @details The key is 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7
36  * @ingroup CAPI_NETWORK_NFC_TAG_MIFARE_MODULE
37  */
38 const unsigned char NFC_TAG_MIFARE_KEY_NFC_FORUM[6] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7};
39
40 /* LCOV_EXCL_START */
41 static void _tag_format_ndef_cb(net_nfc_error_e result,
42         void *user_data)
43 {
44         nfc_tag_format_completed_cb callback;
45         void *user_param;
46
47         LOG_BEGIN();
48
49         if (user_data == NULL) {
50                 LOG_ERR("user_data is NULL");
51                 return;
52         }
53
54         g_variant_get((GVariant *)user_data,
55                 "(uu)",
56                 (guint *)&callback,
57                 (guint *)&user_param);
58
59         if (callback != NULL) {
60                 callback(nfc_common_convert_error_code(__func__, result),
61                         user_param);
62         }
63
64         g_variant_unref((GVariant *)user_data);
65 }
66 /* LCOV_EXCL_STOP */
67
68 int nfc_tag_format_ndef(nfc_tag_h tag,
69         unsigned char *key,
70         int key_size,
71         nfc_tag_format_completed_cb callback,
72         void *user_data)
73 {
74         int ret;
75         data_h key_data = NULL;
76
77         LOG_BEGIN();
78
79         CHECK_SUPPORTED(NFC_TAG_FEATURE);
80
81         /* LCOV_EXCL_START */
82         CHECK_INIT();
83         CHECK_INVALID(tag == NULL);
84         CHECK_ACTIVATED();
85         CHECK_APP_PERMISSION();
86
87         ret = net_nfc_create_data(&key_data, key, key_size);
88         if (ret == NET_NFC_OK) {
89                 net_nfc_target_handle_h handle = NULL;
90
91                 ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag,
92                         &handle);
93                 if (ret == NET_NFC_OK) {
94                         GVariant *parameter;
95
96                         parameter = g_variant_new("(uu)",
97                                 GPOINTER_TO_UINT(callback),
98                                 GPOINTER_TO_UINT(user_data));
99                         if (parameter != NULL) {
100                                 ret = net_nfc_client_ndef_format(
101                                         handle,
102                                         key_data,
103                                         _tag_format_ndef_cb,
104                                         parameter);
105                                 if (ret != NET_NFC_OK)
106                                         g_variant_unref(parameter);
107                         } else {
108                                 ret = NET_NFC_ALLOC_FAIL;
109                         }
110                 } else {
111                         LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
112                 }
113
114                 net_nfc_free_data(key_data);
115         } else {
116                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
117         }
118
119         return nfc_common_convert_error_code(__func__, ret);
120         /* LCOV_EXCL_STOP */
121 }
122
123 /* LCOV_EXCL_START */
124 static void _tag_read_ndef_cb(net_nfc_error_e result, ndef_message_h message,
125         void *user_data)
126 {
127         nfc_tag_read_completed_cb callback;
128         void *user_param;
129
130         LOG_BEGIN();
131
132         if (user_data == NULL) {
133                 LOG_ERR("user_data is NULL");
134                 return;
135         }
136
137         g_variant_get((GVariant *)user_data,
138                 "(uu)",
139                 (guint *)&callback,
140                 (guint *)&user_param);
141
142         if (callback != NULL) {
143                 callback(nfc_common_convert_error_code(__func__, result),
144                         (nfc_ndef_message_h)message,
145                         user_param);
146         }
147
148         g_variant_unref((GVariant *)user_data);
149 }
150 /* LCOV_EXCL_STOP */
151
152 int nfc_tag_read_ndef(nfc_tag_h tag,
153         nfc_tag_read_completed_cb callback,
154         void * user_data)
155 {
156         int ret;
157         net_nfc_target_handle_h handle = NULL;
158
159         CHECK_SUPPORTED(NFC_TAG_FEATURE);
160
161         /* LCOV_EXCL_START */
162         CHECK_INIT();
163         CHECK_INVALID(tag == NULL);
164         CHECK_ACTIVATED();
165
166         ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
167         if (ret == NET_NFC_OK) {
168                 GVariant *parameter;
169
170                 parameter = g_variant_new("(uu)",
171                         GPOINTER_TO_UINT(callback),
172                         GPOINTER_TO_UINT(user_data));
173                 if (parameter != NULL) {
174                         ret = net_nfc_client_ndef_read(
175                                 handle,
176                                 _tag_read_ndef_cb,
177                                 parameter);
178                         if (ret != NET_NFC_OK)
179                                 g_variant_unref(parameter);
180                 } else {
181                         ret = NET_NFC_ALLOC_FAIL;
182                 }
183         } else {
184                 LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
185         }
186
187         return nfc_common_convert_error_code(__func__, ret);
188         /* LCOV_EXCL_STOP */
189 }
190
191 /* LCOV_EXCL_START */
192 static void _tag_write_ndef_cb(net_nfc_error_e result,
193         void *user_data)
194 {
195         nfc_tag_write_completed_cb callback;
196         void *user_param;
197
198         LOG_BEGIN();
199
200         if (user_data == NULL) {
201                 LOG_ERR("user_data is NULL");
202                 return;
203         }
204
205         g_variant_get((GVariant *)user_data,
206                 "(uu)",
207                 (guint *)&callback,
208                 (guint *)&user_param);
209
210         if (callback != NULL)
211                 callback(nfc_common_convert_error_code(__func__, result), user_param);
212
213         g_variant_unref((GVariant *)user_data);
214 }
215 /* LCOV_EXCL_STOP */
216
217 int nfc_tag_write_ndef(nfc_tag_h tag,
218         nfc_ndef_message_h msg,
219         nfc_tag_write_completed_cb callback,
220         void *user_data)
221 {
222         int ret;
223         net_nfc_target_handle_h handle = NULL;
224         unsigned int byte_size = 0;
225         uint32_t max_len = 0;
226         net_nfc_ndef_card_state_e state = NET_NFC_NDEF_CARD_INVALID;
227
228         CHECK_SUPPORTED(NFC_TAG_FEATURE);
229
230         /* LCOV_EXCL_START */
231         CHECK_INIT();
232         CHECK_INVALID(tag == NULL);
233         CHECK_ACTIVATED();
234         CHECK_APP_PERMISSION();
235
236         ret = net_nfc_get_tag_ndef_state((net_nfc_target_info_h)tag, &state);
237         if (ret != NET_NFC_OK) {
238                 LOG_ERR("net_nfc_get_tag_ndef_state failed, [%d]", ret);
239                 return nfc_common_convert_error_code(__func__, ret);;
240         }
241
242         if (state == NET_NFC_NDEF_CARD_READ_ONLY)
243                 return NFC_ERROR_READ_ONLY_NDEF;
244
245         ret = net_nfc_get_tag_max_data_size((net_nfc_target_info_h)tag, &max_len);
246         if (ret != NET_NFC_OK) {
247                 LOG_ERR("net_nfc_get_tag_max_data_size failed, [%d]", ret);
248                 return nfc_common_convert_error_code(__func__, ret);;
249         }
250
251         ret = nfc_common_get_rawdata_size(msg, &byte_size);
252         if (ret != NFC_ERROR_NONE) {
253                 LOG_ERR("nfc_ndef_message_get_rawdata_size failed, [%d]", ret);
254                 return nfc_common_convert_error_code(__func__, ret);;
255         }
256
257         if (max_len < byte_size)
258                 return NFC_ERROR_NO_SPACE_ON_NDEF;
259
260         ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
261         if (ret == NET_NFC_OK) {
262                 GVariant *parameter;
263
264                 parameter = g_variant_new("(uu)",
265                         GPOINTER_TO_UINT(callback),
266                         GPOINTER_TO_UINT(user_data));
267                 if (parameter != NULL) {
268                         ret = net_nfc_client_ndef_write(
269                                 handle,
270                                 msg,
271                                 _tag_write_ndef_cb,
272                                 parameter);
273                         if (ret != NET_NFC_OK)
274                                 g_variant_unref(parameter);
275                 } else {
276                         ret = NET_NFC_ALLOC_FAIL;
277                 }
278         } else {
279                 LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
280         }
281
282         return nfc_common_convert_error_code(__func__, ret);
283         /* LCOV_EXCL_STOP */
284 }
285
286 /* LCOV_EXCL_START */
287 static void _tag_transceive_data_cb(net_nfc_error_e result, data_h arg_data,
288         void *user_data)
289 {
290         nfc_tag_transceive_completed_cb callback;
291         void *user_param;
292
293         LOG_BEGIN();
294
295         if (user_data == NULL)
296                 return;
297
298         g_variant_get((GVariant *)user_data,
299                 "(uu)",
300                 (guint *)&callback,
301                 (guint *)&user_param);
302
303         if (callback != NULL) {
304                 uint8_t *buffer = NULL;
305                 uint32_t length = 0;
306
307                 if (result == NET_NFC_OK && arg_data != NULL) {
308                         buffer = net_nfc_get_data_buffer(arg_data);
309                         length = net_nfc_get_data_length(arg_data);
310                 }
311
312                 callback(nfc_common_convert_error_code(__func__, result),
313                         buffer,
314                         length,
315                         user_param);
316         }
317
318         g_variant_unref((GVariant *)user_data);
319 }
320 /* LCOV_EXCL_STOP */
321
322 int nfc_tag_transceive(nfc_tag_h tag,
323         unsigned char *buffer,
324         int buffer_size,
325         nfc_tag_transceive_completed_cb callback,
326         void *user_data)
327 {
328         int ret;
329         data_h rawdata = NULL;
330
331         CHECK_SUPPORTED(NFC_TAG_FEATURE);
332
333         /* LCOV_EXCL_START */
334         CHECK_INIT();
335         CHECK_INVALID(tag == NULL);
336         CHECK_INVALID(buffer == NULL);
337         CHECK_INVALID(buffer_size <= 0);
338         CHECK_ACTIVATED();
339         CHECK_APP_PERMISSION();
340
341         ret = net_nfc_create_data(&rawdata, buffer, buffer_size);
342         if (ret == NET_NFC_OK) {
343                 net_nfc_target_handle_h handle = NULL;
344
345                 ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
346                 if (ret == NET_NFC_OK) {
347                         GVariant *parameter;
348
349                         parameter = g_variant_new("(uu)",
350                                 GPOINTER_TO_UINT(callback),
351                                 GPOINTER_TO_UINT(user_data));
352                         if (parameter != NULL) {
353                                 ret = net_nfc_client_transceive_data(
354                                         handle,
355                                         rawdata,
356                                         _tag_transceive_data_cb,
357                                         parameter);
358                                 if (ret != NET_NFC_OK)
359                                         g_variant_unref(parameter);
360                         } else {
361                                 ret = NET_NFC_ALLOC_FAIL;
362                         }
363                 } else {
364                         LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
365                 }
366
367                 net_nfc_free_data(rawdata);
368         } else {
369                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
370         }
371
372         return nfc_common_convert_error_code(__func__, ret);
373         /* LCOV_EXCL_STOP */
374 }
375
376 int nfc_tag_get_type(nfc_tag_h tag, nfc_tag_type_e *type)
377 {
378         int ret;
379
380         LOG_BEGIN();
381
382         CHECK_SUPPORTED(NFC_TAG_FEATURE);
383
384         /* LCOV_EXCL_START */
385         CHECK_INIT();
386         CHECK_INVALID(tag == NULL);
387         CHECK_INVALID(type == NULL);
388
389         ret = net_nfc_get_tag_type(tag, (net_nfc_target_type_e *)type);
390
391         return nfc_common_convert_error_code(__func__, ret);
392         /* LCOV_EXCL_STOP */
393 }
394
395 int nfc_tag_is_support_ndef(nfc_tag_h tag, bool *is_support)
396 {
397         int ret;
398
399         LOG_BEGIN();
400
401         CHECK_SUPPORTED(NFC_TAG_FEATURE);
402
403         /* LCOV_EXCL_START */
404         CHECK_INIT();
405         CHECK_INVALID(tag == NULL);
406         CHECK_INVALID(is_support == NULL);
407
408         ret = net_nfc_get_tag_ndef_support(tag, is_support);
409
410         return nfc_common_convert_error_code(__func__, ret);
411         /* LCOV_EXCL_STOP */
412 }
413
414 int nfc_tag_get_maximum_ndef_size(nfc_tag_h tag, unsigned int *max_size)
415 {
416         int ret;
417
418         LOG_BEGIN();
419         CHECK_SUPPORTED(NFC_TAG_FEATURE);
420
421         /* LCOV_EXCL_START */
422         CHECK_INIT();
423         CHECK_INVALID(tag == NULL);
424         CHECK_INVALID(max_size == NULL);
425
426         ret = net_nfc_get_tag_max_data_size(tag, max_size);
427
428         return nfc_common_convert_error_code(__func__, ret);
429         /* LCOV_EXCL_STOP */
430 }
431
432 int nfc_tag_get_ndef_size(nfc_tag_h tag, unsigned int *actual_data_size)
433 {
434         int ret;
435
436         LOG_BEGIN();
437
438         CHECK_SUPPORTED(NFC_TAG_FEATURE);
439
440         /* LCOV_EXCL_START */
441         CHECK_INIT();
442         CHECK_INVALID(tag == NULL);
443         CHECK_INVALID(actual_data_size == NULL);
444
445         ret = net_nfc_get_tag_actual_data_size(tag, actual_data_size);
446
447         return nfc_common_convert_error_code(__func__, ret);
448         /* LCOV_EXCL_STOP */
449 }
450
451 int nfc_tag_foreach_information(nfc_tag_h tag,
452         nfc_tag_information_cb callback,
453         void *user_data)
454 {
455         int ret;
456         int i, count = 0;
457         char **keys = NULL;
458         data_h value = NULL;
459
460         bool cont;
461
462         LOG_BEGIN();
463         CHECK_SUPPORTED(NFC_TAG_FEATURE);
464
465         /* LCOV_EXCL_START */
466         CHECK_INIT();
467         CHECK_INVALID(tag == NULL);
468         CHECK_INVALID(callback == NULL);
469
470         ret = net_nfc_get_tag_info_keys((net_nfc_target_info_h)tag,
471                 &keys, &count);
472
473         if (ret != NET_NFC_OK)
474                 return nfc_common_convert_error_code(__func__, ret);
475
476         for (i = 0; i < count; i++) {
477                 net_nfc_get_tag_info_value((net_nfc_target_info_h)tag, keys[i],
478                         &value);
479
480                 cont = callback(keys[i],
481                         net_nfc_get_data_buffer(value),
482                         net_nfc_get_data_length(value),
483                         user_data);
484
485                 if (!cont)
486                         break;
487         }
488
489         return NFC_ERROR_NONE;
490         /* LCOV_EXCL_STOP */
491 }
492
493 /* FIXME */
494 /* LCOV_EXCL_START */
495 static void _mifare_authenticate_with_keyA_cb(net_nfc_error_e result,
496         void *user_data)
497 {
498         nfc_mifare_authenticate_with_keyA_completed_cb callback;
499         void *user_param;
500
501         LOG_BEGIN();
502
503         if (user_data == NULL) {
504                 LOG_ERR("user_data is NULL");
505                 return;
506         }
507
508         g_variant_get((GVariant *)user_data,
509                 "(uu)",
510                 (guint *)&callback,
511                 (guint *)&user_param);
512
513         if (callback != NULL)
514                 callback(nfc_common_convert_error_code(__func__, result), user_param);
515
516         g_variant_unref((GVariant *)user_data);
517 }
518 /* LCOV_EXCL_STOP */
519
520 int nfc_mifare_authenticate_with_keyA(nfc_tag_h tag,
521         int sector_index,
522         unsigned char *auth_key,
523         nfc_mifare_authenticate_with_keyA_completed_cb callback,
524         void *user_data)
525 {
526         int ret;
527         data_h auth_key_data = NULL;
528
529         CHECK_SUPPORTED(NFC_TAG_FEATURE);
530
531         /* LCOV_EXCL_START */
532         CHECK_INIT();
533         CHECK_INVALID(tag == NULL);
534         CHECK_INVALID(auth_key == NULL);
535         CHECK_ACTIVATED();
536
537         ret = net_nfc_create_data(&auth_key_data, auth_key, 6);
538         if (ret == NET_NFC_OK) {
539                 net_nfc_target_handle_h handle = NULL;
540
541                 ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
542                 if (ret == NET_NFC_OK) {
543                         GVariant *parameter;
544
545                         parameter = g_variant_new("(uu)",
546                                 GPOINTER_TO_UINT(callback),
547                                 GPOINTER_TO_UINT(user_data));
548                         if (parameter != NULL) {
549                                 ret = net_nfc_client_mifare_authenticate_with_keyA(
550                                         handle,
551                                         sector_index,
552                                         auth_key_data,
553                                         _mifare_authenticate_with_keyA_cb,
554                                         parameter);
555                                 if (ret != NET_NFC_OK)
556                                         g_variant_unref(parameter);
557                         } else {
558                                 ret = NET_NFC_ALLOC_FAIL;
559                         }
560                 } else {
561                         LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
562                 }
563
564                 net_nfc_free_data(auth_key_data);
565         } else {
566                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
567         }
568
569         return nfc_common_convert_error_code(__func__, ret);
570         /* LCOV_EXCL_STOP */
571 }
572
573
574 /* LCOV_EXCL_START */
575 static void _mifare_authenticate_with_keyB_cb(net_nfc_error_e result,
576         void *user_data)
577 {
578         nfc_mifare_authenticate_with_keyB_completed_cb callback;
579         void *user_param;
580
581         if (user_data == NULL) {
582                 LOG_ERR("user_data is NULL");
583                 return;
584         }
585
586         g_variant_get((GVariant *)user_data,
587                 "(uu)",
588                 (guint *)&callback,
589                 (guint *)&user_param);
590
591         if (callback != NULL)
592                 callback(nfc_common_convert_error_code(__func__, result), user_param);
593
594         g_variant_unref((GVariant *)user_data);
595 }
596 /* LCOV_EXCL_STOP */
597
598 int nfc_mifare_authenticate_with_keyB(nfc_tag_h tag,
599         int sector_index,
600         unsigned char *auth_key,
601         nfc_mifare_authenticate_with_keyB_completed_cb callback,
602         void *user_data)
603 {
604         int ret;
605         data_h auth_key_data = NULL;
606
607         LOG_BEGIN();
608
609         CHECK_SUPPORTED(NFC_TAG_FEATURE);
610
611         /* LCOV_EXCL_START */
612         CHECK_INIT();
613         CHECK_INVALID(tag == NULL);
614         CHECK_INVALID(auth_key == NULL);
615         CHECK_ACTIVATED();
616
617         ret = net_nfc_create_data(&auth_key_data, auth_key, 6);
618         if (ret == NET_NFC_OK) {
619                 net_nfc_target_handle_h handle = NULL;
620
621                 ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
622                 if (ret == NET_NFC_OK) {
623                         GVariant *parameter;
624
625                         parameter = g_variant_new("(uu)",
626                                 GPOINTER_TO_UINT(callback),
627                                 GPOINTER_TO_UINT(user_data));
628                         if (parameter != NULL) {
629                                 ret = net_nfc_client_mifare_authenticate_with_keyB(
630                                         handle,
631                                         sector_index,
632                                         auth_key_data,
633                                         _mifare_authenticate_with_keyB_cb,
634                                         parameter);
635                                 if (ret != NET_NFC_OK)
636                                         g_variant_unref(parameter);
637                         } else {
638                                 ret = NET_NFC_ALLOC_FAIL;
639                         }
640                 } else {
641                         LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
642                 }
643
644                 net_nfc_free_data(auth_key_data);
645         } else {
646                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
647         }
648
649         return nfc_common_convert_error_code(__func__, ret);
650         /* LCOV_EXCL_STOP */
651 }
652
653 /* FIXME */
654 /* LCOV_EXCL_START */
655 static void _mifare_read_block_cb(net_nfc_error_e result, data_h data,
656         void *user_data)
657 {
658         nfc_mifare_read_block_completed_cb callback;
659         void *user_param;
660
661         if (user_data == NULL) {
662                 LOG_ERR("user_data is NULL");
663                 return;
664         }
665
666         g_variant_get((GVariant *)user_data,
667                 "(uu)",
668                 (guint *)&callback,
669                 (guint *)&user_param);
670
671         if (callback != NULL) {
672                 uint8_t *buffer = NULL;
673                 int length = 0;
674
675                 if (result == NET_NFC_OK && data != NULL) {
676                         buffer = net_nfc_get_data_buffer(data);
677                         length = net_nfc_get_data_length(data);
678                 }
679
680                 callback(nfc_common_convert_error_code(__func__, result),
681                         buffer,
682                         length,
683                         user_param);
684         }
685
686         g_variant_unref((GVariant *)user_data);
687 }
688 /* LCOV_EXCL_STOP */
689
690 int nfc_mifare_read_block(nfc_tag_h tag,
691         int block_index,
692         nfc_mifare_read_block_completed_cb callback,
693         void *user_data)
694 {
695         int ret;
696         net_nfc_target_handle_h handle = NULL;
697
698         LOG_BEGIN();
699
700         CHECK_SUPPORTED(NFC_TAG_FEATURE);
701
702         /* LCOV_EXCL_START */
703         CHECK_INIT();
704         CHECK_INVALID(tag == NULL);
705
706         ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
707         if (ret == NET_NFC_OK) {
708                 GVariant *parameter;
709
710                 parameter = g_variant_new("(uu)",
711                         GPOINTER_TO_UINT(callback),
712                         GPOINTER_TO_UINT(user_data));
713                 if (parameter != NULL) {
714                         ret = net_nfc_client_mifare_read(
715                                 handle,
716                                 block_index,
717                                 _mifare_read_block_cb,
718                                 parameter);
719                         if (ret != NET_NFC_OK)
720                                 g_variant_unref(parameter);
721                 } else {
722                         ret = NET_NFC_ALLOC_FAIL;
723                 }
724         } else {
725                 LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
726         }
727
728         return nfc_common_convert_error_code(__func__, ret);
729         /* LCOV_EXCL_STOP */
730 }
731
732 int nfc_mifare_read_page(nfc_tag_h tag,
733         int page_index,
734         nfc_mifare_read_page_completed_cb callback,
735         void *user_data)
736 {
737         int ret;
738         GVariant *parameter;
739
740         LOG_BEGIN();
741
742         CHECK_SUPPORTED(NFC_TAG_FEATURE);
743
744         /* LCOV_EXCL_START */
745         CHECK_INIT();
746         CHECK_INVALID(tag == NULL);
747
748         parameter = g_variant_new("(uu)",
749                 GPOINTER_TO_UINT(callback),
750                 GPOINTER_TO_UINT(user_data));
751         if (parameter != NULL) {
752                 g_variant_unref(parameter);
753                 ret = NET_NFC_OK;
754         } else {
755                 ret = NET_NFC_ALLOC_FAIL;
756         }
757
758         return nfc_common_convert_error_code(__func__, ret);
759         /* LCOV_EXCL_STOP */
760 }
761
762 /* LCOV_EXCL_START */
763 static void _mifare_write_block_cb(net_nfc_error_e result, void *user_data)
764 {
765         nfc_mifare_write_block_completed_cb callback;
766         void *user_param;
767
768         LOG_BEGIN();
769
770         if (user_data == NULL) {
771                 LOG_ERR("user_data is NULL");
772                 return;
773         }
774
775         g_variant_get((GVariant *)user_data,
776                 "(uu)",
777                 (guint *)&callback,
778                 (guint *)&user_param);
779
780         if (callback != NULL)
781                 callback(nfc_common_convert_error_code(__func__, result), user_param);
782
783         g_variant_unref((GVariant *)user_data);
784 }
785 /* LCOV_EXCL_STOP */
786
787 int nfc_mifare_write_block(nfc_tag_h tag,
788         int block_index,
789         unsigned char *buffer,
790         int buffer_size,
791         nfc_mifare_write_block_completed_cb callback,
792         void *user_data)
793 {
794         int ret;
795         data_h block_data = NULL;
796
797         LOG_BEGIN();
798
799         CHECK_SUPPORTED(NFC_TAG_FEATURE);
800
801         /* LCOV_EXCL_START */
802         CHECK_INIT();
803         CHECK_INVALID(tag == NULL);
804         CHECK_INVALID(buffer == NULL);
805         CHECK_INVALID(buffer_size <= 0);
806         CHECK_ACTIVATED();
807
808         ret = net_nfc_create_data(&block_data, buffer, buffer_size);
809         if (ret == NET_NFC_OK) {
810                 net_nfc_target_handle_h handle = NULL;
811
812                 ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag,
813                         &handle);
814                 if (ret == NET_NFC_OK) {
815                         GVariant *parameter;
816
817                         parameter = g_variant_new("(uu)",
818                                 GPOINTER_TO_UINT(callback),
819                                 GPOINTER_TO_UINT(user_data));
820                         if (parameter != NULL) {
821                                 ret = net_nfc_client_mifare_write_block(
822                                         handle,
823                                         block_index,
824                                         block_data,
825                                         _mifare_write_block_cb,
826                                         parameter);
827                                 if (ret != NET_NFC_OK)
828                                         g_variant_unref(parameter);
829                         } else {
830                                 ret = NET_NFC_ALLOC_FAIL;
831                         }
832                 } else {
833                         LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
834                 }
835
836                 net_nfc_free_data(block_data);
837         } else {
838                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
839         }
840
841         return nfc_common_convert_error_code(__func__, ret);
842         /* LCOV_EXCL_STOP */
843 }
844
845 /* LCOV_EXCL_START */
846 static void _mifare_write_page_cb(net_nfc_error_e result, void *user_data)
847 {
848         nfc_mifare_write_page_completed_cb callback;
849         void *user_param;
850
851         LOG_BEGIN();
852
853         if (user_data == NULL) {
854                 LOG_ERR("user_data is NULL");
855                 return;
856         }
857
858         g_variant_get((GVariant *)user_data,
859                 "(uu)",
860                 (guint *)&callback,
861                 (guint *)&user_param);
862
863         if (callback != NULL)
864                 callback(nfc_common_convert_error_code(__func__, result), user_param);
865
866         g_variant_unref((GVariant *)user_data);
867 }
868 /* LCOV_EXCL_STOP */
869
870 int nfc_mifare_write_page(nfc_tag_h tag,
871         int page_index,
872         unsigned char *buffer,
873         int buffer_size,
874         nfc_mifare_write_page_completed_cb callback,
875         void *user_data)
876 {
877         int ret;
878         data_h block_data = NULL;
879
880         LOG_BEGIN();
881         CHECK_SUPPORTED(NFC_TAG_FEATURE);
882
883         /* LCOV_EXCL_START */
884         CHECK_INIT();
885         CHECK_INVALID(tag == NULL);
886         CHECK_INVALID(buffer == NULL);
887         CHECK_INVALID(buffer_size <= 0);
888         CHECK_ACTIVATED();
889
890         ret = net_nfc_create_data(&block_data, buffer, buffer_size);
891         if (ret == NET_NFC_OK) {
892                 net_nfc_target_handle_h handle = NULL;
893
894                 ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag,
895                         &handle);
896                 if (ret == NET_NFC_OK) {
897                         GVariant *parameter;
898
899                         parameter = g_variant_new("(uu)",
900                                 GPOINTER_TO_UINT(callback),
901                                 GPOINTER_TO_UINT(user_data));
902                         if (parameter != NULL) {
903                                 ret = net_nfc_client_mifare_write_page(
904                                         handle,
905                                         page_index,
906                                         block_data,
907                                         _mifare_write_page_cb,
908                                         parameter);
909                                 if (ret != NET_NFC_OK)
910                                         g_variant_unref(parameter);
911                         } else {
912                                 ret = NET_NFC_ALLOC_FAIL;
913                         }
914                 } else {
915                         LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
916                 }
917
918                 net_nfc_free_data(block_data);
919         } else {
920                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
921         }
922
923         return nfc_common_convert_error_code(__func__, ret);
924         /* LCOV_EXCL_STOP */
925 }
926
927 /* LCOV_EXCL_START */
928 static void _mifare_increment_cb(net_nfc_error_e result, void *user_data)
929 {
930         nfc_mifare_increment_completed_cb callback;
931         void *user_param;
932
933         LOG_BEGIN();
934
935         if (user_data == NULL) {
936                 LOG_ERR("user_data is NULL");
937                 return;
938         }
939
940         g_variant_get((GVariant *)user_data,
941                 "(uu)",
942                 (guint *)&callback,
943                 (guint *)&user_param);
944
945         if (callback != NULL)
946                 callback(nfc_common_convert_error_code(__func__, result), user_param);
947
948         g_variant_unref((GVariant *)user_data);
949 }
950 /* LCOV_EXCL_STOP */
951
952 int nfc_mifare_increment(nfc_tag_h tag,
953         int block_index,
954         int value,
955         nfc_mifare_increment_completed_cb callback,
956         void *user_data)
957 {
958         int ret;
959         net_nfc_target_handle_h handle = NULL;
960
961         LOG_BEGIN();
962
963         CHECK_SUPPORTED(NFC_TAG_FEATURE);
964
965         /* LCOV_EXCL_START */
966         CHECK_INIT();
967         CHECK_INVALID(tag == NULL);
968         CHECK_ACTIVATED();
969
970         ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
971         if (ret == NET_NFC_OK) {
972                 GVariant *parameter;
973
974                 parameter = g_variant_new("(uu)",
975                         GPOINTER_TO_UINT(callback),
976                         GPOINTER_TO_UINT(user_data));
977                 if (parameter != NULL) {
978                         ret = net_nfc_client_mifare_increment(
979                                 handle,
980                                 block_index,
981                                 value,
982                                 _mifare_increment_cb,
983                                 parameter);
984                         if (ret != NET_NFC_OK)
985                                 g_variant_unref(parameter);
986                 } else {
987                         ret = NET_NFC_ALLOC_FAIL;
988                 }
989         } else {
990                 LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
991         }
992
993         return nfc_common_convert_error_code(__func__, ret);
994         /* LCOV_EXCL_STOP */
995 }
996
997 /* LCOV_EXCL_START */
998 static void _mifare_decrement_cb(net_nfc_error_e result, void *user_data)
999 {
1000         nfc_mifare_decrement_completed_cb callback;
1001         void *user_param;
1002
1003         LOG_BEGIN();
1004
1005         if (user_data == NULL) {
1006                 LOG_ERR("user_data is NULL");
1007                 return;
1008         }
1009
1010         g_variant_get((GVariant *)user_data,
1011                 "(uu)",
1012                 (guint *)&callback,
1013                 (guint *)&user_param);
1014
1015         if (callback != NULL)
1016                 callback(nfc_common_convert_error_code(__func__, result), user_param);
1017
1018         g_variant_unref((GVariant *)user_data);
1019 }
1020 /* LCOV_EXCL_STOP */
1021
1022 int nfc_mifare_decrement(nfc_tag_h tag,
1023         int block_index,
1024         int value,
1025         nfc_mifare_decrement_completed_cb callback,
1026         void *user_data)
1027 {
1028         int ret;
1029         net_nfc_target_handle_h handle = NULL;
1030
1031         LOG_BEGIN();
1032
1033         CHECK_SUPPORTED(NFC_TAG_FEATURE);
1034
1035         /* LCOV_EXCL_START */
1036         CHECK_INIT();
1037         CHECK_INVALID(tag == NULL);
1038         CHECK_ACTIVATED();
1039
1040         ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
1041         if (ret == NET_NFC_OK) {
1042                 GVariant *parameter;
1043
1044                 parameter = g_variant_new("(uu)",
1045                         GPOINTER_TO_UINT(callback),
1046                         GPOINTER_TO_UINT(user_data));
1047                 if (parameter != NULL) {
1048                         ret = net_nfc_client_mifare_decrement(
1049                                 handle,
1050                                 block_index,
1051                                 value,
1052                                 _mifare_decrement_cb,
1053                                 parameter);
1054                         if (ret != NET_NFC_OK)
1055                                 g_variant_unref(parameter);
1056                 } else {
1057                         ret = NET_NFC_ALLOC_FAIL;
1058                 }
1059         } else {
1060                 LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
1061         }
1062
1063         return nfc_common_convert_error_code(__func__, ret);
1064         /* LCOV_EXCL_STOP */
1065 }
1066
1067 /* LCOV_EXCL_START */
1068 static void _mifare_transfer_cb(net_nfc_error_e result, void *user_data)
1069 {
1070         nfc_mifare_transfer_completed_cb callback;
1071         void *user_param;
1072
1073         LOG_BEGIN();
1074
1075         if (user_data == NULL) {
1076                 LOG_ERR("user_data is NULL");
1077                 return;
1078         }
1079
1080         g_variant_get((GVariant *)user_data,
1081                 "(uu)",
1082                 (guint *)&callback,
1083                 (guint *)&user_param);
1084
1085         if (callback != NULL)
1086                 callback(nfc_common_convert_error_code(__func__, result), user_param);
1087
1088         g_variant_unref((GVariant *)user_data);
1089 }
1090 /* LCOV_EXCL_STOP */
1091
1092 int nfc_mifare_transfer(nfc_tag_h tag,
1093         int block_index,
1094         nfc_mifare_transfer_completed_cb callback,
1095         void *user_data)
1096 {
1097         int ret;
1098         net_nfc_target_handle_h handle = NULL;
1099
1100         LOG_BEGIN();
1101
1102         CHECK_SUPPORTED(NFC_TAG_FEATURE);
1103
1104         /* LCOV_EXCL_START */
1105         CHECK_INIT();
1106         CHECK_INVALID(tag == NULL);
1107         CHECK_ACTIVATED();
1108
1109         ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
1110         if (ret == NET_NFC_OK) {
1111                 GVariant *parameter;
1112
1113                 parameter = g_variant_new("(uu)",
1114                         GPOINTER_TO_UINT(callback),
1115                         GPOINTER_TO_UINT(user_data));
1116                 if (parameter != NULL) {
1117                         ret = net_nfc_client_mifare_transfer(
1118                                 handle,
1119                                 block_index,
1120                                 _mifare_transfer_cb,
1121                                 parameter);
1122                         if (ret != NET_NFC_OK)
1123                                 g_variant_unref(parameter);
1124                 } else {
1125                         ret = NET_NFC_ALLOC_FAIL;
1126                 }
1127         } else {
1128                 LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
1129         }
1130
1131         return nfc_common_convert_error_code(__func__, ret);
1132         /* LCOV_EXCL_STOP */
1133 }
1134
1135 /* LCOV_EXCL_START */
1136 static void _mifare_restore_cb(net_nfc_error_e result, void *user_data)
1137 {
1138         nfc_mifare_restore_completed_cb callback;
1139         void *user_param;
1140
1141         LOG_BEGIN();
1142
1143         if (user_data == NULL) {
1144                 LOG_ERR("user_data is NULL");
1145                 return;
1146         }
1147
1148         g_variant_get((GVariant *)user_data,
1149                 "(uu)",
1150                 (guint *)&callback,
1151                 (guint *)&user_param);
1152
1153         if (callback != NULL)
1154                 callback(nfc_common_convert_error_code(__func__, result), user_param);
1155
1156         g_variant_unref((GVariant *)user_data);
1157 }
1158 /* LCOV_EXCL_STOP */
1159
1160 int nfc_mifare_restore(nfc_tag_h tag,
1161         int block_index,
1162         nfc_mifare_restore_completed_cb callback,
1163         void *user_data)
1164 {
1165         int ret;
1166         net_nfc_target_handle_h handle = NULL;
1167
1168         LOG_BEGIN();
1169
1170         CHECK_SUPPORTED(NFC_TAG_FEATURE);
1171
1172         /* LCOV_EXCL_START */
1173         CHECK_INIT();
1174         CHECK_INVALID(tag == NULL);
1175         CHECK_ACTIVATED();
1176
1177         ret = net_nfc_get_tag_handle((net_nfc_target_info_h)tag, &handle);
1178         if (ret == NET_NFC_OK) {
1179                 GVariant *parameter;
1180
1181                 parameter = g_variant_new("(uu)",
1182                         GPOINTER_TO_UINT(callback),
1183                         GPOINTER_TO_UINT(user_data));
1184                 if (parameter != NULL) {
1185                         ret = net_nfc_client_mifare_restore(
1186                                 handle,
1187                                 block_index,
1188                                 _mifare_restore_cb,
1189                                 parameter);
1190                         if (ret != NET_NFC_OK)
1191                                 g_variant_unref(parameter);
1192                 } else {
1193                         ret = NET_NFC_ALLOC_FAIL;
1194                 }
1195         } else {
1196                 LOG_ERR("net_nfc_get_tag_handle failed, [%d]", ret);
1197         }
1198
1199         return nfc_common_convert_error_code(__func__, ret);
1200         /* LCOV_EXCL_STOP */
1201 }
1202
1203 int nfc_barcode_get_barcode(unsigned char **barcode, int *barcode_len)
1204 {
1205         int ret;
1206
1207         LOG_BEGIN();
1208
1209         CHECK_SUPPORTED(NFC_TAG_FEATURE);
1210
1211         /* LCOV_EXCL_START */
1212         CHECK_INIT();
1213         CHECK_INVALID(barcode == NULL);
1214         CHECK_INVALID(barcode_len == NULL);
1215
1216         *barcode_len = 0;
1217
1218         ret = net_nfc_client_barcode_get_barcode_sync(barcode, barcode_len);
1219
1220         return nfc_common_convert_error_code(__func__, ret);
1221         /* LCOV_EXCL_STOP */
1222 }