From 2a1078dc3729bab248bbd28c8336749481e045cf Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Thu, 16 Mar 2017 15:07:32 -0700 Subject: SupplicantP2pIfaceHal: Support for P2P client list and save config Also, Moved mac address to long conversion methods to NativeUtil to help with unit tests. Bug: 36042785 Test: Added unit tests. Change-Id: I6c1a78abf8c0f8159195c2542dfe217a7b954df0 --- .../android/server/wifi/SupplicantP2pIfaceHal.java | 92 ++++++++++++++++++++++ .../android/server/wifi/SupplicantStaIfaceHal.java | 23 ++---- .../java/com/android/server/wifi/WifiNative.java | 26 +++--- .../com/android/server/wifi/util/NativeUtil.java | 26 ++++++ 4 files changed, 139 insertions(+), 28 deletions(-) (limited to 'service') diff --git a/service/java/com/android/server/wifi/SupplicantP2pIfaceHal.java b/service/java/com/android/server/wifi/SupplicantP2pIfaceHal.java index ba7064b4d..6a90f875f 100644 --- a/service/java/com/android/server/wifi/SupplicantP2pIfaceHal.java +++ b/service/java/com/android/server/wifi/SupplicantP2pIfaceHal.java @@ -45,9 +45,11 @@ import com.android.server.wifi.util.NativeUtil; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * Native calls sending requests to the P2P Hals, and callbacks for receiving P2P events @@ -1998,6 +2000,96 @@ public class SupplicantP2pIfaceHal { return result.isSuccess(); } } + + /** + * Set the client list for the provided network. + * + * @param networkId Id of the network. + * @param clientListStr Space separated list of clients. + * @return true, if operation was successful. + */ + public boolean setClientList(int networkId, String clientListStr) { + synchronized (mLock) { + if (!checkSupplicantP2pIfaceAndLogFailure("setClientList")) return false; + if (TextUtils.isEmpty(clientListStr)) { + Log.e(TAG, "Invalid client list"); + return false; + } + ISupplicantP2pNetwork network = getNetwork(networkId); + if (network == null) { + Log.e(TAG, "Invalid network id "); + return false; + } + SupplicantResult result = new SupplicantResult( + "setClientList(" + networkId + ", " + clientListStr + ")"); + ArrayList clients = new ArrayList<>(); + for (String clientStr : Arrays.asList(clientListStr.split("\\s+"))) { + clients.add(NativeUtil.macAddressToByteArray(clientStr)); + } + try { + result.setResult(network.setClientList(clients)); + } catch (RemoteException e) { + Log.e(TAG, "ISupplicantP2pIface exception: " + e); + supplicantServiceDiedHandler(); + } + return result.isSuccess(); + } + } + + /** + * Set the client list for the provided network. + * + * @param networkId Id of the network. + * @return Space separated list of clients if successfull, null otherwise. + */ + public String getClientList(int networkId) { + synchronized (mLock) { + if (!checkSupplicantP2pIfaceAndLogFailure("getClientList")) return null; + ISupplicantP2pNetwork network = getNetwork(networkId); + if (network == null) { + Log.e(TAG, "Invalid network id "); + return null; + } + SupplicantResult result = new SupplicantResult( + "getClientList(" + networkId + ")"); + try { + network.getClientList( + (SupplicantStatus status, ArrayList clients) -> { + result.setResult(status, clients); + }); + } catch (RemoteException e) { + Log.e(TAG, "ISupplicantP2pIface exception: " + e); + supplicantServiceDiedHandler(); + } + if (!result.isSuccess()) { + return null; + } + ArrayList clients = result.getResult(); + return clients.stream() + .map(NativeUtil::macAddressFromByteArray) + .collect(Collectors.joining(" ")); + } + } + + /** + * Persist the current configurations to disk. + * + * @return true, if operation was successful. + */ + public boolean saveConfig() { + synchronized (mLock) { + if (!checkSupplicantP2pIfaceAndLogFailure("saveConfig")) return false; + SupplicantResult result = new SupplicantResult("saveConfig()"); + try { + result.setResult(mISupplicantP2pIface.saveConfig()); + } catch (RemoteException e) { + Log.e(TAG, "ISupplicantP2pIface exception: " + e); + supplicantServiceDiedHandler(); + } + return result.isSuccess(); + } + } + /** Container class allowing propagation of status and/or value * from callbacks. * diff --git a/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java b/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java index 8f141e6a2..8308b4c0d 100644 --- a/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java +++ b/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java @@ -1732,18 +1732,6 @@ public class SupplicantStaIfaceHal { } } - /** - * Helper utility to convert the bssid bytes to long. - */ - private Long toLongBssid(byte[] bssidBytes) { - try { - return ByteBufferReader.readInteger( - ByteBuffer.wrap(bssidBytes), ByteOrder.BIG_ENDIAN, bssidBytes.length); - } catch (BufferUnderflowException | IllegalArgumentException e) { - return 0L; - } - } - @Override public void onNetworkAdded(int id) { logCallback("onNetworkAdded"); @@ -1794,7 +1782,7 @@ public class SupplicantStaIfaceHal { addAnqpElementToMap(elementsMap, HSConnCapability, hs20Data.connectionCapability); addAnqpElementToMap(elementsMap, HSOSUProviders, hs20Data.osuProvidersList); mWifiMonitor.broadcastAnqpDoneEvent( - mIfaceName, new AnqpEvent(toLongBssid(bssid), elementsMap)); + mIfaceName, new AnqpEvent(NativeUtil.macAddressToLong(bssid), elementsMap)); } } @@ -1805,7 +1793,7 @@ public class SupplicantStaIfaceHal { synchronized (mLock) { mWifiMonitor.broadcastIconDoneEvent( mIfaceName, - new IconEvent(toLongBssid(bssid), fileName, data.size(), + new IconEvent(NativeUtil.macAddressToLong(bssid), fileName, data.size(), NativeUtil.byteArrayFromArrayList(data))); } } @@ -1815,7 +1803,8 @@ public class SupplicantStaIfaceHal { logCallback("onHs20SubscriptionRemediation"); synchronized (mLock) { mWifiMonitor.broadcastWnmEvent( - mIfaceName, new WnmData(toLongBssid(bssid), url, osuMethod)); + mIfaceName, + new WnmData(NativeUtil.macAddressToLong(bssid), url, osuMethod)); } } @@ -1826,8 +1815,8 @@ public class SupplicantStaIfaceHal { synchronized (mLock) { mWifiMonitor.broadcastWnmEvent( mIfaceName, - new WnmData(toLongBssid(bssid), url, reasonCode == WnmData.ESS, - reAuthDelayInSec)); + new WnmData(NativeUtil.macAddressToLong(bssid), url, + reasonCode == WnmData.ESS, reAuthDelayInSec)); } } diff --git a/service/java/com/android/server/wifi/WifiNative.java b/service/java/com/android/server/wifi/WifiNative.java index 87ace7839..fb8c0e612 100644 --- a/service/java/com/android/server/wifi/WifiNative.java +++ b/service/java/com/android/server/wifi/WifiNative.java @@ -1276,29 +1276,33 @@ public class WifiNative { } /** - * Get P2P client list for the given network ID. - * @return true on success, false otherwise. + * Set the client list for the provided network. + * + * @param netId Id of the network. + * @return Space separated list of clients if successfull, null otherwise. */ public String getP2pClientList(int netId) { - // TODO(b/36042785): Add HIDL method. - return null; + return mSupplicantP2pIfaceHal.getClientList(netId); } /** - * Set P2P client list for the given network ID. - * @return true on success, false otherwise. + * Set the client list for the provided network. + * + * @param netId Id of the network. + * @param list Space separated list of clients. + * @return true, if operation was successful. */ public boolean setP2pClientList(int netId, String list) { - // TODO(b/36042785): Add HIDL method. - return false; + return mSupplicantP2pIfaceHal.setClientList(netId, list); } /** - * Save the current configuration to wpa_supplicant.conf. + * Save the current configuration to p2p_supplicant.conf. + * + * @return true on success, false otherwise. */ public boolean saveConfig() { - // TODO(b/36042785): Add HIDL method. - return false; + return mSupplicantP2pIfaceHal.saveConfig(); } /******************************************************** diff --git a/service/java/com/android/server/wifi/util/NativeUtil.java b/service/java/com/android/server/wifi/util/NativeUtil.java index 50f32fa4d..a13b682a0 100644 --- a/service/java/com/android/server/wifi/util/NativeUtil.java +++ b/service/java/com/android/server/wifi/util/NativeUtil.java @@ -18,9 +18,13 @@ package com.android.server.wifi.util; import android.text.TextUtils; +import com.android.server.wifi.ByteBufferReader; + import libcore.util.HexEncoding; +import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.CharBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.CharsetDecoder; @@ -155,6 +159,28 @@ public class NativeUtil { return HexEncoding.decode(cleanMac.toCharArray(), false); } + /** + * Converts an array of 6 bytes to a long representing the MAC address. + * + * @param macArray byte array of mac values, must have length 6 + * @return Long value of the mac address. + * @throws IllegalArgumentException for malformed inputs. + */ + public static Long macAddressToLong(byte[] macArray) { + if (macArray == null) { + throw new IllegalArgumentException("null mac bytes"); + } + if (macArray.length != MAC_LENGTH) { + throw new IllegalArgumentException("invalid macArray length: " + macArray.length); + } + try { + return ByteBufferReader.readInteger( + ByteBuffer.wrap(macArray), ByteOrder.BIG_ENDIAN, macArray.length); + } catch (BufferUnderflowException | IllegalArgumentException e) { + throw new IllegalArgumentException("invalid macArray"); + } + } + /** * Remove enclosed quotes of the provided string. * -- cgit v1.2.3