1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <aidl/android/hardware/thermal/Temperature.h>
20 #include <android-base/chrono_utils.h>
21 
22 #include <chrono>
23 #include <shared_mutex>
24 #include <string_view>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include "thermal_info.h"
29 
30 namespace aidl {
31 namespace android {
32 namespace hardware {
33 namespace thermal {
34 namespace implementation {
35 
36 using ::android::base::boot_clock;
37 constexpr int kToleranceIntervalMs = 1000;
38 
39 struct PredictionSample {
PredictionSamplePredictionSample40     PredictionSample(int num_out_samples) {
41         timestamp = boot_clock::time_point::min();
42         values = std::vector<float>(num_out_samples, NAN);
43     }
44     boot_clock::time_point timestamp;
45     std::vector<float> values;
46 };
47 
48 struct PredictorSensorInfo {
49     std::string sensor_name;
50     int sample_duration;
51     int num_out_samples;
52     std::vector<PredictionSample> samples;
53     int cur_index;
54 };
55 
56 struct PredictedSensorInfo {
57     std::string sensor_name;
58     std::string linked_sensor;
59     int duration;
60     int prediction_index;
61 };
62 
63 class ThermalPredictionsHelper {
64   public:
65     ThermalPredictionsHelper() = default;
66     ~ThermalPredictionsHelper() = default;
67     // Disallow copy and assign
68     ThermalPredictionsHelper(const ThermalPredictionsHelper &) = delete;
69     void operator=(const ThermalPredictionsHelper &) = delete;
70 
71     bool initializePredictionSensors(
72             const std::unordered_map<std::string, SensorInfo> &sensor_info_map);
73     bool updateSensor(std::string_view sensor_name, std::vector<float> &values);
74     SensorReadStatus readSensor(std::string_view sensor_name, float *temp);
75 
76   private:
77     std::unordered_map<std::string, PredictorSensorInfo> predictor_sensors_;
78     std::unordered_map<std::string, PredictedSensorInfo> predicted_sensors_;
79     mutable std::shared_mutex sensor_predictions_mutex_;
80 
81     bool registerPredictedSensor(std::string_view sensor_name, std::string_view linked_sensor,
82                                  int duration);
83     bool registerPredictorSensor(std::string_view sensor_name, int sample_duration,
84                                  int num_out_samples);
85 };
86 
87 }  // namespace implementation
88 }  // namespace thermal
89 }  // namespace hardware
90 }  // namespace android
91 }  // namespace aidl
92