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 19 import android.annotation.IntDef; 20 import android.annotation.SystemApi; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.text.TextUtils; 24 import android.util.SparseArray; 25 26 import com.android.internal.util.MessageUtils; 27 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 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 */ 39 @SystemApi 40 public 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. 44 public static final int FLAG_MULTICAST_FILTER_ON = 0; 45 public static final int FLAG_HAS_IPV4_ADDRESS = 1; 46 47 /** {@hide} */ 48 @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS}) 49 @Retention(RetentionPolicy.SOURCE) 50 public @interface Flags {} 51 52 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( 60 long lifetime, int filteredRas, int currentRas, int programLength, @Flags int flags) { 61 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} */ 109 public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) { 110 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 120 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)); 126 } 127 return TextUtils.join("|", names); 128 } 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 } 136