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/platform_sensor_util.h"
18 
19 #ifdef GTEST
20 // This value is taken from the SMGR API definition.
21 #define SNS_SMGR_SAMPLING_RATE_INVERSION_POINT_V01 1000
22 #else
23 #include "sns_smgr_common_v01.h"
24 #endif  // GTEST
25 
26 namespace chre {
27 
intervalToSmgrSamplingRate(Nanoseconds interval)28 uint16_t intervalToSmgrSamplingRate(Nanoseconds interval) {
29   Milliseconds millis = Milliseconds(interval);
30   if (millis.getMilliseconds() > SNS_SMGR_SAMPLING_RATE_INVERSION_POINT_V01) {
31     return millis.getMilliseconds();
32   } else if (interval != Nanoseconds(0)) {
33     return static_cast<uint16_t>(
34         Seconds(1).toRawNanoseconds() / interval.toRawNanoseconds());
35   } else {
36     return 0;
37   }
38 }
39 
intervalToSmgrQ16ReportRate(Nanoseconds interval)40 uint32_t intervalToSmgrQ16ReportRate(Nanoseconds interval) {
41   uint64_t freq = UINT32_MAX;
42   if (interval != Nanoseconds(0)) {
43     freq = (Seconds(1).toRawNanoseconds() << 16) / interval.toRawNanoseconds();
44   }
45   return (freq > UINT32_MAX) ? UINT32_MAX : static_cast<uint32_t>(freq);
46 }
47 
48 }  // namespace chre
49