summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/ScanDetail.java
blob: 868a192fd1a69b524e5a5a17a9db2c9c55056b2c (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.android.server.wifi;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiSsid;

import com.android.server.wifi.anqp.ANQPElement;
import com.android.server.wifi.anqp.Constants;
import com.android.server.wifi.anqp.HSFriendlyNameElement;
import com.android.server.wifi.anqp.VenueNameElement;
import com.android.server.wifi.hotspot2.NetworkDetail;
import com.android.server.wifi.hotspot2.PasspointMatch;
import com.android.server.wifi.hotspot2.PasspointMatchInfo;
import com.android.server.wifi.hotspot2.Utils;
import com.android.server.wifi.hotspot2.pps.HomeSP;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ScanDetail {
    private final ScanResult mScanResult;
    private volatile NetworkDetail mNetworkDetail;
    private final Map<HomeSP, PasspointMatch> mMatches;
    private long mSeen = 0;

    public ScanDetail(NetworkDetail networkDetail, WifiSsid wifiSsid, String BSSID,
                      String caps, int level, int frequency, long tsf) {
        mNetworkDetail = networkDetail;
        mScanResult = new ScanResult(wifiSsid, BSSID, caps, level, frequency, tsf );
        mSeen = System.currentTimeMillis();
        //mScanResult.seen = mSeen;
        mScanResult.channelWidth = networkDetail.getChannelWidth();
        mScanResult.centerFreq0 = networkDetail.getCenterfreq0();
        mScanResult.centerFreq1 = networkDetail.getCenterfreq1();
        if (networkDetail.is80211McResponderSupport())
            mScanResult.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
        mMatches = null;
    }

    public ScanDetail(WifiSsid wifiSsid, String BSSID, String caps, int level, int frequency,
                      long tsf, long seen) {
        mNetworkDetail = null;
        mScanResult = new ScanResult(wifiSsid, BSSID, caps, level, frequency, tsf );
        mSeen = seen;
        //mScanResult.seen = mSeen;
        mScanResult.channelWidth = 0;
        mScanResult.centerFreq0 = 0;
        mScanResult.centerFreq1 = 0;
        mScanResult.flags = 0;
        mMatches = null;
    }

    private ScanDetail(ScanResult scanResult, NetworkDetail networkDetail,
                       Map<HomeSP, PasspointMatch> matches) {
        mScanResult = scanResult;
        mNetworkDetail = networkDetail;
        mMatches = matches;
    }

    public void updateResults(NetworkDetail networkDetail, int level, WifiSsid wssid, String ssid,
                              String flags, int freq, long tsf) {
        mScanResult.level = level;
        mScanResult.wifiSsid = wssid;
        // Keep existing API
        mScanResult.SSID = ssid;
        mScanResult.capabilities = flags;
        mScanResult.frequency = freq;
        mScanResult.timestamp = tsf;
        mSeen = System.currentTimeMillis();
        //mScanResult.seen = mSeen;
        mScanResult.channelWidth = networkDetail.getChannelWidth();
        mScanResult.centerFreq0 = networkDetail.getCenterfreq0();
        mScanResult.centerFreq1 = networkDetail.getCenterfreq1();
        if (networkDetail.is80211McResponderSupport())
            mScanResult.setFlag(ScanResult.FLAG_80211mc_RESPONDER);
        if (networkDetail.isInterworking())
            mScanResult.setFlag(ScanResult.FLAG_PASSPOINT_NETWORK);
    }

    public void propagateANQPInfo(Map<Constants.ANQPElementType, ANQPElement> anqpElements) {
        if (anqpElements.isEmpty()) {
            return;
        }
        mNetworkDetail = mNetworkDetail.complete(anqpElements);
        HSFriendlyNameElement fne = (HSFriendlyNameElement)anqpElements.get(
                Constants.ANQPElementType.HSFriendlyName);
        // !!! Match with language
        if (fne != null && !fne.getNames().isEmpty()) {
            mScanResult.venueName = fne.getNames().get(0).getText();
        } else {
            VenueNameElement vne =
                    (((VenueNameElement)anqpElements.get(Constants.ANQPElementType.ANQPVenueName)));
            if (vne != null && !vne.getNames().isEmpty()) {
                mScanResult.venueName = vne.getNames().get(0).getText();
            }
        }
    }

    public ScanResult getScanResult() {
        return mScanResult;
    }

    public NetworkDetail getNetworkDetail() {
        return mNetworkDetail;
    }

    public String getSSID() {
        return mNetworkDetail == null ? mScanResult.SSID : mNetworkDetail.getSSID();
    }

    public String getBSSIDString() {
        return  mNetworkDetail == null ? mScanResult.BSSID : mNetworkDetail.getBSSIDString();
    }

    public String toKeyString() {
        NetworkDetail networkDetail = mNetworkDetail;
        if (networkDetail != null) {
            return networkDetail.toKeyString();
        }
        else {
            return String.format("'%s':%012x", mScanResult.BSSID, Utils.parseMac(mScanResult.BSSID));
        }
    }

    public long getSeen() {
        return mSeen;
    }

    public long setSeen() {
        mSeen = System.currentTimeMillis();
        mScanResult.seen = mSeen;
        return mSeen;
    }

    public List<PasspointMatchInfo> getMatchList() {
        if (mMatches == null || mMatches.isEmpty()) {
            return null;
        }

        List<PasspointMatchInfo> list = new ArrayList<>();
        for (Map.Entry<HomeSP, PasspointMatch> entry : mMatches.entrySet()) {
            new PasspointMatchInfo(entry.getValue(), this, entry.getKey());
        }
        return list;
    }

    @Override
    public String toString() {
        try {
            return String.format("'%s'/%012x",
                    mScanResult.SSID, Utils.parseMac(mScanResult.BSSID));
        }
        catch (IllegalArgumentException iae) {
            return String.format("'%s'/----", mScanResult.BSSID);
        }
    }
}