1 /* 2 * Copyright (C) 2024 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.p2p; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.IntRange; 21 import android.annotation.NonNull; 22 import android.annotation.RequiresApi; 23 import android.os.Build; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import com.android.wifi.flags.Flags; 28 29 /** 30 * A class representing a Wi-Fi P2P USD based service advertisement configuration for advertising 31 * the services. 32 */ 33 @RequiresApi(Build.VERSION_CODES.BAKLAVA) 34 @FlaggedApi(Flags.FLAG_WIFI_DIRECT_R2) 35 public final class WifiP2pUsdBasedLocalServiceAdvertisementConfig implements Parcelable { 36 /** 37 * Default channel frequency for USD based service discovery. 38 */ 39 private static final int USD_DEFAULT_DISCOVERY_CHANNEL_MHZ = 2437; 40 41 /** 42 * Frequency on which the service needs to be advertised. 43 */ 44 private int mFrequencyMhz; 45 WifiP2pUsdBasedLocalServiceAdvertisementConfig(int frequencyMhz)46 private WifiP2pUsdBasedLocalServiceAdvertisementConfig(int frequencyMhz) { 47 mFrequencyMhz = frequencyMhz; 48 } 49 50 /** 51 * Get the frequency on which the service is advertised. 52 */ 53 @IntRange(from = 0) getFrequencyMhz()54 public int getFrequencyMhz() { 55 return mFrequencyMhz; 56 } 57 58 /** 59 * Generates a string of all the defined elements. 60 * 61 * @return a compiled string representing all elements 62 */ toString()63 public String toString() { 64 StringBuilder sbuf = new StringBuilder("WifiP2pUsdBasedLocalServiceAdvertisementConfig:"); 65 sbuf.append("\n Frequency: ").append(mFrequencyMhz); 66 return sbuf.toString(); 67 } 68 69 /** Implement the Parcelable interface */ describeContents()70 public int describeContents() { 71 return 0; 72 } 73 74 /** Implement the Parcelable interface */ writeToParcel(@onNull Parcel dest, int flags)75 public void writeToParcel(@NonNull Parcel dest, int flags) { 76 dest.writeInt(mFrequencyMhz); 77 } 78 79 /** Implement the Parcelable interface */ WifiP2pUsdBasedLocalServiceAdvertisementConfig(@onNull Parcel in)80 private WifiP2pUsdBasedLocalServiceAdvertisementConfig(@NonNull Parcel in) { 81 this.mFrequencyMhz = in.readInt(); 82 } 83 84 /** Implement the Parcelable interface */ 85 @NonNull 86 public static final Creator<WifiP2pUsdBasedLocalServiceAdvertisementConfig> CREATOR = 87 new Creator<WifiP2pUsdBasedLocalServiceAdvertisementConfig>() { 88 public WifiP2pUsdBasedLocalServiceAdvertisementConfig createFromParcel(Parcel in) { 89 return new WifiP2pUsdBasedLocalServiceAdvertisementConfig(in); 90 } 91 92 public WifiP2pUsdBasedLocalServiceAdvertisementConfig[] newArray(int size) { 93 return new WifiP2pUsdBasedLocalServiceAdvertisementConfig[size]; 94 } 95 }; 96 97 /** 98 * Builder for {@link WifiP2pUsdBasedLocalServiceAdvertisementConfig}. 99 */ 100 public static final class Builder { 101 private int mFrequencyMhz; 102 103 /** 104 * Constructs a Builder with default values. 105 */ Builder()106 public Builder() { 107 mFrequencyMhz = USD_DEFAULT_DISCOVERY_CHANNEL_MHZ; 108 } 109 110 /** 111 * Specifies the frequency requested for advertising the service. 112 * 113 * @param frequencyMhz The requested frequency on which the service needs to be advertised. 114 * If not set, the default frequency is 115 * {@link #USD_DEFAULT_DISCOVERY_CHANNEL_MHZ} MHz. 116 * 117 * @return The builder to facilitate chaining {@code builder.setXXX(..).setXXX(..)}. 118 * 119 */ 120 @NonNull setFrequencyMhz(@ntRangefrom = 1) int frequencyMhz)121 public Builder setFrequencyMhz(@IntRange(from = 1) int frequencyMhz) { 122 if (frequencyMhz <= 0) { 123 throw new IllegalArgumentException("Frequency must be greater than 0"); 124 } 125 mFrequencyMhz = frequencyMhz; 126 return this; 127 } 128 129 /** 130 * Build {@link WifiP2pUsdBasedLocalServiceAdvertisementConfig} given the 131 * current requests made on the builder. 132 * @return {@link WifiP2pUsdBasedLocalServiceAdvertisementConfig} constructed based on 133 * builder method calls. 134 */ 135 @NonNull build()136 public WifiP2pUsdBasedLocalServiceAdvertisementConfig build() { 137 return new WifiP2pUsdBasedLocalServiceAdvertisementConfig(mFrequencyMhz); 138 } 139 } 140 } 141