blob: 5dabf35c48674a6b013f68790f02601a105de85f [file] [log] [blame]
Hugo Benichi4fc3ee52016-06-02 11:20:27 +09001/*
2 * Copyright (C) 2016 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
17package android.net.metrics;
18
Hugo Benichicf6b12f2016-07-04 11:28:05 +090019import android.annotation.IntDef;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010020import android.annotation.UnsupportedAppUsage;
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090021import android.os.Parcel;
22import android.os.Parcelable;
Hugo Benichicf6b12f2016-07-04 11:28:05 +090023import android.text.TextUtils;
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090024import android.util.SparseArray;
25
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090026import com.android.internal.util.MessageUtils;
27
Hugo Benichicf6b12f2016-07-04 11:28:05 +090028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30import java.util.ArrayList;
31import java.util.BitSet;
32import java.util.List;
33
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090034/**
35 * An event logged when there is a change or event that requires updating the
36 * the APF program in place with a new APF program.
37 * {@hide}
38 */
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090039public final class ApfProgramEvent implements Parcelable {
40
41 // Bitflag constants describing what an Apf program filters.
42 // Bits are indexeds from LSB to MSB, starting at index 0.
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090043 public static final int FLAG_MULTICAST_FILTER_ON = 0;
44 public static final int FLAG_HAS_IPV4_ADDRESS = 1;
45
Hugo Benichicf6b12f2016-07-04 11:28:05 +090046 /** {@hide} */
47 @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS})
48 @Retention(RetentionPolicy.SOURCE)
49 public @interface Flags {}
50
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010051 @UnsupportedAppUsage
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090052 public long lifetime; // Maximum computed lifetime of the program in seconds
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010053 @UnsupportedAppUsage
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090054 public long actualLifetime; // Effective program lifetime in seconds
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010055 @UnsupportedAppUsage
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090056 public int filteredRas; // Number of RAs filtered by the APF program
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010057 @UnsupportedAppUsage
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090058 public int currentRas; // Total number of current RAs at generation time
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010059 @UnsupportedAppUsage
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090060 public int programLength; // Length of the APF program in bytes
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010061 @UnsupportedAppUsage
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090062 public int flags; // Bitfield compound of FLAG_* constants
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090063
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010064 @UnsupportedAppUsage
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090065 public ApfProgramEvent() {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090066 }
67
68 private ApfProgramEvent(Parcel in) {
69 this.lifetime = in.readLong();
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090070 this.actualLifetime = in.readLong();
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090071 this.filteredRas = in.readInt();
72 this.currentRas = in.readInt();
73 this.programLength = in.readInt();
74 this.flags = in.readInt();
75 }
76
77 @Override
78 public void writeToParcel(Parcel out, int flags) {
79 out.writeLong(lifetime);
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090080 out.writeLong(actualLifetime);
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090081 out.writeInt(filteredRas);
82 out.writeInt(currentRas);
83 out.writeInt(programLength);
84 out.writeInt(flags);
85 }
86
87 @Override
88 public int describeContents() {
89 return 0;
90 }
91
92 @Override
93 public String toString() {
94 String lifetimeString = (lifetime < Long.MAX_VALUE) ? lifetime + "s" : "forever";
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090095 return String.format("ApfProgramEvent(%d/%d RAs %dB %ds/%s %s)", filteredRas, currentRas,
96 programLength, actualLifetime, lifetimeString, namesOf(flags));
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090097 }
98
99 public static final Parcelable.Creator<ApfProgramEvent> CREATOR
100 = new Parcelable.Creator<ApfProgramEvent>() {
101 public ApfProgramEvent createFromParcel(Parcel in) {
102 return new ApfProgramEvent(in);
103 }
104
105 public ApfProgramEvent[] newArray(int size) {
106 return new ApfProgramEvent[size];
107 }
108 };
109
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100110 @UnsupportedAppUsage
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900111 public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900112 int bitfield = 0;
113 if (hasIPv4) {
114 bitfield |= (1 << FLAG_HAS_IPV4_ADDRESS);
115 }
116 if (multicastFilterOn) {
117 bitfield |= (1 << FLAG_MULTICAST_FILTER_ON);
118 }
119 return bitfield;
120 }
121
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900122 private static String namesOf(@Flags int bitfield) {
123 List<String> names = new ArrayList<>(Integer.bitCount(bitfield));
124 BitSet set = BitSet.valueOf(new long[]{bitfield & Integer.MAX_VALUE});
125 // Only iterate over flag bits which are set.
126 for (int bit = set.nextSetBit(0); bit >= 0; bit = set.nextSetBit(bit+1)) {
127 names.add(Decoder.constants.get(bit));
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900128 }
Hugo Benichi0d1c65b2016-06-22 17:01:43 +0900129 return TextUtils.join("|", names);
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900130 }
131
132 final static class Decoder {
133 static final SparseArray<String> constants =
134 MessageUtils.findMessageNames(
135 new Class[]{ApfProgramEvent.class}, new String[]{"FLAG_"});
136 }
137}