[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[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                 "(uu)",
33                 (guint *)&callback,
34                 (guint *)&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 > NFC_AC_TYPE_UNKNOWN);
100         CHECK_ACTIVATED();
101
102         switch (type) {
103         case NFC_AC_TYPE_BT:
104                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
105                 break;
106
107         case NFC_AC_TYPE_WIFI:
108                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
109                 break;
110
111         case NFC_AC_TYPE_WIFI_DIRECT:
112                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
113                 break;
114
115         case NFC_AC_TYPE_UNKNOWN:
116                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
117                 break;
118
119         default:
120                 break;
121         }
122
123         parameter = g_variant_new("(uu)",
124                 GPOINTER_TO_UINT(callback),
125                 GPOINTER_TO_UINT(user_data));
126         if (parameter != NULL) {
127                 ret = net_nfc_client_p2p_connection_handover(
128                         (net_nfc_target_handle_h)target,
129                         net_ac_type,
130                         _p2p_handover_cb,
131                         parameter);
132                 if (ret != NET_NFC_OK)
133                         g_variant_unref(parameter);
134         } else {
135                 ret = NET_NFC_ALLOC_FAIL;
136         }
137
138         return nfc_common_convert_error_code(__func__, ret);
139         /* LCOV_EXCL_STOP */
140 }
141
142 /* LCOV_EXCL_START */
143 static void _connection_handover_event_cb(
144         net_nfc_handover_event_e event,
145         net_nfc_error_e result,
146         net_nfc_conn_handover_carrier_type_e carrier,
147         data_h ac_data,
148         data_h ndef_message,
149         void *user_data)
150 {
151         LOG_BEGIN();
152
153         if (gdbus_nfc_context.on_handover_event_cb != NULL) {
154                 nfc_ndef_message_h message;
155                 nfc_ac_type_e type;
156                 char *address;
157
158                 net_nfc_create_ndef_message_from_rawdata(&message, ndef_message);
159
160                 if (event == NET_NFC_HANDOVER_START) {
161                         type = NFC_AC_TYPE_UNKNOWN;
162                         address = NULL;
163                 } else {
164                         switch (carrier) {
165                         case NET_NFC_CONN_HANDOVER_CARRIER_BT:
166                                 type = NFC_AC_TYPE_BT;
167                                 address = nfc_common_get_bt_address_string(ac_data);
168                                 break;
169
170                         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
171                                 type = NFC_AC_TYPE_WIFI;
172                                 address = nfc_common_get_bt_address_string(ac_data);
173                                 break;
174
175                         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
176                                 type = NFC_AC_TYPE_WIFI_DIRECT;
177                                 address = nfc_common_get_bt_address_string(ac_data);
178                                 break;
179
180                         default:
181                                 type = NFC_AC_TYPE_UNKNOWN;
182                                 address = NULL;
183                                 break;
184                         }
185                 }
186
187                 gdbus_nfc_context.on_handover_event_cb(
188                         nfc_common_convert_error_code(__func__, result),
189                         event,
190                         type,
191                         address,
192                         message,
193                         gdbus_nfc_context.on_handover_event_user_data);
194
195                 if (address != NULL)
196                         g_free(address);
197                 net_nfc_free_ndef_message(message);
198         }
199
200         LOG_END();
201 }
202 /* LCOV_EXCL_STOP */
203
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         /* LCOV_EXCL_START */
211         CHECK_INIT();
212         CHECK_INVALID(callback == NULL);
213
214         gdbus_nfc_context.on_handover_event_cb = callback;
215         gdbus_nfc_context.on_handover_event_user_data = user_data;
216
217         net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
218
219         return NFC_ERROR_NONE;
220         /* LCOV_EXCL_STOP */
221 }
222
223 int nfc_connection_handover_unset_event_cb(void)
224 {
225         LOG_BEGIN();
226
227         CHECK_SUPPORTED(NFC_P2P_FEATURE);
228
229         /* LCOV_EXCL_START */
230         CHECK_INIT();
231
232         net_nfc_client_handover_unset_handover_event_cb();
233
234         gdbus_nfc_context.on_handover_event_cb = NULL;
235         gdbus_nfc_context.on_handover_event_user_data = NULL;
236
237         return NFC_ERROR_NONE;
238         /* LCOV_EXCL_STOP */
239 }
240
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         /* LCOV_EXCL_START */
255         if (carrier == NFC_AC_TYPE_BT)
256                 _is_support_ac_type = true;
257
258         if (_is_support_ac_type)
259                 set_last_result(NFC_ERROR_NONE);
260         else
261                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
262
263         return _is_support_ac_type;
264         /* LCOV_EXCL_STOP */
265 }
266
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         /* LCOV_EXCL_START */
276         CHECK_INIT();
277         CHECK_INVALID(msg == NULL);
278         CHECK_INVALID(result == NULL);
279
280         ret = net_nfc_import_handover_from_ndef_message(
281                 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
282
283         return nfc_common_convert_error_code(__func__, ret);
284         /* LCOV_EXCL_STOP */
285 }
286
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         /* LCOV_EXCL_START */
296         CHECK_INIT();
297         CHECK_INVALID(message == NULL);
298         CHECK_INVALID(random_number == NULL);
299
300         result = net_nfc_get_handover_random_number(
301                 (net_nfc_ch_message_h)message,
302                 random_number);
303
304         return nfc_common_convert_error_code(__func__, result);
305         /* LCOV_EXCL_STOP */
306 }
307
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         /* LCOV_EXCL_START */
317         CHECK_INIT();
318         CHECK_INVALID(message == NULL);
319         CHECK_INVALID(count == NULL);
320
321         result = net_nfc_get_handover_carrier_count(
322                 (net_nfc_ch_message_h)message,
323                 count);
324
325         return nfc_common_convert_error_code(__func__, result);
326         /* LCOV_EXCL_STOP */
327 }
328
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         /* LCOV_EXCL_START */
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         /* LCOV_EXCL_STOP */
349 }
350
351 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
352 {
353         net_nfc_error_e result;
354         net_nfc_conn_handover_carrier_type_e temp;
355
356         LOG_BEGIN();
357
358         CHECK_SUPPORTED(NFC_P2P_FEATURE);
359
360         /* LCOV_EXCL_START */
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         /* LCOV_EXCL_STOP */
389 }
390
391 int nfc_handover_message_destroy(nfc_handover_message_h message)
392 {
393         net_nfc_error_e result;
394
395         LOG_BEGIN();
396
397         CHECK_SUPPORTED(NFC_P2P_FEATURE);
398
399         /* LCOV_EXCL_START */
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         /* LCOV_EXCL_STOP */
407 }
408
409
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         /* LCOV_EXCL_START */
420         CHECK_INIT();
421         CHECK_INVALID(carrier == NULL);
422         CHECK_INVALID(cps == NULL);
423
424         result = net_nfc_get_handover_carrier_cps(
425                 (net_nfc_ch_carrier_h)carrier, &temp);
426         if (result == NET_NFC_OK) {
427                 switch (temp) {
428                 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE:
429                         *cps = NFC_AC_STATE_INACTIVATE;
430                         break;
431
432                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE:
433                         *cps = NFC_AC_STATE_ACTIVATE;
434                         break;
435
436                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING:
437                         *cps = NFC_AC_STATE_ACTIVATING;
438                         break;
439
440                 default:
441                         *cps = NFC_AC_STATE_UNKNOWN;
442                         break;
443                 }
444         }
445
446         return nfc_common_convert_error_code(__func__, result);
447         /* LCOV_EXCL_STOP */
448 }
449
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         /* LCOV_EXCL_START */
460         CHECK_INIT();
461         CHECK_INVALID(carrier == NULL);
462         CHECK_INVALID(type == NULL);
463
464         result = net_nfc_get_handover_carrier_type(
465                 (net_nfc_ch_carrier_h)carrier, &temp);
466         if (result == NET_NFC_OK) {
467                 switch (temp) {
468                 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
469                         *type = NFC_AC_TYPE_BT;
470                         break;
471
472                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
473                         *type = NFC_AC_TYPE_WIFI;
474                         break;
475
476                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
477                         *type = NFC_AC_TYPE_WIFI_DIRECT;
478                         break;
479
480                 default:
481                         *type = NFC_AC_TYPE_UNKNOWN;
482                         break;
483                 }
484         }
485
486         return nfc_common_convert_error_code(__func__, result);
487         /* LCOV_EXCL_STOP */
488 }
489
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         /* LCOV_EXCL_START */
499         CHECK_INIT();
500         CHECK_INVALID(carrier == NULL);
501         CHECK_INVALID(record == NULL);
502
503         result = net_nfc_get_handover_carrier_record(
504                 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
505
506         return nfc_common_convert_error_code(__func__, result);
507         /* LCOV_EXCL_STOP */
508 }
509
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         /* LCOV_EXCL_START */
519         CHECK_INIT();
520         CHECK_INVALID(carrier == NULL);
521         CHECK_INVALID(count == NULL);
522
523         result = net_nfc_get_handover_auxiliary_record_count(
524                 (net_nfc_ch_carrier_h)carrier, count);
525
526         return nfc_common_convert_error_code(__func__, result);
527         /* LCOV_EXCL_STOP */
528 }
529
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         /* LCOV_EXCL_START */
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         /* LCOV_EXCL_STOP */
549 }
550
551 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
552 {
553         net_nfc_error_e result;
554
555         LOG_BEGIN();
556
557         CHECK_SUPPORTED(NFC_P2P_FEATURE);
558
559         /* LCOV_EXCL_START */
560         CHECK_INIT();
561         CHECK_INVALID(carrier == NULL);
562         CHECK_INVALID(config == NULL);
563
564         result = net_nfc_create_carrier_config_from_carrier(
565                 (net_nfc_carrier_config_h *)config,
566                 (net_nfc_ch_carrier_h)carrier);
567
568         return nfc_common_convert_error_code(__func__, result);
569         /* LCOV_EXCL_STOP */
570 }
571
572 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
573 {
574         net_nfc_error_e result;
575
576         LOG_BEGIN();
577
578         CHECK_SUPPORTED(NFC_P2P_FEATURE);
579
580         /* LCOV_EXCL_START */
581         CHECK_INIT();
582         CHECK_INVALID(carrier == NULL);
583
584         result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
585
586         return nfc_common_convert_error_code(__func__, result);
587         /* LCOV_EXCL_STOP */
588 }
589
590
591 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
592 {
593         net_nfc_error_e result;
594
595         LOG_BEGIN();
596
597         CHECK_SUPPORTED(NFC_P2P_FEATURE);
598
599         /* LCOV_EXCL_START */
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         /* LCOV_EXCL_STOP */
611 }
612
613 int nfc_handover_config_destroy(nfc_handover_config_h config)
614 {
615         net_nfc_error_e result;
616
617         LOG_BEGIN();
618
619         CHECK_SUPPORTED(NFC_P2P_FEATURE);
620
621         /* LCOV_EXCL_START */
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         /* LCOV_EXCL_STOP */
629 }
630