043edee6efde2efccd76b5f3b063b0eb893ca574
[platform/core/multimedia/libmm-sound.git] / mm_sound_bootsound.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <memory.h>
24 #include <unistd.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <vconf.h>
30
31 #include <sys/stat.h>
32 #include <errno.h>
33
34 #include <semaphore.h>
35 #include <errno.h>
36
37 #include <mm_types.h>
38 #include <mm_error.h>
39 #include <mm_message.h>
40 #include <mm_debug.h>
41 #include <mm_sound.h>
42 #include <mm_sound_private.h>
43
44 #define KEYTONE_PATH        "/tmp/keytone"  /* Keytone pipe path */
45 #define FILE_FULL_PATH 1024                             /* File path lenth */
46 #define MAX_RETRY 40
47 #define RETRY_INTERVAL_USEC 50000
48
49 #define ROLE_NAME_LEN 64                                /* Role name length */
50 #define VOLUME_GAIN_TYPE_LEN 64         /* Volume gain type length */
51
52 typedef struct {
53         char filename[FILE_FULL_PATH];
54         char role[ROLE_NAME_LEN];
55         char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
56 } ipc_t;
57
58 #define MMSOUND_STRNCPY(dst,src,size)\
59 do { \
60         if(src != NULL && dst != NULL && size > 0) {\
61                 strncpy(dst,src,size); \
62                 dst[size-1] = '\0';\
63         } else if(dst == NULL) {       \
64                 debug_error("STRNCPY ERROR: Destination String is NULL\n"); \
65         }       \
66         else if(size <= 0) {      \
67                 debug_error("STRNCPY ERROR: Destination String is NULL\n"); \
68         }       \
69         else {    \
70                 debug_error("STRNCPY ERROR: Destination String is NULL\n"); \
71         }       \
72 } while(0)
73
74 EXPORT_API
75 int mm_sound_boot_ready(int timeout_sec)
76 {
77         struct timespec ts;
78         sem_t* sem = NULL;
79
80         debug_msg("[BOOT] check for sync....");
81         if ((sem = sem_open ("booting-sound", O_CREAT, 0660, 0))== SEM_FAILED) {
82                 debug_error ("error creating sem : %d", errno);
83                 return -1;
84         }
85
86         debug_msg("[BOOT] start to wait ready....timeout is set to %d sec", timeout_sec);
87         clock_gettime(CLOCK_REALTIME, &ts);
88         ts.tv_sec += timeout_sec;
89
90         if (sem_timedwait(sem, &ts) == -1) {
91                 if (errno == ETIMEDOUT)
92                         debug_warning("[BOOT] timeout!\n");
93         } else {
94                 debug_msg("[BOOT] ready wait success!!!!");
95                 sem_post(sem);
96         }
97
98         return 0;
99 }
100
101 EXPORT_API
102 int mm_sound_boot_play_sound(char* path)
103 {
104         int err = 0;
105         int fd = -1;
106         int size = 0;
107         ipc_t data = {{0,},{0,},{0,}};
108
109         debug_msg("[BOOT] play boot sound [%s]!!!!", path);
110         if (path == NULL)
111                 return MM_ERROR_SOUND_INVALID_FILE;
112
113         /* Check whether file exists */
114         fd = open(path, O_RDONLY);
115         if (fd == -1) {
116                 debug_error("file open failed with [%s][%d]\n", strerror(errno), errno);
117                 switch (errno) {
118                 case ENOENT:
119                         return MM_ERROR_SOUND_FILE_NOT_FOUND;
120                 default:
121                         return MM_ERROR_SOUND_INTERNAL;
122                 }
123         }
124         close(fd);
125         fd = -1;
126
127         /* Open PIPE */
128         fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
129         if (fd == -1) {
130                 debug_error("Fail to open pipe\n");
131                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
132         }
133
134         MMSOUND_STRNCPY(data.filename, path, FILE_FULL_PATH);
135         MMSOUND_STRNCPY(data.role, "system", ROLE_NAME_LEN);
136         MMSOUND_STRNCPY(data.volume_gain_type, "booting", VOLUME_GAIN_TYPE_LEN);
137
138         debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]\n", data.filename, data.role, data.volume_gain_type);
139         size = sizeof(ipc_t);
140
141         /* Write to PIPE */
142         err = write(fd, &data, size);
143         if (err < 0) {
144                 debug_error("Fail to write data: [%s][%d]\n", strerror(errno), errno);
145                 close(fd);
146                 return MM_ERROR_SOUND_INTERNAL;
147         }
148         /* Close PIPE */
149         close(fd);
150
151         return MM_ERROR_NONE;
152 }