Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net.metrics; |
| 18 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 20 | import android.annotation.UnsupportedAppUsage; |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 23 | import android.text.TextUtils; |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 24 | import android.util.SparseArray; |
| 25 | |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 26 | import com.android.internal.util.MessageUtils; |
| 27 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.BitSet; |
| 32 | import java.util.List; |
| 33 | |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 34 | /** |
| 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 Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 39 | public 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 Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 43 | public static final int FLAG_MULTICAST_FILTER_ON = 0; |
| 44 | public static final int FLAG_HAS_IPV4_ADDRESS = 1; |
| 45 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 46 | /** {@hide} */ |
| 47 | @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS}) |
| 48 | @Retention(RetentionPolicy.SOURCE) |
| 49 | public @interface Flags {} |
| 50 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 51 | @UnsupportedAppUsage |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 52 | public long lifetime; // Maximum computed lifetime of the program in seconds |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 53 | @UnsupportedAppUsage |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 54 | public long actualLifetime; // Effective program lifetime in seconds |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 55 | @UnsupportedAppUsage |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 56 | public int filteredRas; // Number of RAs filtered by the APF program |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 57 | @UnsupportedAppUsage |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 58 | public int currentRas; // Total number of current RAs at generation time |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 59 | @UnsupportedAppUsage |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 60 | public int programLength; // Length of the APF program in bytes |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 61 | @UnsupportedAppUsage |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 62 | public int flags; // Bitfield compound of FLAG_* constants |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 63 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 64 | @UnsupportedAppUsage |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 65 | public ApfProgramEvent() { |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private ApfProgramEvent(Parcel in) { |
| 69 | this.lifetime = in.readLong(); |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 70 | this.actualLifetime = in.readLong(); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 71 | 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 Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 80 | out.writeLong(actualLifetime); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 81 | 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 Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame] | 95 | return String.format("ApfProgramEvent(%d/%d RAs %dB %ds/%s %s)", filteredRas, currentRas, |
| 96 | programLength, actualLifetime, lifetimeString, namesOf(flags)); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 97 | } |
| 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 Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame^] | 110 | @UnsupportedAppUsage |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 111 | public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) { |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 112 | 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 Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 122 | 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 Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 128 | } |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame] | 129 | return TextUtils.join("|", names); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 130 | } |
| 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 | } |