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 <android-base/chrono_utils.h>
20 #include <android-base/unique_fd.h>
21 #include <linux/genetlink.h>
22 #include <netlink/genl/ctrl.h>
23 #include <netlink/genl/genl.h>
24 #include <utils/Looper.h>
25 #include <utils/Thread.h>
26 
27 #include <chrono>
28 #include <condition_variable>
29 #include <future>
30 #include <list>
31 #include <mutex>
32 #include <set>
33 #include <string>
34 #include <thread>
35 #include <unordered_map>
36 #include <vector>
37 
38 namespace aidl {
39 namespace android {
40 namespace hardware {
41 namespace thermal {
42 namespace implementation {
43 
44 using ::android::base::boot_clock;
45 using ::android::base::unique_fd;
46 using WatcherCallback = std::function<std::chrono::milliseconds(
47         const std::unordered_map<std::string, float> &uevent_sensor_map)>;
48 
49 // A helper class for monitoring thermal files changes.
50 class ThermalWatcher : public ::android::Thread {
51   public:
ThermalWatcher(const WatcherCallback & cb)52     explicit ThermalWatcher(const WatcherCallback &cb)
53         : Thread(false), cb_(cb), looper_(new ::android::Looper(true)) {}
54     ~ThermalWatcher() = default;
55 
56     // Disallow copy and assign.
57     ThermalWatcher(const ThermalWatcher &) = delete;
58     void operator=(const ThermalWatcher &) = delete;
59 
60     // Start the thread and return true if it succeeds.
61     bool startWatchingDeviceFiles();
62     // Give the file watcher a list of files to start watching. This helper
63     // class will by default wait for modifications to the file with a looper.
64     // This should be called before starting watcher thread.
65     // For monitoring uevents.
66     void registerFilesToWatch(const std::set<std::string> &sensors_to_watch);
67     // For monitoring thermal genl events.
68     void registerFilesToWatchNl(const std::set<std::string> &sensors_to_watch);
69     // Wake up the looper thus the worker thread, immediately. This can be called
70     // in any thread.
71     void wake();
72 
73   private:
74     // The work done by the watcher thread. This will use inotify to check for
75     // modifications to the files to watch. If any modification is seen this
76     // will callback the registered function with the new data read from the
77     // modified file.
78     bool threadLoop() override;
79 
80     // Parse uevent message
81     void parseUevent(std::unordered_map<std::string, float> *sensor_map);
82 
83     // Parse thermal netlink message
84     void parseGenlink(std::unordered_map<std::string, float> *sensor_map);
85 
86     // Maps watcher filer descriptor to watched file path.
87     std::unordered_map<int, std::string> watch_to_file_path_map_;
88 
89     // The callback function. Called whenever thermal uevent is seen.
90     // The function passed in should expect a string in the form (type).
91     // Where type is the name of the thermal zone that trigger a uevent notification.
92     // Callback will return thermal trigger status for next polling decision.
93     const WatcherCallback cb_;
94 
95     ::android::sp<::android::Looper> looper_;
96 
97     // For uevent socket registration.
98     ::android::base::unique_fd uevent_fd_;
99     // For thermal genl socket registration.
100     ::android::base::unique_fd thermal_genl_fd_;
101     // Sensor list which monitor flag is enabled.
102     std::set<std::string> monitored_sensors_;
103     // Sleep interval voting result
104     std::chrono::milliseconds sleep_ms_;
105     // Timestamp for last thermal update
106     boot_clock::time_point last_update_time_;
107     // For thermal genl socket object.
108     struct nl_sock *sk_thermal;
109 };
110 
111 }  // namespace implementation
112 }  // namespace thermal
113 }  // namespace hardware
114 }  // namespace android
115 }  // namespace aidl
116