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