Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 491906a7 authored by Irfan Sheriff's avatar Irfan Sheriff Committed by Android (Google) Code Review
Browse files

Merge "Move ARP test functionality to ArpPeer"

parents 4170c374 55b9821d
Loading
Loading
Loading
Loading
+41 −1
Original line number Diff line number Diff line
@@ -16,8 +16,12 @@

package android.net.arp;

import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.RouteInfo;
import android.os.SystemClock;
import android.util.Log;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Inet6Address;
@@ -35,6 +39,8 @@ import libcore.net.RawSocket;
 * @hide
 */
public class ArpPeer {
    private static final boolean DBG = false;
    private static final String TAG = "ArpPeer";
    private String mInterfaceName;
    private final InetAddress mMyAddr;
    private final byte[] mMyMac = new byte[6];
@@ -46,7 +52,6 @@ public class ArpPeer {
    private static final int ARP_LENGTH = 28;
    private static final int MAC_ADDR_LENGTH = 6;
    private static final int IPV4_LENGTH = 4;
    private static final String TAG = "ArpPeer";

    public ArpPeer(String interfaceName, InetAddress myAddr, String mac,
                   InetAddress peer) throws SocketException {
@@ -123,6 +128,41 @@ public class ArpPeer {
        return null;
    }

    public static boolean doArp(String myMacAddress, LinkProperties linkProperties,
            int timeoutMillis, int numArpPings, int minArpResponses) {
        String interfaceName = linkProperties.getInterfaceName();
        InetAddress inetAddress = null;
        InetAddress gateway = null;
        boolean success;

        for (LinkAddress la : linkProperties.getLinkAddresses()) {
            inetAddress = la.getAddress();
            break;
        }

        for (RouteInfo route : linkProperties.getRoutes()) {
            gateway = route.getGateway();
            break;
        }

        try {
            ArpPeer peer = new ArpPeer(interfaceName, inetAddress, myMacAddress, gateway);
            int responses = 0;
            for (int i=0; i < numArpPings; i++) {
                if(peer.doArp(timeoutMillis) != null) responses++;
            }
            if (DBG) Log.d(TAG, "ARP test result: " + responses + "/" + numArpPings);
            success = (responses >= minArpResponses);
            peer.close();
        } catch (SocketException se) {
            //Consider an Arp socket creation issue as a successful Arp
            //test to avoid any wifi connectivity issues
            Log.e(TAG, "ARP test initiation failure: " + se);
            success = true;
        }
        return success;
    }

    public void close() {
        try {
            mSocket.close();
+3 −48
Original line number Diff line number Diff line
@@ -170,9 +170,6 @@ public class WifiWatchdogStateMachine extends StateMachine {
    static final int RSSI_FETCH_SUCCEEDED                           = BASE + 24;
    static final int RSSI_FETCH_FAILED                              = BASE + 25;

    private static final int SINGLE_ARP_CHECK = 0;
    private static final int FULL_ARP_CHECK   = 1;

    private Context mContext;
    private ContentResolver mContentResolver;
    private WifiManager mWifiManager;
@@ -651,7 +648,9 @@ public class WifiWatchdogStateMachine extends StateMachine {
                    break;
                case CMD_ARP_CHECK:
                    if (msg.arg1 == mArpToken) {
                        if (doArpTest(FULL_ARP_CHECK) == true) {
                        boolean success = ArpPeer.doArp(mWifiInfo.getMacAddress(), mLinkProperties,
                                mArpPingTimeoutMs, mNumArpPings, mMinArpResponses);
                        if (success) {
                            if (DBG) log("Notify link is good " + mCurrentSignalLevel);
                            mWsmChannel.sendMessage(GOOD_LINK_DETECTED);
                        } else {
@@ -842,50 +841,6 @@ public class WifiWatchdogStateMachine extends StateMachine {
        return true;
    }

    private boolean doArpTest(int type) {
        boolean success;

        String iface = mLinkProperties.getInterfaceName();
        String mac = mWifiInfo.getMacAddress();
        InetAddress inetAddress = null;
        InetAddress gateway = null;

        for (LinkAddress la : mLinkProperties.getLinkAddresses()) {
            inetAddress = la.getAddress();
            break;
        }

        for (RouteInfo route : mLinkProperties.getRoutes()) {
            gateway = route.getGateway();
            break;
        }

        if (DBG) log("ARP " + iface + "addr: " + inetAddress + "mac: " + mac + "gw: " + gateway);

        try {
            ArpPeer peer = new ArpPeer(iface, inetAddress, mac, gateway);
            if (type == SINGLE_ARP_CHECK) {
                success = (peer.doArp(mArpPingTimeoutMs) != null);
                if (DBG) log("single ARP test result: " + success);
            } else {
                int responses = 0;
                for (int i=0; i < mNumArpPings; i++) {
                    if(peer.doArp(mArpPingTimeoutMs) != null) responses++;
                }
                if (DBG) log("full ARP test result: " + responses + "/" + mNumArpPings);
                success = (responses >= mMinArpResponses);
            }
            peer.close();
        } catch (SocketException se) {
            //Consider an Arp socket creation issue as a successful Arp
            //test to avoid any wifi connectivity issues
            loge("ARP test initiation failure: " + se);
            success = true;
        }

        return success;
    }

    private int calculateSignalLevel(int rssi) {
        int signalLevel = WifiManager.calculateSignalLevel(rssi,
                WifiManager.RSSI_LEVELS);