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
16 #include "miscdevice_common.h"
17
18 #include "sensors_errors.h"
19
20 namespace OHOS {
21 namespace Sensors {
22 using namespace OHOS::HiviewDFX;
23
24 namespace {
25 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceCommon" };
26 constexpr int32_t MIN_VIBRATOR_COUNT = 0;
27 constexpr int32_t MAX_VIBRATOR_COUNT = 100;
28 constexpr int32_t MIN_VIBRATOR_INTENSITY = 0;
29 constexpr int32_t MAX_VIBRATOR_INTENSITY = 255;
30 constexpr int32_t HALF_AN_HOUR = 1800000; // ms
31 } // namespace
32
CheckCustomVibratorEffect(const std::vector<int32_t> & timing,const std::vector<int32_t> & intensity,int32_t periodCount)33 bool MiscdeviceCommon::CheckCustomVibratorEffect(const std::vector<int32_t> &timing,
34 const std::vector<int32_t> &intensity, int32_t periodCount)
35 {
36 if ((periodCount < MIN_VIBRATOR_COUNT) || (periodCount > MAX_VIBRATOR_COUNT)) {
37 MISC_HILOGE("input param invalid is failed");
38 return false;
39 }
40 if (timing.size() != intensity.size()) {
41 MISC_HILOGE("timing size invalid is failed");
42 return false;
43 }
44 int32_t totalTime = 0;
45 for (uint32_t i = 0; i < timing.size(); i = i + 2) {
46 totalTime += timing[i];
47 }
48 if (totalTime > HALF_AN_HOUR) {
49 MISC_HILOGE("totalTime invalid is failed");
50 return false;
51 }
52 for (uint32_t i = 0; i < intensity.size(); i++) {
53 if ((intensity[i] < MIN_VIBRATOR_INTENSITY) || (intensity[i] > MAX_VIBRATOR_INTENSITY)) {
54 return false;
55 }
56 }
57 return true;
58 }
59 } // namespace Sensors
60 } // namespace OHOS
61