3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #define API __attribute__ ((visibility("default")))
33 static unsigned int init_cnt;
34 static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
36 API int feedback_initialize(void)
38 pthread_mutex_lock(&fmutex);
40 _E("there is no valid profile module."); //LCOV_EXCL_LINE
41 pthread_mutex_unlock(&fmutex);
42 return FEEDBACK_ERROR_NOT_SUPPORTED;
46 pthread_mutex_unlock(&fmutex);
47 return FEEDBACK_ERROR_NONE;
50 /* initialize device */
53 /* initialize profile feature */
57 pthread_mutex_unlock(&fmutex);
58 return FEEDBACK_ERROR_NONE;
61 API int feedback_deinitialize(void)
63 pthread_mutex_lock(&fmutex);
65 pthread_mutex_unlock(&fmutex);
66 return FEEDBACK_ERROR_NOT_INITIALIZED;
70 pthread_mutex_unlock(&fmutex);
71 return FEEDBACK_ERROR_NONE;
74 /* deinitialize device */
77 /* deinitialize profile feature */
81 pthread_mutex_unlock(&fmutex);
82 return FEEDBACK_ERROR_NONE;
85 API int feedback_play(feedback_pattern_e pattern)
91 /* check initialize */
92 pthread_mutex_lock(&fmutex);
94 _E("Not initialized"); //LCOV_EXCL_LINE
95 pthread_mutex_unlock(&fmutex);
96 return FEEDBACK_ERROR_NOT_INITIALIZED;
98 pthread_mutex_unlock(&fmutex);
100 if (pattern <= FEEDBACK_PATTERN_NONE ||
101 pattern >= profile->max_pattern) {
102 _E("Invalid parameter : pattern(%d)", pattern);
103 return FEEDBACK_ERROR_INVALID_PARAMETER;
106 /* if you need to switch pattern */
107 if (profile->get_switched_pattern) {
108 result = profile->get_switched_pattern(pattern, &switched);
110 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
111 profile->str_pattern[pattern], profile->str_pattern[switched]);
116 /* play all device */
117 err = devices_play(pattern);
119 * devices_play() returns error even if all devices are failed.
120 * It means if to play anything is successful,
121 * this function regards as success.
124 return FEEDBACK_ERROR_NOT_SUPPORTED;
126 return FEEDBACK_ERROR_OPERATION_FAILED;
128 return FEEDBACK_ERROR_NONE;
131 API int feedback_play_type(feedback_type_e type, feedback_pattern_e pattern)
133 const struct device_ops *dev;
138 /* check initialize */
139 pthread_mutex_lock(&fmutex);
141 _E("Not initialized"); //LCOV_EXCL_LINE
142 pthread_mutex_unlock(&fmutex);
143 return FEEDBACK_ERROR_NOT_INITIALIZED;
145 pthread_mutex_unlock(&fmutex);
147 if (type <= FEEDBACK_TYPE_NONE ||
148 type >= profile->max_type) {
149 _E("Invalid parameter : type(%d)", type);
150 return FEEDBACK_ERROR_INVALID_PARAMETER;
153 if (pattern <= FEEDBACK_PATTERN_NONE ||
154 pattern >= profile->max_pattern) {
155 _E("Invalid parameter : pattern(%d)", pattern);
156 return FEEDBACK_ERROR_INVALID_PARAMETER;
159 /* if you need to switch pattern */
160 if (profile->get_switched_pattern) {
161 result = profile->get_switched_pattern(pattern, &switched);
163 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
164 profile->str_pattern[pattern], profile->str_pattern[switched]);
169 /* play proper device */
170 dev = find_device(type);
172 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
173 return FEEDBACK_ERROR_NOT_SUPPORTED;
176 err = dev->play(pattern);
178 return FEEDBACK_ERROR_NOT_SUPPORTED;
179 else if (err == -ECOMM || err == -EACCES)
180 return FEEDBACK_ERROR_PERMISSION_DENIED;
182 return FEEDBACK_ERROR_OPERATION_FAILED;
184 return FEEDBACK_ERROR_NONE;
187 API int feedback_stop(void)
191 /* check initialize */
192 pthread_mutex_lock(&fmutex);
194 _E("Not initialized"); //LCOV_EXCL_LINE
195 pthread_mutex_unlock(&fmutex);
196 return FEEDBACK_ERROR_NOT_INITIALIZED;
198 pthread_mutex_unlock(&fmutex);
200 /* stop all device */
201 err = devices_stop();
203 return FEEDBACK_ERROR_NOT_SUPPORTED;
204 else if (err == -ECOMM)
205 return FEEDBACK_ERROR_PERMISSION_DENIED;
207 return FEEDBACK_ERROR_OPERATION_FAILED;
209 return FEEDBACK_ERROR_NONE;
212 API int feedback_is_supported_pattern(feedback_type_e type, feedback_pattern_e pattern, bool *status)
214 const struct device_ops *dev;
220 /* check initialize */
221 pthread_mutex_lock(&fmutex);
223 _E("Not initialized"); //LCOV_EXCL_LINE
224 pthread_mutex_unlock(&fmutex);
225 return FEEDBACK_ERROR_NOT_INITIALIZED;
227 pthread_mutex_unlock(&fmutex);
230 _E("Invalid parameter : status(NULL)");
231 return FEEDBACK_ERROR_INVALID_PARAMETER;
234 if (type <= FEEDBACK_TYPE_NONE ||
235 type >= profile->max_type) {
236 _E("Invalid parameter : type(%d)", type);
237 return FEEDBACK_ERROR_INVALID_PARAMETER;
240 if (pattern <= FEEDBACK_PATTERN_NONE ||
241 pattern >= profile->max_pattern) {
242 _E("Invalid parameter : pattern(%d)", pattern);
243 return FEEDBACK_ERROR_INVALID_PARAMETER;
246 /* if you need to switch pattern */
247 if (profile->get_switched_pattern) {
248 result = profile->get_switched_pattern(pattern, &switched);
250 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
251 profile->str_pattern[pattern], profile->str_pattern[switched]);
256 /* play proper device */
257 dev = find_device(type);
259 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
260 return FEEDBACK_ERROR_NOT_SUPPORTED;
263 err = dev->is_supported(pattern, &supported);
265 return FEEDBACK_ERROR_NOT_SUPPORTED; //LCOV_EXCL_LINE System Error
266 else if (err == -ECOMM || err == -EACCES)
267 return FEEDBACK_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE System Error
269 _E("fail to invoke is_supported() : pattern(%s)", profile->str_pattern[pattern]); //LCOV_EXCL_LINE
270 return FEEDBACK_ERROR_OPERATION_FAILED;
275 return FEEDBACK_ERROR_NONE;
278 //LCOV_EXCL_START Internal APIs. TODO Will make iUTC
280 API int feedback_play_type_by_name(char *type, char *pattern)
282 feedback_type_e etype;
283 feedback_pattern_e epattern;
287 if (!type || !pattern) {
288 _E("Invalid parameter : type(%x), pattern(%x)", type, pattern);
289 return FEEDBACK_ERROR_INVALID_PARAMETER;
292 type_max = profile->max_type;
293 for (etype = 0; etype < type_max; ++etype) {
294 if (!strncmp(type, profile->str_type[etype], strlen(type)))
298 if (etype == type_max) {
299 _E("Invalid parameter : type(%s)", type);
300 return FEEDBACK_ERROR_INVALID_PARAMETER;
303 pattern_max = profile->max_pattern;
304 for (epattern = 0; epattern < pattern_max; ++epattern) {
305 if (!strncmp(pattern, profile->str_pattern[epattern], strlen(pattern)))
309 if (epattern == pattern_max) {
310 _E("Invalid parameter : pattern(%s)", pattern);
311 return FEEDBACK_ERROR_INVALID_PARAMETER;
314 return feedback_play_type(etype, epattern);
317 API int feedback_get_resource_path(feedback_type_e type, feedback_pattern_e pattern, char** path)
319 const struct device_ops *dev;
320 char buf[PATH_MAX] = {0,};
324 _E("Invalid parameter : path(NULL)");
325 return FEEDBACK_ERROR_INVALID_PARAMETER;
328 if (type <= FEEDBACK_TYPE_NONE ||
329 type >= profile->max_type) {
330 _E("Invalid parameter : type(%d)", type);
331 return FEEDBACK_ERROR_INVALID_PARAMETER;
334 if (type == FEEDBACK_TYPE_VIBRATION)
335 return FEEDBACK_ERROR_NOT_SUPPORTED;
337 if (pattern <= FEEDBACK_PATTERN_NONE ||
338 pattern >= profile->max_pattern) {
339 _E("Invalid parameter : pattern(%d)", pattern);
340 return FEEDBACK_ERROR_INVALID_PARAMETER;
343 /* proper device get path */
344 dev = find_device(type);
346 err = dev->get_path(pattern, buf, sizeof(buf));
348 return FEEDBACK_ERROR_OPERATION_FAILED;
352 return FEEDBACK_ERROR_NONE;
355 API int feedback_set_resource_path(feedback_type_e type, feedback_pattern_e pattern, char *path)
357 const struct device_ops *dev;
360 if (type <= FEEDBACK_TYPE_NONE ||
361 type >= profile->max_type) {
362 _E("Invalid parameter : type(%d)", type);
363 return FEEDBACK_ERROR_INVALID_PARAMETER;
366 if (pattern <= FEEDBACK_PATTERN_NONE ||
367 pattern >= profile->max_pattern) {
368 _E("Invalid parameter : pattern(%d)", pattern);
369 return FEEDBACK_ERROR_INVALID_PARAMETER;
372 if (type == FEEDBACK_TYPE_VIBRATION) {
373 _E("Not supported type");
374 return FEEDBACK_ERROR_NOT_SUPPORTED;
377 /* proper device set path */
378 dev = find_device(type);
380 err = dev->set_path(pattern, path);
382 return FEEDBACK_ERROR_OPERATION_FAILED;
385 return FEEDBACK_ERROR_NONE;