[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / net_nfc_client_transceive.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <string.h>
18
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"
29
30 #ifndef NET_NFC_EXPORT_API
31 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
32 #endif
33
34 static NetNfcGDbusTransceive *transceive_proxy = NULL;
35
36 static GVariant *transceive_data_to_transceive_variant(
37                                                 net_nfc_target_type_e dev_type,
38                                                 data_s *data);
39
40 static void transceive_call(GObject *source_object,
41                                 GAsyncResult *res,
42                                 gpointer user_data);
43
44 static void transceive_data_call(GObject *source_object,
45                                 GAsyncResult *res,
46                                 gpointer user_data);
47
48 static GVariant *transceive_data_to_transceive_variant(
49                                                 net_nfc_target_type_e devType,
50                                                 data_s *data)
51 {
52         data_s transceive_info = { NULL, };
53         GVariant *variant;
54
55         if (data == NULL)
56         {
57                 DEBUG_ERR_MSG("data is empty");
58
59                 return NULL;
60         }
61
62         switch (devType)
63         {
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                 {
71                         memcpy(transceive_info.buffer,
72                                 data->buffer,
73                                 data->length);
74
75                         net_nfc_util_compute_CRC(CRC_A,
76                                 transceive_info.buffer,
77                                 transceive_info.length);
78                 }
79                 break;
80
81         case NET_NFC_JEWEL_PICC :
82                 if (data->length > 9)
83                 {
84                         DEBUG_ERR_MSG("data length is larger than 9");
85
86                         return NULL;
87                 }
88
89                 if (net_nfc_util_init_data(&transceive_info, 9) == true)
90                 {
91                         memcpy(transceive_info.buffer,
92                                 data->buffer,
93                                 data->length);
94
95                         net_nfc_util_compute_CRC(CRC_B,
96                                         transceive_info.buffer,
97                                         transceive_info.length);
98                 }
99                 break;
100
101         default :
102                 if(net_nfc_util_init_data(&transceive_info,
103                         data->length) == true)
104                 {
105                         memcpy(transceive_info.buffer,
106                                 data->buffer,
107                                 data->length);
108                 }
109                 break;
110         }
111
112         variant = net_nfc_util_gdbus_data_to_variant(&transceive_info);
113
114         net_nfc_util_clear_data(&transceive_info);
115
116         return variant;
117 }
118
119 static void transceive_data_call(GObject *source_object,
120                                 GAsyncResult *res,
121                                 gpointer user_data)
122 {
123         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
124         net_nfc_error_e out_result;
125         GVariant *out_data = NULL;
126         GError *error = NULL;
127
128         g_assert(user_data != NULL);
129
130         if (net_nfc_gdbus_transceive_call_transceive_data_finish(
131                                 NET_NFC_GDBUS_TRANSCEIVE(source_object),
132                                 (gint *)&out_result,
133                                 &out_data,
134                                 res,
135                                 &error) == FALSE)
136         {
137                 DEBUG_ERR_MSG("Can not finish transceive: %s", error->message);
138                 out_result = NET_NFC_IPC_FAIL;
139
140                 g_error_free(error);
141         }
142
143         if (func_data->callback != NULL)
144         {
145                 data_s resp = { NULL, };
146
147                 net_nfc_util_gdbus_variant_to_data_s(out_data, &resp);
148
149                 ((nfc_transceive_data_callback)func_data->callback)(
150                                         out_result,
151                                         &resp,
152                                         func_data->user_data);
153
154                 net_nfc_util_clear_data(&resp);
155         }
156
157         g_free(func_data);
158 }
159
160 static void transceive_call(GObject *source_object,
161                                 GAsyncResult *res,
162                                 gpointer user_data)
163 {
164         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
165         net_nfc_error_e out_result;
166         GError *error = NULL;
167
168         g_assert(user_data != NULL);
169
170         if (net_nfc_gdbus_transceive_call_transceive_finish(
171                                 NET_NFC_GDBUS_TRANSCEIVE(source_object),
172                                 (gint *)&out_result,
173                                 res,
174                                 &error) == FALSE)
175         {
176                 DEBUG_ERR_MSG("Can not finish transceive: %s", error->message);
177                 out_result = NET_NFC_IPC_FAIL;
178
179                 g_error_free(error);
180         }
181
182         if (func_data->callback != NULL)
183         {
184                 ((nfc_transceive_callback)func_data->callback)(
185                         out_result,
186                         func_data->user_data);
187         }
188
189         g_free(func_data);
190 }
191
192 NET_NFC_EXPORT_API
193 net_nfc_error_e net_nfc_client_transceive(net_nfc_target_handle_h handle,
194                                         data_h rawdata,
195                                         nfc_transceive_callback callback,
196                                         void *user_data)
197 {
198         net_nfc_target_info_s *target_info;
199         NetNfcCallback *funcdata;
200         GVariant *arg_data;
201
202         if (transceive_proxy == NULL)
203         {
204                 if(net_nfc_client_transceive_init() != NET_NFC_OK)
205                 {
206                         DEBUG_ERR_MSG("transceive_proxy fail");
207                         return NET_NFC_NOT_INITIALIZED;
208                 }
209         }
210
211         if (handle == NULL || rawdata == NULL)
212                 return NET_NFC_NULL_PARAMETER;
213
214         /* prevent executing daemon when nfc is off */
215         if (net_nfc_client_manager_is_activated() == false) {
216                 return NET_NFC_NOT_ACTIVATED;
217         }
218
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;
222
223         DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
224
225         arg_data = transceive_data_to_transceive_variant(target_info->devType,
226                 rawdata);
227         if (arg_data == NULL) {
228                 return NET_NFC_INVALID_PARAM;
229         }
230
231         funcdata = g_try_new0(NetNfcCallback, 1);
232         if (funcdata == NULL) {
233                 g_variant_unref(arg_data);
234
235                 return NET_NFC_ALLOC_FAIL;
236         }
237
238         funcdata->callback = (gpointer)callback;
239         funcdata->user_data = user_data;
240
241         net_nfc_gdbus_transceive_call_transceive(transceive_proxy,
242                                         GPOINTER_TO_UINT(handle),
243                                         target_info->devType,
244                                         arg_data,
245                                         NULL,
246                                         transceive_call,
247                                         funcdata);
248
249         return NET_NFC_OK;
250 }
251
252 NET_NFC_EXPORT_API
253 net_nfc_error_e net_nfc_client_transceive_data(net_nfc_target_handle_h handle,
254                                         data_h rawdata,
255                                         nfc_transceive_data_callback callback,
256                                         void *user_data)
257 {
258         net_nfc_target_info_s *target_info;
259         NetNfcCallback *funcdata;
260         GVariant *arg_data;
261
262         if (transceive_proxy == NULL)
263         {
264                 if(net_nfc_client_transceive_init() != NET_NFC_OK)
265                 {
266                         DEBUG_ERR_MSG("transceive_proxy fail");
267                         return NET_NFC_NOT_INITIALIZED;
268                 }
269         }
270
271         if (handle == NULL || rawdata == NULL)
272                 return NET_NFC_NULL_PARAMETER;
273
274         /* prevent executing daemon when nfc is off */
275         if (net_nfc_client_manager_is_activated() == false) {
276                 return NET_NFC_NOT_ACTIVATED;
277         }
278
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;
282
283         DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
284
285         arg_data = transceive_data_to_transceive_variant(target_info->devType,
286                 rawdata);
287         if (arg_data == NULL) {
288                 return NET_NFC_INVALID_PARAM;
289         }
290
291         funcdata = g_try_new0(NetNfcCallback, 1);
292         if (funcdata == NULL) {
293                 g_variant_unref(arg_data);
294
295                 return NET_NFC_ALLOC_FAIL;
296         }
297
298         funcdata->callback = (gpointer)callback;
299         funcdata->user_data = user_data;
300
301         net_nfc_gdbus_transceive_call_transceive_data(transceive_proxy,
302                                         GPOINTER_TO_UINT(handle),
303                                         target_info->devType,
304                                         arg_data,
305                                         NULL,
306                                         transceive_data_call,
307                                         funcdata);
308
309         return NET_NFC_OK;
310 }
311
312 NET_NFC_EXPORT_API
313 net_nfc_error_e net_nfc_client_transceive_sync(net_nfc_target_handle_h handle,
314                                         data_h rawdata)
315 {
316         net_nfc_target_info_s *target_info;
317         net_nfc_error_e out_result = NET_NFC_OK;
318         GError *error = NULL;
319         GVariant *arg_data;
320
321         if (handle == NULL || rawdata == NULL)
322                 return NET_NFC_NULL_PARAMETER;
323
324         if (transceive_proxy == NULL)
325         {
326                 if(net_nfc_client_transceive_init() != NET_NFC_OK)
327                 {
328                         DEBUG_ERR_MSG("transceive_proxy fail");
329                         return NET_NFC_NOT_INITIALIZED;
330                 }
331         }
332
333         /* prevent executing daemon when nfc is off */
334         if (net_nfc_client_manager_is_activated() == false) {
335                 return NET_NFC_NOT_ACTIVATED;
336         }
337
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;
341
342         DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
343
344         arg_data = transceive_data_to_transceive_variant(target_info->devType,
345                 rawdata);
346         if (arg_data == NULL)
347                 return NET_NFC_ALLOC_FAIL;
348
349         if (net_nfc_gdbus_transceive_call_transceive_sync(transceive_proxy,
350                                         GPOINTER_TO_UINT(handle),
351                                         target_info->devType,
352                                         arg_data,
353                                         (gint *)&out_result,
354                                         NULL,
355                                         &error) == FALSE)
356         {
357                 DEBUG_ERR_MSG("Transceive (sync call) failed: %s",
358                         error->message);
359                 out_result = NET_NFC_IPC_FAIL;
360
361                 g_error_free(error);
362         }
363
364         return out_result;
365 }
366
367 NET_NFC_EXPORT_API
368 net_nfc_error_e net_nfc_client_transceive_data_sync(
369                                                 net_nfc_target_handle_h handle,
370                                                 data_h rawdata,
371                                                 data_h *response)
372 {
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;
377         GVariant *arg_data;
378
379         if (handle == NULL || rawdata == NULL)
380                 return NET_NFC_NULL_PARAMETER;
381
382         if (transceive_proxy == NULL)
383         {
384                 if(net_nfc_client_transceive_init() != NET_NFC_OK)
385                 {
386                         DEBUG_ERR_MSG("transceive_proxy fail");
387                         return NET_NFC_NOT_INITIALIZED;
388                 }
389         }
390
391         /* prevent executing daemon when nfc is off */
392         if (net_nfc_client_manager_is_activated() == false) {
393                 return NET_NFC_NOT_ACTIVATED;
394         }
395
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;
399
400         DEBUG_CLIENT_MSG("send request :: transceive = [%p]", handle);
401
402         arg_data = transceive_data_to_transceive_variant(target_info->devType,
403                 rawdata);
404         if (arg_data == NULL)
405                 return NET_NFC_ALLOC_FAIL;
406
407         if (net_nfc_gdbus_transceive_call_transceive_data_sync(
408                                         transceive_proxy,
409                                         GPOINTER_TO_UINT(handle),
410                                         target_info->devType,
411                                         arg_data,
412                                         (gint *)&out_result,
413                                         &out_data,
414                                         NULL,
415                                         &error) == FALSE)
416         {
417                 DEBUG_ERR_MSG("Transceive (sync call) failed: %s",
418                         error->message);
419                 out_result = NET_NFC_IPC_FAIL;
420
421                 g_error_free(error);
422         }
423
424         if (response && out_data != NULL)
425         {
426                 *response = net_nfc_util_gdbus_variant_to_data(out_data);
427         }
428
429         return out_result;
430 }
431
432
433 net_nfc_error_e net_nfc_client_transceive_init(void)
434 {
435         GError *error = NULL;
436
437         if (transceive_proxy)
438         {
439                 DEBUG_CLIENT_MSG("Already initialized");
440
441                 return NET_NFC_OK;
442         }
443
444         transceive_proxy = net_nfc_gdbus_transceive_proxy_new_for_bus_sync(
445                                         G_BUS_TYPE_SYSTEM,
446                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
447                                         "org.tizen.NetNfcService",
448                                         "/org/tizen/NetNfcService/Transceive",
449                                         NULL,
450                                         &error);
451         if (transceive_proxy == NULL)
452         {
453                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
454                 g_error_free(error);
455
456                 return NET_NFC_UNKNOWN_ERROR;
457         }
458
459         return NET_NFC_OK;
460 }
461
462 void net_nfc_client_transceive_deinit(void)
463 {
464         if (transceive_proxy)
465         {
466                 g_object_unref(transceive_proxy);
467                 transceive_proxy = NULL;
468         }
469 }