1 /*
2  * Copyright (C) 2022 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.aware;
18 
19 import android.annotation.IntRange;
20 import android.net.wifi.ScanResult;
21 import android.net.wifi.WifiAnnotations;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import androidx.annotation.NonNull;
26 
27 import java.util.Objects;
28 
29 /**
30  * Wi-Fi Aware data-path channel information. The information can be extracted from the
31  * {@link WifiAwareNetworkInfo} using {@link WifiAwareNetworkInfo#getChannelInfoList()} ()}.
32  * Wi-Fi Aware data-path channel information includes the channel frequency, bandwidth and num of
33  * the spatial streams.
34  */
35 public final class WifiAwareChannelInfo implements Parcelable {
36     private final int mChannelFreq;
37     private final int mChannelBandwidth;
38     private final int mNumSpatialStreams;
39 
40     /** @hide **/
WifiAwareChannelInfo(int channelFreq, int channelBandwidth, int numSpatialStreams)41     public WifiAwareChannelInfo(int channelFreq, int channelBandwidth, int numSpatialStreams) {
42         mChannelFreq = channelFreq;
43         mChannelBandwidth = channelBandwidth;
44         mNumSpatialStreams = numSpatialStreams;
45     }
46 
47     /**
48      * Get the channel frequency in MHZ used by the Aware data path
49      * @return An integer represent the frequency of the channel in MHZ.
50      */
51     @IntRange(from = 0)
getChannelFrequencyMhz()52     public int getChannelFrequencyMhz() {
53         return mChannelFreq;
54     }
55 
56     /**
57      * Get the channel bandwidth used by the Aware data path
58      * @return one of {@link ScanResult#CHANNEL_WIDTH_20MHZ},
59      * {@link ScanResult#CHANNEL_WIDTH_40MHZ},
60      * {@link ScanResult#CHANNEL_WIDTH_80MHZ}, {@link ScanResult#CHANNEL_WIDTH_160MHZ}
61      * or {@link ScanResult #CHANNEL_WIDTH_80MHZ_PLUS_MHZ}.
62      */
63     @IntRange(from = 0)
getChannelBandwidth()64     public @WifiAnnotations.ChannelWidth int getChannelBandwidth() {
65         return mChannelBandwidth;
66     }
67 
68     /**
69      * Get the number of the spatial streams used by the Aware data path
70      * @return An integer represent number of the spatial streams are using in this channel.
71      */
72     @IntRange(from = 0)
getSpatialStreamCount()73     public int getSpatialStreamCount() {
74         return mNumSpatialStreams;
75     }
76 
WifiAwareChannelInfo(Parcel in)77     private WifiAwareChannelInfo(Parcel in) {
78         mChannelFreq = in.readInt();
79         mChannelBandwidth = in.readInt();
80         mNumSpatialStreams = in.readInt();
81     }
82 
83     public static final @NonNull Creator<WifiAwareChannelInfo> CREATOR =
84             new Creator<WifiAwareChannelInfo>() {
85                 @Override
86                 public WifiAwareChannelInfo createFromParcel(Parcel in) {
87                     return new WifiAwareChannelInfo(in);
88                 }
89 
90                 @Override
91                 public WifiAwareChannelInfo[] newArray(int size) {
92                     return new WifiAwareChannelInfo[size];
93                 }
94             };
95 
96     @Override
describeContents()97     public int describeContents() {
98         return 0;
99     }
100 
101     @Override
writeToParcel(@onNull Parcel dest, int flags)102     public void writeToParcel(@NonNull Parcel dest, int flags) {
103         dest.writeInt(mChannelFreq);
104         dest.writeInt(mChannelBandwidth);
105         dest.writeInt(mNumSpatialStreams);
106     }
107 
108     @Override
toString()109     public String toString() {
110         StringBuilder builder = new StringBuilder().append("{")
111                 .append(".channelFreq = ").append(mChannelFreq)
112                 .append(", .channelBandwidth = ").append(mChannelBandwidth)
113                 .append(", .numSpatialStreams = ").append(mNumSpatialStreams)
114                 .append("}");
115         return builder.toString();
116     }
117 
118     @Override
equals(Object obj)119     public boolean equals(Object obj) {
120         if (this == obj) {
121             return true;
122         }
123 
124         if (!(obj instanceof WifiAwareChannelInfo)) {
125             return false;
126         }
127 
128         WifiAwareChannelInfo lhs = (WifiAwareChannelInfo) obj;
129         return  mChannelFreq == lhs.mChannelFreq
130                 && mChannelBandwidth == lhs.mChannelBandwidth
131                 && mNumSpatialStreams == lhs.mNumSpatialStreams;
132     }
133 
134     @Override
hashCode()135     public int hashCode() {
136         return Objects.hash(mChannelFreq, mChannelBandwidth, mNumSpatialStreams);
137     }
138 }
139