1 /*
2  * Copyright (C) 2012 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 android.net.wifi.p2p.nsd;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 /**
26  * A class for storing service information that is advertised
27  * over a Wi-Fi peer-to-peer setup
28  *
29  * @see WifiP2pUpnpServiceInfo
30  * @see WifiP2pDnsSdServiceInfo
31  */
32 public class WifiP2pServiceInfo implements Parcelable {
33 
34     /**
35      * All service protocol types.
36      */
37     public static final int SERVICE_TYPE_ALL             = 0;
38 
39     /**
40      * DNS based service discovery protocol.
41      */
42     public static final int SERVICE_TYPE_BONJOUR         = 1;
43 
44     /**
45      * UPnP protocol.
46      */
47     public static final int SERVICE_TYPE_UPNP            = 2;
48 
49     /**
50      * WS-Discovery protocol
51      * @hide
52      */
53     public static final int SERVICE_TYPE_WS_DISCOVERY    = 3;
54 
55     /**
56      * Vendor Specific protocol
57      */
58     public static final int SERVICE_TYPE_VENDOR_SPECIFIC = 255;
59 
60     /**
61      * the list of query string for wpa_supplicant
62      *
63      * e.g)
64      * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
65      * {"bonjour", "045f697070c00c000c01", "094d795072696e746572c027"
66      *
67      * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
68      * {"bonjour", "096d797072696e746572045f697070c00c001001",
69      *  "09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074"}
70      *
71      * [UPnP]
72      * # UPnP uuid
73      * {"upnp", "10", "uuid:6859dede-8574-59ab-9332-123456789012"}
74      *
75      * # UPnP rootdevice
76      * {"upnp", "10", "uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice"}
77      *
78      * # UPnP device
79      * {"upnp", "10", "uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
80      * -org:device:InternetGatewayDevice:1"}
81      *
82      *  # UPnP service
83      * {"upnp", "10", "uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
84      * -org:service:ContentDirectory:2"}
85      */
86     private List<String> mQueryList;
87 
88     /**
89      * This is only used in subclass.
90      *
91      * @param queryList query string for wpa_supplicant
92      * @hide
93      */
WifiP2pServiceInfo(List<String> queryList)94     protected WifiP2pServiceInfo(List<String> queryList) {
95         if (queryList == null) {
96             throw new IllegalArgumentException("query list cannot be null");
97         }
98         mQueryList = queryList;
99     }
100 
101    /**
102     * Return the list of the query string for wpa_supplicant.
103     *
104     * @return the list of the query string for wpa_supplicant.
105     * @hide
106     */
getSupplicantQueryList()107    public List<String> getSupplicantQueryList() {
108        return mQueryList;
109    }
110 
111    /**
112     * Converts byte array to hex string.
113     *
114     * @param data
115     * @return hex string.
116     * @hide
117     */
bin2HexStr(byte[] data)118    static String bin2HexStr(byte[] data) {
119        StringBuffer sb = new StringBuffer();
120 
121        for (byte b: data) {
122            String s = null;
123            try {
124                s = Integer.toHexString(b & 0xff);
125            } catch (Exception e) {
126                e.printStackTrace();
127                return null;
128            }
129            //add 0 padding
130            if (s.length() == 1) {
131                sb.append('0');
132            }
133            sb.append(s);
134        }
135        return sb.toString();
136    }
137 
138    @Override
equals(Object o)139    public boolean equals(Object o) {
140        if (o == this) {
141            return true;
142        }
143        if (!(o instanceof WifiP2pServiceInfo)) {
144            return false;
145        }
146 
147        WifiP2pServiceInfo servInfo = (WifiP2pServiceInfo)o;
148        return  mQueryList.equals(servInfo.mQueryList);
149    }
150 
151    @Override
hashCode()152    public int hashCode() {
153        int result = 17;
154        result = 31 * result + (mQueryList == null ? 0 : mQueryList.hashCode());
155        return result;
156    }
157 
158     /** Implement the Parcelable interface {@hide} */
describeContents()159     public int describeContents() {
160         return 0;
161     }
162 
163     /** Implement the Parcelable interface {@hide} */
writeToParcel(Parcel dest, int flags)164     public void writeToParcel(Parcel dest, int flags) {
165         dest.writeStringList(mQueryList);
166     }
167 
168     /** Implement the Parcelable interface {@hide} */
169     public static final Creator<WifiP2pServiceInfo> CREATOR =
170         new Creator<WifiP2pServiceInfo>() {
171             public WifiP2pServiceInfo createFromParcel(Parcel in) {
172 
173                 List<String> data = new ArrayList<String>();
174                 in.readStringList(data);
175                 return new WifiP2pServiceInfo(data);
176             }
177 
178             public WifiP2pServiceInfo[] newArray(int size) {
179                 return new WifiP2pServiceInfo[size];
180             }
181         };
182 }
183