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 
17 package android.util.proto;
18 
19 import android.util.AggStats;
20 import android.util.Duration;
21 
22 import java.io.IOException;
23 
24 /**
25  * This class contains a list of helper functions to write common proto in
26  * //frameworks/base/core/proto/android/base directory
27  * @hide
28  */
29 public class ProtoUtils {
30 
31     /**
32      * Dump AggStats to ProtoOutputStream
33      */
toAggStatsProto(ProtoOutputStream proto, long fieldId, long min, long average, long max)34     public static void toAggStatsProto(ProtoOutputStream proto, long fieldId,
35             long min, long average, long max) {
36         final long aggStatsToken = proto.start(fieldId);
37         proto.write(AggStats.MIN, min);
38         proto.write(AggStats.AVERAGE, average);
39         proto.write(AggStats.MAX, max);
40         proto.end(aggStatsToken);
41     }
42 
43     /**
44      * Dump Duration to ProtoOutputStream
45      */
toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs)46     public static void toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs) {
47         final long token = proto.start(fieldId);
48         proto.write(Duration.START_MS, startMs);
49         proto.write(Duration.END_MS, endMs);
50         proto.end(token);
51     }
52 
53     /**
54      * Helper function to write bit-wise flags to proto as repeated enums
55      */
writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId, int flags, int[] origEnums, int[] protoEnums)56     public static void writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId,
57             int flags, int[] origEnums, int[] protoEnums) {
58         if (protoEnums.length != origEnums.length) {
59             throw new IllegalArgumentException("The length of origEnums must match protoEnums");
60         }
61         int len = origEnums.length;
62         for (int i = 0; i < len; i++) {
63             // handle zero flag case.
64             if (origEnums[i] == 0 && flags == 0) {
65                 proto.write(fieldId, protoEnums[i]);
66                 return;
67             }
68             if ((flags & origEnums[i]) != 0) {
69                 proto.write(fieldId, protoEnums[i]);
70             }
71         }
72     }
73 
74     /**
75      * Provide debug data about the current field as a string
76      */
currentFieldToString(ProtoInputStream proto)77     public static String currentFieldToString(ProtoInputStream proto) throws IOException {
78         StringBuilder sb = new StringBuilder();
79 
80         final int fieldNumber = proto.getFieldNumber();
81         final int wireType = proto.getWireType();
82         long fieldConstant;
83 
84         sb.append("Offset : 0x" + Integer.toHexString(proto.getOffset()));
85         sb.append("\nField Number : 0x" + Integer.toHexString(proto.getFieldNumber()));
86         sb.append("\nWire Type : ");
87         switch (wireType) {
88             case ProtoStream.WIRE_TYPE_VARINT:
89                 sb.append("varint");
90                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
91                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_INT64);
92                 sb.append("\nField Value : 0x" + Long.toHexString(proto.readLong(fieldConstant)));
93                 break;
94             case ProtoStream.WIRE_TYPE_FIXED64:
95                 sb.append("fixed64");
96                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
97                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED64);
98                 sb.append("\nField Value : 0x" + Long.toHexString(proto.readLong(fieldConstant)));
99                 break;
100             case ProtoStream.WIRE_TYPE_LENGTH_DELIMITED:
101                 sb.append("length delimited");
102                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
103                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_BYTES);
104                 sb.append("\nField Bytes : " + proto.readBytes(fieldConstant));
105                 break;
106             case ProtoStream.WIRE_TYPE_START_GROUP:
107                 sb.append("start group");
108                 break;
109             case ProtoStream.WIRE_TYPE_END_GROUP:
110                 sb.append("end group");
111                 break;
112             case ProtoStream.WIRE_TYPE_FIXED32:
113                 sb.append("fixed32");
114                 fieldConstant = ProtoStream.makeFieldId(fieldNumber,
115                         ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED32);
116                 sb.append("\nField Value : 0x" + Integer.toHexString(proto.readInt(fieldConstant)));
117                 break;
118             default:
119                 sb.append("unknown(" + proto.getWireType() + ")");
120         }
121         return sb.toString();
122     }
123 }
124