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.
25 #include "feedback-ids-internal.h"
32 #define API __attribute__ ((visibility("default")))
35 static unsigned int init_cnt;
36 static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
38 static int restart_callback()
40 const struct device_ops *dev;
42 dev = find_device(FEEDBACK_TYPE_VIBRATION);
44 _E("Not supported device : type(FEEDBACK_TYPE_VIBRATION)"); //LCOV_EXCL_LINE
52 API int feedback_initialize(void)
56 pthread_mutex_lock(&fmutex);
58 _E("there is no valid profile module."); //LCOV_EXCL_LINE
59 pthread_mutex_unlock(&fmutex);
60 return FEEDBACK_ERROR_NOT_SUPPORTED;
64 pthread_mutex_unlock(&fmutex);
65 return FEEDBACK_ERROR_NONE;
68 ret = register_signal_handler(restart_callback);
70 _E("Fail to register signal handler: %d", ret);
72 /* initialize device */
75 /* initialize profile feature */
79 pthread_mutex_unlock(&fmutex);
80 return FEEDBACK_ERROR_NONE;
83 API int feedback_deinitialize(void)
87 pthread_mutex_lock(&fmutex);
89 pthread_mutex_unlock(&fmutex);
90 return FEEDBACK_ERROR_NOT_INITIALIZED;
94 pthread_mutex_unlock(&fmutex);
95 return FEEDBACK_ERROR_NONE;
98 ret = unregister_signal_handler(restart_callback);
100 _E("Fail to unregister signal handler: %d", ret);
102 /* deinitialize device */
105 /* deinitialize profile feature */
109 pthread_mutex_unlock(&fmutex);
110 return FEEDBACK_ERROR_NONE;
113 API int feedback_play(feedback_pattern_e pattern)
119 /* check initialize */
120 pthread_mutex_lock(&fmutex);
122 _E("Not initialized"); //LCOV_EXCL_LINE
123 pthread_mutex_unlock(&fmutex);
124 return FEEDBACK_ERROR_NOT_INITIALIZED;
126 pthread_mutex_unlock(&fmutex);
128 if (pattern <= FEEDBACK_PATTERN_NONE ||
129 pattern >= profile->max_pattern) {
130 _E("Invalid parameter : pattern(%d)", pattern);
131 return FEEDBACK_ERROR_INVALID_PARAMETER;
134 /* if you need to switch pattern */
135 if (profile->get_switched_pattern) {
136 result = profile->get_switched_pattern(pattern, &switched);
138 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
139 profile->str_pattern(pattern), profile->str_pattern(switched));
144 /* play all device */
145 err = devices_play(pattern);
147 * devices_play() returns error even if all devices are failed.
148 * It means if to play anything is successful,
149 * this function regards as success.
152 return FEEDBACK_ERROR_NOT_SUPPORTED;
154 return FEEDBACK_ERROR_OPERATION_FAILED;
156 return FEEDBACK_ERROR_NONE;
159 API int feedback_play_type(feedback_type_e type, feedback_pattern_e pattern)
161 const struct device_ops *dev;
166 /* check initialize */
167 pthread_mutex_lock(&fmutex);
169 _E("Not initialized"); //LCOV_EXCL_LINE
170 pthread_mutex_unlock(&fmutex);
171 return FEEDBACK_ERROR_NOT_INITIALIZED;
173 pthread_mutex_unlock(&fmutex);
175 if (type <= FEEDBACK_TYPE_NONE ||
176 type >= profile->max_type) {
177 _E("Invalid parameter : type(%d)", type);
178 return FEEDBACK_ERROR_INVALID_PARAMETER;
181 if (pattern <= FEEDBACK_PATTERN_NONE ||
182 pattern >= profile->max_pattern) {
183 _E("Invalid parameter : pattern(%d)", pattern);
184 return FEEDBACK_ERROR_INVALID_PARAMETER;
187 /* if you need to switch pattern */
188 if (profile->get_switched_pattern) {
189 result = profile->get_switched_pattern(pattern, &switched);
191 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
192 profile->str_pattern(pattern), profile->str_pattern(switched));
197 /* play proper device */
198 dev = find_device(type);
200 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
201 return FEEDBACK_ERROR_NOT_SUPPORTED;
204 err = dev->play(pattern);
206 return FEEDBACK_ERROR_NOT_SUPPORTED;
207 else if (err == -ECOMM || err == -EACCES)
208 return FEEDBACK_ERROR_PERMISSION_DENIED;
210 return FEEDBACK_ERROR_OPERATION_FAILED;
212 return FEEDBACK_ERROR_NONE;
215 API int feedback_stop(void)
219 /* check initialize */
220 pthread_mutex_lock(&fmutex);
222 _E("Not initialized"); //LCOV_EXCL_LINE
223 pthread_mutex_unlock(&fmutex);
224 return FEEDBACK_ERROR_NOT_INITIALIZED;
226 pthread_mutex_unlock(&fmutex);
228 /* stop all device */
229 err = devices_stop();
231 return FEEDBACK_ERROR_NOT_SUPPORTED;
232 else if (err == -ECOMM || err == -EACCES)
233 return FEEDBACK_ERROR_PERMISSION_DENIED;
235 return FEEDBACK_ERROR_OPERATION_FAILED;
237 return FEEDBACK_ERROR_NONE;
240 API int feedback_is_supported_pattern(feedback_type_e type, feedback_pattern_e pattern, bool *status)
242 const struct device_ops *dev;
248 /* check initialize */
249 pthread_mutex_lock(&fmutex);
251 _E("Not initialized"); //LCOV_EXCL_LINE
252 pthread_mutex_unlock(&fmutex);
253 return FEEDBACK_ERROR_NOT_INITIALIZED;
255 pthread_mutex_unlock(&fmutex);
258 _E("Invalid parameter : status(NULL)");
259 return FEEDBACK_ERROR_INVALID_PARAMETER;
262 if (type <= FEEDBACK_TYPE_NONE ||
263 type >= profile->max_type) {
264 _E("Invalid parameter : type(%d)", type);
265 return FEEDBACK_ERROR_INVALID_PARAMETER;
268 if (pattern <= FEEDBACK_PATTERN_NONE ||
269 pattern >= profile->max_pattern) {
270 _E("Invalid parameter : pattern(%d)", pattern);
271 return FEEDBACK_ERROR_INVALID_PARAMETER;
274 /* if you need to switch pattern */
275 if (profile->get_switched_pattern) {
276 result = profile->get_switched_pattern(pattern, &switched);
278 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
279 profile->str_pattern(pattern), profile->str_pattern(switched));
284 /* play proper device */
285 dev = find_device(type);
287 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
288 return FEEDBACK_ERROR_NOT_SUPPORTED;
291 err = dev->is_supported(pattern, &supported);
293 return FEEDBACK_ERROR_NOT_SUPPORTED; //LCOV_EXCL_LINE System Error
294 else if (err == -ECOMM || err == -EACCES)
295 return FEEDBACK_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE System Error
297 _E("fail to invoke is_supported() : pattern(%d)", pattern); //LCOV_EXCL_LINE
298 return FEEDBACK_ERROR_OPERATION_FAILED;
303 return FEEDBACK_ERROR_NONE;
307 API int feedback_play_type_by_name(char *type, char *pattern)
309 feedback_type_e etype;
310 feedback_pattern_e epattern;
314 if (!type || !pattern) {
315 _E("Invalid parameter : type(%x), pattern(%x)", type, pattern);
316 return FEEDBACK_ERROR_INVALID_PARAMETER;
319 type_max = profile->max_type;
320 for (etype = 0; etype < type_max; ++etype) {
321 if (!strncmp(type, profile->str_type[etype], strlen(type)))
325 if (etype == type_max) {
326 _E("Invalid parameter : type(%s)", type);
327 return FEEDBACK_ERROR_INVALID_PARAMETER;
330 pattern_max = profile->max_pattern;
331 for (epattern = 0; epattern < pattern_max; ++epattern) {
332 if (!strncmp(pattern, profile->str_pattern(epattern), strlen(pattern)))
336 if (epattern == pattern_max) {
337 _E("Invalid parameter : pattern(%s)", pattern);
338 return FEEDBACK_ERROR_INVALID_PARAMETER;
341 return feedback_play_type(etype, epattern);
344 API int feedback_play_internal(feedback_pattern_internal_e pattern)
350 /* check initialize */
351 pthread_mutex_lock(&fmutex);
353 _E("Not initialized"); //LCOV_EXCL_LINE
354 pthread_mutex_unlock(&fmutex);
355 return FEEDBACK_ERROR_NOT_INITIALIZED;
357 pthread_mutex_unlock(&fmutex);
359 if (pattern <= FEEDBACK_PATTERN_NONE ||
360 pattern >= profile->max_pattern) {
361 _E("Invalid parameter : pattern(%d)", pattern);
362 return FEEDBACK_ERROR_INVALID_PARAMETER;
365 /* if you need to switch pattern */
366 if (profile->get_switched_pattern) {
367 result = profile->get_switched_pattern(pattern, &switched);
369 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
370 profile->str_pattern(pattern), profile->str_pattern(switched));
375 /* play all device */
376 err = devices_play(pattern);
378 * devices_play() returns error even if all devices are failed.
379 * It means if to play anything is successful,
380 * this function regards as success.
383 return FEEDBACK_ERROR_NOT_SUPPORTED;
385 return FEEDBACK_ERROR_OPERATION_FAILED;
387 return FEEDBACK_ERROR_NONE;
390 API int feedback_play_type_internal(feedback_type_e type, feedback_pattern_internal_e pattern)
392 const struct device_ops *dev;
397 /* check initialize */
398 pthread_mutex_lock(&fmutex);
400 _E("Not initialized"); //LCOV_EXCL_LINE
401 pthread_mutex_unlock(&fmutex);
402 return FEEDBACK_ERROR_NOT_INITIALIZED;
404 pthread_mutex_unlock(&fmutex);
406 if (type <= FEEDBACK_TYPE_NONE ||
407 type >= profile->max_type) {
408 _E("Invalid parameter : type(%d)", type);
409 return FEEDBACK_ERROR_INVALID_PARAMETER;
412 if (pattern <= FEEDBACK_PATTERN_INTERNAL_NONE ||
413 pattern >= profile->max_pattern) {
414 _E("Invalid parameter : pattern(%d)", pattern);
415 return FEEDBACK_ERROR_INVALID_PARAMETER;
418 /* if you need to switch pattern */
419 if (profile->get_switched_pattern) {
420 result = profile->get_switched_pattern(pattern, &switched);
422 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
423 profile->str_pattern(pattern), profile->str_pattern(switched));
428 /* play proper device */
429 dev = find_device(type);
431 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
432 return FEEDBACK_ERROR_NOT_SUPPORTED;
435 err = dev->play(pattern);
437 return FEEDBACK_ERROR_NOT_SUPPORTED;
438 else if (err == -ECOMM || err == -EACCES)
439 return FEEDBACK_ERROR_PERMISSION_DENIED;
441 return FEEDBACK_ERROR_OPERATION_FAILED;
443 return FEEDBACK_ERROR_NONE;
446 API int feedback_get_resource_path(feedback_type_e type, feedback_pattern_e pattern, char** path)
448 const struct device_ops *dev;
449 char buf[PATH_MAX] = {0,};
453 _E("Invalid parameter : path(NULL)");
454 return FEEDBACK_ERROR_INVALID_PARAMETER;
457 if (type <= FEEDBACK_TYPE_NONE ||
458 type >= profile->max_type) {
459 _E("Invalid parameter : type(%d)", type);
460 return FEEDBACK_ERROR_INVALID_PARAMETER;
463 if (type == FEEDBACK_TYPE_VIBRATION)
464 return FEEDBACK_ERROR_NOT_SUPPORTED;
466 if (pattern <= FEEDBACK_PATTERN_NONE ||
467 pattern >= profile->max_pattern) {
468 _E("Invalid parameter : pattern(%d)", pattern);
469 return FEEDBACK_ERROR_INVALID_PARAMETER;
472 /* proper device get path */
473 dev = find_device(type);
475 err = dev->get_path(pattern, buf, sizeof(buf));
477 return FEEDBACK_ERROR_OPERATION_FAILED;
481 return FEEDBACK_ERROR_NONE;
484 API int feedback_set_resource_path(feedback_type_e type, feedback_pattern_e pattern, char *path)
486 const struct device_ops *dev;
489 if (type <= FEEDBACK_TYPE_NONE ||
490 type >= profile->max_type) {
491 _E("Invalid parameter : type(%d)", type);
492 return FEEDBACK_ERROR_INVALID_PARAMETER;
495 if (pattern <= FEEDBACK_PATTERN_NONE ||
496 pattern >= profile->max_pattern) {
497 _E("Invalid parameter : pattern(%d)", pattern);
498 return FEEDBACK_ERROR_INVALID_PARAMETER;
501 if (type == FEEDBACK_TYPE_VIBRATION) {
502 _E("Not supported type"); //LCOV_EXCL_LINE
503 return FEEDBACK_ERROR_NOT_SUPPORTED;
506 /* proper device set path */
507 dev = find_device(type);
509 err = dev->set_path(pattern, path);
511 return FEEDBACK_ERROR_OPERATION_FAILED;
514 return FEEDBACK_ERROR_NONE;