• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup vibrator
18  * @{
19  *
20  * @brief Provides functions for managing vibrators.
21  * @since 6
22  */
23 
24 /**
25  * @file vibrator_agent.h
26  *
27  * @brief Declares the functions for starting or stopping a vibrator.
28  * @since 6
29  */
30 #ifndef VIBRATOR_AGENT_H
31 #include <stdint.h>
32 #include <vector>
33 #include "vibrator_agent_type.h"
34 #define VIBRATOR_AGENT_H
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 namespace OHOS {
41 namespace Sensors {
42 /**
43  * @brief Indicates the mode of stopping a one-shot vibration effect.
44  *
45  * @since 6
46  */
47 static const char *VIBRATOR_STOP_MODE_TIME = "time";
48 
49 /**
50  * @brief Indicates the mode of stopping a preset vibration effect.
51  *
52  * @since 6
53  */
54 static const char *VIBRATOR_STOP_MODE_PRESET = "preset";
55 
56 /**
57  * @brief Controls this vibrator to perform a vibration with a preset vibration effect.
58  *
59  * @param effectId Indicates the preset vibration effect, which is described in {@link vibrator_agent_type.h}, for
60  * example:{@link VIBRATOR_TYPE_CLOCK_TIMER}: Describes the vibration effect of the vibrator when a user adjusts the
61  * timer.
62  * @return Returns <b>0</b> if the vibrator vibrates as expected; returns <b>-1</b> otherwise, for example, the preset
63  * vibration effect is not supported.
64  *
65  * @since 6
66  */
67 int32_t StartVibrator(const char *effectId);
68 
69 /**
70  * @brief Controls this vibrator to perform a one-shot vibration at a given duration.
71  *
72  * @param duration Indicates the duration that the one-shot vibration lasts, in milliseconds.
73  * @return Returns <b>0</b> if the vibrator vibrates as expected; returns <b>-1</b> otherwise, for example, the given
74  * duration for the one-shot vibration is invalid.
75  *
76  * @since 6
77  */
78 int32_t StartVibratorOnce(int32_t duration);
79 
80 /**
81  * @brief Query whether the device supports custom vibration.
82  *
83  * @return Returning true indicates support; otherwise, it indicates no support.
84  *
85  * @since 10
86  */
87 bool IsSupportVibratorCustom();
88 
89 /**
90  * @brief Play a custom vibration sequence.
91  *
92  * @param fd Indicates the file handle for custom vibration sequence.
93  * @param offset Indicates the starting address (in bytes) of the custom vibration sequence.
94  * @param length Indicates the total length (in bytes) of the custom vibration sequence.
95  * @return Returning 0 indicates success; otherwise, it indicates failure.
96  *
97  * @since 10
98  */
99 int32_t PlayVibratorCustom(int32_t fd, int64_t offset, int64_t length);
100 
101 /**
102  * @brief Sets the number of cycles for vibration.
103  * @param count Indicates the number of cycles for vibration.
104  * @since 9
105  */
106 bool SetLoopCount(int32_t count);
107 
108 /**
109  * @brief Stop the motor vibration according to the input mode.
110  *
111  * @param mode Indicates the mode of the vibration to stop. The values can be <b>time</b> and <b>preset</b>,
112  * respectively representing a one-shot vibration mode and a preset vibration mode.
113  * @return Returns <b>0</b> if the vibration is stopped as expected; returns <b>-1</b> otherwise.
114  * @since 6
115  */
116 int32_t StopVibrator(const char *mode);
117 
118 /**
119  * @brief Cancel the current motor vibration.
120  * @return Returning 0 indicates success; otherwise, it indicates failure.
121  * @since 10
122  */
123 int32_t Cancel();
124 
125 /**
126  * @brief Set the usage of vibration.
127  *
128  * @param usage Indicates the vibration usage, which is described in {@link vibrator_agent_type.h},for
129  * example:{@link USAGE_ALARM}: Describes the vibration is used for alarm.
130  *
131  * @since 9
132  */
133 bool SetUsage(int32_t usage, bool systemUsage = false);
134 
135 /**
136  * @brief Query whether the HdHaptic is supported.
137  *
138  * @return Returning true indicates support; otherwise, it indicates no support.
139  *
140  * @since 12
141  */
142 bool IsHdHapticSupported();
143 
144 /**
145  * @brief Query whether a vibration effect is supported.
146  *
147  * @param effectId Indicates the preset vibration effect, which is described in {@link vibrator_agent_type.h}, for
148  * example:{@link VIBRATOR_TYPE_CLOCK_TIMER}: Describes the vibration effect of the vibrator when a user adjusts the
149  * timer.
150  * @param state Indicates a pointer to the query result.
151  * @return Returning 0 indicates success; otherwise, it indicates failure.
152  *
153  * @since 10
154  */
155 int32_t IsSupportEffect(const char *effectId, bool *state);
156 
157 /**
158  * @brief Obtain the vibration delay, the time interval from the time the vibration is issued
159  *        to the time when the vibration is started, which can be used in the scene of sound
160  *        and vibration coordination.
161  * @param delayTime: Out of the parameter, return the vibration time delay, the time interval
162  *        from the time the vibration is issued to the start of the vibration, in milliseconds.
163  * @return 0 indicates success, otherwise indicates failure.
164  * @since 11
165  */
166 int32_t GetDelayTime(int32_t &delayTime);
167 
168 /**
169  * @brief Decodes the vibration media file and generates the vibration waveform.
170  * @param fileDescription: The resulting vibration sequence package, such as {@link VibrationPackage}.
171  * @param package: the resulting vibration sequence package, such as {@link VibrationPackage}.
172  * @return 0 indicates success, otherwise indicates failure.
173  * @since 11
174  */
175 int32_t PreProcess(const VibratorFileDescription &fd, VibratorPackage &package);
176 
177 /**
178  * @brief Analyze the complete waveform based on the seek time and return the waveform after the time point.
179  * @param seekTime: The seek time point.
180  * @param completePackage: the complete vibration sequence package, such as {@link VibrationPackage}.
181  * @param seekPackage: the resulting after the time point vibration sequence package, such as {@link VibrationPackage}.
182  * @return 0 indicates success, otherwise indicates failure.
183  * @since 16
184  */
185 int32_t SeekTimeOnPackage(int32_t seekTime, const VibratorPackage &completePackage, VibratorPackage &seekPackage);
186 
187 /**
188  * @brief Modulate a series of weaveform with another weaveform.
189  * @param modulationCurve: A weaveform indicating how to modulate, only modulationCurve.points,
190  * modulationCurve.pointNum, modulationCurve.time and modulationCurve.duration are used.
191  * @param curvePointNum: count of curvePoints
192  * @param duration: duration of curvePoints
193  * @param beforeModulationPackage: Weaveform to be modulated.
194  * @param afterModulationPackage: Result weaveform after modulation.
195  * @return 0 indicates success, otherwise indicates failure.
196  * @since 20
197  */
198 int32_t ModulatePackage(VibratorCurvePoint* modulationCurve, const int32_t curvePointNum, const int32_t duration,
199     const VibratorPackage &beforeModulationPackage, VibratorPackage &afterModulationPackage);
200 
201 /**
202  * @brief Free up the vibration sequence package memory.
203  * @param package: Vibration sequence packages, such as {@link VibrationPackage}.
204  * @return 0 indicates success, otherwise indicates failure.
205  * @since 11
206  */
207 int32_t FreeVibratorPackage(VibratorPackage &package);
208 
209 /**
210  * @brief Play the vibration sequence.
211  * @param pattern: Vibration sequences, such as {@link VibratorPattern}.
212  * @return 0 indicates success, otherwise indicates failure.
213  * @since 11
214  */
215 int32_t PlayPattern(const VibratorPattern &pattern);
216 
217 /**
218  * @brief Set the vibration effect adjustment parameters.
219  * @param parameter: Vibration adjustment parameter, such as {@link VibratorParameter}.
220  * @return true indicates success, otherwise indicates failure.
221  * @since 11
222  */
223 bool SetParameters(const VibratorParameter &parameter);
224 
225 /**
226  * @brief Control the vibrator to perform vibration with a preset vibration effect at a certain intensity.
227  *
228  * @param effectId Indicates the preset vibration effect, which is described in {@link vibrator_agent_type.h}, for
229  * example:{@link VIBRATOR_TYPE_HARD}: Describes the hard vibration effect of the vibrator.
230  * @param intensity Indicates the intensity of vibration, ranging from 1 to 100.
231  * @return Returns <b>0</b> if the vibrator vibrates as expected; otherwise indicates failure.
232  *
233  * @since 12
234  */
235 int32_t PlayPrimitiveEffect(const char *effectId, int32_t intensity);
236 
237 /**
238  * @brief Controls this vibrator to perform a vibration with a preset vibration effect.
239  *
240  * @param effectId Indicates the preset vibration effect, which is described in {@link vibrator_agent_type.h}, for
241  * example:{@link VIBRATOR_TYPE_CLOCK_TIMER}: Describes the vibration effect of the vibrator when a user adjusts the
242  * timer.
243  * @param param Indicate the device and vibrator information that needs to be controlled, which is described in
244  *  {@link vibrator_agent_type.h}.
245  * @return Returns <b>0</b> if the vibrator vibrates as expected; returns <b>-1</b> otherwise, for example, the preset
246  * vibration effect is not supported.
247  *
248  * @since 19
249  */
250 int32_t StartVibratorEnhanced(const VibratorIdentifier identifier, const char *effectId);
251 
252 /**
253  * @brief Controls this vibrator to perform a one-shot vibration at a given duration.
254  *
255  * @param duration Indicates the duration that the one-shot vibration lasts, in milliseconds.
256  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
257  *  {@link vibrator_agent_type.h}.
258  * @return Returns <b>0</b> if the vibrator vibrates as expected; returns <b>-1</b> otherwise, for example, the given
259  * duration for the one-shot vibration is invalid.
260  *
261  * @since 19
262  */
263 int32_t StartVibratorOnceEnhanced(const VibratorIdentifier identifier, int32_t duration);
264 
265 /**
266  * @brief Query whether the device supports custom vibration.
267  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
268  *  {@link vibrator_agent_type.h}.
269  * @return Returning true indicates support; otherwise, it indicates no support.
270  *
271  * @since 19
272  */
273 bool IsSupportVibratorCustomEnhanced(const VibratorIdentifier identifier);
274 
275 /**
276  * @brief Play a custom vibration sequence.
277  *
278  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
279  *  {@link vibrator_agent_type.h}.
280  * @param fd Indicates the file handle for custom vibration sequence.
281  * @param offset Indicates the starting address (in bytes) of the custom vibration sequence.
282  * @param length Indicates the total length (in bytes) of the custom vibration sequence.
283  * @return Returning 0 indicates success; otherwise, it indicates failure.
284  *
285  * @since 19
286  */
287 int32_t PlayVibratorCustomEnhanced(const VibratorIdentifier identifier, int32_t fd, int64_t offset, int64_t length);
288 
289 /**
290  * @brief Sets the number of cycles for vibration.
291  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
292  *  {@link vibrator_agent_type.h}.
293  * @param count Indicates the number of cycles for vibration.
294  * @since 19
295  */
296 bool SetLoopCountEnhanced(const VibratorIdentifier identifier, int32_t count);
297 
298 /**
299  * @brief Stop the motor vibration according to the input mode.
300  *
301  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
302  *  {@link vibrator_agent_type.h}.
303  * @param mode Indicates the mode of the vibration to stop. The values can be <b>time</b> and <b>preset</b>,
304  * respectively representing a one-shot vibration mode and a preset vibration mode.
305  * @return Returns <b>0</b> if the vibration is stopped as expected; returns <b>-1</b> otherwise.
306  * @since 19
307  */
308 int32_t StopVibratorEnhanced(const VibratorIdentifier identifier, const char *mode);
309 
310 /**
311  * @brief Cancel the current motor vibration.
312  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
313  *  {@link vibrator_agent_type.h}.
314  * @return Returning 0 indicates success; otherwise, it indicates failure.
315  * @since 19
316  */
317 int32_t CancelEnhanced(const VibratorIdentifier identifier);
318 
319 /**
320  * @brief Set the usage of vibration.
321  *
322  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
323  *  {@link vibrator_agent_type.h}.
324  * @param usage Indicates the vibration usage, which is described in {@link vibrator_agent_type.h},for
325  * example:{@link USAGE_ALARM}: Describes the vibration is used for alarm.
326  * @since 19
327  */
328 bool SetUsageEnhanced(const VibratorIdentifier identifier, int32_t usage, bool systemUsage = false);
329 
330 /**
331  * @brief Query whether the HdHaptic is supported.
332  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
333  *  {@link vibrator_agent_type.h}.
334  * @return Returning true indicates support; otherwise, it indicates no support.
335  *
336  * @since 19
337  */
338 bool IsHdHapticSupportedEnhanced(const VibratorIdentifier identifier);
339 
340 /**
341  * @brief Query whether a vibration effect is supported.
342  *
343  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
344  *  {@link vibrator_agent_type.h}.
345  * @param effectId Indicates the preset vibration effect, which is described in {@link vibrator_agent_type.h}, for
346  * example:{@link VIBRATOR_TYPE_CLOCK_TIMER}: Describes the vibration effect of the vibrator when a user adjusts the
347  * timer.
348  * @param state Indicates a pointer to the query result.
349  * @return Returning 0 indicates success; otherwise, it indicates failure.
350  *
351  * @since 19
352  */
353 int32_t IsSupportEffectEnhanced(const VibratorIdentifier identifier, const char *effectId, bool *state);
354 
355 /**
356  * @brief Obtain the vibration delay, the time interval from the time the vibration is issued
357  *        to the time when the vibration is started, which can be used in the scene of sound
358  *        and vibration coordination.
359  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
360  *  {@link vibrator_agent_type.h}.
361  * @param delayTime: Out of the parameter, return the vibration time delay, the time interval
362  *        from the time the vibration is issued to the start of the vibration, in milliseconds.
363  * @return 0 indicates success, otherwise indicates failure.
364  * @since 19
365  */
366 int32_t GetDelayTimeEnhanced(const VibratorIdentifier identifier, int32_t &delayTime);
367 
368 /**
369  * @brief Play the vibration sequence.
370  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
371  *  {@link vibrator_agent_type.h}.
372  * @param pattern: Vibration sequences, such as {@link VibratorPattern}.
373  * @return 0 indicates success, otherwise indicates failure.
374  * @since 19
375  */
376 int32_t PlayPatternEnhanced(const VibratorIdentifier identifier, const VibratorPattern &pattern);
377 
378 /**
379  * @brief Set the vibration effect adjustment parameters.
380  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
381  *  {@link vibrator_agent_type.h}.
382  * @param parameter: Vibration adjustment parameter, such as {@link VibratorParameter}.
383  * @return true indicates success, otherwise indicates failure.
384  * @since 19
385  */
386 bool SetParametersEnhanced(const VibratorIdentifier identifier, const VibratorParameter &parameter);
387 
388 /**
389  * @brief Control the vibrator to perform vibration with a preset vibration effect at a certain intensity.
390  *
391  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
392  *  {@link vibrator_agent_type.h}.
393  * @param effectId Indicates the preset vibration effect, which is described in {@link vibrator_agent_type.h}, for
394  * example:{@link VIBRATOR_TYPE_HARD}: Describes the hard vibration effect of the vibrator.
395  * @param intensity Indicates the intensity of vibration, ranging from 1 to 100.
396  * @return Returns <b>0</b> if the vibrator vibrates as expected; otherwise indicates failure.
397  *
398  * @since 19
399  */
400 int32_t PlayPrimitiveEffectEnhanced(const VibratorIdentifier identifier, const char *effectId, int32_t intensity);
401 
402 /**
403  * @brief Retrieve a list of vibrator IDs available for the specified parameters.
404  *
405  * This function fetches the identifiers of all vibrators that match the given parameters.
406  *
407  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
408  *  {@link vibrator_agent_type.h}.
409  * @param vibratorInfo A reference to a vector that will be populated with available vibrator information,
410  *  each represented as a `VibratorInfos` structure.
411  * @return Returns <b>0</b> if the operation is successful; otherwise, indicates failure with an error code.
412  *
413  * @since 19
414  */
415 int32_t GetVibratorList(const VibratorIdentifier& identifier, std::vector<VibratorInfos>& vibratorInfo);
416 
417 /**
418  * @brief Retrieve information about a specific vibration effect type.
419  *
420  * This function obtains detailed information regarding the specified vibration effect, such as its support status
421  * and duration.
422  *
423  * @param identifier Indicate the device and vibrator information that needs to be controlled, which is described in
424  *  {@link vibrator_agent_type.h}.
425  * @param effectType A string representing the type of vibration effect to query, as defined in
426  *  {@link vibrator_agent_type.h}.
427  * @param effectInfo A reference to an `EffectInfo` structure that will be populated with the effect's details.
428  * @return Returns <b>0</b> if the operation is successful; otherwise, indicates failure with an error code.
429  *
430  * @since 19
431  */
432 int32_t GetEffectInfo(const VibratorIdentifier& identifier, const std::string& effectType, EffectInfo& effectInfo);
433 
434 /**
435  * @brief Subscribe a user to receive updates from the vibrator.
436  *
437  * This function registers a specified user to receive notifications about vibrator state changes or events.
438  *
439  * @param user The user information that need to be subscribed to the vibrator notification, defined as `VibratorUser`.
440  * @return Returns <b>0</b> if the subscription is successful; otherwise, indicates failure with an error code.
441  *
442  * @since 19
443  */
444 int32_t SubscribeVibratorPlug(const VibratorUser& user);
445 
446 /**
447  * @brief Unsubscribe a user from receiving updates from the vibrator.
448  *
449  * This function removes a specified user from receiving notifications about vibrator state changes or events.
450  *
451  * @param user The user information that needs to be unsubscribed from the vibrator notifications,
452  *  defined as `VibratorUser`.
453  * @return Returns <b>0</b> if the unsubscription is successful; otherwise, indicates failure with an error code.
454  *
455  * @since 19
456  */
457 int32_t UnSubscribeVibratorPlug(const VibratorUser& user);
458 
459 /**
460  * @brief Play the vibration sequence.
461  * @param pattern: Vibration sequences with sessionId, such as {@link VibratorPattern}.
462  * @return 0 indicates success, otherwise indicates failure.
463  * @since 20
464  */
465 int32_t PlayPatternBySessionId(uint32_t sessionId, const VibratorPattern &pattern);
466 
467 /**
468  * @brief Play the vibration sequence.
469  * @param package: Vibration sequence packages, such as {@link VibratorPackage}.
470  * @return 0 indicates success, otherwise indicates failure.
471  * @since 20
472  */
473 int32_t PlayPackageBySessionId(uint32_t sessionId, const VibratorPackage &package);
474 
475 /**
476  * @brief Stop the motor vibration according to sessionId.
477  * @param sessionId Indicates the sessionId of the vibration to stop.
478  * @return Returns <b>0</b> if the vibration is stopped as expected; returns <b>-1</b> otherwise.
479  * @since 20
480  */
481 int32_t StopVibrateBySessionId(uint32_t sessionId);
482 } // namespace Sensors
483 } // namespace OHOS
484 #ifdef __cplusplus
485 };
486 #endif
487 /** @} */
488 #endif // endif VIBRATOR_AGENT_H