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