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.
28 #include <system_info.h>
37 #define HAPTIC_DEVICE 0
39 enum haptic_iteration {
40 HAPTIC_ITERATION_ONCE = 1,
41 HAPTIC_ITERATION_INFINITE = 256,
44 #define METHOD_OPEN "OpenDevice"
45 #define METHOD_CLOSE "CloseDevice"
46 #define METHOD_VIBRATE_PATTERN "VibratePattern"
47 #define METHOD_STOP "StopDevice"
48 #define METHOD_IS_SUPPORTED "IsSupported"
49 #define METHOD_GET_EFFECT "GetEffect"
51 #define DEFAULT_DURATION 100
52 #define SIP_DURATION 60
53 #define WHITESPACE " \t"
55 #define VIBRATION_FEATURE "http://tizen.org/feature/feedback.vibration"
58 static unsigned int v_handle;
60 static inline char *trim_str(char *s)
64 s += strspn(s, WHITESPACE);
67 for (t = strchr(s, 0); t > s; t--)
68 if (!strchr(WHITESPACE, t[-1]))
74 inline int is_vibration_mode(void)
79 static int haptic_open(void)
84 snprintf(buf_index, sizeof(buf_index), "%d", HAPTIC_DEVICE);
87 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
88 VIBRATOR_INTERFACE_HAPTIC, METHOD_OPEN,
92 static int haptic_close(unsigned int handle)
97 snprintf(buf_handle, sizeof(buf_handle), "%u", handle);
100 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
101 VIBRATOR_INTERFACE_HAPTIC, METHOD_CLOSE,
105 static int haptic_is_supported(char *pattern)
111 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
112 VIBRATOR_INTERFACE_HAPTIC, METHOD_IS_SUPPORTED,
116 static int haptic_vibrate_effect(unsigned int handle,
123 char buf_feedback[32];
124 char buf_priority[32];
126 snprintf(buf_handle, sizeof(buf_handle), "%u", handle);
129 snprintf(buf_feedback, sizeof(buf_feedback), "%d", feedback);
130 arr[2] = buf_feedback;
131 snprintf(buf_priority, sizeof(buf_priority), "%d", priority);
132 arr[3] = buf_priority;
134 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
135 VIBRATOR_INTERFACE_HAPTIC, METHOD_VIBRATE_PATTERN,
139 static int haptic_vibrate_stop(unsigned int handle)
144 snprintf(buf_handle, sizeof(buf_handle), "%u", handle);
147 return dbus_method_sync(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
148 VIBRATOR_INTERFACE_HAPTIC, METHOD_STOP,
152 static int get_priority(feedback_pattern_e pattern)
154 if (profile->get_priority)
155 return profile->get_priority(pattern);
156 return PRIORITY_MIDDLE;
159 static void vibrator_init(void)
164 ret = system_info_get_platform_bool(VIBRATION_FEATURE, &haptic_avail);
168 } else if (ret == 0 && !haptic_avail) {
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) {
211 if (v_handle == 0) { //LCOV_EXCL_LINE System Error
212 _E("Not initialized"); //LCOV_EXCL_LINE
213 return -ENOENT; //LCOV_EXCL_LINE System Error
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"); //LCOV_EXCL_LINE
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"); //LCOV_EXCL_LINE
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) {
277 if (v_handle == 0) { //LCOV_EXCL_LINE System Error
278 _E("Not initialized"); //LCOV_EXCL_LINE
279 return -ENOENT; //LCOV_EXCL_LINE System Error
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) {
309 if (v_handle == 0) { //LCOV_EXCL_LINE System Error
310 _E("Not initialized"); //LCOV_EXCL_LINE
311 return -ENOENT; //LCOV_EXCL_LINE System Error
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"); //LCOV_EXCL_LINE
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"); //LCOV_EXCL_LINE
343 _D("is_supported : %d", ret);
348 //LCOV_EXCL_START Not Supported Feature
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);