Git init
[framework/multimedia/libmm-sound.git] / mm_sound_keysound.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
30 #include <sys/stat.h>
31 #include <errno.h>
32
33 #include <mm_types.h>
34 #include <mm_error.h>
35 #include <mm_message.h>
36 #include <mm_debug.h>
37 #include <mm_sound.h>
38 #include <mm_sound_private.h>
39
40 #define KEYTONE_PATH "/tmp/keytone"             /* Keytone pipe path */
41 #define FILE_FULL_PATH 1024                             /* File path lenth */
42 typedef struct {
43         char filename[1024];
44         int vol_type;
45 }ipc_t;
46
47 EXPORT_API
48 int mm_sound_play_keysound(const char *filename, const volume_type_t vol_type)
49 {
50         int err = MM_ERROR_NONE;
51         int fd = -1;
52         int size = 0;
53         ipc_t data = {{0,},};
54
55         if(!filename)
56                 return MM_ERROR_SOUND_INVALID_FILE;
57
58         fd = open(filename, O_RDONLY);
59         if(fd == -1)
60         {
61                 debug_error("file open failed with %s\n", strerror(errno));
62                 switch(errno)
63                 {
64                 case EACCES:
65                         return MM_ERROR_SOUND_FILE_NOT_FOUND;
66                 case EMFILE:
67                 case ENFILE:
68                         return MM_ERROR_SOUND_INVALID_STATE;
69                 default:
70                         return MM_ERROR_SOUND_INTERNAL;
71                 }
72         }
73         close(fd); fd = -1;
74
75         fd = open(KEYTONE_PATH, O_WRONLY | O_NONBLOCK);
76         if (fd == -1)
77         {
78                 debug_error("Fail to open pipe\n");
79                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
80         }
81         data.vol_type = vol_type;
82
83         strncpy(data.filename, filename, FILE_FULL_PATH);
84         debug_msg("The file name [%s]\n", data.filename);
85         size = sizeof(ipc_t);
86         err = write(fd, &data, size);
87         if(err<0)
88         {
89                 debug_error("Fail to write data: %s\n", strerror(err));
90                 close(fd);
91                 return MM_ERROR_SOUND_INTERNAL;
92
93         }
94         close(fd);
95
96         debug_fleave();
97         return MM_ERROR_NONE;
98 }
99
100