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.FlaggedApi;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.RequiresApi;
23 import android.annotation.SystemApi;
24 import android.net.wifi.OuiKeyedData;
25 import android.os.Build;
26 
27 import com.android.modules.utils.build.SdkLevel;
28 import com.android.wifi.flags.Flags;
29 
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 
34 /**
35  * An object providing information about a Wi-Fi Aware discovery session with a specific peer.
36  * @see DiscoverySessionCallback#onServiceDiscovered(ServiceDiscoveryInfo)
37  * @see DiscoverySessionCallback#onServiceDiscoveredWithinRange(ServiceDiscoveryInfo, int)
38  */
39 public final class ServiceDiscoveryInfo {
40     private final byte[] mServiceSpecificInfo;
41     private final List<byte[]> mMatchFilters;
42     private final int mPeerCipherSuite;
43     private final byte[] mScid;
44     private final PeerHandle mPeerHandle;
45     private final String mPairingAlias;
46     private final AwarePairingConfig mPairingConfig;
47     private final List<OuiKeyedData> mVendorData;
48 
49     /**
50      * @hide
51      */
ServiceDiscoveryInfo(PeerHandle peerHandle, int peerCipherSuite, @Nullable byte[] serviceSpecificInfo, @NonNull List<byte[]> matchFilter, @Nullable byte[] scid, String pairingAlias, AwarePairingConfig pairingConfig, @Nullable OuiKeyedData[] vendorData)52     public ServiceDiscoveryInfo(PeerHandle peerHandle, int peerCipherSuite,
53             @Nullable byte[] serviceSpecificInfo,
54             @NonNull List<byte[]> matchFilter, @Nullable byte[] scid, String pairingAlias,
55             AwarePairingConfig pairingConfig, @Nullable OuiKeyedData[] vendorData) {
56         mServiceSpecificInfo = serviceSpecificInfo;
57         mMatchFilters = matchFilter;
58         mPeerCipherSuite = peerCipherSuite;
59         mScid = scid;
60         mPeerHandle = peerHandle;
61         mPairingAlias = pairingAlias;
62         mPairingConfig = pairingConfig;
63         mVendorData = vendorData != null ? Arrays.asList(vendorData) : Collections.emptyList();
64     }
65 
66     /**
67      * Get the peer handle for the peer matching our discovery operation
68      * @return An opaque handle representing the discovered peer.
69      */
70     @NonNull
getPeerHandle()71     public PeerHandle getPeerHandle() {
72         return mPeerHandle;
73     }
74 
75     /**
76      * Get the filter which resulted in this service discovery. For
77      * {@link PublishConfig#PUBLISH_TYPE_UNSOLICITED},
78      * {@link SubscribeConfig#SUBSCRIBE_TYPE_PASSIVE} discovery sessions this is the publisher's
79      * match filter. For {@link PublishConfig#PUBLISH_TYPE_SOLICITED},
80      * {@link SubscribeConfig#SUBSCRIBE_TYPE_ACTIVE} discovery sessions this is the subscriber's
81      * match filter.
82      * @return A list of byte arrays representing the match filter. An empty list if match filter
83      * is not set.
84      */
85     @NonNull
getMatchFilters()86     public List<byte[]> getMatchFilters() {
87         return mMatchFilters;
88     }
89 
90     /**
91      * The service specific information (arbitrary byte array) provided by the peer as part of its
92      * discovery configuration.
93      * @see PublishConfig.Builder#setServiceSpecificInfo(byte[])
94      * @see SubscribeConfig.Builder#setServiceSpecificInfo(byte[])
95      * @return An arbitrary byte array represent the service specific information. {@code null} if
96      * service specific information is not set.
97      */
98     @Nullable
getServiceSpecificInfo()99     public byte[] getServiceSpecificInfo() {
100         return mServiceSpecificInfo;
101     }
102 
103     /**
104      * Get the Security context identifier is associate with PMK for data path security config. Only
105      * use for {@link Characteristics#WIFI_AWARE_CIPHER_SUITE_NCS_PK_128} and
106      * {@link Characteristics#WIFI_AWARE_CIPHER_SUITE_NCS_PK_256} to get the PMKID set by
107      * {@link WifiAwareDataPathSecurityConfig.Builder#setPmkId(byte[])} from publish session.
108      * This can help the Wi-Fi Aware data-path setup to select the correct PMK/PMKID
109      * @return An arbitrary byte array represent the security context identifier. {@code null} if
110      * Security context identifier is not set.
111      */
112     @Nullable
getScid()113     public byte[] getScid() {
114         return mScid;
115     }
116 
117     /**
118      * Get the cipher suite type specified by the publish session to be used for data-path setup.
119      * @return peerCipherSuite An integer represent the cipher suite used to encrypt the data-path.
120      */
getPeerCipherSuite()121     public @Characteristics.WifiAwareDataPathCipherSuites int getPeerCipherSuite() {
122         return mPeerCipherSuite;
123     }
124 
125     /**
126      * Get the paired device alias if the discovered device has already paired. If not null device
127      * will automatically start the NAN pairing verification,
128      * {@link DiscoverySessionCallback#onPairingVerificationSucceed(PeerHandle, String)}
129      * will trigger when verification is finished
130      */
131     @Nullable
getPairedAlias()132     public String getPairedAlias() {
133         return mPairingAlias;
134     }
135 
136     /**
137      * Get the discovered device's pairing config. Can be used for the following pairing setup or
138      * bootstrapping request.
139      * @see AwarePairingConfig
140      */
141     @Nullable
getPairingConfig()142     public AwarePairingConfig getPairingConfig() {
143         return mPairingConfig;
144     }
145 
146     /**
147      * Get the vendor-provided configuration data, if it exists.
148      *
149      * @return Vendor configuration data, or empty list if it does not exist.
150      * @hide
151      */
152     @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
153     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
154     @SystemApi
155     @NonNull
getVendorData()156     public List<OuiKeyedData> getVendorData() {
157         if (!SdkLevel.isAtLeastV()) {
158             throw new UnsupportedOperationException();
159         }
160         return mVendorData != null ? mVendorData : Collections.emptyList();
161     }
162 }
163