Fix for wrong statement
[platform/core/api/nfc.git] / src / net_nfc_client_test.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_debug_internal.h"
18 #include "net_nfc_util_gdbus_internal.h"
19 #include "net_nfc_gdbus.h"
20 #include "net_nfc_client.h"
21 #include "net_nfc_client_util_internal.h"
22 #include "net_nfc_client_manager.h"
23 #include "net_nfc_client_test.h"
24
25 #ifndef NET_NFC_EXPORT_API
26 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
27 #endif
28
29 /* LCOV_EXCL_START */
30
31 static void test_call_sim_test_callback(GObject *source_object,
32                                         GAsyncResult *res,
33                                         gpointer user_data);
34
35 static void test_call_prbs_test_callback(GObject *source_object,
36                                         GAsyncResult *res,
37                                         gpointer user_data);
38
39 static void test_call_get_firmware_version_callback(GObject *source_object,
40                                                 GAsyncResult *res,
41                                                 gpointer user_data);
42
43 static void test_call_set_ee_data_callback(GObject *source_object,
44                                         GAsyncResult *res,
45                                         gpointer user_data);
46
47 static void test_call_ese_test_callback(GObject *source_object,
48                                         GAsyncResult *res,
49                                         gpointer user_data);
50
51 static NetNfcGDbusTest *test_proxy = NULL;
52
53
54 static void test_call_sim_test_callback(GObject *source_object,
55                                         GAsyncResult *res,
56                                         gpointer user_data)
57 {
58         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
59         net_nfc_error_e out_result;
60         GError *error = NULL;
61
62         g_assert(user_data != NULL);
63
64         if (net_nfc_gdbus_test_call_sim_test_finish(
65                                 NET_NFC_GDBUS_TEST(source_object),
66                                 (gint *)&out_result,
67                                 res,
68                                 &error) == FALSE) {
69                 DEBUG_ERR_MSG("Can not finish sim_test: %s",
70                         error->message);
71                 out_result = NET_NFC_IPC_FAIL;
72
73                 g_error_free(error);
74         }
75
76         if (func_data->callback != NULL) {
77                 net_nfc_client_test_sim_test_completed callback =
78                         (net_nfc_client_test_sim_test_completed)func_data->callback;
79
80                 callback(out_result, func_data->user_data);
81         }
82
83         g_free(func_data);
84 }
85
86 static void test_call_prbs_test_callback(GObject *source_object,
87                                         GAsyncResult *res,
88                                         gpointer user_data)
89 {
90         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
91         net_nfc_error_e out_result;
92         GError *error = NULL;
93
94         g_assert(user_data != NULL);
95
96         if (net_nfc_gdbus_test_call_prbs_test_finish(
97                                 NET_NFC_GDBUS_TEST(source_object),
98                                 (gint *)&out_result,
99                                 res,
100                                 &error) == FALSE) {
101                 DEBUG_ERR_MSG("Can not finish prbs test: %s",
102                         error->message);
103                 out_result = NET_NFC_IPC_FAIL;
104
105                 g_error_free(error);
106         }
107
108         if (func_data->callback != NULL) {
109                 net_nfc_client_test_prbs_test_completed callback =
110                         (net_nfc_client_test_prbs_test_completed)func_data->callback;
111
112                 callback(out_result, func_data->user_data);
113         }
114
115         g_free(func_data);
116 }
117
118 static void test_call_get_firmware_version_callback(GObject *source_object,
119                                                 GAsyncResult *res,
120                                                 gpointer user_data)
121 {
122         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
123         net_nfc_error_e out_result;
124         gchar *out_version = NULL;
125         GError *error = NULL;
126
127         g_assert(user_data != NULL);
128
129         if (net_nfc_gdbus_test_call_get_firmware_version_finish(
130                                 NET_NFC_GDBUS_TEST(source_object),
131                                 (gint *)&out_result,
132                                 &out_version,
133                                 res,
134                                 &error) == FALSE) {
135                 DEBUG_ERR_MSG("Can not finish get_firmware_version: %s",
136                         error->message);
137                 out_result = NET_NFC_IPC_FAIL;
138
139                 g_error_free(error);
140         }
141
142         if (func_data->callback != NULL) {
143                 net_nfc_client_test_get_firmware_version_completed callback =
144                         (net_nfc_client_test_get_firmware_version_completed)func_data->callback;
145
146                 callback(out_result, out_version, func_data->user_data);
147         }
148
149         g_free(out_version);
150         g_free(func_data);
151 }
152
153 static void test_call_set_ee_data_callback(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_test_call_set_ee_data_finish(
164                                 NET_NFC_GDBUS_TEST(source_object),
165                                 (gint *)&out_result,
166                                 res,
167                                 &error) == FALSE) {
168                 DEBUG_ERR_MSG("Can not finish set_ee_data: %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_test_set_ee_data_completed callback =
177                         (net_nfc_client_test_set_ee_data_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 test_call_ese_test_callback(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_test_call_ese_test_finish(
196                                 NET_NFC_GDBUS_TEST(source_object),
197                                 (gint *)&out_result,
198                                 res,
199                                 &error) == FALSE) {
200                 DEBUG_ERR_MSG("Can not finish sim_test: %s",
201                         error->message);
202                 out_result = NET_NFC_IPC_FAIL;
203
204                 g_error_free(error);
205         }
206
207         if (func_data->callback != NULL) {
208                 net_nfc_client_test_ese_test_completed callback =
209                         (net_nfc_client_test_ese_test_completed)func_data->callback;
210
211                 callback(out_result, func_data->user_data);
212         }
213
214         g_free(func_data);
215 }
216
217
218 NET_NFC_EXPORT_API
219 net_nfc_error_e net_nfc_client_test_sim_test(
220                         net_nfc_client_test_sim_test_completed callback,
221                         void *user_data)
222 {
223         NetNfcCallback *func_data;
224
225         if (test_proxy == NULL) {
226                 if (net_nfc_client_test_init() != NET_NFC_OK) {
227                         DEBUG_ERR_MSG("test_proxy fail");
228                         return NET_NFC_NOT_INITIALIZED;
229                 }
230         }
231
232         /* prevent executing daemon when nfc is off */
233         if (net_nfc_client_manager_is_activated() == false)
234                 return NET_NFC_NOT_ACTIVATED;
235
236         func_data = g_try_new0(NetNfcCallback, 1);
237         if (func_data == NULL)
238                 return NET_NFC_ALLOC_FAIL;
239
240         func_data->callback = (gpointer)callback;
241         func_data->user_data = user_data;
242
243         net_nfc_gdbus_test_call_sim_test(test_proxy,
244                                         NULL,
245                                         test_call_sim_test_callback,
246                                         func_data);
247
248         return NET_NFC_OK;
249 }
250
251 NET_NFC_EXPORT_API
252 net_nfc_error_e net_nfc_client_test_sim_test_sync(void)
253 {
254         net_nfc_error_e out_result = NET_NFC_OK;
255         GError *error = NULL;
256
257         if (test_proxy == NULL) {
258                 if (net_nfc_client_test_init() != NET_NFC_OK) {
259                         DEBUG_ERR_MSG("test_proxy fail");
260                         return NET_NFC_NOT_INITIALIZED;
261                 }
262         }
263
264         /* prevent executing daemon when nfc is off */
265         if (net_nfc_client_manager_is_activated() == false)
266                 return NET_NFC_NOT_ACTIVATED;
267
268         if (net_nfc_gdbus_test_call_sim_test_sync(test_proxy,
269                                         (gint *)&out_result,
270                                         NULL,
271                                         &error) == FALSE) {
272                 DEBUG_ERR_MSG("can not call SimTest: %s",
273                                 error->message);
274                 out_result = NET_NFC_IPC_FAIL;
275
276                 g_error_free(error);
277         }
278
279         return out_result;
280 }
281
282 NET_NFC_EXPORT_API
283 net_nfc_error_e net_nfc_client_test_prbs_test(uint32_t tech,
284                         uint32_t rate,
285                         net_nfc_client_test_prbs_test_completed callback,
286                         void *user_data)
287 {
288         NetNfcCallback *func_data;
289
290         if (test_proxy == NULL) {
291                 if (net_nfc_client_test_init() != NET_NFC_OK) {
292                         DEBUG_ERR_MSG("test_proxy fail");
293                         return NET_NFC_NOT_INITIALIZED;
294                 }
295         }
296
297         /* prevent executing daemon when nfc is off */
298         if (net_nfc_client_manager_is_activated() == false)
299                 return NET_NFC_NOT_ACTIVATED;
300
301         func_data = g_try_new0(NetNfcCallback, 1);
302         if (func_data == NULL)
303                 return NET_NFC_ALLOC_FAIL;
304
305         func_data->callback = (gpointer)callback;
306         func_data->user_data = user_data;
307
308         net_nfc_gdbus_test_call_prbs_test(test_proxy,
309                                         tech,
310                                         rate,
311                                         NULL,
312                                         test_call_prbs_test_callback,
313                                         func_data);
314
315         return NET_NFC_OK;
316 }
317
318 NET_NFC_EXPORT_API
319 net_nfc_error_e net_nfc_client_test_prbs_test_sync(uint32_t tech,
320                                                 uint32_t rate)
321 {
322         net_nfc_error_e out_result = NET_NFC_OK;
323         GError *error = NULL;
324
325         if (test_proxy == NULL) {
326                 if (net_nfc_client_test_init() != NET_NFC_OK) {
327                         DEBUG_ERR_MSG("test_proxy fail");
328                         return NET_NFC_NOT_INITIALIZED;
329                 }
330         }
331
332         /* prevent executing daemon when nfc is off */
333         if (net_nfc_client_manager_is_activated() == false)
334                 return NET_NFC_NOT_ACTIVATED;
335
336         if (net_nfc_gdbus_test_call_prbs_test_sync(test_proxy,
337                                         tech,
338                                         rate,
339                                         (gint *)&out_result,
340                                         NULL,
341                                         &error) == FALSE) {
342                 DEBUG_ERR_MSG("can not call PrbsTest: %s",
343                                 error->message);
344                 out_result = NET_NFC_IPC_FAIL;
345
346                 g_error_free(error);
347         }
348
349         return out_result;
350 }
351
352 NET_NFC_EXPORT_API
353 net_nfc_error_e net_nfc_client_test_get_firmware_version(
354                 net_nfc_client_test_get_firmware_version_completed callback,
355                 void *user_data)
356 {
357         NetNfcCallback *func_data;
358
359         if (test_proxy == NULL) {
360                 if (net_nfc_client_test_init() != NET_NFC_OK) {
361                         DEBUG_ERR_MSG("test_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         func_data = g_try_new0(NetNfcCallback, 1);
371         if (func_data == NULL)
372                 return NET_NFC_ALLOC_FAIL;
373
374         func_data->callback = (gpointer)callback;
375         func_data->user_data = user_data;
376
377         net_nfc_gdbus_test_call_get_firmware_version(test_proxy,
378                                 NULL,
379                                 test_call_get_firmware_version_callback,
380                                 func_data);
381
382         return NET_NFC_OK;
383 }
384
385 NET_NFC_EXPORT_API
386 net_nfc_error_e net_nfc_client_test_get_firmware_version_sync(char **version)
387 {
388         net_nfc_error_e out_result = NET_NFC_OK;
389         gchar *out_version = NULL;
390         GError *error = NULL;
391
392         if (version == NULL)
393                 return NET_NFC_NULL_PARAMETER;
394
395         *version = NULL;
396
397         if (test_proxy == NULL) {
398                 if (net_nfc_client_test_init() != NET_NFC_OK) {
399                         DEBUG_ERR_MSG("test_proxy fail");
400                         return NET_NFC_NOT_INITIALIZED;
401                 }
402         }
403
404         /* prevent executing daemon when nfc is off */
405         if (net_nfc_client_manager_is_activated() == false)
406                 return NET_NFC_NOT_ACTIVATED;
407
408         if (net_nfc_gdbus_test_call_get_firmware_version_sync(test_proxy,
409                                         (gint *)&out_result,
410                                         &out_version,
411                                         NULL,
412                                         &error) == TRUE) {
413                 *version = out_version;
414         } else {
415                 DEBUG_ERR_MSG("can not call Get Firmware version: %s",
416                                 error->message);
417                 out_result = NET_NFC_IPC_FAIL;
418
419                 g_error_free(error);
420         }
421
422         return out_result;
423 }
424
425 NET_NFC_EXPORT_API
426 net_nfc_error_e net_nfc_client_test_set_ee_data(int mode,
427                         int reg_id,
428                         data_h data,
429                         net_nfc_client_test_set_ee_data_completed callback,
430                         void *user_data)
431 {
432         NetNfcCallback *func_data;
433         GVariant *variant;
434
435         if (test_proxy == NULL) {
436                 if (net_nfc_client_test_init() != NET_NFC_OK) {
437                         DEBUG_ERR_MSG("test_proxy fail");
438                         return NET_NFC_NOT_INITIALIZED;
439                 }
440         }
441
442         /* prevent executing daemon when nfc is off */
443         if (net_nfc_client_manager_is_activated() == false)
444                 return NET_NFC_NOT_ACTIVATED;
445
446         func_data = g_try_new0(NetNfcCallback, 1);
447         if (func_data == NULL)
448                 return NET_NFC_ALLOC_FAIL;
449
450         func_data->callback = (gpointer)callback;
451         func_data->user_data = user_data;
452
453         variant = net_nfc_util_gdbus_data_to_variant((data_s *)data);
454
455         net_nfc_gdbus_test_call_set_ee_data(test_proxy,
456                                         mode,
457                                         reg_id,
458                                         variant,
459                                         NULL,
460                                         test_call_set_ee_data_callback,
461                                         func_data);
462
463         return NET_NFC_OK;
464 }
465
466 NET_NFC_EXPORT_API
467 net_nfc_error_e net_nfc_client_test_set_ee_data_sync(int mode,
468                                                 int reg_id,
469                                                 data_h data)
470 {
471         net_nfc_error_e out_result = NET_NFC_OK;
472         GVariant *variant = NULL;
473         GError *error = NULL;
474
475         if (test_proxy == NULL) {
476                 if (net_nfc_client_test_init() != NET_NFC_OK) {
477                         DEBUG_ERR_MSG("test_proxy fail");
478                         return NET_NFC_NOT_INITIALIZED;
479                 }
480         }
481
482         /* prevent executing daemon when nfc is off */
483         if (net_nfc_client_manager_is_activated() == false)
484                 return NET_NFC_NOT_ACTIVATED;
485
486         variant = net_nfc_util_gdbus_data_to_variant((data_s *)data);
487
488         if (net_nfc_gdbus_test_call_set_ee_data_sync(test_proxy,
489                                         mode,
490                                         reg_id,
491                                         variant,
492                                         (gint *)&out_result,
493                                         NULL,
494                                         &error) == FALSE) {
495                 DEBUG_ERR_MSG("can not call SetEeTest: %s",
496                                 error->message);
497                 out_result = NET_NFC_IPC_FAIL;
498
499                 g_error_free(error);
500         }
501
502         return out_result;
503 }
504
505 NET_NFC_EXPORT_API
506 net_nfc_error_e net_nfc_client_test_ese_test(
507                         net_nfc_client_test_ese_test_completed callback,
508                         void *user_data)
509 {
510         NetNfcCallback *func_data;
511
512         DEBUG_CLIENT_MSG("NFC ESE Test!!!!");
513
514         if (test_proxy == NULL) {
515                 if (net_nfc_client_test_init() != NET_NFC_OK) {
516                         DEBUG_ERR_MSG("test_proxy fail");
517                         return NET_NFC_NOT_INITIALIZED;
518                 }
519         }
520
521         /* prevent executing daemon when nfc is off */
522         if (net_nfc_client_manager_is_activated() == false)
523                 return NET_NFC_NOT_ACTIVATED;
524
525         func_data = g_try_new0(NetNfcCallback, 1);
526         if (func_data == NULL)
527                 return NET_NFC_ALLOC_FAIL;
528
529         func_data->callback = (gpointer)callback;
530         func_data->user_data = user_data;
531
532         net_nfc_gdbus_test_call_ese_test(test_proxy,
533                                         NULL,
534                                         test_call_ese_test_callback,
535                                         func_data);
536
537         return NET_NFC_OK;
538 }
539
540 NET_NFC_EXPORT_API
541 net_nfc_error_e net_nfc_client_test_ese_test_sync(void)
542 {
543         net_nfc_error_e out_result = NET_NFC_OK;
544         GError *error = NULL;
545
546         DEBUG_CLIENT_MSG("NFC ESE Test SYNC!!!!");
547
548         if (test_proxy == NULL) {
549                 if (net_nfc_client_test_init() != NET_NFC_OK) {
550                         DEBUG_ERR_MSG("test_proxy fail");
551                         return NET_NFC_NOT_INITIALIZED;
552                 }
553         }
554
555         /* prevent executing daemon when nfc is off */
556         if (net_nfc_client_manager_is_activated() == false)
557                 return NET_NFC_NOT_ACTIVATED;
558
559         if (net_nfc_gdbus_test_call_ese_test_sync(test_proxy,
560                                         (gint *)&out_result,
561                                         NULL,
562                                         &error) == FALSE) {
563                 DEBUG_ERR_MSG("can not call ESE Test: %s",
564                                 error->message);
565                 out_result = NET_NFC_IPC_FAIL;
566
567                 g_error_free(error);
568         }
569
570         return out_result;
571 }
572
573 NET_NFC_EXPORT_API
574 net_nfc_error_e net_nfc_client_test_set_se_tech_type_sync(
575         net_nfc_se_type_e type, int tech)
576 {
577         net_nfc_error_e out_result = NET_NFC_OK;
578         GError *error = NULL;
579
580         if (test_proxy == NULL) {
581                 if (net_nfc_client_test_init() != NET_NFC_OK) {
582                         DEBUG_ERR_MSG("test_proxy fail");
583                         return NET_NFC_NOT_INITIALIZED;
584                 }
585         }
586
587         /* prevent executing daemon when nfc is off */
588         if (net_nfc_client_manager_is_activated() == false)
589                 return NET_NFC_NOT_ACTIVATED;
590
591         if (net_nfc_gdbus_test_call_set_se_tech_type_sync(test_proxy,
592                                         (guint32)type,
593                                         (guint32)tech,
594                                         (gint *)&out_result,
595                                         NULL,
596                                         &error) == FALSE) {
597                 DEBUG_ERR_MSG("can not call SetSeTechType: %s",
598                                 error->message);
599                 out_result = NET_NFC_IPC_FAIL;
600
601                 g_error_free(error);
602         }
603
604         return out_result;
605 }
606
607 NET_NFC_EXPORT_API
608 net_nfc_error_e net_nfc_client_test_set_listen_tech_mask_sync(uint32_t tech)
609 {
610         net_nfc_error_e result = NET_NFC_OK;
611         GError *error = NULL;
612
613         DEBUG_CLIENT_MSG("net_nfc_client_test_set_listen_tech_mask_sync start");
614
615         if (test_proxy == NULL) {
616                 if (net_nfc_client_test_init() != NET_NFC_OK) {
617                         DEBUG_ERR_MSG("test_proxy fail");
618                         return NET_NFC_NOT_INITIALIZED;
619                 }
620         }
621
622         /* prevent executing daemon when nfc is off */
623         if (net_nfc_client_manager_is_activated() == false)
624                 return NET_NFC_NOT_ACTIVATED;
625
626         if (net_nfc_gdbus_test_call_set_listen_tech_mask_sync(test_proxy,
627                                         tech,
628                                         &result,
629                                         NULL,
630                                         &error) == FALSE) {
631                 DEBUG_ERR_MSG("can not call listen tech mask: %s",
632                                 error->message);
633                 result = NET_NFC_IPC_FAIL;
634
635                 g_error_free(error);
636         }
637
638         return result;
639 }
640
641
642 net_nfc_error_e net_nfc_client_test_init(void)
643 {
644         GError *error = NULL;
645
646         if (test_proxy) {
647                 DEBUG_CLIENT_MSG("Already initialized");
648
649                 return NET_NFC_OK;
650         }
651
652         test_proxy = net_nfc_gdbus_test_proxy_new_for_bus_sync(
653                                         G_BUS_TYPE_SYSTEM,
654                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
655                                         "org.tizen.NetNfcService",
656                                         "/org/tizen/NetNfcService/Test",
657                                         NULL,
658                                         &error);
659         if (test_proxy == NULL) {
660                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
661                 g_error_free(error);
662
663                 return NET_NFC_UNKNOWN_ERROR;
664         }
665
666         return NET_NFC_OK;
667 }
668
669 void net_nfc_client_test_deinit(void)
670 {
671         if (test_proxy) {
672                 g_object_unref(test_proxy);
673                 test_proxy = NULL;
674         }
675 }
676
677 /* LCOV_EXCL_STOP */
678