• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "compatible_connection.h"
16 
17 #include <ctime>
18 #include <string>
19 #include <vector>
20 
21 #include "sensors_errors.h"
22 
23 namespace OHOS {
24 namespace Sensors {
25 using namespace OHOS::HiviewDFX;
26 namespace {
27 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "CompatibleConnection" };
28 std::vector<std::string> vibratorEffect_ = {"haptic.clock.timer"};
29 IVibratorHdiConnection::VibratorStopMode vibrateMode_;
30 }
31 uint32_t CompatibleConnection::duration_ = -1;
32 std::atomic_bool CompatibleConnection::isStop_ = false;
33 
ConnectHdi()34 int32_t CompatibleConnection::ConnectHdi()
35 {
36     CALL_LOG_ENTER;
37     return ERR_OK;
38 }
39 
StartOnce(uint32_t duration)40 int32_t CompatibleConnection::StartOnce(uint32_t duration)
41 {
42     CALL_LOG_ENTER;
43     duration_ = duration;
44     if (!vibrateThread_.joinable()) {
45         std::thread senocdDataThread(CompatibleConnection::VibrateProcess);
46         vibrateThread_ = std::move(senocdDataThread);
47         isStop_ = false;
48     }
49     vibrateMode_ = VIBRATOR_STOP_MODE_TIME;
50     return ERR_OK;
51 }
52 
Start(const std::string & effectType)53 int32_t CompatibleConnection::Start(const std::string &effectType)
54 {
55     CALL_LOG_ENTER;
56     if (std::find(vibratorEffect_.begin(), vibratorEffect_.end(), effectType) == vibratorEffect_.end()) {
57         MISC_HILOGE("Not support %{public}s type", effectType.c_str());
58         return VIBRATOR_ON_ERR;
59     }
60     if (!vibrateThread_.joinable()) {
61         std::thread senocdDataThread(CompatibleConnection::VibrateProcess);
62         vibrateThread_ = std::move(senocdDataThread);
63         isStop_ = false;
64     }
65     vibrateMode_ = VIBRATOR_STOP_MODE_PRESET;
66     return ERR_OK;
67 }
68 
Stop(VibratorStopMode mode)69 int32_t CompatibleConnection::Stop(VibratorStopMode mode)
70 {
71     CALL_LOG_ENTER;
72     if (mode < 0 || mode >= VIBRATOR_STOP_MODE_INVALID) {
73         MISC_HILOGE("mode: %{public}d invalid", mode);
74         return VIBRATOR_OFF_ERR;
75     }
76     if (vibrateMode_ != mode) {
77         MISC_HILOGE("should start vibrate first");
78         return VIBRATOR_OFF_ERR;
79     }
80     if (vibrateThread_.joinable()) {
81         MISC_HILOGI("stop vibrate thread");
82         isStop_ = true;
83         vibrateThread_.join();
84     }
85     return ERR_OK;
86 }
87 
DestroyHdiConnection()88 int32_t CompatibleConnection::DestroyHdiConnection()
89 {
90     CALL_LOG_ENTER;
91     return ERR_OK;
92 }
93 
VibrateProcess()94 void CompatibleConnection::VibrateProcess()
95 {
96     CALL_LOG_ENTER;
97     clock_t vibrateStartTime = clock();
98     while (static_cast<uint32_t>(clock() - vibrateStartTime) < duration_) {
99         if (isStop_) {
100             MISC_HILOGI("thread should stop");
101             break;
102         }
103     }
104     return;
105 }
106 }  // namespace Sensors
107 }  // namespace OHOS
108