1package com.android.hotspot2.osu;
2
3import com.android.hotspot2.omadm.OMAException;
4import com.android.hotspot2.omadm.XMLNode;
5import com.android.hotspot2.osu.commands.BrowserURI;
6import com.android.hotspot2.osu.commands.ClientCertInfo;
7import com.android.hotspot2.osu.commands.GetCertData;
8import com.android.hotspot2.osu.commands.MOData;
9import com.android.hotspot2.osu.commands.MOURN;
10import com.android.hotspot2.osu.commands.OSUCommandData;
11
12import java.util.HashMap;
13import java.util.Map;
14
15public class OSUCommand {
16    private final OSUCommandID mOSUCommand;
17    private final ExecCommand mExecCommand;
18    private final OSUCommandData mCommandData;
19
20    private static final Map<String, OSUCommandID> sCommands = new HashMap<>();
21    private static final Map<String, ExecCommand> sExecs = new HashMap<>();
22
23    static {
24        sCommands.put("exec", OSUCommandID.Exec);
25        sCommands.put("addmo", OSUCommandID.AddMO);
26        sCommands.put("updatenode", OSUCommandID.UpdateNode);      // Multi
27        sCommands.put("nomoupdate", OSUCommandID.NoMOUpdate);
28
29        sExecs.put("launchbrowsertouri", ExecCommand.Browser);
30        sExecs.put("getcertificate", ExecCommand.GetCert);
31        sExecs.put("useclientcerttls", ExecCommand.UseClientCertTLS);
32        sExecs.put("uploadmo", ExecCommand.UploadMO);
33    }
34
35    public OSUCommand(XMLNode child) throws OMAException {
36        mOSUCommand = sCommands.get(child.getStrippedTag());
37
38        switch (mOSUCommand) {
39            case Exec:
40                /*
41                 * Receipt of this element by a mobile device causes the following command
42                 * to be executed.
43                 */
44                child = child.getSoleChild();
45                mExecCommand = sExecs.get(child.getStrippedTag());
46                if (mExecCommand == null) {
47                    throw new OMAException("Unrecognized exec command: " + child.getStrippedTag());
48                }
49                switch (mExecCommand) {
50                    case Browser:
51                        /*
52                         * When the mobile device receives this command, it launches its default
53                         * browser to the URI contained in this element. The URI must use HTTPS as
54                         * the protocol and must contain an FQDN.
55                         */
56                        mCommandData = new BrowserURI(child);
57                        break;
58                    case GetCert:
59                        mCommandData = new GetCertData(child);
60                        break;
61                    case UploadMO:
62                        mCommandData = new MOURN(child);
63                        break;
64                    case UseClientCertTLS:
65                        /*
66                         * Command to mobile to re-negotiate the TLS connection using a client
67                         * certificate of the accepted type or Issuer to authenticate with the
68                         * Subscription server.
69                         */
70                        mCommandData = new ClientCertInfo(child);
71                        break;
72                    default:
73                        mCommandData = null;
74                        break;
75                }
76                break;
77            case AddMO:
78                /*
79                 * This command causes an management object in the mobile devices management tree
80                 * at the specified location to be added.
81                 * If there is already a management object at that location, the object is replaced.
82                 */
83                mExecCommand = null;
84                mCommandData = new MOData(child);
85                break;
86            case UpdateNode:
87                /*
88                 * This command causes the update of an interior node and its child nodes (if any)
89                 * at the location specified in the management tree URI attribute. The content of
90                 * this element is the MO node XML.
91                 */
92                mExecCommand = null;
93                mCommandData = new MOData(child);
94                break;
95            case NoMOUpdate:
96                /*
97                 * This response is used when there is no command to be executed nor update of
98                 * any MO required.
99                 */
100                mExecCommand = null;
101                mCommandData = null;
102                break;
103            default:
104                mExecCommand = null;
105                mCommandData = null;
106                break;
107        }
108    }
109
110    public OSUCommandID getOSUCommand() {
111        return mOSUCommand;
112    }
113
114    public ExecCommand getExecCommand() {
115        return mExecCommand;
116    }
117
118    public OSUCommandData getCommandData() {
119        return mCommandData;
120    }
121
122    @Override
123    public String toString() {
124        return "OSUCommand{" +
125                "OSUCommand=" + mOSUCommand +
126                ", execCommand=" + mExecCommand +
127                ", commandData=" + mCommandData +
128                '}';
129    }
130}
131