Home | History | Annotate | Download | only in metrics
      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  * An event recorded when IpReachabilityMonitor sends a neighbor probe or receives
     28  * a neighbor probe result.
     29  * {@hide}
     30  */
     31 @SystemApi
     32 public final class IpReachabilityEvent implements Parcelable {
     33 
     34     // 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;
     45 
     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
     51     // byte 3: when byte 2 == PROBE, errno code from RTNetlink or IpReachabilityMonitor.
     52     public final int eventType;
     53 
     54     /** {@hide} */
     55     public IpReachabilityEvent(String ifName, int eventType) {
     56         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 
     65     @Override
     66     public void writeToParcel(Parcel out, int flags) {
     67         out.writeString(ifName);
     68         out.writeInt(eventType);
     69     }
     70 
     71     @Override
     72     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) {
     88     }
     89 
     90     public static void logNudFailed(String ifName) {
     91     }
     92 
     93     public static void logProvisioningLost(String ifName) {
     94     }
     95 
     96     /**
     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 
    108     @Override
    109     public String toString() {
    110         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);
    114     }
    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     }
    121 }
    122