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.
19 #include "net_nfc_typedef_internal.h"
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_util_gdbus_internal.h"
23 #include "net_nfc_gdbus.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_internal.h"
28 #include "net_nfc_client_transceive.h"
30 #ifndef NET_NFC_EXPORT_API
31 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
34 static NetNfcGDbusTransceive *transceive_proxy = NULL;
36 static GVariant *transceive_data_to_transceive_variant(
37 net_nfc_target_type_e dev_type,
40 static void transceive_call(GObject *source_object,
44 static void transceive_data_call(GObject *source_object,
48 static GVariant *transceive_data_to_transceive_variant(
49 net_nfc_target_type_e devType,
52 data_s transceive_info = { NULL, };
57 DEBUG_ERR_MSG("data is empty");
64 case NET_NFC_MIFARE_MINI_PICC :
65 case NET_NFC_MIFARE_1K_PICC :
66 case NET_NFC_MIFARE_4K_PICC :
67 case NET_NFC_MIFARE_ULTRA_PICC :
68 if (net_nfc_util_init_data(&transceive_info,
69 data->length + 2) == true)
71 memcpy(transceive_info.buffer,
75 net_nfc_util_compute_CRC(CRC_A,
76 transceive_info.buffer,
77 transceive_info.length);
81 case NET_NFC_JEWEL_PICC :
84 DEBUG_ERR_MSG("data length is larger than 9");
89 if (net_nfc_util_init_data(&transceive_info, 9) == true)
91 memcpy(transceive_info.buffer,
95 net_nfc_util_compute_CRC(CRC_B,
96 transceive_info.buffer,
97 transceive_info.length);
102 if(net_nfc_util_init_data(&transceive_info,
103 data->length) == true)
105 memcpy(transceive_info.buffer,
112 variant = net_nfc_util_gdbus_data_to_variant(&transceive_info);
114 net_nfc_util_clear_data(&transceive_info);
119 static void transceive_data_call(GObject *source_object,
123 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
124 net_nfc_error_e out_result;
125 GVariant *out_data = NULL;
126 GError *error = NULL;
128 g_assert(user_data != NULL);
130 if (net_nfc_gdbus_transceive_call_transceive_data_finish(
131 NET_NFC_GDBUS_TRANSCEIVE(source_object),
137 DEBUG_ERR_MSG("Can not finish transceive: %s", error->message);
138 out_result = NET_NFC_IPC_FAIL;
143 if (func_data->callback != NULL)
145 data_s resp = { NULL, };
147 net_nfc_util_gdbus_variant_to_data_s(out_data, &resp);
149 ((nfc_transceive_data_callback)func_data->callback)(
152 func_data->user_data);
154 net_nfc_util_clear_data(&resp);
160 static void transceive_call(GObject *source_object,
164 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
165 net_nfc_error_e out_result;
166 GError *error = NULL;
168 g_assert(user_data != NULL);
170 if (net_nfc_gdbus_transceive_call_transceive_finish(
171 NET_NFC_GDBUS_TRANSCEIVE(source_object),
176 DEBUG_ERR_MSG("Can not finish transceive: %s", error->message);
177 out_result = NET_NFC_IPC_FAIL;
182 if (func_data->callback != NULL)
184 ((nfc_transceive_callback)func_data->callback)(
186 func_data->user_data);
193 net_nfc_error_e net_nfc_client_transceive(net_nfc_target_handle_h handle,
195 nfc_transceive_callback callback,
198 net_nfc_target_info_s *target_info;
199 NetNfcCallback *funcdata;
202 if (transceive_proxy == NULL)
204 if(net_nfc_client_transceive_init() != NET_NFC_OK)
206 DEBUG_ERR_MSG("transceive_proxy fail");
207 return NET_NFC_NOT_INITIALIZED;
211 if (handle == NULL || rawdata == NULL)
212 return NET_NFC_NULL_PARAMETER;
214 /* prevent executing daemon when nfc is off */
215 if (net_nfc_client_manager_is_activated() == false) {
216 return NET_NFC_NOT_ACTIVATED;
219 target_info = net_nfc_client_tag_get_client_target_info();
220 if (target_info == NULL || target_info->handle == NULL)
221 return NET_NFC_NOT_CONNECTED;
223 DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
225 arg_data = transceive_data_to_transceive_variant(target_info->devType,
227 if (arg_data == NULL) {
228 return NET_NFC_INVALID_PARAM;
231 funcdata = g_try_new0(NetNfcCallback, 1);
232 if (funcdata == NULL) {
233 g_variant_unref(arg_data);
235 return NET_NFC_ALLOC_FAIL;
238 funcdata->callback = (gpointer)callback;
239 funcdata->user_data = user_data;
241 net_nfc_gdbus_transceive_call_transceive(transceive_proxy,
242 GPOINTER_TO_UINT(handle),
243 target_info->devType,
253 net_nfc_error_e net_nfc_client_transceive_data(net_nfc_target_handle_h handle,
255 nfc_transceive_data_callback callback,
258 net_nfc_target_info_s *target_info;
259 NetNfcCallback *funcdata;
262 if (transceive_proxy == NULL)
264 if(net_nfc_client_transceive_init() != NET_NFC_OK)
266 DEBUG_ERR_MSG("transceive_proxy fail");
267 return NET_NFC_NOT_INITIALIZED;
271 if (handle == NULL || rawdata == NULL)
272 return NET_NFC_NULL_PARAMETER;
274 /* prevent executing daemon when nfc is off */
275 if (net_nfc_client_manager_is_activated() == false) {
276 return NET_NFC_NOT_ACTIVATED;
279 target_info = net_nfc_client_tag_get_client_target_info();
280 if (target_info == NULL || target_info->handle == NULL)
281 return NET_NFC_NOT_CONNECTED;
283 DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
285 arg_data = transceive_data_to_transceive_variant(target_info->devType,
287 if (arg_data == NULL) {
288 return NET_NFC_INVALID_PARAM;
291 funcdata = g_try_new0(NetNfcCallback, 1);
292 if (funcdata == NULL) {
293 g_variant_unref(arg_data);
295 return NET_NFC_ALLOC_FAIL;
298 funcdata->callback = (gpointer)callback;
299 funcdata->user_data = user_data;
301 net_nfc_gdbus_transceive_call_transceive_data(transceive_proxy,
302 GPOINTER_TO_UINT(handle),
303 target_info->devType,
306 transceive_data_call,
313 net_nfc_error_e net_nfc_client_transceive_sync(net_nfc_target_handle_h handle,
316 net_nfc_target_info_s *target_info;
317 net_nfc_error_e out_result = NET_NFC_OK;
318 GError *error = NULL;
321 if (handle == NULL || rawdata == NULL)
322 return NET_NFC_NULL_PARAMETER;
324 if (transceive_proxy == NULL)
326 if(net_nfc_client_transceive_init() != NET_NFC_OK)
328 DEBUG_ERR_MSG("transceive_proxy fail");
329 return NET_NFC_NOT_INITIALIZED;
333 /* prevent executing daemon when nfc is off */
334 if (net_nfc_client_manager_is_activated() == false) {
335 return NET_NFC_NOT_ACTIVATED;
338 target_info = net_nfc_client_tag_get_client_target_info();
339 if (target_info == NULL || target_info->handle == NULL)
340 return NET_NFC_NOT_CONNECTED;
342 DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
344 arg_data = transceive_data_to_transceive_variant(target_info->devType,
346 if (arg_data == NULL)
347 return NET_NFC_ALLOC_FAIL;
349 if (net_nfc_gdbus_transceive_call_transceive_sync(transceive_proxy,
350 GPOINTER_TO_UINT(handle),
351 target_info->devType,
357 DEBUG_ERR_MSG("Transceive (sync call) failed: %s",
359 out_result = NET_NFC_IPC_FAIL;
368 net_nfc_error_e net_nfc_client_transceive_data_sync(
369 net_nfc_target_handle_h handle,
373 net_nfc_target_info_s *target_info;
374 net_nfc_error_e out_result = NET_NFC_OK;
375 GVariant *out_data = NULL;
376 GError *error = NULL;
379 if (handle == NULL || rawdata == NULL)
380 return NET_NFC_NULL_PARAMETER;
382 if (transceive_proxy == NULL)
384 if(net_nfc_client_transceive_init() != NET_NFC_OK)
386 DEBUG_ERR_MSG("transceive_proxy fail");
387 return NET_NFC_NOT_INITIALIZED;
391 /* prevent executing daemon when nfc is off */
392 if (net_nfc_client_manager_is_activated() == false) {
393 return NET_NFC_NOT_ACTIVATED;
396 target_info = net_nfc_client_tag_get_client_target_info();
397 if (target_info == NULL || target_info->handle == NULL)
398 return NET_NFC_NOT_CONNECTED;
400 DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
402 arg_data = transceive_data_to_transceive_variant(target_info->devType,
404 if (arg_data == NULL)
405 return NET_NFC_ALLOC_FAIL;
407 if (net_nfc_gdbus_transceive_call_transceive_data_sync(
409 GPOINTER_TO_UINT(handle),
410 target_info->devType,
417 DEBUG_ERR_MSG("Transceive (sync call) failed: %s",
419 out_result = NET_NFC_IPC_FAIL;
424 if (response && out_data != NULL)
426 *response = net_nfc_util_gdbus_variant_to_data(out_data);
433 net_nfc_error_e net_nfc_client_transceive_init(void)
435 GError *error = NULL;
437 if (transceive_proxy)
439 DEBUG_CLIENT_MSG("Already initialized");
444 transceive_proxy = net_nfc_gdbus_transceive_proxy_new_for_bus_sync(
446 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
447 "org.tizen.NetNfcService",
448 "/org/tizen/NetNfcService/Transceive",
451 if (transceive_proxy == NULL)
453 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
456 return NET_NFC_UNKNOWN_ERROR;
462 void net_nfc_client_transceive_deinit(void)
464 if (transceive_proxy)
466 g_object_unref(transceive_proxy);
467 transceive_proxy = NULL;