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.SystemApi; 20 import android.annotation.TestApi; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.util.SparseArray; 24 25 import com.android.internal.util.MessageUtils; 26 27 /** 28 * An event recorded when IpReachabilityMonitor sends a neighbor probe or receives 29 * a neighbor probe result. 30 * {@hide} 31 */ 32 @SystemApi 33 @TestApi 34 public final class IpReachabilityEvent implements IpConnectivityLog.Event { 35 36 // Event types. 37 /** A probe forced by IpReachabilityMonitor. */ 38 public static final int PROBE = 1 << 8; 39 /** Neighbor unreachable after a forced probe. */ 40 public static final int NUD_FAILED = 2 << 8; 41 /** Neighbor unreachable after a forced probe, IP provisioning is also lost. */ 42 public static final int PROVISIONING_LOST = 3 << 8; 43 /** Neighbor unreachable notification from kernel. */ 44 public static final int NUD_FAILED_ORGANIC = 4 << 8; 45 /** Neighbor unreachable notification from kernel, IP provisioning is also lost. */ 46 public static final int PROVISIONING_LOST_ORGANIC = 5 << 8; 47 48 // eventType byte format (MSB to LSB): 49 // byte 0: unused 50 // byte 1: unused 51 // byte 2: type of event: PROBE, NUD_FAILED, PROVISIONING_LOST 52 // byte 3: when byte 2 == PROBE, errno code from RTNetlink or IpReachabilityMonitor. 53 /** @hide */ 54 public final int eventType; 55 IpReachabilityEvent(int eventType)56 public IpReachabilityEvent(int eventType) { 57 this.eventType = eventType; 58 } 59 IpReachabilityEvent(Parcel in)60 private IpReachabilityEvent(Parcel in) { 61 this.eventType = in.readInt(); 62 } 63 64 /** @hide */ 65 @Override writeToParcel(Parcel out, int flags)66 public void writeToParcel(Parcel out, int flags) { 67 out.writeInt(eventType); 68 } 69 70 /** @hide */ 71 @Override describeContents()72 public int describeContents() { 73 return 0; 74 } 75 76 /** @hide */ 77 public static final @android.annotation.NonNull Parcelable.Creator<IpReachabilityEvent> CREATOR 78 = new Parcelable.Creator<IpReachabilityEvent>() { 79 public IpReachabilityEvent createFromParcel(Parcel in) { 80 return new IpReachabilityEvent(in); 81 } 82 83 public IpReachabilityEvent[] newArray(int size) { 84 return new IpReachabilityEvent[size]; 85 } 86 }; 87 88 @Override toString()89 public String toString() { 90 int hi = eventType & 0xff00; 91 int lo = eventType & 0x00ff; 92 String eventName = Decoder.constants.get(hi); 93 return String.format("IpReachabilityEvent(%s:%02x)", eventName, lo); 94 } 95 96 @Override equals(Object obj)97 public boolean equals(Object obj) { 98 if (obj == null || !(obj.getClass().equals(IpReachabilityEvent.class))) return false; 99 final IpReachabilityEvent other = (IpReachabilityEvent) obj; 100 return eventType == other.eventType; 101 } 102 103 final static class Decoder { 104 static final SparseArray<String> constants = 105 MessageUtils.findMessageNames(new Class[]{IpReachabilityEvent.class}, 106 new String[]{"PROBE", "PROVISIONING_", "NUD_"}); 107 } 108 } 109