2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "net_nfc_typedef_internal.h"
17 #include "net_nfc_debug_internal.h"
18 #include "net_nfc_util_gdbus_internal.h"
19 #include "net_nfc_gdbus.h"
20 #include "net_nfc_data.h"
21 #include "net_nfc_target_info.h"
22 #include "net_nfc_client.h"
23 #include "net_nfc_client_manager.h"
24 #include "net_nfc_client_tag.h"
25 #include "net_nfc_client_tag_internal.h"
26 #include "net_nfc_neard.h"
29 static NetNfcGDbusTag *tag_proxy = NULL;
31 static NetNfcCallback tag_discovered_func_data;
32 static NetNfcCallback tag_detached_func_data;
34 static net_nfc_target_info_s *client_target_info = NULL;
35 static net_nfc_event_filter_e client_filter = NET_NFC_ALL_ENABLE;
37 static gboolean tag_check_filter(net_nfc_target_type_e type)
39 net_nfc_event_filter_e converted = NET_NFC_ALL_ENABLE;
41 NFC_DBG("client filter = %d", client_filter);
43 if (type >= NET_NFC_ISO14443_A_PICC
44 && type <= NET_NFC_MIFARE_DESFIRE_PICC)
46 converted = NET_NFC_ISO14443A_ENABLE;
48 else if (type >= NET_NFC_ISO14443_B_PICC
49 && type <= NET_NFC_ISO14443_BPRIME_PICC)
51 converted = NET_NFC_ISO14443B_ENABLE;
53 else if (type == NET_NFC_FELICA_PICC)
55 converted = NET_NFC_FELICA_ENABLE;
57 else if (type == NET_NFC_JEWEL_PICC)
59 converted = NET_NFC_FELICA_ENABLE;
61 else if (type == NET_NFC_ISO15693_PICC)
63 converted = NET_NFC_ISO15693_ENABLE;
66 if ((converted & client_filter) == 0)
72 static void tag_get_info_list(guint8 *buffer, gint number_of_keys,
73 net_nfc_tag_info_s **list)
78 net_nfc_tag_info_s *current = NULL;
79 net_nfc_tag_info_s *tmp_list = NULL;
81 RET_IF(NULL == buffer);
83 tmp_list = g_new0(net_nfc_tag_info_s, number_of_keys);
86 while (i < number_of_keys)
92 length = *pos; /* first values is length of key */
95 str = g_new0(gchar, length + 1);
96 memcpy(str, pos, length);
98 NFC_DBG("key = [%s]", str);
105 length = *pos; /* first value is length of value */
111 net_nfc_create_data(&value, pos, length);
115 current->value = value;
124 static void tag_get_target_info(guint handle,
126 gboolean is_ndef_supported,
127 guchar ndef_card_state,
129 guint actual_data_size,
130 guint number_of_keys,
131 GVariant *target_info_values,
133 net_nfc_target_info_s **info)
135 guint8 *buffer = NULL;
136 net_nfc_tag_info_s *list = NULL;
137 net_nfc_target_info_s *info_data = NULL;
139 RET_IF(NULL == info);
141 net_nfc_util_gdbus_variant_to_buffer(target_info_values, &buffer, NULL);
143 tag_get_info_list(buffer, number_of_keys, &list);
145 info_data = g_new0(net_nfc_target_info_s, 1);
147 info_data->ndefCardState = ndef_card_state;
148 info_data->actualDataSize = actual_data_size;
149 info_data->maxDataSize = max_data_size;
150 info_data->devType = dev_type;
151 info_data->handle = GUINT_TO_POINTER(handle);
152 info_data->is_ndef_supported = (uint8_t)is_ndef_supported;
153 info_data->number_of_keys = number_of_keys;
154 info_data->tag_info_list = list;
156 net_nfc_util_gdbus_variant_to_data_s(raw_data, &info_data->raw_data);
161 static void tag_is_tag_connected(GObject *source_object,
162 GAsyncResult *res, gpointer user_data)
165 GError *error = NULL;
166 net_nfc_error_e out_result;
167 gboolean out_is_connected = false;
168 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
169 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
171 g_assert(user_data != NULL);
173 ret = net_nfc_gdbus_tag_call_is_tag_connected_finish(
174 NET_NFC_GDBUS_TAG(source_object),
177 (gint32 *)&out_dev_type,
183 NFC_ERR("Can not finish is_tag_connected: %s", error->message);
186 out_result = NET_NFC_IPC_FAIL;
189 if (func_data->callback != NULL)
191 net_nfc_client_tag_is_tag_connected_completed callback =
192 (net_nfc_client_tag_is_tag_connected_completed)func_data->callback;
194 if (out_is_connected == FALSE)
195 out_result = NET_NFC_NOT_CONNECTED;
197 callback(out_result, out_dev_type, func_data->user_data);
203 static void tag_get_current_tag_info(GObject *source_object,
204 GAsyncResult *res, gpointer user_data)
207 guint out_handle = 0;
208 GError *error = NULL;
209 guint out_max_data_size = 0;
210 guint out_number_of_keys = 0;
211 GVariant *out_raw_data = NULL;
212 guchar out_ndef_card_state = 0;
213 guint out_actual_data_size = 0;
214 gboolean out_is_connected = FALSE;
215 gboolean out_is_ndef_supported = FALSE;
216 GVariant *out_target_info_values = NULL;
217 net_nfc_error_e out_result = NET_NFC_OK;
218 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
219 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
221 g_assert(user_data != NULL);
223 ret = net_nfc_gdbus_tag_call_get_current_tag_info_finish (
224 NET_NFC_GDBUS_TAG(source_object),
228 (gint *)&out_dev_type,
229 &out_is_ndef_supported,
230 &out_ndef_card_state,
232 &out_actual_data_size,
234 &out_target_info_values,
241 out_result = NET_NFC_IPC_FAIL;
243 NFC_ERR("Can not finish get_current_tag_info: %s", error->message);
247 if (out_result == NET_NFC_OK && out_is_connected == true)
249 net_nfc_release_tag_info(client_target_info);
250 client_target_info = NULL;
252 if (tag_check_filter(out_dev_type) == true)
254 tag_get_target_info(out_handle,
256 out_is_ndef_supported,
259 out_actual_data_size,
261 out_target_info_values,
263 &client_target_info);
267 NFC_INFO("The detected target is filtered out, type [%d]", out_dev_type);
269 out_is_connected = false;
273 if (func_data->callback != NULL)
275 net_nfc_client_tag_get_current_tag_info_completed callback =
276 (net_nfc_client_tag_get_current_tag_info_completed)func_data->callback;
278 if (out_result == NET_NFC_OK && out_is_connected == false)
279 out_result = NET_NFC_NOT_CONNECTED;
281 callback(out_result, client_target_info, func_data->user_data);
287 static void tag_get_current_target_handle(GObject *source_object,
288 GAsyncResult *res, gpointer user_data)
291 GError *error = NULL;
292 gboolean out_is_connected = false;
293 net_nfc_error_e out_result = NET_NFC_OK;
294 net_nfc_target_handle_s *out_handle = NULL;
295 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
296 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
298 g_assert(user_data != NULL);
300 ret = net_nfc_gdbus_tag_call_get_current_target_handle_finish(
301 NET_NFC_GDBUS_TAG(source_object),
304 (guint *)&out_handle,
305 (gint *)&out_dev_type,
311 NFC_ERR("Can not finish get_current_target_handle: %s", error->message);
314 out_result = NET_NFC_IPC_FAIL;
317 if (func_data->callback != NULL)
319 net_nfc_client_tag_get_current_target_handle_completed callback =
320 (net_nfc_client_tag_get_current_target_handle_completed)func_data->callback;
322 if (out_result == NET_NFC_OK && out_is_connected == FALSE)
323 out_result = NET_NFC_NOT_CONNECTED;
325 callback(out_result, GUINT_TO_POINTER(out_handle), func_data->user_data);
331 static void tag_tag_discovered(NetNfcGDbusTag *object,
334 gboolean arg_is_ndef_supported,
335 guchar arg_ndef_card_state,
336 guint arg_max_data_size,
337 guint arg_actual_data_size,
338 guint arg_number_of_keys,
339 GVariant *arg_target_info_values,
340 GVariant *arg_raw_data,
343 net_nfc_client_tag_tag_discovered callback;
345 NFC_INFO(">>> SIGNAL arrived");
347 net_nfc_release_tag_info(client_target_info);
348 client_target_info = NULL;
350 if (tag_check_filter(arg_dev_type) == FALSE)
352 NFC_INFO("The detected target is filtered out, type [%d]", arg_dev_type);
357 tag_get_target_info(arg_handle,
359 arg_is_ndef_supported,
362 arg_actual_data_size,
364 arg_target_info_values,
366 &client_target_info);
368 if (tag_discovered_func_data.callback != NULL)
370 callback = (net_nfc_client_tag_tag_discovered)tag_discovered_func_data.callback;
372 callback(client_target_info, tag_discovered_func_data.user_data);
376 static void tag_tag_detached(NetNfcGDbusTag *object, guint arg_handle,
377 gint arg_dev_type, gpointer user_data)
379 NFC_INFO(">>> SIGNAL arrived");
380 net_nfc_client_tag_tag_detached callback;
381 if (tag_check_filter(arg_dev_type) == TRUE)
383 if (tag_detached_func_data.callback != NULL)
385 callback = (net_nfc_client_tag_tag_detached)tag_detached_func_data.callback;
387 callback(tag_detached_func_data.user_data);
392 NFC_INFO("The detected target is filtered out, type [%d]", arg_dev_type);
395 net_nfc_release_tag_info(client_target_info);
396 client_target_info = NULL;
399 /* internal function */
400 gboolean net_nfc_client_tag_is_connected(void)
402 return net_nfc_neard_is_tag_connected();
405 net_nfc_target_info_s *net_nfc_client_tag_get_client_target_info(void)
407 net_nfc_neard_get_current_tag_info(&client_target_info);
408 return client_target_info;
413 API net_nfc_error_e net_nfc_client_tag_is_tag_connected(
414 net_nfc_client_tag_is_tag_connected_completed callback, void *user_data)
416 NetNfcCallback *func_data;
418 RETV_IF(NULL == tag_proxy, NET_NFC_NOT_INITIALIZED);
420 /* prevent executing daemon when nfc is off */
421 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
423 func_data = g_try_new0(NetNfcCallback, 1);
424 if (func_data == NULL)
425 return NET_NFC_ALLOC_FAIL;
427 func_data->callback = (gpointer)callback;
428 func_data->user_data = user_data;
430 net_nfc_gdbus_tag_call_is_tag_connected(tag_proxy,
431 net_nfc_client_gdbus_get_privilege(),
433 tag_is_tag_connected,
440 API net_nfc_error_e net_nfc_client_tag_is_tag_connected_sync(
441 net_nfc_target_type_e *dev_type)
444 GError *error = NULL;
445 net_nfc_target_info_s *info;
446 gboolean out_is_connected = FALSE;
447 net_nfc_error_e result = NET_NFC_OK;
448 net_nfc_target_type_e out_dev_type = NET_NFC_UNKNOWN_TARGET;
450 RETV_IF(NULL == tag_proxy, NET_NFC_NOT_INITIALIZED);
452 /* prevent executing daemon when nfc is off */
453 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
455 info = net_nfc_client_tag_get_client_target_info();
458 /* try to request target information from server */
459 ret = net_nfc_gdbus_tag_call_is_tag_connected_sync(tag_proxy,
460 net_nfc_client_gdbus_get_privilege(),
463 (gint *)&out_dev_type,
469 NFC_ERR("Can not get is_tag_connected result: %s", error->message);
472 return NET_NFC_IPC_FAIL;
475 if (TRUE == out_is_connected)
478 *dev_type = out_dev_type;
484 result = NET_NFC_NOT_CONNECTED;
489 /* target was connected */
490 if (dev_type != NULL)
491 *dev_type = info->devType;
500 API net_nfc_error_e net_nfc_client_tag_get_current_tag_info(
501 net_nfc_client_tag_get_current_tag_info_completed callback, void *user_data)
503 NetNfcCallback *func_data;
505 RETV_IF(NULL == tag_proxy, NET_NFC_NOT_INITIALIZED);
507 /* prevent executing daemon when nfc is off */
508 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
510 func_data = g_try_new0(NetNfcCallback, 1);
511 if (func_data == NULL)
512 return NET_NFC_ALLOC_FAIL;
514 func_data->callback = (gpointer)callback;
515 func_data->user_data = user_data;
517 net_nfc_gdbus_tag_call_get_current_tag_info(tag_proxy,
518 net_nfc_client_gdbus_get_privilege(),
520 tag_get_current_tag_info,
527 API net_nfc_error_e net_nfc_client_tag_get_current_tag_info_sync(
528 net_nfc_target_info_s **info)
530 /* prevent executing daemon when nfc is off */
531 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
533 return net_nfc_neard_get_current_tag_info(info);
537 API net_nfc_error_e net_nfc_client_tag_get_current_target_handle(
538 net_nfc_client_tag_get_current_target_handle_completed callback, void *user_data)
540 NetNfcCallback *func_data;
542 RETV_IF(NULL == tag_proxy, NET_NFC_NOT_INITIALIZED);
544 /* prevent executing daemon when nfc is off */
545 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
547 func_data = g_try_new0(NetNfcCallback, 1);
548 if (func_data == NULL)
549 return NET_NFC_ALLOC_FAIL;
551 func_data->callback = (gpointer)callback;
552 func_data->user_data = user_data;
554 net_nfc_gdbus_tag_call_get_current_target_handle(tag_proxy,
555 net_nfc_client_gdbus_get_privilege(),
557 tag_get_current_target_handle,
564 API net_nfc_error_e net_nfc_client_tag_get_current_target_handle_sync(
565 net_nfc_target_handle_s **handle)
567 /* prevent executing daemon when nfc is off */
568 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
570 return net_nfc_neard_get_current_target_handle(handle);
573 API void net_nfc_client_tag_set_tag_discovered(
574 net_nfc_client_tag_tag_discovered callback, void *user_data)
576 RET_IF(NULL == callback);
578 net_nfc_neard_set_tag_discovered(callback, user_data);
581 API void net_nfc_client_tag_unset_tag_discovered(void)
583 net_nfc_neard_unset_tag_discovered();
586 API void net_nfc_client_tag_set_tag_detached(
587 net_nfc_client_tag_tag_detached callback, void *user_data)
589 RET_IF(NULL == callback);
591 net_nfc_neard_set_tag_detached(callback, user_data);
594 API void net_nfc_client_tag_unset_tag_detached(void)
596 net_nfc_neard_unset_tag_detached();
599 API void net_nfc_client_tag_set_filter(net_nfc_event_filter_e filter)
601 client_filter = filter;
604 API net_nfc_event_filter_e net_nfc_client_tag_get_filter(void)
606 return client_filter;
609 net_nfc_error_e net_nfc_client_tag_init(void)
611 GError *error = NULL;
615 NFC_WARN("Alrady initialized");
619 if (client_target_info)
621 net_nfc_release_tag_info(client_target_info);
622 client_target_info = NULL;
625 client_filter = NET_NFC_ALL_ENABLE;
627 tag_proxy = net_nfc_gdbus_tag_proxy_new_for_bus_sync(
629 G_DBUS_PROXY_FLAGS_NONE,
630 "org.tizen.NetNfcService",
631 "/org/tizen/NetNfcService/Tag",
634 if (NULL == tag_proxy)
636 NFC_ERR("Can not create proxy : %s", error->message);
639 return NET_NFC_UNKNOWN_ERROR;
642 g_signal_connect(tag_proxy, "tag-discovered", G_CALLBACK(tag_tag_discovered), NULL);
643 g_signal_connect(tag_proxy, "tag-detached", G_CALLBACK(tag_tag_detached), NULL);
648 void net_nfc_client_tag_deinit(void)
650 client_filter = NET_NFC_ALL_ENABLE;
652 net_nfc_release_tag_info(client_target_info);
653 client_target_info = NULL;
655 net_nfc_client_tag_unset_tag_discovered();
656 net_nfc_client_tag_unset_tag_detached();
660 g_object_unref(tag_proxy);