Sync code with Tizen 3.0 branch
[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 int nfc_p2p_connection_handover(nfc_p2p_target_h target,
83         nfc_ac_type_e type,
84         nfc_p2p_connection_handover_completed_cb callback,
85         void *user_data)
86 {
87         int ret;
88         net_nfc_conn_handover_carrier_type_e net_ac_type =
89                 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
90         GVariant *parameter;
91
92         LOG_BEGIN();
93
94         CHECK_SUPPORTED(NFC_P2P_FEATURE);
95
96         /* LCOV_EXCL_START */
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         /* LCOV_EXCL_STOP */
138 }
139
140 /* LCOV_EXCL_START */
141 static void _connection_handover_event_cb(
142         net_nfc_handover_event_e event,
143         net_nfc_error_e result,
144         net_nfc_conn_handover_carrier_type_e carrier,
145         data_h ac_data,
146         data_h ndef_message,
147         void *user_data)
148 {
149         LOG_BEGIN();
150
151         if (gdbus_nfc_context.on_handover_event_cb != NULL) {
152                 nfc_ndef_message_h message;
153                 nfc_ac_type_e type;
154                 char *address;
155
156                 net_nfc_create_ndef_message_from_rawdata(&message, ndef_message);
157
158                 if (event == NET_NFC_HANDOVER_START) {
159                         type = NFC_AC_TYPE_UNKNOWN;
160                         address = NULL;
161                 } else {
162                         switch (carrier) {
163                         case NET_NFC_CONN_HANDOVER_CARRIER_BT:
164                                 type = NFC_AC_TYPE_BT;
165                                 address = nfc_common_get_bt_address_string(ac_data);
166                                 break;
167
168                         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
169                                 type = NFC_AC_TYPE_WIFI;
170                                 address = nfc_common_get_bt_address_string(ac_data);
171                                 break;
172
173                         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
174                                 type = NFC_AC_TYPE_WIFI_DIRECT;
175                                 address = nfc_common_get_bt_address_string(ac_data);
176                                 break;
177
178                         default:
179                                 type = NFC_AC_TYPE_UNKNOWN;
180                                 address = NULL;
181                                 break;
182                         }
183                 }
184
185                 gdbus_nfc_context.on_handover_event_cb(
186                         nfc_common_convert_error_code(__func__, result),
187                         event,
188                         type,
189                         address,
190                         message,
191                         gdbus_nfc_context.on_handover_event_user_data);
192
193                 if (address != NULL)
194                         g_free(address);
195                 net_nfc_free_ndef_message(message);
196         }
197
198         LOG_END();
199 }
200 /* LCOV_EXCL_STOP */
201
202 int nfc_connection_handover_set_event_cb(nfc_connection_handover_event_cb callback, void *user_data)
203 {
204         LOG_BEGIN();
205
206         CHECK_SUPPORTED(NFC_P2P_FEATURE);
207
208         /* LCOV_EXCL_START */
209         CHECK_INIT();
210         CHECK_INVALID(callback == NULL);
211
212         gdbus_nfc_context.on_handover_event_cb = callback;
213         gdbus_nfc_context.on_handover_event_user_data = user_data;
214
215         net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
216
217         return NFC_ERROR_NONE;
218         /* LCOV_EXCL_STOP */
219 }
220
221 int nfc_connection_handover_unset_event_cb(void)
222 {
223         LOG_BEGIN();
224
225         CHECK_SUPPORTED(NFC_P2P_FEATURE);
226
227         /* LCOV_EXCL_START */
228         CHECK_INIT();
229
230         net_nfc_client_handover_unset_handover_event_cb();
231
232         gdbus_nfc_context.on_handover_event_cb = NULL;
233         gdbus_nfc_context.on_handover_event_user_data = NULL;
234
235         return NFC_ERROR_NONE;
236         /* LCOV_EXCL_STOP */
237 }
238
239 bool nfc_p2p_is_supported_ac_type(nfc_ac_type_e carrier)
240 {
241         bool _is_support_p2p = false;
242         bool _is_support_ac_type = false;
243
244         LOG_BEGIN();
245
246         _is_support_p2p = nfc_common_is_supported(NFC_P2P_FEATURE);
247         if (!_is_support_p2p) {
248                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
249                 return false;
250         }
251
252         /* LCOV_EXCL_START */
253         if (carrier == NFC_AC_TYPE_BT)
254                 _is_support_ac_type = true;
255
256         if (_is_support_ac_type)
257                 set_last_result(NFC_ERROR_NONE);
258         else
259                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
260
261         return _is_support_ac_type;
262         /* LCOV_EXCL_STOP */
263 }
264
265 int nfc_handover_message_import_from_ndef_message(nfc_handover_message_h *result, nfc_ndef_message_h msg)
266 {
267         net_nfc_error_e ret;
268
269         LOG_BEGIN();
270
271         CHECK_SUPPORTED(NFC_P2P_FEATURE);
272
273         /* LCOV_EXCL_START */
274         CHECK_INIT();
275         CHECK_INVALID(msg == NULL);
276         CHECK_INVALID(result == NULL);
277
278         ret = net_nfc_import_handover_from_ndef_message(
279                 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
280
281         return nfc_common_convert_error_code(__func__, ret);
282         /* LCOV_EXCL_STOP */
283 }
284
285 int nfc_handover_message_get_random_number(nfc_handover_message_h message, unsigned short *random_number)
286 {
287         net_nfc_error_e result;
288
289         LOG_BEGIN();
290
291         CHECK_SUPPORTED(NFC_P2P_FEATURE);
292
293         /* LCOV_EXCL_START */
294         CHECK_INIT();
295         CHECK_INVALID(message == NULL);
296         CHECK_INVALID(random_number == NULL);
297
298         result = net_nfc_get_handover_random_number(
299                 (net_nfc_ch_message_h)message,
300                 random_number);
301
302         return nfc_common_convert_error_code(__func__, result);
303         /* LCOV_EXCL_STOP */
304 }
305
306 int nfc_handover_message_get_carrier_count(nfc_handover_message_h message, unsigned int *count)
307 {
308         net_nfc_error_e result;
309
310         LOG_BEGIN();
311
312         CHECK_SUPPORTED(NFC_P2P_FEATURE);
313
314         /* LCOV_EXCL_START */
315         CHECK_INIT();
316         CHECK_INVALID(message == NULL);
317         CHECK_INVALID(count == NULL);
318
319         result = net_nfc_get_handover_carrier_count(
320                 (net_nfc_ch_message_h)message,
321                 count);
322
323         return nfc_common_convert_error_code(__func__, result);
324         /* LCOV_EXCL_STOP */
325 }
326
327 int nfc_handover_message_get_carrier(nfc_handover_message_h message, int index, nfc_handover_carrier_h *carrier)
328 {
329         net_nfc_error_e result;
330
331         LOG_BEGIN();
332
333         CHECK_SUPPORTED(NFC_P2P_FEATURE);
334
335         /* LCOV_EXCL_START */
336         CHECK_INIT();
337         CHECK_INVALID(message == NULL);
338         CHECK_INVALID(carrier == NULL);
339
340         result = net_nfc_get_handover_carrier(
341                 (net_nfc_ch_message_h)message,
342                 index,
343                 (net_nfc_ch_carrier_h *)carrier);
344
345         return nfc_common_convert_error_code(__func__, result);
346         /* LCOV_EXCL_STOP */
347 }
348
349 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
350 {
351         net_nfc_error_e result;
352         net_nfc_conn_handover_carrier_type_e temp;
353
354         LOG_BEGIN();
355
356         CHECK_SUPPORTED(NFC_P2P_FEATURE);
357
358         /* LCOV_EXCL_START */
359         CHECK_INIT();
360         CHECK_INVALID(message == NULL);
361         CHECK_INVALID(carrier == NULL);
362
363         switch (type) {
364         case NFC_AC_TYPE_BT:
365                 temp = NET_NFC_CONN_HANDOVER_CARRIER_BT;
366                 break;
367
368         case NFC_AC_TYPE_WIFI:
369                 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
370                 break;
371
372         case NFC_AC_TYPE_WIFI_DIRECT:
373                 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
374                 break;
375
376         default:
377                 temp = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
378                 break;
379         }
380         result = net_nfc_get_handover_carrier_by_type(
381                 (net_nfc_ch_message_h)message,
382                 temp,
383                 (net_nfc_ch_carrier_h *)carrier);
384
385         return nfc_common_convert_error_code(__func__, result);
386         /* LCOV_EXCL_STOP */
387 }
388
389 int nfc_handover_message_destroy(nfc_handover_message_h message)
390 {
391         net_nfc_error_e result;
392
393         LOG_BEGIN();
394
395         CHECK_SUPPORTED(NFC_P2P_FEATURE);
396
397         /* LCOV_EXCL_START */
398         CHECK_INIT();
399         CHECK_INVALID(message == NULL);
400
401         result = net_nfc_free_handover_message((net_nfc_ch_message_h)message);
402
403         return nfc_common_convert_error_code(__func__, result);
404         /* LCOV_EXCL_STOP */
405 }
406
407
408 int nfc_handover_carrier_get_cps(nfc_handover_carrier_h carrier, nfc_ac_state_e *cps)
409 {
410         net_nfc_error_e result;
411         net_nfc_conn_handover_carrier_state_e temp;
412
413         LOG_BEGIN();
414
415         CHECK_SUPPORTED(NFC_P2P_FEATURE);
416
417         /* LCOV_EXCL_START */
418         CHECK_INIT();
419         CHECK_INVALID(carrier == NULL);
420         CHECK_INVALID(cps == NULL);
421
422         result = net_nfc_get_handover_carrier_cps(
423                 (net_nfc_ch_carrier_h)carrier, &temp);
424         if (result == NET_NFC_OK) {
425                 switch (temp) {
426                 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE:
427                         *cps = NFC_AC_STATE_INACTIVATE;
428                         break;
429
430                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE:
431                         *cps = NFC_AC_STATE_ACTIVATE;
432                         break;
433
434                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING:
435                         *cps = NFC_AC_STATE_ACTIVATING;
436                         break;
437
438                 default:
439                         *cps = NFC_AC_STATE_UNKNOWN;
440                         break;
441                 }
442         }
443
444         return nfc_common_convert_error_code(__func__, result);
445         /* LCOV_EXCL_STOP */
446 }
447
448 int nfc_handover_carrier_get_type(nfc_handover_carrier_h carrier, nfc_ac_type_e *type)
449 {
450         net_nfc_error_e result;
451         net_nfc_conn_handover_carrier_type_e temp;
452
453         LOG_BEGIN();
454
455         CHECK_SUPPORTED(NFC_P2P_FEATURE);
456
457         /* LCOV_EXCL_START */
458         CHECK_INIT();
459         CHECK_INVALID(carrier == NULL);
460         CHECK_INVALID(type == NULL);
461
462         result = net_nfc_get_handover_carrier_type(
463                 (net_nfc_ch_carrier_h)carrier, &temp);
464         if (result == NET_NFC_OK) {
465                 switch (temp) {
466                 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
467                         *type = NFC_AC_TYPE_BT;
468                         break;
469
470                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
471                         *type = NFC_AC_TYPE_WIFI;
472                         break;
473
474                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
475                         *type = NFC_AC_TYPE_WIFI_DIRECT;
476                         break;
477
478                 default:
479                         *type = NFC_AC_TYPE_UNKNOWN;
480                         break;
481                 }
482         }
483
484         return nfc_common_convert_error_code(__func__, result);
485         /* LCOV_EXCL_STOP */
486 }
487
488 int nfc_handover_carrier_get_carrier_record(nfc_handover_carrier_h carrier, nfc_ndef_record_h *record)
489 {
490         net_nfc_error_e result;
491
492         LOG_BEGIN();
493
494         CHECK_SUPPORTED(NFC_P2P_FEATURE);
495
496         /* LCOV_EXCL_START */
497         CHECK_INIT();
498         CHECK_INVALID(carrier == NULL);
499         CHECK_INVALID(record == NULL);
500
501         result = net_nfc_get_handover_carrier_record(
502                 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
503
504         return nfc_common_convert_error_code(__func__, result);
505         /* LCOV_EXCL_STOP */
506 }
507
508 int nfc_handover_carrier_get_auxiliary_record_count(nfc_handover_carrier_h carrier, unsigned int *count)
509 {
510         net_nfc_error_e result;
511
512         LOG_BEGIN();
513
514         CHECK_SUPPORTED(NFC_P2P_FEATURE);
515
516         /* LCOV_EXCL_START */
517         CHECK_INIT();
518         CHECK_INVALID(carrier == NULL);
519         CHECK_INVALID(count == NULL);
520
521         result = net_nfc_get_handover_auxiliary_record_count(
522                 (net_nfc_ch_carrier_h)carrier, count);
523
524         return nfc_common_convert_error_code(__func__, result);
525         /* LCOV_EXCL_STOP */
526 }
527
528 int nfc_handover_carrier_get_auxiliary_record(nfc_handover_carrier_h carrier, int index, nfc_ndef_record_h *record)
529 {
530         net_nfc_error_e result;
531
532         LOG_BEGIN();
533
534         CHECK_SUPPORTED(NFC_P2P_FEATURE);
535
536         /* LCOV_EXCL_START */
537         CHECK_INIT();
538         CHECK_INVALID(carrier == NULL);
539         CHECK_INVALID(record == NULL);
540
541         result = net_nfc_get_handover_auxiliary_record(
542                 (net_nfc_ch_carrier_h)carrier,
543                 index, (ndef_record_h *)record);
544
545         return nfc_common_convert_error_code(__func__, result);
546         /* LCOV_EXCL_STOP */
547 }
548
549 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
550 {
551         net_nfc_error_e result;
552
553         LOG_BEGIN();
554
555         CHECK_SUPPORTED(NFC_P2P_FEATURE);
556
557         /* LCOV_EXCL_START */
558         CHECK_INIT();
559         CHECK_INVALID(carrier == NULL);
560         CHECK_INVALID(config == NULL);
561
562         result = net_nfc_create_carrier_config_from_carrier(
563                 (net_nfc_carrier_config_h *)config,
564                 (net_nfc_ch_carrier_h)carrier);
565
566         return nfc_common_convert_error_code(__func__, result);
567         /* LCOV_EXCL_STOP */
568 }
569
570 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
571 {
572         net_nfc_error_e result;
573
574         LOG_BEGIN();
575
576         CHECK_SUPPORTED(NFC_P2P_FEATURE);
577
578         /* LCOV_EXCL_START */
579         CHECK_INIT();
580         CHECK_INVALID(carrier == NULL);
581
582         result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
583
584         return nfc_common_convert_error_code(__func__, result);
585         /* LCOV_EXCL_STOP */
586 }
587
588
589 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
590 {
591         net_nfc_error_e result;
592
593         LOG_BEGIN();
594
595         CHECK_SUPPORTED(NFC_P2P_FEATURE);
596
597         /* LCOV_EXCL_START */
598         CHECK_INIT();
599         CHECK_INVALID(config == NULL);
600         CHECK_INVALID(size == NULL);
601         CHECK_INVALID(data == NULL);
602
603         result = net_nfc_get_carrier_config_property(
604                 (net_nfc_carrier_config_h)config,
605                 attribute, size, data);
606
607         return nfc_common_convert_error_code(__func__, result);
608         /* LCOV_EXCL_STOP */
609 }
610
611 int nfc_handover_config_destroy(nfc_handover_config_h config)
612 {
613         net_nfc_error_e result;
614
615         LOG_BEGIN();
616
617         CHECK_SUPPORTED(NFC_P2P_FEATURE);
618
619         /* LCOV_EXCL_START */
620         CHECK_INIT();
621         CHECK_INVALID(config == NULL);
622
623         result = net_nfc_free_carrier_config((net_nfc_carrier_config_h)config);
624
625         return nfc_common_convert_error_code(__func__, result);
626         /* LCOV_EXCL_STOP */
627 }
628