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.
31 #define API __attribute__ ((visibility("default")))
34 static unsigned int init_cnt;
35 static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
37 static int restart_callback()
39 const struct device_ops *dev;
41 dev = find_device(FEEDBACK_TYPE_VIBRATION);
43 _E("Not supported device : type(FEEDBACK_TYPE_VIBRATION)"); //LCOV_EXCL_LINE
51 API int feedback_initialize(void)
55 pthread_mutex_lock(&fmutex);
57 _E("there is no valid profile module."); //LCOV_EXCL_LINE
58 pthread_mutex_unlock(&fmutex);
59 return FEEDBACK_ERROR_NOT_SUPPORTED;
63 pthread_mutex_unlock(&fmutex);
64 return FEEDBACK_ERROR_NONE;
67 ret = register_signal_handler(restart_callback);
69 _E("Fail to register signal handler: %d", ret);
71 /* initialize device */
74 /* initialize profile feature */
78 pthread_mutex_unlock(&fmutex);
79 return FEEDBACK_ERROR_NONE;
82 API int feedback_deinitialize(void)
86 pthread_mutex_lock(&fmutex);
88 pthread_mutex_unlock(&fmutex);
89 return FEEDBACK_ERROR_NOT_INITIALIZED;
93 pthread_mutex_unlock(&fmutex);
94 return FEEDBACK_ERROR_NONE;
97 ret = unregister_signal_handler(restart_callback);
99 _E("Fail to unregister signal handler: %d", ret);
101 /* deinitialize device */
104 /* deinitialize profile feature */
108 pthread_mutex_unlock(&fmutex);
109 return FEEDBACK_ERROR_NONE;
112 API int feedback_play(feedback_pattern_e pattern)
118 /* check initialize */
119 pthread_mutex_lock(&fmutex);
121 _E("Not initialized"); //LCOV_EXCL_LINE
122 pthread_mutex_unlock(&fmutex);
123 return FEEDBACK_ERROR_NOT_INITIALIZED;
125 pthread_mutex_unlock(&fmutex);
127 if (pattern <= FEEDBACK_PATTERN_NONE ||
128 pattern >= profile->max_pattern) {
129 _E("Invalid parameter : pattern(%d)", pattern);
130 return FEEDBACK_ERROR_INVALID_PARAMETER;
133 /* if you need to switch pattern */
134 if (profile->get_switched_pattern) {
135 result = profile->get_switched_pattern(pattern, &switched);
137 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
138 profile->str_pattern(pattern), profile->str_pattern(switched));
143 /* play all device */
144 err = devices_play(pattern);
146 * devices_play() returns error even if all devices are failed.
147 * It means if to play anything is successful,
148 * this function regards as success.
151 return FEEDBACK_ERROR_NOT_SUPPORTED;
153 return FEEDBACK_ERROR_OPERATION_FAILED;
155 return FEEDBACK_ERROR_NONE;
158 API int feedback_play_type(feedback_type_e type, feedback_pattern_e pattern)
160 const struct device_ops *dev;
165 /* check initialize */
166 pthread_mutex_lock(&fmutex);
168 _E("Not initialized"); //LCOV_EXCL_LINE
169 pthread_mutex_unlock(&fmutex);
170 return FEEDBACK_ERROR_NOT_INITIALIZED;
172 pthread_mutex_unlock(&fmutex);
174 if (type <= FEEDBACK_TYPE_NONE ||
175 type >= profile->max_type) {
176 _E("Invalid parameter : type(%d)", type);
177 return FEEDBACK_ERROR_INVALID_PARAMETER;
180 if (pattern <= FEEDBACK_PATTERN_NONE ||
181 pattern >= profile->max_pattern) {
182 _E("Invalid parameter : pattern(%d)", pattern);
183 return FEEDBACK_ERROR_INVALID_PARAMETER;
186 /* if you need to switch pattern */
187 if (profile->get_switched_pattern) {
188 result = profile->get_switched_pattern(pattern, &switched);
190 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
191 profile->str_pattern(pattern), profile->str_pattern(switched));
196 /* play proper device */
197 dev = find_device(type);
199 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
200 return FEEDBACK_ERROR_NOT_SUPPORTED;
203 err = dev->play(pattern);
205 return FEEDBACK_ERROR_NOT_SUPPORTED;
206 else if (err == -ECOMM || err == -EACCES)
207 return FEEDBACK_ERROR_PERMISSION_DENIED;
209 return FEEDBACK_ERROR_OPERATION_FAILED;
211 return FEEDBACK_ERROR_NONE;
214 API int feedback_stop(void)
218 /* check initialize */
219 pthread_mutex_lock(&fmutex);
221 _E("Not initialized"); //LCOV_EXCL_LINE
222 pthread_mutex_unlock(&fmutex);
223 return FEEDBACK_ERROR_NOT_INITIALIZED;
225 pthread_mutex_unlock(&fmutex);
227 /* stop all device */
228 err = devices_stop();
230 return FEEDBACK_ERROR_NOT_SUPPORTED;
231 else if (err == -ECOMM || err == -EACCES)
232 return FEEDBACK_ERROR_PERMISSION_DENIED;
234 return FEEDBACK_ERROR_OPERATION_FAILED;
236 return FEEDBACK_ERROR_NONE;
239 API int feedback_is_supported_pattern(feedback_type_e type, feedback_pattern_e pattern, bool *status)
241 const struct device_ops *dev;
247 /* check initialize */
248 pthread_mutex_lock(&fmutex);
250 _E("Not initialized"); //LCOV_EXCL_LINE
251 pthread_mutex_unlock(&fmutex);
252 return FEEDBACK_ERROR_NOT_INITIALIZED;
254 pthread_mutex_unlock(&fmutex);
257 _E("Invalid parameter : status(NULL)");
258 return FEEDBACK_ERROR_INVALID_PARAMETER;
261 if (type <= FEEDBACK_TYPE_NONE ||
262 type >= profile->max_type) {
263 _E("Invalid parameter : type(%d)", type);
264 return FEEDBACK_ERROR_INVALID_PARAMETER;
267 if (pattern <= FEEDBACK_PATTERN_NONE ||
268 pattern >= profile->max_pattern) {
269 _E("Invalid parameter : pattern(%d)", pattern);
270 return FEEDBACK_ERROR_INVALID_PARAMETER;
273 /* if you need to switch pattern */
274 if (profile->get_switched_pattern) {
275 result = profile->get_switched_pattern(pattern, &switched);
277 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
278 profile->str_pattern(pattern), profile->str_pattern(switched));
283 /* play proper device */
284 dev = find_device(type);
286 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
287 return FEEDBACK_ERROR_NOT_SUPPORTED;
290 err = dev->is_supported(pattern, &supported);
292 return FEEDBACK_ERROR_NOT_SUPPORTED; //LCOV_EXCL_LINE System Error
293 else if (err == -ECOMM || err == -EACCES)
294 return FEEDBACK_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE System Error
296 _E("fail to invoke is_supported() : pattern(%d)", pattern); //LCOV_EXCL_LINE
297 return FEEDBACK_ERROR_OPERATION_FAILED;
302 return FEEDBACK_ERROR_NONE;
306 API int feedback_play_type_by_name(char *type, char *pattern)
308 feedback_type_e etype;
309 feedback_pattern_e epattern;
313 if (!type || !pattern) {
314 _E("Invalid parameter : type(%x), pattern(%x)", type, pattern);
315 return FEEDBACK_ERROR_INVALID_PARAMETER;
318 type_max = profile->max_type;
319 for (etype = 0; etype < type_max; ++etype) {
320 if (!strncmp(type, profile->str_type[etype], strlen(type)))
324 if (etype == type_max) {
325 _E("Invalid parameter : type(%s)", type);
326 return FEEDBACK_ERROR_INVALID_PARAMETER;
329 pattern_max = profile->max_pattern;
330 for (epattern = 0; epattern < pattern_max; ++epattern) {
331 if (!strncmp(pattern, profile->str_pattern(epattern), strlen(pattern)))
335 if (epattern == pattern_max) {
336 _E("Invalid parameter : pattern(%s)", pattern);
337 return FEEDBACK_ERROR_INVALID_PARAMETER;
340 return feedback_play_type(etype, epattern);
343 API int feedback_play_internal(feedback_pattern_internal_e pattern)
349 /* check initialize */
350 pthread_mutex_lock(&fmutex);
352 _E("Not initialized"); //LCOV_EXCL_LINE
353 pthread_mutex_unlock(&fmutex);
354 return FEEDBACK_ERROR_NOT_INITIALIZED;
356 pthread_mutex_unlock(&fmutex);
358 if (pattern <= FEEDBACK_PATTERN_NONE ||
359 pattern >= profile->max_pattern) {
360 _E("Invalid parameter : pattern(%d)", pattern);
361 return FEEDBACK_ERROR_INVALID_PARAMETER;
364 /* if you need to switch pattern */
365 if (profile->get_switched_pattern) {
366 result = profile->get_switched_pattern(pattern, &switched);
368 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
369 profile->str_pattern(pattern), profile->str_pattern(switched));
374 /* play all device */
375 err = devices_play(pattern);
377 * devices_play() returns error even if all devices are failed.
378 * It means if to play anything is successful,
379 * this function regards as success.
382 return FEEDBACK_ERROR_NOT_SUPPORTED;
384 return FEEDBACK_ERROR_OPERATION_FAILED;
386 return FEEDBACK_ERROR_NONE;
389 API int feedback_play_type_internal(feedback_type_e type, feedback_pattern_internal_e pattern)
391 const struct device_ops *dev;
396 /* check initialize */
397 pthread_mutex_lock(&fmutex);
399 _E("Not initialized"); //LCOV_EXCL_LINE
400 pthread_mutex_unlock(&fmutex);
401 return FEEDBACK_ERROR_NOT_INITIALIZED;
403 pthread_mutex_unlock(&fmutex);
405 if (type <= FEEDBACK_TYPE_NONE ||
406 type >= profile->max_type) {
407 _E("Invalid parameter : type(%d)", type);
408 return FEEDBACK_ERROR_INVALID_PARAMETER;
411 if (pattern <= FEEDBACK_PATTERN_INTERNAL_NONE ||
412 pattern >= profile->max_pattern) {
413 _E("Invalid parameter : pattern(%d)", pattern);
414 return FEEDBACK_ERROR_INVALID_PARAMETER;
417 /* if you need to switch pattern */
418 if (profile->get_switched_pattern) {
419 result = profile->get_switched_pattern(pattern, &switched);
421 _W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
422 profile->str_pattern(pattern), profile->str_pattern(switched));
427 /* play proper device */
428 dev = find_device(type);
430 _E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
431 return FEEDBACK_ERROR_NOT_SUPPORTED;
434 err = dev->play(pattern);
436 return FEEDBACK_ERROR_NOT_SUPPORTED;
437 else if (err == -ECOMM || err == -EACCES)
438 return FEEDBACK_ERROR_PERMISSION_DENIED;
440 return FEEDBACK_ERROR_OPERATION_FAILED;
442 return FEEDBACK_ERROR_NONE;
445 API int feedback_get_resource_path(feedback_type_e type, feedback_pattern_e pattern, char** path)
447 const struct device_ops *dev;
448 char buf[PATH_MAX] = {0,};
452 _E("Invalid parameter : path(NULL)");
453 return FEEDBACK_ERROR_INVALID_PARAMETER;
456 if (type <= FEEDBACK_TYPE_NONE ||
457 type >= profile->max_type) {
458 _E("Invalid parameter : type(%d)", type);
459 return FEEDBACK_ERROR_INVALID_PARAMETER;
462 if (type == FEEDBACK_TYPE_VIBRATION)
463 return FEEDBACK_ERROR_NOT_SUPPORTED;
465 if (pattern <= FEEDBACK_PATTERN_NONE ||
466 pattern >= profile->max_pattern) {
467 _E("Invalid parameter : pattern(%d)", pattern);
468 return FEEDBACK_ERROR_INVALID_PARAMETER;
471 /* proper device get path */
472 dev = find_device(type);
474 err = dev->get_path(pattern, buf, sizeof(buf));
476 return FEEDBACK_ERROR_OPERATION_FAILED;
480 return FEEDBACK_ERROR_NONE;
483 API int feedback_set_resource_path(feedback_type_e type, feedback_pattern_e pattern, char *path)
485 const struct device_ops *dev;
488 if (type <= FEEDBACK_TYPE_NONE ||
489 type >= profile->max_type) {
490 _E("Invalid parameter : type(%d)", type);
491 return FEEDBACK_ERROR_INVALID_PARAMETER;
494 if (pattern <= FEEDBACK_PATTERN_NONE ||
495 pattern >= profile->max_pattern) {
496 _E("Invalid parameter : pattern(%d)", pattern);
497 return FEEDBACK_ERROR_INVALID_PARAMETER;
500 if (type == FEEDBACK_TYPE_VIBRATION) {
501 _E("Not supported type"); //LCOV_EXCL_LINE
502 return FEEDBACK_ERROR_NOT_SUPPORTED;
505 /* proper device set path */
506 dev = find_device(type);
508 err = dev->set_path(pattern, path);
510 return FEEDBACK_ERROR_OPERATION_FAILED;
513 return FEEDBACK_ERROR_NONE;