Added a gcov flag to gauge line & function coverage
[platform/core/api/nfc.git] / src / nfc_p2p_handover.c
1 /*
2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 "nfc_common.h"
18
19 /* LCOV_EXCL_START */
20 static void _p2p_handover_cb(net_nfc_error_e result,
21         net_nfc_conn_handover_carrier_type_e carrier, data_h ac_data, void *user_data)
22 {
23         nfc_p2p_connection_handover_completed_cb callback;
24         void *user_param;
25
26         LOG_BEGIN();
27
28         if (user_data == NULL)
29                 return;
30
31         g_variant_get((GVariant *)user_data,
32                 "(tt)",
33                 &callback,
34                 &user_param);
35
36         if (callback != NULL) {
37                 nfc_ac_type_e carrior_type;
38                 char *buffer;
39                 int len = 0;
40
41                 switch (carrier) {
42                 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
43                         carrior_type = NFC_AC_TYPE_BT;
44                         buffer = nfc_common_get_bt_address_string(ac_data);
45                         if (buffer != NULL)
46                                 len = strlen(buffer);
47                         break;
48
49                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
50                         carrior_type = NFC_AC_TYPE_WIFI;
51                         buffer = nfc_common_get_bt_address_string(ac_data);
52                         if (buffer != NULL)
53                                 len = strlen(buffer);
54                         break;
55
56                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
57                         carrior_type = NFC_AC_TYPE_WIFI_DIRECT;
58                         buffer = nfc_common_get_bt_address_string(ac_data);
59                         if (buffer != NULL)
60                                 len = strlen(buffer);
61                         break;
62
63                 default:
64                         carrior_type = NFC_AC_TYPE_UNKNOWN;
65                         buffer = NULL;
66                         len = 0;
67                         break;
68                 }
69
70                 callback(nfc_common_convert_error_code(__func__, result),
71                         carrior_type, (void *)buffer,
72                         len, user_param);
73
74                 if (buffer != NULL)
75                         g_free(buffer);
76         }
77
78         g_variant_unref((GVariant *)user_data);
79 }
80 /* LCOV_EXCL_STOP */
81
82 /* LCOV_EXCL_START */
83 int nfc_p2p_connection_handover(nfc_p2p_target_h target,
84         nfc_ac_type_e type,
85         nfc_p2p_connection_handover_completed_cb callback,
86         void *user_data)
87 {
88         int ret;
89         net_nfc_conn_handover_carrier_type_e net_ac_type =
90                 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
91         GVariant *parameter;
92
93         LOG_BEGIN();
94
95         CHECK_SUPPORTED(NFC_P2P_FEATURE);
96
97         CHECK_INIT();
98         CHECK_INVALID(target == NULL);
99         CHECK_INVALID(type < 0);
100         CHECK_INVALID(type > NFC_AC_TYPE_UNKNOWN);
101         CHECK_ACTIVATED();
102
103         switch (type) {
104         case NFC_AC_TYPE_BT:
105                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
106                 break;
107
108         case NFC_AC_TYPE_WIFI:
109                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
110                 break;
111
112         case NFC_AC_TYPE_WIFI_DIRECT:
113                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
114                 break;
115
116         case NFC_AC_TYPE_UNKNOWN:
117                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
118                 break;
119         }
120
121         parameter = g_variant_new("(tt)",
122                 callback,
123                 user_data);
124         if (parameter != NULL) {
125                 ret = net_nfc_client_p2p_connection_handover(
126                         (net_nfc_target_handle_h)target,
127                         net_ac_type,
128                         _p2p_handover_cb,
129                         parameter);
130                 if (ret != NET_NFC_OK)
131                         g_variant_unref(parameter);
132         } else {
133                 ret = NET_NFC_ALLOC_FAIL;
134         }
135
136         return nfc_common_convert_error_code(__func__, ret);
137
138 }
139 /* LCOV_EXCL_STOP */
140
141 /* LCOV_EXCL_START */
142 static void _connection_handover_event_cb(
143         net_nfc_handover_event_e event,
144         net_nfc_error_e result,
145         net_nfc_conn_handover_carrier_type_e carrier,
146         data_h ac_data,
147         data_h ndef_message,
148         void *user_data)
149 {
150         LOG_BEGIN();
151
152         if (gdbus_nfc_context.on_handover_event_cb != NULL) {
153                 nfc_ndef_message_h message;
154                 nfc_ac_type_e type;
155                 char *address;
156
157                 net_nfc_create_ndef_message_from_rawdata(&message, ndef_message);
158
159                 if (event == NET_NFC_HANDOVER_START) {
160                         type = NFC_AC_TYPE_UNKNOWN;
161                         address = NULL;
162                 } else {
163                         switch (carrier) {
164                         case NET_NFC_CONN_HANDOVER_CARRIER_BT:
165                                 type = NFC_AC_TYPE_BT;
166                                 address = nfc_common_get_bt_address_string(ac_data);
167                                 break;
168
169                         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
170                                 type = NFC_AC_TYPE_WIFI;
171                                 address = nfc_common_get_bt_address_string(ac_data);
172                                 break;
173
174                         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
175                                 type = NFC_AC_TYPE_WIFI_DIRECT;
176                                 address = nfc_common_get_bt_address_string(ac_data);
177                                 break;
178
179                         default:
180                                 type = NFC_AC_TYPE_UNKNOWN;
181                                 address = NULL;
182                                 break;
183                         }
184                 }
185
186                 gdbus_nfc_context.on_handover_event_cb(
187                         nfc_common_convert_error_code(__func__, result),
188                         event,
189                         type,
190                         address,
191                         message,
192                         gdbus_nfc_context.on_handover_event_user_data);
193
194                 if (address != NULL)
195                         g_free(address);
196                 net_nfc_free_ndef_message(message);
197         }
198
199         LOG_END();
200 }
201 /* LCOV_EXCL_STOP */
202
203 /* LCOV_EXCL_START */
204 int nfc_connection_handover_set_event_cb(nfc_connection_handover_event_cb callback, void *user_data)
205 {
206         LOG_BEGIN();
207
208         CHECK_SUPPORTED(NFC_P2P_FEATURE);
209
210         CHECK_INIT();
211         CHECK_INVALID(callback == NULL);
212
213         gdbus_nfc_context.on_handover_event_cb = callback;
214         gdbus_nfc_context.on_handover_event_user_data = user_data;
215
216         net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
217
218         return NFC_ERROR_NONE;
219 }
220 /* LCOV_EXCL_STOP */
221
222 /* LCOV_EXCL_START */
223 int nfc_connection_handover_unset_event_cb(void)
224 {
225         LOG_BEGIN();
226
227         CHECK_SUPPORTED(NFC_P2P_FEATURE);
228
229         CHECK_INIT();
230
231         net_nfc_client_handover_unset_handover_event_cb();
232
233         gdbus_nfc_context.on_handover_event_cb = NULL;
234         gdbus_nfc_context.on_handover_event_user_data = NULL;
235
236         return NFC_ERROR_NONE;
237 }
238 /* LCOV_EXCL_STOP */
239
240 /* LCOV_EXCL_START */
241 bool nfc_p2p_is_supported_ac_type(nfc_ac_type_e carrier)
242 {
243         bool _is_support_p2p = false;
244         bool _is_support_ac_type = false;
245
246         LOG_BEGIN();
247
248         _is_support_p2p = nfc_common_is_supported(NFC_P2P_FEATURE);
249         if (!_is_support_p2p) {
250                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
251                 return false;
252         }
253
254         if (carrier == NFC_AC_TYPE_BT)
255                 _is_support_ac_type = true;
256
257         if (_is_support_ac_type)
258                 set_last_result(NFC_ERROR_NONE);
259         else
260                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
261
262         return _is_support_ac_type;
263 }
264 /* LCOV_EXCL_STOP */
265
266 /* LCOV_EXCL_START */
267 int nfc_handover_message_import_from_ndef_message(nfc_handover_message_h *result, nfc_ndef_message_h msg)
268 {
269         net_nfc_error_e ret;
270
271         LOG_BEGIN();
272
273         CHECK_SUPPORTED(NFC_P2P_FEATURE);
274
275         CHECK_INIT();
276         CHECK_INVALID(msg == NULL);
277         CHECK_INVALID(result == NULL);
278
279         ret = net_nfc_import_handover_from_ndef_message(
280                 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
281
282         return nfc_common_convert_error_code(__func__, ret);
283 }
284 /* LCOV_EXCL_STOP */
285
286 /* LCOV_EXCL_START */
287 int nfc_handover_message_get_random_number(nfc_handover_message_h message, unsigned short *random_number)
288 {
289         net_nfc_error_e result;
290
291         LOG_BEGIN();
292
293         CHECK_SUPPORTED(NFC_P2P_FEATURE);
294
295         CHECK_INIT();
296         CHECK_INVALID(message == NULL);
297         CHECK_INVALID(random_number == NULL);
298
299         result = net_nfc_get_handover_random_number(
300                 (net_nfc_ch_message_h)message,
301                 random_number);
302
303         return nfc_common_convert_error_code(__func__, result);
304 }
305 /* LCOV_EXCL_STOP */
306
307 /* LCOV_EXCL_START */
308 int nfc_handover_message_get_carrier_count(nfc_handover_message_h message, unsigned int *count)
309 {
310         net_nfc_error_e result;
311
312         LOG_BEGIN();
313
314         CHECK_SUPPORTED(NFC_P2P_FEATURE);
315
316         CHECK_INIT();
317         CHECK_INVALID(message == NULL);
318         CHECK_INVALID(count == NULL);
319
320         result = net_nfc_get_handover_carrier_count(
321                 (net_nfc_ch_message_h)message,
322                 count);
323
324         return nfc_common_convert_error_code(__func__, result);
325 }
326 /* LCOV_EXCL_STOP */
327
328 /* LCOV_EXCL_START */
329 int nfc_handover_message_get_carrier(nfc_handover_message_h message, int index, nfc_handover_carrier_h *carrier)
330 {
331         net_nfc_error_e result;
332
333         LOG_BEGIN();
334
335         CHECK_SUPPORTED(NFC_P2P_FEATURE);
336
337
338         CHECK_INIT();
339         CHECK_INVALID(message == NULL);
340         CHECK_INVALID(carrier == NULL);
341
342         result = net_nfc_get_handover_carrier(
343                 (net_nfc_ch_message_h)message,
344                 index,
345                 (net_nfc_ch_carrier_h *)carrier);
346
347         return nfc_common_convert_error_code(__func__, result);
348 }
349 /* LCOV_EXCL_STOP */
350
351 /* LCOV_EXCL_START */
352 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
353 {
354         net_nfc_error_e result;
355         net_nfc_conn_handover_carrier_type_e temp;
356
357         LOG_BEGIN();
358
359         CHECK_SUPPORTED(NFC_P2P_FEATURE);
360
361         CHECK_INIT();
362         CHECK_INVALID(message == NULL);
363         CHECK_INVALID(carrier == NULL);
364
365         switch (type) {
366         case NFC_AC_TYPE_BT:
367                 temp = NET_NFC_CONN_HANDOVER_CARRIER_BT;
368                 break;
369
370         case NFC_AC_TYPE_WIFI:
371                 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
372                 break;
373
374         case NFC_AC_TYPE_WIFI_DIRECT:
375                 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
376                 break;
377
378         default:
379                 temp = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
380                 break;
381         }
382         result = net_nfc_get_handover_carrier_by_type(
383                 (net_nfc_ch_message_h)message,
384                 temp,
385                 (net_nfc_ch_carrier_h *)carrier);
386
387         return nfc_common_convert_error_code(__func__, result);
388 }
389 /* LCOV_EXCL_STOP */
390
391 /* LCOV_EXCL_START */
392 int nfc_handover_message_destroy(nfc_handover_message_h message)
393 {
394         net_nfc_error_e result;
395
396         LOG_BEGIN();
397
398         CHECK_SUPPORTED(NFC_P2P_FEATURE);
399
400         CHECK_INIT();
401         CHECK_INVALID(message == NULL);
402
403         result = net_nfc_free_handover_message((net_nfc_ch_message_h)message);
404
405         return nfc_common_convert_error_code(__func__, result);
406 }
407 /* LCOV_EXCL_STOP */
408
409 /* LCOV_EXCL_START */
410 int nfc_handover_carrier_get_cps(nfc_handover_carrier_h carrier, nfc_ac_state_e *cps)
411 {
412         net_nfc_error_e result;
413         net_nfc_conn_handover_carrier_state_e temp;
414
415         LOG_BEGIN();
416
417         CHECK_SUPPORTED(NFC_P2P_FEATURE);
418
419         CHECK_INIT();
420         CHECK_INVALID(carrier == NULL);
421         CHECK_INVALID(cps == NULL);
422
423         result = net_nfc_get_handover_carrier_cps(
424                 (net_nfc_ch_carrier_h)carrier, &temp);
425         if (result == NET_NFC_OK) {
426                 switch (temp) {
427                 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE:
428                         *cps = NFC_AC_STATE_INACTIVATE;
429                         break;
430
431                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE:
432                         *cps = NFC_AC_STATE_ACTIVATE;
433                         break;
434
435                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING:
436                         *cps = NFC_AC_STATE_ACTIVATING;
437                         break;
438
439                 default:
440                         *cps = NFC_AC_STATE_UNKNOWN;
441                         break;
442                 }
443         }
444
445         return nfc_common_convert_error_code(__func__, result);
446 }
447 /* LCOV_EXCL_STOP */
448
449 /* LCOV_EXCL_START */
450 int nfc_handover_carrier_get_type(nfc_handover_carrier_h carrier, nfc_ac_type_e *type)
451 {
452         net_nfc_error_e result;
453         net_nfc_conn_handover_carrier_type_e temp;
454
455         LOG_BEGIN();
456
457         CHECK_SUPPORTED(NFC_P2P_FEATURE);
458
459         CHECK_INIT();
460         CHECK_INVALID(carrier == NULL);
461         CHECK_INVALID(type == NULL);
462
463         result = net_nfc_get_handover_carrier_type(
464                 (net_nfc_ch_carrier_h)carrier, &temp);
465         if (result == NET_NFC_OK) {
466                 switch (temp) {
467                 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
468                         *type = NFC_AC_TYPE_BT;
469                         break;
470
471                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
472                         *type = NFC_AC_TYPE_WIFI;
473                         break;
474
475                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
476                         *type = NFC_AC_TYPE_WIFI_DIRECT;
477                         break;
478
479                 default:
480                         *type = NFC_AC_TYPE_UNKNOWN;
481                         break;
482                 }
483         }
484
485         return nfc_common_convert_error_code(__func__, result);
486 }
487 /* LCOV_EXCL_STOP */
488
489 /* LCOV_EXCL_START */
490 int nfc_handover_carrier_get_carrier_record(nfc_handover_carrier_h carrier, nfc_ndef_record_h *record)
491 {
492         net_nfc_error_e result;
493
494         LOG_BEGIN();
495
496         CHECK_SUPPORTED(NFC_P2P_FEATURE);
497
498         CHECK_INIT();
499         CHECK_INVALID(carrier == NULL);
500         CHECK_INVALID(record == NULL);
501
502         result = net_nfc_get_handover_carrier_record(
503                 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
504
505         return nfc_common_convert_error_code(__func__, result);
506 }
507 /* LCOV_EXCL_STOP */
508
509 /* LCOV_EXCL_START */
510 int nfc_handover_carrier_get_auxiliary_record_count(nfc_handover_carrier_h carrier, unsigned int *count)
511 {
512         net_nfc_error_e result;
513
514         LOG_BEGIN();
515
516         CHECK_SUPPORTED(NFC_P2P_FEATURE);
517
518         CHECK_INIT();
519         CHECK_INVALID(carrier == NULL);
520         CHECK_INVALID(count == NULL);
521
522         result = net_nfc_get_handover_auxiliary_record_count(
523                 (net_nfc_ch_carrier_h)carrier, count);
524
525         return nfc_common_convert_error_code(__func__, result);
526 }
527 /* LCOV_EXCL_STOP */
528
529 /* LCOV_EXCL_START */
530 int nfc_handover_carrier_get_auxiliary_record(nfc_handover_carrier_h carrier, int index, nfc_ndef_record_h *record)
531 {
532         net_nfc_error_e result;
533
534         LOG_BEGIN();
535
536         CHECK_SUPPORTED(NFC_P2P_FEATURE);
537
538
539         CHECK_INIT();
540         CHECK_INVALID(carrier == NULL);
541         CHECK_INVALID(record == NULL);
542
543         result = net_nfc_get_handover_auxiliary_record(
544                 (net_nfc_ch_carrier_h)carrier,
545                 index, (ndef_record_h *)record);
546
547         return nfc_common_convert_error_code(__func__, result);
548 }
549 /* LCOV_EXCL_STOP */
550
551 /* LCOV_EXCL_START */
552 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
553 {
554         net_nfc_error_e result;
555
556         LOG_BEGIN();
557
558         CHECK_SUPPORTED(NFC_P2P_FEATURE);
559
560
561         CHECK_INIT();
562         CHECK_INVALID(carrier == NULL);
563         CHECK_INVALID(config == NULL);
564
565         result = net_nfc_create_carrier_config_from_carrier(
566                 (net_nfc_carrier_config_h *)config,
567                 (net_nfc_ch_carrier_h)carrier);
568
569         return nfc_common_convert_error_code(__func__, result);
570 }
571 /* LCOV_EXCL_STOP */
572
573 /* LCOV_EXCL_START */
574 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
575 {
576         net_nfc_error_e result;
577
578         LOG_BEGIN();
579
580         CHECK_SUPPORTED(NFC_P2P_FEATURE);
581
582         CHECK_INIT();
583         CHECK_INVALID(carrier == NULL);
584
585         result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
586
587         return nfc_common_convert_error_code(__func__, result);
588 }
589 /* LCOV_EXCL_STOP */
590
591 /* LCOV_EXCL_START */
592 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
593 {
594         net_nfc_error_e result;
595
596         LOG_BEGIN();
597
598         CHECK_SUPPORTED(NFC_P2P_FEATURE);
599
600         CHECK_INIT();
601         CHECK_INVALID(config == NULL);
602         CHECK_INVALID(size == NULL);
603         CHECK_INVALID(data == NULL);
604
605         result = net_nfc_get_carrier_config_property(
606                 (net_nfc_carrier_config_h)config,
607                 attribute, size, data);
608
609         return nfc_common_convert_error_code(__func__, result);
610 }
611 /* LCOV_EXCL_STOP */
612
613 /* LCOV_EXCL_START */
614 int nfc_handover_config_destroy(nfc_handover_config_h config)
615 {
616         net_nfc_error_e result;
617
618         LOG_BEGIN();
619
620         CHECK_SUPPORTED(NFC_P2P_FEATURE);
621
622         CHECK_INIT();
623         CHECK_INVALID(config == NULL);
624
625         result = net_nfc_free_carrier_config((net_nfc_carrier_config_h)config);
626
627         return nfc_common_convert_error_code(__func__, result);
628 }
629 /* LCOV_EXCL_STOP */