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