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.net.wifi.p2p.WifiP2pDevice;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /**
25  * A class for a response of upnp service discovery.
26  *
27  * @hide
28  */
29 public class WifiP2pUpnpServiceResponse extends WifiP2pServiceResponse {
30 
31     /**
32      * UPnP version. should be {@link WifiP2pUpnpServiceInfo#VERSION_1_0}
33      */
34     private int mVersion;
35 
36     /**
37      * The list of Unique Service Name.
38      * e.g)
39      *{"uuid:6859dede-8574-59ab-9332-123456789012",
40      *"uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice"}
41      */
42     private List<String> mUniqueServiceNames;
43 
44     /**
45      * Return UPnP version number.
46      *
47      * @return version number.
48      * @see WifiP2pUpnpServiceInfo#VERSION_1_0
49      */
getVersion()50     public int getVersion() {
51         return mVersion;
52     }
53 
54     /**
55      * Return Unique Service Name strings.
56      *
57      * @return Unique Service Name.<br>
58      * e.g ) <br>
59      * <ul>
60      * <li>"uuid:6859dede-8574-59ab-9332-123456789012"
61      * <li>"uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice"
62      * <li>"uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:device:
63      * MediaServer:2"
64      * <li>"uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:service:
65      * ContentDirectory:2"
66      * </ul>
67      */
getUniqueServiceNames()68     public List<String> getUniqueServiceNames() {
69         return mUniqueServiceNames;
70     }
71 
72     /**
73      * hidden constructor.
74      *
75      * @param status status code
76      * @param transId transaction id
77      * @param dev source device
78      * @param data UPnP response data.
79      */
WifiP2pUpnpServiceResponse(int status, int transId, WifiP2pDevice dev, byte[] data)80     protected WifiP2pUpnpServiceResponse(int status,
81             int transId, WifiP2pDevice dev, byte[] data) {
82         super(WifiP2pServiceInfo.SERVICE_TYPE_UPNP,
83                 status, transId, dev, data);
84         if (!parse()) {
85             throw new IllegalArgumentException("Malformed upnp service response");
86         }
87     }
88 
89     /**
90      * Parse UPnP service discovery response
91      *
92      * @return {@code true} if the operation succeeded
93      */
parse()94     private boolean parse() {
95         /*
96          * The data format is as follows.
97          *
98          * ______________________________________________________
99          * |  Version (1)  |          USN (Variable)            |
100          */
101         if (mData == null) {
102             // the empty is OK.
103             return true;
104         }
105 
106         if (mData.length < 1) {
107             return false;
108         }
109 
110         mVersion = mData[0] & 0xff;
111         String[] names = new String(mData, 1, mData.length-1).split(",");
112         mUniqueServiceNames = new ArrayList<String>();
113         for (String name : names) {
114             mUniqueServiceNames.add(name);
115         }
116         return true;
117     }
118 
119     @Override
toString()120     public String toString() {
121         StringBuffer sbuf = new StringBuffer();
122         sbuf.append("serviceType:UPnP(").append(mServiceType).append(")");
123         sbuf.append(" status:").append(Status.toString(mStatus));
124         sbuf.append(" srcAddr:").append(mDevice.deviceAddress);
125         sbuf.append(" version:").append(String.format("%02x", mVersion));
126         if (mUniqueServiceNames != null) {
127             for (String name : mUniqueServiceNames) {
128                 sbuf.append(" usn:").append(name);
129             }
130         }
131         return sbuf.toString();
132     }
133 
134     /**
135      * Create upnp service response.
136      *
137      * <pre>This is only used in{@link WifiP2pServiceResponse}
138      *
139      * @param status status code.
140      * @param transId transaction id.
141      * @param device source device.
142      * @param data UPnP response data.
143      * @return UPnP service response data.
144      * @hide
145      */
newInstance(int status, int transId, WifiP2pDevice device, byte[] data)146     static WifiP2pUpnpServiceResponse newInstance(int status,
147             int transId, WifiP2pDevice device, byte[] data) {
148         if (status != WifiP2pServiceResponse.Status.SUCCESS) {
149             return new WifiP2pUpnpServiceResponse(status, transId, device, null);
150         }
151 
152         try {
153             return new WifiP2pUpnpServiceResponse(status, transId, device, data);
154         } catch (IllegalArgumentException e) {
155             e.printStackTrace();
156         }
157         return null;
158     }
159 }
160