155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync/*
255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * Copyright (C) 2011 The Android Open Source Project
355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync *
455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * Licensed under the Apache License, Version 2.0 (the "License");
555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * you may not use this file except in compliance with the License.
655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * You may obtain a copy of the License at
755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync *
855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync *      http://www.apache.org/licenses/LICENSE-2.0
955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync *
1055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * Unless required by applicable law or agreed to in writing, software
1155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * distributed under the License is distributed on an "AS IS" BASIS,
1255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * See the License for the specific language governing permissions and
1455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * limitations under the License.
1555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync */
1655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
1755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo syncpackage android.net.wifi.p2p;
1855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
1955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo syncimport android.os.Parcelable;
2055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo syncimport android.os.Parcel;
2155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo syncimport android.util.Log;
2255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
2355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo syncimport java.util.regex.Pattern;
2455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
2555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync/**
2655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync * A class representing a Wi-Fi p2p device
276f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff *
286f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff * {@see WifiP2pManager}
2955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync */
3055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo syncpublic class WifiP2pDevice implements Parcelable {
3155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
3255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final String TAG = "WifiP2pDevice";
336f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff
3455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
356f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * The device name is a user friendly string to identify a Wi-Fi p2p device
3655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
3755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public String deviceName;
3855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
3955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
406f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * The device MAC address uniquely identifies a Wi-Fi p2p device
4155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
4255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public String deviceAddress;
4355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
4455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
4555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * interfaceAddress
4655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *
4755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * This address is used during group owner negotiation as the Intended
4855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * P2P Interface Address and the group interface will be created with
4955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * address as the local address in case of successfully completed
5055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * negotiation.
516f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * @hide
5255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
5355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public String interfaceAddress;
5455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
5555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
566f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * Primary device type identifies the type of device. For example, an application
576f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * could filter the devices discovered to only display printers if the purpose is to
586f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * enable a printing action from the user. See the Wi-Fi Direct technical specification
596f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * for the full list of standard device types supported.
6055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
6155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public String primaryDeviceType;
6255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
6355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
646f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * Secondary device type is an optional attribute that can be provided by a device in
656f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * addition to the primary device type.
6655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
6755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public String secondaryDeviceType;
6855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
6955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
7055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    // These definitions match the ones in wpa_supplicant
7155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /* WPS config methods supported */
7255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int WPS_CONFIG_DISPLAY         = 0x0008;
7355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int WPS_CONFIG_PUSHBUTTON      = 0x0080;
7455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int WPS_CONFIG_KEYPAD          = 0x0100;
7555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
7655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /* Device Capability bitmap */
7755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int DEVICE_CAPAB_SERVICE_DISCOVERY         = 1;
7855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int DEVICE_CAPAB_CLIENT_DISCOVERABILITY    = 1<<1;
7955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int DEVICE_CAPAB_CONCURRENT_OPER           = 1<<2;
8055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int DEVICE_CAPAB_INFRA_MANAGED             = 1<<3;
8155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int DEVICE_CAPAB_DEVICE_LIMIT              = 1<<4;
8255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int DEVICE_CAPAB_INVITATION_PROCEDURE      = 1<<5;
8355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
8455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /* Group Capability bitmap */
8555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int GROUP_CAPAB_GROUP_OWNER                = 1;
8655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int GROUP_CAPAB_PERSISTENT_GROUP           = 1<<1;
8755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int GROUP_CAPAB_GROUP_LIMIT                = 1<<2;
8855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int GROUP_CAPAB_INTRA_BSS_DIST             = 1<<3;
8955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int GROUP_CAPAB_CROSS_CONN                 = 1<<4;
9055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int GROUP_CAPAB_PERSISTENT_RECONN          = 1<<5;
9155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private static final int GROUP_CAPAB_GROUP_FORMATION            = 1<<6;
9255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
9355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
9455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * WPS config methods supported
956f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * @hide
9655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
9755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public int wpsConfigMethodsSupported;
9855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
9955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
10055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * Device capability
1016f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * @hide
10255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
10355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public int deviceCapability;
10455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
10555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
10655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * Group capability
1076f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * @hide
10855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
10955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public int groupCapability;
11055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
111651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    public static final int CONNECTED   = 0;
112651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    public static final int INVITED     = 1;
113651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    public static final int FAILED      = 2;
114651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    public static final int AVAILABLE   = 3;
115651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    public static final int UNAVAILABLE = 4;
11655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
117651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    /** Device connection status */
118651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    public int status = UNAVAILABLE;
11955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
120651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    public WifiP2pDevice() {
12155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
12255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
12355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    /**
12455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     * @param string formats supported include
12555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13
12655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  pri_dev_type=1-0050F204-1 name='p2p-TEST1' config_methods=0x188 dev_capab=0x27
12755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  group_capab=0x0
12855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *
12955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  P2P-DEVICE-LOST p2p_dev_addr=fa:7b:7a:42:02:13
13055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *
13155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  fa:7b:7a:42:02:13
13255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *
13355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  P2P-PROV-DISC-PBC-REQ 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27
13455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27
13555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  group_capab=0x0
13655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *
13755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  P2P-PROV-DISC-ENTER-PIN 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27
13855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27
13955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  group_capab=0x0
14055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *
14155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  P2P-PROV-DISC-SHOW-PIN 42:fc:89:e1:e2:27 44490607 p2p_dev_addr=42:fc:89:e1:e2:27
14255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27
14355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  group_capab=0x0
14455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *
14555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     *  Note: The events formats can be looked up in the wpa_supplicant code
1466f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff     * @hide
14755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync     */
14855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public WifiP2pDevice(String string) throws IllegalArgumentException {
14955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        String[] tokens = string.split(" ");
15055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
15155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (tokens.length < 1) {
15255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            throw new IllegalArgumentException("Malformed supplicant event");
15355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
15455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
15555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        /* Just a device address */
15655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (tokens.length == 1) {
15755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            deviceAddress = string;
15855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            return;
15955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
16055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
16155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        for (String token : tokens) {
16255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            String[] nameValue = token.split("=");
16355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            if (nameValue.length != 2) continue;
16455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
16555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            if (nameValue[0].equals("p2p_dev_addr")) {
16655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                deviceAddress = nameValue[1];
16755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                continue;
16855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
16955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
17055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            if (nameValue[0].equals("pri_dev_type")) {
17155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                primaryDeviceType = nameValue[1];
17255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                continue;
17355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
17455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
17555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            if (nameValue[0].equals("name")) {
17655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                deviceName = trimQuotes(nameValue[1]);
1778c57bcdbc6b360ee27d59b957a53e64c03257c9drepo sync                continue;
17855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
17955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
18055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            if (nameValue[0].equals("config_methods")) {
18155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                wpsConfigMethodsSupported = parseHex(nameValue[1]);
18255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                continue;
18355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
18455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
18555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            if (nameValue[0].equals("dev_capab")) {
18655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                deviceCapability = parseHex(nameValue[1]);
18755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                continue;
18855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
18955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
19055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            if (nameValue[0].equals("group_capab")) {
19155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                groupCapability = parseHex(nameValue[1]);
19255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                continue;
19355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
19455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
19555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
19655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (tokens[0].startsWith("P2P-DEVICE-FOUND")) {
197651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff            status = AVAILABLE;
19855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
19955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
20055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
2016f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    /** Returns true if WPS push button configuration is supported */
2026f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    public boolean wpsPbcSupported() {
2036f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff        return (wpsConfigMethodsSupported & WPS_CONFIG_PUSHBUTTON) != 0;
2046f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    }
2056f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff
2066f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    /** Returns true if WPS keypad configuration is supported */
2076f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    public boolean wpsKeypadSupported() {
2086f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff        return (wpsConfigMethodsSupported & WPS_CONFIG_KEYPAD) != 0;
2096f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    }
2106f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff
2116f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    /** Returns true if WPS display configuration is supported */
2126f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    public boolean wpsDisplaySupported() {
2136f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff        return (wpsConfigMethodsSupported & WPS_CONFIG_DISPLAY) != 0;
2146f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    }
2156f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff
2166f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    /** Returns true if the device is capable of service discovery */
2176f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    public boolean isServiceDiscoveryCapable() {
2186f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff        return (deviceCapability & DEVICE_CAPAB_SERVICE_DISCOVERY) != 0;
2196f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    }
2206f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff
2216f7d385d964949e507dcc9c88012372f48d0bce7Irfan Sheriff    /** Returns true if the device is a group owner */
22255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public boolean isGroupOwner() {
22355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        return (groupCapability & GROUP_CAPAB_GROUP_OWNER) != 0;
22455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
22555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
22655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    @Override
22755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public boolean equals(Object obj) {
22855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (this == obj) return true;
22955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (!(obj instanceof WifiP2pDevice)) return false;
23055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
23155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        WifiP2pDevice other = (WifiP2pDevice) obj;
23255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (other == null || other.deviceAddress == null) {
23355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            return (deviceAddress == null);
23455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
2358c57bcdbc6b360ee27d59b957a53e64c03257c9drepo sync        return other.deviceAddress.equals(deviceAddress);
23655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
23755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
23855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public String toString() {
23955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        StringBuffer sbuf = new StringBuffer();
24055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("Device: ").append(deviceName);
24155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n deviceAddress: ").append(deviceAddress);
24255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n interfaceAddress: ").append(interfaceAddress);
24355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n primary type: ").append(primaryDeviceType);
24455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n secondary type: ").append(secondaryDeviceType);
24555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n wps: ").append(wpsConfigMethodsSupported);
24655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n grpcapab: ").append(groupCapability);
24755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n devcapab: ").append(deviceCapability);
24855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        sbuf.append("\n status: ").append(status);
24955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        return sbuf.toString();
25055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
25155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
252651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    /** Implement the Parcelable interface */
25355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public int describeContents() {
25455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        return 0;
25555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
25655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
257651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    /** copy constructor */
25855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public WifiP2pDevice(WifiP2pDevice source) {
25955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (source != null) {
26055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            deviceName = source.deviceName;
26155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            deviceAddress = source.deviceAddress;
26255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            interfaceAddress = source.interfaceAddress;
26355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            primaryDeviceType = source.primaryDeviceType;
26455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            secondaryDeviceType = source.secondaryDeviceType;
26555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            wpsConfigMethodsSupported = source.wpsConfigMethodsSupported;
26655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            deviceCapability = source.deviceCapability;
26755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            groupCapability = source.groupCapability;
26855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            status = source.status;
26955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
27055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
27155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
272651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    /** Implement the Parcelable interface */
27355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public void writeToParcel(Parcel dest, int flags) {
27455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeString(deviceName);
27555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeString(deviceAddress);
27655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeString(interfaceAddress);
27755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeString(primaryDeviceType);
27855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeString(secondaryDeviceType);
27955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeInt(wpsConfigMethodsSupported);
28055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeInt(deviceCapability);
28155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        dest.writeInt(groupCapability);
282651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff        dest.writeInt(status);
28355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
28455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
285651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff    /** Implement the Parcelable interface */
28655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    public static final Creator<WifiP2pDevice> CREATOR =
28755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        new Creator<WifiP2pDevice>() {
28855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            public WifiP2pDevice createFromParcel(Parcel in) {
28955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                WifiP2pDevice device = new WifiP2pDevice();
29055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.deviceName = in.readString();
29155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.deviceAddress = in.readString();
29255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.interfaceAddress = in.readString();
29355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.primaryDeviceType = in.readString();
29455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.secondaryDeviceType = in.readString();
29555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.wpsConfigMethodsSupported = in.readInt();
29655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.deviceCapability = in.readInt();
29755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                device.groupCapability = in.readInt();
298651cdfcbac6245f570475991588ddc2d30265e8dIrfan Sheriff                device.status = in.readInt();
29955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                return device;
30055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
30155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
30255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            public WifiP2pDevice[] newArray(int size) {
30355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync                return new WifiP2pDevice[size];
30455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            }
30555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        };
30655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
30755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private String trimQuotes(String str) {
30855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        str = str.trim();
30955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (str.startsWith("'") && str.endsWith("'")) {
31055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            return str.substring(1, str.length()-1);
31155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
31255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        return str;
31355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
31455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
31555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    //supported formats: 0x1abc, 0X1abc, 1abc
31655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    private int parseHex(String hexString) {
31755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        int num = 0;
31855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        if (hexString.startsWith("0x") || hexString.startsWith("0X")) {
31955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            hexString = hexString.substring(2);
32055bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
32155bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync
32255bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        try {
32355bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            num = Integer.parseInt(hexString, 16);
32455bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        } catch(NumberFormatException e) {
32555bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync            Log.e(TAG, "Failed to parse hex string " + hexString);
32655bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        }
32755bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync        return num;
32855bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync    }
32955bc5f3e0408bcb5a39a6732de0b2d1aa99a55berepo sync}
330