Tizen 2.0 Release
[framework/api/nfc.git] / src / nfc.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 <unistd.h>
18
19 #include <net_nfc.h>
20 #include <net_nfc_typedef_private.h>
21 #include <dlog.h>
22 #include <nfc.h>
23 #include <nfc_private.h>
24 #include <net_nfc_exchanger.h>
25 #include <vconf.h>
26 #include <Ecore_X.h>
27 #include <app_manager.h>
28
29 /**
30  * @brief The default factory key.
31  * @details The key is 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
32  * @ingroup CAPI_NETWORK_NFC_TAG_MIFARE_MODULE
33  */
34 const unsigned char NFC_TAG_MIFARE_KEY_DEFAULT[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
35
36 /**
37  * @brief The well-known key for tags formatted according to the MIFARE Application Directory (MAD) specification.
38  * @details The key is 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5
39  * @ingroup CAPI_NETWORK_NFC_TAG_MIFARE_MODULE
40  */
41 const unsigned char NFC_TAG_MIFARE_KEY_APPLICATION_DIRECTORY[6] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
42
43 /**
44  * @brief The well-known key for tags formatted according to the NDEF on Mifare Classic specification.
45  * @details The key is 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7
46  * @ingroup CAPI_NETWORK_NFC_TAG_MIFARE_MODULE
47  */
48 const unsigned char NFC_TAG_MIFARE_KEY_NFC_FORUM[6] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7};
49
50
51 /**
52  * @brief RTD(Record type definition) Type - Smart Poster type.
53  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
54  */
55 const unsigned char NFC_RECORD_SMART_POSTER_TYPE[2] = { 'S', 'p' };
56
57 /**
58  * @brief  RTD(Record type definition) Type - Text type.
59  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
60  */
61 const unsigned char NFC_RECORD_TEXT_TYPE[1] = { 'T' };
62 /**
63  * @brief  RTD(Record type definition) Type - URI type.
64  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
65  */
66 const unsigned char NFC_RECORD_URI_TYPE[1] = { 'U' };
67 /**
68  * @brief  RTD(Record type definition) Type - Alternative Carrier type.
69  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
70  */
71 const unsigned char NFC_RECORD_ALTERNATIVE_CARRIER_TYPE[2] = { 'a','c' };
72 /**
73  * @brief  RTD(Record type definition) Type - Handover Carrier type.
74  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
75  */
76 const unsigned char NFC_RECORD_HANDOVER_CARRIER_TYPE[2] = { 'H','c' };
77 /**
78  * @brief  RTD(Record type definition) Type - Handover Request type.
79  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
80  */
81 const unsigned char NFC_RECORD_HANDOVER_REQUEST_TYPE[2] = { 'H','r' };
82 /**
83  * @brief  RTD(Record type definition) Type - Handover Select type.
84  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
85  */
86 const unsigned char NFC_RECORD_HANDOVER_SELECT_TYPE[2] = { 'H','s' };
87
88
89 #ifdef LOG_TAG
90 #undef LOG_TAG
91 #endif
92 #define LOG_TAG "CAPI_NETWORK_NFC"
93
94 static pid_t _get_focus_app_pid()
95 {
96         Ecore_X_Window focus;
97         pid_t pid;
98
99         ecore_x_init(NULL);
100
101         focus = ecore_x_window_focus_get();
102         if (ecore_x_netwm_pid_get(focus, &pid))
103                 return pid;
104
105         return -1;
106 }
107
108 static pid_t _get_current_app_pid()
109 {
110         char *app_id = NULL;
111         app_context_h context = NULL;
112         pid_t pid, pgid;
113
114         pid = getpid();
115         app_manager_get_app_id(pid, &app_id);
116         app_manager_get_app_context(app_id, &context);
117
118         app_context_get_pid(context, &pgid);
119
120         free(app_id);
121         app_context_destroy(context);
122
123         return pgid;
124 }
125
126 static bool _check_app_permission()
127 {
128         pid_t focus_app_pid, current_app_pid;
129
130         focus_app_pid = _get_focus_app_pid();
131         current_app_pid = _get_current_app_pid();
132
133         LOGE( "[check app permission] focus_app_pid [%d],  current_app_pid [%d]", focus_app_pid, current_app_pid);
134
135         return (focus_app_pid == current_app_pid) ? true : false;
136 }
137
138 static int _return_invalid_param(const char *func){
139         LOGE( "INVALID_PARAMETER (0x%08x)", NFC_ERROR_INVALID_PARAMETER);
140         return NFC_ERROR_INVALID_PARAMETER;
141 }
142
143 static int _convert_error_code(const char *func, int native_error_code)
144 {
145         int error_code = NFC_ERROR_NONE;
146         char * errorstr = NULL;
147         switch(native_error_code){
148                 case 0 :
149                         error_code = NFC_ERROR_NONE;
150                         errorstr  = "ERROR_NONE";
151                         break;
152
153                 case NET_NFC_ALLOC_FAIL:
154                         error_code = NFC_ERROR_OUT_OF_MEMORY;
155                         errorstr  = "OUT_OF_MEMORY";
156                         break;
157
158                 case NET_NFC_UNKNOWN_ERROR:
159                 case NET_NFC_THREAD_CREATE_FAIL:
160                 case NET_NFC_INVALID_STATE:
161                 case NET_NFC_IPC_FAIL:
162                 case NET_NFC_BUFFER_TOO_SMALL:
163                 case NET_NFC_COMMUNICATE_WITH_CONTROLLER_FAILED:
164                 case NET_NFC_RF_ERROR:
165                 case NET_NFC_NOT_SUPPORTED:
166                 case NET_NFC_TAG_READ_FAILED:
167                 case NET_NFC_TAG_WRITE_FAILED:
168                 case NET_NFC_OPERATION_FAIL:
169                 case NET_NFC_SECURITY_FAIL:
170                 case NET_NFC_INSUFFICIENT_STORAGE:
171                 case NET_NFC_NOT_CONNECTED:
172                 case NET_NFC_NOT_INITIALIZED:
173                 case NET_NFC_NOT_REGISTERED:
174                         error_code = NFC_ERROR_OPERATION_FAILED;
175                         errorstr  = "OPERATION_FAILED";
176                         break;
177
178                 case NET_NFC_OUT_OF_BOUND:
179                 case NET_NFC_NULL_PARAMETER:
180                 case NET_NFC_NOT_ALLOWED_OPERATION:
181                 case NET_NFC_LLCP_INVALID_SOCKET:
182                 case NET_NFC_NO_DATA_FOUND:
183                         error_code = NFC_ERROR_INVALID_PARAMETER;
184                         errorstr  = "INVALID_PARAMETER";
185                         break;
186                 case NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE:
187                         error_code = NFC_ERROR_INVALID_RECORD_TYPE;
188                         errorstr  = "INVALID_RECORD_TYPE";
189                         break;
190
191                 case NET_NFC_ALREADY_INITIALIZED:
192                 case NET_NFC_ALREADY_REGISTERED:
193                         error_code = NFC_ERROR_NONE;
194                         errorstr  = "ERROR_NONE";
195                         break;
196
197                 case NET_NFC_RF_TIMEOUT:
198                         error_code = NFC_ERROR_TIMED_OUT;
199                         errorstr  = "TIMED_OUT";
200                         break;
201                 case NET_NFC_INVALID_FORMAT:
202                 case NET_NFC_NDEF_TYPE_LENGTH_IS_NOT_OK:
203                 case NET_NFC_NDEF_ID_LENGTH_IS_NOT_OK:
204                 case NET_NFC_NDEF_BUF_END_WITHOUT_ME:
205                         error_code = NFC_ERROR_INVALID_NDEF_MESSAGE;
206                         errorstr  = "INVALID_NDEF_MESSAGE";
207                         break;
208                 case NET_NFC_NO_NDEF_MESSAGE:
209                         error_code = NFC_ERROR_NO_NDEF_MESSAGE;
210                         errorstr  = "NO_NDEF_MESSAGE";
211                         break;
212                 case NET_NFC_BUSY :
213                         error_code = NFC_ERROR_DEVICE_BUSY;
214                         errorstr  = "DEVICE_BUSY";
215                         break;
216                 case NET_NFC_NO_NDEF_SUPPORT:
217                         error_code = NFC_ERROR_NOT_NDEF_FORMAT;
218                         errorstr  = "NOT_SUPPORTED";
219                         break;
220                 default :
221                         error_code = NFC_ERROR_OPERATION_FAILED;
222                         errorstr  = "OPERATION_FAILED";
223         }
224
225         LOGE( "NFC %s(0x%08x)", errorstr, error_code);
226
227         return error_code;
228
229 }
230
231 _nfc_context_s g_nfc_context;
232
233
234 static void nfc_manager_set_activation_completed_cb(nfc_activation_completed_cb callback , void *user_data)
235 {
236         g_nfc_context.on_activation_completed_cb = callback;
237         g_nfc_context.on_activation_completed_user_data = user_data;
238         g_nfc_context.on_activation_doing = true;
239 }
240
241 static void nfc_manager_unset_activation_completed_cb(void)
242 {
243         g_nfc_context.on_activation_completed_cb = NULL;
244         g_nfc_context.on_activation_completed_user_data = NULL;
245         g_nfc_context.on_activation_doing = false;
246 }
247
248 static bool nfc_manager_check_activation(void)
249 {
250         return g_nfc_context.on_activation_doing;
251 }
252
253
254 void _nfc_response_handler(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
255 {
256         LOGI("NFC message %d - start result[%d] ", message, result);
257
258         int capi_result = _convert_error_code("EVENT", result);
259
260         switch ( message ){
261                 case NET_NFC_MESSAGE_TRANSCEIVE:
262                 {
263                         if( trans_data != NULL ){
264                                 _async_callback_data *user_cb = (_async_callback_data*)trans_data;
265
266                                 if( user_cb->callback_type == _NFC_CALLBACK_TYPE_DATA ){
267                                         unsigned char * buffer = NULL;
268                                         int buffer_size = 0;
269                                         if( result == 0 && data != NULL){
270                                                 data_s *arg = (data_s*) data;
271                                                 buffer = arg->buffer;
272                                                 buffer_size = arg->length;
273                                         }
274                                         void (* data_type_callback)(int result , unsigned char * buffer, int buffer_size,  void * user_data);
275                                         data_type_callback = user_cb->callback;
276                                         data_type_callback(capi_result, buffer, buffer_size, user_cb->user_data);
277                                 }else if ( user_cb->callback_type == _NFC_CALLBACK_TYPE_RESULT){
278                                         void (* result_type_callback)(int result , void * user_data);
279                                         result_type_callback = user_cb->callback;
280                                         result_type_callback(capi_result, user_cb->user_data);
281                                 }
282                                 free(user_cb);
283                         }
284                         break;
285                 }
286                 case NET_NFC_MESSAGE_READ_NDEF:
287                 {
288                         if( trans_data != NULL ) {
289                                 ndef_message_h ndef_message = (ndef_message_h)data;
290                                 _async_callback_data *user_cb = (_async_callback_data*)trans_data;
291                                 ((nfc_tag_read_completed_cb)user_cb->callback)(capi_result, ndef_message, user_cb->user_data);
292                                 free(user_cb);
293                         }
294                         break;
295                 }
296                 case NET_NFC_MESSAGE_WRITE_NDEF:
297                 {
298                         if( trans_data != NULL ){
299                                 _async_callback_data *user_cb = (_async_callback_data*)trans_data;
300                                 ((nfc_tag_write_completed_cb)user_cb->callback)(capi_result, user_cb->user_data);
301                                 free(user_cb);
302                         }
303                         break;
304                 }
305                 case NET_NFC_MESSAGE_TAG_DISCOVERED:
306                 {
307                         net_nfc_target_info_s *target_info = (net_nfc_target_info_s *)data;
308
309                         if (g_nfc_context.current_tag != NULL)
310                         {
311                                 net_nfc_release_tag_info((net_nfc_target_info_h)g_nfc_context.current_tag);
312                                 g_nfc_context.current_tag = NULL;
313                         }
314
315                         net_nfc_duplicate_target_info((net_nfc_target_info_h)target_info, (net_nfc_target_info_h *)&g_nfc_context.current_tag);
316
317                         if (g_nfc_context.on_tag_discovered_cb)
318                         {
319                                 g_nfc_context.on_tag_discovered_cb(NFC_DISCOVERED_TYPE_ATTACHED, (nfc_tag_h)g_nfc_context.current_tag, g_nfc_context.on_tag_discovered_user_data);
320                         }
321
322                         //ndef discovered cb
323                         if (g_nfc_context.on_ndef_discovered_cb && target_info->raw_data.buffer != NULL)
324                         {
325                                 ndef_message_h ndef_message;
326                                 net_nfc_create_ndef_message_from_rawdata(&ndef_message, (data_h)&(target_info->raw_data));
327                                 g_nfc_context.on_ndef_discovered_cb(ndef_message, g_nfc_context.on_ndef_discovered_user_data);
328                                 net_nfc_free_ndef_message(ndef_message);
329                         }
330                         break;
331                 }
332                 case NET_NFC_MESSAGE_NOTIFY:
333                 {
334                         break;
335                 }
336                 case NET_NFC_MESSAGE_TAG_DETACHED:
337                 {
338                         if( g_nfc_context.on_tag_discovered_cb ){
339                                 g_nfc_context.on_tag_discovered_cb( NFC_DISCOVERED_TYPE_DETACHED,  (nfc_tag_h)g_nfc_context.current_tag , g_nfc_context.on_tag_discovered_user_data );
340                         }
341
342                         if (g_nfc_context.current_tag != NULL)
343                         {
344                                 net_nfc_release_tag_info((net_nfc_target_info_h)g_nfc_context.current_tag);
345                                 g_nfc_context.current_tag = NULL;
346                         }
347                         break;
348                 }
349                 case NET_NFC_MESSAGE_P2P_DISCOVERED:
350                 {
351                         g_nfc_context.current_target = (net_nfc_target_handle_h)data;
352                         g_nfc_context.on_p2p_recv_cb = NULL;
353                         g_nfc_context.on_p2p_recv_user_data = NULL;
354                         g_nfc_context.on_p2p_send_completed_cb = NULL;
355                         g_nfc_context.on_p2p_send_completed_user_data = NULL;
356
357                         if( g_nfc_context.on_p2p_discovered_cb ){
358                                 g_nfc_context.on_p2p_discovered_cb(NFC_DISCOVERED_TYPE_ATTACHED , (nfc_p2p_target_h)g_nfc_context.current_target, g_nfc_context.on_p2p_discovered_user_data );
359                         }
360
361                         break;
362                 }
363                 case NET_NFC_MESSAGE_P2P_DETACHED:
364                 {
365                         if( g_nfc_context.on_p2p_discovered_cb ){
366                                 g_nfc_context.on_p2p_discovered_cb( NFC_DISCOVERED_TYPE_DETACHED,  (nfc_p2p_target_h)(g_nfc_context.current_target) , g_nfc_context.on_p2p_discovered_user_data );
367                         }
368                         memset(&g_nfc_context.current_target , 0 , sizeof( g_nfc_context.current_target ));
369                         g_nfc_context.on_p2p_recv_cb = NULL;
370                         g_nfc_context.on_p2p_recv_user_data = NULL;
371                         g_nfc_context.on_p2p_send_completed_cb = NULL;
372                         g_nfc_context.on_p2p_send_completed_user_data = NULL;
373
374                         break;
375                 }
376                 case NET_NFC_MESSAGE_P2P_SEND :
377                 {
378                         if( g_nfc_context.on_p2p_send_completed_cb != NULL ){
379
380                                 nfc_p2p_send_completed_cb       cb = g_nfc_context.on_p2p_send_completed_cb;
381                                 void *                                          user_data = g_nfc_context.on_p2p_send_completed_user_data;
382                                 g_nfc_context.on_p2p_send_completed_cb = NULL;
383                                 g_nfc_context.on_p2p_send_completed_user_data = NULL;
384                                 cb(capi_result , user_data );
385                         }
386                         break;
387                 }
388                 case NET_NFC_MESSAGE_P2P_RECEIVE :
389                 {
390                         if( g_nfc_context.on_p2p_recv_cb != NULL ){
391                                 ndef_message_h ndef_message ;
392                                 net_nfc_create_ndef_message_from_rawdata (&ndef_message, (data_h)(data) );
393                                 g_nfc_context.on_p2p_recv_cb( (nfc_p2p_target_h)(g_nfc_context.current_target) , ndef_message ,g_nfc_context.on_p2p_recv_user_data );
394                                 net_nfc_free_ndef_message(ndef_message);
395                         }
396
397                         break;
398                 }
399                 case NET_NFC_MESSAGE_FORMAT_NDEF:
400                 {
401                         if( trans_data != NULL) {
402                                 _async_callback_data *user_cb = (_async_callback_data*)trans_data;
403                                 ((nfc_tag_format_completed_cb)user_cb->callback)(capi_result, user_cb->user_data);
404                                 free(user_cb);
405                         }
406                         break;
407                 }
408
409                 case NET_NFC_MESSAGE_CONNECTION_HANDOVER :
410                 {
411                         if( g_nfc_context.on_p2p_connection_handover_completed_cb != NULL ){
412
413                                 net_nfc_conn_handover_carrier_type_e type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
414                                 nfc_ac_type_e carrior_type = NFC_AC_TYPE_UNKNOWN;
415                                 char * ac_data = NULL;
416                                 int ac_data_size = 0;
417                                 char * temp = NULL;
418                                 char buffer[50] = {0,};
419                                 data_h ac_info = NULL;
420
421
422                                 net_nfc_exchanger_get_alternative_carrier_type((net_nfc_connection_handover_info_h)data, &type);
423                                 if (type == NET_NFC_CONN_HANDOVER_CARRIER_BT)
424                                 {
425                                         carrior_type = NFC_AC_TYPE_BT;
426                                         if(net_nfc_exchanger_get_alternative_carrier_data((net_nfc_connection_handover_info_h)data, &ac_info)== 0)
427                                         {
428                                                 temp = (char *)net_nfc_get_data_buffer(ac_info);
429                                                 if( temp != NULL)
430                                                 {
431
432                                                         snprintf(buffer, 50, "%02x:%02x:%02x:%02x:%02x:%02x",temp[0], temp[1], temp[2], temp[3], temp[4], temp[5]);
433
434                                                          ac_data = (strdup(buffer));
435                                                          ac_data_size = strlen(ac_data ) +1;
436                                                 }
437                                                 net_nfc_free_data(ac_info);
438                                         }
439                                 }
440
441                                 nfc_p2p_connection_handover_completed_cb        cb = g_nfc_context.on_p2p_connection_handover_completed_cb;
442                                 void *                                                                          user_data = g_nfc_context.on_p2p_connection_handover_completed_user_data;
443                                 g_nfc_context.on_p2p_connection_handover_completed_cb = NULL;
444                                 g_nfc_context.on_p2p_connection_handover_completed_user_data = NULL;
445                                 cb(capi_result , carrior_type, (void *)ac_data, ac_data_size, user_data );
446
447                                 net_nfc_exchanger_free_alternative_carrier_data((net_nfc_connection_handover_info_h)data);
448                                 free(ac_data);
449                         }
450                         break;
451                 }
452
453
454                 case NET_NFC_MESSAGE_IS_TAG_CONNECTED :
455                 {
456                         net_nfc_target_type_e  devType = *(net_nfc_target_type_e *)data;
457
458                         if( (devType == NET_NFC_NFCIP1_TARGET )||(devType == NET_NFC_NFCIP1_INITIATOR ))
459                         {
460                                 net_nfc_get_current_target_handle(trans_data);
461                         }
462                         else if( (devType > NET_NFC_UNKNOWN_TARGET )&&(devType < NET_NFC_NFCIP1_TARGET ))
463                         {
464                                 net_nfc_get_current_tag_info(trans_data);
465                         }
466                         else
467                         {
468                                 if (result == NET_NFC_NOT_CONNECTED)
469                                 {
470                                         capi_result = NFC_ERROR_NONE;
471                                 }
472
473                                 if( g_nfc_context.on_initialize_completed_cb ){
474                                         nfc_initialize_completed_cb             cb = g_nfc_context.on_initialize_completed_cb;
475                                         g_nfc_context.on_initialize_completed_cb = NULL;
476                                         cb( capi_result,trans_data );
477                                 }
478                         }
479                         break;
480                 }
481
482                 case NET_NFC_MESSAGE_GET_CURRENT_TAG_INFO :
483                 {
484                         net_nfc_target_info_s *target_info = (net_nfc_target_info_s *)data;
485
486                         if (target_info != NULL)
487                         {
488                                 if (g_nfc_context.current_tag != NULL)
489                                 {
490                                         net_nfc_release_tag_info((net_nfc_target_info_h)g_nfc_context.current_tag);
491                                         g_nfc_context.current_tag = NULL;
492                                 }
493
494                                 net_nfc_duplicate_target_info((net_nfc_target_info_h)target_info, (net_nfc_target_info_h *)&g_nfc_context.current_tag);
495                         }
496
497                         nfc_initialize_completed_cb cb = g_nfc_context.on_initialize_completed_cb;
498                         g_nfc_context.on_initialize_completed_cb = NULL;
499                         if (cb)
500                         {
501                                 cb(capi_result, trans_data);
502                         }
503                         break;
504                 }
505
506                 case NET_NFC_MESSAGE_GET_CURRENT_TARGET_HANDLE :
507                 {
508                         g_nfc_context.current_target = (net_nfc_target_handle_h)data;
509
510                         nfc_initialize_completed_cb             cb = g_nfc_context.on_initialize_completed_cb;
511                         g_nfc_context.on_initialize_completed_cb = NULL;
512                         if( cb ){
513                                 cb( capi_result, trans_data );
514                         }
515
516                         break;
517                 }
518
519
520                 case NET_NFC_MESSAGE_INIT :
521                 {
522                         bool activated = true;
523
524                         LOGE( "_nfc_response_handler NET_NFC_MESSAGE_INIT\n");
525
526                         if (result == NET_NFC_OK){
527                                 if( g_nfc_context.on_activation_changed_cb != NULL ){
528                                         g_nfc_context.on_activation_changed_cb(activated , g_nfc_context.on_activation_changed_user_data);
529                                         LOGE( "_nfc_response_handler changed call back NET_NFC_MESSAGE_INIT NET_NFC_OK\n");
530                                 }
531
532                                 if( g_nfc_context.on_activation_completed_cb != NULL ){
533                                         g_nfc_context.on_activation_completed_cb(result , g_nfc_context.on_activation_completed_user_data);
534                                         LOGE( "_nfc_response_handler completed call back NET_NFC_MESSAGE_INIT NET_NFC_OK\n");
535
536                                         nfc_manager_unset_activation_completed_cb();
537                                 }
538                                 else
539                                 {
540                                         g_nfc_context.on_activation_doing = false;
541                                         LOGE( "g_nfc_context.on_activation_doing\n");
542                                 }
543
544
545                         }
546
547                         break;
548                 }
549
550                 case NET_NFC_MESSAGE_DEINIT :
551                 {
552                         bool activated = false;
553
554                         if (result == NET_NFC_OK){
555                                 if( g_nfc_context.on_activation_changed_cb != NULL ){
556                                         g_nfc_context.on_activation_changed_cb(activated , g_nfc_context.on_activation_changed_user_data);
557                                         LOGE( "_nfc_response_handler NET_NFC_MESSAGE_DEINIT NET_NFC_OK\n");
558                                 }
559
560                                 if( g_nfc_context.on_activation_completed_cb != NULL ){
561                                         g_nfc_context.on_activation_completed_cb(result , g_nfc_context.on_activation_completed_user_data);
562                                         LOGE( "_nfc_response_handler completed call back NET_NFC_MESSAGE_INIT NET_NFC_OK\n");
563
564                                         nfc_manager_unset_activation_completed_cb();
565                                 }
566                                 else
567                                 {
568                                         g_nfc_context.on_activation_doing = false;
569                                         LOGE( "g_nfc_context.on_activation_doing\n");
570                                 }
571                         }
572
573                         break;
574                 }
575
576                 case NET_NFC_MESSAGE_SET_SE :
577                 {
578                         if( trans_data != NULL ){
579                                 _async_callback_data *user_cb = (_async_callback_data*)trans_data;
580                                 ((nfc_set_card_emulation_completed_cb)user_cb->callback)(capi_result, user_cb->user_data);
581                                 free(user_cb);
582                         }
583                         break;
584                 }
585
586                 case NET_NFC_MESSAGE_SE_START_TRANSACTION :
587                 case NET_NFC_MESSAGE_SE_END_TRANSACTION :
588                 case NET_NFC_MESSAGE_SE_TYPE_TRANSACTION:
589                 case NET_NFC_MESSAGE_SE_CONNECTIVITY :
590                 case NET_NFC_MESSAGE_SE_FIELD_ON :
591                 case NET_NFC_MESSAGE_SE_FIELD_OFF :
592                 case NET_NFC_MESSAGE_SE_TYPE_CHANGED :
593                 {
594                         nfc_se_event_e event = NFC_SE_EVENT_START_TRANSACTION;
595                         switch( message ){
596                                 case NET_NFC_MESSAGE_SE_START_TRANSACTION:
597                                         event = NFC_SE_EVENT_START_TRANSACTION;
598                                         break;
599                                 case NET_NFC_MESSAGE_SE_END_TRANSACTION:
600                                         event = NFC_SE_EVENT_END_TRANSACTION;
601                                         break;
602                                 case NET_NFC_MESSAGE_SE_CONNECTIVITY:
603                                         event = NFC_SE_EVENT_CONNECTIVITY;
604                                         break;
605                                 case NET_NFC_MESSAGE_SE_FIELD_ON :
606                                         event = NFC_SE_EVENT_FIELD_ON;
607                                         break;
608                                 case NET_NFC_MESSAGE_SE_FIELD_OFF :
609                                         event = NFC_SE_EVENT_FIELD_OFF;
610                                         break;
611                                 case NET_NFC_MESSAGE_SE_TYPE_TRANSACTION:
612                                         event = NFC_SE_EVENT_TRANSACTION;
613                                         break;
614                                 case NET_NFC_MESSAGE_SE_TYPE_CHANGED:
615                                         event = NFC_SE_EVENT_SE_TYPE_CHANGED;
616                                         break;
617                                 default:
618                                         break;
619                         }
620
621                         if( g_nfc_context.on_se_event_cb ){
622                                 g_nfc_context.on_se_event_cb(event, g_nfc_context.on_se_event_user_data);
623                         }
624                         if( message == NET_NFC_MESSAGE_SE_TYPE_TRANSACTION){
625                                 net_nfc_se_event_info_s* transaction_data = (net_nfc_se_event_info_s*)data;
626                                 if( g_nfc_context.on_se_transaction_event_cb && transaction_data != NULL){
627                                         g_nfc_context.on_se_transaction_event_cb(transaction_data->aid.buffer,transaction_data->aid.length, transaction_data->param.buffer,transaction_data->param.length  , g_nfc_context.on_se_transaction_event_user_data);
628                                 }
629                         }
630                 }
631
632                 default :
633                         break;
634         }
635
636 }
637
638
639 bool nfc_manager_is_supported(void)
640 {
641
642         int nfc_supported = 0;
643
644         net_nfc_is_supported(&nfc_supported);
645
646         return nfc_supported;
647
648 }
649
650
651 int nfc_manager_set_activation(bool activation, nfc_activation_completed_cb callback, void *user_data)
652 {
653         int ret = 0;
654         int nfc_supported = 0;
655         bool nfc_check_activation = 0;
656
657         nfc_check_activation = nfc_manager_check_activation();
658
659         if (nfc_check_activation == true)
660         {
661                 LOGE( "nfc_manager_check_activation BUSY!!!!!\n");
662                 return NFC_ERROR_DEVICE_BUSY;
663         }
664
665         net_nfc_is_supported(&nfc_supported);
666
667         if (!nfc_supported)
668         {
669                 ret = NFC_ERROR_NOT_SUPPORTED;
670         }
671         else
672         {
673                 if (nfc_manager_is_activated() == activation)
674                 {
675                         if (activation)
676                         {
677                                 ret = NFC_ERROR_ALREADY_ACTIVATED;
678                         }
679                         else
680                         {
681                                 ret = NFC_ERROR_ALREADY_DEACTIVATED;
682                         }
683                 }
684                 else
685                 {
686                         nfc_manager_set_activation_completed_cb(callback, user_data);
687
688                         ret = net_nfc_set_state(activation, NULL);
689
690                         if (ret == NET_NFC_OK)
691                         {
692                                 ret = NFC_ERROR_NONE;
693                                 LOGE( "nfc_manager_set_activation net_nfc_set_state success\n");
694                         }
695                         else
696                         {
697                                 nfc_manager_unset_activation_completed_cb();
698                                 ret = NFC_ERROR_OPERATION_FAILED;
699                                 LOGE( "nfc_manager_set_activation net_nfc_set_state fail\n");
700                         }
701                 }
702         }
703         return ret;
704
705 }
706
707 int nfc_manager_set_activation_changed_cb(nfc_activation_changed_cb callback , void *user_data)
708 {
709         if( callback == NULL)
710                 return _return_invalid_param(__func__);
711         g_nfc_context.on_activation_changed_cb = callback;
712         g_nfc_context.on_activation_changed_user_data = user_data;
713         return 0;
714 }
715
716 void nfc_manager_unset_activation_changed_cb(void)
717 {
718         g_nfc_context.on_activation_changed_cb = NULL;
719         g_nfc_context.on_activation_changed_user_data = NULL;
720 }
721
722
723
724
725 bool nfc_manager_is_activated(void)
726 {
727         int activated = 0;
728
729         net_nfc_get_state(&activated);
730
731         if(activated)
732         {
733                 return true;
734         }
735         else
736         {
737                 return false;
738         }
739
740 }
741
742 static bool _is_initialized()
743 {
744         return g_nfc_context.initialized;
745 }
746
747 int nfc_manager_initialize (nfc_initialize_completed_cb callback, void *user_data)
748 {
749         int ret;
750         int nfc_supported = 0;
751
752         net_nfc_is_supported(&nfc_supported);
753
754         if(!nfc_supported)
755         {
756                 LOGE( "nfc_manager_initialize  NFC_ERROR_NOT_SUPPORTED \n");
757                 return  NFC_ERROR_NOT_SUPPORTED;
758         }
759
760         if (!_is_initialized())
761         {
762                 ret = net_nfc_initialize();
763                 if( ret != NET_NFC_OK )
764                         return _convert_error_code(__func__, ret);
765
766                 memset( &g_nfc_context , 0 , sizeof( g_nfc_context));
767                 net_nfc_set_response_callback( _nfc_response_handler , &g_nfc_context);
768                 g_nfc_context.initialized = true;
769                 net_nfc_state_activate();
770                 g_nfc_context.on_initialize_completed_cb = callback;
771                 ret = net_nfc_is_tag_connected(user_data);
772                 if( ret != NET_NFC_OK )
773                         return _convert_error_code(__func__, ret);
774         }
775
776         return NFC_ERROR_NONE;
777 }
778
779 int nfc_manager_initialize_sync()
780 {
781         int ret;
782         int nfc_supported = 0;
783
784         net_nfc_is_supported(&nfc_supported);
785         if (!nfc_supported)
786         {
787                 LOGE("nfc_manager_initialize  NFC_ERROR_NOT_SUPPORTED \n");
788                 return NFC_ERROR_NOT_SUPPORTED;
789         }
790
791         if (!_is_initialized())
792         {
793                 int devType;
794
795                 ret = net_nfc_initialize();
796                 if (ret != NET_NFC_OK)
797                         return _convert_error_code(__func__, ret);
798
799                 memset(&g_nfc_context, 0, sizeof(g_nfc_context));
800                 net_nfc_set_response_callback(_nfc_response_handler, &g_nfc_context);
801                 g_nfc_context.initialized = true;
802                 net_nfc_state_activate();
803
804                 ret = net_nfc_is_tag_connected_sync(&devType);
805                 if (ret == NET_NFC_OK)
806                 {
807                         if ((devType == NET_NFC_NFCIP1_TARGET) || (devType == NET_NFC_NFCIP1_INITIATOR))
808                         {
809                                 net_nfc_get_current_target_handle_sync((net_nfc_target_handle_h *)&g_nfc_context.current_target);
810                         }
811                         else if ((devType > NET_NFC_UNKNOWN_TARGET) && (devType < NET_NFC_NFCIP1_TARGET))
812                         {
813                                 if (g_nfc_context.current_tag != NULL)
814                                 {
815                                         net_nfc_release_tag_info((net_nfc_target_info_h)g_nfc_context.current_tag);
816                                         g_nfc_context.current_tag = NULL;
817                                 }
818
819                                 net_nfc_get_current_tag_info_sync((net_nfc_target_info_h *)&g_nfc_context.current_tag);
820                         }
821                 }
822                 else
823                 {
824                         if (ret == NET_NFC_NOT_CONNECTED)
825                         {
826                                 ret = NFC_ERROR_NONE;
827                         }
828                 }
829
830                 if (ret != NET_NFC_OK)
831                         return _convert_error_code(__func__, ret);
832         }
833
834         return NFC_ERROR_NONE;
835 }
836
837 int nfc_manager_deinitialize (void)
838 {
839         int ret = NFC_ERROR_NONE;
840
841         if (_is_initialized())
842         {
843                 net_nfc_state_deactivate();
844
845                 ret = net_nfc_deinitialize();
846
847                 if (ret == 0)
848                         net_nfc_unset_response_callback();
849
850                 g_nfc_context.initialized = false;
851         }
852
853         return _convert_error_code(__func__, ret);
854 }
855
856 int nfc_manager_set_tag_discovered_cb( nfc_tag_discovered_cb callback , void * user_data)
857 {
858         if( callback == NULL)
859                 return _return_invalid_param(__func__);
860         g_nfc_context.on_tag_discovered_cb = callback;
861         g_nfc_context.on_tag_discovered_user_data = user_data;
862         return 0;
863 }
864 void nfc_manager_unset_tag_discovered_cb( void )
865 {
866         g_nfc_context.on_tag_discovered_cb = NULL;
867         g_nfc_context.on_tag_discovered_user_data = NULL;
868 }
869
870 int nfc_manager_set_ndef_discovered_cb( nfc_ndef_discovered_cb callback , void *user_data)
871 {
872         if( callback == NULL)
873                 return _return_invalid_param(__func__);
874         g_nfc_context.on_ndef_discovered_cb= callback;
875         g_nfc_context.on_ndef_discovered_user_data= user_data;
876         return 0;
877 }
878
879 void nfc_manager_unset_ndef_discovered_cb( void )
880 {
881
882         g_nfc_context.on_ndef_discovered_cb = NULL;
883         g_nfc_context.on_ndef_discovered_user_data = NULL;
884 }
885
886 void nfc_manager_set_tag_filter(int filter )
887 {
888         net_nfc_set_tag_filter(filter);
889 }
890
891 int nfc_manager_get_tag_filter(void)
892 {
893
894         return net_nfc_get_tag_filter();
895 }
896
897 int nfc_manager_get_connected_tag(nfc_tag_h * tag)
898 {
899         int ret;
900         if( tag == NULL )
901                 return _return_invalid_param(__func__);
902
903         if(g_nfc_context.current_tag == NULL)
904         {
905                 return NFC_ERROR_NO_DEVICE;
906         }
907
908         if(g_nfc_context.current_tag->handle == NULL)
909         {
910                 ret = NFC_ERROR_NO_DEVICE;
911         }
912         else
913         {
914                 *tag = (nfc_tag_h)g_nfc_context.current_tag;
915                 ret = NFC_ERROR_NONE;
916         }
917         return ret;
918 }
919
920 int nfc_manager_get_connected_target(nfc_p2p_target_h *target)
921 {
922         int ret;
923         if( target == NULL )
924                 return _return_invalid_param(__func__);
925
926         if(g_nfc_context.current_target == NULL)
927         {
928                 ret = NFC_ERROR_NO_DEVICE;
929         }
930         else
931         {
932                 *target = (nfc_p2p_target_h)g_nfc_context.current_target;
933                 ret = NFC_ERROR_NONE;
934         }
935
936         return ret;
937 }
938
939 int nfc_manager_set_system_handler_enable(bool enable)
940 {
941         int ret = NFC_ERROR_NONE;
942
943         ret = net_nfc_set_launch_popup_state(enable);
944
945         if( ret != NET_NFC_OK )
946                 return _convert_error_code(__func__, ret);
947         else
948                 return ret;
949 }
950
951 bool nfc_manager_is_system_handler_enabled(void)
952 {
953         int state = 0;
954
955         net_nfc_get_launch_popup_state(&state);
956
957         return state;
958 }
959
960 int nfc_manager_set_card_emulation_se_type(nfc_se_type_e type, nfc_set_card_emulation_completed_cb callback, void* user_data)
961 {
962         if ((type < NFC_SE_TYPE_DISABLE) || (type > NFC_SE_TYPE_UICC))
963         {
964                 return _return_invalid_param(__func__);
965         }
966
967         if(!nfc_manager_is_activated())
968         {
969                 return NFC_ERROR_NOT_ACTIVATED;
970         }
971
972         _async_callback_data *trans_data = NULL;
973         int ret=0;
974         net_nfc_se_type_e se_type;
975
976         if( callback != NULL )
977         {
978                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
979                 if(trans_data == NULL )
980                         return NFC_ERROR_OUT_OF_MEMORY;
981                 memset(trans_data , 0 , sizeof(_async_callback_data));
982                 trans_data->callback = callback;
983                 trans_data->user_data = user_data;
984         }
985
986         switch(type)
987         {
988         case NFC_SE_TYPE_DISABLE:
989                 se_type = NET_NFC_SE_TYPE_NONE;
990                 break;
991         case NFC_SE_TYPE_ESE:
992                 se_type = NET_NFC_SE_TYPE_ESE;
993                 break;
994         case NFC_SE_TYPE_UICC:
995                 se_type = NET_NFC_SE_TYPE_UICC;
996                 break;
997         default:
998         break;
999         }
1000
1001         ret = net_nfc_set_secure_element_type(se_type, (void *)trans_data);
1002
1003         return _convert_error_code(__func__, ret);
1004 }
1005
1006 int nfc_manager_get_card_emulation_se_type(nfc_se_type_e* type)
1007 {
1008         int ret = NFC_ERROR_NONE;
1009         int se_type;
1010
1011         if ((ret = vconf_get_int(VCONFKEY_NFC_SE_TYPE, &se_type)) != 0)
1012         {
1013                 ret = NFC_ERROR_OPERATION_FAILED;
1014                 return ret;
1015         }
1016         else
1017         {
1018                 if ((se_type >= NFC_SE_TYPE_DISABLE) && (se_type <= NFC_SE_TYPE_UICC))
1019                         *type =         se_type;
1020                 else
1021                         ret = NFC_ERROR_OPERATION_FAILED;
1022         }
1023
1024         return ret;
1025 }
1026
1027 int nfc_manager_get_cached_message(nfc_ndef_message_h *ndef_message)
1028 {
1029         int ret;
1030         if( ndef_message == NULL )
1031                 return _return_invalid_param(__func__);
1032         ret = net_nfc_retrieve_current_ndef_message(ndef_message);
1033         return _convert_error_code(__func__, ret);
1034 }
1035
1036 int nfc_ndef_record_create(nfc_ndef_record_h* record, nfc_record_tnf_e tnf, const unsigned char* type, int type_size , const unsigned char * id , int id_size, const unsigned char * payload, int payload_size)
1037 {
1038         if(record == NULL  )
1039                 return _return_invalid_param(__func__);
1040
1041         data_s type_data = { (unsigned char*)type, type_size };
1042         data_s id_data = { (unsigned char*)id , id_size };
1043         data_s payload_data = {(unsigned char*)payload , payload_size };
1044         int ret;
1045         ret = net_nfc_create_record((ndef_record_h*)record , tnf , (data_h)&type_data , (data_h)&id_data, (data_h)&payload_data);
1046
1047         return _convert_error_code(__func__, ret);
1048 }
1049
1050 int nfc_ndef_record_create_text(nfc_ndef_record_h* record, const char * text, const char * lang_code, nfc_encode_type_e encode )
1051 {
1052
1053         if(record == NULL ||  text == NULL || lang_code == NULL )
1054                 return _return_invalid_param(__func__);
1055         int ret;
1056         ret = net_nfc_create_text_type_record((ndef_record_h*)record, text, lang_code, encode);
1057         return _convert_error_code(__func__, ret);
1058 }
1059
1060 int nfc_ndef_record_create_uri(nfc_ndef_record_h* record, const char* uri)
1061 {
1062
1063         if(record == NULL ||  uri == NULL)
1064                 return _return_invalid_param(__func__);
1065         int ret;
1066         ret = net_nfc_create_uri_type_record((ndef_record_h*)record , uri , NET_NFC_SCHEMA_FULL_URI);
1067         return _convert_error_code(__func__, ret);
1068 }
1069
1070 int nfc_ndef_record_create_mime(nfc_ndef_record_h* record, const char * mime_type , const unsigned char * data , int data_size)
1071 {
1072         if(record == NULL ||  mime_type == NULL || data == NULL)
1073                 return _return_invalid_param(__func__);
1074         return nfc_ndef_record_create(record , NFC_RECORD_TNF_MIME_MEDIA , (unsigned char *) mime_type, strlen(mime_type), NULL, 0, data, data_size );
1075 }
1076
1077 int nfc_ndef_record_get_mime_type(nfc_ndef_record_h record , char **mime_type){
1078
1079         int ret;
1080         unsigned char *typename;
1081         int length;
1082
1083         if(record == NULL ||  mime_type == NULL)
1084                 return _return_invalid_param(__func__);
1085         nfc_record_tnf_e tnf;
1086         if( nfc_ndef_record_get_tnf(record, &tnf ) != 0 || tnf != NFC_RECORD_TNF_MIME_MEDIA ){
1087                 return NFC_ERROR_INVALID_RECORD_TYPE;
1088         }
1089
1090         ret = nfc_ndef_record_get_type(record,&typename, &length);
1091         if( ret != 0 )
1092                 return _convert_error_code(__func__, ret);
1093
1094         *mime_type = malloc(length+1);
1095         if( *mime_type == NULL ){
1096                 LOGE( "OUT_OF_MEMORY (0x%08x)", NFC_ERROR_OUT_OF_MEMORY);
1097                 return NFC_ERROR_OUT_OF_MEMORY;
1098         }
1099
1100         memset(*mime_type, 0 , length+1);
1101         memcpy(*mime_type, typename , length );
1102         return 0;
1103 }
1104
1105 int nfc_ndef_record_destroy(nfc_ndef_record_h record)
1106 {
1107
1108         if(record == NULL  )
1109                 return _return_invalid_param(__func__);
1110         int ret;
1111         ret = net_nfc_free_record(record);
1112         return _convert_error_code(__func__, ret);
1113 }
1114
1115 int nfc_ndef_record_set_id (nfc_ndef_record_h record, unsigned char *id , int id_size)
1116 {
1117
1118         if(record == NULL ||  id == NULL )
1119                 return _return_invalid_param(__func__);
1120         int ret;
1121         data_s id_data = {id, id_size};
1122         ret = net_nfc_set_record_id(record, (data_h)&id_data);
1123         return _convert_error_code(__func__, ret);
1124 }
1125
1126 int nfc_ndef_record_get_payload (nfc_ndef_record_h record, unsigned char ** payload, int *size)
1127 {
1128
1129         if(record == NULL ||  payload == NULL || size == NULL )
1130                 return _return_invalid_param(__func__);
1131         int ret ;
1132         data_s *payload_data;
1133         ret = net_nfc_get_record_payload(record, (data_h*)&payload_data);
1134         if( ret == 0){
1135                 *payload = payload_data->buffer;
1136                 *size = payload_data->length;
1137         }
1138         return _convert_error_code(__func__, ret);
1139 }
1140
1141 int nfc_ndef_record_get_type (nfc_ndef_record_h record, unsigned char ** type, int *size)
1142 {
1143
1144         int ret ;
1145         data_s *type_data;
1146         ret = net_nfc_get_record_type(record, (data_h*)&type_data);
1147         if( ret == 0){
1148                 *type = type_data->buffer;
1149                 *size = type_data->length;
1150         }
1151         return _convert_error_code(__func__, ret);
1152 }
1153
1154 int nfc_ndef_record_get_id (nfc_ndef_record_h record, unsigned char **id , int *size)
1155 {
1156
1157         if(record == NULL ||  id == NULL || size == NULL )
1158                 return _return_invalid_param(__func__);
1159
1160         int ret ;
1161         data_s *id_data;
1162         ret = net_nfc_get_record_id(record, (data_h*)&id_data);
1163         if( ret == 0){
1164                 *id = id_data->buffer;
1165                 *size = id_data->length;
1166         }
1167         return _convert_error_code(__func__, ret);
1168 }
1169
1170 int nfc_ndef_record_get_tnf(nfc_ndef_record_h record, nfc_record_tnf_e * tnf)
1171 {
1172
1173         if(record == NULL ||  tnf == NULL )
1174                 return _return_invalid_param(__func__);
1175         int ret;
1176         ret = net_nfc_get_record_tnf(record, (net_nfc_record_tnf_e*)tnf);
1177         return _convert_error_code(__func__, ret);
1178 }
1179
1180 int nfc_ndef_record_get_text(nfc_ndef_record_h record, char** buffer)
1181 {
1182
1183         if(record == NULL ||  buffer == NULL)
1184                 return _return_invalid_param(__func__);
1185         int ret;
1186         ret = net_nfc_create_text_string_from_text_record(record, buffer);
1187         return _convert_error_code(__func__, ret);
1188 }
1189
1190 int nfc_ndef_record_get_langcode(nfc_ndef_record_h record, char **lang_code)
1191 {
1192
1193         if(record == NULL ||  lang_code == NULL)
1194                 return _return_invalid_param(__func__);
1195         int ret;
1196         ret = net_nfc_get_languange_code_string_from_text_record(record, lang_code);
1197         return _convert_error_code(__func__, ret);
1198 }
1199 int nfc_ndef_record_get_encode_type(nfc_ndef_record_h record, nfc_encode_type_e *encode)
1200 {
1201
1202         if(record == NULL ||  encode == NULL  )
1203                 return _return_invalid_param(__func__);
1204         int ret=0;
1205         ret = net_nfc_get_encoding_type_from_text_record(record, (net_nfc_encode_type_e*)encode);
1206 //      if( ret == NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE)
1207 //              LOGE("%s reord type is not text type");
1208         return _convert_error_code(__func__, ret);
1209 }
1210 int nfc_ndef_record_get_uri(nfc_ndef_record_h record , char **uri)
1211 {
1212
1213         if(record == NULL ||  uri == NULL  )
1214                 return _return_invalid_param(__func__);
1215         int ret=0;
1216         ret = net_nfc_create_uri_string_from_uri_record(record, uri);
1217         return _convert_error_code(__func__, ret);
1218 }
1219
1220 int nfc_ndef_message_create(nfc_ndef_message_h* ndef_message)
1221 {
1222
1223         if( ndef_message == NULL )
1224                 return _return_invalid_param(__func__);
1225         int ret=0;
1226         ret = net_nfc_create_ndef_message(ndef_message);
1227         return _convert_error_code(__func__, ret);
1228 }
1229
1230 int nfc_ndef_message_create_from_rawdata(nfc_ndef_message_h* ndef_message, const unsigned char* rawdata, int rawdata_size)
1231 {
1232
1233         if( ndef_message == NULL || rawdata == NULL)
1234                 return _return_invalid_param(__func__);
1235         int ret=0;
1236         data_s rawdata_data = {(unsigned char *)rawdata, rawdata_size};
1237         ret = net_nfc_create_ndef_message_from_rawdata((ndef_message_h*)ndef_message , (data_h)&rawdata_data);
1238         return _convert_error_code(__func__, ret);
1239 }
1240
1241 int nfc_ndef_message_destroy(nfc_ndef_message_h ndef_message)
1242 {
1243
1244         if( ndef_message == NULL )
1245                 return _return_invalid_param(__func__);
1246         int ret=0;
1247         ret = net_nfc_free_ndef_message(ndef_message);
1248         return _convert_error_code(__func__, ret);
1249 }
1250
1251 int nfc_ndef_message_get_record_count(nfc_ndef_message_h ndef_message , int *count)
1252 {
1253
1254         if( ndef_message == NULL || count == NULL)
1255                 return _return_invalid_param(__func__);
1256         int ret=0;
1257         ret = net_nfc_get_ndef_message_record_count(ndef_message , count);
1258         return _convert_error_code(__func__, ret);
1259 }
1260
1261 int nfc_ndef_message_get_rawdata(nfc_ndef_message_h ndef_message , unsigned char ** rawdata , int *rawdata_size)
1262 {
1263
1264         if( ndef_message == NULL || rawdata == NULL || rawdata_size == NULL)
1265                 return _return_invalid_param(__func__);
1266         int ret=0;
1267         data_s *rawdata_data;
1268         ret = net_nfc_create_rawdata_from_ndef_message(ndef_message , (data_h*)&rawdata_data);
1269         if( ret == 0 )
1270         {
1271                 *rawdata = rawdata_data->buffer;
1272                 *rawdata_size = rawdata_data->length;
1273         }
1274         return _convert_error_code(__func__, ret);
1275 }
1276
1277 int nfc_ndef_message_get_rawdata_size(nfc_ndef_message_h ndef_message , int *byte_size)
1278 {
1279
1280         if( ndef_message == NULL || byte_size == NULL)
1281                 return _return_invalid_param(__func__);
1282         int ret=0;
1283         ret = net_nfc_get_ndef_message_byte_length(ndef_message , byte_size);
1284         return _convert_error_code(__func__, ret);
1285 }
1286
1287 int nfc_ndef_message_append_record(nfc_ndef_message_h ndef_message , nfc_ndef_record_h record)
1288 {
1289
1290         if( ndef_message == NULL || record == NULL)
1291                 return _return_invalid_param(__func__);
1292         int ret=0;
1293         ret = net_nfc_append_record_to_ndef_message(ndef_message , record );
1294         return _convert_error_code(__func__, ret);
1295 }
1296
1297 int nfc_ndef_message_insert_record(nfc_ndef_message_h ndef_message , int index, nfc_ndef_record_h record)
1298 {
1299
1300         if( ndef_message == NULL || record == NULL )
1301                 return _return_invalid_param(__func__);
1302         int ret=0;
1303         ret = net_nfc_append_record_by_index(ndef_message , index , record);
1304         return _convert_error_code(__func__, ret);
1305 }
1306
1307 int nfc_ndef_message_remove_record(nfc_ndef_message_h ndef_message , int index)
1308 {
1309
1310         if( ndef_message == NULL )
1311                 return _return_invalid_param(__func__);
1312         int ret=0;
1313         ret = net_nfc_remove_record_by_index(ndef_message , index);
1314         return _convert_error_code(__func__, ret);
1315 }
1316
1317 int nfc_ndef_message_get_record(nfc_ndef_message_h ndef_message , int index , nfc_ndef_record_h *record)
1318 {
1319
1320         if( ndef_message == NULL || record == NULL)
1321                 return _return_invalid_param(__func__);
1322         int ret=0;
1323         ret = net_nfc_get_record_by_index(ndef_message , index , (ndef_record_h*)record);
1324         return _convert_error_code(__func__, ret);
1325 }
1326
1327 int nfc_tag_get_type( nfc_tag_h tag , nfc_tag_type_e *type)
1328 {
1329
1330         if( tag == NULL || type == NULL)
1331                 return _return_invalid_param(__func__);
1332         int ret=0;
1333         ret = net_nfc_get_tag_type( tag, (net_nfc_target_type_e*) type);
1334         return _convert_error_code(__func__, ret);
1335 }
1336
1337 int nfc_tag_is_support_ndef(nfc_tag_h tag, bool * is_support)
1338 {
1339
1340         if( tag == NULL || is_support== NULL)
1341                 return _return_invalid_param(__func__);
1342         int ret=0;
1343         ret = net_nfc_get_tag_ndef_support(tag , is_support);
1344         return _convert_error_code(__func__, ret);
1345 }
1346
1347 int nfc_tag_get_maximum_ndef_size(nfc_tag_h tag , unsigned int * max_size)
1348 {
1349
1350         if( tag == NULL || max_size == NULL)
1351                 return _return_invalid_param(__func__);
1352         int ret=0;
1353         ret = net_nfc_get_tag_max_data_size(tag , max_size);
1354         return _convert_error_code(__func__, ret);
1355 }
1356
1357 int nfc_tag_get_ndef_size(nfc_tag_h tag, unsigned int * actual_data_size)
1358 {
1359
1360         if( tag == NULL || actual_data_size == NULL)
1361                 return _return_invalid_param(__func__);
1362         int ret=0;
1363         ret = net_nfc_get_tag_actual_data_size(tag , actual_data_size);
1364         return _convert_error_code(__func__, ret);
1365 }
1366
1367 int nfc_tag_foreach_information(nfc_tag_h tag , nfc_tag_information_cb callback , void * user_data )
1368 {
1369         int i;
1370
1371         net_nfc_tag_info_s *taglist = g_nfc_context.current_tag->tag_info_list;
1372
1373         if( tag == NULL || callback == NULL )
1374                 return _return_invalid_param(__func__);
1375
1376         for(i=0; i<g_nfc_context.current_tag->number_of_keys; i++){
1377                 bool cont;
1378                 cont = callback(taglist[i].key, net_nfc_get_data_buffer(taglist[i].value), net_nfc_get_data_length(taglist[i].value), user_data);
1379                 if( !cont )
1380                         break;
1381         }
1382
1383         return 0;
1384 }
1385
1386 int nfc_tag_transceive( nfc_tag_h tag, unsigned char * buffer, int buffer_size,  nfc_tag_transceive_completed_cb callback , void * user_data )
1387 {
1388
1389         if( tag == NULL )
1390                 return _return_invalid_param(__func__);
1391
1392         if(!nfc_manager_is_activated())
1393         {
1394                 return NFC_ERROR_NOT_ACTIVATED;
1395         }
1396
1397         if(_check_app_permission() == false)
1398         {
1399                 LOGE("permission check fail");
1400                 return NFC_ERROR_SECURITY_RESTRICTED;
1401         }
1402
1403         int ret=0;
1404         data_s rawdata = { buffer, buffer_size };
1405         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1406
1407
1408         _async_callback_data * trans_data = NULL;
1409         if( callback != NULL ){
1410                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1411                 if(trans_data == NULL )
1412                         return NFC_ERROR_OUT_OF_MEMORY;
1413                 memset(trans_data , 0 , sizeof(_async_callback_data));
1414                 trans_data->callback = callback;
1415                 trans_data->user_data = user_data;
1416                 trans_data->callback_type = _NFC_CALLBACK_TYPE_DATA;
1417         }
1418         ret = net_nfc_transceive((net_nfc_target_handle_h)tag_info->handle , (data_h) &rawdata, trans_data );
1419
1420
1421         return _convert_error_code(__func__, ret);
1422 }
1423
1424 int nfc_tag_read_ndef( nfc_tag_h tag, nfc_tag_read_completed_cb callback , void * user_data)
1425 {
1426
1427         if( tag == NULL )
1428                 return _return_invalid_param(__func__);
1429
1430         if(!nfc_manager_is_activated())
1431         {
1432                 return NFC_ERROR_NOT_ACTIVATED;
1433         }
1434
1435         int ret=0;
1436         _async_callback_data * trans_data = NULL;
1437         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1438
1439
1440         if( callback != NULL ){
1441                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1442                 if(trans_data == NULL )
1443                         return NFC_ERROR_OUT_OF_MEMORY;
1444                 memset(trans_data , 0 , sizeof(_async_callback_data));
1445                 trans_data->callback = callback;
1446                 trans_data->user_data = user_data;
1447         }
1448         ret = net_nfc_read_tag((net_nfc_target_handle_h)tag_info->handle , trans_data );
1449         return _convert_error_code(__func__, ret);
1450 }
1451
1452 int nfc_tag_write_ndef(nfc_tag_h tag, nfc_ndef_message_h msg , nfc_tag_write_completed_cb callback ,  void *user_data)
1453 {
1454
1455         if( tag == NULL )
1456                 return _return_invalid_param(__func__);
1457
1458         if(!nfc_manager_is_activated())
1459         {
1460                 return NFC_ERROR_NOT_ACTIVATED;
1461         }
1462
1463         if(_check_app_permission() == false)
1464         {
1465                 LOGE("permission check fail");
1466                 return NFC_ERROR_SECURITY_RESTRICTED;
1467         }
1468
1469         int ret=0;
1470         _async_callback_data * trans_data = NULL;
1471         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1472
1473         if (tag_info->ndefCardState == NET_NFC_NDEF_CARD_READ_ONLY )
1474         {
1475                 return NFC_ERROR_READ_ONLY_NDEF;
1476
1477         }
1478
1479          int byte_size = 0;
1480         nfc_ndef_message_get_rawdata_size(msg , &byte_size);
1481
1482         if(tag_info->maxDataSize < byte_size)
1483         {
1484                 return NFC_ERROR_NO_SPACE_ON_NDEF;
1485         }
1486
1487
1488
1489         if( callback != NULL ){
1490                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1491                 if(trans_data == NULL )
1492                         return NFC_ERROR_OUT_OF_MEMORY;
1493                 memset(trans_data , 0 , sizeof(_async_callback_data));
1494                 trans_data->callback = callback;
1495                 trans_data->user_data = user_data;
1496         }
1497         ret = net_nfc_write_ndef( (net_nfc_target_handle_h)tag_info->handle , msg , trans_data );
1498         return _convert_error_code(__func__, ret);
1499 }
1500
1501 int nfc_tag_format_ndef(nfc_tag_h tag , unsigned char * key, int key_size , nfc_tag_format_completed_cb callback, void * user_data )
1502 {
1503
1504         if( tag == NULL )
1505                 return _return_invalid_param(__func__);
1506
1507         if(!nfc_manager_is_activated())
1508         {
1509                 return NFC_ERROR_NOT_ACTIVATED;
1510         }
1511
1512         if(_check_app_permission() == false)
1513         {
1514                 LOGE("permission check fail");
1515                 return NFC_ERROR_SECURITY_RESTRICTED;
1516         }
1517
1518         data_s key_data = { key, key_size };
1519         int ret=0;
1520         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1521
1522         _async_callback_data * trans_data = NULL;
1523         if( callback != NULL ){
1524                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1525                 if(trans_data == NULL )
1526                         return NFC_ERROR_OUT_OF_MEMORY;
1527                 memset(trans_data , 0 , sizeof(_async_callback_data));
1528                 trans_data->callback = callback;
1529                 trans_data->user_data = user_data;
1530         }
1531
1532         ret = net_nfc_format_ndef( (net_nfc_target_handle_h)tag_info->handle, (data_h)&key_data, trans_data );
1533         return _convert_error_code(__func__, ret);
1534 }
1535
1536 int nfc_mifare_authenticate_with_keyA(nfc_tag_h tag,  int sector_index, unsigned char * auth_key, nfc_mifare_authenticate_with_keyA_completed_cb callback, void *user_data)
1537 {
1538
1539         if( tag == NULL )
1540                 return _return_invalid_param(__func__);
1541
1542         if(!nfc_manager_is_activated())
1543         {
1544                 return NFC_ERROR_NOT_ACTIVATED;
1545         }
1546
1547         data_s auth_key_data = { auth_key , 6};
1548         int ret = 0;
1549         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1550
1551         _async_callback_data * trans_data = NULL;
1552         if( callback != NULL ){
1553                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1554                 if(trans_data == NULL )
1555                         return NFC_ERROR_OUT_OF_MEMORY;
1556                 memset(trans_data , 0 , sizeof(_async_callback_data));
1557                 trans_data->callback = callback;
1558                 trans_data->user_data = user_data;
1559                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1560         }
1561
1562         ret = net_nfc_mifare_authenticate_with_keyA( (net_nfc_target_handle_h)tag_info->handle, sector_index, (data_h)&auth_key_data, trans_data);
1563         return _convert_error_code(__func__, ret);
1564 }
1565
1566 int nfc_mifare_authenticate_with_keyB(nfc_tag_h tag,  int sector_index, unsigned char * auth_key, nfc_mifare_authenticate_with_keyB_completed_cb callback, void *user_data)
1567 {
1568
1569         if( tag == NULL )
1570                 return _return_invalid_param(__func__);
1571
1572         if(!nfc_manager_is_activated())
1573         {
1574                 return NFC_ERROR_NOT_ACTIVATED;
1575         }
1576
1577         data_s auth_key_data = { auth_key , 6};
1578         int ret = 0;
1579         _async_callback_data * trans_data = NULL;
1580         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1581
1582         if( callback != NULL ){
1583                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1584                 if(trans_data == NULL )
1585                         return NFC_ERROR_OUT_OF_MEMORY;
1586                 memset(trans_data , 0 , sizeof(_async_callback_data));
1587                 trans_data->callback = callback;
1588                 trans_data->user_data = user_data;
1589                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1590         }
1591
1592         ret = net_nfc_mifare_authenticate_with_keyB( (net_nfc_target_handle_h)tag_info->handle, sector_index, (data_h)&auth_key_data, trans_data);
1593         return _convert_error_code(__func__, ret);
1594 }
1595
1596 int nfc_mifare_read_block(nfc_tag_h tag, int block_index, nfc_mifare_read_block_completed_cb callback, void *user_data)
1597 {
1598
1599         if( tag == NULL )
1600                 return _return_invalid_param(__func__);
1601
1602         if(!nfc_manager_is_activated())
1603         {
1604                 return NFC_ERROR_NOT_ACTIVATED;
1605         }
1606
1607         int ret = 0;
1608         _async_callback_data * trans_data = NULL;
1609         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1610
1611         if( callback != NULL ){
1612                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1613                 if(trans_data == NULL )
1614                         return NFC_ERROR_OUT_OF_MEMORY;
1615                 memset(trans_data , 0 , sizeof(_async_callback_data));
1616                 trans_data->callback = callback;
1617                 trans_data->user_data = user_data;
1618                 trans_data->callback_type = _NFC_CALLBACK_TYPE_DATA;
1619         }
1620
1621         ret = net_nfc_mifare_read( (net_nfc_target_handle_h)tag_info->handle, block_index, trans_data);
1622         return _convert_error_code(__func__, ret);
1623 }
1624
1625 int nfc_mifare_read_page(nfc_tag_h tag, int page_index, nfc_mifare_read_block_completed_cb callback, void *user_data)
1626 {
1627         if(!nfc_manager_is_activated())
1628         {
1629                 return NFC_ERROR_NOT_ACTIVATED;
1630         }
1631
1632         return nfc_mifare_read_block(tag, page_index, callback, user_data);
1633 }
1634
1635 int nfc_mifare_write_block (nfc_tag_h tag, int block_index, unsigned char* buffer, int buffer_size, nfc_mifare_write_block_completed_cb callback, void* user_data)
1636 {
1637
1638         if( tag == NULL )
1639                 return _return_invalid_param(__func__);
1640
1641         if(!nfc_manager_is_activated())
1642         {
1643                 return NFC_ERROR_NOT_ACTIVATED;
1644         }
1645
1646         int ret = 0;
1647         data_s block_data = { buffer , buffer_size};
1648         _async_callback_data * trans_data = NULL;
1649         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1650
1651         if( callback != NULL ){
1652                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1653                 if(trans_data == NULL )
1654                         return NFC_ERROR_OUT_OF_MEMORY;
1655                 memset(trans_data , 0 , sizeof(_async_callback_data));
1656                 trans_data->callback = callback;
1657                 trans_data->user_data = user_data;
1658                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1659         }
1660
1661         ret = net_nfc_mifare_write_block( (net_nfc_target_handle_h)tag_info->handle, block_index, (data_h)&block_data, trans_data);
1662         return _convert_error_code(__func__, ret);
1663 }
1664
1665 int nfc_mifare_write_page(nfc_tag_h tag, int page_index, unsigned char* buffer, int buffer_size, nfc_mifare_write_block_completed_cb callback, void* user_data)
1666 {
1667
1668         if( tag == NULL )
1669                 return _return_invalid_param(__func__);
1670
1671         if(!nfc_manager_is_activated())
1672         {
1673                 return NFC_ERROR_NOT_ACTIVATED;
1674         }
1675
1676         int ret = 0;
1677         data_s block_data = { buffer , buffer_size};
1678         _async_callback_data * trans_data = NULL;
1679
1680         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1681
1682
1683         if( callback != NULL ){
1684                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1685                 if(trans_data == NULL )
1686                         return NFC_ERROR_OUT_OF_MEMORY;
1687                 memset(trans_data , 0 , sizeof(_async_callback_data));
1688                 trans_data->callback = callback;
1689                 trans_data->user_data = user_data;
1690                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1691         }
1692
1693         ret = net_nfc_mifare_write_page( (net_nfc_target_handle_h)tag_info->handle, page_index, (data_h)&block_data, trans_data);
1694         return _convert_error_code(__func__, ret);
1695 }
1696
1697 int nfc_mifare_increment(nfc_tag_h tag, int block_index, int value, nfc_mifare_increment_completed_cb callback, void *user_data)
1698 {
1699
1700         if( tag == NULL )
1701                 return _return_invalid_param(__func__);
1702
1703         if(!nfc_manager_is_activated())
1704         {
1705                 return NFC_ERROR_NOT_ACTIVATED;
1706         }
1707
1708         int ret = 0;
1709         _async_callback_data * trans_data = NULL;
1710         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1711
1712         if( callback != NULL ){
1713                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1714                 if(trans_data == NULL )
1715                         return NFC_ERROR_OUT_OF_MEMORY;
1716                 memset(trans_data , 0 , sizeof(_async_callback_data));
1717                 trans_data->callback = callback;
1718                 trans_data->user_data = user_data;
1719                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1720         }
1721
1722         ret = net_nfc_mifare_increment( (net_nfc_target_handle_h)tag_info->handle, block_index,value, trans_data);
1723         return _convert_error_code(__func__, ret);
1724
1725 }
1726
1727 int nfc_mifare_decrement(nfc_tag_h tag, int block_index, int value, nfc_mifare_decrement_completed_cb callback, void *user_data)
1728 {
1729
1730         if( tag == NULL )
1731                 return _return_invalid_param(__func__);
1732
1733         if(!nfc_manager_is_activated())
1734         {
1735                 return NFC_ERROR_NOT_ACTIVATED;
1736         }
1737
1738         int ret = 0;
1739         _async_callback_data * trans_data = NULL;
1740         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1741
1742
1743         if( callback != NULL ){
1744                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1745                 if(trans_data == NULL )
1746                         return NFC_ERROR_OUT_OF_MEMORY;
1747                 memset(trans_data , 0 , sizeof(_async_callback_data));
1748                 trans_data->callback = callback;
1749                 trans_data->user_data = user_data;
1750                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1751         }
1752
1753         ret = net_nfc_mifare_decrement( (net_nfc_target_handle_h)tag_info->handle, block_index,value, trans_data);
1754         return _convert_error_code(__func__, ret);
1755 }
1756
1757 int nfc_mifare_transfer(nfc_tag_h tag, int block_index, nfc_mifare_transfer_completed_cb callback, void *user_data)
1758 {
1759
1760         if( tag == NULL )
1761                 return _return_invalid_param(__func__);
1762
1763         if(!nfc_manager_is_activated())
1764         {
1765                 return NFC_ERROR_NOT_ACTIVATED;
1766         }
1767
1768         int ret = 0;
1769         _async_callback_data * trans_data = NULL;
1770         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1771
1772
1773         if( callback != NULL ){
1774                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1775                 if(trans_data == NULL )
1776                         return NFC_ERROR_OUT_OF_MEMORY;
1777                 memset(trans_data , 0 , sizeof(_async_callback_data));
1778                 trans_data->callback = callback;
1779                 trans_data->user_data = user_data;
1780                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1781         }
1782
1783         ret = net_nfc_mifare_transfer( (net_nfc_target_handle_h)tag_info->handle, block_index, trans_data);
1784         return _convert_error_code(__func__, ret);
1785 }
1786
1787 int nfc_mifare_restore(nfc_tag_h tag, int block_index, nfc_mifare_restore_completed_cb callback, void *user_data)
1788 {
1789
1790         if( tag == NULL )
1791                 return _return_invalid_param(__func__);
1792
1793         if(!nfc_manager_is_activated())
1794         {
1795                 return NFC_ERROR_NOT_ACTIVATED;
1796         }
1797
1798
1799         int ret = 0;
1800         _async_callback_data * trans_data = NULL;
1801         net_nfc_target_info_s *tag_info = (net_nfc_target_info_s*)tag;
1802
1803         if( callback != NULL ){
1804                 trans_data = (_async_callback_data*)malloc( sizeof(_async_callback_data));
1805                 if(trans_data == NULL )
1806                         return NFC_ERROR_OUT_OF_MEMORY;
1807                 memset(trans_data , 0 , sizeof(_async_callback_data));
1808                 trans_data->callback = callback;
1809                 trans_data->user_data = user_data;
1810                 trans_data->callback_type = _NFC_CALLBACK_TYPE_RESULT;
1811         }
1812
1813         ret = net_nfc_mifare_restore( (net_nfc_target_handle_h)tag_info->handle, block_index, trans_data);
1814         return _convert_error_code(__func__, ret);
1815 }
1816
1817 int nfc_p2p_send(nfc_p2p_target_h target , nfc_ndef_message_h message , nfc_p2p_send_completed_cb callback, void *user_data){
1818         int ret;
1819
1820         if( target == NULL || message == NULL  )
1821                 return _return_invalid_param(__func__);
1822
1823         if(!nfc_manager_is_activated())
1824         {
1825                 return NFC_ERROR_NOT_ACTIVATED;
1826         }
1827
1828         if(_check_app_permission() == false)
1829         {
1830                 LOGE("permission check fail");
1831                 return NFC_ERROR_SECURITY_RESTRICTED;
1832         }
1833
1834         net_nfc_exchanger_data_h data_handle;
1835         data_h rawdata;
1836         net_nfc_create_rawdata_from_ndef_message(message, &rawdata);
1837         ret = net_nfc_create_exchanger_data(&data_handle,  rawdata);
1838         net_nfc_free_data(rawdata);
1839
1840
1841         if( ret != 0)
1842                 return _convert_error_code(__func__, ret);
1843
1844         ret = net_nfc_send_exchanger_data(data_handle ,(net_nfc_target_handle_h)target, NULL);
1845
1846         if( ret != 0 ){
1847                 net_nfc_free_exchanger_data(data_handle);
1848                 return _convert_error_code(__func__, ret);
1849         }
1850
1851         g_nfc_context.on_p2p_send_completed_cb = callback;
1852         g_nfc_context.on_p2p_send_completed_user_data = user_data;
1853
1854         ret = net_nfc_free_exchanger_data(data_handle);
1855
1856         if( ret != 0 ){
1857                 return _convert_error_code(__func__, ret);
1858         }
1859
1860         return 0;
1861 }
1862
1863 int nfc_p2p_connection_handover(nfc_p2p_target_h target , nfc_ac_type_e type, nfc_p2p_connection_handover_completed_cb callback, void *user_data){
1864         int ret;
1865         net_nfc_conn_handover_carrier_type_e net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1866         if( target == NULL  )
1867                 return _return_invalid_param(__func__);
1868
1869         if( type > NFC_AC_TYPE_UNKNOWN  )
1870                 return _return_invalid_param(__func__);
1871
1872         if(!nfc_manager_is_activated())
1873         {
1874                 return NFC_ERROR_NOT_ACTIVATED;
1875         }
1876
1877         switch( type ){
1878                 case NFC_AC_TYPE_BT:
1879                         net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
1880                         break;
1881                 case NFC_AC_TYPE_WIFI:
1882                         net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
1883                         break;
1884                 case NFC_AC_TYPE_WIFI_DIRECT:
1885                         net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
1886                         break;
1887                 case NFC_AC_TYPE_UNKNOWN :
1888                         net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1889                         break;
1890
1891                 default:
1892                         break;
1893                 }
1894         ret = net_nfc_exchanger_request_connection_handover((net_nfc_target_handle_h)target, net_ac_type);
1895
1896         if( ret != 0 ){
1897                 return _convert_error_code(__func__, ret);
1898         }
1899
1900         g_nfc_context.on_p2p_connection_handover_completed_cb = callback;
1901         g_nfc_context.on_p2p_connection_handover_completed_user_data = user_data;
1902
1903         return 0;
1904 }
1905
1906 bool nfc_p2p_is_supported_ac_type( nfc_ac_type_e carrior){
1907
1908         if( carrior > NFC_AC_TYPE_UNKNOWN  )
1909                 return false;
1910
1911
1912         if( carrior == NFC_AC_TYPE_BT){
1913                 return true;
1914         }
1915         else{
1916                 return false;
1917         }
1918 }
1919
1920 int nfc_p2p_set_data_received_cb(nfc_p2p_target_h target, nfc_p2p_data_recived_cb callback, void *user_data){
1921         if( target == NULL || callback == NULL )
1922                 return _return_invalid_param(__func__);
1923
1924 //      if( g_nfc_context.current_target.connection_id != ((net_nfc_target_handle_s*)target)->connection_id )
1925         if(g_nfc_context.current_target != target )
1926                 return _return_invalid_param(__func__);
1927
1928         g_nfc_context.on_p2p_recv_cb = callback;
1929         g_nfc_context.on_p2p_recv_user_data = user_data;
1930         return 0;
1931 }
1932
1933 int nfc_p2p_unset_data_received_cb(nfc_p2p_target_h target){
1934         if( target == NULL )
1935                 return _return_invalid_param(__func__);
1936
1937 //      if( g_nfc_context.current_target.connection_id != ((net_nfc_target_handle_s*)target)->connection_id )
1938         if(g_nfc_context.current_target != target )
1939                 return _return_invalid_param(__func__);
1940
1941         g_nfc_context.on_p2p_recv_cb = NULL;
1942         g_nfc_context.on_p2p_recv_user_data = NULL;
1943         return 0;
1944 }
1945
1946 int nfc_manager_set_p2p_target_discovered_cb( nfc_p2p_target_discovered_cb callback , void *user_data){
1947         if( callback == NULL )
1948                 return _return_invalid_param(__func__);
1949         g_nfc_context.on_p2p_discovered_cb = callback;
1950         g_nfc_context.on_p2p_discovered_user_data = user_data;
1951         return 0;
1952 }
1953
1954 void nfc_manager_unset_p2p_target_discovered_cb( void ){
1955         g_nfc_context.on_p2p_discovered_cb = NULL;
1956         g_nfc_context.on_p2p_discovered_user_data = NULL;
1957 }
1958
1959 int nfc_manager_set_se_event_cb(nfc_se_event_cb callback, void *user_data){
1960         if( callback == NULL )
1961                 return _return_invalid_param(__func__);
1962         g_nfc_context.on_se_event_cb = callback;
1963         g_nfc_context.on_se_event_user_data = user_data;
1964         return 0;
1965 }
1966
1967 void nfc_manager_unset_se_event_cb(void){
1968         g_nfc_context.on_se_event_cb = NULL;
1969         g_nfc_context.on_se_event_user_data = NULL;
1970 }
1971
1972 int nfc_manager_set_se_transaction_event_cb(nfc_se_transaction_event_cb callback, void *user_data){
1973         if( callback == NULL )
1974                 return _return_invalid_param(__func__);
1975         g_nfc_context.on_se_transaction_event_cb = callback;
1976         g_nfc_context.on_se_transaction_event_user_data = user_data;
1977         return 0;
1978 }
1979
1980 void nfc_manager_unset_se_transaction_event_cb(void){
1981         g_nfc_context.on_se_transaction_event_cb = NULL;
1982         g_nfc_context.on_se_transaction_event_user_data = NULL;
1983 }