fixup! Do not raise() inside signal handler
[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
29 #include <semaphore.h>
30
31 #include <mm_error.h>
32 #include <mm_debug.h>
33
34 #define KEYTONE_PATH        "/tmp/keytone"  /* Keytone pipe path */
35 #define FILE_FULL_PATH 1024                             /* File path lenth */
36 #define MAX_RETRY 40
37 #define RETRY_INTERVAL_USEC 50000
38
39 #define ROLE_NAME_LEN 64                                /* Role name length */
40 #define VOLUME_GAIN_TYPE_LEN 64         /* Volume gain type length */
41
42 typedef struct {
43         char filename[FILE_FULL_PATH];
44         char role[ROLE_NAME_LEN];
45         char volume_gain_type[VOLUME_GAIN_TYPE_LEN];
46 } ipc_t;
47
48 #define MMSOUND_STRNCPY(dst,src,size)\
49 do { \
50         if(src != NULL && dst != NULL && size > 0) {\
51                 strncpy(dst,src,size-1); \
52                 dst[size-1] = '\0';\
53         } else if(dst == NULL) {       \
54                 debug_error("STRNCPY ERROR: Destination String is NULL"); \
55         }       \
56         else if(size <= 0) {      \
57                 debug_error("STRNCPY ERROR: Destination String is NULL"); \
58         }       \
59         else {    \
60                 debug_error("STRNCPY ERROR: Destination String is NULL"); \
61         }       \
62 } while(0)
63
64 EXPORT_API
65 int mm_sound_boot_ready(int timeout_sec)
66 {
67         struct timespec ts;
68         sem_t* sem = NULL;
69
70         debug_msg("[BOOT] check for sync....");
71         if ((sem = sem_open ("booting-sound", O_CREAT, 0660, 0))== SEM_FAILED) {
72                 debug_error ("error creating sem : %d", errno);
73                 return -1;
74         }
75
76         debug_msg("[BOOT] start to wait ready....timeout is set to %d sec", timeout_sec);
77         clock_gettime(CLOCK_REALTIME, &ts);
78         ts.tv_sec += timeout_sec;
79
80         if (sem_timedwait(sem, &ts) == -1) {
81                 if (errno == ETIMEDOUT)
82                         debug_warning("[BOOT] timeout!");
83         } else {
84                 debug_msg("[BOOT] ready wait success!!!!");
85                 sem_post(sem);
86         }
87
88         return 0;
89 }
90
91 EXPORT_API
92 int mm_sound_boot_play_sound(char* path)
93 {
94         int err = 0;
95         int fd = -1;
96         int size = 0;
97         ipc_t data = {{0,},{0,},{0,}};
98
99         debug_msg("[BOOT] play boot sound [%s]!!!!", path);
100         if (path == NULL)
101                 return MM_ERROR_SOUND_INVALID_FILE;
102
103         /* Check whether file exists */
104         fd = open(path, O_RDONLY);
105         if (fd == -1) {
106                 char str_error[256];
107                 strerror_r(errno, str_error, sizeof(str_error));
108                 debug_error("file open failed with [%s][%d]", str_error, errno);
109                 switch (errno) {
110                 case ENOENT:
111                         return MM_ERROR_SOUND_FILE_NOT_FOUND;
112                 default:
113                         return MM_ERROR_SOUND_INTERNAL;
114                 }
115         }
116         close(fd);
117         fd = -1;
118
119         /* Open PIPE */
120         fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
121         if (fd == -1) {
122                 debug_error("Fail to open pipe");
123                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
124         }
125
126         MMSOUND_STRNCPY(data.filename, path, FILE_FULL_PATH);
127         MMSOUND_STRNCPY(data.role, "system", ROLE_NAME_LEN);
128         MMSOUND_STRNCPY(data.volume_gain_type, "booting", VOLUME_GAIN_TYPE_LEN);
129
130         debug_msg("filepath=[%s], role=[%s], volume_gain_type=[%s]", data.filename, data.role, data.volume_gain_type);
131         size = sizeof(ipc_t);
132
133         /* Write to PIPE */
134         err = write(fd, &data, size);
135         if (err < 0) {
136                 char str_error[256];
137                 strerror_r(errno, str_error, sizeof(str_error));
138                 debug_error("Fail to write data: [%s][%d]", str_error, errno);
139                 close(fd);
140                 return MM_ERROR_SOUND_INTERNAL;
141         }
142         /* Close PIPE */
143         close(fd);
144
145         return MM_ERROR_NONE;
146 }