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)
81 return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
82 VIBRATOR_INTERFACE_HAPTIC, METHOD_OPEN,
83 g_variant_new("(i)", HAPTIC_DEVICE));
86 static int haptic_close(unsigned int handle)
88 return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
89 VIBRATOR_INTERFACE_HAPTIC, METHOD_CLOSE,
90 g_variant_new("(u)", handle));
93 static int haptic_is_supported(const char *pattern)
95 return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
96 VIBRATOR_INTERFACE_HAPTIC, METHOD_IS_SUPPORTED,
97 g_variant_new("(s)", pattern));
100 static int haptic_vibrate_effect(unsigned int handle,
105 return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
106 VIBRATOR_INTERFACE_HAPTIC, METHOD_VIBRATE_PATTERN,
107 g_variant_new("(usii)", handle, pattern, feedback, priority));
110 static int haptic_vibrate_stop(unsigned int handle)
112 return dbus_method_sync_var(VIBRATOR_BUS_NAME, VIBRATOR_PATH_HAPTIC,
113 VIBRATOR_INTERFACE_HAPTIC, METHOD_STOP,
114 g_variant_new("(u)", handle));
117 static int get_priority(feedback_pattern_e pattern)
119 if (profile->get_priority)
120 return profile->get_priority(pattern);
121 return PRIORITY_MIDDLE;
124 static void vibrator_init(void)
129 ret = system_info_get_platform_bool(VIBRATION_FEATURE, &haptic_avail);
133 } else if (ret == 0 && !haptic_avail) {
140 if (ret == -EACCES || ret == -ECOMM || ret == -EPERM) {
141 _E("haptic_open ==> FAIL!! : %d", ret); //LCOV_EXCL_LINE
142 v_handle = -EACCES; //LCOV_EXCL_LINE System Error
143 return; //LCOV_EXCL_LINE System Error
146 _E("haptic_open ==> FAIL!! : %d", ret); //LCOV_EXCL_LINE
147 v_handle = -ENOTSUP; //LCOV_EXCL_LINE System Error
148 return; //LCOV_EXCL_LINE System Error
151 /* Set vibration handle */
152 v_handle = (unsigned int)ret;
155 static void vibrator_exit(void)
159 if ((int)v_handle > 0) {
160 ret = haptic_close(v_handle);
162 _E("haptic_close is failed : %d", ret); //LCOV_EXCL_LINE
168 static int vibrator_play(feedback_pattern_e pattern)
175 if ((int)v_handle <= 0) {
176 if (v_handle == 0) { //LCOV_EXCL_LINE System Error
177 _E("Not initialized"); //LCOV_EXCL_LINE
178 return -ENOENT; //LCOV_EXCL_LINE System Error
180 _E("Not supported vibration"); //LCOV_EXCL_LINE
181 return v_handle; //LCOV_EXCL_LINE System Error
184 if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibstatus) < 0) {
185 _D("fail to get vibration status, will work as turning off"); //LCOV_EXCL_LINE
189 if (vibstatus == 0 && profile->get_always_alert_case &&
190 !profile->get_always_alert_case(FEEDBACK_TYPE_VIBRATION, pattern)) {
191 _D("Vibration condition is OFF (vibstatus : %d)", vibstatus); //LCOV_EXCL_LINE
195 if (vibstatus && profile->get_always_off_case &&
196 profile->get_always_off_case(FEEDBACK_TYPE_VIBRATION, pattern)) {
197 _D("Vibration always off condition"); //LCOV_EXCL_LINE
201 if (pattern <= FEEDBACK_PATTERN_NONE ||
202 pattern >= profile->max_pattern) {
203 _E("Not supported vibration pattern"); //LCOV_EXCL_LINE
207 if (profile->get_strength_type)
208 level = profile->get_strength_type(FEEDBACK_TYPE_VIBRATION, pattern);
210 level = DEFAULT_VIB_LEVEL * HAPTIC_FEEDBACK_STEP;
212 temp = strdup(profile->str_pattern(pattern));
213 data = trim_str(temp);
216 _E("Not supported vibration pattern"); //LCOV_EXCL_LINE
220 _D("pattern : %s", data);
222 ret = haptic_vibrate_effect(v_handle, data, level, get_priority(pattern));
225 _E("fail to play vibration"); //LCOV_EXCL_LINE
227 if (ret == -ECOMM || ret == -ENOTSUP)
228 return ret; //LCOV_EXCL_LINE System Error
232 _D("Play success! Data is %s", data);
237 static int vibrator_stop(void)
241 if ((int)v_handle <= 0) {
242 if (v_handle == 0) { //LCOV_EXCL_LINE System Error
243 _E("Not initialized"); //LCOV_EXCL_LINE
244 return -ENOENT; //LCOV_EXCL_LINE System Error
246 _E("Not supported vibration"); //LCOV_EXCL_LINE
247 return v_handle; //LCOV_EXCL_LINE System Error
250 /* stop haptic device */
251 ret = haptic_vibrate_stop(v_handle);
253 _E("haptic_vibrate_stop is failed"); //LCOV_EXCL_LINE
255 return ret; //LCOV_EXCL_LINE System Error
262 static int vibrator_is_supported(int pattern, bool *supported)
269 _E("Invalid parameter : supported(NULL)"); //LCOV_EXCL_LINE
273 if ((int)v_handle <= 0) {
274 if (v_handle == 0) { //LCOV_EXCL_LINE System Error
275 _E("Not initialized"); //LCOV_EXCL_LINE
276 return -ENOENT; //LCOV_EXCL_LINE System Error
278 _E("Not supported vibration"); //LCOV_EXCL_LINE
279 *supported = false; //LCOV_EXCL_LINE System Error
283 if (pattern <= FEEDBACK_PATTERN_NONE ||
284 pattern >= profile->max_pattern) {
285 _E("Not supported vibration pattern"); //LCOV_EXCL_LINE
290 /* get vibration data */
291 temp = strdup(profile->str_pattern(pattern));
292 data = trim_str(temp);
299 ret = haptic_is_supported(data);
302 _E("fail to get support information"); //LCOV_EXCL_LINE
308 _D("is_supported : %d", ret);
313 //LCOV_EXCL_START Not Supported Feature
314 static int vibrator_get_path(feedback_pattern_e pattern, char *buf, unsigned int buflen)
319 static int vibrator_set_path(feedback_pattern_e pattern, char *path)
325 static const struct device_ops vibrator_device_ops = {
326 .type = FEEDBACK_TYPE_VIBRATION,
328 .init = vibrator_init,
329 .exit = vibrator_exit,
330 .play = vibrator_play,
331 .stop = vibrator_stop,
332 .is_supported = vibrator_is_supported,
333 .get_path = vibrator_get_path,
334 .set_path = vibrator_set_path,
337 DEVICE_OPS_REGISTER(&vibrator_device_ops);