Remove the validation check of volume level in the client side
[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 <fcntl.h>
27 #include <vconf.h>
28 #include <stdlib.h>
29
30 #include <semaphore.h>
31
32 #include <mm_error.h>
33 #include <mm_debug.h>
34
35 #define KEYTONE_PATH        "/tmp/keytone"  /* Keytone pipe path */
36 #define FILE_FULL_PATH 1024                             /* File path lenth */
37 #define MAX_RETRY 40
38 #define RETRY_INTERVAL_USEC 50000
39
40 #define ROLE_NAME_LEN 64                                /* Role name length */
41 #define VOLUME_GAIN_TYPE_LEN 64         /* Volume gain type length */
42
43 typedef struct {
44         char filename[FILE_FULL_PATH];
45         char role[ROLE_NAME_LEN];
46         char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
47 } ipc_t;
48
49 #define MMSOUND_STRNCPY(dst, src, size) \
50 do { \
51         if (src != NULL && dst != NULL && size > 0) { \
52                 strncpy(dst, src, size - 1); \
53                 dst[size - 1] = '\0'; \
54         } else if (dst == NULL) { \
55                 debug_error("STRNCPY ERROR: Destination String is NULL"); \
56         } \
57         else if (size <= 0) { \
58                 debug_error("STRNCPY ERROR: Destination String is NULL"); \
59         } \
60         else { \
61                 debug_error("STRNCPY ERROR: Destination String is NULL"); \
62         } \
63 } while (0)
64
65 static void unlink_if_symbolic_link(const char *path)
66 {
67         int ret = 0;
68         char *resolved_path = NULL;
69
70         if (path == NULL)
71                 return;
72
73         /* return if it does not exist */
74         if ((ret = access(path, F_OK)))
75                 return;
76
77         if ((resolved_path = realpath(path, NULL))) {
78                 /* assume that the path paramether is an absolute path */
79                 if (strcmp(path, resolved_path)) {
80                         debug_warning("unexpected symbolic link!, unlink the symbolic link(%s) to the resolved path(%s)", path, resolved_path);
81                         unlink(path);
82                 }
83                 free(resolved_path);
84         } else {
85                 char str_error[256];
86                 strerror_r(errno, str_error, sizeof(str_error));
87                 debug_warning("failed to realpath() for path:%s, err:%s", path, str_error);
88         }
89 }
90
91 EXPORT_API
92 int mm_sound_boot_ready(int timeout_sec)
93 {
94         struct timespec ts;
95         sem_t* sem = NULL;
96
97         debug_msg("[BOOT] check for sync....");
98         if ((sem = sem_open("booting-sound", O_CREAT, 0660, 0)) == SEM_FAILED) {
99                 debug_error("error creating sem : %d", errno);
100                 return -1;
101         }
102
103         debug_msg("[BOOT] start to wait ready....timeout is set to %d sec", timeout_sec);
104         clock_gettime(CLOCK_REALTIME, &ts);
105         ts.tv_sec += timeout_sec;
106
107         if (sem_timedwait(sem, &ts) == -1) {
108                 if (errno == ETIMEDOUT)
109                         debug_warning("[BOOT] timeout!");
110         } else {
111                 debug_msg("[BOOT] ready wait success!!!!");
112                 sem_post(sem);
113         }
114
115         return 0;
116 }
117
118 EXPORT_API
119 int mm_sound_boot_play_sound(char* path)
120 {
121         int err = 0;
122         int fd = -1;
123         int size = 0;
124         ipc_t data = { { 0, }, { 0, }, { 0, } };
125
126         debug_msg("[BOOT] play boot sound [%s]!!!!", path);
127         if (path == NULL)
128                 return MM_ERROR_SOUND_INVALID_FILE;
129
130         /* Check whether file exists */
131         fd = open(path, O_RDONLY);
132         if (fd == -1) {
133                 char str_error[256];
134                 int errsv = errno;
135                 strerror_r(errsv, str_error, sizeof(str_error));
136                 debug_error("file open failed with [%s][%d]", str_error, errsv);
137                 switch (errsv) {
138                 case ENOENT:
139                         return MM_ERROR_SOUND_FILE_NOT_FOUND;
140                 default:
141                         return MM_ERROR_SOUND_INTERNAL;
142                 }
143         }
144         close(fd);
145         fd = -1;
146
147         unlink_if_symbolic_link(KEYTONE_PATH);
148         /* Open PIPE */
149         fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
150         if (fd == -1) {
151                 debug_error("Fail to open pipe");
152                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
153         }
154
155         MMSOUND_STRNCPY(data.filename, path, FILE_FULL_PATH);
156         MMSOUND_STRNCPY(data.role, "system", ROLE_NAME_LEN);
157         MMSOUND_STRNCPY(data.volume_gain_type, "booting", VOLUME_GAIN_TYPE_LEN);
158
159         debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]", data.filename, data.role, data.volume_gain_type);
160         size = sizeof(ipc_t);
161
162         /* Write to PIPE */
163         err = write(fd, &data, size);
164         if (err < 0) {
165                 char str_error[256];
166                 strerror_r(errno, str_error, sizeof(str_error));
167                 debug_error("Fail to write data: [%s][%d]", str_error, errno);
168                 close(fd);
169                 return MM_ERROR_SOUND_INTERNAL;
170         }
171         /* Close PIPE */
172         close(fd);
173
174         return MM_ERROR_NONE;
175 }