2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.net.wifi;
19 import android.os.Parcelable;
20 import android.os.Parcel;
22 import java.util.BitSet;
25 * A class representing a configured Wi-Fi network, including the
26 * security configuration. Android will not necessarily support
27 * all of these security schemes initially.
29 public class WifiConfiguration implements Parcelable {
32 public static final String ssidVarName = "ssid";
34 public static final String bssidVarName = "bssid";
36 public static final String pskVarName = "psk";
38 public static final String[] wepKeyVarNames = { "wep_key0", "wep_key1", "wep_key2", "wep_key3" };
40 public static final String wepTxKeyIdxVarName = "wep_tx_keyidx";
42 public static final String priorityVarName = "priority";
44 public static final String hiddenSSIDVarName = "scan_ssid";
47 public class EnterpriseField {
48 private String varName;
51 private EnterpriseField(String varName) {
52 this.varName = varName;
56 public void setValue(String value) {
60 public String varName() {
64 public String value() {
70 public EnterpriseField eap = new EnterpriseField("eap");
72 public EnterpriseField phase2 = new EnterpriseField("phase2");
74 public EnterpriseField identity = new EnterpriseField("identity");
76 public EnterpriseField anonymous_identity = new EnterpriseField("anonymous_identity");
78 public EnterpriseField password = new EnterpriseField("password");
80 public EnterpriseField client_cert = new EnterpriseField("client_cert");
82 public EnterpriseField private_key = new EnterpriseField("private_key");
84 public EnterpriseField ca_cert = new EnterpriseField("ca_cert");
87 public EnterpriseField[] enterpriseFields = {
88 eap, phase2, identity, anonymous_identity, password, client_cert,
89 private_key, ca_cert };
92 * Recognized key management schemes.
94 public static class KeyMgmt {
97 /** WPA is not used; plaintext or static WEP could be used. */
98 public static final int NONE = 0;
99 /** WPA pre-shared key (requires {@code preSharedKey} to be specified). */
100 public static final int WPA_PSK = 1;
101 /** WPA using EAP authentication. Generally used with an external authentication server. */
102 public static final int WPA_EAP = 2;
103 /** IEEE 802.1X using EAP authentication and (optionally) dynamically
104 * generated WEP keys. */
105 public static final int IEEE8021X = 3;
107 public static final String varName = "key_mgmt";
109 public static final String[] strings = { "NONE", "WPA_PSK", "WPA_EAP", "IEEE8021X" };
113 * Recognized security protocols.
115 public static class Protocol {
116 private Protocol() { }
118 /** WPA/IEEE 802.11i/D3.0 */
119 public static final int WPA = 0;
120 /** WPA2/IEEE 802.11i */
121 public static final int RSN = 1;
123 public static final String varName = "proto";
125 public static final String[] strings = { "WPA", "RSN" };
129 * Recognized IEEE 802.11 authentication algorithms.
131 public static class AuthAlgorithm {
132 private AuthAlgorithm() { }
134 /** Open System authentication (required for WPA/WPA2) */
135 public static final int OPEN = 0;
136 /** Shared Key authentication (requires static WEP keys) */
137 public static final int SHARED = 1;
138 /** LEAP/Network EAP (only used with LEAP) */
139 public static final int LEAP = 2;
141 public static final String varName = "auth_alg";
143 public static final String[] strings = { "OPEN", "SHARED", "LEAP" };
147 * Recognized pairwise ciphers for WPA.
149 public static class PairwiseCipher {
150 private PairwiseCipher() { }
152 /** Use only Group keys (deprecated) */
153 public static final int NONE = 0;
154 /** Temporal Key Integrity Protocol [IEEE 802.11i/D7.0] */
155 public static final int TKIP = 1;
156 /** AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0] */
157 public static final int CCMP = 2;
159 public static final String varName = "pairwise";
161 public static final String[] strings = { "NONE", "TKIP", "CCMP" };
165 * Recognized group ciphers.
167 * CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]
168 * TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0]
169 * WEP104 = WEP (Wired Equivalent Privacy) with 104-bit key
170 * WEP40 = WEP (Wired Equivalent Privacy) with 40-bit key (original 802.11)
173 public static class GroupCipher {
174 private GroupCipher() { }
176 /** WEP40 = WEP (Wired Equivalent Privacy) with 40-bit key (original 802.11) */
177 public static final int WEP40 = 0;
178 /** WEP104 = WEP (Wired Equivalent Privacy) with 104-bit key */
179 public static final int WEP104 = 1;
180 /** Temporal Key Integrity Protocol [IEEE 802.11i/D7.0] */
181 public static final int TKIP = 2;
182 /** AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0] */
183 public static final int CCMP = 3;
185 public static final String varName = "group";
187 public static final String[] strings = { "WEP40", "WEP104", "TKIP", "CCMP" };
190 /** Possible status of a network configuration. */
191 public static class Status {
194 /** this is the network we are currently connected to */
195 public static final int CURRENT = 0;
196 /** supplicant will not attempt to use this network */
197 public static final int DISABLED = 1;
198 /** supplicant will consider this network available for association */
199 public static final int ENABLED = 2;
201 public static final String[] strings = { "current", "disabled", "enabled" };
205 * The ID number that the supplicant uses to identify this
206 * network configuration entry. This must be passed as an argument
207 * to most calls into the supplicant.
209 public int networkId;
212 * The current status of this network configuration entry.
217 * The network's SSID. Can either be an ASCII string,
218 * which must be enclosed in double quotation marks
219 * (e.g., {@code "MyNetwork"}, or a string of
220 * hex digits,which are not enclosed in quotes
221 * (e.g., {@code 01a243f405}).
225 * When set, this network configuration entry should only be used when
226 * associating with the AP having the specified BSSID. The value is
227 * a string in the format of an Ethernet MAC address, e.g.,
228 * <code>XX:XX:XX:XX:XX:XX</code> where each <code>X</code> is a hex digit.
233 * Pre-shared key for use with WPA-PSK.
235 * When the value of this key is read, the actual key is
236 * not returned, just a "*" if the key has a value, or the null
239 public String preSharedKey;
241 * Up to four WEP keys. Either an ASCII string enclosed in double
242 * quotation marks (e.g., {@code "abcdef"} or a string
243 * of hex digits (e.g., {@code 0102030405}).
245 * When the value of one of these keys is read, the actual key is
246 * not returned, just a "*" if the key has a value, or the null
249 public String[] wepKeys;
251 /** Default WEP key index, ranging from 0 to 3. */
252 public int wepTxKeyIndex;
255 * Priority determines the preference given to a network by {@code wpa_supplicant}
256 * when choosing an access point with which to associate.
261 * This is a network that does not broadcast its SSID, so an
262 * SSID-specific probe request must be used for scans.
264 public boolean hiddenSSID;
267 * The set of key management protocols supported by this configuration.
268 * See {@link KeyMgmt} for descriptions of the values.
269 * Defaults to WPA-PSK WPA-EAP.
271 public BitSet allowedKeyManagement;
273 * The set of security protocols supported by this configuration.
274 * See {@link Protocol} for descriptions of the values.
275 * Defaults to WPA RSN.
277 public BitSet allowedProtocols;
279 * The set of authentication protocols supported by this configuration.
280 * See {@link AuthAlgorithm} for descriptions of the values.
281 * Defaults to automatic selection.
283 public BitSet allowedAuthAlgorithms;
285 * The set of pairwise ciphers for WPA supported by this configuration.
286 * See {@link PairwiseCipher} for descriptions of the values.
287 * Defaults to CCMP TKIP.
289 public BitSet allowedPairwiseCiphers;
291 * The set of group ciphers supported by this configuration.
292 * See {@link GroupCipher} for descriptions of the values.
293 * Defaults to CCMP TKIP WEP104 WEP40.
295 public BitSet allowedGroupCiphers;
298 public WifiConfiguration() {
304 allowedKeyManagement = new BitSet();
305 allowedProtocols = new BitSet();
306 allowedAuthAlgorithms = new BitSet();
307 allowedPairwiseCiphers = new BitSet();
308 allowedGroupCiphers = new BitSet();
309 wepKeys = new String[4];
310 for (int i = 0; i < wepKeys.length; i++)
312 for (EnterpriseField field : enterpriseFields) {
313 field.setValue(null);
317 public String toString() {
318 StringBuffer sbuf = new StringBuffer();
319 if (this.status == WifiConfiguration.Status.CURRENT) {
321 } else if (this.status == WifiConfiguration.Status.DISABLED) {
324 sbuf.append("ID: ").append(this.networkId).append(" SSID: ").append(this.SSID).
325 append(" BSSID: ").append(this.BSSID).append(" PRIO: ").append(this.priority).
327 sbuf.append(" KeyMgmt:");
328 for (int k = 0; k < this.allowedKeyManagement.size(); k++) {
329 if (this.allowedKeyManagement.get(k)) {
331 if (k < KeyMgmt.strings.length) {
332 sbuf.append(KeyMgmt.strings[k]);
338 sbuf.append(" Protocols:");
339 for (int p = 0; p < this.allowedProtocols.size(); p++) {
340 if (this.allowedProtocols.get(p)) {
342 if (p < Protocol.strings.length) {
343 sbuf.append(Protocol.strings[p]);
350 sbuf.append(" AuthAlgorithms:");
351 for (int a = 0; a < this.allowedAuthAlgorithms.size(); a++) {
352 if (this.allowedAuthAlgorithms.get(a)) {
354 if (a < AuthAlgorithm.strings.length) {
355 sbuf.append(AuthAlgorithm.strings[a]);
362 sbuf.append(" PairwiseCiphers:");
363 for (int pc = 0; pc < this.allowedPairwiseCiphers.size(); pc++) {
364 if (this.allowedPairwiseCiphers.get(pc)) {
366 if (pc < PairwiseCipher.strings.length) {
367 sbuf.append(PairwiseCipher.strings[pc]);
374 sbuf.append(" GroupCiphers:");
375 for (int gc = 0; gc < this.allowedGroupCiphers.size(); gc++) {
376 if (this.allowedGroupCiphers.get(gc)) {
378 if (gc < GroupCipher.strings.length) {
379 sbuf.append(GroupCipher.strings[gc]);
385 sbuf.append('\n').append(" PSK: ");
386 if (this.preSharedKey != null) {
390 for (EnterpriseField field : enterpriseFields) {
391 sbuf.append('\n').append(" " + field.varName() + ": ");
392 String value = field.value();
393 if (value != null) sbuf.append(value);
396 return sbuf.toString();
400 * Construct a WifiConfiguration from a scanned network
401 * @param scannedAP the scan result used to construct the config entry
402 * TODO: figure out whether this is a useful way to construct a new entry.
404 public WifiConfiguration(ScanResult scannedAP) {
406 SSID = scannedAP.SSID;
407 BSSID = scannedAP.BSSID;
411 private static BitSet readBitSet(Parcel src) {
412 int cardinality = src.readInt();
414 BitSet set = new BitSet();
415 for (int i = 0; i < cardinality; i++)
416 set.set(src.readInt());
421 private static void writeBitSet(Parcel dest, BitSet set) {
424 dest.writeInt(set.cardinality());
426 while ((nextSetBit = set.nextSetBit(nextSetBit + 1)) != -1)
427 dest.writeInt(nextSetBit);
430 /** Implement the Parcelable interface {@hide} */
431 public int describeContents() {
435 /** Implement the Parcelable interface {@hide} */
436 public void writeToParcel(Parcel dest, int flags) {
437 dest.writeInt(networkId);
438 dest.writeInt(status);
439 dest.writeString(SSID);
440 dest.writeString(BSSID);
441 dest.writeString(preSharedKey);
442 for (String wepKey : wepKeys)
443 dest.writeString(wepKey);
444 dest.writeInt(wepTxKeyIndex);
445 dest.writeInt(priority);
446 dest.writeInt(hiddenSSID ? 1 : 0);
448 writeBitSet(dest, allowedKeyManagement);
449 writeBitSet(dest, allowedProtocols);
450 writeBitSet(dest, allowedAuthAlgorithms);
451 writeBitSet(dest, allowedPairwiseCiphers);
452 writeBitSet(dest, allowedGroupCiphers);
454 for (EnterpriseField field : enterpriseFields) {
455 dest.writeString(field.value());
459 /** Implement the Parcelable interface {@hide} */
460 public static final Creator<WifiConfiguration> CREATOR =
461 new Creator<WifiConfiguration>() {
462 public WifiConfiguration createFromParcel(Parcel in) {
463 WifiConfiguration config = new WifiConfiguration();
464 config.networkId = in.readInt();
465 config.status = in.readInt();
466 config.SSID = in.readString();
467 config.BSSID = in.readString();
468 config.preSharedKey = in.readString();
469 for (int i = 0; i < config.wepKeys.length; i++)
470 config.wepKeys[i] = in.readString();
471 config.wepTxKeyIndex = in.readInt();
472 config.priority = in.readInt();
473 config.hiddenSSID = in.readInt() != 0;
474 config.allowedKeyManagement = readBitSet(in);
475 config.allowedProtocols = readBitSet(in);
476 config.allowedAuthAlgorithms = readBitSet(in);
477 config.allowedPairwiseCiphers = readBitSet(in);
478 config.allowedGroupCiphers = readBitSet(in);
480 for (EnterpriseField field : config.enterpriseFields) {
481 field.setValue(in.readString());
486 public WifiConfiguration[] newArray(int size) {
487 return new WifiConfiguration[size];