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;
22 import android.util.SparseArray;
23 
24 import com.android.internal.util.MessageUtils;
25 
26 /**
27  * {@hide}
28  */
29 @SystemApi
30 public final class IpReachabilityEvent extends IpConnectivityEvent implements Parcelable {
31 
32     public static final int PROBE             = 1 << 8;
33     public static final int NUD_FAILED        = 2 << 8;
34     public static final int PROVISIONING_LOST = 3 << 8;
35 
36     public final String ifName;
37     // eventType byte format (MSB to LSB):
38     // byte 0: unused
39     // byte 1: unused
40     // byte 2: type of event: PROBE, NUD_FAILED, PROVISIONING_LOST
41     // byte 3: kernel errno from RTNetlink or IpReachabilityMonitor
42     public final int eventType;
43 
IpReachabilityEvent(String ifName, int eventType)44     private IpReachabilityEvent(String ifName, int eventType) {
45         this.ifName = ifName;
46         this.eventType = eventType;
47     }
48 
IpReachabilityEvent(Parcel in)49     private IpReachabilityEvent(Parcel in) {
50         this.ifName = in.readString();
51         this.eventType = in.readInt();
52     }
53 
writeToParcel(Parcel out, int flags)54     public void writeToParcel(Parcel out, int flags) {
55         out.writeString(ifName);
56         out.writeInt(eventType);
57     }
58 
describeContents()59     public int describeContents() {
60         return 0;
61     }
62 
63     public static final Parcelable.Creator<IpReachabilityEvent> CREATOR
64         = new Parcelable.Creator<IpReachabilityEvent>() {
65         public IpReachabilityEvent createFromParcel(Parcel in) {
66             return new IpReachabilityEvent(in);
67         }
68 
69         public IpReachabilityEvent[] newArray(int size) {
70             return new IpReachabilityEvent[size];
71         }
72     };
73 
logProbeEvent(String ifName, int nlErrorCode)74     public static void logProbeEvent(String ifName, int nlErrorCode) {
75         logEvent(new IpReachabilityEvent(ifName, PROBE | (nlErrorCode & 0xFF)));
76     }
77 
logNudFailed(String ifName)78     public static void logNudFailed(String ifName) {
79         logEvent(new IpReachabilityEvent(ifName, NUD_FAILED));
80     }
81 
logProvisioningLost(String ifName)82     public static void logProvisioningLost(String ifName) {
83         logEvent(new IpReachabilityEvent(ifName, PROVISIONING_LOST));
84     }
85 
86     @Override
toString()87     public String toString() {
88         return String.format("IpReachabilityEvent(%s, %s)", ifName,
89                 Decoder.constants.get(eventType));
90     }
91 
92     final static class Decoder {
93         static final SparseArray<String> constants =
94                 MessageUtils.findMessageNames(new Class[]{IpReachabilityEvent.class},
95                 new String[]{"PROBE", "PROVISIONING_", "NUD_"});
96     }
97 };
98