-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvibrator_server.c
More file actions
1375 lines (1191 loc) · 39.6 KB
/
vibrator_server.c
File metadata and controls
1375 lines (1191 loc) · 39.6 KB
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* Copyright (C) 2023 Xiaomi Corperation
*
* 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.
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <fcntl.h>
#include <kvdb.h>
#include <mqueue.h>
#include <netpacket/rpmsg.h>
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <uv.h>
#include <nuttx/input/ff.h>
#include "vibrator_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define VIBRATOR_LOCAL 0
#define VIBRATOR_REMOTE 1
#define VIBRATOR_COUNT 2
#define VIBRATOR_MAX_CLIENTS 16
#define VIBRATOR_MAX_AMPLITUDE 255
#define VIBRATOR_DEFAULT_AMPLITUDE -1
#define VIBRATOR_DEFAULT_DISABLE false
#define VIBRATOR_INVALID_VALUE -1
#define VIBRATOR_STRONG_MAGNITUDE 0x7fff
/* 0.7 * (VIBRATOR_STRONG_MAGNITUDE - VIBRATOR_LIGHT_MAGNITUDE)
+ VIBRATOR_LIGHT_MAGNITUDE */
#define VIBRATOR_MEDIUM_MAGNITUDE 0x6CCC
#define VIBRATOR_LIGHT_MAGNITUDE 0x3fff
#define VIBRATOR_CUSTOM_DATA_LEN 3
#define VIBRATOR_DEV_FS "/dev/lra0"
#define KVDB_KEY_VIBRATOR_MODE "persist.vibration.mode"
#define KVDB_KEY_VIBRATOR_DISABLE "persist.vibration_disable"
#define KVDB_KEY_VIBRATOR_CALIB "ro.factory.motor_calib"
/****************************************************************************
* Private Types
****************************************************************************/
typedef struct {
int fd;
int16_t curr_app_id;
int16_t curr_magnitude;
uint8_t curr_amplitude;
int32_t capabilities;
vibrator_intensity_e intensity;
uint8_t disabled;
} ff_dev_t;
typedef struct {
ff_dev_t* ff_dev;
uv_timer_t timer;
vibrator_msg_t msg;
vibrator_waveform_t wave;
vibrator_compose_t composition;
} threadargs;
typedef struct vibrator_context_s {
uv_poll_t poll_handle;
uv_os_sock_t sock;
threadargs* thread_args;
} vibrator_context_t;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: ff_play()
*
* Description:
* operate the driver's interface, vibrator device control
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* effect_id - ID of the predefined effect will be played. If effectId is
* valid(non-negative value), the timeout_ms value will be
* ignored, and the real playing length will be set in
* playLengtMs and returned to VibratorService. If effectId is
* invalid, value in param timeout_ms will be used as the play
* length for playing a constant effect.
* timeout_ms - playing length, non-zero means playing, zero means stop
* playing.
* play_length_ms - the playing length in ms unit which will be returned to
* VibratorService if the request is playing a predefined
* effect.
*
* Returned Value:
* return the ret of file system operations
*
****************************************************************************/
static int ff_play(ff_dev_t* ff_dev, int effect_id, uint32_t timeout_ms,
long* play_length_ms)
{
int16_t data[VIBRATOR_CUSTOM_DATA_LEN] = { 0, 0, 0 };
struct ff_effect effect;
struct ff_event_s play;
int ret;
memset(&play, 0, sizeof(play));
if (timeout_ms != 0) {
/* if curr_app_id is valid, then remove the effect from the device
first */
if (ff_dev->curr_app_id != VIBRATOR_INVALID_VALUE) {
ret = ioctl(ff_dev->fd, EVIOCRMFF, ff_dev->curr_app_id);
if (ret < 0) {
VIBRATORERR("ioctl EVIOCRMFF failed, errno = %d", errno);
goto errout;
}
ff_dev->curr_app_id = VIBRATOR_INVALID_VALUE;
}
/* if effect_id is valid, then upload the predefined effect with
effect_id, else upload a constant effect with timeout_ms */
memset(&effect, 0, sizeof(effect));
if (effect_id != VIBRATOR_INVALID_VALUE) {
data[0] = effect_id;
effect.type = FF_PERIODIC;
effect.u.periodic.waveform = FF_CUSTOM;
effect.u.periodic.magnitude = ff_dev->curr_magnitude;
effect.u.periodic.custom_data = data;
effect.u.periodic.custom_len = sizeof(int16_t) * VIBRATOR_CUSTOM_DATA_LEN;
} else {
effect.type = FF_CONSTANT;
effect.u.constant.level = ff_dev->curr_magnitude;
effect.replay.length = timeout_ms;
}
effect.id = ff_dev->curr_app_id;
effect.replay.delay = 0;
ret = ioctl(ff_dev->fd, EVIOCSFF, &effect);
if (ret < 0) {
VIBRATORERR("ioctl EVIOCSFF failed, errno = %d", errno);
goto errout;
}
/* update the curr_app_id with the ID obtained from device driver */
ff_dev->curr_app_id = effect.id;
/* return the effect play length to vibrator service */
if (effect_id != VIBRATOR_INVALID_VALUE && play_length_ms != NULL) {
*play_length_ms = data[1] * 1000 + data[2];
VIBRATORINFO("*play_length_ms = %ld", *play_length_ms);
}
if (ff_dev->curr_app_id == VIBRATOR_INVALID_VALUE)
return ret;
/* write the play event to vibrator device */
play.value = 1;
play.code = ff_dev->curr_app_id;
ret = write(ff_dev->fd, (const void*)&play, sizeof(play));
if (ret < 0) {
VIBRATORERR("write failed, errno = %d", errno);
ret = ioctl(ff_dev->fd, EVIOCRMFF, ff_dev->curr_app_id);
if (ret < 0)
VIBRATORERR("ioctl EVIOCRMFF failed, errno = %d", errno);
goto errout;
}
} else if (ff_dev->curr_app_id != VIBRATOR_INVALID_VALUE) {
/* stop vibration if timeout_ms is zero and curr_app_id is valid */
ret = ioctl(ff_dev->fd, EVIOCRMFF, ff_dev->curr_app_id);
if (ret < 0) {
VIBRATORERR("ioctl EVIOCRMFF failed, errno = %d", errno);
goto errout;
}
ff_dev->curr_app_id = VIBRATOR_INVALID_VALUE;
}
return 0;
errout:
ff_dev->curr_app_id = VIBRATOR_INVALID_VALUE;
return ret;
}
/****************************************************************************
* Name: ff_set_amplitude()
*
* Description:
* operate the driver's interface, set gain to vibrator device
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* amplitude - vibration instensity, range[0,255]
*
* Returned Value:
* return the ret of write
*
****************************************************************************/
static int ff_set_amplitude(ff_dev_t* ff_dev, uint8_t amplitude)
{
struct ff_event_s gain;
int tmp;
int ret;
memset(&gain, 0, sizeof(gain));
tmp = amplitude * (VIBRATOR_STRONG_MAGNITUDE - VIBRATOR_LIGHT_MAGNITUDE) / 255;
tmp += VIBRATOR_LIGHT_MAGNITUDE;
gain.code = FF_GAIN;
gain.value = tmp;
ret = write(ff_dev->fd, &gain, sizeof(gain));
if (ret < 0) {
VIBRATORERR("write FF_GAIN failed, errno = %d", errno);
return ret;
}
ff_dev->curr_magnitude = tmp;
return ret;
}
/****************************************************************************
* Name: ff_calibrate()
*
* Description:
* operate the driver's interface, calibrate vibrator device
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* data - buffer that store calibrate result data
*
* Returned Value:
* 0 means success
*
****************************************************************************/
static int ff_calibrate(ff_dev_t* ff_dev, uint8_t* data)
{
int ret;
ret = ioctl(ff_dev->fd, EVIOCCALIBRATE, data);
if (ret < 0) {
VIBRATORERR("ff device calibrate failed, errno = %d", errno);
}
return ret;
}
/****************************************************************************
* Name: ff_set_calibvalue()
*
* Description:
* operate the driver's interface, set calibrate value to vibrator device
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* data - buffer that store calibrate value which will be set to device
*
* Returned Value:
* 0 means success
*
****************************************************************************/
static int ff_set_calibvalue(ff_dev_t* ff_dev, uint8_t* data)
{
int ret;
ret = ioctl(ff_dev->fd, EVIOCSETCALIBDATA, data);
if (ret < 0) {
VIBRATORERR("ff device set calibrate data failed, errno = %d", errno);
}
return ret;
}
/****************************************************************************
* Name: ff_control()
*
* Description:
* Send custom control command to the force feedback device driver.
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* cmd - ioctl command code
* arg - control data send or received
*
* Returned Value:
* 0 means success, otherwise it means failure
*
****************************************************************************/
static int ff_control(ff_dev_t* ff_dev, uint32_t cmd, uint8_t* arg)
{
int ret;
ret = ioctl(ff_dev->fd, cmd, arg);
if (ret < 0) {
VIBRATORERR("ff device control command failed, errno = %d", errno);
}
return ret;
}
/****************************************************************************
* Name: play_effect()
*
* Description:
* play the predefined effect.
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* effect_id - ID of the predefined effect will be played.
* es - effect intensity.
* play_length_ms - the playing length in ms unit which will be returned to
* VibratorService if the request is playing a predefined
* effect.
*
* Returned Value:
* return the ret of ff_play
*
****************************************************************************/
static int play_effect(ff_dev_t* ff_dev, int effect_id,
vibrator_effect_strength_e es, long* play_length_ms)
{
switch (es) {
case VIBRATION_LIGHT: {
ff_dev->curr_magnitude = VIBRATOR_LIGHT_MAGNITUDE;
break;
}
case VIBRATION_MEDIUM: {
ff_dev->curr_magnitude = VIBRATOR_MEDIUM_MAGNITUDE;
break;
}
case VIBRATION_STRONG: {
ff_dev->curr_magnitude = VIBRATOR_STRONG_MAGNITUDE;
break;
}
default: {
break;
}
}
return ff_play(ff_dev, effect_id, VIBRATOR_INVALID_VALUE,
play_length_ms);
}
/****************************************************************************
* Name: play_primitive()
*
* Description:
* play the predefined effect with the specified amplitude.
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* effect_id - ID of the predefined effect will be played.
* amplitude - effect amplitude(0-1.0).
* play_length_ms - the playing length in ms unit which will be returned to
* VibratorService if the request is playing a predefined
* effect.
*
* Returned Value:
* return the ret of ff_play
*
****************************************************************************/
static int play_primitive(ff_dev_t* ff_dev, int effect_id,
float amplitude, long* play_length_ms)
{
int tmp;
tmp = (uint8_t)(amplitude * VIBRATOR_MAX_AMPLITUDE);
ff_dev->curr_magnitude = tmp * (VIBRATOR_STRONG_MAGNITUDE - VIBRATOR_LIGHT_MAGNITUDE) / 255;
ff_dev->curr_magnitude += VIBRATOR_LIGHT_MAGNITUDE;
return ff_play(ff_dev, effect_id, VIBRATOR_INVALID_VALUE,
play_length_ms);
}
/****************************************************************************
* Name: on()
*
* Description:
* turn on vibrator.
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* timeout_ms - number of milliseconds to vibrate
*
* Returned Value:
* return the ret of ff_play
*
****************************************************************************/
static int on(ff_dev_t* ff_dev, uint32_t timeout_ms)
{
return ff_play(ff_dev, VIBRATOR_INVALID_VALUE, timeout_ms, NULL);
}
/****************************************************************************
* Name: off()
*
* Description:
* turn off vibrator.
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
*
* Returned Value:
* return the ret of ff_play
*
****************************************************************************/
static int off(ff_dev_t* ff_dev)
{
return ff_play(ff_dev, VIBRATOR_INVALID_VALUE, 0, NULL);
}
/****************************************************************************
* Name: get_primitive_duration()
*
* Description:
* get the duration of the predefined effect with the specified id.
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* effect_id - ID of the predefined effect.
* duration - the duration of the predefined effect in ms.
*
* Returned Value:
* return the ret of ioctl
*
****************************************************************************/
static int get_primitive_duration(ff_dev_t* ff_dev, int effect_id,
int32_t* duration)
{
int16_t data[VIBRATOR_CUSTOM_DATA_LEN] = { 0, 0, 0 };
struct ff_effect effect;
int ret;
memset(&effect, 0, sizeof(effect));
data[0] = effect_id;
effect.type = FF_PERIODIC;
effect.u.periodic.waveform = FF_CUSTOM;
effect.u.periodic.custom_data = data;
effect.u.periodic.custom_len = sizeof(int16_t) * VIBRATOR_CUSTOM_DATA_LEN;
ret = ioctl(ff_dev->fd, EVIOCGDURATION, &effect);
if (ret < 0) {
VIBRATORERR("ioctl EVIOCGDURATION failed, errno = %d", errno);
return ret;
}
/* return the effect play length to vibrator service */
if (duration != NULL) {
*duration = data[1] * 1000 + data[2];
VIBRATORINFO("effectid = %d *duration = %" PRIu32, effect_id, *duration);
}
return ret;
}
/****************************************************************************
* Name: scale()
*
* Description:
* scale the amplitude with the given intensity.
*
* Input Parameters:
* amplitude - vibration amplitude, range 0 - 255
* intensity - vibration intensity
*
* Returned Value:
* return the scaled vibration amplitude
*
****************************************************************************/
static int scale(int amplitude, vibrator_intensity_e intensity)
{
int scale_amplitude;
switch (intensity) {
case VIBRATION_INTENSITY_LOW:
scale_amplitude = amplitude * 0.5;
break;
case VIBRATION_INTENSITY_MEDIUM:
scale_amplitude = amplitude * 0.7;
break;
case VIBRATION_INTENSITY_HIGH:
scale_amplitude = amplitude * 1;
break;
default:
scale_amplitude = VIBRATOR_MAX_AMPLITUDE;
}
return scale_amplitude;
}
/****************************************************************************
* Name: should_vibrate()
*
* Description:
* confirm whether vibration is allowed.
*
* Input Parameters:
* disabled - vibration disabled flag
*
* Returned Value:
* true: allowed, false: not allowed
*
****************************************************************************/
static bool should_vibrate(uint8_t disabled)
{
return !disabled;
}
/****************************************************************************
* Name: should_repeat()
*
* Description:
* confirm whether waveform repeat is allowed.
*
* Input Parameters:
* repeat - the index into the timings array at which to repeat
* timings - motor vibration time
* len - length of timings array
*
* Returned Value:
* true: allowed, false: not allowed
*
****************************************************************************/
static bool should_repeat(int repeat, uint32_t timings[],
uint8_t amplitude[], uint8_t len)
{
int ret = false;
if (repeat < 0)
return ret;
while (repeat < len) {
if (timings[repeat] != 0 && amplitude[repeat] > 0) {
ret = true;
break;
}
repeat++;
}
return ret;
}
/****************************************************************************
* Name: receive_stop()
*
* Description:
* stop vibration
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
*
* Returned Value:
* return stop ioctl value
*
****************************************************************************/
static int receive_stop(ff_dev_t* ff_dev)
{
return off(ff_dev);
}
/****************************************************************************
* Name: receive_start()
*
* Description:
* start a constant vibration with timeoutms
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* timeoutms - number of milliseconds to vibrate
*
* Returned Value:
* return stop ioctl value
*
****************************************************************************/
static int receive_start(ff_dev_t* ff_dev, uint32_t timeoutms)
{
int scale_amplitude;
int ret;
if (!should_vibrate(ff_dev->disabled))
return -ENOTSUP;
scale_amplitude = scale(ff_dev->curr_amplitude, ff_dev->intensity);
/* Note: ordering is important here! Many haptic drivers will reset their
amplitude when enabled, so we always have to enable first, then set
the amplitude. */
ret = on(ff_dev, timeoutms);
if (ret < 0) {
VIBRATORERR("Error: ioctl failed, errno = %d", errno);
}
return ff_set_amplitude(ff_dev, scale_amplitude);
}
/****************************************************************************
* Name: waveform_timer_cb()
*
* Description:
* callback function to wait for a given vibration time
*
* Input Parameters:
* timer - the handle of the uv timer
*
****************************************************************************/
static void waveform_timer_cb(uv_timer_t* timer)
{
threadargs* thread_args = timer->data;
vibrator_waveform_t* wave = &thread_args->wave;
ff_dev_t* ff_dev = thread_args->ff_dev;
uint16_t duration;
uint8_t amplitude;
uv_timer_stop(timer);
if (wave->count < wave->length) {
VIBRATORINFO("index(count) = %d", wave->count);
amplitude = scale(wave->amplitudes[wave->count], ff_dev->intensity);
duration = wave->timings[wave->count++];
if (amplitude != 0 && duration > 0) {
on(ff_dev, duration);
ff_set_amplitude(ff_dev, amplitude);
}
uv_timer_start(&thread_args->timer, waveform_timer_cb, duration, 0);
} else if (wave->repeat < 0) {
VIBRATORINFO("repeat < 0, play waveform exit");
} else {
wave->count = wave->repeat;
uv_timer_start(&thread_args->timer, waveform_timer_cb, 0, 0);
}
}
/****************************************************************************
* Name: receive_waveform()
*
* Description:
* receive waveform from vibrator_upper file
*
* Input Parameters:
* args - the args of threadargs
*
****************************************************************************/
static int receive_waveform(void* args)
{
threadargs* thread_args = args;
vibrator_waveform_t* wave = &thread_args->wave;
wave->count = 0;
if (!should_vibrate(thread_args->ff_dev->disabled))
return -ENOTSUP;
if (!should_repeat(wave->repeat, wave->timings,
wave->amplitudes, wave->length))
wave->repeat = -1;
return uv_timer_start(&thread_args->timer, waveform_timer_cb, 0, 0);
}
/****************************************************************************
* Name: compose_timer_cb()
*
* Description:
* callback function to wait for a given vibration time
*
* Input Parameters:
* timer - the handle of the uv timer
*
****************************************************************************/
static void compose_timer_cb(uv_timer_t* timer)
{
threadargs* thread_args = timer->data;
vibrator_compose_t* compose = &thread_args->composition;
ff_dev_t* ff_dev = thread_args->ff_dev;
int32_t play_length = 0;
uint8_t amplitude;
uv_timer_stop(timer);
VIBRATORINFO("index(count) = %d", compose->index);
amplitude = scale(compose->composite_effect[compose->index].scale * VIBRATOR_MAX_AMPLITUDE, ff_dev->intensity);
VIBRATORINFO("scale %f, primitive %d", compose->composite_effect[compose->index].scale, (int)compose->composite_effect[compose->index].primitive);
if (amplitude != 0) {
play_primitive(ff_dev, compose->composite_effect[compose->index].primitive, amplitude, (long*)&play_length);
}
compose->index++;
if (compose->index == compose->length) {
if (compose->repeat < 0) {
VIBRATORINFO("repeat < 0, play compose exit");
return;
}
VIBRATORINFO("repeat from index %d", compose->repeat);
compose->index = compose->repeat;
}
uv_timer_start(&thread_args->timer, compose_timer_cb, play_length + compose->composite_effect[compose->index].delay_ms, 0);
VIBRATORINFO("play_length %d delay_ms %d", (int)play_length, (int)compose->composite_effect[compose->index].delay_ms);
}
/****************************************************************************
* Name: receive_compose()
*
* Description:
* receive compose from vibrator_upper file
*
* Input Parameters:
* args - the args of threadargs
*
****************************************************************************/
static int receive_compose(void* args)
{
threadargs* thread_args = args;
vibrator_compose_t* compose = &thread_args->composition;
compose->index = 0;
if (!should_vibrate(thread_args->ff_dev->disabled))
return -ENOTSUP;
return uv_timer_start(&thread_args->timer, compose_timer_cb,
compose->composite_effect[compose->index].delay_ms, 0);
}
/****************************************************************************
* Name: interval_timer_cb()
*
* Description:
* callback function to wait for a given time interval
*
* Input Parameters:
* timer - the handle of the uv timer
*
****************************************************************************/
static void interval_timer_cb(uv_timer_t* timer)
{
threadargs* thread_args = (threadargs*)timer->data;
vibrator_waveform_t* wave = &thread_args->wave;
ff_dev_t* ff_dev = thread_args->ff_dev;
int32_t duration = wave->timings[0];
if (wave->count-- == 0) {
uv_timer_stop(timer);
return;
}
receive_start(ff_dev, duration);
}
/****************************************************************************
* Name: receive_interval()
*
* Description:
* receive receive_interval from vibrator_upper file
*
* Input Parameters:
* args - the args of threadargs
*
****************************************************************************/
static int receive_interval(void* args)
{
threadargs* thread_args = (threadargs*)args;
return uv_timer_start(&thread_args->timer, interval_timer_cb, 0,
thread_args->wave.timings[0] + thread_args->wave.timings[1]);
}
/****************************************************************************
* Name: receive_predefined()
*
* Description:
* receive predefined from vibrator_upper file
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* eff - effect struct, including effect id and effect strength
*
* Returned Value:
* return the play_effect value
*
****************************************************************************/
static int receive_predefined(ff_dev_t* ff_dev, vibrator_effect_t* eff)
{
int32_t play_length;
int ret;
if (!should_vibrate(ff_dev->disabled))
return -ENOTSUP;
ret = play_effect(ff_dev, eff->effect_id, eff->es, (long*)&play_length);
if (ret >= 0)
eff->play_length = play_length;
return ret;
}
/****************************************************************************
* Name: receive_primitive()
*
* Description:
* receive play primitive from vibrator_upper file
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* eff - effect struct, including effect id and effect strength
*
* Returned Value:
* return the play_effect value
*
****************************************************************************/
static int receive_primitive(ff_dev_t* ff_dev, vibrator_effect_t* eff)
{
int32_t play_length;
int ret;
if (!should_vibrate(ff_dev->disabled))
return -ENOTSUP;
ret = play_primitive(ff_dev, eff->effect_id, eff->amplitude, (long*)&play_length);
if (ret >= 0)
eff->play_length = play_length;
return ret;
}
/****************************************************************************
* Name: receive_set_intensity()
*
* Description:
* recevice set vibrator intensity operation from vibrator_upper file
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* intensity - vibration intensity
*
* Returned Value:
* return the ret of write
*
****************************************************************************/
static int receive_set_intensity(ff_dev_t* ff_dev,
vibrator_intensity_e intensity)
{
int ret;
ff_dev->intensity = intensity;
ret = property_set_int32(KVDB_KEY_VIBRATOR_MODE, ff_dev->intensity);
if (ret < 0) {
return ret;
}
return OK;
}
/****************************************************************************
* Name: receive_get_intensity()
*
* Description:
* recevice get vibrator intensity operation from vibrator_upper file
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* intensity - vibration intensity
*
* Returned Value:
* return OK(success)
*
****************************************************************************/
static int receive_get_intensity(ff_dev_t* ff_dev,
vibrator_intensity_e* intensity)
{
ff_dev->intensity = property_get_int32(KVDB_KEY_VIBRATOR_MODE,
ff_dev->intensity);
*intensity = ff_dev->intensity;
return OK;
}
/****************************************************************************
* Name: receive_set_disable()
*
* Description:
* recevice set vibrator disable operation from vibrator_upper file
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* disable - vibration disable
*
* Returned Value:
* return the ret of write
*
****************************************************************************/
static int receive_set_disable(ff_dev_t* ff_dev, uint8_t disable)
{
ff_dev->disabled = disable;
return property_set_bool(KVDB_KEY_VIBRATOR_DISABLE, !!disable);
}
/****************************************************************************
* Name: receive_is_disabled()
*
* Description:
* recevice get vibrator disabled operation from vibrator_upper file
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* disable - vibration disable
*
* Returned Value:
* return OK(success)
*
****************************************************************************/
static int receive_is_disabled(ff_dev_t* ff_dev, uint8_t* disable)
{
*disable = ff_dev->disabled;
return OK;
}
/****************************************************************************
* Name: receive_set_amplitude()
*
* Description:
* recevice set vibrator amplitude operation from vibrator_upper file
*
* Input Parameters:
* ff_dev - structure for operating the ff device driver
* amplitude - vibration amplitude, range 0 - 255
*
* Returned Value:
* return the ret of write
*
****************************************************************************/
static int receive_set_amplitude(ff_dev_t* ff_dev, uint8_t amplitude)
{
ff_dev->curr_amplitude = amplitude;
return ff_set_amplitude(ff_dev, amplitude);
}