blob: ee09e229266164b301742de36590d9ba5d6c1ad1 [file] [log] [blame]
Hugo Benichi25bf8f52016-04-19 09:52:39 +09001/*
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
17package android.net.metrics;
18
19import android.annotation.SystemApi;
20import android.os.Parcel;
21import android.os.Parcelable;
Hugo Benichi5df9d722016-04-25 17:16:35 +090022import android.util.SparseArray;
23
24import com.android.internal.util.MessageUtils;
Hugo Benichi25bf8f52016-04-19 09:52:39 +090025
26/**
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090027 * An event recorded when IpReachabilityMonitor sends a neighbor probe or receives
28 * a neighbor probe result.
Hugo Benichi25bf8f52016-04-19 09:52:39 +090029 * {@hide}
30 */
31@SystemApi
Hugo Benichicfddd682016-05-31 16:28:06 +090032public final class IpReachabilityEvent implements Parcelable {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090033
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090034 // 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 Benichi25bf8f52016-04-19 09:52:39 +090045
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 Benichi0d1c65b2016-06-22 17:01:43 +090051 // byte 3: when byte 2 == PROBE, errno code from RTNetlink or IpReachabilityMonitor.
Hugo Benichi25bf8f52016-04-19 09:52:39 +090052 public final int eventType;
53
Hugo Benichicfddd682016-05-31 16:28:06 +090054 /** {@hide} */
55 public IpReachabilityEvent(String ifName, int eventType) {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090056 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 Benichi0d1c65b2016-06-22 17:01:43 +090065 @Override
Hugo Benichi25bf8f52016-04-19 09:52:39 +090066 public void writeToParcel(Parcel out, int flags) {
67 out.writeString(ifName);
68 out.writeInt(eventType);
69 }
70
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090071 @Override
Hugo Benichi25bf8f52016-04-19 09:52:39 +090072 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 Benichi25bf8f52016-04-19 09:52:39 +090088 }
89
90 public static void logNudFailed(String ifName) {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090091 }
92
93 public static void logProvisioningLost(String ifName) {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090094 }
Hugo Benichi5df9d722016-04-25 17:16:35 +090095
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090096 /**
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 Benichi5df9d722016-04-25 17:16:35 +0900108 @Override
109 public String toString() {
Hugo Benichi0d1c65b2016-06-22 17:01:43 +0900110 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 Benichi5df9d722016-04-25 17:16:35 +0900114 }
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 Benichicfddd682016-05-31 16:28:06 +0900121}