1 /*
2  * Copyright (C) 2020 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_WLCREPORTER_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_WLCREPORTER_H
19 
20 #include <aidl/android/frameworks/stats/IStats.h>
21 #include <utils/RefBase.h>
22 
23 #include <cstdint>
24 
25 namespace android {
26 namespace hardware {
27 namespace google {
28 namespace pixel {
29 
30 using aidl::android::frameworks::stats::IStats;
31 
32 /**
33  * A class to upload wireless metrics
34  */
35 class WlcReporter : public RefBase {
36   public:
37     void checkAndReport(const std::shared_ptr<IStats> &stats_client, const bool online,
38                         const char *ptmc_uevent);
39 
40     WlcReporter(const char *ptmc_path);
41 
42   private:
43     struct WlcStatus {
44         bool is_charging;
45         bool check_charger_vendor_id;
46         bool check_charger_vendor_id_scheduled;
47         int check_vendor_id_attempts;
48         WlcStatus();
49     };
50     WlcStatus wlc_status_;
51     struct ReportRecord {
52         char const *name;
53         int64_t last_reported_time_in_sec_today;
54         int64_t last_reported_time_in_sec;
55         int count_today;
56         ReportRecord(char const *name_);
57     };
58     ReportRecord rec_wlc_vendor_{"wlc_vendor"};
59     ReportRecord rec_orientation_{"orientation"};
60 
61     void checkVendorId(const std::shared_ptr<IStats> &stats_client, const char *ptmc_uevent);
62     void scheduleReportVendorId(const std::shared_ptr<IStats> &stats_client);
63     void reportOrientation(const std::shared_ptr<IStats> &stats_client);
64     void reportVendor(const std::shared_ptr<IStats> &stats_client, const int ptmcId);
65     bool reportVendorMayRetry(const std::shared_ptr<IStats> &stats_client, const char *ptmc_uevent);
66     // Translate device orientation value from sensor Hal to atom enum value
67     int translateDeviceOrientationToAtomValue(int orientation);
68     void reportInBackground(const std::shared_ptr<IStats> &stats_client, const char *ptmc_path);
69     /*
70      * Wait timer for make delay before read ptmc path, return false on error
71      * timerfd: fd create by timerfd_create, need create/close by caller
72      **/
73     bool ptmcWaitTimer(int timerfd);
74     /* For some case (ex if wireless charger was connect to a low power PTMC AC
75      * adapter), the wireless charger keep restaring (it might casuse will
76      * check and update data in a not reasonable rare).
77      * return: true, it has not hit upload rare limit
78      *         false, it has hit rate litmit, we should drop current
79      *                upload atom
80      **/
81     bool checkRateLimit(int64_t minSecond, int maxCount, ReportRecord *rec);
82 
83     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
84     // store everything in the values array at the index of the field number
85     // -2.
86     const int kVendorAtomOffset = 2;
87     const int kMaxVendorIdAttempts = 5;
88     const char *kWirelessChargerPtmcPath;
89 
90     int readPtmcId(const char *ptmc_str);
91 };
92 
93 }  // namespace pixel
94 }  // namespace google
95 }  // namespace hardware
96 }  // namespace android
97 
98 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_WLCREPORTER_H
99