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