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.
18 #include <glib-object.h>
20 #include "net_nfc_typedef_internal.h"
21 #include "net_nfc_client_tag.h"
22 #include "net_nfc_test_tag.h"
24 static net_nfc_target_info_s *global_info = NULL;
26 static void run_next_callback(gpointer user_data)
32 callback = (GCallback)(user_data);
38 static gchar *tag_type_to_string(net_nfc_target_type_e dev_type)
42 case NET_NFC_UNKNOWN_TARGET:
43 return "Unknown Target";
44 case NET_NFC_GENERIC_PICC:
45 return "Generic PICC";
46 case NET_NFC_ISO14443_A_PICC:
47 return "ISO14443 PICC";
48 case NET_NFC_ISO14443_4A_PICC:
49 return "ISO14443 4A PICC";
50 case NET_NFC_ISO14443_3A_PICC:
51 return "ISO14443 3A PICC";
52 case NET_NFC_MIFARE_MINI_PICC:
53 return "Mifare mini PICC";
54 case NET_NFC_MIFARE_1K_PICC:
55 return "Mifare 1K PICC";
56 case NET_NFC_MIFARE_4K_PICC:
57 return "Mifare 4K PICC";
58 case NET_NFC_MIFARE_ULTRA_PICC:
59 return "Mifare Ultra PICC";
60 case NET_NFC_MIFARE_DESFIRE_PICC:
61 return "Mifare Desfire PICC";
62 case NET_NFC_ISO14443_B_PICC:
63 return "ISO14443 B PICC";
64 case NET_NFC_ISO14443_4B_PICC:
65 return "ISO14443 4B PICC";
66 case NET_NFC_ISO14443_BPRIME_PICC:
67 return "ISO14443 BPRIME PICC";
68 case NET_NFC_FELICA_PICC:
70 case NET_NFC_JEWEL_PICC:
72 case NET_NFC_ISO15693_PICC:
73 return "ISO15693 PICC";
74 case NET_NFC_NFCIP1_TARGET:
75 return "NFCIP1 Target";
76 case NET_NFC_NFCIP1_INITIATOR:
77 return "NFCIP1 Initiator";
81 return "Invalid Target";
84 static void print_is_tag_connected(net_nfc_target_type_e dev_type)
88 net_nfc_target_type_e type;
90 net_nfc_get_tag_type(global_info, &type);
93 g_print("DevType is same as Discovered tag\n");
97 static void print_get_current_target_handle(net_nfc_target_handle_s *handle)
99 net_nfc_target_handle_s *global_handle;
100 guint global_handle_id;
103 net_nfc_get_tag_handle(global_info, &global_handle);
105 global_handle_id = GPOINTER_TO_UINT(global_handle);
106 handle_id = GPOINTER_TO_UINT(handle);
108 g_print("Tag handle %x, Current Tag handle %x\n",
111 if (global_handle_id == handle_id)
112 g_print("Current Tag is matched discovered Tag\n");
115 static void print_get_current_tag_info(net_nfc_target_info_s *info)
117 net_nfc_target_handle_s *handle;
119 if (global_info == NULL)
121 g_print("Discovered tag info does not exist\n");
127 g_print("Current tag info does not exist\n");
131 net_nfc_get_tag_handle(info, &handle);
132 print_get_current_target_handle(handle);
137 static void tag_detached(void *user_data)
139 g_print("TagDetached\n");
142 static void is_tag_connected_completed(net_nfc_error_e result,
143 net_nfc_target_type_e dev_type,
146 g_print("IsTagConnected Completed %d\n", result);
147 g_print("--- dev type : %s (%d)\n", tag_type_to_string(dev_type),
150 if (result == NET_NFC_OK)
151 print_is_tag_connected(dev_type);
152 else if (result == NET_NFC_NOT_CONNECTED)
153 g_print("NET_NFC_NOT_CONNECTED\n");
155 run_next_callback(user_data);
158 static void get_current_tag_info_completed(net_nfc_error_e result,
159 net_nfc_target_info_s *info, void *user_data)
161 g_print("GetCurrentTagInfo Completed %d\n", result);
163 if (result == NET_NFC_OK)
164 print_get_current_tag_info(info);
166 run_next_callback(user_data);
169 static void get_current_target_handle_completed(net_nfc_error_e result,
170 net_nfc_target_handle_s *handle, void *user_data)
172 g_print("GetCurrentTargetHandle Completed %d\n", result);
174 if (result == NET_NFC_OK)
175 print_get_current_target_handle(handle);
177 run_next_callback(user_data);
182 static void tag_discovered(net_nfc_target_info_s *info, void *user_data)
184 g_print("TagDiscovered\n");
186 net_nfc_duplicate_target_info(info, &global_info);
188 run_next_callback(user_data);
193 void net_nfc_test_tag_is_tag_connected(gpointer data, gpointer user_data)
195 net_nfc_client_tag_is_tag_connected(is_tag_connected_completed,
199 void net_nfc_test_tag_get_current_tag_info(gpointer data, gpointer user_data)
201 net_nfc_client_tag_get_current_tag_info(get_current_tag_info_completed,
205 void net_nfc_test_tag_get_current_target_handle(gpointer data,
208 net_nfc_client_tag_get_current_target_handle(
209 get_current_target_handle_completed,
213 void net_nfc_test_tag_is_tag_connected_sync(gpointer data, gpointer user_data)
215 net_nfc_error_e result;
216 net_nfc_target_type_e dev_type;
218 result = net_nfc_client_tag_is_tag_connected_sync(&dev_type);
220 if (result == NET_NFC_OK)
221 print_is_tag_connected(dev_type);
222 else if (result == NET_NFC_NOT_CONNECTED)
223 g_print("NET_NFC_NOT_CONNECTED\n");
225 run_next_callback(user_data);
228 void net_nfc_test_tag_get_current_tag_info_sync(gpointer data,
231 net_nfc_error_e result;
232 net_nfc_target_info_s *info;
234 result = net_nfc_client_tag_get_current_tag_info_sync(&info);
236 if (result == NET_NFC_OK)
237 print_get_current_tag_info(info);
239 run_next_callback(user_data);
242 void net_nfc_test_tag_get_current_target_handle_sync(gpointer data,
245 net_nfc_error_e result;
246 net_nfc_target_handle_s *handle;
248 result = net_nfc_client_tag_get_current_target_handle_sync(&handle);
250 if (result == NET_NFC_OK)
251 print_get_current_target_handle(handle);
253 run_next_callback(user_data);
256 void net_nfc_test_tag_set_tag_discovered(gpointer data, gpointer user_data)
258 g_print("Waiting for TagDiscovered Signal\n");
260 net_nfc_client_tag_unset_tag_detached();
262 net_nfc_client_tag_set_tag_detached(tag_detached, NULL);
264 net_nfc_client_tag_unset_tag_discovered();
266 net_nfc_client_tag_set_tag_discovered(tag_discovered, user_data);
269 net_nfc_target_info_s* net_nfc_test_tag_get_target_info(void)
274 void net_nfc_test_tag_set_tag_detached(gpointer data, gpointer user_data)
276 g_print("Waiting for TagDetached Singal\n");
278 net_nfc_client_tag_set_tag_detached(tag_detached, NULL);
281 void net_nfc_test_tag_set_filter(gpointer data, gpointer user_data)
283 net_nfc_event_filter_e filter = NET_NFC_ALL_ENABLE;
285 net_nfc_client_tag_set_filter(filter);
288 void net_nfc_test_tag_get_filter(gpointer data, gpointer user_data)
290 net_nfc_event_filter_e filter = NET_NFC_ALL_DISABLE;
292 filter = net_nfc_client_tag_get_filter();
294 g_print(" NFC tag filter = %d", filter);