Sync code with Tizen 3.0 branch
[platform/core/api/nfc.git] / src / nfc_p2p.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_send_cb(net_nfc_error_e result, void *user_data)
21 {
22         nfc_p2p_send_completed_cb callback;
23         void *user_param;
24
25         LOG_BEGIN();
26
27         if (user_data == NULL)
28                 return;
29
30         g_variant_get((GVariant *)user_data,
31                 "(tt)",
32                 &callback,
33                 &user_param);
34
35         if (callback != NULL)
36                 callback(nfc_common_convert_error_code(__func__, result), user_param);
37
38         g_variant_unref((GVariant *)user_data);
39 }
40 /* LCOV_EXCL_STOP */
41
42 int nfc_p2p_send(nfc_p2p_target_h target,
43         nfc_ndef_message_h message,
44         nfc_p2p_send_completed_cb callback,
45         void *user_data)
46 {
47         int ret;
48         data_h rawdata;
49         GVariant *parameter;
50
51         LOG_BEGIN();
52
53         CHECK_SUPPORTED(NFC_P2P_FEATURE);
54
55         /* LCOV_EXCL_START */
56         CHECK_INIT();
57         CHECK_INVALID(message == NULL);
58         CHECK_INVALID(target == NULL);
59         CHECK_ACTIVATED();
60         CHECK_APP_PERMISSION();
61
62         parameter = g_variant_new("(tt)",
63                 callback,
64                 user_data);
65         if (parameter != NULL) {
66                 net_nfc_create_rawdata_from_ndef_message(message, &rawdata);
67                 ret = net_nfc_client_p2p_send((net_nfc_target_handle_h)target,
68                         rawdata,
69                         _p2p_send_cb,
70                         parameter);
71                 if (ret != NET_NFC_OK)
72                         g_variant_unref(parameter);
73
74                 net_nfc_free_data(rawdata);
75         } else {
76                 ret = NET_NFC_ALLOC_FAIL;
77         }
78
79         return nfc_common_convert_error_code(__func__, ret);
80         /* LCOV_EXCL_STOP */
81 }
82
83 int nfc_p2p_send_no_permission(
84         nfc_p2p_target_h target,
85         nfc_ndef_message_h message,
86         nfc_p2p_send_completed_cb callback,
87         void *user_data)
88 {
89         int ret;
90         data_h rawdata;
91         GVariant *parameter;
92
93         LOG_BEGIN();
94
95         CHECK_SUPPORTED(NFC_P2P_FEATURE);
96
97         /* LCOV_EXCL_START */
98         CHECK_INIT();
99         CHECK_INVALID(target == NULL);
100         CHECK_INVALID(message == NULL);
101         CHECK_ACTIVATED();
102
103         /* skip check app permission */
104
105         parameter = g_variant_new("(tt)",
106                 callback,
107                 user_data);
108         if (parameter != NULL) {
109                 net_nfc_create_rawdata_from_ndef_message(message, &rawdata);
110                 ret = net_nfc_client_p2p_send((net_nfc_target_handle_h)target,
111                         rawdata,
112                         _p2p_send_cb,
113                         parameter);
114                 if (ret != NET_NFC_OK)
115                         g_variant_unref(parameter);
116
117                 net_nfc_free_data(rawdata);
118         } else {
119                 ret = NET_NFC_ALLOC_FAIL;
120         }
121
122         return nfc_common_convert_error_code(__func__, ret);
123         /* LCOV_EXCL_STOP */
124 }
125
126 /* LCOV_EXCL_START */
127 static void _p2p_set_data_received_cb(data_h data, void *user_data)
128 {
129         LOG_BEGIN();
130
131         if (gdbus_nfc_context.on_p2p_recv_cb != NULL) {
132                 ndef_message_h ndef_message;
133
134                 net_nfc_create_ndef_message_from_rawdata(&ndef_message, data);
135
136                 gdbus_nfc_context.on_p2p_recv_cb(
137                         (nfc_p2p_target_h)(gdbus_nfc_context.current_target),
138                         ndef_message,
139                         gdbus_nfc_context.on_p2p_recv_user_data);
140
141                 net_nfc_free_ndef_message(ndef_message);
142         }
143 }
144 /* LCOV_EXCL_STOP */
145
146 int nfc_p2p_set_data_received_cb(nfc_p2p_target_h target,
147         nfc_p2p_data_received_cb callback,
148         void *user_data)
149 {
150         LOG_BEGIN();
151
152         CHECK_SUPPORTED(NFC_P2P_FEATURE);
153
154         /* LCOV_EXCL_START */
155         CHECK_INIT();
156         CHECK_INVALID(target == NULL);
157         CHECK_INVALID(callback == NULL);
158         CHECK_INVALID(gdbus_nfc_context.current_target != target);
159
160         gdbus_nfc_context.on_p2p_recv_cb = callback;
161         gdbus_nfc_context.on_p2p_recv_user_data = user_data;
162
163         net_nfc_client_p2p_set_data_received(_p2p_set_data_received_cb, NULL);
164
165         return NFC_ERROR_NONE;
166         /* LCOV_EXCL_STOP */
167 }
168
169 int nfc_p2p_unset_data_received_cb(nfc_p2p_target_h target)
170 {
171         LOG_BEGIN();
172
173         CHECK_SUPPORTED(NFC_P2P_FEATURE);
174
175         /* LCOV_EXCL_START */
176         CHECK_INIT();
177         CHECK_INVALID(target == NULL);
178         CHECK_INVALID(gdbus_nfc_context.current_target != target);
179
180         net_nfc_client_p2p_unset_data_received();
181
182         gdbus_nfc_context.on_p2p_recv_cb = NULL;
183         gdbus_nfc_context.on_p2p_recv_user_data = NULL;
184
185         return NFC_ERROR_NONE;
186         /* LCOV_EXCL_STOP */
187 }
188
189 /* LCOV_EXCL_START */
190 static void _snep_start_server_cb(
191         net_nfc_snep_handle_h arg_handle,
192         net_nfc_snep_type_t event,
193         net_nfc_error_e result,
194         nfc_ndef_message_h msg,
195         void *user_data)
196 {
197         nfc_snep_event_cb callback;
198         void *user_param;
199
200         LOG_BEGIN();
201
202         if (user_data == NULL) {
203                 LOG_ERR("user_data is NULL");
204                 return;
205         }
206
207         g_variant_get((GVariant *)user_data,
208                 "(tt)",
209                 &callback,
210                 &user_param);
211
212         if (callback != NULL) {
213                 nfc_snep_event_e snep_event;
214
215                 switch ((int)event) {
216                 case NET_NFC_SNEP_GET:
217                         snep_event = NFC_SNEP_EVENT_GET;
218                         break;
219
220                 case NET_NFC_SNEP_PUT:
221                         snep_event = NFC_SNEP_EVENT_PUT;
222                         break;
223
224                 case NET_NFC_LLCP_REGISTERED:
225                         snep_event = NFC_SNEP_EVENT_REGISTER;
226                         break;
227
228                 case NET_NFC_LLCP_UNREGISTERED:
229                         snep_event = NFC_SNEP_EVENT_UNREGISTER;
230                         break;
231
232                 case NET_NFC_LLCP_START:
233                         snep_event = NFC_SNEP_EVENT_START;
234                         break;
235
236                 case NET_NFC_LLCP_STOP:
237                 default:
238                         snep_event = NFC_SNEP_EVENT_STOP;
239                         break;
240                 }
241
242                 callback(arg_handle, snep_event,
243                         nfc_common_convert_error_code(__func__, result),
244                         msg, user_param);
245
246                 if (snep_event == NFC_SNEP_EVENT_STOP)
247                         g_variant_unref(user_data);
248         }
249 }
250 /* LCOV_EXCL_STOP */
251
252 int nfc_snep_start_server(nfc_p2p_target_h target,
253         const char *san,
254         int sap,
255         nfc_snep_event_cb callback,
256         void *user_data)
257 {
258         net_nfc_error_e result;
259         GVariant *parameter;
260
261         LOG_BEGIN();
262
263         CHECK_SUPPORTED(NFC_P2P_FEATURE);
264
265         /* LCOV_EXCL_START */
266         CHECK_INIT();
267         CHECK_INVALID(target == NULL);
268
269         parameter = g_variant_new("(tt)",
270                 callback,
271                 user_data);
272         if (parameter != NULL) {
273                 result = net_nfc_client_snep_start_server(
274                         (net_nfc_target_handle_h)target,
275                         san,
276                         (sap_t)sap,
277                         _snep_start_server_cb,
278                         parameter);
279                 if (result != NET_NFC_OK)
280                         g_variant_unref(parameter);
281         } else {
282                 result = NET_NFC_ALLOC_FAIL;
283         }
284
285         return nfc_common_convert_error_code(__func__, result);
286         /* LCOV_EXCL_STOP */
287 }
288
289 /* LCOV_EXCL_START */
290 static void _snep_start_client_cb(net_nfc_snep_handle_h arg_handle,
291         net_nfc_snep_type_t event,
292         net_nfc_error_e result,
293         nfc_ndef_message_h msg,
294         void *user_data)
295 {
296         nfc_snep_event_cb callback;
297         void *user_param;
298
299         LOG_BEGIN();
300
301         if (user_data == NULL) {
302                 LOG_ERR("user_data is NULL");
303                 return;
304         }
305
306         g_variant_get((GVariant *)user_data,
307                 "(tt)",
308                 &callback,
309                 &user_param);
310
311         if (callback != NULL) {
312                 nfc_snep_event_e snep_event;
313
314                 switch ((int)event) {
315                 case NET_NFC_SNEP_GET:
316                         snep_event = NFC_SNEP_EVENT_GET;
317                         break;
318
319                 case NET_NFC_SNEP_PUT:
320                         snep_event = NFC_SNEP_EVENT_PUT;
321                         break;
322
323                 case NET_NFC_LLCP_REGISTERED:
324                         snep_event = NFC_SNEP_EVENT_REGISTER;
325                         break;
326
327                 case NET_NFC_LLCP_UNREGISTERED:
328                         snep_event = NFC_SNEP_EVENT_UNREGISTER;
329                         break;
330
331                 case NET_NFC_LLCP_START:
332                         snep_event = NFC_SNEP_EVENT_START;
333                         break;
334
335                 case NET_NFC_LLCP_STOP:
336                 default:
337                         snep_event = NFC_SNEP_EVENT_STOP;
338                         break;
339                 }
340
341                 callback(arg_handle, snep_event,
342                         nfc_common_convert_error_code(__func__, result),
343                         msg, user_param);
344
345                 if (snep_event == NFC_SNEP_EVENT_STOP)
346                         g_variant_unref(user_data);
347         }
348 }
349 /* LCOV_EXCL_STOP */
350
351 int nfc_snep_start_client(nfc_p2p_target_h target,
352         const char *san,
353         int sap,
354         nfc_snep_event_cb callback,
355         void *user_data)
356 {
357         net_nfc_error_e result;
358         GVariant *parameter;
359
360         LOG_BEGIN();
361
362         CHECK_SUPPORTED(NFC_P2P_FEATURE);
363
364         /* LCOV_EXCL_START */
365         CHECK_INIT();
366         CHECK_INVALID(target == NULL);
367
368         parameter = g_variant_new("(tt)",
369                 callback,
370                 user_data);
371         if (parameter != NULL) {
372                 result = net_nfc_client_snep_start_client(
373                         (net_nfc_target_handle_h)target,
374                         san,
375                         (sap_t)sap,
376                         _snep_start_client_cb,
377                         parameter);
378                 if (result != NET_NFC_OK)
379                         g_variant_unref(parameter);
380         } else {
381                 result = NET_NFC_ALLOC_FAIL;
382         }
383
384         return nfc_common_convert_error_code(__func__, result);
385         /* LCOV_EXCL_STOP */
386 }
387
388 /* LCOV_EXCL_START */
389 static void _snep_send_request_cb(net_nfc_snep_handle_h target,
390         net_nfc_snep_type_t event, net_nfc_error_e result, ndef_message_h msg, void *user_data)
391 {
392         nfc_snep_event_cb callback;
393         void *user_param;
394
395         LOG_BEGIN();
396
397         if (user_data == NULL) {
398                 LOG_ERR("user_data is NULL");
399                 return;
400         }
401
402         g_variant_get((GVariant *)user_data,
403                 "(tt)",
404                 &callback,
405                 &user_param);
406
407         if (callback != NULL) {
408                 callback((nfc_p2p_snep_h)target,
409                         event,
410                         nfc_common_convert_error_code(__func__, result),
411                         (nfc_ndef_message_h)msg,
412                         user_param);
413         }
414
415         g_variant_unref((GVariant *)user_data);
416 }
417 /* LCOV_EXCL_STOP */
418
419 int nfc_snep_send_client_request(nfc_p2p_snep_h handle,
420         nfc_snep_type_e type,
421         nfc_ndef_message_h msg,
422         nfc_snep_event_cb callback,
423         void *user_data)
424 {
425         net_nfc_error_e result;
426         GVariant *parameter;
427
428         LOG_BEGIN();
429
430         CHECK_SUPPORTED(NFC_P2P_FEATURE);
431
432         /* LCOV_EXCL_START */
433         CHECK_INIT();
434         CHECK_INVALID(handle == NULL);
435         CHECK_INVALID(msg == NULL);
436
437         parameter = g_variant_new("(tt)",
438                 callback,
439                 user_data);
440         if (parameter != NULL) {
441                 result = net_nfc_client_snep_send_client_request(
442                         (net_nfc_target_handle_h)handle,
443                         type,
444                         msg,
445                         _snep_send_request_cb,
446                         parameter);
447                 if (result != NET_NFC_OK)
448                         g_variant_unref(parameter);
449         } else {
450                 result = NET_NFC_ALLOC_FAIL;
451         }
452
453         return nfc_common_convert_error_code(__func__, result);
454         /* LCOV_EXCL_STOP */
455 }
456
457 /* LCOV_EXCL_START */
458 static void _snep_event_cb(net_nfc_snep_handle_h target, net_nfc_snep_type_t event,
459         net_nfc_error_e result, ndef_message_h msg, void *user_data)
460 {
461         nfc_snep_event_cb callback;
462         void *user_param;
463
464         LOG_BEGIN();
465
466         if (user_data == NULL) {
467                 LOG_ERR("user_data is NULL");
468                 return;
469         }
470
471         g_variant_get((GVariant *)user_data,
472                 "(tt)",
473                 &callback,
474                 &user_param);
475
476         if (callback != NULL) {
477                 switch ((int)event) {
478                 case NET_NFC_SNEP_GET:
479                         event = NFC_SNEP_EVENT_GET;
480                         break;
481
482                 case NET_NFC_SNEP_PUT:
483                         event = NFC_SNEP_EVENT_PUT;
484                         break;
485
486                 case NET_NFC_LLCP_REGISTERED:
487                         event = NFC_SNEP_EVENT_REGISTER;
488                         break;
489
490                 case NET_NFC_LLCP_UNREGISTERED:
491                         event = NFC_SNEP_EVENT_UNREGISTER;
492                         break;
493
494                 case NET_NFC_LLCP_START:
495                         event = NFC_SNEP_EVENT_START;
496                         break;
497
498                 case NET_NFC_LLCP_STOP:
499                         event = NFC_SNEP_EVENT_STOP;
500                         break;
501                 }
502
503                 callback(target, event,
504                         nfc_common_convert_error_code(__func__, result),
505                         (ndef_message_h)msg, user_param);
506         }
507 }
508 /* LCOV_EXCL_STOP */
509
510 int nfc_snep_register_server(const char *san, int sap,
511         nfc_snep_event_cb callback, void *user_data)
512 {
513         net_nfc_error_e result;
514         GVariant *parameter;
515
516         LOG_BEGIN();
517
518         CHECK_SUPPORTED(NFC_P2P_FEATURE);
519
520         /* LCOV_EXCL_START */
521         CHECK_INIT();
522         CHECK_INVALID(san == NULL);
523         CHECK_INVALID(sap == 0);
524
525         parameter = g_variant_new("(tt)",
526                 callback,
527                 user_data);
528         if (parameter != NULL) {
529                 result = net_nfc_client_snep_register_server(san, (sap_t)sap,
530                         _snep_event_cb, parameter);
531                 if (result != NET_NFC_OK)
532                         g_variant_unref(parameter);
533         } else {
534                 result = NET_NFC_ALLOC_FAIL;
535         }
536
537         return nfc_common_convert_error_code(__func__, result);
538         /* LCOV_EXCL_STOP */
539 }
540
541 int nfc_snep_unregister_server(const char *san, int sap)
542 {
543         net_nfc_error_e result;
544
545         LOG_BEGIN();
546
547         CHECK_SUPPORTED(NFC_P2P_FEATURE);
548
549         /* LCOV_EXCL_START */
550         CHECK_INIT();
551
552         result = net_nfc_client_snep_unregister_server(san, (sap_t)sap);
553
554         return nfc_common_convert_error_code(__func__, result);
555         /* LCOV_EXCL_STOP */
556 }
557
558 int nfc_snep_stop_service(nfc_p2p_target_h target, nfc_p2p_snep_h clients)
559 {
560         net_nfc_error_e result;
561
562         LOG_BEGIN();
563
564         CHECK_SUPPORTED(NFC_P2P_FEATURE);
565
566         /* LCOV_EXCL_START */
567         CHECK_INIT();
568
569         result = net_nfc_client_snep_stop_service_sync(target, clients);
570
571         return nfc_common_convert_error_code(__func__, result);
572         /* LCOV_EXCL_STOP */
573 }
574