1 /* 2 * Copyright (C) 2019 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.wifi; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SystemApi; 23 import android.net.MacAddress; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 import android.util.Log; 27 28 import com.android.wifi.flags.Flags; 29 30 import java.util.Objects; 31 32 /** @hide */ 33 @SystemApi 34 public final class WifiClient implements Parcelable { 35 36 private static final String TAG = "WifiClient"; 37 38 private final MacAddress mMacAddress; 39 40 /** The identifier of the AP instance which the client connected. */ 41 private final String mApInstanceIdentifier; 42 43 /** 44 * Reason for disconnection, if known. 45 * 46 * <p>This field is only meaningful when a client disconnects. 47 * It will not be updated while a client is connected. 48 */ 49 @WifiAnnotations.SoftApDisconnectReason 50 private final int mDisconnectReason; 51 52 /** 53 * The mac address of this client. 54 */ 55 @NonNull getMacAddress()56 public MacAddress getMacAddress() { 57 return mMacAddress; 58 } 59 60 /** 61 * Get AP instance identifier. 62 * 63 * The AP instance identifier is a unique identity which can be used to 64 * associate the {@link SoftApInfo} to a specific {@link WifiClient} 65 * - see {@link SoftApInfo#getApInstanceIdentifier()} 66 * @hide 67 */ 68 @NonNull getApInstanceIdentifier()69 public String getApInstanceIdentifier() { 70 return mApInstanceIdentifier; 71 } 72 73 /** 74 * Get the reason the client disconnected from the AP. 75 * 76 * <p>This field is only populated when the WifiClient is returned via 77 * {@link WifiManager.SoftApCallback#onClientsDisconnected}. 78 * The value {@link DeauthenticationReasonCode#REASON_UNKNOWN} is used as the default value 79 * and in the case where a client connects. 80 * @return a disconnection reason code to provide information on why the disconnect happened. 81 */ 82 @FlaggedApi(Flags.FLAG_SOFTAP_DISCONNECT_REASON) 83 @WifiAnnotations.SoftApDisconnectReason getDisconnectReason()84 public int getDisconnectReason() { 85 return mDisconnectReason; 86 } 87 WifiClient(Parcel in)88 private WifiClient(Parcel in) { 89 mMacAddress = in.readParcelable(null); 90 mApInstanceIdentifier = in.readString(); 91 mDisconnectReason = in.readInt(); 92 } 93 94 /** @hide */ WifiClient(@onNull MacAddress macAddress, @NonNull String apInstanceIdentifier)95 public WifiClient(@NonNull MacAddress macAddress, @NonNull String apInstanceIdentifier) { 96 this(macAddress, apInstanceIdentifier, DeauthenticationReasonCode.REASON_UNKNOWN); 97 } 98 99 /** @hide */ WifiClient(@onNull MacAddress macAddress, @NonNull String apInstanceIdentifier, @WifiAnnotations.SoftApDisconnectReason int disconnectReason)100 public WifiClient(@NonNull MacAddress macAddress, @NonNull String apInstanceIdentifier, 101 @WifiAnnotations.SoftApDisconnectReason int disconnectReason) { 102 if (macAddress == null) { 103 Log.wtf(TAG, "Null MacAddress provided"); 104 this.mMacAddress = WifiManager.ALL_ZEROS_MAC_ADDRESS; 105 } else { 106 this.mMacAddress = macAddress; 107 } 108 this.mApInstanceIdentifier = apInstanceIdentifier; 109 this.mDisconnectReason = disconnectReason; 110 } 111 112 @Override describeContents()113 public int describeContents() { 114 return 0; 115 } 116 117 @Override writeToParcel(@onNull Parcel dest, int flags)118 public void writeToParcel(@NonNull Parcel dest, int flags) { 119 dest.writeParcelable(mMacAddress, flags); 120 dest.writeString(mApInstanceIdentifier); 121 dest.writeInt(mDisconnectReason); 122 } 123 124 @NonNull 125 public static final Creator<WifiClient> CREATOR = new Creator<WifiClient>() { 126 public WifiClient createFromParcel(Parcel in) { 127 return new WifiClient(in); 128 } 129 130 public WifiClient[] newArray(int size) { 131 return new WifiClient[size]; 132 } 133 }; 134 135 @NonNull 136 @Override toString()137 public String toString() { 138 return "WifiClient{" 139 + "mMacAddress=" + mMacAddress 140 + "mApInstanceIdentifier=" + mApInstanceIdentifier 141 + "mDisconnectReason=" + mDisconnectReason 142 + '}'; 143 } 144 145 @Override equals(@ullable Object o)146 public boolean equals(@Nullable Object o) { 147 if (this == o) return true; 148 if (!(o instanceof WifiClient client)) return false; 149 return Objects.equals(mMacAddress, client.mMacAddress) 150 && mApInstanceIdentifier.equals(client.mApInstanceIdentifier) 151 && mDisconnectReason == client.mDisconnectReason; 152 } 153 154 @Override hashCode()155 public int hashCode() { 156 return Objects.hash(mMacAddress, mApInstanceIdentifier, mDisconnectReason); 157 } 158 } 159