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