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