1 /*
2  * Copyright (C) 2018 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 #ifndef HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H
19 
20 #include <aidl/android/frameworks/stats/IStats.h>
21 #include <android-base/chrono_utils.h>
22 #include <pixelstats/BatteryCapacityReporter.h>
23 #include <pixelstats/PcaChargeStats.h>
24 #include <pixelstats/WirelessChargeStats.h>
25 #include <pixelstats/WlcReporter.h>
26 
27 namespace android {
28 namespace hardware {
29 namespace google {
30 namespace pixel {
31 
32 using aidl::android::frameworks::stats::IStats;
33 
34 /**
35  * A class to listen for uevents and report reliability events to
36  * the PixelStats HAL.
37  * Runs in a background thread if created with ListenForeverInNewThread().
38  * Alternatively, process one message at a time with ProcessUevent().
39  */
40 class UeventListener {
41   public:
42     /* Both WirelessChargerPtmcUevent and WirelessChargerPtmcPath is use to get
43      * wireless charger ptmx id, for most case we don't need asisign both of
44      * them.
45      **/
46     struct UeventPaths {
47         const char *const AudioUevent;
48         const char *const SsocDetailsPath;
49         const char *const OverheatPath;
50         const char *const ChargeMetricsPath;
51         const char *const TypeCPartnerUevent;
52         const char *const TypeCPartnerVidPath;
53         const char *const TypeCPartnerPidPath;
54         const char *const WirelessChargerPtmcUevent;
55         const char *const WirelessChargerPtmcPath;
56     };
57     constexpr static const char *const ssoc_details_path =
58             "/sys/class/power_supply/battery/ssoc_details";
59     constexpr static const char *const overheat_path_default =
60             "/sys/devices/platform/soc/soc:google,overheat_mitigation";
61     constexpr static const char *const charge_metrics_path_default =
62             "/sys/class/power_supply/battery/charge_stats";
63     constexpr static const char *const typec_partner_vid_path_default =
64             "/sys/class/typec/port0-partner/identity/id_header";
65     constexpr static const char *const typec_partner_pid_path_default =
66             "/sys/class/typec/port0-partner/identity/product";
67     constexpr static const char *const typec_partner_uevent_default = "DEVTYPE=typec_partner";
68 
69     UeventListener(const std::string audio_uevent, const std::string ssoc_details_path = "",
70                    const std::string overheat_path = overheat_path_default,
71                    const std::string charge_metrics_path = charge_metrics_path_default,
72                    const std::string typec_partner_vid_path = typec_partner_vid_path_default,
73                    const std::string typec_partner_pid_path = typec_partner_pid_path_default);
74     UeventListener(const struct UeventPaths &paths);
75 
76     bool ProcessUevent();  // Process a single Uevent.
77     void ListenForever();  // Process Uevents forever
78 
79   private:
80     bool ReadFileToInt(const std::string &path, int *val);
81     bool ReadFileToInt(const char *path, int *val);
82     void ReportMicStatusUevents(const std::shared_ptr<IStats> &stats_client, const char *devpath,
83                                 const char *mic_status);
84     void ReportMicBrokenOrDegraded(const std::shared_ptr<IStats> &stats_client, const int mic,
85                                    const bool isBroken);
86     void ReportUsbPortOverheatEvent(const std::shared_ptr<IStats> &stats_client,
87                                     const char *driver);
88     void ReportChargeStats(const std::shared_ptr<IStats> &stats_client, const std::string line,
89                            const std::string wline_at, const std::string wline_ac,
90                            const std::string pca_line);
91     void ReportVoltageTierStats(const std::shared_ptr<IStats> &stats_client, const char *line,
92                                 const bool has_wireless, const std::string wfile_contents);
93     void ReportChargeMetricsEvent(const std::shared_ptr<IStats> &stats_client, const char *driver);
94     void ReportWlc(const std::shared_ptr<IStats> &stats_client, const bool pow_wireless,
95                    const bool online, const char *ptmc);
96     void ReportBatteryCapacityFGEvent(const std::shared_ptr<IStats> &stats_client,
97                                       const char *subsystem);
98     void ReportTypeCPartnerId(const std::shared_ptr<IStats> &stats_client);
99 
100     const std::string kAudioUevent;
101     const std::string kBatterySSOCPath;
102     const std::string kUsbPortOverheatPath;
103     const std::string kChargeMetricsPath;
104     const std::string kTypeCPartnerUevent;
105     const std::string kTypeCPartnerVidPath;
106     const std::string kTypeCPartnerPidPath;
107     const std::string kWirelessChargerPtmcUevent;
108     const std::string kWirelessChargerPtmcPath;
109 
110     BatteryCapacityReporter battery_capacity_reporter_;
111 
112     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
113     // store everything in the values array at the index of the field number
114     // -2.
115     const int kVendorAtomOffset = 2;
116 
117     int uevent_fd_;
118     int log_fd_;
119 
120     PcaChargeStats pca_charge_stats_;
121     WirelessChargeStats wireless_charge_stats_;
122 
123     WlcReporter wlc_reporter_ = WlcReporter(kWirelessChargerPtmcPath.c_str());
124 };
125 
126 }  // namespace pixel
127 }  // namespace google
128 }  // namespace hardware
129 }  // namespace android
130 
131 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H
132