blob: 715c95e7270b821b0fb798d00a95963f958b01ae [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
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010019import android.annotation.UnsupportedAppUsage;
Hugo Benichi25bf8f52016-04-19 09:52:39 +090020import 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 */
Hugo Benichicfddd682016-05-31 16:28:06 +090031public final class IpReachabilityEvent implements Parcelable {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090032
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090033 // Event types.
34 /** A probe forced by IpReachabilityMonitor. */
35 public static final int PROBE = 1 << 8;
36 /** Neighbor unreachable after a forced probe. */
37 public static final int NUD_FAILED = 2 << 8;
38 /** Neighbor unreachable after a forced probe, IP provisioning is also lost. */
39 public static final int PROVISIONING_LOST = 3 << 8;
Hugo Benichi95cb2262017-01-11 10:11:26 +090040 /** Neighbor unreachable notification from kernel. */
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090041 public static final int NUD_FAILED_ORGANIC = 4 << 8;
Hugo Benichi95cb2262017-01-11 10:11:26 +090042 /** Neighbor unreachable notification from kernel, IP provisioning is also lost. */
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090043 public static final int PROVISIONING_LOST_ORGANIC = 5 << 8;
Hugo Benichi25bf8f52016-04-19 09:52:39 +090044
Hugo Benichi25bf8f52016-04-19 09:52:39 +090045 // eventType byte format (MSB to LSB):
46 // byte 0: unused
47 // byte 1: unused
48 // byte 2: type of event: PROBE, NUD_FAILED, PROVISIONING_LOST
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090049 // byte 3: when byte 2 == PROBE, errno code from RTNetlink or IpReachabilityMonitor.
Hugo Benichi25bf8f52016-04-19 09:52:39 +090050 public final int eventType;
51
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010052 @UnsupportedAppUsage
Hugo Benichi948a8592017-03-16 16:33:47 +090053 public IpReachabilityEvent(int eventType) {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090054 this.eventType = eventType;
55 }
56
57 private IpReachabilityEvent(Parcel in) {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090058 this.eventType = in.readInt();
59 }
60
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090061 @Override
Hugo Benichi25bf8f52016-04-19 09:52:39 +090062 public void writeToParcel(Parcel out, int flags) {
Hugo Benichi25bf8f52016-04-19 09:52:39 +090063 out.writeInt(eventType);
64 }
65
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090066 @Override
Hugo Benichi25bf8f52016-04-19 09:52:39 +090067 public int describeContents() {
68 return 0;
69 }
70
71 public static final Parcelable.Creator<IpReachabilityEvent> CREATOR
72 = new Parcelable.Creator<IpReachabilityEvent>() {
73 public IpReachabilityEvent createFromParcel(Parcel in) {
74 return new IpReachabilityEvent(in);
75 }
76
77 public IpReachabilityEvent[] newArray(int size) {
78 return new IpReachabilityEvent[size];
79 }
80 };
81
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090082 /**
83 * Returns the NUD failure event type code corresponding to the given conditions.
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090084 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010085 @UnsupportedAppUsage
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090086 public static int nudFailureEventType(boolean isFromProbe, boolean isProvisioningLost) {
87 if (isFromProbe) {
88 return isProvisioningLost ? PROVISIONING_LOST : NUD_FAILED;
89 } else {
90 return isProvisioningLost ? PROVISIONING_LOST_ORGANIC : NUD_FAILED_ORGANIC;
91 }
92 }
93
Hugo Benichi5df9d722016-04-25 17:16:35 +090094 @Override
95 public String toString() {
Hugo Benichi0d1c65b2016-06-22 17:01:43 +090096 int hi = eventType & 0xff00;
97 int lo = eventType & 0x00ff;
98 String eventName = Decoder.constants.get(hi);
Hugo Benichi948a8592017-03-16 16:33:47 +090099 return String.format("IpReachabilityEvent(%s:%02x)", eventName, lo);
Hugo Benichi5df9d722016-04-25 17:16:35 +0900100 }
101
102 final static class Decoder {
103 static final SparseArray<String> constants =
104 MessageUtils.findMessageNames(new Class[]{IpReachabilityEvent.class},
105 new String[]{"PROBE", "PROVISIONING_", "NUD_"});
106 }
Hugo Benichicfddd682016-05-31 16:28:06 +0900107}