2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
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"
30 #ifndef NET_NFC_EXPORT_API
31 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
36 static NetNfcGDbusTag *tag_proxy = NULL;
38 static NetNfcCallback tag_discovered_func_data;
39 static NetNfcCallback tag_detached_func_data;
41 static net_nfc_target_info_s *client_target_info = NULL;
42 static net_nfc_event_filter_e client_filter = NET_NFC_ALL_ENABLE;
44 static gboolean tag_check_filter(net_nfc_target_type_e type);
46 static void tag_get_info_list(guint8 *buffer,
48 net_nfc_tag_info_s **list);
50 static void tag_get_target_info(guint handle,
52 gboolean is_ndef_supported,
53 guchar ndef_card_state,
55 guint actual_data_size,
57 GVariant *target_info_values,
59 net_nfc_target_info_s **info);
63 static void tag_is_tag_connected(GObject *source_object,
67 static void tag_get_current_tag_info(GObject *source_object,
71 static void tag_get_current_target_handle(GObject *source_object,
76 static void tag_tag_discovered(NetNfcGDbusTag *object,
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,
88 static void tag_tag_detached(NetNfcGDbusTag *object,
93 static gboolean tag_check_filter(net_nfc_target_type_e type)
95 net_nfc_event_filter_e converted = NET_NFC_ALL_ENABLE;
97 DEBUG_CLIENT_MSG("client filter = %d", client_filter);
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;
113 if ((converted & client_filter) == 0)
119 static void tag_get_info_list(guint8 *buffer,
121 net_nfc_tag_info_s **list)
123 net_nfc_tag_info_s *tmp_list = NULL;
124 net_nfc_tag_info_s *current = NULL;
129 guint8 *pos = buffer;
134 tmp_list = g_new0(net_nfc_tag_info_s, number_of_keys);
137 while (i < number_of_keys) {
142 length = *pos; /* first values is length of key */
145 str = g_new0(gchar, length + 1);
146 memcpy(str, pos, length);
148 SECURE_MSG("key = [%s]", str);
155 length = *pos; /* first value is length of value */
160 net_nfc_create_data(&value, pos, length);
164 current->value = value;
173 static void tag_get_target_info(guint handle,
175 gboolean is_ndef_supported,
176 guchar ndef_card_state,
178 guint actual_data_size,
179 guint number_of_keys,
180 GVariant *target_info_values,
182 net_nfc_target_info_s **info)
184 guint8 *buffer = NULL;
185 net_nfc_target_info_s *info_data = NULL;
186 net_nfc_tag_info_s *list = NULL;
191 net_nfc_util_gdbus_variant_to_buffer(target_info_values,
194 tag_get_info_list(buffer, number_of_keys, &list);
196 info_data = g_new0(net_nfc_target_info_s, 1);
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;
207 net_nfc_util_gdbus_variant_to_data_s(raw_data,
208 &info_data->raw_data);
215 static void tag_is_tag_connected(GObject *source_object,
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;
225 g_assert(user_data != NULL);
227 if (net_nfc_gdbus_tag_call_is_tag_connected_finish(
228 NET_NFC_GDBUS_TAG(source_object),
231 (gint32 *)&out_dev_type,
234 DEBUG_ERR_MSG("Can not finish is_tag_connected: %s",
238 out_result = NET_NFC_IPC_FAIL;
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;
245 if (out_is_connected == FALSE)
246 out_result = NET_NFC_NOT_CONNECTED;
248 callback(out_result, out_dev_type, func_data->user_data);
254 static void tag_get_current_tag_info(GObject *source_object,
258 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
259 net_nfc_error_e out_result = NET_NFC_OK;
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;
272 GError *error = NULL;
274 g_assert(user_data != NULL);
276 if (net_nfc_gdbus_tag_call_get_current_tag_info_finish(
277 NET_NFC_GDBUS_TAG(source_object),
281 (gint *)&out_dev_type,
282 &out_is_ndef_supported,
283 &out_ndef_card_state,
285 &out_actual_data_size,
287 &out_target_info_values,
291 out_result = NET_NFC_IPC_FAIL;
293 DEBUG_ERR_MSG("Can not finish get_current_tag_info: %s",
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;
302 if (tag_check_filter(out_dev_type) == true) {
303 tag_get_target_info(out_handle,
305 out_is_ndef_supported,
308 out_actual_data_size,
310 out_target_info_values,
312 &client_target_info);
314 INFO_MSG("The detected target is filtered out, type [%d]", out_dev_type);
316 out_is_connected = false;
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;
324 if (out_result == NET_NFC_OK && out_is_connected == false)
325 out_result = NET_NFC_NOT_CONNECTED;
327 callback(out_result, client_target_info, func_data->user_data);
333 static void tag_get_current_target_handle(GObject *source_object,
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;
344 g_assert(user_data != NULL);
346 if (net_nfc_gdbus_tag_call_get_current_target_handle_finish(
347 NET_NFC_GDBUS_TAG(source_object),
350 (guint *)&out_handle,
351 (gint *)&out_dev_type,
354 DEBUG_ERR_MSG("Can not finish get_current_target_handle: %s",
358 out_result = NET_NFC_IPC_FAIL;
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;
365 if (out_result == NET_NFC_OK && out_is_connected == FALSE)
366 out_result = NET_NFC_NOT_CONNECTED;
369 GUINT_TO_POINTER(out_handle),
370 func_data->user_data);
376 static void tag_tag_discovered(NetNfcGDbusTag *object,
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,
388 INFO_MSG(">>> SIGNAL arrived");
390 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
391 client_target_info = NULL;
393 if (tag_check_filter(arg_dev_type) == FALSE) {
394 DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
399 tag_get_target_info(arg_handle,
401 arg_is_ndef_supported,
404 arg_actual_data_size,
406 arg_target_info_values,
408 &client_target_info);
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;
414 callback(client_target_info,
415 tag_discovered_func_data.user_data);
419 static void tag_tag_detached(NetNfcGDbusTag *object,
424 INFO_MSG(">>> SIGNAL arrived");
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;
431 callback(tag_detached_func_data.user_data);
434 DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
437 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
438 client_target_info = NULL;
441 /* internal function */
442 gboolean net_nfc_client_tag_is_connected(void)
444 if (client_target_info == NULL)
447 if (client_target_info->handle == NULL)
453 net_nfc_target_info_s *net_nfc_client_tag_get_client_target_info(void)
455 return client_target_info;
459 net_nfc_error_e net_nfc_client_tag_is_tag_connected_sync(
460 net_nfc_target_type_e *dev_type)
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;
468 if (tag_proxy == NULL)
469 return NET_NFC_NOT_INITIALIZED;
471 /* prevent executing daemon when nfc is off */
472 if (net_nfc_client_manager_is_activated() == false)
473 return NET_NFC_NOT_ACTIVATED;
475 info = net_nfc_client_tag_get_client_target_info();
477 /* try to request target information from server */
478 if (net_nfc_gdbus_tag_call_is_tag_connected_sync(tag_proxy,
481 (gint *)&out_dev_type,
484 DEBUG_ERR_MSG("Can not get is_tag_connected result: %s",
486 result = NET_NFC_IPC_FAIL;
493 if (out_is_connected == true) {
495 *dev_type = out_dev_type;
499 result = NET_NFC_NOT_CONNECTED;
502 /* target was connected */
503 if (dev_type != NULL)
504 *dev_type = info->devType;
513 net_nfc_error_e net_nfc_client_barcode_get_barcode_sync(
514 unsigned char **barcode, int* barcode_len)
517 GError *error = NULL;
518 GVariant *out_barcode = NULL;
519 net_nfc_error_e result = NET_NFC_OK;
521 if (tag_proxy == NULL)
522 return NET_NFC_NOT_INITIALIZED;
524 /* prevent executing daemon when nfc is off */
525 if (net_nfc_client_manager_is_activated() == false)
526 return NET_NFC_NOT_ACTIVATED;
528 if (net_nfc_gdbus_tag_call_get_barcode_sync(tag_proxy,
533 DEBUG_ERR_MSG("Can no get barcode result: %s", error->message);
534 result = NET_NFC_IPC_FAIL;
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);
545 if (data == NULL || data->buffer == NULL || data->length == 0)
546 return NET_NFC_OPERATION_FAIL;
549 *barcode_len = data->length;
550 *barcode = (unsigned char *)calloc(*barcode_len, sizeof(unsigned char));
551 memcpy(*barcode, data->buffer, *barcode_len);
554 net_nfc_util_free_data(data);
561 net_nfc_error_e net_nfc_client_tag_get_current_tag_info_sync(
562 net_nfc_target_info_h *info)
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;
577 if (tag_proxy == NULL)
578 return NET_NFC_NOT_INITIALIZED;
580 /* prevent executing daemon when nfc is off */
581 if (net_nfc_client_manager_is_activated() == false)
582 return NET_NFC_NOT_ACTIVATED;
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,
590 (gint *)&out_dev_type,
591 &out_is_ndef_supported,
592 &out_ndef_card_state,
594 &out_actual_data_size,
596 &out_target_info_values,
600 DEBUG_ERR_MSG("Can no get current_tag_info result: %s",
602 result = NET_NFC_IPC_FAIL;
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,
614 out_is_ndef_supported,
617 out_actual_data_size,
619 out_target_info_values,
621 &client_target_info);
625 DEBUG_CLIENT_MSG("The detected target is filtered out");
627 result = NET_NFC_NOT_CONNECTED;
630 result = NET_NFC_NOT_CONNECTED;
637 if (result == NET_NFC_OK && info != NULL)
638 *info = client_target_info;
644 net_nfc_error_e net_nfc_client_tag_get_current_target_handle_sync(
645 net_nfc_target_handle_h *handle)
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;
654 if (tag_proxy == NULL)
655 return NET_NFC_NOT_INITIALIZED;
657 /* prevent executing daemon when nfc is off */
658 if (net_nfc_client_manager_is_activated() == false)
659 return NET_NFC_NOT_ACTIVATED;
661 info = net_nfc_client_tag_get_client_target_info();
663 if (net_nfc_gdbus_tag_call_get_current_target_handle_sync(
668 (gint *)&out_dev_type,
671 DEBUG_ERR_MSG("Can no get current_target_handle result: %s",
673 result = NET_NFC_IPC_FAIL;
680 if (result == NET_NFC_OK) {
681 if (out_is_connected == true) {
683 *handle = GUINT_TO_POINTER(out_handle);
687 result = NET_NFC_NOT_CONNECTED;
690 } else if (info->devType == NET_NFC_NFCIP1_INITIATOR ||
691 info->devType == NET_NFC_NFCIP1_TARGET) {
693 *handle = info->handle;
697 result = NET_NFC_NOT_CONNECTED;
704 void net_nfc_client_tag_set_tag_discovered(
705 net_nfc_client_tag_tag_discovered callback,
708 if (callback == NULL)
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 */
719 tag_discovered_func_data.callback = (gpointer)callback;
720 tag_discovered_func_data.user_data = user_data;
724 void net_nfc_client_tag_unset_tag_discovered(void)
726 tag_discovered_func_data.callback = NULL;
727 tag_discovered_func_data.user_data = NULL;
731 void net_nfc_client_tag_set_tag_detached(
732 net_nfc_client_tag_tag_detached callback,
735 if (callback == NULL)
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 */
746 tag_detached_func_data.callback = (gpointer)callback;
747 tag_detached_func_data.user_data = user_data;
751 void net_nfc_client_tag_unset_tag_detached(void)
753 tag_detached_func_data.callback = NULL;
754 tag_detached_func_data.user_data = NULL;
758 void net_nfc_client_tag_set_filter(net_nfc_event_filter_e filter)
760 client_filter = filter;
764 net_nfc_event_filter_e net_nfc_client_tag_get_filter(void)
766 return client_filter;
769 net_nfc_error_e net_nfc_client_tag_init(void)
771 GError *error = NULL;
774 DEBUG_CLIENT_MSG("Alrady initialized");
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;
785 client_filter = NET_NFC_ALL_ENABLE;
787 tag_proxy = net_nfc_gdbus_tag_proxy_new_for_bus_sync(
789 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
790 "org.tizen.NetNfcService",
791 "/org/tizen/NetNfcService/Tag",
794 if (tag_proxy == NULL) {
795 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
798 return NET_NFC_UNKNOWN_ERROR;
801 g_signal_connect(tag_proxy, "tag-discovered",
802 G_CALLBACK(tag_tag_discovered), NULL);
804 g_signal_connect(tag_proxy, "tag-detached",
805 G_CALLBACK(tag_tag_detached), NULL);
810 void net_nfc_client_tag_deinit(void)
812 client_filter = NET_NFC_ALL_ENABLE;
814 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
815 client_target_info = NULL;
817 net_nfc_client_tag_unset_tag_discovered();
818 net_nfc_client_tag_unset_tag_detached();
821 g_object_unref(tag_proxy);