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 com.android.server.wifi.hal;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.MacAddress;
22 import android.net.wifi.OuiKeyedData;
23 import android.net.wifi.aware.AwarePairingConfig;
24 import android.net.wifi.aware.ConfigRequest;
25 import android.net.wifi.aware.PublishConfig;
26 import android.net.wifi.aware.SubscribeConfig;
27 import android.net.wifi.aware.WifiAwareChannelInfo;
28 import android.net.wifi.aware.WifiAwareDataPathSecurityConfig;
29 import android.util.Log;
30 
31 import com.android.internal.annotations.VisibleForTesting;
32 import com.android.server.wifi.aware.Capabilities;
33 import com.android.server.wifi.aware.PairingConfigManager;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.function.Supplier;
38 
39 /**
40  * Wrapper class for IWifiNanIface HAL calls.
41  */
42 public class WifiNanIface implements WifiHal.WifiInterface {
43     private static final String TAG = "WifiNanIface";
44     private IWifiNanIface mWifiNanIface;
45 
46     @VisibleForTesting
47     static final String SERVICE_NAME_FOR_OOB_DATA_PATH = "Wi-Fi Aware Data Path";
48 
49     /**
50      * Event types for a cluster event indication.
51      */
52     public static class NanClusterEventType {
53         public static final int DISCOVERY_MAC_ADDRESS_CHANGED = 0;
54         public static final int STARTED_CLUSTER = 1;
55         public static final int JOINED_CLUSTER = 2;
56 
57         /**
58          * Convert NanClusterEventType from HIDL to framework.
59          */
fromHidl(int code)60         public static int fromHidl(int code) {
61             switch (code) {
62                 case android.hardware.wifi.V1_0.NanClusterEventType.DISCOVERY_MAC_ADDRESS_CHANGED:
63                     return DISCOVERY_MAC_ADDRESS_CHANGED;
64                 case android.hardware.wifi.V1_0.NanClusterEventType.STARTED_CLUSTER:
65                     return STARTED_CLUSTER;
66                 case android.hardware.wifi.V1_0.NanClusterEventType.JOINED_CLUSTER:
67                     return JOINED_CLUSTER;
68                 default:
69                     Log.e(TAG, "Unknown NanClusterEventType received from HIDL: " + code);
70                     return -1;
71             }
72         }
73 
74         /**
75          * Convert NanClusterEventType from AIDL to framework.
76          */
fromAidl(int code)77         public static int fromAidl(int code) {
78             switch (code) {
79                 case android.hardware.wifi.NanClusterEventType.DISCOVERY_MAC_ADDRESS_CHANGED:
80                     return DISCOVERY_MAC_ADDRESS_CHANGED;
81                 case android.hardware.wifi.NanClusterEventType.STARTED_CLUSTER:
82                     return STARTED_CLUSTER;
83                 case android.hardware.wifi.NanClusterEventType.JOINED_CLUSTER:
84                     return JOINED_CLUSTER;
85                 default:
86                     Log.e(TAG, "Unknown NanClusterEventType received from HIDL: " + code);
87                     return -1;
88             }
89         }
90     }
91 
92     /**
93      * NAN DP (data-path) channel config options.
94      */
95     public static class NanDataPathChannelCfg {
96         public static final int CHANNEL_NOT_REQUESTED = 0;
97         public static final int REQUEST_CHANNEL_SETUP = 1;
98         public static final int FORCE_CHANNEL_SETUP = 2;
99 
100         /**
101          * Convert NanDataPathChannelCfg from framework to HIDL.
102          */
toHidl(int code)103         public static int toHidl(int code) {
104             switch (code) {
105                 case CHANNEL_NOT_REQUESTED:
106                     return android.hardware.wifi.V1_0.NanDataPathChannelCfg.CHANNEL_NOT_REQUESTED;
107                 case REQUEST_CHANNEL_SETUP:
108                     return android.hardware.wifi.V1_0.NanDataPathChannelCfg.REQUEST_CHANNEL_SETUP;
109                 case FORCE_CHANNEL_SETUP:
110                     return android.hardware.wifi.V1_0.NanDataPathChannelCfg.FORCE_CHANNEL_SETUP;
111                 default:
112                     Log.e(TAG, "Unknown NanDataPathChannelCfg received from framework: " + code);
113                     return -1;
114             }
115         }
116 
117         /**
118          * Convert NanDataPathChannelCfg from framework to AIDL.
119          */
toAidl(int code)120         public static int toAidl(int code) {
121             switch (code) {
122                 case CHANNEL_NOT_REQUESTED:
123                     return android.hardware.wifi.NanDataPathChannelCfg.CHANNEL_NOT_REQUESTED;
124                 case REQUEST_CHANNEL_SETUP:
125                     return android.hardware.wifi.NanDataPathChannelCfg.REQUEST_CHANNEL_SETUP;
126                 case FORCE_CHANNEL_SETUP:
127                     return android.hardware.wifi.NanDataPathChannelCfg.FORCE_CHANNEL_SETUP;
128                 default:
129                     Log.e(TAG, "Unknown NanDataPathChannelCfg received from framework: " + code);
130                     return -1;
131             }
132         }
133     }
134 
135     /**
136      * Ranging in the context of discovery session indication controls.
137      */
138     public static class NanRangingIndication {
139         public static final int CONTINUOUS_INDICATION_MASK = 1 << 0;
140         public static final int INGRESS_MET_MASK = 1 << 1;
141         public static final int EGRESS_MET_MASK = 1 << 2;
142 
143         /**
144          * Convert NanRangingIndication from HIDL to framework.
145          */
fromHidl(int rangingInd)146         public static int fromHidl(int rangingInd) {
147             int frameworkRangingInd = 0;
148             if ((android.hardware.wifi.V1_0.NanRangingIndication.CONTINUOUS_INDICATION_MASK
149                     & rangingInd) != 0) {
150                 frameworkRangingInd |= CONTINUOUS_INDICATION_MASK;
151             }
152             if ((android.hardware.wifi.V1_0.NanRangingIndication.INGRESS_MET_MASK
153                     & rangingInd) != 0) {
154                 frameworkRangingInd |= INGRESS_MET_MASK;
155             }
156             if ((android.hardware.wifi.V1_0.NanRangingIndication.EGRESS_MET_MASK
157                     & rangingInd) != 0) {
158                 frameworkRangingInd |= EGRESS_MET_MASK;
159             }
160             return frameworkRangingInd;
161         }
162 
163         /**
164          * Convert NanRangingIndication from AIDL to framework.
165          */
fromAidl(int rangingInd)166         public static int fromAidl(int rangingInd) {
167             int frameworkRangingInd = 0;
168             if ((android.hardware.wifi.NanRangingIndication.CONTINUOUS_INDICATION_MASK
169                     & rangingInd) != 0) {
170                 frameworkRangingInd |= CONTINUOUS_INDICATION_MASK;
171             }
172             if ((android.hardware.wifi.NanRangingIndication.INGRESS_MET_MASK
173                     & rangingInd) != 0) {
174                 frameworkRangingInd |= INGRESS_MET_MASK;
175             }
176             if ((android.hardware.wifi.NanRangingIndication.EGRESS_MET_MASK
177                     & rangingInd) != 0) {
178                 frameworkRangingInd |= EGRESS_MET_MASK;
179             }
180             return frameworkRangingInd;
181         }
182     }
183 
184     /**
185      * NAN API response codes used in request notifications and events.
186      */
187     public static class NanStatusCode {
188         public static final int SUCCESS = 0;
189         public static final int INTERNAL_FAILURE = 1;
190         public static final int PROTOCOL_FAILURE = 2;
191         public static final int INVALID_SESSION_ID = 3;
192         public static final int NO_RESOURCES_AVAILABLE = 4;
193         public static final int INVALID_ARGS = 5;
194         public static final int INVALID_PEER_ID = 6;
195         public static final int INVALID_NDP_ID = 7;
196         public static final int NAN_NOT_ALLOWED = 8;
197         public static final int NO_OTA_ACK = 9;
198         public static final int ALREADY_ENABLED = 10;
199         public static final int FOLLOWUP_TX_QUEUE_FULL = 11;
200         public static final int UNSUPPORTED_CONCURRENCY_NAN_DISABLED = 12;
201         public static final int INVALID_PAIRING_ID = 13;
202         public static final int INVALID_BOOTSTRAPPING_ID = 14;
203         public static final int REDUNDANT_REQUEST = 15;
204         public static final int NOT_SUPPORTED = 16;
205         public static final int NO_CONNECTION = 17;
206 
207         /**
208          * Convert NanStatusCode from HIDL to framework.
209          */
fromHidl(int code)210         public static int fromHidl(int code) {
211             switch (code) {
212                 case android.hardware.wifi.V1_0.NanStatusType.SUCCESS:
213                     return SUCCESS;
214                 case android.hardware.wifi.V1_0.NanStatusType.INTERNAL_FAILURE:
215                     return INTERNAL_FAILURE;
216                 case android.hardware.wifi.V1_0.NanStatusType.PROTOCOL_FAILURE:
217                     return PROTOCOL_FAILURE;
218                 case android.hardware.wifi.V1_0.NanStatusType.INVALID_SESSION_ID:
219                     return INVALID_SESSION_ID;
220                 case android.hardware.wifi.V1_0.NanStatusType.NO_RESOURCES_AVAILABLE:
221                     return NO_RESOURCES_AVAILABLE;
222                 case android.hardware.wifi.V1_0.NanStatusType.INVALID_ARGS:
223                     return INVALID_ARGS;
224                 case android.hardware.wifi.V1_0.NanStatusType.INVALID_PEER_ID:
225                     return INVALID_PEER_ID;
226                 case android.hardware.wifi.V1_0.NanStatusType.INVALID_NDP_ID:
227                     return INVALID_NDP_ID;
228                 case android.hardware.wifi.V1_0.NanStatusType.NAN_NOT_ALLOWED:
229                     return NAN_NOT_ALLOWED;
230                 case android.hardware.wifi.V1_0.NanStatusType.NO_OTA_ACK:
231                     return NO_OTA_ACK;
232                 case android.hardware.wifi.V1_0.NanStatusType.ALREADY_ENABLED:
233                     return ALREADY_ENABLED;
234                 case android.hardware.wifi.V1_0.NanStatusType.FOLLOWUP_TX_QUEUE_FULL:
235                     return FOLLOWUP_TX_QUEUE_FULL;
236                 case android.hardware.wifi.V1_0.NanStatusType.UNSUPPORTED_CONCURRENCY_NAN_DISABLED:
237                     return UNSUPPORTED_CONCURRENCY_NAN_DISABLED;
238                 default:
239                     Log.e(TAG, "Unknown NanStatusType received from HIDL: " + code);
240                     return -1;
241             }
242         }
243 
244         /**
245          * Convert NanStatusCode from AIDL to framework.
246          */
fromAidl(int code)247         public static int fromAidl(int code) {
248             switch (code) {
249                 case android.hardware.wifi.NanStatusCode.SUCCESS:
250                     return SUCCESS;
251                 case android.hardware.wifi.NanStatusCode.INTERNAL_FAILURE:
252                     return INTERNAL_FAILURE;
253                 case android.hardware.wifi.NanStatusCode.PROTOCOL_FAILURE:
254                     return PROTOCOL_FAILURE;
255                 case android.hardware.wifi.NanStatusCode.INVALID_SESSION_ID:
256                     return INVALID_SESSION_ID;
257                 case android.hardware.wifi.NanStatusCode.NO_RESOURCES_AVAILABLE:
258                     return NO_RESOURCES_AVAILABLE;
259                 case android.hardware.wifi.NanStatusCode.INVALID_ARGS:
260                     return INVALID_ARGS;
261                 case android.hardware.wifi.NanStatusCode.INVALID_PEER_ID:
262                     return INVALID_PEER_ID;
263                 case android.hardware.wifi.NanStatusCode.INVALID_NDP_ID:
264                     return INVALID_NDP_ID;
265                 case android.hardware.wifi.NanStatusCode.NAN_NOT_ALLOWED:
266                     return NAN_NOT_ALLOWED;
267                 case android.hardware.wifi.NanStatusCode.NO_OTA_ACK:
268                     return NO_OTA_ACK;
269                 case android.hardware.wifi.NanStatusCode.ALREADY_ENABLED:
270                     return ALREADY_ENABLED;
271                 case android.hardware.wifi.NanStatusCode.FOLLOWUP_TX_QUEUE_FULL:
272                     return FOLLOWUP_TX_QUEUE_FULL;
273                 case android.hardware.wifi.NanStatusCode.UNSUPPORTED_CONCURRENCY_NAN_DISABLED:
274                     return UNSUPPORTED_CONCURRENCY_NAN_DISABLED;
275                 case android.hardware.wifi.NanStatusCode.INVALID_PAIRING_ID:
276                     return INVALID_PAIRING_ID;
277                 case android.hardware.wifi.NanStatusCode.INVALID_BOOTSTRAPPING_ID:
278                     return INVALID_BOOTSTRAPPING_ID;
279                 case android.hardware.wifi.NanStatusCode.REDUNDANT_REQUEST:
280                     return REDUNDANT_REQUEST;
281                 case android.hardware.wifi.NanStatusCode.NOT_SUPPORTED:
282                     return NOT_SUPPORTED;
283                 case android.hardware.wifi.NanStatusCode.NO_CONNECTION:
284                     return NO_CONNECTION;
285                 default:
286                     Log.e(TAG, "Unknown NanStatusType received from AIDL: " + code);
287                     return -1;
288             }
289         }
290     }
291 
292     /**
293      * Configuration parameters used in the call to enableAndConfigure.
294      */
295     public static class PowerParameters {
296         public int discoveryWindow24Ghz;
297         public int discoveryWindow5Ghz;
298         public int discoveryWindow6Ghz;
299         public int discoveryBeaconIntervalMs;
300         public int numberOfSpatialStreamsInDiscovery;
301         public boolean enableDiscoveryWindowEarlyTermination;
302     }
303 
WifiNanIface(@onNull android.hardware.wifi.V1_0.IWifiNanIface nanIface)304     public WifiNanIface(@NonNull android.hardware.wifi.V1_0.IWifiNanIface nanIface) {
305         Log.i(TAG, "Creating WifiNanIface using the HIDL implementation");
306         mWifiNanIface = createWifiNanIfaceHidlImplMockable(nanIface);
307     }
308 
WifiNanIface(@onNull android.hardware.wifi.IWifiNanIface nanIface)309     public WifiNanIface(@NonNull android.hardware.wifi.IWifiNanIface nanIface) {
310         mWifiNanIface = createWifiNanIfaceAidlImplMockable(nanIface);
311     }
312 
createWifiNanIfaceHidlImplMockable( android.hardware.wifi.V1_0.IWifiNanIface nanIface)313     protected WifiNanIfaceHidlImpl createWifiNanIfaceHidlImplMockable(
314             android.hardware.wifi.V1_0.IWifiNanIface nanIface) {
315         return new WifiNanIfaceHidlImpl(nanIface);
316     }
317 
createWifiNanIfaceAidlImplMockable( android.hardware.wifi.IWifiNanIface nanIface)318     protected WifiNanIfaceAidlImpl createWifiNanIfaceAidlImplMockable(
319             android.hardware.wifi.IWifiNanIface nanIface) {
320         return new WifiNanIfaceAidlImpl(nanIface);
321     }
322 
validateAndCall(String methodStr, T defaultVal, @NonNull Supplier<T> supplier)323     private <T> T validateAndCall(String methodStr, T defaultVal, @NonNull Supplier<T> supplier) {
324         if (mWifiNanIface == null) {
325             Log.wtf(TAG, "Cannot call " + methodStr + " because mWifiNanIface is null");
326             return defaultVal;
327         }
328         return supplier.get();
329     }
330 
331     /**
332      * Enable verbose logging.
333      */
enableVerboseLogging(boolean verbose)334     public void enableVerboseLogging(boolean verbose) {
335         if (mWifiNanIface != null) {
336             mWifiNanIface.enableVerboseLogging(verbose);
337         }
338     }
339 
340     /**
341      * See comments for {@link IWifiNanIface#registerFrameworkCallback(Callback)}
342      */
registerFrameworkCallback(Callback cb)343     public boolean registerFrameworkCallback(Callback cb) {
344         return validateAndCall("registerFrameworkCallback", false,
345                 () -> mWifiNanIface.registerFrameworkCallback(cb));
346     }
347 
348     /**
349      * See comments for {@link IWifiNanIface#getName()}
350      */
351     @Override
352     @Nullable
getName()353     public String getName() {
354         return validateAndCall("getName", null,
355                 () -> mWifiNanIface.getName());
356     }
357 
358     /**
359      * See comments for {@link IWifiNanIface#getCapabilities(short)}
360      */
getCapabilities(short transactionId)361     public boolean getCapabilities(short transactionId) {
362         return validateAndCall("getCapabilities", false,
363                 () -> mWifiNanIface.getCapabilities(transactionId));
364     }
365 
366     /**
367      * See comments for {@link IWifiNanIface#enableAndConfigure(short, ConfigRequest, boolean,
368      *                         boolean, boolean, boolean, int, int, PowerParameters)}
369      */
enableAndConfigure(short transactionId, ConfigRequest configRequest, boolean notifyIdentityChange, boolean initialConfiguration, boolean rangingEnabled, boolean isInstantCommunicationEnabled, int instantModeChannel, int clusterId, int macAddressRandomizationIntervalSec, PowerParameters powerParameters)370     public boolean enableAndConfigure(short transactionId, ConfigRequest configRequest,
371             boolean notifyIdentityChange, boolean initialConfiguration, boolean rangingEnabled,
372             boolean isInstantCommunicationEnabled, int instantModeChannel, int clusterId,
373             int macAddressRandomizationIntervalSec, PowerParameters powerParameters) {
374         return validateAndCall("enableAndConfigure", false,
375                 () -> mWifiNanIface.enableAndConfigure(transactionId, configRequest,
376                         notifyIdentityChange, initialConfiguration, rangingEnabled,
377                         isInstantCommunicationEnabled, instantModeChannel, clusterId,
378                         macAddressRandomizationIntervalSec, powerParameters));
379     }
380 
381     /**
382      * See comments for {@link IWifiNanIface#disable(short)}
383      */
disable(short transactionId)384     public boolean disable(short transactionId) {
385         return validateAndCall("disable", false,
386                 () -> mWifiNanIface.disable(transactionId));
387     }
388 
389     /**
390      * See comments for {@link IWifiNanIface#publish(short, byte, PublishConfig, byte[])}
391      */
publish(short transactionId, byte publishId, PublishConfig publishConfig, byte[] nik)392     public boolean publish(short transactionId, byte publishId, PublishConfig publishConfig,
393             byte[] nik) {
394         return validateAndCall("publish", false,
395                 () -> mWifiNanIface.publish(transactionId, publishId, publishConfig, nik));
396     }
397 
398     /**
399      * See comments for {@link IWifiNanIface#subscribe(short, byte, SubscribeConfig, byte[])}
400      */
subscribe(short transactionId, byte subscribeId, SubscribeConfig subscribeConfig, byte[] nik)401     public boolean subscribe(short transactionId, byte subscribeId,
402             SubscribeConfig subscribeConfig, byte[] nik) {
403         return validateAndCall("subscribe", false,
404                 () -> mWifiNanIface.subscribe(transactionId, subscribeId, subscribeConfig, nik));
405     }
406 
407     /**
408      * See comments for {@link IWifiNanIface#sendMessage(short, byte, int, MacAddress, byte[])}
409      */
sendMessage(short transactionId, byte pubSubId, int requestorInstanceId, MacAddress dest, byte[] message)410     public boolean sendMessage(short transactionId, byte pubSubId, int requestorInstanceId,
411             MacAddress dest, byte[] message) {
412         return validateAndCall("sendMessage", false,
413                 () -> mWifiNanIface.sendMessage(transactionId, pubSubId, requestorInstanceId,
414                         dest, message));
415     }
416 
417     /**
418      * See comments for {@link IWifiNanIface#stopPublish(short, byte)}
419      */
stopPublish(short transactionId, byte pubSubId)420     public boolean stopPublish(short transactionId, byte pubSubId) {
421         return validateAndCall("stopPublish", false,
422                 () -> mWifiNanIface.stopPublish(transactionId, pubSubId));
423     }
424 
425     /**
426      * See comments for {@link IWifiNanIface#stopSubscribe(short, byte)}
427      */
stopSubscribe(short transactionId, byte pubSubId)428     public boolean stopSubscribe(short transactionId, byte pubSubId) {
429         return validateAndCall("stopSubscribe", false,
430                 () -> mWifiNanIface.stopSubscribe(transactionId, pubSubId));
431     }
432 
433     /**
434      * See comments for {@link IWifiNanIface#createAwareNetworkInterface(short, String)}
435      */
createAwareNetworkInterface(short transactionId, String interfaceName)436     public boolean createAwareNetworkInterface(short transactionId, String interfaceName) {
437         return validateAndCall("createAwareNetworkInterface", false,
438                 () -> mWifiNanIface.createAwareNetworkInterface(transactionId, interfaceName));
439     }
440 
441     /**
442      * See comments for {@link IWifiNanIface#deleteAwareNetworkInterface(short, String)}
443      */
deleteAwareNetworkInterface(short transactionId, String interfaceName)444     public boolean deleteAwareNetworkInterface(short transactionId, String interfaceName) {
445         return validateAndCall("deleteAwareNetworkInterface", false,
446                 () -> mWifiNanIface.deleteAwareNetworkInterface(transactionId, interfaceName));
447     }
448 
449     /**
450      * See comments for
451      * {@link IWifiNanIface#initiateDataPath(short, int, int, int, MacAddress, String, boolean, byte[], Capabilities, WifiAwareDataPathSecurityConfig, byte)}
452      */
initiateDataPath(short transactionId, int peerId, int channelRequestType, int channel, MacAddress peer, String interfaceName, boolean isOutOfBand, byte[] appInfo, Capabilities capabilities, WifiAwareDataPathSecurityConfig securityConfig, byte pubSubId)453     public boolean initiateDataPath(short transactionId, int peerId, int channelRequestType,
454             int channel, MacAddress peer, String interfaceName,
455             boolean isOutOfBand, byte[] appInfo, Capabilities capabilities,
456             WifiAwareDataPathSecurityConfig securityConfig, byte pubSubId) {
457         return validateAndCall("initiateDataPath", false,
458                 () -> mWifiNanIface.initiateDataPath(transactionId, peerId, channelRequestType,
459                         channel, peer, interfaceName, isOutOfBand, appInfo, capabilities,
460                         securityConfig, pubSubId));
461     }
462 
463     /**
464      * See comments for
465      * {@link IWifiNanIface#respondToDataPathRequest(short, boolean, int, String, byte[], boolean, Capabilities, WifiAwareDataPathSecurityConfig, byte)}
466      */
respondToDataPathRequest(short transactionId, boolean accept, int ndpId, String interfaceName, byte[] appInfo, boolean isOutOfBand, Capabilities capabilities, WifiAwareDataPathSecurityConfig securityConfig, byte pubSubId)467     public boolean respondToDataPathRequest(short transactionId, boolean accept, int ndpId,
468             String interfaceName, byte[] appInfo,
469             boolean isOutOfBand, Capabilities capabilities,
470             WifiAwareDataPathSecurityConfig securityConfig, byte pubSubId) {
471         return validateAndCall("respondToDataPathRequest", false,
472                 () -> mWifiNanIface.respondToDataPathRequest(transactionId, accept, ndpId,
473                         interfaceName, appInfo, isOutOfBand, capabilities, securityConfig,
474                         pubSubId));
475     }
476 
477     /**
478      * See comments for {@link IWifiNanIface#endDataPath(short, int)}
479      */
endDataPath(short transactionId, int ndpId)480     public boolean endDataPath(short transactionId, int ndpId) {
481         return validateAndCall("endDataPath", false,
482                 () -> mWifiNanIface.endDataPath(transactionId, ndpId));
483     }
484 
485     /**
486      * {@link IWifiNanIface#initiateNanPairingRequest(short, int, MacAddress, byte[], boolean, int, byte[], String, int, int)}
487      */
initiatePairing(short transactionId, int peerId, MacAddress peer, byte[] pairingIdentityKey, boolean enablePairingCache, int requestType, byte[] pmk, String password, int akm, int cipherSuite)488     public boolean initiatePairing(short transactionId, int peerId, MacAddress peer,
489             byte[] pairingIdentityKey, boolean enablePairingCache, int requestType, byte[] pmk,
490             String password, int akm, int cipherSuite) {
491         return validateAndCall("initiatePairing", false,
492                 () -> mWifiNanIface.initiateNanPairingRequest(transactionId, peerId, peer,
493                         pairingIdentityKey, enablePairingCache, requestType, pmk, password, akm,
494                         cipherSuite));
495     }
496 
497     /**
498      * {@link IWifiNanIface#endPairing(short, int)}
499      */
endPairing(short transactionId, int pairingId)500     public boolean endPairing(short transactionId, int pairingId) {
501         return validateAndCall("initiatePairing", false,
502                 () -> mWifiNanIface.endPairing(transactionId, pairingId));
503     }
504 
505     /**
506      * {@link IWifiNanIface#respondToPairingRequest(short, int, boolean, byte[], boolean, int, byte[], String, int, int)}
507      */
respondToPairingRequest(short transactionId, int pairingId, boolean accept, byte[] pairingIdentityKey, boolean enablePairingCache, int requestType, byte[] pmk, String password, int akm, int cipherSuite)508     public boolean respondToPairingRequest(short transactionId, int pairingId, boolean accept,
509             byte[] pairingIdentityKey, boolean enablePairingCache, int requestType, byte[] pmk,
510             String password, int akm, int cipherSuite) {
511         return validateAndCall("respondToPairingRequest", false,
512                 () -> mWifiNanIface.respondToPairingRequest(transactionId, pairingId, accept,
513                         pairingIdentityKey, enablePairingCache, requestType, pmk, password, akm,
514                         cipherSuite));
515     }
516     /**
517      * {@link IWifiNanIface#initiateNanBootstrappingRequest(short, int, MacAddress, int, byte[], byte, boolean)}
518      */
initiateBootstrapping(short transactionId, int peerId, MacAddress peer, int method, byte[] cookie, byte pubSubId, boolean isComeBack)519     public boolean initiateBootstrapping(short transactionId, int peerId, MacAddress peer,
520             int method, byte[] cookie, byte pubSubId, boolean isComeBack) {
521         return validateAndCall("initiateBootstrapping", false,
522                 () -> mWifiNanIface.initiateNanBootstrappingRequest(transactionId, peerId, peer,
523                         method, cookie, pubSubId, isComeBack));
524     }
525     /**
526      * {@link IWifiNanIface#respondToNanBootstrappingRequest(short, int, boolean, byte)}
527      */
respondToBootstrappingRequest(short transactionId, int bootstrappingId, boolean accept, byte pubSubId)528     public boolean respondToBootstrappingRequest(short transactionId, int bootstrappingId,
529             boolean accept, byte pubSubId) {
530         return validateAndCall("initiateBootstrapping", false,
531                 () -> mWifiNanIface.respondToNanBootstrappingRequest(transactionId, bootstrappingId,
532                         accept, pubSubId));
533     }
534 
535     /**
536      * See comments for {@link IWifiNanIface#suspend(short, byte)}
537      */
suspendRequest(short transactionId, byte pubSubId)538     public boolean suspendRequest(short transactionId, byte pubSubId) {
539         return validateAndCall("suspendRequest", false,
540             () -> mWifiNanIface.suspend(transactionId, pubSubId));
541     }
542 
543     /**
544      * See comments for {@link IWifiNanIface#resume(short, byte)}
545      */
resumeRequest(short transactionId, byte pubSubId)546     public boolean resumeRequest(short transactionId, byte pubSubId) {
547         return validateAndCall("resumeRequest", false,
548             () -> mWifiNanIface.resume(transactionId, pubSubId));
549     }
550 
551     /**
552      * Framework callback object. Will get called when the equivalent events are received
553      * from the HAL.
554      */
555     public interface Callback {
556         /**
557          * Invoked in response to a capability request.
558          * @param id ID corresponding to the original request.
559          * @param capabilities Capability data.
560          */
notifyCapabilitiesResponse(short id, Capabilities capabilities)561         void notifyCapabilitiesResponse(short id, Capabilities capabilities);
562 
563         /**
564          * Invoked in response to an enable request.
565          * @param id ID corresponding to the original request.
566          * @param status Status the operation (see {@link NanStatusCode}).
567          */
notifyEnableResponse(short id, int status)568         void notifyEnableResponse(short id, int status);
569 
570         /**
571          * Invoked in response to a config request.
572          * @param id ID corresponding to the original request.
573          * @param status Status the operation (see {@link NanStatusCode}).
574          */
notifyConfigResponse(short id, int status)575         void notifyConfigResponse(short id, int status);
576 
577         /**
578          * Invoked in response to a disable request.
579          * @param id ID corresponding to the original request.
580          * @param status Status the operation (see {@link NanStatusCode}).
581          */
notifyDisableResponse(short id, int status)582         void notifyDisableResponse(short id, int status);
583 
584         /**
585          * Invoked to notify the status of the start publish request.
586          * @param id ID corresponding to the original request.
587          * @param status Status the operation (see {@link NanStatusCode}).
588          * @param publishId
589          */
notifyStartPublishResponse(short id, int status, byte publishId)590         void notifyStartPublishResponse(short id, int status, byte publishId);
591 
592         /**
593          * Invoked to notify the status of the start subscribe request.
594          * @param id ID corresponding to the original request.
595          * @param status Status the operation (see {@link NanStatusCode}).
596          * @param subscribeId ID of the new subscribe session (if successfully created).
597          */
notifyStartSubscribeResponse(short id, int status, byte subscribeId)598         void notifyStartSubscribeResponse(short id, int status, byte subscribeId);
599 
600         /**
601          * Invoked in response to a transmit followup request.
602          * @param id ID corresponding to the original request.
603          * @param status Status the operation (see {@link NanStatusCode}).
604          */
notifyTransmitFollowupResponse(short id, int status)605         void notifyTransmitFollowupResponse(short id, int status);
606 
607         /**
608          * Invoked in response to a create data interface request.
609          * @param id ID corresponding to the original request.
610          * @param status Status the operation (see {@link NanStatusCode}).
611          */
notifyCreateDataInterfaceResponse(short id, int status)612         void notifyCreateDataInterfaceResponse(short id, int status);
613 
614         /**
615          * Invoked in response to a delete data interface request.
616          * @param id ID corresponding to the original request.
617          * @param status Status the operation (see {@link NanStatusCode}).
618          */
notifyDeleteDataInterfaceResponse(short id, int status)619         void notifyDeleteDataInterfaceResponse(short id, int status);
620 
621         /**
622          * Invoked in response to a delete data interface request.
623          * @param id ID corresponding to the original request.
624          * @param status Status the operation (see {@link NanStatusCode}).
625          * @param ndpInstanceId
626          */
notifyInitiateDataPathResponse(short id, int status, int ndpInstanceId)627         void notifyInitiateDataPathResponse(short id, int status, int ndpInstanceId);
628 
629         /**
630          * Invoked in response to a respond to data path indication request.
631          * @param id ID corresponding to the original request.
632          * @param status Status the operation (see {@link NanStatusCode}).
633          */
notifyRespondToDataPathIndicationResponse(short id, int status)634         void notifyRespondToDataPathIndicationResponse(short id, int status);
635 
636         /**
637          * Invoked in response to a terminate data path request.
638          * @param id ID corresponding to the original request.
639          * @param status Status the operation (see {@link NanStatusCode}).
640          */
notifyTerminateDataPathResponse(short id, int status)641         void notifyTerminateDataPathResponse(short id, int status);
642 
643         /**
644          * Invoked in response to a initiate NAN pairing request.
645          * @param id ID corresponding to the original request.
646          * @param status Status the operation (see {@link NanStatusCode}).
647          */
notifyInitiatePairingResponse(short id, int status, int pairingInstanceId)648         void notifyInitiatePairingResponse(short id, int status,
649                 int pairingInstanceId);
650 
651         /**
652          * Invoked in response to a response NAN pairing request.
653          * @param id ID corresponding to the original request.
654          * @param status Status the operation (see {@link NanStatusCode}).
655          */
notifyRespondToPairingIndicationResponse(short id, int status)656         void notifyRespondToPairingIndicationResponse(short id, int status);
657 
658         /**
659          * Invoked in response to a initiate NAN Bootstrapping request.
660          * @param id ID corresponding to the original request.
661          * @param status Status the operation (see {@link NanStatusCode}).
662          */
notifyInitiateBootstrappingResponse(short id, int status, int bootstrappingInstanceId)663         void notifyInitiateBootstrappingResponse(short id, int status,
664                 int bootstrappingInstanceId);
665 
666         /**
667          * Invoked in response to a response NAN Bootstrapping request.
668          * @param id ID corresponding to the original request.
669          * @param status Status the operation (see {@link NanStatusCode}).
670          */
notifyRespondToBootstrappingIndicationResponse(short id, int status)671         void notifyRespondToBootstrappingIndicationResponse(short id, int status);
672 
673         /**
674          * Invoked in response to a connection suspension request.
675          * @param id ID corresponding to the original request.
676          * @param status Status the operation (see {@link NanStatusCode}).
677          */
notifySuspendResponse(short id, int status)678         void notifySuspendResponse(short id, int status);
679 
680         /**
681          * Invoked in response to a connection resume request.
682          * @param id ID corresponding to the original request.
683          * @param status Status the operation (see {@link NanStatusCode}).
684          */
notifyResumeResponse(short id, int status)685         void notifyResumeResponse(short id, int status);
686 
687         /**
688          * Invoked in response to a pairing termination request.
689          * @param id ID corresponding to the original request.
690          * @param status Status the operation (see {@link NanStatusCode}).
691          */
notifyTerminatePairingResponse(short id, int status)692         void notifyTerminatePairingResponse(short id, int status);
693 
694         /**
695          * Indicates that a cluster event has been received.
696          * @param eventType Type of the cluster event (see {@link NanClusterEventType}).
697          * @param addr MAC Address associated with the corresponding event.
698          */
eventClusterEvent(int eventType, byte[] addr)699         void eventClusterEvent(int eventType, byte[] addr);
700 
701         /**
702          * Indicates that a NAN has been disabled.
703          * @param status Status the operation (see {@link NanStatusCode}).
704          */
eventDisabled(int status)705         void eventDisabled(int status);
706 
707         /**
708          * Indicates that an active publish session has terminated.
709          * @param sessionId Discovery session ID of the terminated session.
710          * @param status Status the operation (see {@link NanStatusCode}).
711          */
eventPublishTerminated(byte sessionId, int status)712         void eventPublishTerminated(byte sessionId, int status);
713 
714         /**
715          * Indicates that an active subscribe session has terminated.
716          * @param sessionId Discovery session ID of the terminated session.
717          * @param status Status the operation (see {@link NanStatusCode}).
718          */
eventSubscribeTerminated(byte sessionId, int status)719         void eventSubscribeTerminated(byte sessionId, int status);
720 
721         /**
722          * Indicates that a match has occurred - i.e. a service has been discovered.
723          * @param discoverySessionId Publish or subscribe discovery session ID of an existing
724          *                           discovery session.
725          * @param peerId Unique ID of the peer. Can be used subsequently in sendMessage()
726          *               or to set up a data-path.
727          * @param addr NAN Discovery (management) MAC address of the peer.
728          * @param serviceSpecificInfo The arbitrary information contained in the
729          *                            |NanDiscoveryCommonConfig.serviceSpecificInfo| of
730          *                            the peer's discovery session configuration.
731          * @param matchFilter The match filter from the discovery packet (publish or subscribe)
732          *                    which caused service discovery. Matches the
733          *                    |NanDiscoveryCommonConfig.txMatchFilter| of the peer's unsolicited
734          *                    publish message or of the local device's Active subscribe message.
735          * @param rangingIndicationType The ranging event(s) which triggered the ranging. Ex. can
736          *                              indicate that continuous ranging was requested, or else that
737          *                              an ingress event occurred. See {@link NanRangingIndication}.
738          * @param rangingMeasurementInMm If ranging was required and executed, contains the
739          *                               distance to the peer in mm.
740          * @param scid Security Context Identifier identifies the Security Context.
741          *             For NAN Shared Key Cipher Suite, this field contains the 16 octet PMKID
742          *             identifying the PMK used for setting up the Secure Data Path.
743          * @param peerCipherType Cipher type for data-paths constructed in the context of this
744          *                       discovery session.
745          * @param vendorData Additional vendor-specific parameters, or null if not provided.
746          */
eventMatch(byte discoverySessionId, int peerId, byte[] addr, byte[] serviceSpecificInfo, byte[] matchFilter, int rangingIndicationType, int rangingMeasurementInMm, byte[] scid, int peerCipherType, byte[] nonce, byte[] tag, AwarePairingConfig pairingConfig, @Nullable List<OuiKeyedData> vendorData)747         void eventMatch(byte discoverySessionId, int peerId, byte[] addr,
748                 byte[] serviceSpecificInfo, byte[] matchFilter, int rangingIndicationType,
749                 int rangingMeasurementInMm, byte[] scid, int peerCipherType, byte[] nonce,
750                 byte[] tag, AwarePairingConfig pairingConfig,
751                 @Nullable List<OuiKeyedData> vendorData);
752 
753         /**
754          * Indicates that a previously discovered match (service) has expired.
755          * @param discoverySessionId Discovery session ID of the expired match.
756          * @param peerId Peer ID of the expired match.
757          */
eventMatchExpired(byte discoverySessionId, int peerId)758         void eventMatchExpired(byte discoverySessionId, int peerId);
759 
760         /**
761          * Indicates that a followup message has been received from a peer.
762          * @param discoverySessionId Discovery session (publish or subscribe) ID of a previously
763          *                           created discovery session.
764          * @param peerId Unique ID of the peer.
765          * @param addr NAN Discovery (management) MAC address of the peer.
766          * @param serviceSpecificInfo Received message from the peer. There is no semantic
767          *                            meaning to these bytes. They are passed-through from sender
768          *                            to receiver as-is with no parsing.
769          */
eventFollowupReceived(byte discoverySessionId, int peerId, byte[] addr, byte[] serviceSpecificInfo)770         void eventFollowupReceived(byte discoverySessionId, int peerId, byte[] addr,
771                 byte[] serviceSpecificInfo);
772 
773         /**
774          * Provides status of a completed followup message transmit operation.
775          * @param id ID corresponding to the original request.
776          * @param status Status the operation (see {@link NanStatusCode}).
777          */
eventTransmitFollowup(short id, int status)778         void eventTransmitFollowup(short id, int status);
779 
780         /**
781          * Indicates that a data-path (NDP) setup has been requested by an initiator peer
782          * (received by the intended responder).
783          * @param discoverySessionId ID of an active publish or subscribe discovery session.
784          * @param peerDiscMacAddr MAC address of the Initiator peer. This is the MAC address of
785          *                        the peer's management/discovery NAN interface.
786          * @param ndpInstanceId ID of the data-path. Used to identify the data-path in further
787          *                      negotiation/APIs.
788          * @param appInfo Arbitrary information communicated from the peer as part of the
789          *                data-path setup process. There is no semantic meaning to these bytes.
790          *                They are passed from sender to receiver as-is with no parsing.
791          */
eventDataPathRequest(byte discoverySessionId, byte[] peerDiscMacAddr, int ndpInstanceId, byte[] appInfo)792         void eventDataPathRequest(byte discoverySessionId, byte[] peerDiscMacAddr,
793                 int ndpInstanceId, byte[] appInfo);
794 
795         /**
796          * Indicates that a data-path (NDP) setup has been completed. Received by both the
797          * Initiator and Responder.
798          * @param status Status the operation (see {@link NanStatusCode}).
799          * @param ndpInstanceId ID of the data-path.
800          * @param dataPathSetupSuccess Indicates whether the data-path setup succeeded (true)
801          *                             or failed (false).
802          * @param peerNdiMacAddr MAC address of the peer's data-interface (not its
803          *                       management/discovery interface).
804          * @param appInfo Arbitrary information communicated from the peer as part of the
805          *                data-path setup process. There is no semantic meaning to  these bytes.
806          *                They are passed from sender to receiver as-is with no parsing.
807          * @param channelInfos
808          */
eventDataPathConfirm(int status, int ndpInstanceId, boolean dataPathSetupSuccess, byte[] peerNdiMacAddr, byte[] appInfo, List<WifiAwareChannelInfo> channelInfos)809         void eventDataPathConfirm(int status, int ndpInstanceId, boolean dataPathSetupSuccess,
810                 byte[] peerNdiMacAddr, byte[] appInfo, List<WifiAwareChannelInfo> channelInfos);
811 
812         /**
813          * Indicates that a data-path (NDP) schedule has been updated (ex. channels
814          * have been changed).
815          * @param peerDiscoveryAddress Discovery address (NMI) of the peer to which the NDP
816          *                             is connected.
817          * @param ndpInstanceIds List of NDPs to which this update applies.
818          * @param channelInfo Updated channel(s) information.
819          */
eventDataPathScheduleUpdate(byte[] peerDiscoveryAddress, ArrayList<Integer> ndpInstanceIds, List<WifiAwareChannelInfo> channelInfo)820         void eventDataPathScheduleUpdate(byte[] peerDiscoveryAddress,
821                 ArrayList<Integer> ndpInstanceIds, List<WifiAwareChannelInfo> channelInfo);
822 
823         /**
824          * Indicates that a list of data-paths (NDP) have been terminated. Received by both the
825          * Initiator and Responder.
826          * @param ndpInstanceId Data-path ID of the terminated data-path.
827          */
eventDataPathTerminated(int ndpInstanceId)828         void eventDataPathTerminated(int ndpInstanceId);
829 
830         /**
831          * Indicates that the pairing request is from the peer device.
832          */
eventPairingRequest(int discoverySessionId, int peerId, byte[] peerDiscMacAddr, int ndpInstanceId, int requestType, boolean enableCache, byte[] nonce, byte[] tag)833         void eventPairingRequest(int discoverySessionId, int peerId, byte[] peerDiscMacAddr,
834                 int ndpInstanceId, int requestType, boolean enableCache, byte[] nonce, byte[] tag);
835 
836         /**
837          * Indicates that the pairing is finished
838          */
eventPairingConfirm(int pairingId, boolean accept, int reason, int requestType, boolean enableCache, PairingConfigManager.PairingSecurityAssociationInfo npksa)839         void eventPairingConfirm(int pairingId, boolean accept, int reason, int requestType,
840                 boolean enableCache,
841                 PairingConfigManager.PairingSecurityAssociationInfo npksa);
842 
843         /**
844          * Indicates that the bootstrapping request is from the peer device.
845          */
eventBootstrappingRequest(int discoverySessionId, int peerId, byte[] peerDiscMacAddr, int bootstrappingInstanceId, int method)846         void eventBootstrappingRequest(int discoverySessionId, int peerId, byte[] peerDiscMacAddr,
847                 int bootstrappingInstanceId, int method);
848 
849         /**
850          * Indicates that the bootstrapping is finished
851          */
eventBootstrappingConfirm(int pairingId, int responseCode, int reason, int comebackDelay, byte[] cookie)852         void eventBootstrappingConfirm(int pairingId, int responseCode, int reason,
853                 int comebackDelay, byte[] cookie);
854 
855         /**
856          * Indicates that the suspension mode has changed, i.e., the device has entered or exited
857          * the suspension mode
858          */
eventSuspensionModeChanged(boolean isSuspended)859         void eventSuspensionModeChanged(boolean isSuspended);
860     }
861 }
862