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