-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvibrator_api.c
486 lines (420 loc) · 14.8 KB
/
vibrator_api.c
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
/****************************************************************************
* 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.
****************************************************************************/
/****************************************************************************
* @brief Included Files
****************************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <netpacket/rpmsg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "vibrator_internal.h"
/****************************************************************************
* @brief Private Functions
****************************************************************************/
/**
* @brief Fill the vibrator message header
*
* @details This function fills the vibrator message header using the specified type.
*
* @param buffer The buffer of the vibrator_msg_tS.
*/
static void vibrator_msg_packet(vibrator_msg_t* buffer)
{
switch (buffer->type) {
case VIBRATION_WAVEFORM:
case VIBRATION_INTERVAL:
buffer->request_len = VIBRATOR_MSG_HEADER + sizeof(vibrator_waveform_t);
buffer->response_len = VIBRATOR_MSG_RESULT;
break;
case VIBRATION_EFFECT:
buffer->request_len = VIBRATOR_MSG_HEADER + sizeof(vibrator_effect_t);
buffer->response_len = VIBRATOR_MSG_HEADER + sizeof(vibrator_effect_t);
break;
case VIBRATION_START:
buffer->request_len = VIBRATOR_MSG_HEADER + sizeof(uint32_t);
buffer->response_len = VIBRATOR_MSG_RESULT;
break;
case VIBRATION_STOP:
buffer->request_len = VIBRATOR_MSG_HEADER;
buffer->response_len = VIBRATOR_MSG_RESULT;
break;
case VIBRATION_SET_AMPLITUDE:
buffer->request_len = VIBRATOR_MSG_HEADER + sizeof(uint8_t);
buffer->response_len = VIBRATOR_MSG_RESULT;
break;
case VIBRATION_GET_CAPABLITY:
buffer->request_len = VIBRATOR_MSG_HEADER;
buffer->response_len = VIBRATOR_MSG_HEADER + sizeof(int32_t);
break;
case VIBRATION_GET_INTENSITY:
buffer->request_len = VIBRATOR_MSG_HEADER;
buffer->response_len = VIBRATOR_MSG_HEADER + sizeof(int32_t);
break;
case VIBRATION_SET_INTENSITY:
buffer->request_len = sizeof(vibrator_intensity_e) + VIBRATOR_MSG_HEADER;
buffer->response_len = VIBRATOR_MSG_RESULT;
break;
case VIBRATION_CALIBRATE:
buffer->request_len = VIBRATOR_MSG_HEADER;
buffer->response_len = VIBRATOR_MSG_HEADER + VIBRATOR_CALIBVALUE_MAX;
break;
case VIBRATION_SET_CALIBVALUE:
buffer->request_len = VIBRATOR_MSG_HEADER + VIBRATOR_CALIBVALUE_MAX;
buffer->response_len = VIBRATOR_MSG_RESULT;
break;
default:
VIBRATORERR("unknown message type %d", buffer->type);
buffer->request_len = sizeof(vibrator_msg_t);
buffer->response_len = sizeof(vibrator_msg_t);
break;
}
}
/**
* @brief Open message queue
*
* @details This function opens the message queue for vibrator messages.
*
* @param buffer The type of the vibrator_msg_t.
*
* @return Returns a flag indicating whether the vibration is sent.
*/
static int vibrator_commit(vibrator_msg_t* buffer)
{
int fd;
int ret;
#ifdef CONFIG_VIBRATOR_SERVER
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
.sun_path = PROP_SERVER_PATH,
};
#else
struct sockaddr_rpmsg addr = {
.rp_family = AF_RPMSG,
.rp_name = PROP_SERVER_PATH,
.rp_cpu = CONFIG_VIBRATOR_SERVER_CPUNAME,
};
#endif
#ifdef CONFIG_VIBRATOR_SERVER
fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
#else
fd = socket(AF_RPMSG, SOCK_STREAM | SOCK_CLOEXEC, 0);
#endif
if (fd < 0) {
VIBRATORERR("socket fail, errno = %d", errno);
return fd;
}
ret = connect(fd, (const struct sockaddr*)&addr, sizeof(addr));
if (ret < 0) {
VIBRATORERR("client: connect failure, errno = %d", errno);
ret = -errno;
goto errout;
}
vibrator_msg_packet(buffer);
ret = send(fd, buffer, buffer->request_len, 0);
if (ret < 0) {
VIBRATORERR("send fail, errno = %d", errno);
ret = -errno;
goto errout;
}
ret = recv(fd, buffer, sizeof(vibrator_msg_t), 0);
if (ret < buffer->response_len) {
VIBRATORERR("recv fail, errno = %d", errno);
ret = ret < 0 ? -errno : -EINVAL;
goto errout;
}
VIBRATORINFO("recv len = %d, result = %" PRIi32, ret, buffer->result);
ret = buffer->result;
errout:
close(fd);
return ret;
}
/****************************************************************************
* @brief Public Functions
*
* @details This file contains nine interfaces vibrator_play_waveform,
* vibrator_play_oneshot, vibrator_play_predefined, vibrator_get_intensity,
* vibrator_set_intensity, vibrator_cancel, vibrator_start,
* vibrator_set_amplitude, and vibrator_get_capabilities
* and the detailed information of each interface has been described
*
****************************************************************************/
/**
* @brief Play a waveform vibration.
*
* @param timings The pattern of alternating on-off timings, starting with off.
* Timing values of 0 will cause the timing/amplitude pair to be ignored.
* @param amplitudes The amplitude values of the timing/amplitude pairs.
* Amplitude values must be between 0 and 255, or equal to DEFAULT_AMPLITUDE.
* An amplitude value of 0 implies the motor is off.
* @param repeat The index into the timings array at which to repeat, or -1 if
* you don't want to repeat.
* @param length The length of timings and amplitudes pairs.
* @return Returns the flag that the vibrator is playing waveform.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_play_waveform(uint32_t timings[], uint8_t amplitudes[],
int8_t repeat, uint8_t length)
{
vibrator_waveform_t wave;
vibrator_msg_t buffer;
if (repeat < -1 || repeat >= length)
return -EINVAL;
wave.length = length;
wave.repeat = repeat;
memcpy(wave.timings, timings, sizeof(uint32_t) * length);
memcpy(wave.amplitudes, amplitudes, sizeof(uint8_t) * length);
buffer.type = VIBRATION_WAVEFORM;
buffer.wave = wave;
return vibrator_commit(&buffer);
}
/**
* @brief Play composed primitive effect.
*
* @param composite_effects The composition of primitive effects.
* @param repeat The index into the primitive array at which to repeat, or -1 if
* you don't want to repeat.
* @param length The length of composite effects array.
* @return Returns the flag that the vibrator is playing the predefined effect.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_play_compose(vibrator_composite_effect_t* composite_effects,
int8_t repeat, uint8_t length)
{
vibrator_msg_t buffer;
if (repeat < -1 || repeat >= length)
return -EINVAL;
buffer.type = VIBRATION_COMPOSITION;
buffer.composition.length = length;
buffer.composition.repeat = repeat;
buffer.composition.index = 0;
memcpy(buffer.composition.composite_effect, composite_effects,
sizeof(vibrator_composite_effect_t) * length);
return vibrator_commit(&buffer);
}
/**
* @brief Play an interval vibration with specified duration and interval.
*
* @param duration The duration of vibration.
* @param interval The time interval between two vibrations.
* @param count The number of vibrations.
* @return Returns the flag that the vibrator is playing interval.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_play_interval(int32_t duration, int32_t interval,
int16_t count)
{
vibrator_msg_t buffer;
if (duration <= 0 || interval < 0 || count < 0)
return -EINVAL;
buffer.type = VIBRATION_INTERVAL;
buffer.wave.timings[0] = duration;
buffer.wave.timings[1] = interval;
buffer.wave.count = count;
return vibrator_commit(&buffer);
}
/**
* @brief Play a one-shot vibration.
*
* @param timing The number of milliseconds to vibrate. Must be positive.
* @param amplitude The amplitude of vibration; must be a value between 1 and 255, or DEFAULT_AMPLITUDE.
* @return Returns the flag that the vibrator is playing one shot.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_play_oneshot(uint32_t timing, uint8_t amplitude)
{
return vibrator_play_waveform(&timing, &litude, -1, 1);
}
/**
* @brief Play a predefined vibration effect.
*
* @param effect_id The ID of the effect to perform.
* @param es The vibration intensity.
* @param play_length Returned effect play duration.
* @return Returns the flag that the vibrator is playing the predefined effect.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_play_predefined(uint8_t effect_id, vibrator_effect_strength_e es,
int32_t* play_length)
{
vibrator_msg_t buffer;
int ret;
if (es < VIBRATION_LIGHT || es > VIBRATION_DEFAULTES)
return -EINVAL;
buffer.type = VIBRATION_EFFECT;
buffer.effect.effect_id = effect_id;
buffer.effect.es = es;
ret = vibrator_commit(&buffer);
if (ret >= 0) {
if (play_length != NULL)
*play_length = buffer.effect.play_length;
}
return ret;
}
/**
* @brief Play a predefined vibration effect with the specified amplitude.
*
* @param effect_id The ID of the effect to perform.
* @param amplitude Vibration amplitude (0.0~1.0).
* @param play_length Returned effect play duration.
* @return Returns the flag that the vibrator is playing the predefined effect.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_play_primitive(uint8_t effect_id, float amplitude,
int32_t* play_length)
{
vibrator_msg_t buffer;
int ret;
if (amplitude < 0.0 || amplitude > 1.0)
return -EINVAL;
buffer.type = VIBRATION_PRIMITIVE;
buffer.effect.effect_id = effect_id;
buffer.effect.amplitude = amplitude;
ret = vibrator_commit(&buffer);
if (ret >= 0) {
if (play_length != NULL)
*play_length = buffer.effect.play_length;
}
return ret;
}
/**
* @brief Get vibration intensity.
*
* @param intensity Buffer that stores intensity.
* @return Returns the flag indicating success in getting vibrator intensity.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_get_intensity(vibrator_intensity_e* intensity)
{
vibrator_msg_t buffer;
int ret;
buffer.type = VIBRATION_GET_INTENSITY;
ret = vibrator_commit(&buffer);
if (ret >= 0)
*intensity = buffer.intensity;
return ret;
}
/**
* @brief Set vibration intensity.
*
* @param intensity The vibration intensity.
* @return Returns the flag indicating whether setting the intensity was successful.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_set_intensity(vibrator_intensity_e intensity)
{
vibrator_msg_t buffer;
if (intensity < VIBRATION_INTENSITY_LOW || intensity > VIBRATION_INTENSITY_OFF)
return -EINVAL;
buffer.type = VIBRATION_SET_INTENSITY;
buffer.intensity = intensity;
return vibrator_commit(&buffer);
}
/**
* @brief Cancel the vibration.
*
* @return Returns the flag that the vibration is stopped.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_cancel(void)
{
vibrator_msg_t buffer;
buffer.type = VIBRATION_STOP;
return vibrator_commit(&buffer);
}
/**
* @brief Start the vibrator with vibrate time.
*
* @param timeoutms Number of milliseconds to vibrate.
* @return Returns the flag that the vibration has started.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_start(int32_t timeoutms)
{
vibrator_msg_t buffer;
buffer.type = VIBRATION_START;
buffer.timeoutms = timeoutms;
return vibrator_commit(&buffer);
}
/**
* @brief Set vibration amplitude.
*
* @param amplitude The amplitude of vibration; must be a value between 1 and 255, or DEFAULT_AMPLITUDE.
* @return Returns the flag indicating whether setting the vibrator amplitude was successful.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_set_amplitude(uint8_t amplitude)
{
vibrator_msg_t buffer;
buffer.type = VIBRATION_SET_AMPLITUDE;
buffer.amplitude = amplitude;
return vibrator_commit(&buffer);
}
/**
* @brief Get vibration capabilities.
*
* @param capabilities Buffer that stores capabilities.
* @return Returns the flag indicating success in getting vibrator capability.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_get_capabilities(int32_t* capabilities)
{
vibrator_msg_t buffer;
int ret;
buffer.type = VIBRATION_GET_CAPABLITY;
buffer.capabilities = 0;
ret = vibrator_commit(&buffer);
if (ret >= 0)
*capabilities = buffer.capabilities;
return ret;
}
/**
* @brief Calibrate vibrator when it is not calibrated, Generally at the time of leaving the factory.
*
* @param data Buffer that stores the calibration result data.
* @return Returns the flag indicating whether the vibrator calibration was successful.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_calibrate(uint8_t* data)
{
vibrator_msg_t buffer;
int ret;
buffer.type = VIBRATION_CALIBRATE;
memset(buffer.calibvalue, 0, VIBRATOR_CALIBVALUE_MAX);
ret = vibrator_commit(&buffer);
if (ret >= 0)
memcpy(data, buffer.calibvalue, VIBRATOR_CALIBVALUE_MAX);
return ret;
}
/**
* @brief Get vibration calibration data.
*
* @param data Buffer that stores calibration data.
* @return Returns the flag indicating success in setting vibrator calibration data.
* Greater than or equal to 0 means success; otherwise, it means failure.
*/
int vibrator_set_calibvalue(uint8_t* data)
{
vibrator_msg_t buffer;
buffer.type = VIBRATION_SET_CALIBVALUE;
memcpy(buffer.calibvalue, data, VIBRATOR_CALIBVALUE_MAX);
return vibrator_commit(&buffer);
}