seperate sound device from feedback internal code
[platform/core/system/libsvi.git] / src / feedback.c
1 /*
2  * libfeedback
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18
19 #include <stdio.h>
20 #include <string.h>
21 #include "feedback.h"
22 #include "feedback-internal.h"
23 #include "feedback-log.h"
24 #include "devices.h"
25
26 #define MAX_PATH_LENGTH      256
27 #define NOT_ASSIGNED         NULL
28
29 #ifndef API
30 #define API __attribute__ ((visibility("default")))
31 #endif
32
33 static feedback_h feedback_handle = NOT_ASSIGNED;
34
35 API int feedback_initialize()
36 {
37         int err = -1;
38
39         if (feedback_handle != NOT_ASSIGNED) {
40                 FEEDBACK_LOG("Already initialized");
41                 return FEEDBACK_ERROR_NONE;
42         }
43
44         /* initialize device */
45         devices_init();
46
47         err = feedback_init(&feedback_handle);
48         if (FEEDBACK_FAILED(err)) {
49                 FEEDBACK_ERROR("feedback_init is failed");
50                 return FEEDBACK_ERROR_OPERATION_FAILED;
51         }
52
53         return FEEDBACK_ERROR_NONE;
54 }
55
56 API int feedback_deinitialize()
57 {
58         int err = -1;
59
60         if (feedback_handle == NOT_ASSIGNED) {
61                 FEEDBACK_ERROR("Not initialized");
62                 return FEEDBACK_ERROR_NOT_INITIALIZED;
63         }
64
65         /* deinitialize device */
66         devices_exit();
67
68         err = feedback_fini(feedback_handle);
69         if (FEEDBACK_FAILED(err)) {
70                 FEEDBACK_ERROR("feedback_fini is failed");
71                 return FEEDBACK_ERROR_OPERATION_FAILED;
72         }
73
74         feedback_handle = NOT_ASSIGNED;
75         return FEEDBACK_ERROR_NONE;
76 }
77
78 API int feedback_play(feedback_pattern_e pattern)
79 {
80         int err = -1;
81
82         if (feedback_handle == NOT_ASSIGNED) {
83                 FEEDBACK_ERROR("Not initialized");
84                 return FEEDBACK_ERROR_NOT_INITIALIZED;
85         }
86
87         if (pattern < FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
88                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
89                 return FEEDBACK_ERROR_INVALID_PARAMETER;
90         }
91
92         if (pattern == FEEDBACK_PATTERN_NONE) {
93                 FEEDBACK_LOG("pattern is NONE");
94                 return FEEDBACK_ERROR_NONE;
95         }
96
97         /* play all device */
98         devices_play(pattern);
99
100         err = feedback_play_vibration(feedback_handle, pattern);
101         if (FEEDBACK_FAILED(err)) {
102                 FEEDBACK_ERROR("feedback_play_vibration is failed");
103                 return FEEDBACK_ERROR_OPERATION_FAILED;
104         }
105
106         return FEEDBACK_ERROR_NONE;
107 }
108
109 API int feedback_play_type(feedback_type_e type, feedback_pattern_e pattern)
110 {
111         const struct device_ops *dev;
112         int err = -1;
113
114         if (feedback_handle == NOT_ASSIGNED) {
115                 FEEDBACK_ERROR("Not initialized");
116                 return FEEDBACK_ERROR_NOT_INITIALIZED;
117         }
118
119         if (type <= FEEDBACK_TYPE_NONE || type >= FEEDBACK_TYPE_END) {
120                 FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
121                 return FEEDBACK_ERROR_INVALID_PARAMETER;
122         }
123
124         if (pattern < FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
125                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
126                 return FEEDBACK_ERROR_INVALID_PARAMETER;
127         }
128
129         if (pattern == FEEDBACK_PATTERN_NONE) {
130                 FEEDBACK_LOG("pattern is NONE");
131                 return FEEDBACK_ERROR_NONE;
132         }
133
134         switch(type) {
135                 case FEEDBACK_TYPE_SOUND:
136                         dev = find_device(type);
137                         if (dev) {
138                                 err = dev->play(pattern);
139                                 if (err < 0)
140                                         FEEDBACK_ERROR("feedback_play_sound is failed");
141                         }
142                         break;
143                 case FEEDBACK_TYPE_VIBRATION:
144                         err = feedback_play_vibration(feedback_handle, pattern);
145                         if (FEEDBACK_FAILED(err))
146                                 FEEDBACK_ERROR("feedback_play(type:%d) is failed", type);
147                         break;
148                 default:
149                         FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
150                 return FEEDBACK_ERROR_INVALID_PARAMETER;
151         }
152
153         return FEEDBACK_ERROR_NONE;
154 }
155
156 API int feedback_get_resource_path(feedback_type_e type, feedback_pattern_e pattern, char** path)
157 {
158         const struct device_ops *dev;
159         int err = -1;
160         char buf[MAX_PATH_LENGTH] = {0,};
161
162         if (path == NULL) {
163                 FEEDBACK_ERROR("Invalid parameter : path(NULL)");
164                 return FEEDBACK_ERROR_INVALID_PARAMETER;
165         }
166
167         if (type <= FEEDBACK_TYPE_NONE || type >= FEEDBACK_TYPE_END) {
168                 FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
169                 return FEEDBACK_ERROR_INVALID_PARAMETER;
170         }
171
172         if (pattern <= FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
173                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
174                 return FEEDBACK_ERROR_INVALID_PARAMETER;
175         }
176
177         if (type == FEEDBACK_TYPE_SOUND) {
178                 dev = find_device(type);
179                 if (dev)
180                         err = dev->get_path(pattern, buf, sizeof(buf));
181         } else if (type == FEEDBACK_TYPE_VIBRATION)
182                 err = feedback_get_path(type, pattern, buf, MAX_PATH_LENGTH);
183
184         if (FEEDBACK_FAILED(err)) {
185                 FEEDBACK_ERROR("feedback_get_path is failed");
186                 return FEEDBACK_ERROR_OPERATION_FAILED;
187         }
188
189         *path = strdup(buf);
190         return FEEDBACK_ERROR_NONE;
191 }
192
193 API int feedback_set_resource_path(feedback_type_e type, feedback_pattern_e pattern, char* path)
194 {
195         const struct device_ops *dev;
196         int err = -1;
197
198         if (path == NULL) {
199                 FEEDBACK_ERROR("Invalid parameter : path(NULL)");
200                 return FEEDBACK_ERROR_INVALID_PARAMETER;
201         }
202
203         if (type <= FEEDBACK_TYPE_NONE || type >= FEEDBACK_TYPE_END) {
204                 FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
205                 return FEEDBACK_ERROR_INVALID_PARAMETER;
206         }
207
208         if (pattern <= FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
209                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
210                 return FEEDBACK_ERROR_INVALID_PARAMETER;
211         }
212
213         if (type == FEEDBACK_TYPE_SOUND) {
214                 dev = find_device(type);
215                 if (dev)
216                         err = dev->set_path(pattern, path);
217         } else if (type == FEEDBACK_TYPE_VIBRATION)
218                 err = feedback_set_path(type, pattern, path);
219
220         if (FEEDBACK_FAILED(err)) {
221                 FEEDBACK_ERROR("feedback_set_path is failed");
222                 return FEEDBACK_ERROR_OPERATION_FAILED;
223         }
224
225         return FEEDBACK_ERROR_NONE;
226 }