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")))
36 static NetNfcGDbusTransceive *transceive_proxy = NULL;
38 static GVariant *transceive_data_to_transceive_variant(
39 net_nfc_target_type_e dev_type,
42 static void transceive_call(GObject *source_object,
46 static void transceive_data_call(GObject *source_object,
50 static GVariant *transceive_data_to_transceive_variant(
51 net_nfc_target_type_e devType,
54 data_s transceive_info = { NULL, };
58 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) {
70 memcpy(transceive_info.buffer,
74 net_nfc_util_compute_CRC(CRC_A,
75 transceive_info.buffer,
76 transceive_info.length);
80 case NET_NFC_JEWEL_PICC:
81 if (data->length > 9) {
82 DEBUG_ERR_MSG("data length is larger than 9");
87 if (net_nfc_util_init_data(&transceive_info, 9) == true) {
88 memcpy(transceive_info.buffer,
92 net_nfc_util_compute_CRC(CRC_B,
93 transceive_info.buffer,
94 transceive_info.length);
99 if (net_nfc_util_init_data(&transceive_info,
100 data->length) == true) {
101 memcpy(transceive_info.buffer,
108 variant = net_nfc_util_gdbus_data_to_variant(&transceive_info);
110 net_nfc_util_clear_data(&transceive_info);
115 static void transceive_data_call(GObject *source_object,
119 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
120 net_nfc_error_e out_result;
121 GVariant *out_data = NULL;
122 GError *error = NULL;
124 g_assert(user_data != NULL);
126 if (net_nfc_gdbus_transceive_call_transceive_data_finish(
127 NET_NFC_GDBUS_TRANSCEIVE(source_object),
132 DEBUG_ERR_MSG("Can not finish transceive: %s", error->message);
133 out_result = NET_NFC_IPC_FAIL;
138 if (func_data->callback != NULL) {
139 data_s resp = { NULL, };
141 net_nfc_util_gdbus_variant_to_data_s(out_data, &resp);
143 ((nfc_transceive_data_callback)func_data->callback)(
146 func_data->user_data);
148 net_nfc_util_clear_data(&resp);
154 static void transceive_call(GObject *source_object,
158 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
159 net_nfc_error_e out_result;
160 GError *error = NULL;
162 g_assert(user_data != NULL);
164 if (net_nfc_gdbus_transceive_call_transceive_finish(
165 NET_NFC_GDBUS_TRANSCEIVE(source_object),
169 DEBUG_ERR_MSG("Can not finish transceive: %s", error->message);
170 out_result = NET_NFC_IPC_FAIL;
175 if (func_data->callback != NULL) {
176 ((nfc_transceive_callback)func_data->callback)(
178 func_data->user_data);
185 net_nfc_error_e net_nfc_client_transceive(net_nfc_target_handle_h handle,
187 nfc_transceive_callback callback,
190 net_nfc_target_info_s *target_info;
191 NetNfcCallback *funcdata;
194 if (transceive_proxy == NULL) {
195 if (net_nfc_client_transceive_init() != NET_NFC_OK) {
196 DEBUG_ERR_MSG("transceive_proxy fail");
197 return NET_NFC_NOT_INITIALIZED;
201 if (handle == NULL || rawdata == NULL)
202 return NET_NFC_NULL_PARAMETER;
204 /* prevent executing daemon when nfc is off */
205 if (net_nfc_client_manager_is_activated() == false)
206 return NET_NFC_NOT_ACTIVATED;
208 target_info = net_nfc_client_tag_get_client_target_info();
209 if (target_info == NULL || target_info->handle == NULL)
210 return NET_NFC_NOT_CONNECTED;
212 DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
214 arg_data = transceive_data_to_transceive_variant(target_info->devType,
216 if (arg_data == NULL)
217 return NET_NFC_INVALID_PARAM;
219 funcdata = g_try_new0(NetNfcCallback, 1);
220 if (funcdata == NULL) {
221 g_variant_unref(arg_data);
223 return NET_NFC_ALLOC_FAIL;
226 funcdata->callback = (gpointer)callback;
227 funcdata->user_data = user_data;
229 net_nfc_gdbus_transceive_call_transceive(transceive_proxy,
230 GPOINTER_TO_UINT(handle),
231 target_info->devType,
241 net_nfc_error_e net_nfc_client_transceive_data(net_nfc_target_handle_h handle,
243 nfc_transceive_data_callback callback,
246 net_nfc_target_info_s *target_info;
247 NetNfcCallback *funcdata;
250 if (transceive_proxy == NULL) {
251 if (net_nfc_client_transceive_init() != NET_NFC_OK) {
252 DEBUG_ERR_MSG("transceive_proxy fail");
253 return NET_NFC_NOT_INITIALIZED;
257 if (handle == NULL || rawdata == NULL)
258 return NET_NFC_NULL_PARAMETER;
260 /* prevent executing daemon when nfc is off */
261 if (net_nfc_client_manager_is_activated() == false)
262 return NET_NFC_NOT_ACTIVATED;
264 target_info = net_nfc_client_tag_get_client_target_info();
265 if (target_info == NULL || target_info->handle == NULL)
266 return NET_NFC_NOT_CONNECTED;
268 DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
270 arg_data = transceive_data_to_transceive_variant(target_info->devType,
272 if (arg_data == NULL)
273 return NET_NFC_INVALID_PARAM;
275 funcdata = g_try_new0(NetNfcCallback, 1);
276 if (funcdata == NULL) {
277 g_variant_unref(arg_data);
279 return NET_NFC_ALLOC_FAIL;
282 funcdata->callback = (gpointer)callback;
283 funcdata->user_data = user_data;
285 net_nfc_gdbus_transceive_call_transceive_data(transceive_proxy,
286 GPOINTER_TO_UINT(handle),
287 target_info->devType,
290 transceive_data_call,
297 net_nfc_error_e net_nfc_client_transceive_sync(net_nfc_target_handle_h handle,
300 net_nfc_target_info_s *target_info;
301 net_nfc_error_e out_result = NET_NFC_OK;
302 GError *error = NULL;
305 if (handle == NULL || rawdata == NULL)
306 return NET_NFC_NULL_PARAMETER;
308 if (transceive_proxy == NULL) {
309 if (net_nfc_client_transceive_init() != NET_NFC_OK) {
310 DEBUG_ERR_MSG("transceive_proxy fail");
311 return NET_NFC_NOT_INITIALIZED;
315 /* prevent executing daemon when nfc is off */
316 if (net_nfc_client_manager_is_activated() == false)
317 return NET_NFC_NOT_ACTIVATED;
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 DEBUG_CLIENT_MSG("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,
337 DEBUG_ERR_MSG("Transceive (sync call) failed: %s",
339 out_result = NET_NFC_IPC_FAIL;
348 net_nfc_error_e net_nfc_client_transceive_data_sync(
349 net_nfc_target_handle_h handle,
353 net_nfc_target_info_s *target_info;
354 net_nfc_error_e out_result = NET_NFC_OK;
355 GVariant *out_data = NULL;
356 GError *error = NULL;
359 if (handle == NULL || rawdata == NULL)
360 return NET_NFC_NULL_PARAMETER;
362 if (transceive_proxy == NULL) {
363 if (net_nfc_client_transceive_init() != NET_NFC_OK) {
364 DEBUG_ERR_MSG("transceive_proxy fail");
365 return NET_NFC_NOT_INITIALIZED;
369 /* prevent executing daemon when nfc is off */
370 if (net_nfc_client_manager_is_activated() == false)
371 return NET_NFC_NOT_ACTIVATED;
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 DEBUG_CLIENT_MSG("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,
393 DEBUG_ERR_MSG("Transceive (sync call) failed: %s",
395 out_result = NET_NFC_IPC_FAIL;
400 if (response && out_data != NULL)
401 *response = net_nfc_util_gdbus_variant_to_data(out_data);
407 net_nfc_error_e net_nfc_client_transceive_init(void)
409 GError *error = NULL;
411 if (transceive_proxy) {
412 DEBUG_CLIENT_MSG("Already initialized");
417 transceive_proxy = net_nfc_gdbus_transceive_proxy_new_for_bus_sync(
419 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
420 "org.tizen.NetNfcService",
421 "/org/tizen/NetNfcService/Transceive",
424 if (transceive_proxy == NULL) {
425 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
428 return NET_NFC_UNKNOWN_ERROR;
434 void net_nfc_client_transceive_deinit(void)
436 if (transceive_proxy) {
437 g_object_unref(transceive_proxy);
438 transceive_proxy = NULL;