Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +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 | |
| 19 | import android.annotation.SystemApi; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 22 | import android.util.SparseArray; |
| 23 | |
| 24 | import com.android.internal.util.MessageUtils; |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 25 | |
| 26 | /** |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame^] | 27 | * An event recorded when IpReachabilityMonitor sends a neighbor probe or receives |
| 28 | * a neighbor probe result. |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 29 | * {@hide} |
| 30 | */ |
| 31 | @SystemApi |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 32 | public final class IpReachabilityEvent implements Parcelable { |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 33 | |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame^] | 34 | // Event types. |
| 35 | /** A probe forced by IpReachabilityMonitor. */ |
| 36 | public static final int PROBE = 1 << 8; |
| 37 | /** Neighbor unreachable after a forced probe. */ |
| 38 | public static final int NUD_FAILED = 2 << 8; |
| 39 | /** Neighbor unreachable after a forced probe, IP provisioning is also lost. */ |
| 40 | public static final int PROVISIONING_LOST = 3 << 8; |
| 41 | /** {@hide} Neighbor unreachable notification from kernel. */ |
| 42 | public static final int NUD_FAILED_ORGANIC = 4 << 8; |
| 43 | /** {@hide} Neighbor unreachable notification from kernel, IP provisioning is also lost. */ |
| 44 | public static final int PROVISIONING_LOST_ORGANIC = 5 << 8; |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 45 | |
| 46 | public final String ifName; |
| 47 | // eventType byte format (MSB to LSB): |
| 48 | // byte 0: unused |
| 49 | // byte 1: unused |
| 50 | // byte 2: type of event: PROBE, NUD_FAILED, PROVISIONING_LOST |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame^] | 51 | // byte 3: when byte 2 == PROBE, errno code from RTNetlink or IpReachabilityMonitor. |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 52 | public final int eventType; |
| 53 | |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 54 | /** {@hide} */ |
| 55 | public IpReachabilityEvent(String ifName, int eventType) { |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 56 | this.ifName = ifName; |
| 57 | this.eventType = eventType; |
| 58 | } |
| 59 | |
| 60 | private IpReachabilityEvent(Parcel in) { |
| 61 | this.ifName = in.readString(); |
| 62 | this.eventType = in.readInt(); |
| 63 | } |
| 64 | |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame^] | 65 | @Override |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 66 | public void writeToParcel(Parcel out, int flags) { |
| 67 | out.writeString(ifName); |
| 68 | out.writeInt(eventType); |
| 69 | } |
| 70 | |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame^] | 71 | @Override |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 72 | public int describeContents() { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | public static final Parcelable.Creator<IpReachabilityEvent> CREATOR |
| 77 | = new Parcelable.Creator<IpReachabilityEvent>() { |
| 78 | public IpReachabilityEvent createFromParcel(Parcel in) { |
| 79 | return new IpReachabilityEvent(in); |
| 80 | } |
| 81 | |
| 82 | public IpReachabilityEvent[] newArray(int size) { |
| 83 | return new IpReachabilityEvent[size]; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | public static void logProbeEvent(String ifName, int nlErrorCode) { |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | public static void logNudFailed(String ifName) { |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public static void logProvisioningLost(String ifName) { |
Hugo Benichi | 25bf8f5 | 2016-04-19 09:52:39 +0900 | [diff] [blame] | 94 | } |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 95 | |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame^] | 96 | /** |
| 97 | * Returns the NUD failure event type code corresponding to the given conditions. |
| 98 | * {@hide} |
| 99 | */ |
| 100 | public static int nudFailureEventType(boolean isFromProbe, boolean isProvisioningLost) { |
| 101 | if (isFromProbe) { |
| 102 | return isProvisioningLost ? PROVISIONING_LOST : NUD_FAILED; |
| 103 | } else { |
| 104 | return isProvisioningLost ? PROVISIONING_LOST_ORGANIC : NUD_FAILED_ORGANIC; |
| 105 | } |
| 106 | } |
| 107 | |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 108 | @Override |
| 109 | public String toString() { |
Hugo Benichi | 0d1c65b | 2016-06-22 17:01:43 +0900 | [diff] [blame^] | 110 | int hi = eventType & 0xff00; |
| 111 | int lo = eventType & 0x00ff; |
| 112 | String eventName = Decoder.constants.get(hi); |
| 113 | return String.format("IpReachabilityEvent(%s, %s:%02x)", ifName, eventName, lo); |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | final static class Decoder { |
| 117 | static final SparseArray<String> constants = |
| 118 | MessageUtils.findMessageNames(new Class[]{IpReachabilityEvent.class}, |
| 119 | new String[]{"PROBE", "PROVISIONING_", "NUD_"}); |
| 120 | } |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 121 | } |