tizen 2.3.1 release
[framework/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 static void _p2p_handover_cb(net_nfc_error_e result,
20         net_nfc_conn_handover_carrier_type_e carrier, data_h ac_data, void *user_data)
21 {
22         nfc_p2p_connection_handover_completed_cb callback;
23         void *user_param;
24
25         LOG_BEGIN();
26
27         if (user_data == NULL) {
28                 return;
29         }
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
79         g_variant_unref((GVariant *)user_data);
80 }
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         CHECK_INIT();
96         CHECK_INVALID(target == NULL);
97         CHECK_INVALID(type > NFC_AC_TYPE_UNKNOWN);
98         CHECK_ACTIVATED();
99
100         switch (type)
101         {
102         case NFC_AC_TYPE_BT :
103                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
104                 break;
105
106         case NFC_AC_TYPE_WIFI :
107                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
108                 break;
109
110         case NFC_AC_TYPE_WIFI_DIRECT :
111                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
112                 break;
113
114         case NFC_AC_TYPE_UNKNOWN :
115                 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
116                 break;
117
118         default:
119                 break;
120         }
121
122         parameter = g_variant_new("(uu)",
123                 GPOINTER_TO_UINT(callback),
124                 GPOINTER_TO_UINT(user_data));
125         if (parameter != NULL) {
126                 ret = net_nfc_client_p2p_connection_handover(
127                         (net_nfc_target_handle_h)target,
128                         net_ac_type,
129                         _p2p_handover_cb,
130                         parameter);
131                 if (ret != NET_NFC_OK) {
132                         g_variant_unref(parameter);
133                 }
134         } else {
135                 ret = NET_NFC_ALLOC_FAIL;
136         }
137
138         return nfc_common_convert_error_code(__func__, ret);
139 }
140
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                 }
196                 net_nfc_free_ndef_message(message);
197         }
198
199         LOG_END();
200 }
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         CHECK_INIT();
208         CHECK_INVALID(callback == NULL);
209
210         gdbus_nfc_context.on_handover_event_cb = callback;
211         gdbus_nfc_context.on_handover_event_user_data = user_data;
212
213         net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
214
215         return NFC_ERROR_NONE;
216 }
217
218 int nfc_connection_handover_unset_event_cb(void)
219 {
220         LOG_BEGIN();
221
222         CHECK_SUPPORTED(NFC_P2P_FEATURE);
223         CHECK_INIT();
224
225         net_nfc_client_handover_unset_handover_event_cb();
226
227         gdbus_nfc_context.on_handover_event_cb = NULL;
228         gdbus_nfc_context.on_handover_event_user_data = NULL;
229
230         return NFC_ERROR_NONE;
231 }
232
233 bool nfc_p2p_is_supported_ac_type(nfc_ac_type_e carrier)
234 {
235         bool result = false;
236
237         LOG_BEGIN();
238
239         if (carrier == NFC_AC_TYPE_BT)
240                 result = true;
241
242         return result;
243 }
244
245 int nfc_handover_message_import_from_ndef_message(nfc_handover_message_h *result, nfc_ndef_message_h msg)
246 {
247         net_nfc_error_e ret;
248
249         LOG_BEGIN();
250
251         CHECK_SUPPORTED(NFC_P2P_FEATURE);
252         CHECK_INIT();
253         CHECK_INVALID(msg == NULL);
254         CHECK_INVALID(result == NULL);
255
256         ret = net_nfc_import_handover_from_ndef_message(
257                 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
258
259         return nfc_common_convert_error_code(__func__, ret);
260 }
261
262 int nfc_handover_message_get_random_number(nfc_handover_message_h message, unsigned short *random_number)
263 {
264         net_nfc_error_e result;
265
266         LOG_BEGIN();
267
268         CHECK_SUPPORTED(NFC_P2P_FEATURE);
269         CHECK_INIT();
270         CHECK_INVALID(message == NULL);
271         CHECK_INVALID(random_number == NULL);
272
273         result = net_nfc_get_handover_random_number(
274                 (net_nfc_ch_message_h)message,
275                 random_number);
276
277         return nfc_common_convert_error_code(__func__, result);
278 }
279
280 int nfc_handover_message_get_carrier_count(nfc_handover_message_h message, unsigned int *count)
281 {
282         net_nfc_error_e result;
283
284         LOG_BEGIN();
285
286         CHECK_SUPPORTED(NFC_P2P_FEATURE);
287         CHECK_INIT();
288         CHECK_INVALID(message == NULL);
289         CHECK_INVALID(count == NULL);
290
291         result = net_nfc_get_handover_carrier_count(
292                 (net_nfc_ch_message_h)message,
293                 count);
294
295         return nfc_common_convert_error_code(__func__, result);
296 }
297
298 int nfc_handover_message_get_carrier(nfc_handover_message_h message, int index, nfc_handover_carrier_h *carrier)
299 {
300         net_nfc_error_e result;
301
302         LOG_BEGIN();
303
304         CHECK_SUPPORTED(NFC_P2P_FEATURE);
305         CHECK_INIT();
306         CHECK_INVALID(message == NULL);
307         CHECK_INVALID(carrier == NULL);
308
309         result = net_nfc_get_handover_carrier(
310                 (net_nfc_ch_message_h)message,
311                 index,
312                 (net_nfc_ch_carrier_h *)carrier);
313
314         return nfc_common_convert_error_code(__func__, result);
315 }
316
317 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
318 {
319         net_nfc_error_e result;
320         net_nfc_conn_handover_carrier_type_e temp;
321
322         LOG_BEGIN();
323
324         CHECK_SUPPORTED(NFC_P2P_FEATURE);
325         CHECK_INIT();
326         CHECK_INVALID(message == NULL);
327         CHECK_INVALID(carrier == NULL);
328
329         switch (type) {
330         case NFC_AC_TYPE_BT :
331                 temp = NET_NFC_CONN_HANDOVER_CARRIER_BT;
332                 break;
333
334         case NFC_AC_TYPE_WIFI :
335                 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
336                 break;
337
338         case NFC_AC_TYPE_WIFI_DIRECT :
339                 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
340                 break;
341
342         default:
343                 temp = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
344                 break;
345         }
346         result = net_nfc_get_handover_carrier_by_type(
347                 (net_nfc_ch_message_h)message,
348                 temp,
349                 (net_nfc_ch_carrier_h *)carrier);
350
351         return nfc_common_convert_error_code(__func__, result);
352 }
353
354 int nfc_handover_message_destroy(nfc_handover_message_h message)
355 {
356         net_nfc_error_e result;
357
358         LOG_BEGIN();
359
360         CHECK_SUPPORTED(NFC_P2P_FEATURE);
361         CHECK_INIT();
362         CHECK_INVALID(message == NULL);
363
364         result = net_nfc_free_handover_message((net_nfc_ch_message_h)message);
365
366         return nfc_common_convert_error_code(__func__, result);
367 }
368
369
370 int nfc_handover_carrier_get_cps(nfc_handover_carrier_h carrier, nfc_ac_state_e *cps)
371 {
372         net_nfc_error_e result;
373         net_nfc_conn_handover_carrier_state_e temp;
374
375         LOG_BEGIN();
376
377         CHECK_SUPPORTED(NFC_P2P_FEATURE);
378         CHECK_INIT();
379         CHECK_INVALID(carrier == NULL);
380         CHECK_INVALID(cps == NULL);
381
382         result = net_nfc_get_handover_carrier_cps(
383                 (net_nfc_ch_carrier_h)carrier, &temp);
384         if (result == NET_NFC_OK) {
385                 switch (temp) {
386                 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE :
387                         *cps = NFC_AC_STATE_INACTIVATE;
388                         break;
389
390                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE :
391                         *cps = NFC_AC_STATE_ACTIVATE;
392                         break;
393
394                 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING :
395                         *cps = NFC_AC_STATE_ACTIVATING;
396                         break;
397
398                 default :
399                         *cps = NFC_AC_STATE_UNKNOWN;
400                         break;
401                 }
402         }
403
404         return nfc_common_convert_error_code(__func__, result);
405 }
406
407 int nfc_handover_carrier_get_type(nfc_handover_carrier_h carrier, nfc_ac_type_e *type)
408 {
409         net_nfc_error_e result;
410         net_nfc_conn_handover_carrier_type_e temp;
411
412         LOG_BEGIN();
413
414         CHECK_SUPPORTED(NFC_P2P_FEATURE);
415         CHECK_INIT();
416         CHECK_INVALID(carrier == NULL);
417         CHECK_INVALID(type == NULL);
418
419         result = net_nfc_get_handover_carrier_type(
420                 (net_nfc_ch_carrier_h)carrier, &temp);
421         if (result == NET_NFC_OK) {
422                 switch (temp) {
423                 case NET_NFC_CONN_HANDOVER_CARRIER_BT :
424                         *type = NFC_AC_TYPE_BT;
425                         break;
426
427                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS :
428                         *type = NFC_AC_TYPE_WIFI;
429                         break;
430
431                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P :
432                         *type = NFC_AC_TYPE_WIFI_DIRECT;
433                         break;
434
435                 default :
436                         *type = NFC_AC_TYPE_UNKNOWN;
437                         break;
438                 }
439         }
440
441         return nfc_common_convert_error_code(__func__, result);
442 }
443
444 int nfc_handover_carrier_get_carrier_record(nfc_handover_carrier_h carrier, nfc_ndef_record_h *record)
445 {
446         net_nfc_error_e result;
447
448         LOG_BEGIN();
449
450         CHECK_SUPPORTED(NFC_P2P_FEATURE);
451         CHECK_INIT();
452         CHECK_INVALID(carrier == NULL);
453         CHECK_INVALID(record == NULL);
454
455         result = net_nfc_get_handover_carrier_record(
456                 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
457
458         return nfc_common_convert_error_code(__func__, result);
459 }
460
461 int nfc_handover_carrier_get_auxiliary_record_count(nfc_handover_carrier_h carrier, unsigned int *count)
462 {
463         net_nfc_error_e result;
464
465         LOG_BEGIN();
466
467         CHECK_SUPPORTED(NFC_P2P_FEATURE);
468         CHECK_INIT();
469         CHECK_INVALID(carrier == NULL);
470         CHECK_INVALID(count == NULL);
471
472         result = net_nfc_get_handover_auxiliary_record_count(
473                 (net_nfc_ch_carrier_h)carrier, count);
474
475         return nfc_common_convert_error_code(__func__, result);
476 }
477
478 int nfc_handover_carrier_get_auxiliary_record(nfc_handover_carrier_h carrier, int index, nfc_ndef_record_h *record)
479 {
480         net_nfc_error_e result;
481
482         LOG_BEGIN();
483
484         CHECK_SUPPORTED(NFC_P2P_FEATURE);
485         CHECK_INIT();
486         CHECK_INVALID(carrier == NULL);
487         CHECK_INVALID(record == NULL);
488
489         result = net_nfc_get_handover_auxiliary_record(
490                 (net_nfc_ch_carrier_h)carrier,
491                 index, (ndef_record_h *)record);
492
493         return nfc_common_convert_error_code(__func__, result);
494 }
495
496 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
497 {
498         net_nfc_error_e result;
499
500         LOG_BEGIN();
501
502         CHECK_SUPPORTED(NFC_P2P_FEATURE);
503         CHECK_INIT();
504         CHECK_INVALID(carrier == NULL);
505         CHECK_INVALID(config == NULL);
506
507         result = net_nfc_create_carrier_config_from_carrier(
508                 (net_nfc_carrier_config_h *)config,
509                 (net_nfc_ch_carrier_h)carrier);
510
511         return nfc_common_convert_error_code(__func__, result);
512 }
513
514 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
515 {
516         net_nfc_error_e result;
517
518         LOG_BEGIN();
519
520         CHECK_SUPPORTED(NFC_P2P_FEATURE);
521         CHECK_INIT();
522         CHECK_INVALID(carrier == NULL);
523
524         result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
525
526         return nfc_common_convert_error_code(__func__, result);
527 }
528
529
530 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
531 {
532         net_nfc_error_e result;
533
534         LOG_BEGIN();
535
536         CHECK_SUPPORTED(NFC_P2P_FEATURE);
537         CHECK_INIT();
538         CHECK_INVALID(config == NULL);
539         CHECK_INVALID(size == NULL);
540         CHECK_INVALID(data == NULL);
541
542         result = net_nfc_get_carrier_config_property(
543                 (net_nfc_carrier_config_h)config,
544                 attribute, size, data);
545
546         return nfc_common_convert_error_code(__func__, result);
547 }
548
549 int nfc_handover_config_destroy(nfc_handover_config_h config)
550 {
551         net_nfc_error_e result;
552
553         LOG_BEGIN();
554
555         CHECK_SUPPORTED(NFC_P2P_FEATURE);
556         CHECK_INIT();
557         CHECK_INVALID(config == NULL);
558
559         result = net_nfc_free_carrier_config((net_nfc_carrier_config_h)config);
560
561         return nfc_common_convert_error_code(__func__, result);
562 }
563