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

Commit ea5b16ac authored by Irfan Sheriff's avatar Irfan Sheriff
Browse files

Update p2p state machine and API

- Simplify the API with minimal needed functionality
- Fix responses for all async messages from the framework
- Fix state machine handling of connection setup and supplicant communication

Change-Id: I2724c83760b2aaa2068f9cd81ca0754753f83220
parent 0806d518
Loading
Loading
Loading
Loading
+20 −7
Original line number Diff line number Diff line
@@ -243,7 +243,7 @@ public class WifiNative {

    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
    public static String p2pConnect(WifiP2pConfig config) {
    public static String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
        if (config == null) return null;
        List<String> args = new ArrayList<String>();
        WpsConfiguration wpsConfig = config.wpsConfig;
@@ -269,15 +269,15 @@ public class WifiNative {
                break;
        }

        if (config.isPersistent) args.add("persistent");
        if (config.joinExistingGroup) args.add("join");
        /* Persist unless there is an explicit request to not do so*/
        if (config.persist != WifiP2pConfig.Persist.NO) args.add("persistent");
        if (joinExistingGroup) args.add("join");

        int groupOwnerIntent = config.groupOwnerIntent;
        if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
            groupOwnerIntent = 3; //default value
        }
        args.add("go_intent=" + groupOwnerIntent);
        if (config.channel > 0) args.add("freq=" + config.channel);

        String command = "P2P_CONNECT ";
        for (String s : args) command += s + " ";
@@ -300,10 +300,23 @@ public class WifiNative {

    /* Invite a peer to a group */
    public static boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
        if (group == null || deviceAddress == null) return false;
        if (deviceAddress == null) return false;

        if (group == null) {
            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
        } else {
            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
        }
    }

    /* Reinvoke a persistent connection */
    public static boolean p2pReinvoke(int netId, String deviceAddress) {
        if (deviceAddress == null || netId < 0) return false;

        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
    }


    public static String p2pGetInterfaceAddress(String deviceAddress) {
        if (deviceAddress == null) return null;
+11 −15
Original line number Diff line number Diff line
@@ -51,14 +51,16 @@ public class WifiP2pConfig implements Parcelable {
     */
    public int groupOwnerIntent = -1;

    public boolean isPersistent;

    public boolean joinExistingGroup;

    /**
     * Channel frequency in MHz
     * Indicates whether the configuration is saved
     */
    public int channel;
    public enum Persist {
        SYSTEM_DEFAULT,
        YES,
        NO
    }

    public Persist persist = Persist.SYSTEM_DEFAULT;

    public WifiP2pConfig() {
        //set defaults
@@ -112,9 +114,7 @@ public class WifiP2pConfig implements Parcelable {
        sbuf.append("\n address: ").append(deviceAddress);
        sbuf.append("\n wps: ").append(wpsConfig);
        sbuf.append("\n groupOwnerIntent: ").append(groupOwnerIntent);
        sbuf.append("\n isPersistent: ").append(isPersistent);
        sbuf.append("\n joinExistingGroup: ").append(joinExistingGroup);
        sbuf.append("\n channel: ").append(channel);
        sbuf.append("\n persist: ").append(persist.toString());
        return sbuf.toString();
    }

@@ -136,9 +136,7 @@ public class WifiP2pConfig implements Parcelable {
        dest.writeString(deviceAddress);
        dest.writeParcelable(wpsConfig, flags);
        dest.writeInt(groupOwnerIntent);
        dest.writeInt(isPersistent ? 1 : 0);
        dest.writeInt(joinExistingGroup ? 1 : 0);
        dest.writeInt(channel);
        dest.writeString(persist.name());
    }

    /** Implement the Parcelable interface {@hide} */
@@ -150,9 +148,7 @@ public class WifiP2pConfig implements Parcelable {
                config.deviceAddress = in.readString();
                config.wpsConfig = (WpsConfiguration) in.readParcelable(null);
                config.groupOwnerIntent = in.readInt();
                config.isPersistent = (in.readInt() == 1);
                config.joinExistingGroup = (in.readInt() == 1);
                config.channel = in.readInt();
                config.persist = Persist.valueOf(in.readString());
                return config;
            }

+3 −1
Original line number Diff line number Diff line
@@ -51,8 +51,10 @@ public class WifiP2pDeviceList implements Parcelable {
        }
    }

    public void clear() {
    public boolean clear() {
        if (mDevices.isEmpty()) return false;
        mDevices.clear();
        return true;
    }

    public void add(WifiP2pDevice device) {
+1 −1
Original line number Diff line number Diff line
@@ -16,4 +16,4 @@

package android.net.wifi.p2p;

parcelable WifiP2pStatus;
parcelable WifiP2pInfo;
+94 −0
Original line number Diff line number Diff line
@@ -19,34 +19,29 @@ package android.net.wifi.p2p;
import android.os.Parcelable;
import android.os.Parcel;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * A class representing Wi-fi P2p status
 * A class representing connection info on Wi-fi P2p
 * @hide
 */
public class WifiP2pStatus implements Parcelable {
public class WifiP2pInfo implements Parcelable {

    //Comes from the wpa_supplicant
    enum p2p_status_code {
        SUCCESS,
        FAIL_INFO_CURRENTLY_UNAVAILABLE,
        FAIL_INCOMPATIBLE_PARAMS,
        FAIL_LIMIT_REACHED,
        FAIL_INVALID_PARAMS,
        FAIL_UNABLE_TO_ACCOMMODATE,
        FAIL_PREV_PROTOCOL_ERROR,
        FAIL_NO_COMMON_CHANNELS,
        FAIL_UNKNOWN_GROUP,
        FAIL_BOTH_GO_INTENT_15,
        FAIL_INCOMPATIBLE_PROV_METHOD,
        FAIL_REJECTED_BY_USER
    };
    public boolean groupFormed;

    public boolean isGroupOwner;

    public InetAddress groupOwnerAddress;

    public WifiP2pStatus() {
    public WifiP2pInfo() {
    }

    //TODO: add support
    public String toString() {
        StringBuffer sbuf = new StringBuffer();
        sbuf.append("groupFormed: ").append(groupFormed)
            .append("isGroupOwner: ").append(isGroupOwner)
            .append("groupOwnerAddress: ").append(groupOwnerAddress);
        return sbuf.toString();
    }

@@ -56,27 +51,44 @@ public class WifiP2pStatus implements Parcelable {
    }

    /** copy constructor {@hide} */
    //TODO: implement
    public WifiP2pStatus(WifiP2pStatus source) {
    public WifiP2pInfo(WifiP2pInfo source) {
        if (source != null) {
            groupFormed = source.groupFormed;
            isGroupOwner = source.isGroupOwner;
            groupOwnerAddress = source.groupOwnerAddress;
       }
    }

    /** Implement the Parcelable interface {@hide} */
    // STOPSHIP: implement
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(groupFormed ? (byte)1 : (byte)0);
        dest.writeByte(isGroupOwner ? (byte)1 : (byte)0);

        if (groupOwnerAddress != null) {
            dest.writeByte((byte)1);
            dest.writeByteArray(groupOwnerAddress.getAddress());
        } else {
            dest.writeByte((byte)0);
        }
    }

    /** Implement the Parcelable interface {@hide} */
    public static final Creator<WifiP2pStatus> CREATOR =
        new Creator<WifiP2pStatus>() {
            public WifiP2pStatus createFromParcel(Parcel in) {
                WifiP2pStatus status = new WifiP2pStatus();
                return status;
    public static final Creator<WifiP2pInfo> CREATOR =
        new Creator<WifiP2pInfo>() {
            public WifiP2pInfo createFromParcel(Parcel in) {
                WifiP2pInfo info = new WifiP2pInfo();
                info.groupFormed = (in.readByte() == 1);
                info.isGroupOwner = (in.readByte() == 1);
                if (in.readByte() == 1) {
                    try {
                        info.groupOwnerAddress = InetAddress.getByAddress(in.createByteArray());
                    } catch (UnknownHostException e) {}
                }
                return info;
            }

            public WifiP2pStatus[] newArray(int size) {
                return new WifiP2pStatus[size];
            public WifiP2pInfo[] newArray(int size) {
                return new WifiP2pInfo[size];
            }
        };
}
Loading