summaryrefslogtreecommitdiff
path: root/pixelstats/include/pixelstats/UeventListener.h
blob: 6e6f81744845a05da37dd7af56b0394c93ff6464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H
#define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H

#include <aidl/android/frameworks/stats/IStats.h>
#include <android-base/chrono_utils.h>
#include <pixelstats/BatteryCapacityReporter.h>
#include <pixelstats/ChargeStatsReporter.h>

namespace android {
namespace hardware {
namespace google {
namespace pixel {

using aidl::android::frameworks::stats::IStats;
/**
 * A class to listen for uevents and report reliability events to
 * the PixelStats HAL.
 * Runs in a background thread if created with ListenForeverInNewThread().
 * Alternatively, process one message at a time with ProcessUevent().
 */
class UeventListener {
  public:
    struct UeventPaths {
        const char *const AudioUevent;
        const char *const SsocDetailsPath;
        const char *const OverheatPath;
        const char *const ChargeMetricsPath;
        const char *const TypeCPartnerUevent;
        const char *const TypeCPartnerVidPath;
        const char *const TypeCPartnerPidPath;
        const char *const WirelessChargerPtmcUevent;  // Deprecated.
        const char *const WirelessChargerPtmcPath;    // Deprecated.
        const char *const GMSRPath;
    };
    constexpr static const char *const ssoc_details_path =
            "/sys/class/power_supply/battery/ssoc_details";
    constexpr static const char *const overheat_path_default =
            "/sys/devices/platform/soc/soc:google,overheat_mitigation";
    constexpr static const char *const charge_metrics_path_default =
            "/sys/class/power_supply/battery/charge_stats";
    constexpr static const char *const typec_partner_vid_path_default =
            "/sys/class/typec/port0-partner/identity/id_header";
    constexpr static const char *const typec_partner_pid_path_default =
            "/sys/class/typec/port0-partner/identity/product";
    constexpr static const char *const typec_partner_uevent_default = "DEVTYPE=typec_partner";
    constexpr static const char *const gmsr_path = "";

    UeventListener(const std::string audio_uevent, const std::string ssoc_details_path = "",
                   const std::string overheat_path = overheat_path_default,
                   const std::string charge_metrics_path = charge_metrics_path_default,
                   const std::string typec_partner_vid_path = typec_partner_vid_path_default,
                   const std::string typec_partner_pid_path = typec_partner_pid_path_default);
    UeventListener(const struct UeventPaths &paths);

    bool ProcessUevent();  // Process a single Uevent.
    void ListenForever();  // Process Uevents forever

  private:
    bool ReadFileToInt(const std::string &path, int *val);
    bool ReadFileToInt(const char *path, int *val);
    void ReportMicStatusUevents(const std::shared_ptr<IStats> &stats_client, const char *devpath,
                                const char *mic_status);
    void ReportMicBrokenOrDegraded(const std::shared_ptr<IStats> &stats_client, const int mic,
                                   const bool isBroken);
    void ReportUsbPortOverheatEvent(const std::shared_ptr<IStats> &stats_client,
                                    const char *driver);
    void ReportChargeStats(const std::shared_ptr<IStats> &stats_client, const std::string line,
                           const std::string wline_at, const std::string wline_ac,
                           const std::string pca_line);
    void ReportVoltageTierStats(const std::shared_ptr<IStats> &stats_client, const char *line,
                                const bool has_wireless, const std::string wfile_contents);
    void ReportChargeMetricsEvent(const std::shared_ptr<IStats> &stats_client, const char *driver);
    void ReportBatteryCapacityFGEvent(const std::shared_ptr<IStats> &stats_client,
                                      const char *subsystem);
    void ReportTypeCPartnerId(const std::shared_ptr<IStats> &stats_client);
    void ReportGpuEvent(const std::shared_ptr<IStats> &stats_client, const char *driver,
                        const char *gpu_event_type, const char *gpu_event_info);
    void ReportThermalAbnormalEvent(const std::shared_ptr<IStats> &stats_client,
                                    const char *devpath, const char *thermal_abnormal_event_type,
                                    const char *thermal_abnormal_event_info);
    const std::string kAudioUevent;
    const std::string kBatterySSOCPath;
    const std::string kUsbPortOverheatPath;
    const std::string kChargeMetricsPath;
    const std::string kTypeCPartnerUevent;
    const std::string kTypeCPartnerVidPath;
    const std::string kTypeCPartnerPidPath;
    const std::string kBatteryGMSRPath;

