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_manager.h"
26 #include "net_nfc_client_tag_internal.h"
27 #include "net_nfc_client_transceive.h"
29 static NetNfcGDbusTransceive *transceive_proxy = NULL;
31 static GVariant *transceive_data_to_transceive_variant(
32 net_nfc_target_type_e dev_type,
35 static void transceive_call(GObject *source_object,
39 static void transceive_data_call(GObject *source_object,
43 static GVariant *transceive_data_to_transceive_variant(
44 net_nfc_target_type_e devType,
47 data_s transceive_info = { NULL, };
52 NFC_ERR("data is empty");
58 case NET_NFC_MIFARE_MINI_PICC :
59 case NET_NFC_MIFARE_1K_PICC :
60 case NET_NFC_MIFARE_4K_PICC :
61 case NET_NFC_MIFARE_ULTRA_PICC :
62 if (net_nfc_util_alloc_data(&transceive_info,
63 data->length + 2) == true)
65 memcpy(transceive_info.buffer,
69 net_nfc_util_compute_CRC(CRC_A,
70 transceive_info.buffer,
71 transceive_info.length);
75 case NET_NFC_JEWEL_PICC :
78 NFC_ERR("data length is larger than 9");
82 if (net_nfc_util_alloc_data(&transceive_info, 9) == true)
84 memcpy(transceive_info.buffer,
88 net_nfc_util_compute_CRC(CRC_B,
89 transceive_info.buffer,
90 transceive_info.length);
95 if(net_nfc_util_alloc_data(&transceive_info,
96 data->length) == true)
98 memcpy(transceive_info.buffer,
105 variant = net_nfc_util_gdbus_data_to_variant(&transceive_info);
107 net_nfc_util_free_data(&transceive_info);
112 static void transceive_data_call(GObject *source_object,
113 GAsyncResult *res, gpointer user_data)
115 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
116 net_nfc_error_e out_result;
117 GVariant *out_data = NULL;
118 GError *error = NULL;
120 g_assert(user_data != NULL);
122 if (net_nfc_gdbus_transceive_call_transceive_data_finish(
123 NET_NFC_GDBUS_TRANSCEIVE(source_object),
129 NFC_ERR("Can not finish transceive: %s", error->message);
132 out_result = NET_NFC_IPC_FAIL;
135 if (func_data->callback != NULL)
137 data_s resp = { NULL, };
139 net_nfc_util_gdbus_variant_to_data_s(out_data, &resp);
141 ((nfc_transceive_data_callback)func_data->callback)(
144 func_data->user_data);
146 net_nfc_util_free_data(&resp);
152 static void transceive_call(GObject *source_object,
153 GAsyncResult *res, gpointer user_data)
155 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
156 net_nfc_error_e out_result;
157 GError *error = NULL;
159 g_assert(user_data != NULL);
161 if (net_nfc_gdbus_transceive_call_transceive_finish(
162 NET_NFC_GDBUS_TRANSCEIVE(source_object),
167 NFC_ERR("Can not finish transceive: %s", error->message);
170 out_result = NET_NFC_IPC_FAIL;
173 if (func_data->callback != NULL)
175 ((nfc_transceive_callback)func_data->callback)(
177 func_data->user_data);
183 API net_nfc_error_e net_nfc_client_transceive(net_nfc_target_handle_s *handle,
184 data_s *rawdata, nfc_transceive_callback callback, void *user_data)
186 net_nfc_target_info_s *target_info;
187 NetNfcCallback *funcdata;
190 if (transceive_proxy == NULL)
192 NFC_ERR("Can not get TransceiveProxy");
194 return NET_NFC_NOT_INITIALIZED;
198 if (handle == NULL || rawdata == NULL)
199 return NET_NFC_NULL_PARAMETER;
201 /* prevent executing daemon when nfc is off */
202 if (net_nfc_client_manager_is_activated() == false) {
203 return NET_NFC_INVALID_STATE;
206 target_info = net_nfc_client_tag_get_client_target_info();
207 if (target_info == NULL || target_info->handle == NULL)
208 return NET_NFC_NOT_CONNECTED;
210 NFC_DBG("send request :: transceive = [%p]", handle);
212 arg_data = transceive_data_to_transceive_variant(target_info->devType,
214 if (arg_data == NULL) {
215 return NET_NFC_INVALID_PARAM;
218 funcdata = g_try_new0(NetNfcCallback, 1);
219 if (funcdata == NULL) {
220 g_variant_unref(arg_data);
222 return NET_NFC_ALLOC_FAIL;
225 funcdata->callback = (gpointer)callback;
226 funcdata->user_data = user_data;
228 net_nfc_gdbus_transceive_call_transceive(transceive_proxy,
229 GPOINTER_TO_UINT(handle),
230 target_info->devType,
232 net_nfc_client_gdbus_get_privilege(),
240 API net_nfc_error_e net_nfc_client_transceive_data(net_nfc_target_handle_s *handle,
241 data_s *rawdata, nfc_transceive_data_callback callback, void *user_data)
243 net_nfc_target_info_s *target_info;
244 NetNfcCallback *funcdata;
247 if (transceive_proxy == NULL)
249 NFC_ERR("Can not get TransceiveProxy");
251 return NET_NFC_NOT_INITIALIZED;
254 if (handle == NULL || rawdata == NULL)
255 return NET_NFC_NULL_PARAMETER;
257 /* prevent executing daemon when nfc is off */
258 if (net_nfc_client_manager_is_activated() == false) {
259 return NET_NFC_INVALID_STATE;
262 target_info = net_nfc_client_tag_get_client_target_info();
263 if (target_info == NULL || target_info->handle == NULL)
264 return NET_NFC_NOT_CONNECTED;
266 NFC_DBG("send request :: transceive = [%p]", handle);
268 arg_data = transceive_data_to_transceive_variant(target_info->devType,
270 if (arg_data == NULL) {
271 return NET_NFC_INVALID_PARAM;
274 funcdata = g_try_new0(NetNfcCallback, 1);
275 if (funcdata == NULL) {
276 g_variant_unref(arg_data);
278 return NET_NFC_ALLOC_FAIL;
281 funcdata->callback = (gpointer)callback;
282 funcdata->user_data = user_data;
284 net_nfc_gdbus_transceive_call_transceive_data(transceive_proxy,
285 GPOINTER_TO_UINT(handle),
286 target_info->devType,
288 net_nfc_client_gdbus_get_privilege(),
290 transceive_data_call,
296 API net_nfc_error_e net_nfc_client_transceive_sync(net_nfc_target_handle_s *handle,
299 net_nfc_target_info_s *target_info;
300 net_nfc_error_e out_result = NET_NFC_OK;
301 GError *error = NULL;
304 if (handle == NULL || rawdata == NULL)
305 return NET_NFC_NULL_PARAMETER;
307 if (transceive_proxy == NULL)
309 NFC_ERR("Can not get TransceiveProxy");
311 return NET_NFC_NOT_INITIALIZED;
314 /* prevent executing daemon when nfc is off */
315 if (net_nfc_client_manager_is_activated() == false) {
316 return NET_NFC_INVALID_STATE;
319 target_info = net_nfc_client_tag_get_client_target_info();
320 if (target_info == NULL || target_info->handle == NULL)
321 return NET_NFC_NOT_CONNECTED;
323 NFC_DBG("send request :: transceive = [%p]", handle);
325 arg_data = transceive_data_to_transceive_variant(target_info->devType,
327 if (arg_data == NULL)
328 return NET_NFC_ALLOC_FAIL;
330 if (net_nfc_gdbus_transceive_call_transceive_sync(transceive_proxy,
331 GPOINTER_TO_UINT(handle),
332 target_info->devType,
334 net_nfc_client_gdbus_get_privilege(),
339 NFC_ERR("Transceive (sync call) failed: %s",
343 out_result = NET_NFC_IPC_FAIL;
349 API net_nfc_error_e net_nfc_client_transceive_data_sync(
350 net_nfc_target_handle_s *handle, data_s *rawdata, data_s **response)
352 net_nfc_target_info_s *target_info;
353 net_nfc_error_e out_result = NET_NFC_OK;
354 GVariant *out_data = NULL;
355 GError *error = NULL;
358 if (handle == NULL || rawdata == NULL)
359 return NET_NFC_NULL_PARAMETER;
361 if (transceive_proxy == NULL)
363 NFC_ERR("Can not get TransceiveProxy");
365 return NET_NFC_NOT_INITIALIZED;
368 /* prevent executing daemon when nfc is off */
369 if (net_nfc_client_manager_is_activated() == false) {
370 return NET_NFC_INVALID_STATE;
373 target_info = net_nfc_client_tag_get_client_target_info();
374 if (target_info == NULL || target_info->handle == NULL)
375 return NET_NFC_NOT_CONNECTED;
377 NFC_DBG("send request :: transceive = [%p]", handle);
379 arg_data = transceive_data_to_transceive_variant(target_info->devType,
381 if (arg_data == NULL)
382 return NET_NFC_ALLOC_FAIL;
384 if (net_nfc_gdbus_transceive_call_transceive_data_sync(
386 GPOINTER_TO_UINT(handle),
387 target_info->devType,
389 net_nfc_client_gdbus_get_privilege(),
395 NFC_ERR("Transceive (sync call) failed: %s",
399 out_result = NET_NFC_IPC_FAIL;
402 if (response && out_data != NULL)
404 *response = net_nfc_util_gdbus_variant_to_data(out_data);
411 net_nfc_error_e net_nfc_client_transceive_init(void)
413 GError *error = NULL;
415 if (transceive_proxy)
417 NFC_WARN("Already initialized");
421 transceive_proxy = net_nfc_gdbus_transceive_proxy_new_for_bus_sync(
423 G_DBUS_PROXY_FLAGS_NONE,
424 "org.tizen.NetNfcService",
425 "/org/tizen/NetNfcService/Transceive",
428 if (transceive_proxy == NULL)
430 NFC_ERR("Can not create proxy : %s", error->message);
433 return NET_NFC_UNKNOWN_ERROR;
439 void net_nfc_client_transceive_deinit(void)
441 if (transceive_proxy)
443 g_object_unref(transceive_proxy);
444 transceive_proxy = NULL;