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