[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / net_nfc_client_ndef.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 "net_nfc_typedef_internal.h"
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_util_internal.h"
20 #include "net_nfc_util_ndef_message.h"
21 #include "net_nfc_util_gdbus_internal.h"
22 #include "net_nfc_gdbus.h"
23 #include "net_nfc_data.h"
24 #include "net_nfc_ndef_message.h"
25 #include "net_nfc_client.h"
26 #include "net_nfc_client_util_internal.h"
27 #include "net_nfc_client_manager.h"
28 #include "net_nfc_client_ndef.h"
29 #include "net_nfc_client_tag_internal.h"
30
31 #ifndef NET_NFC_EXPORT_API
32 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
33 #endif
34
35 static NetNfcGDbusNdef *ndef_proxy = NULL;
36
37 static gboolean ndef_is_supported_tag(void);
38
39 static void ndef_call_read(GObject *source_object,
40                         GAsyncResult *res,
41                         gpointer user_data);
42
43 static void ndef_call_write(GObject *source_object,
44                         GAsyncResult *res,
45                         gpointer user_data);
46
47 static void ndef_call_make_read_only(GObject *source_object,
48                                 GAsyncResult *res,
49                                 gpointer user_data);
50
51 static void ndef_call_format(GObject *source_object,
52                         GAsyncResult *res,
53                         gpointer user_data);
54
55 static gboolean ndef_is_supported_tag(void)
56 {
57         net_nfc_target_info_s *target_info = NULL;
58
59         target_info = net_nfc_client_tag_get_client_target_info();
60         if (target_info == NULL)
61         {
62                 DEBUG_ERR_MSG("target_info does not exist");
63
64                 return FALSE;
65         }
66
67         switch (target_info->devType)
68         {
69                 case NET_NFC_ISO14443_A_PICC :
70                 case NET_NFC_MIFARE_MINI_PICC :
71                 case NET_NFC_MIFARE_1K_PICC :
72                 case NET_NFC_MIFARE_4K_PICC :
73                 case NET_NFC_MIFARE_ULTRA_PICC :
74                 case NET_NFC_JEWEL_PICC :
75                         return TRUE;
76                         break;
77                 default:
78                         DEBUG_CLIENT_MSG(
79                                 "not supported tag for read only tag");
80                         return FALSE;
81         }
82 }
83
84 static void ndef_call_read(GObject *source_object,
85                         GAsyncResult *res,
86                         gpointer user_data)
87 {
88         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
89         net_nfc_error_e out_result;
90         GVariant *out_data = NULL;
91         GError *error = NULL;
92
93         g_assert(user_data != NULL);
94
95         if (net_nfc_gdbus_ndef_call_read_finish(
96                                 NET_NFC_GDBUS_NDEF(source_object),
97                                 (gint *)&out_result,
98                                 &out_data,
99                                 res,
100                                 &error) == FALSE)
101         {
102                 DEBUG_ERR_MSG("Can not finish read: %s", error->message);
103                 out_result = NET_NFC_IPC_FAIL;
104
105                 g_error_free(error);
106         }
107
108         if (func_data->callback != NULL)
109         {
110                 net_nfc_client_ndef_read_completed callback =
111                         (net_nfc_client_ndef_read_completed)func_data->callback;
112                 ndef_message_h message;
113
114                 message = net_nfc_util_gdbus_variant_to_ndef_message(out_data);
115
116                 callback(out_result, message, func_data->user_data);
117
118                 net_nfc_util_free_ndef_message(message);
119         }
120
121         g_free(func_data);
122 }
123
124 static void ndef_call_write(GObject *source_object,
125                         GAsyncResult *res,
126                         gpointer user_data)
127 {
128         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
129         net_nfc_error_e out_result;
130         GError *error = NULL;
131
132         g_assert(user_data != NULL);
133
134         if (net_nfc_gdbus_ndef_call_write_finish(
135                                 NET_NFC_GDBUS_NDEF(source_object),
136                                 (gint *)&out_result,
137                                 res,
138                                 &error) == FALSE)
139         {
140                 DEBUG_ERR_MSG("Can not finish write: %s", error->message);
141                 out_result = NET_NFC_IPC_FAIL;
142
143                 g_error_free(error);
144         }
145
146         if (func_data->callback != NULL)
147         {
148                 net_nfc_client_ndef_write_completed callback =
149                         (net_nfc_client_ndef_write_completed)func_data->callback;
150
151                 callback(out_result, func_data->user_data);
152         }
153
154         g_free(func_data);
155 }
156
157 static void ndef_call_make_read_only(GObject *source_object,
158                                 GAsyncResult *res,
159                                 gpointer user_data)
160 {
161         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
162         net_nfc_error_e out_result;
163         GError *error = NULL;
164
165         g_assert(user_data != NULL);
166
167         if (net_nfc_gdbus_ndef_call_make_read_only_finish(
168                                 NET_NFC_GDBUS_NDEF(source_object),
169                                 (gint *)&out_result,
170                                 res,
171                                 &error) == FALSE)
172         {
173                 DEBUG_ERR_MSG("Can not finish make read only: %s",
174                                 error->message);
175                 out_result = NET_NFC_IPC_FAIL;
176
177                 g_error_free(error);
178         }
179
180         if (func_data->callback != NULL)
181         {
182                 net_nfc_client_ndef_make_read_only_completed callback =
183                         (net_nfc_client_ndef_make_read_only_completed)func_data->callback;
184
185                 callback(out_result, func_data->user_data);
186         }
187
188         g_free(func_data);
189 }
190
191 static void ndef_call_format(GObject *source_object,
192                         GAsyncResult *res,
193                         gpointer user_data)
194 {
195         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
196         net_nfc_error_e out_result;
197         GError *error = NULL;
198
199         g_assert(user_data != NULL);
200
201         if (net_nfc_gdbus_ndef_call_format_finish(
202                                 NET_NFC_GDBUS_NDEF(source_object),
203                                 (gint *)&out_result,
204                                 res,
205                                 &error) == FALSE)
206         {
207                 DEBUG_ERR_MSG("Can not finish format: %s", error->message);
208                 out_result = NET_NFC_IPC_FAIL;
209
210                 g_error_free(error);
211         }
212
213         if (func_data->callback != NULL)
214         {
215                 net_nfc_client_ndef_format_completed callback =
216                         (net_nfc_client_ndef_format_completed)func_data->callback;
217
218                 callback(out_result, func_data->user_data);
219         }
220
221         g_free(func_data);
222 }
223
224 NET_NFC_EXPORT_API
225 net_nfc_error_e net_nfc_client_ndef_read(net_nfc_target_handle_h handle,
226                                 net_nfc_client_ndef_read_completed callback,
227                                 void *user_data)
228 {
229         NetNfcCallback *func_data;
230
231         if (handle == NULL)
232                 return NET_NFC_NULL_PARAMETER;
233
234         if (ndef_proxy == NULL)
235         {
236                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
237                 {
238                         DEBUG_ERR_MSG("tag_proxy fail");
239                         return NET_NFC_NOT_INITIALIZED;
240                 }
241         }
242
243         /* prevent executing daemon when nfc is off */
244         if (net_nfc_client_manager_is_activated() == false) {
245                 return NET_NFC_NOT_ACTIVATED;
246         }
247
248         if (net_nfc_client_tag_is_connected() == FALSE)
249                 return NET_NFC_NOT_CONNECTED;
250
251         DEBUG_CLIENT_MSG("send request :: read ndef = [%p]", handle);
252
253         func_data = g_try_new0(NetNfcCallback, 1);
254         if (func_data == NULL) {
255                 return NET_NFC_ALLOC_FAIL;
256         }
257
258         func_data->callback = (gpointer)callback;
259         func_data->user_data = user_data;
260
261         net_nfc_gdbus_ndef_call_read(ndef_proxy,
262                         GPOINTER_TO_UINT(handle),
263                         NULL,
264                         ndef_call_read,
265                         func_data);
266
267         return NET_NFC_OK;
268 }
269
270 NET_NFC_EXPORT_API
271 net_nfc_error_e net_nfc_client_ndef_read_sync(net_nfc_target_handle_h handle,
272                                         ndef_message_h *message)
273 {
274         net_nfc_error_e out_result = NET_NFC_OK;
275         GVariant *out_data = NULL;
276         GError *error = NULL;
277
278         if (handle == NULL)
279                 return NET_NFC_NULL_PARAMETER;
280
281         if (ndef_proxy == NULL)
282         {
283                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
284                 {
285                         DEBUG_ERR_MSG("tag_proxy fail");
286                         return NET_NFC_NOT_INITIALIZED;
287                 }
288         }
289
290         /* prevent executing daemon when nfc is off */
291         if (net_nfc_client_manager_is_activated() == false) {
292                 return NET_NFC_NOT_ACTIVATED;
293         }
294
295         if (net_nfc_client_tag_is_connected() == FALSE)
296                 return NET_NFC_NOT_CONNECTED;
297
298         DEBUG_CLIENT_MSG("send request :: read ndef = [%p]", handle);
299
300         if (net_nfc_gdbus_ndef_call_read_sync(ndef_proxy,
301                                         GPOINTER_TO_UINT(handle),
302                                         (gint *)&out_result,
303                                         &out_data,
304                                         NULL,
305                                         &error) == TRUE) {
306                 *message = net_nfc_util_gdbus_variant_to_ndef_message(out_data);
307         } else {
308                 DEBUG_ERR_MSG("can not call read: %s",
309                                 error->message);
310                 out_result = NET_NFC_IPC_FAIL;
311
312                 g_error_free(error);
313         }
314
315         return out_result;
316 }
317
318 NET_NFC_EXPORT_API
319 net_nfc_error_e net_nfc_client_ndef_write(net_nfc_target_handle_h handle,
320                                 ndef_message_h message,
321                                 net_nfc_client_ndef_write_completed callback,
322                                 void *user_data)
323 {
324         NetNfcCallback *func_data;
325         GVariant *arg_data;
326
327         if (handle == NULL || message == NULL)
328                 return NET_NFC_NULL_PARAMETER;
329
330         if (ndef_proxy == NULL)
331         {
332                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
333                 {
334                         DEBUG_ERR_MSG("tag_proxy fail");
335                         return NET_NFC_NOT_INITIALIZED;
336                 }
337         }
338
339         /* prevent executing daemon when nfc is off */
340         if (net_nfc_client_manager_is_activated() == false) {
341                 return NET_NFC_NOT_ACTIVATED;
342         }
343
344         if (net_nfc_client_tag_is_connected() == FALSE)
345                 return NET_NFC_NOT_CONNECTED;
346
347         func_data = g_try_new0(NetNfcCallback, 1);
348         if (func_data == NULL) {
349                 return NET_NFC_ALLOC_FAIL;
350         }
351
352         func_data->callback = (gpointer)callback;
353         func_data->user_data = user_data;
354
355         arg_data = net_nfc_util_gdbus_ndef_message_to_variant(message);
356
357         net_nfc_gdbus_ndef_call_write(ndef_proxy,
358                                 GPOINTER_TO_UINT(handle),
359                                 arg_data,
360                                 NULL,
361                                 ndef_call_write,
362                                 func_data);
363
364         return NET_NFC_OK;
365 }
366
367 NET_NFC_EXPORT_API
368 net_nfc_error_e net_nfc_client_ndef_write_sync(net_nfc_target_handle_h handle,
369                                         ndef_message_h message)
370 {
371         net_nfc_error_e out_result = NET_NFC_OK;
372         GError *error = NULL;
373         GVariant *arg_data;
374
375         if (handle == NULL || message == NULL)
376                 return NET_NFC_NULL_PARAMETER;
377
378         if (ndef_proxy == NULL)
379         {
380                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
381                 {
382                         DEBUG_ERR_MSG("tag_proxy fail");
383                         return NET_NFC_NOT_INITIALIZED;
384                 }
385         }
386
387         /* prevent executing daemon when nfc is off */
388         if (net_nfc_client_manager_is_activated() == false) {
389                 return NET_NFC_NOT_ACTIVATED;
390         }
391
392         if (net_nfc_client_tag_is_connected() == FALSE)
393                 return NET_NFC_NOT_CONNECTED;
394
395         arg_data = net_nfc_util_gdbus_ndef_message_to_variant(message);
396
397         if (net_nfc_gdbus_ndef_call_write_sync(ndef_proxy ,
398                                         GPOINTER_TO_UINT(handle),
399                                         arg_data,
400                                         (gint *)&out_result,
401                                         NULL,
402                                         &error) == FALSE)
403         {
404                 DEBUG_ERR_MSG("can not call write: %s",
405                                 error->message);
406                 out_result = NET_NFC_IPC_FAIL;
407
408                 g_error_free(error);
409         }
410
411         return out_result;
412 }
413
414 NET_NFC_EXPORT_API
415 net_nfc_error_e net_nfc_client_ndef_make_read_only(
416                         net_nfc_target_handle_h handle,
417                         net_nfc_client_ndef_make_read_only_completed callback,
418                         void *user_data)
419 {
420         NetNfcCallback *func_data;
421
422         if (handle == NULL)
423                 return NET_NFC_NULL_PARAMETER;
424
425         if (ndef_proxy == NULL)
426         {
427                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
428                 {
429                         DEBUG_ERR_MSG("tag_proxy fail");
430                         return NET_NFC_NOT_INITIALIZED;
431                 }
432         }
433
434         /* prevent executing daemon when nfc is off */
435         if (net_nfc_client_manager_is_activated() == false) {
436                 return NET_NFC_NOT_ACTIVATED;
437         }
438
439         if (net_nfc_client_tag_is_connected() == FALSE)
440                 return NET_NFC_NOT_CONNECTED;
441
442         if (ndef_is_supported_tag() == FALSE)
443                 return NET_NFC_NOT_SUPPORTED;
444
445         func_data = g_try_new0(NetNfcCallback, 1);
446         if (func_data == NULL) {
447                 return NET_NFC_ALLOC_FAIL;
448         }
449
450         func_data->callback = (gpointer)callback;
451         func_data->user_data = user_data;
452
453         net_nfc_gdbus_ndef_call_make_read_only(ndef_proxy,
454                                 GPOINTER_TO_UINT(handle),
455                                 NULL,
456                                 ndef_call_make_read_only,
457                                 func_data);
458
459         return NET_NFC_OK;
460 }
461
462 NET_NFC_EXPORT_API
463 net_nfc_error_e net_nfc_client_ndef_make_read_only_sync(
464                                         net_nfc_target_handle_h handle)
465 {
466         net_nfc_error_e out_result = NET_NFC_OK;
467         GError *error = NULL;
468
469         if (handle == NULL)
470                 return NET_NFC_NULL_PARAMETER;
471
472         if (ndef_proxy == NULL)
473         {
474                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
475                 {
476                         DEBUG_ERR_MSG("tag_proxy fail");
477                         return NET_NFC_NOT_INITIALIZED;
478                 }
479         }
480
481         /* prevent executing daemon when nfc is off */
482         if (net_nfc_client_manager_is_activated() == false) {
483                 return NET_NFC_NOT_ACTIVATED;
484         }
485
486         if (net_nfc_client_tag_is_connected() == FALSE)
487                 return NET_NFC_NOT_CONNECTED;
488
489         if (ndef_is_supported_tag() == FALSE)
490                 return NET_NFC_NOT_SUPPORTED;
491
492         if (net_nfc_gdbus_ndef_call_make_read_only_sync(ndef_proxy,
493                                         GPOINTER_TO_UINT(handle),
494                                         (gint *)&out_result,
495                                         NULL,
496                                         &error) == FALSE)
497         {
498                 DEBUG_ERR_MSG("can not make read only: %s",
499                                 error->message);
500                 out_result = NET_NFC_IPC_FAIL;
501
502                 g_error_free(error);
503         }
504
505         return out_result;
506 }
507
508 NET_NFC_EXPORT_API
509 net_nfc_error_e net_nfc_client_ndef_format(net_nfc_target_handle_h handle,
510                                 data_h key,
511                                 net_nfc_client_ndef_format_completed callback,
512                                 void *user_data)
513 {
514         NetNfcCallback *func_data;
515         GVariant *arg_data;
516
517         if (handle == NULL)
518                 return NET_NFC_NULL_PARAMETER;
519
520         if (ndef_proxy == NULL)
521         {
522                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
523                 {
524                         DEBUG_ERR_MSG("tag_proxy fail");
525                         return NET_NFC_NOT_INITIALIZED;
526                 }
527         }
528
529         /* prevent executing daemon when nfc is off */
530         if (net_nfc_client_manager_is_activated() == false) {
531                 return NET_NFC_NOT_ACTIVATED;
532         }
533
534         if (net_nfc_client_tag_is_connected() == FALSE)
535                 return NET_NFC_NOT_CONNECTED;
536
537         func_data = g_try_new0(NetNfcCallback, 1);
538         if (func_data == NULL) {
539                 return NET_NFC_ALLOC_FAIL;
540         }
541
542         func_data->callback = (gpointer)callback;
543         func_data->user_data = user_data;
544
545         arg_data = net_nfc_util_gdbus_data_to_variant((data_s *)key);
546
547         net_nfc_gdbus_ndef_call_format(ndef_proxy ,
548                                 GPOINTER_TO_UINT(handle),
549                                 arg_data,
550                                 NULL,
551                                 ndef_call_format,
552                                 func_data);
553
554         return NET_NFC_OK;
555 }
556
557 NET_NFC_EXPORT_API
558 net_nfc_error_e net_nfc_client_ndef_format_sync(
559                                         net_nfc_target_handle_h handle,
560                                         data_h key)
561 {
562         net_nfc_error_e out_result = NET_NFC_OK;
563         GVariant *arg_data;
564         GError *error = NULL;
565
566         if (handle == NULL)
567                 return NET_NFC_NULL_PARAMETER;
568
569         if (ndef_proxy == NULL)
570         {
571                 if(net_nfc_client_ndef_init() != NET_NFC_OK)
572                 {
573                         DEBUG_ERR_MSG("tag_proxy fail");
574                         return NET_NFC_NOT_INITIALIZED;
575                 }
576         }
577
578         /* prevent executing daemon when nfc is off */
579         if (net_nfc_client_manager_is_activated() == false) {
580                 return NET_NFC_NOT_ACTIVATED;
581         }
582
583         if (net_nfc_client_tag_is_connected() == FALSE)
584                 return NET_NFC_NOT_CONNECTED;
585
586         arg_data = net_nfc_util_gdbus_data_to_variant((data_s *)key);
587
588         if (net_nfc_gdbus_ndef_call_format_sync(ndef_proxy ,
589                                         GPOINTER_TO_UINT(handle),
590                                         arg_data,
591                                         (gint *)&out_result,
592                                         NULL,
593                                         &error) == FALSE)
594         {
595                 DEBUG_ERR_MSG("can not call format: %s",
596                                 error->message);
597                 out_result = NET_NFC_IPC_FAIL;
598
599                 g_error_free(error);
600         }
601
602         return out_result;
603 }
604
605 net_nfc_error_e net_nfc_client_ndef_init(void)
606 {
607         GError *error = NULL;
608
609         if (ndef_proxy)
610         {
611                 DEBUG_CLIENT_MSG("Already initialized");
612
613                 return NET_NFC_OK;
614         }
615
616         ndef_proxy = net_nfc_gdbus_ndef_proxy_new_for_bus_sync(
617                                 G_BUS_TYPE_SYSTEM,
618                                 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
619                                 "org.tizen.NetNfcService",
620                                 "/org/tizen/NetNfcService/Ndef",
621                                 NULL,
622                                 &error);
623         if (ndef_proxy == NULL)
624         {
625                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
626                 g_error_free(error);
627
628                 return NET_NFC_UNKNOWN_ERROR;
629         }
630
631         return NET_NFC_OK;
632 }
633
634 void net_nfc_client_ndef_deinit(void)
635 {
636         if (ndef_proxy)
637         {
638                 g_object_unref(ndef_proxy);
639                 ndef_proxy = NULL;
640         }
641 }