1 /*
2  * Copyright (c) 2022 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 #ifndef VIBRATOR_THREAD_H
17 #define VIBRATOR_THREAD_H
18 
19 #include <condition_variable>
20 #include <cstdint>
21 #include <thread>
22 
23 #include "thread_ex.h"
24 
25 #include "vibrator_hdi_connection.h"
26 #include "vibrator_infos.h"
27 
28 namespace OHOS {
29 namespace Sensors {
30 class VibratorThread : public Thread {
31 public:
32     void UpdateVibratorEffect(const VibrateInfo &vibrateInfo);
33     VibrateInfo GetCurrentVibrateInfo();
34     void SetExitStatus(bool status);
35     void WakeUp();
36 
37 protected:
38     virtual bool Run();
39 
40 private:
41     int32_t PlayOnce(const VibrateInfo &info);
42     int32_t PlayEffect(const VibrateInfo &info);
43     int32_t PlayCustomByHdHptic(const VibrateInfo &info);
44     void HandleMultipleVibrations();
45     int32_t PlayCustomByCompositeEffect(const VibrateInfo &info);
46     int32_t PlayCompositeEffect(const VibrateInfo &info, const HdfCompositeEffect &hdfCompositeEffect);
47     std::mutex currentVibrationMutex_;
48     VibrateInfo currentVibration_;
49     std::mutex vibrateMutex_;
50     std::condition_variable cv_;
51     std::atomic<bool> exitFlag_ = false;
52 };
53 #define VibratorDevice VibratorHdiConnection::GetInstance()
54 }  // namespace Sensors
55 }  // namespace OHOS
56 #endif  // VIBRATOR_THREAD_H