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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
/*
* libfeedback
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
*
* 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 <stdbool.h>
#include <string.h>
#include <limits.h>
#include "feedback.h"
#include "feedback-internal.h"
#include "profiles.h"
#include "devices.h"
#include "log.h"
#include "dbus.h"
#ifndef API
#define API __attribute__ ((visibility("default")))
#endif
static unsigned int init_cnt;
static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
static int restart_callback()
{
const struct device_ops *dev;
dev = find_device(FEEDBACK_TYPE_VIBRATION);
if (!dev) {
_E("Not supported device : type(FEEDBACK_TYPE_VIBRATION)"); //LCOV_EXCL_LINE
return -1;
}
if (dev->init)
dev->init();
return 0;
}
API int feedback_initialize(void)
{
int ret;
pthread_mutex_lock(&fmutex);
if (!profile) {
_E("there is no valid profile module."); //LCOV_EXCL_LINE
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_SUPPORTED;
}
if (init_cnt++ > 0) {
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NONE;
}
ret = register_signal_handler(restart_callback);
if (ret < 0)
_E("Fail to register signal handler: %d", ret);
/* initialize device */
devices_init();
/* initialize profile feature */
if (profile->init)
profile->init();
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NONE;
}
API int feedback_deinitialize(void)
{
int ret;
pthread_mutex_lock(&fmutex);
if (!init_cnt) {
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_INITIALIZED;
}
if (init_cnt-- > 1) {
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NONE;
}
ret = unregister_signal_handler(restart_callback);
if (ret < 0)
_E("Fail to unregister signal handler: %d", ret);
/* deinitialize device */
devices_exit();
/* deinitialize profile feature */
if (profile->exit)
profile->exit();
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NONE;
}
API int feedback_play(feedback_pattern_e pattern)
{
int err;
bool result;
int switched;
/* check initialize */
pthread_mutex_lock(&fmutex);
if (!init_cnt) {
_E("Not initialized"); //LCOV_EXCL_LINE
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_INITIALIZED;
}
pthread_mutex_unlock(&fmutex);
if (pattern <= FEEDBACK_PATTERN_NONE ||
pattern >= profile->max_pattern) {
_E("Invalid parameter : pattern(%d)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
/* if you need to switch pattern */
if (profile->get_switched_pattern) {
result = profile->get_switched_pattern(pattern, &switched);
if (result) {
_W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
profile->str_pattern(pattern), profile->str_pattern(switched));
pattern = switched;
}
}
/* play all device */
err = devices_play(pattern);
/**
* devices_play() returns error even if all devices are failed.
* It means if to play anything is successful,
* this function regards as success.
*/
if (err == -ENOTSUP)
return FEEDBACK_ERROR_NOT_SUPPORTED;
else if (err < 0)
return FEEDBACK_ERROR_OPERATION_FAILED;
return FEEDBACK_ERROR_NONE;
}
API int feedback_play_type(feedback_type_e type, feedback_pattern_e pattern)
{
const struct device_ops *dev;
int err;
bool result;
int switched;
/* check initialize */
pthread_mutex_lock(&fmutex);
if (!init_cnt) {
_E("Not initialized"); //LCOV_EXCL_LINE
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_INITIALIZED;
}
pthread_mutex_unlock(&fmutex);
if (type <= FEEDBACK_TYPE_NONE ||
type >= profile->max_type) {
_E("Invalid parameter : type(%d)", type);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (pattern <= FEEDBACK_PATTERN_NONE ||
pattern >= profile->max_pattern) {
_E("Invalid parameter : pattern(%d)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
/* if you need to switch pattern */
if (profile->get_switched_pattern) {
result = profile->get_switched_pattern(pattern, &switched);
if (result) {
_W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
profile->str_pattern(pattern), profile->str_pattern(switched));
pattern = switched;
}
}
/* play proper device */
dev = find_device(type);
if (!dev) {
_E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
return FEEDBACK_ERROR_NOT_SUPPORTED;
}
err = dev->play(pattern);
if (err == -ENOTSUP)
return FEEDBACK_ERROR_NOT_SUPPORTED;
else if (err == -ECOMM || err == -EACCES)
return FEEDBACK_ERROR_PERMISSION_DENIED;
else if (err < 0)
return FEEDBACK_ERROR_OPERATION_FAILED;
return FEEDBACK_ERROR_NONE;
}
API int feedback_stop(void)
{
int err;
/* check initialize */
pthread_mutex_lock(&fmutex);
if (!init_cnt) {
_E("Not initialized"); //LCOV_EXCL_LINE
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_INITIALIZED;
}
pthread_mutex_unlock(&fmutex);
/* stop all device */
err = devices_stop();
if (err == -ENOTSUP)
return FEEDBACK_ERROR_NOT_SUPPORTED;
else if (err == -ECOMM || err == -EACCES)
return FEEDBACK_ERROR_PERMISSION_DENIED;
else if (err < 0)
return FEEDBACK_ERROR_OPERATION_FAILED;
return FEEDBACK_ERROR_NONE;
}
API int feedback_is_supported_pattern(feedback_type_e type, feedback_pattern_e pattern, bool *status)
{
const struct device_ops *dev;
bool supported;
int err;
bool result;
int switched;
/* check initialize */
pthread_mutex_lock(&fmutex);
if (!init_cnt) {
_E("Not initialized"); //LCOV_EXCL_LINE
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_INITIALIZED;
}
pthread_mutex_unlock(&fmutex);
if (!status) {
_E("Invalid parameter : status(NULL)");
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (type <= FEEDBACK_TYPE_NONE ||
type >= profile->max_type) {
_E("Invalid parameter : type(%d)", type);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (pattern <= FEEDBACK_PATTERN_NONE ||
pattern >= profile->max_pattern) {
_E("Invalid parameter : pattern(%d)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
/* if you need to switch pattern */
if (profile->get_switched_pattern) {
result = profile->get_switched_pattern(pattern, &switched);
if (result) {
_W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
profile->str_pattern(pattern), profile->str_pattern(switched));
pattern = switched;
}
}
/* play proper device */
dev = find_device(type);
if (!dev) {
_E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
return FEEDBACK_ERROR_NOT_SUPPORTED;
}
err = dev->is_supported(pattern, &supported);
if (err == -ENOTSUP)
return FEEDBACK_ERROR_NOT_SUPPORTED; //LCOV_EXCL_LINE System Error
else if (err == -ECOMM || err == -EACCES)
return FEEDBACK_ERROR_PERMISSION_DENIED; //LCOV_EXCL_LINE System Error
else if (err < 0) {
_E("fail to invoke is_supported() : pattern(%d)", pattern); //LCOV_EXCL_LINE
return FEEDBACK_ERROR_OPERATION_FAILED;
}
*status = supported;
return FEEDBACK_ERROR_NONE;
}
/* Internal APIs */
API int feedback_play_type_by_name(char *type, char *pattern)
{
feedback_type_e etype;
feedback_pattern_e epattern;
int type_max;
int pattern_max;
if (!type || !pattern) {
_E("Invalid parameter : type(%x), pattern(%x)", type, pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
type_max = profile->max_type;
for (etype = 0; etype < type_max; ++etype) {
if (!strncmp(type, profile->str_type[etype], strlen(type)))
break;
}
if (etype == type_max) {
_E("Invalid parameter : type(%s)", type);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
pattern_max = profile->max_pattern;
for (epattern = 0; epattern < pattern_max; ++epattern) {
if (!strncmp(pattern, profile->str_pattern(epattern), strlen(pattern)))
break;
}
if (epattern == pattern_max) {
_E("Invalid parameter : pattern(%s)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
return feedback_play_type(etype, epattern);
}
API int feedback_play_internal(feedback_pattern_internal_e pattern)
{
int err;
bool result;
int switched;
/* check initialize */
pthread_mutex_lock(&fmutex);
if (!init_cnt) {
_E("Not initialized"); //LCOV_EXCL_LINE
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_INITIALIZED;
}
pthread_mutex_unlock(&fmutex);
if (pattern <= FEEDBACK_PATTERN_NONE ||
pattern >= profile->max_pattern) {
_E("Invalid parameter : pattern(%d)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
/* if you need to switch pattern */
if (profile->get_switched_pattern) {
result = profile->get_switched_pattern(pattern, &switched);
if (result) {
_W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
profile->str_pattern(pattern), profile->str_pattern(switched));
pattern = switched;
}
}
/* play all device */
err = devices_play(pattern);
/**
* devices_play() returns error even if all devices are failed.
* It means if to play anything is successful,
* this function regards as success.
*/
if (err == -ENOTSUP)
return FEEDBACK_ERROR_NOT_SUPPORTED;
else if (err < 0)
return FEEDBACK_ERROR_OPERATION_FAILED;
return FEEDBACK_ERROR_NONE;
}
API int feedback_play_type_internal(feedback_type_e type, feedback_pattern_internal_e pattern)
{
const struct device_ops *dev;
int err;
bool result;
int switched;
/* check initialize */
pthread_mutex_lock(&fmutex);
if (!init_cnt) {
_E("Not initialized"); //LCOV_EXCL_LINE
pthread_mutex_unlock(&fmutex);
return FEEDBACK_ERROR_NOT_INITIALIZED;
}
pthread_mutex_unlock(&fmutex);
if (type <= FEEDBACK_TYPE_NONE ||
type >= profile->max_type) {
_E("Invalid parameter : type(%d)", type);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (pattern <= FEEDBACK_PATTERN_INTERNAL_NONE ||
pattern >= profile->max_pattern) {
_E("Invalid parameter : pattern(%d)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
/* if you need to switch pattern */
if (profile->get_switched_pattern) {
result = profile->get_switched_pattern(pattern, &switched);
if (result) {
_W("pattern is changed : (%s) -> (%s)", //LCOV_EXCL_LINE
profile->str_pattern(pattern), profile->str_pattern(switched));
pattern = switched;
}
}
/* play proper device */
dev = find_device(type);
if (!dev) {
_E("Not supported device : type(%s)", profile->str_type[type]); //LCOV_EXCL_LINE
return FEEDBACK_ERROR_NOT_SUPPORTED;
}
err = dev->play(pattern);
if (err == -ENOTSUP)
return FEEDBACK_ERROR_NOT_SUPPORTED;
else if (err == -ECOMM || err == -EACCES)
return FEEDBACK_ERROR_PERMISSION_DENIED;
else if (err < 0)
return FEEDBACK_ERROR_OPERATION_FAILED;
return FEEDBACK_ERROR_NONE;
}
API int feedback_get_resource_path(feedback_type_e type, feedback_pattern_e pattern, char** path)
{
const struct device_ops *dev;
char buf[PATH_MAX] = {0,};
int err;
if (path == NULL) {
_E("Invalid parameter : path(NULL)");
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (type <= FEEDBACK_TYPE_NONE ||
type >= profile->max_type) {
_E("Invalid parameter : type(%d)", type);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (type == FEEDBACK_TYPE_VIBRATION)
return FEEDBACK_ERROR_NOT_SUPPORTED;
if (pattern <= FEEDBACK_PATTERN_NONE ||
pattern >= profile->max_pattern) {
_E("Invalid parameter : pattern(%d)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
/* proper device get path */
dev = find_device(type);
if (dev) {
err = dev->get_path(pattern, buf, sizeof(buf));
if (err < 0)
return FEEDBACK_ERROR_OPERATION_FAILED;
}
*path = strdup(buf);
return FEEDBACK_ERROR_NONE;
}
API int feedback_set_resource_path(feedback_type_e type, feedback_pattern_e pattern, char *path)
{
const struct device_ops *dev;
int err;
if (type <= FEEDBACK_TYPE_NONE ||
type >= profile->max_type) {
_E("Invalid parameter : type(%d)", type);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (pattern <= FEEDBACK_PATTERN_NONE ||
pattern >= profile->max_pattern) {
_E("Invalid parameter : pattern(%d)", pattern);
return FEEDBACK_ERROR_INVALID_PARAMETER;
}
if (type == FEEDBACK_TYPE_VIBRATION) {
_E("Not supported type"); //LCOV_EXCL_LINE
return FEEDBACK_ERROR_NOT_SUPPORTED;
}
/* proper device set path */
dev = find_device(type);
if (dev) {
err = dev->set_path(pattern, path);
if (err < 0)
return FEEDBACK_ERROR_OPERATION_FAILED;
}
return FEEDBACK_ERROR_NONE;
}
|