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