• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "vibrator_thread.h"
17 
18 #include "sensors_errors.h"
19 
20 namespace OHOS {
21 namespace Sensors {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibratorThread" };
24 }  // namespace
25 
Run()26 bool VibratorThread::Run()
27 {
28     VibrateInfo info = GetCurrentVibrateInfo();
29     std::unique_lock<std::mutex> vibrateLck(vibrateMutex_);
30     if (info.mode == "time") {
31         int32_t ret = VibratorDevice.StartOnce(static_cast<uint32_t>(info.duration));
32         if (ret != SUCCESS) {
33             MISC_HILOGE("StartOnce fail, duration:%{public}d, package:%{public}s",
34                 info.duration, info.packageName.c_str());
35             return false;
36         }
37         cv_.wait_for(vibrateLck, std::chrono::milliseconds(info.duration));
38         VibratorDevice.Stop(HDF_VIBRATOR_MODE_ONCE);
39         std::unique_lock<std::mutex> readyLck(readyMutex_);
40         if (ready_) {
41             MISC_HILOGI("Stop duration:%{public}d, package:%{public}s",
42                 info.duration, info.packageName.c_str());
43             SetReadyStatus(false);
44             return false;
45         }
46     } else if (info.mode == "preset") {
47         for (int32_t i = 0; i < info.count; ++i) {
48             std::string effect = info.effect;
49             int32_t ret = VibratorDevice.Start(effect);
50             if (ret != SUCCESS) {
51                 MISC_HILOGE("Vibrate effect %{public}s failed, package:%{public}s",
52                     effect.c_str(), info.packageName.c_str());
53                 return false;
54             }
55             cv_.wait_for(vibrateLck, std::chrono::milliseconds(info.duration));
56             VibratorDevice.Stop(HDF_VIBRATOR_MODE_PRESET);
57             std::unique_lock<std::mutex> readyLck(readyMutex_);
58             if (ready_) {
59                 MISC_HILOGI("Stop effect %{public}s, package:%{public}s",
60                     effect.c_str(), info.packageName.c_str());
61                 SetReadyStatus(false);
62                 return false;
63             }
64         }
65     }
66     return false;
67 }
68 
UpdateVibratorEffect(VibrateInfo info)69 void VibratorThread::UpdateVibratorEffect(VibrateInfo info)
70 {
71     std::unique_lock<std::mutex> lck(currentVibrationMutex_);
72     currentVibration_ = info;
73 }
74 
GetCurrentVibrateInfo()75 VibrateInfo VibratorThread::GetCurrentVibrateInfo()
76 {
77     std::unique_lock<std::mutex> lck(currentVibrationMutex_);
78     return currentVibration_;
79 }
80 
SetReadyStatus(bool status)81 void VibratorThread::SetReadyStatus(bool status)
82 {
83     ready_ = status;
84 }
85 
NotifyExit()86 void VibratorThread::NotifyExit()
87 {
88     std::unique_lock<std::mutex> readyLck(readyMutex_);
89     SetReadyStatus(true);
90     cv_.notify_one();
91 }
92 }  // namespace Sensors
93 }  // namespace OHOS
94