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
17package android.net.wifi.p2p.nsd;
18
19import java.util.ArrayList;
20import java.util.List;
21import java.util.UUID;
22
23/**
24 * A class for storing Upnp service information that is advertised
25 * over a Wi-Fi peer-to-peer setup.
26 *
27 * {@see android.net.wifi.p2p.WifiP2pManager#addLocalService}
28 * {@see android.net.wifi.p2p.WifiP2pManager#removeLocalService}
29 * {@see WifiP2pServiceInfo}
30 * {@see WifiP2pDnsSdServiceInfo}
31 */
32public class WifiP2pUpnpServiceInfo extends WifiP2pServiceInfo {
33
34    /**
35     * UPnP version 1.0.
36     *
37     * <pre>Query Version should always be set to 0x10 if the query values are
38     * compatible with UPnP Device Architecture 1.0.
39     * @hide
40     */
41    public static final int VERSION_1_0 = 0x10;
42
43    /**
44     * This constructor is only used in newInstance().
45     *
46     * @param queryList
47     */
48    private WifiP2pUpnpServiceInfo(List<String> queryList) {
49        super(queryList);
50    }
51
52    /**
53     * Create UPnP service information object.
54     *
55     * @param uuid a string representation of this UUID in the following format,
56     *  as per <a href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a>.<br>
57     *  e.g) 6859dede-8574-59ab-9332-123456789012
58     * @param device a string representation of this device in the following format,
59     * as per
60     * <a href="http://www.upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf">
61     *  UPnP Device Architecture1.1</a><br>
62     *  e.g) urn:schemas-upnp-org:device:MediaServer:1
63     * @param services a string representation of this service in the following format,
64     * as per
65     * <a href="http://www.upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf">
66     *  UPnP Device Architecture1.1</a><br>
67     *  e.g) urn:schemas-upnp-org:service:ContentDirectory:1
68     * @return UPnP service information object.
69     */
70    public static WifiP2pUpnpServiceInfo newInstance(String uuid,
71            String device, List<String> services) {
72        if (uuid == null || device == null) {
73            throw new IllegalArgumentException("uuid or device cannnot be null");
74        }
75        UUID.fromString(uuid);
76
77        ArrayList<String> info = new ArrayList<String>();
78
79        info.add(createSupplicantQuery(uuid, null));
80        info.add(createSupplicantQuery(uuid, "upnp:rootdevice"));
81        info.add(createSupplicantQuery(uuid, device));
82        if (services != null) {
83            for (String service:services) {
84                info.add(createSupplicantQuery(uuid, service));
85            }
86        }
87
88        return new WifiP2pUpnpServiceInfo(info);
89    }
90
91    /**
92     * Create wpa_supplicant service query for upnp.
93     *
94     * @param uuid
95     * @param data
96     * @return wpa_supplicant service query for upnp
97     */
98    private static String createSupplicantQuery(String uuid, String data) {
99        StringBuffer sb = new StringBuffer();
100        sb.append("upnp ");
101        sb.append(String.format("%02x ", VERSION_1_0));
102        sb.append("uuid:");
103        sb.append(uuid);
104        if (data != null) {
105            sb.append("::");
106            sb.append(data);
107        }
108        return sb.toString();
109    }
110}
111