    const std::unordered_map<std::string, PixelAtoms::GpuEvent::GpuEventType>
            kGpuEventTypeStrToEnum{
                    {"KMD_ERROR",
                     PixelAtoms::GpuEvent::GpuEventType::GpuEvent_GpuEventType_MALI_KMD_ERROR},
                    {"GPU_RESET",
                     PixelAtoms::GpuEvent::GpuEventType::GpuEvent_GpuEventType_MALI_GPU_RESET}};

    const std::unordered_map<std::string, PixelAtoms::GpuEvent::GpuEventInfo>
            kGpuEventInfoStrToEnum{
                    {"CSG_REQ_STATUS_UPDATE",
                     PixelAtoms::GpuEvent::GpuEventInfo::
                             GpuEvent_GpuEventInfo_MALI_CSG_REQ_STATUS_UPDATE},
                    {"CSG_SUSPEND",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSG_SUSPEND},
                    {"CSG_SLOTS_SUSPEND", PixelAtoms::GpuEvent::GpuEventInfo::
                                                  GpuEvent_GpuEventInfo_MALI_CSG_SLOTS_SUSPEND},
                    {"CSG_GROUP_SUSPEND", PixelAtoms::GpuEvent::GpuEventInfo::
                                                  GpuEvent_GpuEventInfo_MALI_CSG_GROUP_SUSPEND},
                    {"CSG_EP_CFG",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSG_EP_CFG},
                    {"CSG_SLOTS_START", PixelAtoms::GpuEvent::GpuEventInfo::
                                                GpuEvent_GpuEventInfo_MALI_CSG_SLOTS_START},
                    {"GROUP_TERM",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_GROUP_TERM},
                    {"QUEUE_START",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_QUEUE_START},
                    {"QUEUE_STOP",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_QUEUE_STOP},
                    {"QUEUE_STOP_ACK",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_QUEUE_STOP_ACK},
                    {"CSG_SLOT_READY",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSG_SLOT_READY},
                    {"L2_PM_TIMEOUT",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_L2_PM_TIMEOUT},
                    {"PM_TIMEOUT",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_PM_TIMEOUT},
                    {"CSF_RESET_OK",
                     PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSF_RESET_OK},
                    {"CSF_RESET_FAILED", PixelAtoms::GpuEvent::GpuEventInfo::
                                                 GpuEvent_GpuEventInfo_MALI_CSF_RESET_FAILED}};

    const std::unordered_map<std::string,
                             PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType>
            kThermalAbnormalityTypeStrToEnum{
                    {"UNKNOWN", PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
                                        ThermalSensorAbnormalityDetected_AbnormalityType_UNKNOWN},
                    {"SENSOR_STUCK",
                     PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
                             ThermalSensorAbnormalityDetected_AbnormalityType_SENSOR_STUCK},
                    {"EXTREME_HIGH_TEMP",
                     PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
                             ThermalSensorAbnormalityDetected_AbnormalityType_EXTREME_HIGH_TEMP},
                    {"EXTREME_LOW_TEMP",
                     PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
                             ThermalSensorAbnormalityDetected_AbnormalityType_EXTREME_LOW_TEMP},
                    {"HIGH_RISING_SPEED",
                     PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
                             ThermalSensorAbnormalityDetected_AbnormalityType_HIGH_RISING_SPEED},
                    {"TEMP_READ_FAIL",
                     PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
                             ThermalSensorAbnormalityDetected_AbnormalityType_TEMP_READ_FAIL},
            };

    BatteryCapacityReporter battery_capacity_reporter_;
    ChargeStatsReporter charge_stats_reporter_;

    // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
    // store everything in the values array at the index of the field number
    // -2.
    const int kVendorAtomOffset = 2;

    int uevent_fd_;
    int log_fd_;
};

}  // namespace pixel
}  // namespace google
}  // namespace hardware
}  // namespace android

#endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H