1 /*
2  * Copyright (C) 2017 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 #ifndef STATS_LOG_EVENT_WRAPPER_H
17 #define STATS_LOG_EVENT_WRAPPER_H
18 
19 #include <binder/Parcel.h>
20 #include <binder/Parcelable.h>
21 #include <binder/Status.h>
22 #include <utils/RefBase.h>
23 #include <vector>
24 
25 namespace android {
26 namespace os {
27 
28 /**
29  * A wrapper for a union type to contain multiple types of values.
30  *
31  */
32 struct StatsLogValue {
33   // Keep in sync with FieldValue.h
34   enum STATS_LOG_VALUE_TYPE {
35     UNKNOWN = 0,
36     INT = 1,
37     LONG = 2,
38     FLOAT = 3,
39     DOUBLE = 4,
40     STRING = 5,
41     STORAGE = 6
42   };
43 
StatsLogValueStatsLogValue44   StatsLogValue() : type(UNKNOWN) {}
45 
StatsLogValueStatsLogValue46   StatsLogValue(int32_t v) {
47     int_value = v;
48     type = INT;
49   }
50 
StatsLogValueStatsLogValue51   StatsLogValue(int64_t v) {
52     long_value = v;
53     type = LONG;
54   }
55 
StatsLogValueStatsLogValue56   StatsLogValue(float v) {
57     float_value = v;
58     type = FLOAT;
59   }
60 
StatsLogValueStatsLogValue61   StatsLogValue(double v) {
62     double_value = v;
63     type = DOUBLE;
64   }
65 
StatsLogValueStatsLogValue66   StatsLogValue(const std::string& v) {
67     str_value = v;
68     type = STRING;
69   }
70 
setTypeStatsLogValue71   void setType(STATS_LOG_VALUE_TYPE t) { type = t; }
72 
73   union {
74     int32_t int_value;
75     int64_t long_value;
76     float float_value;
77     double double_value;
78   };
79   std::string str_value;
80   std::vector<uint8_t> storage_value;
81 
82   STATS_LOG_VALUE_TYPE type;
83 };
84 
85 struct WorkChain {
86   std::vector<int32_t> uids;
87   std::vector<std::string> tags;
88 };
89 
90 // Represents a parcelable object. Only used to send data from Android OS to statsd.
91 class StatsLogEventWrapper : public android::Parcelable {
92  public:
93   StatsLogEventWrapper();
94 
95   StatsLogEventWrapper(StatsLogEventWrapper&& in) = default;
96 
97   android::status_t writeToParcel(android::Parcel* out) const;
98 
99   android::status_t readFromParcel(const android::Parcel* in);
100 
getTagId()101   int getTagId() const { return mTagId; }
102 
getElapsedRealTimeNs()103   int64_t getElapsedRealTimeNs() const { return mElapsedRealTimeNs; }
104 
getWallClockTimeNs()105   int64_t getWallClockTimeNs() const { return mWallClockTimeNs; }
106 
getElements()107   const std::vector<StatsLogValue>& getElements() const { return mElements; }
108 
getWorkChains()109   const std::vector<WorkChain>& getWorkChains() const { return mWorkChains; }
110 
111  private:
112   int mTagId;
113 
114   int64_t mElapsedRealTimeNs;
115 
116   int64_t mWallClockTimeNs;
117 
118   std::vector<StatsLogValue> mElements;
119 
120   std::vector<WorkChain> mWorkChains;
121 };
122 } // Namespace os
123 } // Namespace android
124 
125 
126 #endif  // STATS_LOG_EVENT_WRAPPER_H
127 
128