WnmData.java revision 5a1adfdef3025a595544b3d17e1d5d9afca7673b
1/*
2 * Copyright (C) 2016 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 com.android.server.wifi.hotspot2;
18
19import com.android.server.wifi.WifiMonitor;
20
21import java.io.IOException;
22
23/**
24 * This class carries the payload of a Hotspot 2.0 Wireless Network Management (WNM) frame,
25 * described in the Hotspot 2.0 spec, section 3.2.
26 */
27public class WnmData {
28    public static final int ESS = 1;   // HS2.0 spec section 3.2.1.2, table 4
29
30    private final long mBssid;
31    private final String mUrl;
32    private final boolean mDeauthEvent;
33    private final int mMethod;
34    private final boolean mEss;
35    private final int mDelay;
36
37    public static WnmData buildWnmData(String event) throws IOException {
38        // %012x HS20-SUBSCRIPTION-REMEDIATION "%u %s", osu_method, url
39        // %012x HS20-DEAUTH-IMMINENT-NOTICE "%u %u %s", code, reauth_delay, url
40
41        String[] segments = event.split(" ");
42        if (segments.length < 2) {
43            throw new IOException("Short event");
44        }
45
46        switch (segments[1]) {
47            case WifiMonitor.HS20_SUB_REM_STR: {
48                if (segments.length != 4) {
49                    throw new IOException("Expected 4 segments");
50                }
51                return new WnmData(Long.parseLong(segments[0], 16),
52                        segments[3],
53                        Integer.parseInt(segments[2]));
54            }
55            case WifiMonitor.HS20_DEAUTH_STR: {
56                if (segments.length != 5) {
57                    throw new IOException("Expected 5 segments");
58                }
59                int codeID = Integer.parseInt(segments[2]);
60                if (codeID < 0 || codeID > ESS) {
61                    throw new IOException("Unknown code");
62                }
63                return new WnmData(Long.parseLong(segments[0], 16),
64                        segments[4],
65                        codeID == ESS,
66                        Integer.parseInt(segments[3]));
67            }
68            default:
69                throw new IOException("Unknown event type");
70        }
71    }
72
73    public WnmData(long bssid, String url, int method) {
74        mBssid = bssid;
75        mUrl = url;
76        mMethod = method;
77        mEss = false;
78        mDelay = -1;
79        mDeauthEvent = false;
80    }
81
82    public WnmData(long bssid, String url, boolean ess, int delay) {
83        mBssid = bssid;
84        mUrl = url;
85        mEss = ess;
86        mDelay = delay;
87        mMethod = -1;
88        mDeauthEvent = true;
89    }
90
91    public long getBssid() {
92        return mBssid;
93    }
94
95    public String getUrl() {
96        return mUrl;
97    }
98
99    public boolean isDeauthEvent() {
100        return mDeauthEvent;
101    }
102
103    public int getMethod() {
104        return mMethod;
105    }
106
107    public boolean isEss() {
108        return mEss;
109    }
110
111    public int getDelay() {
112        return mDelay;
113    }
114}
115