summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/WnmData.java
blob: 861f6dd24d95851f2588de9a7468582df2b03b65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wifi;

import java.io.IOException;

/**
 * This class carries the payload of a Hotspot 2.0 Wireless Network Management (WNM) frame,
 * described in the Hotspot 2.0 spec, section 3.2.
 */
public class WnmData {
    private static final int ESS = 1;   // HS2.0 spec section 3.2.1.2, table 4

    private final long mBssid;
    private final String mUrl;
    private final boolean mDeauthEvent;
    private final int mMethod;
    private final boolean mEss;
    private final int mDelay;

    public static WnmData buildWnmData(String event) throws IOException {
        // %012x HS20-SUBSCRIPTION-REMEDIATION "%u %s", osu_method, url
        // %012x HS20-DEAUTH-IMMINENT-NOTICE "%u %u %s", code, reauth_delay, url

        String[] segments = event.split(" ");
        if (segments.length < 2) {
            throw new IOException("Short event");
        }

        switch (segments[1]) {
            case WifiMonitor.HS20_SUB_REM_STR: {
                if (segments.length != 4) {
                    throw new IOException("Expected 4 segments");
                }
                return new WnmData(Long.parseLong(segments[0], 16),
                        segments[3],
                        Integer.parseInt(segments[2]));
            }
            case WifiMonitor.HS20_DEAUTH_STR: {
                if (segments.length != 5) {
                    throw new IOException("Expected 5 segments");
                }
                int codeID = Integer.parseInt(segments[2]);
                if (codeID < 0 || codeID > ESS) {
                    throw new IOException("Unknown code");
                }
                return new WnmData(Long.parseLong(segments[0], 16),
                        segments[4],
                        codeID == ESS,
                        Integer.parseInt(segments[3]));
            }
            default:
                throw new IOException("Unknown event type");
        }
    }

    private WnmData(long bssid, String url, int method) {
        mBssid = bssid;
        mUrl = url;
        mMethod = method;
        mEss = false;
        mDelay = -1;
        mDeauthEvent = false;
    }

    private WnmData(long bssid, String url, boolean ess, int delay) {
        mBssid = bssid;
        mUrl = url;
        mEss = ess;
        mDelay = delay;
        mMethod = -1;
        mDeauthEvent = true;
    }

    public long getBssid() {
        return mBssid;
    }

    public String getUrl() {
        return mUrl;
    }

    public boolean isDeauthEvent() {
        return mDeauthEvent;
    }

    public int getMethod() {
        return mMethod;
    }

    public boolean isEss() {
        return mEss;
    }

    public int getDelay() {
        return mDelay;
    }
}