device: Merge public api on Tizen 2.3 into tizen branch
[platform/core/api/device.git] / src / haptic.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <errno.h>
22
23 #include "haptic.h"
24 #include "common.h"
25 #include "dbus.h"
26
27 #define METHOD_GET_COUNT                        "GetCount"
28 #define METHOD_OPEN_DEVICE                      "OpenDevice"
29 #define METHOD_CLOSE_DEVICE                     "CloseDevice"
30 #define METHOD_STOP_DEVICE                      "StopDevice"
31 #define METHOD_VIBRATE_MONOTONE         "VibrateMonotone"
32
33 enum feedback_e
34 {
35         HAPTIC_FEEDBACK_MIN = 0,
36         HAPTIC_FEEDBACK_MAX = 100,
37 };
38
39 enum priority_e
40 {
41         HAPTIC_PRIORITY_MIN = 0,
42         HAPTIC_PRIORITY_MIDDLE,
43         HAPTIC_PRIORITY_HIGH,
44 };
45
46 int device_haptic_get_count(int *device_number)
47 {
48         int ret;
49
50         if (!device_number)
51                 return DEVICE_ERROR_INVALID_PARAMETER;
52
53         /* request to deviced to get haptic count */
54         ret = dbus_method_sync(DEVICED_BUS_NAME,
55                         DEVICED_PATH_HAPTIC, DEVICED_INTERFACE_HAPTIC,
56                         METHOD_GET_COUNT, NULL, NULL);
57         if (ret < 0)
58                 return errno_to_device_error(ret);
59
60         *device_number = ret;
61         return DEVICE_ERROR_NONE;
62 }
63
64 int device_haptic_open(int device_index, haptic_device_h *device_handle)
65 {
66         char str_index[32];
67         char *arr[1];
68         int ret, max;
69
70         ret = device_haptic_get_count(&max);
71         if (ret < 0)
72                 return ret;
73
74         if (device_index < 0 || device_index >= max)
75                 return DEVICE_ERROR_INVALID_PARAMETER;
76
77         if (!device_handle)
78                 return DEVICE_ERROR_INVALID_PARAMETER;
79
80         snprintf(str_index, sizeof(str_index), "%d", device_index);
81         arr[0] = str_index;
82
83         /* request to deviced to open haptic device */
84         ret = dbus_method_sync(DEVICED_BUS_NAME,
85                         DEVICED_PATH_HAPTIC, DEVICED_INTERFACE_HAPTIC,
86                         METHOD_OPEN_DEVICE, "i", arr);
87         if (ret < 0)
88                 return errno_to_device_error(ret);
89
90         *device_handle = (haptic_device_h)ret;
91         return DEVICE_ERROR_NONE;
92 }
93
94 int device_haptic_close(haptic_device_h device_handle)
95 {
96         char str_handle[32];
97         char *arr[1];
98         int ret;
99
100         if (!device_handle)
101                 return DEVICE_ERROR_INVALID_PARAMETER;
102
103         snprintf(str_handle, sizeof(str_handle), "%u", (unsigned int)device_handle);
104         arr[0] = str_handle;
105
106         /* request to deviced to open haptic device */
107         ret = dbus_method_sync(DEVICED_BUS_NAME,
108                         DEVICED_PATH_HAPTIC, DEVICED_INTERFACE_HAPTIC,
109                         METHOD_CLOSE_DEVICE, "u", arr);
110         if (ret < 0)
111                 return errno_to_device_error(ret);
112
113         return DEVICE_ERROR_NONE;
114 }
115
116 int device_haptic_vibrate(haptic_device_h device_handle, int duration, int feedback, haptic_effect_h *effect_handle)
117 {
118         char str_handle[32];
119         char str_duration[32];
120         char str_feedback[32];
121         char str_priority[32];
122         char *arr[4];
123         int ret, priority;
124
125         if (!device_handle)
126                 return DEVICE_ERROR_INVALID_PARAMETER;
127
128         if (duration < 0)
129                 return DEVICE_ERROR_INVALID_PARAMETER;
130
131         if (feedback < HAPTIC_FEEDBACK_MIN || feedback > HAPTIC_FEEDBACK_MAX)
132                 return DEVICE_ERROR_INVALID_PARAMETER;
133
134         priority = HAPTIC_PRIORITY_MIN;
135
136         snprintf(str_handle, sizeof(str_handle), "%u", (unsigned int)device_handle);
137         arr[0] = str_handle;
138         snprintf(str_duration, sizeof(str_duration), "%d", duration);
139         arr[1] = str_duration;
140         snprintf(str_feedback, sizeof(str_feedback), "%d", feedback);
141         arr[2] = str_feedback;
142         snprintf(str_priority, sizeof(str_priority), "%d", priority);
143         arr[3] = str_priority;
144
145         /* request to deviced to vibrate haptic device */
146         ret = dbus_method_sync(DEVICED_BUS_NAME,
147                         DEVICED_PATH_HAPTIC, DEVICED_INTERFACE_HAPTIC,
148                         METHOD_VIBRATE_MONOTONE, "uiii", arr);
149         if (ret < 0)
150                 return errno_to_device_error(ret);
151
152         if (effect_handle)
153                 *effect_handle = (haptic_effect_h)ret;
154
155         return DEVICE_ERROR_NONE;
156 }
157
158 int device_haptic_stop(haptic_device_h device_handle, haptic_effect_h effect_handle)
159 {
160         char str_handle[32];
161         char *arr[1];
162         int ret;
163
164         if (!device_handle)
165                 return DEVICE_ERROR_INVALID_PARAMETER;
166
167         /* TODO : support to stop haptic effect */
168         snprintf(str_handle, sizeof(str_handle), "%u", (unsigned int)device_handle);
169         arr[0] = str_handle;
170
171         /* request to deviced to open haptic device */
172         ret = dbus_method_sync(DEVICED_BUS_NAME,
173                         DEVICED_PATH_HAPTIC, DEVICED_INTERFACE_HAPTIC,
174                         METHOD_STOP_DEVICE, "u", arr);
175         if (ret < 0)
176                 return errno_to_device_error(ret);
177
178         return DEVICE_ERROR_NONE;
179 }