1 /*
2  * Copyright (C) 2023 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_DISPLAYSTATSREPORTER_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_DISPLAYSTATSREPORTER_H
19 
20 #include <aidl/android/frameworks/stats/IStats.h>
21 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
22 
23 #include <string>
24 
25 namespace android {
26 namespace hardware {
27 namespace google {
28 namespace pixel {
29 
30 using aidl::android::frameworks::stats::IStats;
31 using aidl::android::frameworks::stats::VendorAtomValue;
32 
33 /**
34  * A class to upload Pixel Display Stats metrics
35  */
36 class DisplayStatsReporter {
37   public:
38     DisplayStatsReporter();
39 
40     enum display_stats_type {
41         DISP_PANEL_STATE = 0,
42         DISP_PORT_STATE,
43         HDCP_STATE,
44     };
45     void logDisplayStats(const std::shared_ptr<IStats> &stats_client,
46                          const std::vector<std::string> &display_stats_paths,
47                          const display_stats_type stats_type);
48 
49   private:
50     bool readDisplayErrorCount(const std::string &path, int64_t *val);
51     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
52     // store everything in the values array at the index of the field number
53     // -2.
54     static constexpr int kVendorAtomOffset = 2;
55     static constexpr int kNumOfDisplayPanelErrorStats = 4;
56 
57     int verifyCount(int val, bool *report_stats);
58 
59     /* display state */
60     struct DisplayPanelErrorStats {
61         int64_t primary_error_count_te;
62         int64_t primary_error_count_unknown;
63         int64_t secondary_error_count_te;
64         int64_t secondary_error_count_unknown;
65     };
66     struct DisplayPanelErrorStats prev_panel_data_ = {};
67     void logDisplayPanelErrorStats(const std::shared_ptr<IStats> &stats_client,
68                                    const std::vector<std::string> &display_stats_paths);
69     bool captureDisplayPanelErrorStats(const std::vector<std::string> &display_stats_paths,
70                                        struct DisplayPanelErrorStats *cur_data);
71 
72     /* displayport state */
73     enum display_port_error_stats_index {
74         LINK_NEGOTIATION_FAILURES = 0,
75         EDID_READ_FAILURES,
76         DPCD_READ_FAILURES,
77         EDID_INVALID_FAILURES,
78         SINK_COUNT_INVALID_FAILURES,
79         LINK_UNSTABLE_FAILURES,
80         DISPLAY_PORT_ERROR_STATS_SIZE,
81     };
82     static constexpr int64_t display_port_error_path_index[DISPLAY_PORT_ERROR_STATS_SIZE] = {
83             PixelAtoms::DisplayPortErrorStats::kLinkNegotiationFailuresFieldNumber,
84             PixelAtoms::DisplayPortErrorStats::kEdidReadFailuresFieldNumber,
85             PixelAtoms::DisplayPortErrorStats::kDpcdReadFailuresFieldNumber,
86             PixelAtoms::DisplayPortErrorStats::kEdidInvalidFailuresFieldNumber,
87             PixelAtoms::DisplayPortErrorStats::kSinkCountInvalidFailuresFieldNumber,
88             PixelAtoms::DisplayPortErrorStats::kLinkUnstableFailuresFieldNumber};
89     int64_t prev_dp_data_[DISPLAY_PORT_ERROR_STATS_SIZE] = {0};
90 
91     void logDisplayPortErrorStats(const std::shared_ptr<IStats> &stats_client,
92                                   const std::vector<std::string> &displayport_stats_paths);
93     bool captureDisplayPortErrorStats(const std::vector<std::string> &displayport_stats_paths,
94                                       int64_t *cur_data);
95 
96     /* HDCP state */
97     enum hdcp_auth_type_stats_index {
98         HDCP2_SUCCESS = 0,
99         HDCP2_FALLBACK,
100         HDCP2_FAIL,
101         HDCP1_SUCCESS,
102         HDCP1_FAIL,
103         HDCP0,
104         HDCP_AUTH_TYPE_STATS_SIZE,
105     };
106     static constexpr int64_t hdcp_auth_type_path_index[HDCP_AUTH_TYPE_STATS_SIZE] = {
107             PixelAtoms::HDCPAuthTypeStats::kHdcp2SuccessCountFieldNumber,
108             PixelAtoms::HDCPAuthTypeStats::kHdcp2FallbackCountFieldNumber,
109             PixelAtoms::HDCPAuthTypeStats::kHdcp2FailCountFieldNumber,
110             PixelAtoms::HDCPAuthTypeStats::kHdcp1SuccessCountFieldNumber,
111             PixelAtoms::HDCPAuthTypeStats::kHdcp1FailCountFieldNumber,
112             PixelAtoms::HDCPAuthTypeStats::kHdcp0CountFieldNumber};
113     int64_t prev_hdcp_data_[HDCP_AUTH_TYPE_STATS_SIZE] = {0};
114 
115     void logHDCPAuthTypeStats(const std::shared_ptr<IStats> &stats_client,
116                               const std::vector<std::string> &hdcp_stats_paths);
117     bool captureHDCPAuthTypeStats(const std::vector<std::string> &hdcp_stats_paths,
118                                   int64_t *cur_data);
119 };
120 
121 }  // namespace pixel
122 }  // namespace google
123 }  // namespace hardware
124 }  // namespace android
125 
126 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_DISPLAYSTATSREPORTER_H
127