1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <haptic.h>
#include <devman_haptic.h>
#include <glib.h>
#include <dlog.h>
#include "haptic_private.h"
#undef LOG_TAG
#define LOG_TAG "TIZEN_SYSTEM_HAPTIC"
#define NOT_ASSIGNED -1
#define VIBE_SILENCE -128
#define _MSG_HAPTIC_ERROR_INVALID_PARAMETER "Invalid parameter"
#define _MSG_HAPTIC_ERROR_NO_SUCH_FILE "No such file"
#define _MSG_HAPTIC_ERROR_NOT_SUPPORTED_FORMAT "Not supported format"
#define _MSG_HAPTIC_ERROR_NOT_INITIALIZED "Not initialize"
#define _MSG_HAPTIC_ERROR_OPERATION_FAILED "Operation failed"
#define RETURN_ERR_MSG(err_code, msg) \
do { \
LOGE("[%s] "_MSG_##err_code"(0x%08x) : %s", __FUNCTION__, err_code, msg); \
return err_code; \
}while(0)
#define RETURN_ERR(err_code) \
do { \
LOGE("[%s] "_MSG_##err_code"(0x%08x)", __FUNCTION__, err_code); \
return err_code; \
}while(0)
static int _DEV[] = {
DEV_IDX_ALL,
DEV_IDX_0,
DEV_IDX_1,
};
struct _vibe_pattern {
haptic_vibration_iter_s *iters;
int current;
int size;
int level;
int iter_count;
int pattern_index;
int error;
};
static const int UNDEFINED = 0;
static const int START = 1;
GArray *pattern_table = NULL;
static int initialize = 0;
static int max_device = 0;
static int* haptic_ids = NULL;
int haptic_get_count(int* vibrator_number)
{
int count;
if(vibrator_number == NULL)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
count = device_haptic_get_device_count();
if(count < 0)
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
*vibrator_number = count;
return HAPTIC_ERROR_NONE;
}
int haptic_initialize()
{
int i, j;
int id;
if(initialize)
return HAPTIC_ERROR_NONE;
if( haptic_get_count(&max_device) < 0)
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
haptic_ids = (int*)malloc(sizeof(int)*max_device+1); // max + zero
if(haptic_ids == NULL){
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
}
for(i=0; i<=max_device; i++){
id = device_haptic_open(_DEV[i], 0);
if(id < 0) {
for (j=i; i>=0; i--){
device_haptic_close(haptic_ids[i]);
}
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
}
haptic_ids[i] = id;
}
initialize = 1;
pattern_table = g_array_new(FALSE, TRUE, sizeof(int));
_haptic_init();
return HAPTIC_ERROR_NONE;
}
int haptic_deinitialize()
{
int err, i;
if(!initialize)
RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
for(i=0; i<=max_device; i++){
err = device_haptic_close(haptic_ids[i]);
}
initialize = 0;
if(haptic_ids != NULL)
free(haptic_ids);
g_array_free(pattern_table, TRUE);
pattern_table = NULL;
_haptic_deinit();
return HAPTIC_ERROR_NONE;
}
int haptic_vibrate_monotone(int device_index, int duration_ms, int level)
{
int ret;
if(device_index < 0 || device_index > max_device)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
if(level < HAPTIC_LEVEL_AUTO || level > HAPTIC_LEVEL_5)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
device_index = ((device_index < 3) ? device_index : 0); // xxx
if(!initialize)
RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
if(level == HAPTIC_LEVEL_0)
return HAPTIC_ERROR_NONE;
level = (level == HAPTIC_LEVEL_AUTO) ? 0 : level;
ret = device_haptic_play_monotone_with_detail_feedback_level(haptic_ids[device_index], duration_ms, level);
if(ret < 0){
if(ret == -2)
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
else
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
}
return HAPTIC_ERROR_NONE;
}
int haptic_stop_device(int device_index)
{
int ret;
if(device_index < 0 || device_index > max_device)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
device_index = ((device_index < 3) ? device_index : 0); // xxx
if(!initialize)
RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
ret = device_haptic_stop_play(haptic_ids[device_index]);
if(ret < 0){
if(ret == -2)
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
else
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
}
return HAPTIC_ERROR_NONE;
}
static int _haptic_play_monotone(int device_index, long duration, int level)
{
int ret;
if(device_index < 0 || device_index > max_device)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
device_index = ((device_index < 3) ? device_index : 0); // xxx
if(!initialize)
RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
if(level == HAPTIC_LEVEL_AUTO) {
level = HAPTIC_FEEDBACK_LEVEL_AUTO;
}else {
level = level > 100 ? 100 : (level < 0 ? 0 : level);
}
ret = device_haptic_play_monotone_with_detail_feedback_level(haptic_ids[device_index], duration, level);
if(ret < 0){
if(ret == -2)
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
else
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
}
return HAPTIC_ERROR_NONE;
}
static gboolean _haptic_play_iter(gpointer data)
{
int err;
struct _vibe_pattern* pattern = NULL;
pattern = (struct _vibe_pattern*)data;
if(pattern == NULL)
return false;
if(pattern_table == NULL || g_array_index(pattern_table, int, pattern->pattern_index) != START) {
free(pattern->iters);
free(pattern);
return false;
}
if(pattern->iters == NULL)
return false;
int current = pattern->current;
int device = pattern->iters[current].vibrator_index;
long time = pattern->iters[current].time;
int level = pattern->level;
// set default device, if can't find given device.
if(device >= max_device || device < 0)
device = 0;
if(level != 0 || time != 0){
err = _haptic_play_monotone(device, time, level);
if(err<0){
pattern->error = err;
return false;
}
}
// pattern play finish
if(++pattern->current >= pattern->size){
if(pattern->iter_count <= 0){
free(pattern->iters);
free(pattern);
return false;
}else{
pattern->current = 0;
pattern->iter_count--;
}
}
g_timeout_add(time, _haptic_play_iter, data);
return false;
}
int haptic_play_pattern(haptic_vibration_iter_s* pattern, int pattern_size, int count, int level, int* id)
{
int i, key = -1;
if(id == NULL)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
if(pattern == NULL)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
if(!initialize)
RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
haptic_vibration_iter_s* tmp_ptn = (haptic_vibration_iter_s*)
malloc(sizeof(haptic_vibration_iter_s) * pattern_size);
if(tmp_ptn == NULL){
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
}
memcpy(tmp_ptn, pattern, sizeof(haptic_vibration_iter_s) * pattern_size);
struct _vibe_pattern* vibe_p = (struct _vibe_pattern*)malloc(sizeof(struct _vibe_pattern));
if(vibe_p == NULL){
free(tmp_ptn);
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
}
vibe_p->iters = tmp_ptn;
vibe_p->size = pattern_size;
vibe_p->level = level;
vibe_p->iter_count = count;
vibe_p->current = 0;
vibe_p->error= 0;
for(i=0; i< pattern_table->len; i++){
if(g_array_index(pattern_table, int, i) == UNDEFINED){
key = i;
break;
}
}
if(key == -1){
g_array_append_val(pattern_table, START);
key = pattern_table->len -1;
}else{
g_array_index(pattern_table, int, key) = START;
}
vibe_p->pattern_index = key;
_haptic_play_iter((gpointer)vibe_p);
int error = vibe_p->error;
if(error < 0){
free(vibe_p->iters);
free(vibe_p);
if(error == -2)
RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
else
RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
}
*id = key;
return HAPTIC_ERROR_NONE;
}
int haptic_stop_pattern(int id)
{
if(id < 0)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
if(!initialize || pattern_table == NULL)
RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
if(id >= pattern_table->len)
RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
g_array_index(pattern_table, int, id) = UNDEFINED;
return HAPTIC_ERROR_NONE;
}
|