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; |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 22 | import android.text.TextUtils; |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 23 | import android.util.SparseArray; |
| 24 | |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 25 | import com.android.internal.util.MessageUtils; |
| 26 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.RetentionPolicy; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.BitSet; |
| 31 | import java.util.List; |
| 32 | |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 33 | /** |
| 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 Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 38 | public 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 Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 42 | public static final int FLAG_MULTICAST_FILTER_ON = 0; |
| 43 | public static final int FLAG_HAS_IPV4_ADDRESS = 1; |
| 44 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 45 | /** {@hide} */ |
| 46 | @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS}) |
| 47 | @Retention(RetentionPolicy.SOURCE) |
| 48 | public @interface Flags {} |
| 49 | |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame^] | 50 | 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 Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 56 | |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame^] | 57 | public ApfProgramEvent() { |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private ApfProgramEvent(Parcel in) { |
| 61 | this.lifetime = in.readLong(); |
Hugo Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame^] | 62 | this.actualLifetime = in.readLong(); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 63 | 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 Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame^] | 72 | out.writeLong(actualLifetime); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 73 | 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 Benichi | 22d9b2d | 2017-02-22 13:02:27 +0900 | [diff] [blame^] | 87 | return String.format("ApfProgramEvent(%d/%d RAs %dB %ds/%s %s)", filteredRas, currentRas, |
| 88 | programLength, actualLifetime, lifetimeString, namesOf(flags)); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 89 | } |
| 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 Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 102 | public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) { |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 103 | 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 Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 113 | 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 Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 119 | } |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame] | 120 | return TextUtils.join("|", names); |
Hugo Benichi | 4fc3ee5 | 2016-06-02 11:20:27 +0900 | [diff] [blame] | 121 | } |
| 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 | } |