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.wifi.nl80211;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.util.Log;
22 
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Objects;
26 
27 /**
28  * SingleScanSettings for wificond
29  *
30  * @hide
31  */
32 public class SingleScanSettings implements Parcelable {
33     private static final String TAG = "SingleScanSettings";
34 
35     public int scanType;
36     public boolean enable6GhzRnr;
37     public ArrayList<ChannelSettings> channelSettings;
38     public ArrayList<HiddenNetwork> hiddenNetworks;
39     public byte[] vendorIes;
40 
41     /** public constructor */
SingleScanSettings()42     public SingleScanSettings() { }
43 
44     /** override comparator */
45     @Override
equals(Object rhs)46     public boolean equals(Object rhs) {
47         if (this == rhs) return true;
48         if (!(rhs instanceof SingleScanSettings)) {
49             return false;
50         }
51         SingleScanSettings settings = (SingleScanSettings) rhs;
52         if (settings == null) {
53             return false;
54         }
55         return scanType == settings.scanType
56                 && enable6GhzRnr == settings.enable6GhzRnr
57                 && channelSettings.equals(settings.channelSettings)
58                 && hiddenNetworks.equals(settings.hiddenNetworks)
59                 && Arrays.equals(vendorIes, settings.vendorIes);
60     }
61 
62     /** override hash code */
63     @Override
hashCode()64     public int hashCode() {
65         return Objects.hash(scanType, channelSettings, hiddenNetworks, enable6GhzRnr,
66                 Arrays.hashCode(vendorIes));
67     }
68 
69 
70     /** implement Parcelable interface */
71     @Override
describeContents()72     public int describeContents() {
73         return 0;
74     }
75 
isValidScanType(int scanType)76     private static boolean isValidScanType(int scanType) {
77         return scanType == IWifiScannerImpl.SCAN_TYPE_LOW_SPAN
78                 || scanType == IWifiScannerImpl.SCAN_TYPE_LOW_POWER
79                 || scanType == IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY;
80     }
81 
82     /**
83      * implement Parcelable interface
84      * |flags| is ignored.
85      */
86     @Override
writeToParcel(Parcel out, int flags)87     public void writeToParcel(Parcel out, int flags) {
88         if (!isValidScanType(scanType)) {
89             Log.wtf(TAG, "Invalid scan type " + scanType);
90         }
91         out.writeInt(scanType);
92         out.writeBoolean(enable6GhzRnr);
93         out.writeTypedList(channelSettings);
94         out.writeTypedList(hiddenNetworks);
95         if (vendorIes == null) {
96             out.writeByteArray(new byte[0]);
97         } else {
98             out.writeByteArray(vendorIes);
99         }
100     }
101 
102     /** implement Parcelable interface */
103     public static final Parcelable.Creator<SingleScanSettings> CREATOR =
104             new Parcelable.Creator<SingleScanSettings>() {
105         /**
106          * Caller is responsible for providing a valid parcel.
107          */
108         @Override
109         public SingleScanSettings createFromParcel(Parcel in) {
110             SingleScanSettings result = new SingleScanSettings();
111             result.scanType = in.readInt();
112             if (!isValidScanType(result.scanType)) {
113                 Log.wtf(TAG, "Invalid scan type " + result.scanType);
114             }
115             result.enable6GhzRnr = in.readBoolean();
116             result.channelSettings = new ArrayList<ChannelSettings>();
117             in.readTypedList(result.channelSettings, ChannelSettings.CREATOR);
118             result.hiddenNetworks = new ArrayList<HiddenNetwork>();
119             in.readTypedList(result.hiddenNetworks, HiddenNetwork.CREATOR);
120             result.vendorIes = in.createByteArray();
121             if (result.vendorIes == null) {
122                 result.vendorIes = new byte[0];
123             }
124             if (in.dataAvail() != 0) {
125                 Log.e(TAG, "Found trailing data after parcel parsing.");
126             }
127             return result;
128         }
129 
130         @Override
131         public SingleScanSettings[] newArray(int size) {
132             return new SingleScanSettings[size];
133         }
134     };
135 }
136