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")))
34 static NetNfcGDbusTag *tag_proxy = NULL;
36 static NetNfcCallback tag_discovered_func_data;
37 static NetNfcCallback tag_detached_func_data;
39 static net_nfc_target_info_s *client_target_info = NULL;
40 static net_nfc_event_filter_e client_filter = NET_NFC_ALL_ENABLE;
42 static gboolean tag_check_filter(net_nfc_target_type_e type);
44 static void tag_get_info_list(guint8 *buffer,
46 net_nfc_tag_info_s **list);
48 static void tag_get_target_info(guint handle,
50 gboolean is_ndef_supported,
51 guchar ndef_card_state,
53 guint actual_data_size,
55 GVariant *target_info_values,
57 net_nfc_target_info_s **info);
61 static void tag_is_tag_connected(GObject *source_object,
65 static void tag_get_current_tag_info(GObject *source_object,
69 static void tag_get_current_target_handle(GObject *source_object,
74 static void tag_tag_discovered(NetNfcGDbusTag *object,
77 gboolean arg_is_ndef_supported,
78 guchar arg_ndef_card_state,
79 guint arg_max_data_size,
80 guint arg_actual_data_size,
81 guint arg_number_of_keys,
82 GVariant *arg_target_info_values,
83 GVariant *arg_raw_data,
86 static void tag_tag_detached(NetNfcGDbusTag *object,
91 static gboolean tag_check_filter(net_nfc_target_type_e type)
93 net_nfc_event_filter_e converted = NET_NFC_ALL_ENABLE;
95 DEBUG_CLIENT_MSG("client filter = %d", client_filter);
97 if (type >= NET_NFC_ISO14443_A_PICC &&
98 type <= NET_NFC_MIFARE_DESFIRE_PICC)
100 converted = NET_NFC_ISO14443A_ENABLE;
102 else if (type >= NET_NFC_ISO14443_B_PICC &&
103 type <= NET_NFC_ISO14443_BPRIME_PICC)
105 converted = NET_NFC_ISO14443B_ENABLE;
107 else if (type == NET_NFC_FELICA_PICC)
109 converted = NET_NFC_FELICA_ENABLE;
111 else if (type == NET_NFC_JEWEL_PICC)
113 converted = NET_NFC_FELICA_ENABLE;
115 else if (type == NET_NFC_ISO15693_PICC)
117 converted = NET_NFC_ISO15693_ENABLE;
120 if ((converted & client_filter) == 0)
126 static void tag_get_info_list(guint8 *buffer,
128 net_nfc_tag_info_s **list)
130 net_nfc_tag_info_s *tmp_list = NULL;
131 net_nfc_tag_info_s *current = NULL;
136 guint8 *pos = buffer;
141 tmp_list = g_new0(net_nfc_tag_info_s, number_of_keys);
144 while (i < number_of_keys)
150 length = *pos; /* first values is length of key */
153 str = g_new0(gchar, length + 1);
154 memcpy(str, pos, length);
156 SECURE_MSG("key = [%s]", str);
163 length = *pos; /* first value is length of value */
169 net_nfc_create_data(&value, pos, length);
173 current->value = value;
182 static void tag_get_target_info(guint handle,
184 gboolean is_ndef_supported,
185 guchar ndef_card_state,
187 guint actual_data_size,
188 guint number_of_keys,
189 GVariant *target_info_values,
191 net_nfc_target_info_s **info)
193 guint8 *buffer = NULL;
194 net_nfc_target_info_s *info_data = NULL;
195 net_nfc_tag_info_s *list = NULL;
200 net_nfc_util_gdbus_variant_to_buffer(target_info_values,
203 tag_get_info_list(buffer, number_of_keys, &list);
205 info_data = g_new0(net_nfc_target_info_s, 1);
207 info_data->ndefCardState = ndef_card_state;
208 info_data->actualDataSize = actual_data_size;
209 info_data->maxDataSize = max_data_size;
210 info_data->devType = dev_type;
211 info_data->handle = GUINT_TO_POINTER(handle);
212 info_data->is_ndef_supported = (uint8_t)is_ndef_supported;
213 info_data->number_of_keys = number_of_keys;
214 info_data->tag_info_list = list;
216 net_nfc_util_gdbus_variant_to_data_s(raw_data,
217 &info_data->raw_data);
222 static void tag_is_tag_connected(GObject *source_object,
226 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
227 net_nfc_error_e out_result;
228 gboolean out_is_connected = false;
229 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
230 GError *error = NULL;
232 g_assert(user_data != NULL);
234 if (net_nfc_gdbus_tag_call_is_tag_connected_finish(
235 NET_NFC_GDBUS_TAG(source_object),
238 (gint32 *)&out_dev_type,
242 DEBUG_ERR_MSG("Can not finish is_tag_connected: %s",
246 out_result = NET_NFC_IPC_FAIL;
249 if (func_data->callback != NULL)
251 net_nfc_client_tag_is_tag_connected_completed callback =
252 (net_nfc_client_tag_is_tag_connected_completed)func_data->callback;
254 if (out_is_connected == FALSE)
255 out_result = NET_NFC_NOT_CONNECTED;
257 callback(out_result, out_dev_type, func_data->user_data);
263 static void tag_get_current_tag_info(GObject *source_object,
267 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
268 net_nfc_error_e out_result = NET_NFC_OK;
270 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
271 gboolean out_is_connected = FALSE;
272 gboolean out_is_ndef_supported = FALSE;
273 guchar out_ndef_card_state = 0;
274 guint out_handle = 0;
275 guint out_max_data_size = 0;
276 guint out_actual_data_size = 0;
277 guint out_number_of_keys = 0;
278 GVariant *out_target_info_values = NULL;
279 GVariant *out_raw_data = NULL;
281 GError *error = NULL;
283 g_assert(user_data != NULL);
285 if (net_nfc_gdbus_tag_call_get_current_tag_info_finish (
286 NET_NFC_GDBUS_TAG(source_object),
290 (gint *)&out_dev_type,
291 &out_is_ndef_supported,
292 &out_ndef_card_state,
294 &out_actual_data_size,
296 &out_target_info_values,
301 out_result = NET_NFC_IPC_FAIL;
303 DEBUG_ERR_MSG("Can not finish get_current_tag_info: %s",
308 if (out_result == NET_NFC_OK && out_is_connected == true) {
309 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
310 client_target_info = NULL;
312 if (tag_check_filter(out_dev_type) == true) {
313 tag_get_target_info(out_handle,
315 out_is_ndef_supported,
318 out_actual_data_size,
320 out_target_info_values,
322 &client_target_info);
324 INFO_MSG("The detected target is filtered out, type [%d]", out_dev_type);
326 out_is_connected = false;
330 if (func_data->callback != NULL)
332 net_nfc_client_tag_get_current_tag_info_completed callback =
333 (net_nfc_client_tag_get_current_tag_info_completed)func_data->callback;
335 if (out_result == NET_NFC_OK && out_is_connected == false)
336 out_result = NET_NFC_NOT_CONNECTED;
338 callback(out_result, client_target_info, func_data->user_data);
344 static void tag_get_current_target_handle(GObject *source_object,
348 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
349 net_nfc_error_e out_result = NET_NFC_OK;
350 net_nfc_target_handle_h out_handle = NULL;
351 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
352 gboolean out_is_connected = false;
353 GError *error = NULL;
355 g_assert(user_data != NULL);
357 if (net_nfc_gdbus_tag_call_get_current_target_handle_finish(
358 NET_NFC_GDBUS_TAG(source_object),
361 (guint *)&out_handle,
362 (gint *)&out_dev_type,
366 DEBUG_ERR_MSG("Can not finish get_current_target_handle: %s",
370 out_result = NET_NFC_IPC_FAIL;
373 if (func_data->callback != NULL)
375 net_nfc_client_tag_get_current_target_handle_completed callback =
376 (net_nfc_client_tag_get_current_target_handle_completed)func_data->callback;
378 if (out_result == NET_NFC_OK && out_is_connected == FALSE)
379 out_result = NET_NFC_NOT_CONNECTED;
382 GUINT_TO_POINTER(out_handle),
383 func_data->user_data);
389 static void tag_tag_discovered(NetNfcGDbusTag *object,
392 gboolean arg_is_ndef_supported,
393 guchar arg_ndef_card_state,
394 guint arg_max_data_size,
395 guint arg_actual_data_size,
396 guint arg_number_of_keys,
397 GVariant *arg_target_info_values,
398 GVariant *arg_raw_data,
401 INFO_MSG(">>> SIGNAL arrived");
403 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
404 client_target_info = NULL;
406 if (tag_check_filter(arg_dev_type) == FALSE) {
407 DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
412 tag_get_target_info(arg_handle,
414 arg_is_ndef_supported,
417 arg_actual_data_size,
419 arg_target_info_values,
421 &client_target_info);
423 if (tag_discovered_func_data.callback != NULL) {
424 net_nfc_client_tag_tag_discovered callback =
425 (net_nfc_client_tag_tag_discovered)tag_discovered_func_data.callback;
427 callback(client_target_info,
428 tag_discovered_func_data.user_data);
432 static void tag_tag_detached(NetNfcGDbusTag *object,
437 INFO_MSG(">>> SIGNAL arrived");
439 if (tag_check_filter(arg_dev_type) == TRUE) {
440 if (tag_detached_func_data.callback != NULL) {
441 net_nfc_client_tag_tag_detached callback =
442 (net_nfc_client_tag_tag_detached)tag_detached_func_data.callback;
444 callback(tag_detached_func_data.user_data);
447 DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
450 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
451 client_target_info = NULL;
454 /* internal function */
455 gboolean net_nfc_client_tag_is_connected(void)
457 if (client_target_info == NULL)
460 if (client_target_info->handle == NULL)
466 net_nfc_target_info_s *net_nfc_client_tag_get_client_target_info(void)
468 return client_target_info;
472 net_nfc_error_e net_nfc_client_tag_is_tag_connected_sync(
473 net_nfc_target_type_e *dev_type)
475 net_nfc_target_info_s *info;
476 net_nfc_error_e result = NET_NFC_OK;
477 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
478 gboolean out_is_connected = false;
479 GError *error = NULL;
481 if (tag_proxy == NULL)
482 return NET_NFC_NOT_INITIALIZED;
484 /* prevent executing daemon when nfc is off */
485 if (net_nfc_client_manager_is_activated() == false) {
486 return NET_NFC_NOT_ACTIVATED;
489 info = net_nfc_client_tag_get_client_target_info();
491 /* try to request target information from server */
492 if (net_nfc_gdbus_tag_call_is_tag_connected_sync(tag_proxy,
495 (gint *)&out_dev_type,
499 DEBUG_ERR_MSG("Can not get is_tag_connected result: %s",
501 result = NET_NFC_IPC_FAIL;
508 if (out_is_connected == true) {
510 *dev_type = out_dev_type;
514 result = NET_NFC_NOT_CONNECTED;
517 /* target was connected */
518 if (dev_type != NULL) {
519 *dev_type = info->devType;
529 net_nfc_error_e net_nfc_client_barcode_get_barcode_sync(
530 unsigned char **barcode, int* barcode_len)
533 GError *error = NULL;
534 GVariant *out_barcode = NULL;
535 net_nfc_error_e result = NET_NFC_OK;
537 if (tag_proxy == NULL)
538 return NET_NFC_NOT_INITIALIZED;
540 /* prevent executing daemon when nfc is off */
541 if (net_nfc_client_manager_is_activated() == false) {
542 return NET_NFC_NOT_ACTIVATED;
545 if (net_nfc_gdbus_tag_call_get_barcode_sync(tag_proxy,
551 DEBUG_ERR_MSG("Can no get barcode result: %s", error->message);
552 result = NET_NFC_IPC_FAIL;
559 if(result == NET_NFC_OK)
561 data = g_new0(data_s, 1);
562 net_nfc_util_gdbus_variant_to_data_s(out_barcode, data);
564 if(data == NULL || data->buffer == NULL || data->length == 0)
565 return NET_NFC_OPERATION_FAIL;
568 *barcode_len = data->length;
569 *barcode = (unsigned char *)calloc(*barcode_len, sizeof(unsigned char));
570 memcpy(*barcode, data->buffer, *barcode_len);
573 net_nfc_util_free_data(data);
581 net_nfc_error_e net_nfc_client_tag_get_current_tag_info_sync(
582 net_nfc_target_info_h *info)
584 net_nfc_error_e result = NET_NFC_OK;
585 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
586 gboolean out_is_connected = false;
587 gboolean out_is_ndef_supported = false;
588 guchar out_ndef_card_state = 0;
589 guint out_handle = 0;
590 guint out_max_data_size = 0;
591 guint out_actual_data_size = 0;
592 guint out_number_of_keys = 0;
593 GVariant *out_target_info_values = NULL;
594 GVariant *out_raw_data = NULL;
595 GError *error = NULL;
597 if (tag_proxy == NULL)
598 return NET_NFC_NOT_INITIALIZED;
600 /* prevent executing daemon when nfc is off */
601 if (net_nfc_client_manager_is_activated() == false) {
602 return NET_NFC_NOT_ACTIVATED;
605 if (net_nfc_client_tag_get_client_target_info() == NULL) {
606 /* try to request target information from server */
607 if (net_nfc_gdbus_tag_call_get_current_tag_info_sync(tag_proxy,
611 (gint *)&out_dev_type,
612 &out_is_ndef_supported,
613 &out_ndef_card_state,
615 &out_actual_data_size,
617 &out_target_info_values,
622 DEBUG_ERR_MSG("Can no get current_tag_info result: %s",
624 result = NET_NFC_IPC_FAIL;
631 if (result == NET_NFC_OK) {
632 if (out_is_connected == true) {
633 if (tag_check_filter(out_dev_type) == true) {
634 tag_get_target_info(out_handle,
636 out_is_ndef_supported,
639 out_actual_data_size,
641 out_target_info_values,
643 &client_target_info);
647 DEBUG_CLIENT_MSG("The detected target is filtered out");
649 result = NET_NFC_NOT_CONNECTED;
652 result = NET_NFC_NOT_CONNECTED;
659 if (result == NET_NFC_OK && info != NULL) {
660 *info = client_target_info;
667 net_nfc_error_e net_nfc_client_tag_get_current_target_handle_sync(
668 net_nfc_target_handle_h *handle)
670 net_nfc_target_info_s *info;
671 net_nfc_error_e result;
672 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
673 gboolean out_is_connected = false;
674 guint out_handle = 0;
675 GError *error = NULL;
677 if (tag_proxy == NULL)
678 return NET_NFC_NOT_INITIALIZED;
680 /* prevent executing daemon when nfc is off */
681 if (net_nfc_client_manager_is_activated() == false) {
682 return NET_NFC_NOT_ACTIVATED;
685 info = net_nfc_client_tag_get_client_target_info();
687 if (net_nfc_gdbus_tag_call_get_current_target_handle_sync(
692 (gint *)&out_dev_type,
696 DEBUG_ERR_MSG("Can no get current_target_handle result: %s",
698 result = NET_NFC_IPC_FAIL;
705 if (result == NET_NFC_OK) {
706 if (out_is_connected == true) {
708 *handle = GUINT_TO_POINTER(out_handle);
712 result = NET_NFC_NOT_CONNECTED;
715 } else if (info->devType == NET_NFC_NFCIP1_INITIATOR ||
716 info->devType == NET_NFC_NFCIP1_TARGET) {
718 *handle = info->handle;
722 result = NET_NFC_NOT_CONNECTED;
729 void net_nfc_client_tag_set_tag_discovered(
730 net_nfc_client_tag_tag_discovered callback,
733 if (callback == NULL)
736 if (tag_proxy == NULL)
738 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_discovered_func_data.callback = (gpointer)callback;
747 tag_discovered_func_data.user_data = user_data;
751 void net_nfc_client_tag_unset_tag_discovered(void)
753 tag_discovered_func_data.callback = NULL;
754 tag_discovered_func_data.user_data = NULL;
758 void net_nfc_client_tag_set_tag_detached(
759 net_nfc_client_tag_tag_detached callback,
762 if (callback == NULL)
765 if (tag_proxy == NULL)
767 if (net_nfc_client_tag_init() != NET_NFC_OK)
769 DEBUG_ERR_MSG("tag_proxy fail");
770 /* FIXME : return result of this error */
775 tag_detached_func_data.callback = (gpointer)callback;
776 tag_detached_func_data.user_data = user_data;
780 void net_nfc_client_tag_unset_tag_detached(void)
782 tag_detached_func_data.callback = NULL;
783 tag_detached_func_data.user_data = NULL;
787 void net_nfc_client_tag_set_filter(net_nfc_event_filter_e filter)
789 client_filter = filter;
793 net_nfc_event_filter_e net_nfc_client_tag_get_filter(void)
795 return client_filter;
798 net_nfc_error_e net_nfc_client_tag_init(void)
800 GError *error = NULL;
804 DEBUG_CLIENT_MSG("Alrady initialized");
809 if (client_target_info)
811 net_nfc_release_tag_info(
812 (net_nfc_target_info_h)client_target_info);
813 client_target_info = NULL;
816 client_filter = NET_NFC_ALL_ENABLE;
818 tag_proxy = net_nfc_gdbus_tag_proxy_new_for_bus_sync(
820 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
821 "org.tizen.NetNfcService",
822 "/org/tizen/NetNfcService/Tag",
825 if (tag_proxy == NULL)
827 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
830 return NET_NFC_UNKNOWN_ERROR;
833 g_signal_connect(tag_proxy, "tag-discovered",
834 G_CALLBACK(tag_tag_discovered), NULL);
836 g_signal_connect(tag_proxy, "tag-detached",
837 G_CALLBACK(tag_tag_detached), NULL);
842 void net_nfc_client_tag_deinit(void)
844 client_filter = NET_NFC_ALL_ENABLE;
846 net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
847 client_target_info = NULL;
849 net_nfc_client_tag_unset_tag_discovered();
850 net_nfc_client_tag_unset_tag_detached();
854 g_object_unref(tag_proxy);