summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/WifiCountryCode.java
blob: 66a035f08476fac25efed9ef8422c97dd6196f4f (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * 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 android.text.TextUtils;
import android.util.Log;

/**
 * Provide functions for making changes to WiFi country code.
 * This Country Code is from MCC or phone default setting. This class sends Country Code
 * to driver through wpa_supplicant when WifiStateMachine marks current state as ready
 * using setReadyForChange(true).
 */
public class WifiCountryCode {
    private static final String TAG = "WifiCountryCode";
    private final WifiNative mWifiNative;
    private boolean DBG = false;
    private boolean mReady = false;

    /** config option that indicate whether or not to reset country code to default when
     * cellular radio indicates country code loss
     */
    private boolean mRevertCountryCodeOnCellularLoss;
    private String mDefaultCountryCode = null;
    private String mTelephonyCountryCode = null;
    private String mCurrentCountryCode = null;

    public WifiCountryCode(
            WifiNative wifiNative,
            String oemDefaultCountryCode,
            boolean revertCountryCodeOnCellularLoss) {

        mWifiNative = wifiNative;
        mRevertCountryCodeOnCellularLoss = revertCountryCodeOnCellularLoss;

        if (!TextUtils.isEmpty(oemDefaultCountryCode)) {
            mDefaultCountryCode = oemDefaultCountryCode.toUpperCase();
        } else {
            if (mRevertCountryCodeOnCellularLoss) {
                Log.w(TAG, "config_wifi_revert_country_code_on_cellular_loss is set, "
                         + "but there is no default country code.");
                mRevertCountryCodeOnCellularLoss = false;
                return;
            }
        }

        if (mRevertCountryCodeOnCellularLoss) {
            Log.d(TAG, "Country code will be reverted to " + mDefaultCountryCode
                    + " on MCC loss");
        }
    }

    /**
     * Enable verbose logging for WifiCountryCode.
     */
    public void enableVerboseLogging(int verbose) {
        if (verbose > 0) {
            DBG = true;
        } else {
            DBG = false;
        }
    }

    /**
     * This is called when sim card is removed.
     * In this case we should invalid all other country codes except the
     * phone default one.
     */
    public synchronized void simCardRemoved() {
        if (DBG) Log.d(TAG, "SIM Card Removed");
        // SIM card is removed, we need to reset the country code to phone default.
        mTelephonyCountryCode = null;
        if (mReady) {
            updateCountryCode();
        }
    }

    /**
     * This is called when airplane mode is enabled.
     * In this case we should invalidate all other country code except the
     * phone default one.
     */
    public synchronized void airplaneModeEnabled() {
        if (DBG) Log.d(TAG, "Airplane Mode Enabled");
        // Airplane mode is enabled, we need to reset the country code to phone default.
        // Country code will be set upon when wpa_supplicant starts next time.
        mTelephonyCountryCode = null;
    }

    /**
     * Change the state to indicates if wpa_supplicant is ready to handle country code changing
     * request or not.
     * We call native code to request country code changes only when wpa_supplicant is
     * started but not yet L2 connected.
     */
    public synchronized void setReadyForChange(boolean ready) {
        if (DBG) Log.d(TAG, "Set ready: " + ready);
        mReady = ready;
        // We are ready to set country code now.
        // We need to post pending country code request.
        if (mReady) {
            updateCountryCode();
        }
    }

    /**
     * Handle country code change request.
     * @param countryCode The country code intended to set.
     * This is supposed to be from Telephony service.
     * otherwise we think it is from other applications.
     * @return Returns true if the country code passed in is acceptable.
     */
    public synchronized boolean setCountryCode(String countryCode) {
        if (DBG) Log.d(TAG, "Receive set country code request: " + countryCode);
        // Empty country code.
        if (TextUtils.isEmpty(countryCode)) {
            if (mRevertCountryCodeOnCellularLoss) {
                if (DBG) Log.d(TAG, "Received empty country code, reset to default country code");
                mTelephonyCountryCode = null;
            }
        } else {
            mTelephonyCountryCode = countryCode.toUpperCase();
        }
        // If wpa_supplicant is ready we set the country code now, otherwise it will be
        // set once wpa_supplicant is ready.
        if (mReady) {
            updateCountryCode();
        }
        return true;
    }

    /**
     * Method to get the Country Code that was sent to wpa_supplicant.
     *
     * @return Returns the local copy of the Country Code that was sent to the driver upon
     * setReadyForChange(true).
     * If wpa_supplicant was never started, this may be null even if a SIM reported a valid
     * country code.
     * Returns null if no Country Code was sent to driver.
     */
    public synchronized String getCountryCodeSentToDriver() {
        return mCurrentCountryCode;
    }

    /**
     * Method to return the currently reported Country Code from the SIM or phone default setting.
     *
     * @return The currently reported Country Code from the SIM. If there is no Country Code
     * reported from SIM, a phone default Country Code will be returned.
     * Returns null when there is no Country Code available.
     */
    public synchronized String getCountryCode() {
        return pickCountryCode();
    }

    private void updateCountryCode() {
        if (DBG) Log.d(TAG, "Update country code");
        String country = pickCountryCode();
        // We do not check if the country code equals the current one.
        // There are two reasons:
        // 1. Wpa supplicant may silently modify the country code.
        // 2. If Wifi restarted therefoere wpa_supplicant also restarted,
        // the country code counld be reset to '00' by wpa_supplicant.
        if (country != null) {
            setCountryCodeNative(country);
        }
        // We do not set country code if there is no candidate. This is reasonable
        // because wpa_supplicant usually starts with an international safe country
        // code setting: '00'.
    }

    private String pickCountryCode() {
        if (mTelephonyCountryCode != null) {
            return mTelephonyCountryCode;
        }
        if (mDefaultCountryCode != null) {
            return mDefaultCountryCode;
        }
        // If there is no candidate country code we will return null.
        return null;
    }

    private boolean setCountryCodeNative(String country) {
        if (mWifiNative.setCountryCode(country)) {
            Log.d(TAG, "Succeeded to set country code to: " + country);
            mCurrentCountryCode = country;
            return true;
        }
        Log.d(TAG, "Failed to set country code to: " + country);
        return false;
    }
}