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