1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "chre/platform/slpi/smgr/platform_sensor_util.h" 18 19 #include <algorithm> 20 21 #ifdef GTEST 22 // This value is taken from the SMGR API definition. 23 #define SNS_SMGR_SAMPLING_RATE_INVERSION_POINT_V01 1000 24 #else 25 #include "sns_smgr_common_v01.h" 26 #endif // GTEST 27 28 namespace chre { 29 intervalToSmgrSamplingRate(Nanoseconds interval)30uint16_t intervalToSmgrSamplingRate(Nanoseconds interval) { 31 constexpr uint64_t kInversionPoint = 32 SNS_SMGR_SAMPLING_RATE_INVERSION_POINT_V01; 33 uint16_t smgrRate = 0; 34 Milliseconds millis = Milliseconds(interval); 35 36 if (millis.getMilliseconds() > kInversionPoint) { 37 constexpr uint64_t kMaxInterval = INT16_MAX; 38 39 smgrRate = static_cast<uint16_t>( 40 std::min(millis.getMilliseconds(), kMaxInterval)); 41 } else if (interval != Nanoseconds(0)) { 42 constexpr uint64_t kMaxRate = kInversionPoint; 43 44 uint64_t rateHz = 45 (Seconds(1).toRawNanoseconds() / interval.toRawNanoseconds()); 46 smgrRate = static_cast<uint16_t>(std::min(rateHz, kMaxRate)); 47 } 48 49 return smgrRate; 50 } 51 intervalToSmgrQ16ReportRate(Nanoseconds interval)52uint32_t intervalToSmgrQ16ReportRate(Nanoseconds interval) { 53 // Q16 is interpreted as a signed integer by SMGR, but passed through QMI as a 54 // uint32_t 55 constexpr uint32_t kMaxFreq = INT32_MAX; 56 uint64_t freq = kMaxFreq; 57 58 if (interval != Nanoseconds(0)) { 59 freq = (Seconds(1).toRawNanoseconds() << 16) / interval.toRawNanoseconds(); 60 } 61 62 return (freq > kMaxFreq) ? kMaxFreq : static_cast<uint32_t>(freq); 63 } 64 65 } // namespace chre 66