Remove build error in Tizen 3.0
[platform/core/connectivity/nfc-manager-neard.git] / daemon / net_nfc_server_util.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 #define _GNU_SOURCE
18 #include <string.h>
19 #include <dirent.h>
20 #include <errno.h>
21 #include <pwd.h>
22 #include <grp.h>
23 #include <sys/stat.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <pthread.h>
27 #include <unistd.h>
28 #include <curl/curl.h>
29 #include <glib.h>
30 #include <openssl/evp.h>
31 #include <openssl/bio.h>
32 #include <openssl/buffer.h>
33
34 #include <vconf.h>
35 #include <feedback.h>
36 #include <wav_player.h>
37 #include <appsvc.h>
38 #include <aul.h>
39 #include <bundle_internal.h>
40
41 #ifdef USE_X11
42 #include <Ecore_X.h>
43 #endif
44 #ifdef USE_WAYLAND
45 #include <Ecore.h>
46 #include <Ecore_Wayland.h>
47 #endif
48
49
50 #include "net_nfc_typedef.h"
51 #include "net_nfc_typedef_internal.h"
52 #include "net_nfc_debug_internal.h"
53 #include "net_nfc_util_defines.h"
54 #include "net_nfc_util_internal.h"
55 #include "net_nfc_util_ndef_message.h"
56 #include "net_nfc_util_ndef_record.h"
57 #include "net_nfc_server_context.h"
58 #include "net_nfc_server_se.h"
59 #include "net_nfc_server_util.h"
60
61 #define NET_NFC_MANAGER_SOUND_PATH_TASK_START \
62                 "/usr/share/nfc-manager-daemon/sounds/Operation_sdk.wav"
63
64 #define NET_NFC_MANAGER_SOUND_PATH_TASK_END \
65                 "/usr/share/nfc-manager-daemon/sounds/Operation_sdk.wav"
66
67 #define NET_NFC_MANAGER_SOUND_PATH_TASK_ERROR \
68                 "/usr/share/nfc-manager-daemon/sounds/Operation_sdk.wav"
69
70 #define OSP_K_COND              "__OSP_COND_NAME__"
71 #define OSP_K_COND_TYPE         "nfc"
72 #define OSP_K_LAUNCH_TYPE       "__OSP_LAUNCH_TYPE__"
73
74 static const char osp_launch_type_condition[] = "condition";
75
76 static inline int _mkdir_recursive(char *path, mode_t mode)
77 {
78         char *found = path;
79
80         while (found)
81         {
82                 char tmp_ch;
83
84                 if ('\0' == found[1])
85                         break;
86
87                 found = strchr(found+1, '/');
88
89                 if (found)
90                 {
91                         int ret;
92                         DIR *exist;
93
94                         tmp_ch = *found;
95                         *found = '\0';
96
97                         exist = opendir(path);
98                         if (NULL == exist)
99                         {
100                                 if (ENOENT == errno)
101                                 {
102                                         ret = mkdir(path, mode);
103                                         if (-1 == ret)
104                                         {
105                                                 char buf[1024];
106                                                 NFC_ERR("mkdir() Failed(%s)", strerror_r(errno, buf, sizeof(buf)));
107                                                 return -1;
108                                         }
109                                 }
110                                 else
111                                 {
112                                         char buf[1024];
113                                         NFC_ERR("opendir() Failed(%s)", strerror_r(errno, buf, sizeof(buf)));
114                                         return -1;
115                                 }
116                         }
117                         else
118                         {
119                                 closedir(exist);
120                         }
121                         *found = tmp_ch;
122                 }
123                 else
124                 {
125                         mkdir(path, mode);
126                 }
127         }
128         return 0;
129 }
130
131 static bool _net_nfc_app_util_change_file_owner_permission(FILE *file)
132 {
133         char *buffer = NULL;
134         size_t buffer_len = 0;
135         struct group grp = { 0, };
136         struct passwd pwd = { 0, };
137         struct group *gr_inhouse = NULL;
138         struct passwd *pw_inhouse = NULL;
139
140         RETV_IF(NULL == file, false);
141
142         /* change permission */
143         fchmod(fileno(file), 0777);
144
145         /* change owner */
146         /* get passwd id */
147         buffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
148         if (buffer_len == -1)
149                 buffer_len = 16384;
150
151         _net_nfc_util_alloc_mem(buffer, buffer_len);
152         if (NULL == buffer)
153                 return false;
154
155         getpwnam_r("inhouse", &pwd, buffer, buffer_len, &pw_inhouse);
156         _net_nfc_util_free_mem(buffer);
157
158         /* get group id */
159         buffer_len = sysconf(_SC_GETGR_R_SIZE_MAX);
160         if (buffer_len == -1)
161                 buffer_len = 16384;
162
163         _net_nfc_util_alloc_mem(buffer, buffer_len);
164         if (NULL == buffer)
165                 return false;
166
167         getgrnam_r("inhouse", &grp, buffer, buffer_len, &gr_inhouse);
168         _net_nfc_util_free_mem(buffer);
169
170         if ((pw_inhouse != NULL) && (gr_inhouse != NULL))
171         {
172                 if (fchown(fileno(file), pw_inhouse->pw_uid, gr_inhouse->gr_gid) < 0)
173                         NFC_ERR("failed to change owner");
174         }
175
176         return true;
177 }
178
179 static net_nfc_error_e net_nfc_app_util_store_ndef_message(data_s *data)
180 {
181         int ret;
182         FILE *fp = NULL;
183         char file_name[1024] = { 0, };
184         net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
185
186         RETV_IF(NULL == data, NET_NFC_NULL_PARAMETER);
187
188         /* check and make directory */
189         snprintf(file_name, sizeof(file_name), "%s/%s", NET_NFC_MANAGER_DATA_PATH,
190                         NET_NFC_MANAGER_DATA_PATH_MESSAGE);
191
192         ret = _mkdir_recursive(file_name, 0755);
193         if (-1 == ret)
194         {
195                 NFC_ERR("_mkdir_recursive() Failed");
196                 return NET_NFC_UNKNOWN_ERROR;
197         }
198
199         /* create file */
200         snprintf(file_name, sizeof(file_name), "%s/%s/%s", NET_NFC_MANAGER_DATA_PATH,
201                         NET_NFC_MANAGER_DATA_PATH_MESSAGE, NET_NFC_MANAGER_NDEF_FILE_NAME);
202         SECURE_LOGD("file path : %s", file_name);
203
204         unlink(file_name);
205
206         if ((fp = fopen(file_name, "w")) != NULL)
207         {
208                 int length = 0;
209
210                 if ((length = fwrite(data->buffer, 1, data->length, fp)) > 0)
211                 {
212                         NFC_DBG("[%d] bytes is written", length);
213
214                         _net_nfc_app_util_change_file_owner_permission(fp);
215
216                         fflush(fp);
217                         fsync(fileno(fp));
218
219                         result = NET_NFC_OK;
220                 }
221                 else
222                 {
223                         NFC_ERR("write is failed = [%d]", data->length);
224                         result = NET_NFC_UNKNOWN_ERROR;
225                 }
226
227                 fclose(fp);
228         }
229
230         return result;
231 }
232
233 static bool _net_nfc_app_util_get_operation_from_record(
234                 ndef_record_s *record, char *operation, size_t length)
235 {
236         bool result = false;
237         char *op_text = NULL;
238
239         RETV_IF(NULL == record, result);
240         RETV_IF(NULL == operation, result);
241         RETV_IF(0 == length, result);
242
243         switch (record->TNF)
244         {
245         case NET_NFC_RECORD_WELL_KNOWN_TYPE :
246                 op_text = "http://tizen.org/appcontrol/operation/nfc/wellknown";
247                 break;
248
249         case NET_NFC_RECORD_MIME_TYPE :
250                 op_text = "http://tizen.org/appcontrol/operation/nfc/mime";
251                 break;
252
253         case NET_NFC_RECORD_URI : /* Absolute URI */
254                 op_text = "http://tizen.org/appcontrol/operation/nfc/uri";
255                 break;
256
257         case NET_NFC_RECORD_EXTERNAL_RTD : /* external type */
258         if(strncmp((char*)record->type_s.buffer, NET_NFC_APPLICATION_RECORD,
259                         record->type_s.length)==0)
260         {
261                 //use APPSVC_OPERATION_VIEW in case of Selective App launch
262                 op_text = APPSVC_OPERATION_VIEW;
263         }
264         else
265         {
266                 op_text = "http://tizen.org/appcontrol/operation/nfc/external";
267         }
268         break;
269
270         case NET_NFC_RECORD_EMPTY : /* empty_tag */
271                 op_text = "http://tizen.org/appcontrol/operation/nfc/empty";
272                 break;
273
274         case NET_NFC_RECORD_UNKNOWN : /* unknown msg. discard it */
275         case NET_NFC_RECORD_UNCHAGNED : /* RFU msg. discard it */
276         default :
277                 break;
278         }
279
280         if (op_text != NULL)
281         {
282                 snprintf(operation, length, "%s", op_text);
283                 result = TRUE;
284         }
285
286         return result;
287 }
288
289 static void _to_lower_utf_8(char *str)
290 {
291         while (*str != 0)
292         {
293                 if (*str >= 'A' && *str <= 'Z')
294                         *str += ('a' - 'A');
295
296                 str++;
297         }
298 }
299
300 static void _to_lower(int type, char *str)
301 {
302         _to_lower_utf_8(str);
303 }
304
305 static bool _net_nfc_app_util_get_mime_from_record(
306                 ndef_record_s *record, char *mime, size_t length)
307 {
308         bool result = false;
309
310         RETV_IF(NULL == record, result);
311         RETV_IF(NULL == mime, result);
312         RETV_IF(0 == length, result);
313
314         switch (record->TNF)
315         {
316         case NET_NFC_RECORD_WELL_KNOWN_TYPE :
317                 {
318                         if (record->type_s.buffer == NULL || record->type_s.length == 0 ||
319                                         record->payload_s.buffer == NULL || record->payload_s.length == 0)
320                         {
321                                 NFC_ERR("Broken NDEF Message [NET_NFC_RECORD_WELL_KNOWN_TYPE]");
322                                 break;
323                         }
324
325                         if (record->type_s.length == 1 && record->type_s.buffer[0] == 'U')
326                         {
327                                 snprintf(mime, length, "U/0x%02x", record->payload_s.buffer[0]);
328                         }
329                         else
330                         {
331                                 memcpy(mime, record->type_s.buffer, record->type_s.length);
332                                 mime[record->type_s.length] = '\0';
333
334                                 strncat(mime, "/*", 2);
335                                 mime[record->type_s.length + 2] = '\0';
336                         }
337
338                         result = true;
339                 }
340                 break;
341
342         case NET_NFC_RECORD_MIME_TYPE :
343                 {
344                         int len = 0;
345                         char *token = NULL;
346                         char *buffer = NULL;
347
348                         if (record->type_s.buffer == NULL || record->type_s.length == 0)
349                         {
350                                 NFC_ERR("Broken NDEF Message [NET_NFC_RECORD_MIME_TYPE]");
351                                 break;
352                         }
353
354                         /* get mime type */
355                         _net_nfc_util_alloc_mem(buffer, record->type_s.length + 1);
356                         if (NULL == buffer)
357                         {
358                                 NFC_ERR("_net_nfc_manager_util_alloc_mem return NULL");
359                                 break;
360                         }
361                         memcpy(buffer, record->type_s.buffer, record->type_s.length);
362
363                         token = strchr(buffer, ';');
364                         if (token != NULL)
365                                 len = MIN(token - buffer, length - 1);
366                         else
367                                 len = MIN(strlen(buffer), length - 1);
368
369                         strncpy(mime, buffer, len);
370                         mime[len] = '\0';
371
372                         _to_lower(0, mime);
373
374                         _net_nfc_util_free_mem(buffer);
375
376                         result = true;
377                 }
378                 break;
379
380         case NET_NFC_RECORD_URI : /* Absolute URI */
381         case NET_NFC_RECORD_EXTERNAL_RTD : /* external type */
382         case NET_NFC_RECORD_EMPTY :  /* empty_tag */
383                 result = true;
384                 break;
385
386         case NET_NFC_RECORD_UNKNOWN : /* unknown msg. discard it */
387         case NET_NFC_RECORD_UNCHAGNED : /* RFU msg. discard it */
388         default :
389                 break;
390         }
391
392         return result;
393 }
394
395 #ifdef USE_FULL_URI
396 static bool _net_nfc_app_util_get_uri_from_record(ndef_record_s *record, char *data, size_t length)
397 {
398         bool result = false;
399
400         RETV_IF(NULL == record, result);
401         RETV_IF(NULL == data, result);
402         RETV_IF(0 == length, result);
403
404         switch (record->TNF)
405         {
406         case NET_NFC_RECORD_WELL_KNOWN_TYPE :
407         case NET_NFC_RECORD_URI : /* Absolute URI */
408                 {
409                         char *uri = NULL;
410
411                         if (net_nfc_util_create_uri_string_from_uri_record(record, &uri) == NET_NFC_OK &&
412                                         uri != NULL)
413                         {
414                                 snprintf(data, length, "%s", uri);
415
416                                 _net_nfc_util_free_mem(uri);
417                         }
418                         result = true;
419                 }
420                 break;
421
422         case NET_NFC_RECORD_EXTERNAL_RTD : /* external type */
423                 {
424                         data_s *type = &record->type_s;
425
426                         if (type->length > 0)
427                         {
428 #if 0
429                                 char *buffer = NULL;
430                                 int len = strlen(NET_NFC_UTIL_EXTERNAL_TYPE_SCHEME);
431
432                                 _net_nfc_util_alloc_mem(buffer, type->length + len + 1);
433                                 if (buffer != NULL)
434                                 {
435                                         memcpy(buffer, NET_NFC_UTIL_EXTERNAL_TYPE_SCHEME, len);
436                                         memcpy(buffer + len, type->buffer, type->length);
437
438                                         /* to lower case!! */
439                                         strlwr(buffer);
440
441                                         NFC_DBG("uri record : %s", buffer);
442                                         snprintf(data, length, "%s", buffer);
443
444                                         _net_nfc_util_free_mem(buffer);
445                                 }
446 #else
447                                 int len = MIN(type->length, length - 1);
448                                 memcpy(data, type->buffer, len);
449                                 data[len] = 0;
450
451                                 /* to lower case!! */
452                                 _to_lower(0, data);
453
454                                 result = true;
455 #endif
456                         }
457                 }
458                 break;
459
460         case NET_NFC_RECORD_MIME_TYPE :
461         case NET_NFC_RECORD_EMPTY : /* empy msg. discard it */
462                 result = true;
463                 break;
464
465         case NET_NFC_RECORD_UNKNOWN : /* unknown msg. discard it */
466         case NET_NFC_RECORD_UNCHAGNED : /* RFU msg. discard it */
467         default :
468                 break;
469         }
470
471         return result;
472 }
473 #endif
474
475 static bool _net_nfc_app_util_get_data_from_record(ndef_record_s *record, char *data, size_t length)
476 {
477         bool result = false;
478
479         RETV_IF(NULL == record, result);
480         RETV_IF(NULL == data, result);
481         RETV_IF(0 == length, result);
482
483         switch (record->TNF)
484         {
485         case NET_NFC_RECORD_WELL_KNOWN_TYPE :
486                 {
487                         if (record->type_s.buffer == NULL || record->type_s.length == 0
488                                         || record->payload_s.buffer == NULL || record->payload_s.length == 0)
489                         {
490                                 NFC_ERR("Broken NDEF Message [NET_NFC_RECORD_WELL_KNOWN_TYPE]");
491                                 break;
492                         }
493
494                         if (record->type_s.length == 1 && record->type_s.buffer[0] == 'T')
495                         {
496                                 uint8_t *buffer_temp = record->payload_s.buffer;
497                                 uint32_t buffer_length = record->payload_s.length;
498
499                                 int index = (buffer_temp[0] & 0x3F) + 1;
500                                 int text_length = buffer_length - index;
501
502                                 memcpy(data, &(buffer_temp[index]), MIN(text_length, length));
503                         }
504
505                         result = true;
506                 }
507                 break;
508
509         case NET_NFC_RECORD_MIME_TYPE :
510         case NET_NFC_RECORD_URI : /* Absolute URI */
511                 break;
512         case NET_NFC_RECORD_EXTERNAL_RTD : /* external type */
513                 {
514                         NFC_DBG("NDEF Message with  external type");
515                         if(strncmp((char*)record->type_s.buffer, NET_NFC_APPLICATION_RECORD,
516                         record->type_s.length)==0)
517                         {
518                                 uint8_t *buffer_temp = record->payload_s.buffer;
519                                 uint32_t buffer_length = record->payload_s.length;
520                                 if(buffer_length > length)
521                                 {
522                                         result= false;
523                                 }
524                                 else
525                                 {
526                                         //Copy application id into data
527                                         memcpy(data,buffer_temp,MIN(buffer_length,length));
528                                         result = true;
529                                 }
530                         }
531                 }
532                 break;
533         case NET_NFC_RECORD_EMPTY : /* empy msg. discard it */
534                 result = true;
535                 break;
536
537         case NET_NFC_RECORD_UNKNOWN : /* unknown msg. discard it */
538         case NET_NFC_RECORD_UNCHAGNED : /* RFU msg. discard it */
539         default :
540                 break;
541         }
542
543         return result;
544 }
545
546 net_nfc_error_e net_nfc_app_util_process_ndef(data_s *data)
547 {
548         int ret = 0;
549         char mime[2048] = { 0, };
550         char text[2048] = { 0, };
551         ndef_message_s *msg = NULL;
552         char operation[2048] = { 0, };
553         net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
554 #ifdef USE_FULL_URI
555         char uri[2048] = { 0, };
556 #endif
557
558         RETV_IF(NULL == data, NET_NFC_NULL_PARAMETER);
559         RETV_IF(NULL == data->buffer, NET_NFC_NULL_PARAMETER);
560         RETV_IF(0 == data->length, NET_NFC_NULL_PARAMETER);
561
562         /* create file */
563         if ((result = net_nfc_app_util_store_ndef_message(data)) != NET_NFC_OK)
564         {
565                 NFC_ERR("net_nfc_app_util_store_ndef_message failed [%d]", result);
566                 return result;
567         }
568
569         /* check state of launch popup */
570         if(net_nfc_app_util_check_launch_state() == NET_NFC_NO_LAUNCH_APP_SELECT)
571         {
572                 NFC_DBG("skip launch popup!!!");
573                 result = NET_NFC_OK;
574                 return result;
575         }
576
577         if (net_nfc_util_create_ndef_message(&msg) != NET_NFC_OK)
578         {
579                 NFC_ERR("memory alloc fail..");
580                 return NET_NFC_ALLOC_FAIL;
581         }
582
583         /* parse ndef message and fill appsvc data */
584         if ((result = net_nfc_util_convert_rawdata_to_ndef_message(data, msg)) != NET_NFC_OK)
585         {
586                 NFC_ERR("net_nfc_app_util_store_ndef_message failed [%d]", result);
587                 goto ERROR;
588         }
589
590         if (_net_nfc_app_util_get_operation_from_record(msg->records, operation,
591                                 sizeof(operation)) == FALSE)
592         {
593                 NFC_ERR("_net_nfc_app_util_get_operation_from_record failed [%d]", result);
594                 result = NET_NFC_UNKNOWN_ERROR;
595                 goto ERROR;
596         }
597
598         if (_net_nfc_app_util_get_mime_from_record(msg->records, mime, sizeof(mime)) == FALSE)
599         {
600                 NFC_ERR("_net_nfc_app_util_get_mime_from_record failed [%d]", result);
601                 result = NET_NFC_UNKNOWN_ERROR;
602                 goto ERROR;
603         }
604 #ifdef USE_FULL_URI
605         if (_net_nfc_app_util_get_uri_from_record(msg->records, uri, sizeof(uri)) == FALSE)
606         {
607                 NFC_ERR("_net_nfc_app_util_get_uri_from_record failed [%d]", result);
608                 result = NET_NFC_UNKNOWN_ERROR;
609                 goto ERROR;
610         }
611 #endif
612         /* launch appsvc */
613         if (_net_nfc_app_util_get_data_from_record(msg->records, text, sizeof(text)) == FALSE)
614         {
615                 NFC_ERR("_net_nfc_app_util_get_data_from_record failed [%d]", result);
616                 result = NET_NFC_UNKNOWN_ERROR;
617                 goto ERROR;
618         }
619
620         ret = net_nfc_app_util_appsvc_launch(operation, uri, mime, text);
621 #if 0
622         if (ret == APPSVC_RET_ENOMATCH)
623         {
624                 /* TODO : check again */
625                 ret = net_nfc_app_util_appsvc_launch(operation, uri, mime, text);
626         }
627 #endif
628
629         NFC_DBG("net_nfc_app_util_appsvc_launch return %d", ret);
630
631         result = NET_NFC_OK;
632
633 ERROR :
634         net_nfc_util_free_ndef_message(msg);
635
636         return result;
637 }
638
639
640 static bool net_nfc_app_util_is_dir(const char* path_name)
641 {
642         struct stat statbuf = { 0 };
643
644         if (stat(path_name, &statbuf) == -1)
645                 return false;
646
647         if (S_ISDIR(statbuf.st_mode) != 0)
648                 return true;
649         else
650                 return false;
651 }
652
653 void net_nfc_app_util_clean_storage(const char* src_path)
654 {
655         DIR* dir = NULL;
656         char path[1024] = { 0 };
657         struct dirent* ent = NULL;
658
659         RET_IF((dir = opendir(src_path)) == NULL);
660
661         while ((ent = readdir(dir)) != NULL)
662         {
663                 if (strncmp(ent->d_name, ".", 1) == 0 || strncmp(ent->d_name, "..", 2) == 0)
664                 {
665                         continue;
666                 }
667                 else
668                 {
669                         snprintf(path, 1024, "%s/%s", src_path, ent->d_name);
670
671                         if (net_nfc_app_util_is_dir(path) != false)
672                         {
673                                 net_nfc_app_util_clean_storage(path);
674                                 rmdir(path);
675                         }
676                         else
677                         {
678                                 unlink(path);
679                         }
680                 }
681         }
682
683         closedir(dir);
684
685         rmdir(src_path);
686 }
687
688 void net_nfc_app_util_aul_launch_app(char* package_name, bundle* kb)
689 {
690         int result = 0;
691         if((result = aul_launch_app(package_name, kb)) < 0)
692         {
693                 switch(result)
694                 {
695                 case AUL_R_EINVAL:
696                         NFC_ERR("aul launch error : AUL_R_EINVAL");
697                         break;
698                 case AUL_R_ECOMM:
699                         NFC_ERR("aul launch error : AUL_R_ECOM");
700                         break;
701                 case AUL_R_ERROR:
702                         NFC_ERR("aul launch error : AUL_R_ERROR");
703                         break;
704                 default:
705                         NFC_ERR("aul launch error : unknown ERROR");
706                         break;
707                 }
708         }
709         else
710         {
711                 NFC_DBG("success to launch [%s]", package_name);
712         }
713 }
714
715 int net_nfc_app_util_appsvc_launch(const char *operation, const char *uri, const char *mime, const char *data)
716 {
717         int result = -1;
718
719         bundle *bd = NULL;
720         bool specific_app_launch = false;
721         bd = bundle_create();
722         if (NULL == bd)
723                 return result;
724
725         if (operation != NULL && strlen(operation) > 0)
726         {
727                 NFC_DBG("operation : %s", operation);
728                 appsvc_set_operation(bd, operation);
729                 if(strncmp(operation, APPSVC_OPERATION_VIEW,strlen(APPSVC_OPERATION_VIEW))==0)
730                 {
731                         appsvc_set_appid(bd, data);
732                         specific_app_launch = true;
733                         goto LAUNCH;
734                 }
735         }
736
737         if (uri != NULL && strlen(uri) > 0)
738         {
739                 NFC_DBG("uri : %s", uri);
740                 appsvc_set_uri(bd, uri);
741         }
742
743         if (mime != NULL && strlen(mime) > 0)
744         {
745                 NFC_DBG("mime : %s", mime);
746                 appsvc_set_mime(bd, mime);
747         }
748
749         if (data != NULL && strlen(data) > 0)
750         {
751                 NFC_DBG("data : %s", data);
752                 appsvc_add_data(bd, "data", data);
753         }
754
755         bundle_add(bd, OSP_K_COND, OSP_K_COND_TYPE);
756         bundle_add(bd, OSP_K_LAUNCH_TYPE, osp_launch_type_condition);
757
758 LAUNCH:
759         result = appsvc_run_service(bd, 0, NULL, NULL);
760
761         /*if the app could not be found*/
762         if(specific_app_launch && result == APPSVC_RET_ENOMATCH)
763         {
764                 /*TODO: tizen store launch*/
765         }
766
767         bundle_free(bd);
768
769         return result;
770 }
771
772 void _binary_to_string(uint8_t *buffer, uint32_t len, char *out_buf, uint32_t max_len)
773 {
774         int current = 0;
775
776         RET_IF(0 == len);
777         RET_IF(0 == max_len);
778         RET_IF(NULL == buffer);
779         RET_IF(NULL == out_buf);
780
781         while (len > 0 && current < max_len)
782         {
783                 current += snprintf(out_buf + current, max_len - current, "%02X", *(buffer++));
784                 len--;
785         }
786 }
787
788 void _string_to_binary(const char *input, uint8_t *output, uint32_t *length)
789 {
790         int temp;
791         int current = 0;
792
793         RET_IF(NULL == input);
794         RET_IF(NULL == length);
795         RET_IF(0 == *length);
796         RET_IF(NULL == output);
797         NFC_DBG("_string_to_binary ");
798
799         /* strlen("nfc://secure/aid/") = 17 */
800
801         input += 17;
802
803         while (*input && (current < *length))
804         {
805                 temp = (*input++) - '0';
806
807                 if(temp > 9)
808                         temp -= 7;
809
810                 if(current % 2)
811                         output[current / 2] += temp;
812                 else
813                         output[current / 2] = temp << 4;
814
815                 current++;
816         }
817
818         *length = current / 2;
819 }
820
821 int net_nfc_app_util_launch_se_transaction_app(
822                 net_nfc_secure_element_type_e se_type,
823                 uint8_t *aid,
824                 uint32_t aid_len,
825                 uint8_t *param,
826                 uint32_t param_len)
827 {
828         bundle *bd = NULL;
829
830         /* launch */
831         bd = bundle_create();
832
833         appsvc_set_operation(bd, "http://tizen.org/appcontrol/operation/nfc/transaction");
834
835         /* convert aid to aid string */
836         if (aid != NULL && aid_len > 0)
837         {
838                 char temp_string[1024] = { 0, };
839                 char aid_string[1024] = { 0, };
840
841                 _binary_to_string(aid, aid_len, temp_string, sizeof(temp_string));
842
843                 switch(se_type)
844                 {
845                 case SECURE_ELEMENT_TYPE_UICC:
846                         snprintf(aid_string, sizeof(aid_string), "nfc://secure/SIM1/aid/%s", temp_string);
847                         break;
848
849                 case SECURE_ELEMENT_TYPE_ESE:
850                         snprintf(aid_string, sizeof(aid_string), "nfc://secure/eSE/aid/%s", temp_string);
851                         break;
852                 default:
853                         snprintf(aid_string, sizeof(aid_string), "nfc://secure/aid/%s", temp_string);
854                         break;
855                 }
856
857                 NFC_DBG("aid_string : %s", aid_string);
858                 appsvc_set_uri(bd, aid_string);
859         }
860
861         if (param != NULL && param_len > 0)
862         {
863                 char param_string[1024] = { 0, };
864
865                 _binary_to_string(param, param_len, param_string, sizeof(param_string));
866                 NFC_DBG("param_string : %s", param_string);
867                 appsvc_add_data(bd, "data", param_string);
868         }
869
870         appsvc_run_service(bd, 0, NULL, NULL);
871         bundle_free(bd);
872
873         return 0;
874 }
875
876 int net_nfc_app_util_encode_base64(uint8_t *buffer, uint32_t buf_len, char *result, uint32_t max_result)
877 {
878         int ret = -1;
879         BUF_MEM *bptr;
880         BIO *b64, *bmem;
881
882         RETV_IF(NULL == buffer, ret);
883         RETV_IF(0 == buf_len, ret);
884         RETV_IF(NULL == result, ret);
885         RETV_IF(0 == max_result, ret);
886
887         /* base 64 */
888         b64 = BIO_new(BIO_f_base64());
889         BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
890         bmem = BIO_new(BIO_s_mem());
891         b64 = BIO_push(b64, bmem);
892
893         BIO_write(b64, buffer, buf_len);
894         BIO_flush(b64);
895         BIO_get_mem_ptr(b64, &bptr);
896
897         memset(result, 0, max_result);
898         memcpy(result, bptr->data, MIN(bptr->length, max_result - 1));
899
900         BIO_free_all(b64);
901
902         ret = 0;
903
904         return ret;
905 }
906
907 int net_nfc_app_util_decode_base64(const char *buffer, uint32_t buf_len, uint8_t *result, uint32_t *res_len)
908 {
909         int ret = -1;
910         char *temp = NULL;
911
912         RETV_IF(NULL == buffer, ret);
913         RETV_IF(0 == buf_len, ret);
914         RETV_IF(NULL == result, ret);
915         RETV_IF(NULL == res_len, ret);
916         RETV_IF(0 == *res_len, ret);
917
918         _net_nfc_util_alloc_mem(temp, buf_len);
919         if (temp != NULL)
920         {
921                 BIO *b64, *bmem;
922                 uint32_t temp_len;
923
924                 b64 = BIO_new(BIO_f_base64());
925                 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
926                 bmem = BIO_new_mem_buf((void *)buffer, buf_len);
927                 bmem = BIO_push(b64, bmem);
928
929                 temp_len = BIO_read(bmem, temp, buf_len);
930
931                 BIO_free_all(bmem);
932
933                 memset(result, 0, *res_len);
934                 memcpy(result, temp, MIN(temp_len, *res_len));
935
936                 *res_len = MIN(temp_len, *res_len);
937
938                 _net_nfc_util_free_mem(temp);
939
940                 ret = 0;
941         }
942         else
943         {
944                 NFC_ERR("alloc failed");
945         }
946
947         return ret;
948 }
949
950 pid_t net_nfc_app_util_get_focus_app_pid()
951 {
952 #ifdef USE_X11
953         Ecore_X_Window focus;
954
955         ecore_x_init(":0");
956
957         focus = ecore_x_window_focus_get();
958         if (ecore_x_netwm_pid_get(focus, &pid))
959                 return pid;
960 #endif
961 #ifdef USE_WAYLAND // TO_DO_WAYLAND
962         NFC_DBG("To implement: Wayland support");
963 #endif
964         return -1;
965 }
966
967 bool net_nfc_app_util_check_launch_state()
968 {
969         bool result = false;
970         pid_t focus_app_pid;
971         net_nfc_launch_popup_state_e popup_state;
972
973         focus_app_pid = net_nfc_app_util_get_focus_app_pid();
974
975         popup_state = net_nfc_server_gdbus_get_client_popup_state(focus_app_pid);
976
977         if(NET_NFC_NO_LAUNCH_APP_SELECT == popup_state)
978                 result = true;
979
980         return result;
981 }
982
983 static void _play_sound_callback(int id, void *data)
984 {
985         NFC_DBG("_play_sound_callback");
986
987         if (WAV_PLAYER_ERROR_NONE != wav_player_stop(id))
988                 NFC_ERR("wav_player_stop failed");
989 }
990
991 void net_nfc_manager_util_play_sound(net_nfc_sound_type_e sound_type)
992 {
993         int bSoundOn = 0;
994         int bVibrationOn = 0;
995         int ret;
996
997         if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &bSoundOn) != 0)
998         {
999                 NFC_ERR("vconf_get_bool failed for Sound");
1000                 return;
1001         }
1002
1003         if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &bVibrationOn) != 0)
1004         {
1005                 NFC_ERR("vconf_get_bool failed for Vibration");
1006                 return;
1007         }
1008
1009         if ((sound_type > NET_NFC_TASK_ERROR) || (sound_type < NET_NFC_TASK_START))
1010         {
1011                 NFC_ERR("Invalid Sound Type");
1012                 return;
1013         }
1014
1015         if (bVibrationOn)
1016         {
1017                 NFC_DBG("Play Vibration");
1018
1019                 ret = feedback_initialize();
1020                 if (ret == FEEDBACK_ERROR_NONE)
1021                 {
1022                         ret = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_SIP);
1023                         if (ret != FEEDBACK_ERROR_NONE)
1024                                 NFC_ERR("Failed to play vibration(%d)", ret);
1025                         feedback_deinitialize();
1026                 }
1027                 else
1028                 {
1029                         NFC_ERR("Failed to init vibration(%d)", ret);
1030                 }
1031         }
1032
1033         if (bSoundOn)
1034         {
1035                 char *sound_path = NULL;
1036
1037                 NFC_DBG("Play Sound");
1038
1039                 switch (sound_type)
1040                 {
1041                 case NET_NFC_TASK_START :
1042                         sound_path = strdup(NET_NFC_MANAGER_SOUND_PATH_TASK_START);
1043                         break;
1044                 case NET_NFC_TASK_END :
1045                         sound_path = strdup(NET_NFC_MANAGER_SOUND_PATH_TASK_END);
1046                         break;
1047                 case NET_NFC_TASK_ERROR :
1048                         sound_path = strdup(NET_NFC_MANAGER_SOUND_PATH_TASK_ERROR);
1049                         break;
1050                 }
1051
1052                 if (sound_path != NULL)
1053                 {
1054                         if (WAV_PLAYER_ERROR_NONE ==
1055                                 wav_player_start(sound_path, SOUND_TYPE_MEDIA, _play_sound_callback,
1056                                         NULL, NULL))
1057                         {
1058                                 NFC_DBG("wav_player_start success");
1059                         }
1060
1061                         _net_nfc_util_free_mem(sound_path);
1062                 }
1063                 else
1064                 {
1065                         NFC_ERR("Invalid Sound Path");
1066                 }
1067         }
1068 }