blob: 258d8e13951fadee7e731587f4361f559f913b97 [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.annotation.SystemApi;
21import 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 */
39@SystemApi
40public final class ApfProgramEvent implements Parcelable {
41
42 // Bitflag constants describing what an Apf program filters.
43 // Bits are indexeds from LSB to MSB, starting at index 0.
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090044 public static final int FLAG_MULTICAST_FILTER_ON = 0;
45 public static final int FLAG_HAS_IPV4_ADDRESS = 1;
46
Hugo Benichicf6b12f2016-07-04 11:28:05 +090047 /** {@hide} */
48 @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS})
49 @Retention(RetentionPolicy.SOURCE)
50 public @interface Flags {}
51
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090052 public final long lifetime; // Lifetime of the program in seconds
53 public final int filteredRas; // Number of RAs filtered by the APF program
54 public final int currentRas; // Total number of current RAs at generation time
55 public final int programLength; // Length of the APF program in bytes
56 public final int flags; // Bitfield compound of FLAG_* constants
57
58 /** {@hide} */
59 public ApfProgramEvent(
Hugo Benichicf6b12f2016-07-04 11:28:05 +090060 long lifetime, int filteredRas, int currentRas, int programLength, @Flags int flags) {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +090061 this.lifetime = lifetime;
62 this.filteredRas = filteredRas;
63 this.currentRas = currentRas;
64 this.programLength = programLength;
65 this.flags = flags;
66 }
67
68 private ApfProgramEvent(Parcel in) {
69 this.lifetime = in.readLong();
70 this.filteredRas = in.readInt();
71 this.currentRas = in.readInt();
72 this.programLength = in.readInt();
73 this.flags = in.readInt();
74 }
75
76 @Override
77 public void writeToParcel(Parcel out, int flags) {
78 out.writeLong(lifetime);
79 out.writeInt(filteredRas);
80 out.writeInt(currentRas);
81 out.writeInt(programLength);
82 out.writeInt(flags);
83 }
84
85 @Override
86 public int describeContents() {
87 return 0;
88 }
89
90 @Override
91 public String toString() {
92 String lifetimeString = (lifetime < Long.MAX_VALUE) ? lifetime + "s" : "forever";
93 return String.format("ApfProgramEvent(%d/%d RAs %dB %s %s)",
94 filteredRas, currentRas, programLength, lifetimeString, namesOf(flags));
95 }
96
97 public static final Parcelable.Creator<ApfProgramEvent> CREATOR
98 = new Parcelable.Creator<ApfProgramEvent>() {
99 public ApfProgramEvent createFromParcel(Parcel in) {
100 return new ApfProgramEvent(in);
101 }
102
103 public ApfProgramEvent[] newArray(int size) {
104 return new ApfProgramEvent[size];
105 }
106 };
107
108 /** {@hide} */
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900109 public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) {
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900110 int bitfield = 0;
111 if (hasIPv4) {
112 bitfield |= (1 << FLAG_HAS_IPV4_ADDRESS);
113 }
114 if (multicastFilterOn) {
115 bitfield |= (1 << FLAG_MULTICAST_FILTER_ON);
116 }
117 return bitfield;
118 }
119
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900120 private static String namesOf(@Flags int bitfield) {
121 List<String> names = new ArrayList<>(Integer.bitCount(bitfield));
122 BitSet set = BitSet.valueOf(new long[]{bitfield & Integer.MAX_VALUE});
123 // Only iterate over flag bits which are set.
124 for (int bit = set.nextSetBit(0); bit >= 0; bit = set.nextSetBit(bit+1)) {
125 names.add(Decoder.constants.get(bit));
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900126 }
Hugo Benichi0d1c65b2016-06-22 17:01:43 +0900127 return TextUtils.join("|", names);
Hugo Benichi4fc3ee52016-06-02 11:20:27 +0900128 }
129
130 final static class Decoder {
131 static final SparseArray<String> constants =
132 MessageUtils.findMessageNames(
133 new Class[]{ApfProgramEvent.class}, new String[]{"FLAG_"});
134 }
135}