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.
29 #include "feedback-ids.h"
36 #include "feedback-ids-mobile.h"
39 #include "feedback-ids-wearable.h"
42 #define HAPTIC_DEVICE 0
44 enum haptic_priority {
45 HAPTIC_PRIORITY_MIN = 0,
46 HAPTIC_PRIORITY_MIDDLE,
50 enum haptic_iteration {
51 HAPTIC_ITERATION_ONCE = 1,
52 HAPTIC_ITERATION_INFINITE = 256,
55 #define METHOD_OPEN "OpenDevice"
56 #define METHOD_CLOSE "CloseDevice"
57 #define METHOD_VIBRATE_EFFECT "VibrateEffect"
58 #define METHOD_STOP "StopDevice"
59 #define METHOD_IS_SUPPORTED "IsSupported"
60 #define METHOD_GET_EFFECT "GetEffect"
62 #define DEFAULT_DURATION 100
63 #define SIP_DURATION 60
64 #define WHITESPACE " \t"
67 static unsigned int v_handle;
69 static inline char *trim_str(char *s)
73 s += strspn(s, WHITESPACE);
76 for (t = strchr(s, 0); t > s; t--)
77 if (!strchr(WHITESPACE, t[-1]))
83 inline int is_vibration_mode(void)
88 static int haptic_open(void)
93 snprintf(buf_index, sizeof(buf_index), "%d", HAPTIC_DEVICE);
96 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
97 VIBRATOR_INTERFACE_HAPTIC, METHOD_OPEN,
101 static int haptic_close(unsigned int handle)
106 snprintf(buf_handle, sizeof(buf_handle), "%u", handle);
109 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
110 VIBRATOR_INTERFACE_HAPTIC, METHOD_CLOSE,
114 static int haptic_is_supported(char *pattern)
120 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
121 VIBRATOR_INTERFACE_HAPTIC, METHOD_IS_SUPPORTED,
125 static int haptic_vibrate_effect(unsigned int handle,
132 char buf_feedback[32];
133 char buf_priority[32];
135 snprintf(buf_handle, sizeof(buf_handle), "%u", handle);
138 snprintf(buf_feedback, sizeof(buf_feedback), "%d", feedback);
139 arr[2] = buf_feedback;
140 snprintf(buf_priority, sizeof(buf_priority), "%d", priority);
141 arr[3] = buf_priority;
143 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
144 VIBRATOR_INTERFACE_HAPTIC, METHOD_VIBRATE_EFFECT,
148 static int haptic_vibrate_stop(unsigned int handle)
153 snprintf(buf_handle, sizeof(buf_handle), "%u", handle);
156 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
157 VIBRATOR_INTERFACE_HAPTIC, METHOD_STOP,
161 static int get_priority(feedback_pattern_e pattern)
163 if (pattern >= FEEDBACK_PATTERN_TAP && pattern <= FEEDBACK_PATTERN_HW_HOLD)
164 return HAPTIC_PRIORITY_MIN;
166 return HAPTIC_PRIORITY_MIDDLE;
169 static void vibrator_init(void)
175 if (ret == -EACCES || ret == -ECOMM || ret == -EPERM) {
176 _E("haptic_open ==> FAIL!! : %d", ret); //LCOV_EXCL_LINE
177 v_handle = -EACCES; //LCOV_EXCL_LINE System Error
178 return; //LCOV_EXCL_LINE System Error
181 _E("haptic_open ==> FAIL!! : %d", ret); //LCOV_EXCL_LINE
182 v_handle = -ENOTSUP; //LCOV_EXCL_LINE System Error
183 return; //LCOV_EXCL_LINE System Error
186 /* Set vibration handle */
187 v_handle = (unsigned int)ret;
190 static void vibrator_exit(void)
194 if ((int)v_handle > 0) {
195 ret = haptic_close(v_handle);
197 _E("haptic_close is failed : %d", ret); //LCOV_EXCL_LINE
203 static int vibrator_play(feedback_pattern_e pattern)
210 if ((int)v_handle <= 0) {
212 _E("Not initialized"); //LCOV_EXCL_LINE
215 _E("Not supported vibration"); //LCOV_EXCL_LINE
216 return v_handle; //LCOV_EXCL_LINE System Error
219 if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibstatus) < 0) {
220 _D("fail to get vibration status, will work as turning off"); //LCOV_EXCL_LINE
224 if (vibstatus == 0 && profile->get_always_alert_case &&
225 !profile->get_always_alert_case(FEEDBACK_TYPE_VIBRATION, pattern)) {
226 _D("Vibration condition is OFF (vibstatus : %d)", vibstatus); //LCOV_EXCL_LINE
230 if (vibstatus && profile->get_always_off_case &&
231 profile->get_always_off_case(FEEDBACK_TYPE_VIBRATION, pattern)) {
232 _D("Vibration always off condition"); //LCOV_EXCL_LINE
236 if (pattern <= FEEDBACK_PATTERN_NONE ||
237 pattern >= profile->max_pattern) {
238 _E("Not supported vibration pattern");
242 if (profile->get_strength_type)
243 level = profile->get_strength_type(FEEDBACK_TYPE_VIBRATION, pattern);
245 level = DEFAULT_VIB_LEVEL * HAPTIC_FEEDBACK_STEP;
247 temp = strdup(profile->str_pattern[pattern]);
248 data = trim_str(temp);
251 _E("Not supported vibration pattern");
255 _D("pattern : %s", data);
257 ret = haptic_vibrate_effect(v_handle, data, level, get_priority(pattern));
260 _E("fail to play vibration"); //LCOV_EXCL_LINE
262 if (ret == -ECOMM || ret == -ENOTSUP)
263 return ret; //LCOV_EXCL_LINE System Error
267 _D("Play success! Data is %s", data);
272 static int vibrator_stop(void)
276 if ((int)v_handle <= 0) {
278 _E("Not initialized"); //LCOV_EXCL_LINE
281 _E("Not supported vibration"); //LCOV_EXCL_LINE
282 return v_handle; //LCOV_EXCL_LINE System Error
285 /* stop haptic device */
286 ret = haptic_vibrate_stop(v_handle);
288 _E("haptic_vibrate_stop is failed"); //LCOV_EXCL_LINE
290 return ret; //LCOV_EXCL_LINE System Error
297 static int vibrator_is_supported(int pattern, bool *supported)
304 _E("Invalid parameter : supported(NULL)"); //LCOV_EXCL_LINE
308 if ((int)v_handle <= 0) {
310 _E("Not initialized"); //LCOV_EXCL_LINE
313 _E("Not supported vibration"); //LCOV_EXCL_LINE
314 *supported = false; //LCOV_EXCL_LINE System Error
318 if (pattern <= FEEDBACK_PATTERN_NONE ||
319 pattern >= profile->max_pattern) {
320 _E("Not supported vibration pattern");
325 /* get vibration data */
326 temp = strdup(profile->str_pattern[pattern]);
327 data = trim_str(temp);
334 ret = haptic_is_supported(data);
337 _E("fail to get support information");
343 _D("is_supported : %d", ret);
348 //LCOV_EXCL_START Not used function-Internal APIs TODO Will make iUTC
349 static int vibrator_get_path(feedback_pattern_e pattern, char *buf, unsigned int buflen)
354 static int vibrator_set_path(feedback_pattern_e pattern, char *path)
360 static const struct device_ops vibrator_device_ops = {
361 .type = FEEDBACK_TYPE_VIBRATION,
363 .init = vibrator_init,
364 .exit = vibrator_exit,
365 .play = vibrator_play,
366 .stop = vibrator_stop,
367 .is_supported = vibrator_is_supported,
368 .get_path = vibrator_get_path,
369 .set_path = vibrator_set_path,
372 DEVICE_OPS_REGISTER(&vibrator_device_ops);