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