blob: ad4588f139f9668684ff0c8fe108522a40d72361 [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;
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090020import android.os.Parcel;
21import android.os.Parcelable;
Hugo Benichicf6b12f2016-07-04 11:28:05 +090022import android.text.TextUtils;
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090023import android.util.SparseArray;
24
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090025import com.android.internal.util.MessageUtils;
26
Hugo Benichicf6b12f2016-07-04 11:28:05 +090027import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29import java.util.ArrayList;
30import java.util.BitSet;
31import java.util.List;
32
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090033/**
34 * An event logged when there is a change or event that requires updating the
35 * the APF program in place with a new APF program.
36 * {@hide}
37 */
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090038public final class ApfProgramEvent implements Parcelable {
39
40 // Bitflag constants describing what an Apf program filters.
41 // Bits are indexeds from LSB to MSB, starting at index 0.
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090042 public static final int FLAG_MULTICAST_FILTER_ON = 0;
43 public static final int FLAG_HAS_IPV4_ADDRESS = 1;
44
Hugo Benichicf6b12f2016-07-04 11:28:05 +090045 /** {@hide} */
46 @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS})
47 @Retention(RetentionPolicy.SOURCE)
48 public @interface Flags {}
49
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090050 public long lifetime; // Maximum computed lifetime of the program in seconds
51 public long actualLifetime; // Effective program lifetime in seconds
52 public int filteredRas; // Number of RAs filtered by the APF program
53 public int currentRas; // Total number of current RAs at generation time
54 public int programLength; // Length of the APF program in bytes
55 public int flags; // Bitfield compound of FLAG_* constants
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090056
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090057 public ApfProgramEvent() {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090058 }
59
60 private ApfProgramEvent(Parcel in) {
61 this.lifetime = in.readLong();
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090062 this.actualLifetime = in.readLong();
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090063 this.filteredRas = in.readInt();
64 this.currentRas = in.readInt();
65 this.programLength = in.readInt();
66 this.flags = in.readInt();
67 }
68
69 @Override
70 public void writeToParcel(Parcel out, int flags) {
71 out.writeLong(lifetime);
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090072 out.writeLong(actualLifetime);
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090073 out.writeInt(filteredRas);
74 out.writeInt(currentRas);
75 out.writeInt(programLength);
76 out.writeInt(flags);
77 }
78
79 @Override
80 public int describeContents() {
81 return 0;
82 }
83
84 @Override
85 public String toString() {
86 String lifetimeString = (lifetime < Long.MAX_VALUE) ? lifetime + "s" : "forever";
Hugo Benichi22d9b2d2017-02-22 13:02:27 +090087 return String.format("ApfProgramEvent(%d/%d RAs %dB %ds/%s %s)", filteredRas, currentRas,
88 programLength, actualLifetime, lifetimeString, namesOf(flags));
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090089 }
90
91 public static final Parcelable.Creator<ApfProgramEvent> CREATOR
92 = new Parcelable.Creator<ApfProgramEvent>() {
93 public ApfProgramEvent createFromParcel(Parcel in) {
94 return new ApfProgramEvent(in);
95 }
96
97 public ApfProgramEvent[] newArray(int size) {
98 return new ApfProgramEvent[size];
99 }
100 };
101
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900102 public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900103 int bitfield = 0;
104 if (hasIPv4) {
105 bitfield |= (1 << FLAG_HAS_IPV4_ADDRESS);
106 }
107 if (multicastFilterOn) {
108 bitfield |= (1 << FLAG_MULTICAST_FILTER_ON);
109 }
110 return bitfield;
111 }
112
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900113 private static String namesOf(@Flags int bitfield) {
114 List<String> names = new ArrayList<>(Integer.bitCount(bitfield));
115 BitSet set = BitSet.valueOf(new long[]{bitfield & Integer.MAX_VALUE});
116 // Only iterate over flag bits which are set.
117 for (int bit = set.nextSetBit(0); bit >= 0; bit = set.nextSetBit(bit+1)) {
118 names.add(Decoder.constants.get(bit));
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900119 }
Hugo Benichi0d1c65b2016-06-22 17:01:43 +0900120 return TextUtils.join("|", names);
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900121 }
122
123 final static class Decoder {
124 static final SparseArray<String> constants =
125 MessageUtils.findMessageNames(
126 new Class[]{ApfProgramEvent.class}, new String[]{"FLAG_"});
127 }
128}