summaryrefslogtreecommitdiffstats
path: root/wifi/java/android/net/wifi/passpoint/WifiPasspointManager.java
blob: 0245a3d0617ab7925fae00fd751753517a886be1 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
 * Copyright (C) 2014 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 android.net.wifi.passpoint;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.util.AsyncChannel;
import com.android.internal.util.Protocol;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Provides APIs for managing Wifi Passpoint credentials.
 * @hide
 */
public class WifiPasspointManager {

    private static final String TAG = "PasspointManager";

    private static final boolean DBG = true;

    /* Passpoint states values */

    /** Passpoint is in an unknown state. This should only occur in boot time */
    public static final int PASSPOINT_STATE_UNKNOWN = 0;

    /** Passpoint is disabled. This occurs when wifi is disabled */
    public static final int PASSPOINT_STATE_DISABLED = 1;

    /** Passpoint is enabled and in discovery state */
    public static final int PASSPOINT_STATE_DISCOVERY = 2;

    /** Passpoint is enabled and in access state */
    public static final int PASSPOINT_STATE_ACCESS = 3;

    /** Passpoint is enabled and in provisioning state */
    public static final int PASSPOINT_STATE_PROVISION = 4;

    /* Passpoint callback error codes */

    /** Indicates that the operation failed due to an internal error */
    public static final int REASON_ERROR = 0;

    /** Indicates that the operation failed because wifi is disabled */
    public static final int REASON_WIFI_DISABLED = 1;

    /** Indicates that the operation failed because the framework is busy */
    public static final int REASON_BUSY = 2;

    /** Indicates that the operation failed because parameter is invalid */
    public static final int REASON_INVALID_PARAMETER = 3;

    /** Indicates that the operation failed because the server is not trusted */
    public static final int REASON_NOT_TRUSTED = 4;

    /**
     * protocol supported for Passpoint
     */
    public static final String PROTOCOL_DM = "OMA-DM-ClientInitiated";

    /**
     * protocol supported for Passpoint
     */
    public static final String PROTOCOL_SOAP = "SPP-ClientInitiated";

    /* Passpoint broadcasts */

    /**
     * Broadcast intent action indicating that the state of Passpoint
     * connectivity has changed
     */
    public static final String PASSPOINT_STATE_CHANGED_ACTION =
            "android.net.wifi.passpoint.STATE_CHANGE";

    /**
     * Broadcast intent action indicating that the saved Passpoint credential
     * list has changed
     */
    public static final String PASSPOINT_CRED_CHANGED_ACTION =
            "android.net.wifi.passpoint.CRED_CHANGE";

    /**
     * Broadcast intent action indicating that Passpoint online sign up is
     * avaiable.
     */
    public static final String PASSPOINT_OSU_AVAILABLE_ACTION =
            "android.net.wifi.passpoint.OSU_AVAILABLE";

    /**
     * Broadcast intent action indicating that user remediation is required
     */
    public static final String PASSPOINT_USER_REM_REQ_ACTION =
            "android.net.wifi.passpoint.USER_REM_REQ";

    /**
     * Interface for callback invocation when framework channel is lost
     */
    public interface ChannelListener {
        /**
         * The channel to the framework has been disconnected. Application could
         * try re-initializing using {@link #initialize}
         */
        public void onChannelDisconnected();
    }

    /**
     * Interface for callback invocation on an application action
     */
    public interface ActionListener {
        /** The operation succeeded */
        public void onSuccess();

        /**
         * The operation failed
         *
         * @param reason The reason for failure could be one of
         *            {@link #WIFI_DISABLED}, {@link #ERROR} or {@link #BUSY}
         */
        public void onFailure(int reason);
    }

    /**
     * Interface for callback invocation when doing OSU or user remediation
     */
    public interface OsuRemListener {
        /** The operation succeeded */
        public void onSuccess();

        /**
         * The operation failed
         *
         * @param reason The reason for failure could be one of
         *            {@link #WIFI_DISABLED}, {@link #ERROR} or {@link #BUSY}
         */
        public void onFailure(int reason);

        /**
         * Browser launch is requried for user interaction. When this callback
         * is called, app should launch browser / webview to the given URI.
         *
         * @param uri URI for browser launch
         */
        public void onBrowserLaunch(String uri);

        /**
         * When this is called, app should dismiss the previously lanched browser.
         */
        public void onBrowserDismiss();
    }

    /**
     * A channel that connects the application to the wifi passpoint framework.
     * Most passpoint operations require a Channel as an argument.
     * An instance of Channel is obtained by doing a call on {@link #initialize}
     */
    public static class Channel {
        private final static int INVALID_LISTENER_KEY = 0;

        private ChannelListener mChannelListener;

        private HashMap<Integer, Object> mListenerMap = new HashMap<Integer, Object>();
        private HashMap<Integer, Integer> mListenerMapCount = new HashMap<Integer, Integer>();
        private Object mListenerMapLock = new Object();
        private int mListenerKey = 0;

        private List<ScanResult> mAnqpRequest = new LinkedList<ScanResult>();
        private Object mAnqpRequestLock = new Object();

        private AsyncChannel mAsyncChannel;
        private PasspointHandler mHandler;
        Context mContext;

        Channel(Context context, Looper looper, ChannelListener l) {
            mAsyncChannel = new AsyncChannel();
            mHandler = new PasspointHandler(looper);
            mChannelListener = l;
            mContext = context;
        }

        private int putListener(Object listener) {
            return putListener(listener, 1);
        }

        private int putListener(Object listener, int count) {
            if (listener == null || count <= 0)
                return INVALID_LISTENER_KEY;
            int key;
            synchronized (mListenerMapLock) {
                do {
                    key = mListenerKey++;
                } while (key == INVALID_LISTENER_KEY);
                mListenerMap.put(key, listener);
                mListenerMapCount.put(key, count);
            }
            return key;
        }

        private Object peekListener(int key) {
            Log.d(TAG, "peekListener() key=" + key);
            if (key == INVALID_LISTENER_KEY)
                return null;
            synchronized (mListenerMapLock) {
                return mListenerMap.get(key);
            }
        }


        private Object getListener(int key, boolean forceRemove) {
            Log.d(TAG, "getListener() key=" + key + " force=" + forceRemove);
            if (key == INVALID_LISTENER_KEY)
                return null;
            synchronized (mListenerMapLock) {
                if (!forceRemove) {
                    int count = mListenerMapCount.get(key);
                    Log.d(TAG, "count=" + count);
                    mListenerMapCount.put(key, --count);
                    if (count > 0)
                        return null;
                }
                Log.d(TAG, "remove key");
                mListenerMapCount.remove(key);
                return mListenerMap.remove(key);
            }
        }

        private void anqpRequestStart(ScanResult sr) {
            Log.d(TAG, "anqpRequestStart sr.bssid=" + sr.BSSID);
            synchronized (mAnqpRequestLock) {
                mAnqpRequest.add(sr);
            }
        }

        private void anqpRequestFinish(WifiPasspointInfo result) {
            Log.d(TAG, "anqpRequestFinish pi.bssid=" + result.bssid);
            synchronized (mAnqpRequestLock) {
                for (ScanResult sr : mAnqpRequest)
                    if (sr.BSSID.equals(result.bssid)) {
                        Log.d(TAG, "find hit " + result.bssid);
                        /* sr.passpoint = result; */
                        mAnqpRequest.remove(sr);
                        Log.d(TAG, "mAnqpRequest.len=" + mAnqpRequest.size());
                        break;
                    }
            }
        }

        private void anqpRequestFinish(ScanResult sr) {
            Log.d(TAG, "anqpRequestFinish sr.bssid=" + sr.BSSID);
            synchronized (mAnqpRequestLock) {
                for (ScanResult sr1 : mAnqpRequest)
                    if (sr1.BSSID.equals(sr.BSSID)) {
                        mAnqpRequest.remove(sr1);
                        break;
                    }
            }
        }

        class PasspointHandler extends Handler {
            PasspointHandler(Looper looper) {
                super(looper);
            }

            @Override
            public void handleMessage(Message message) {
                Object listener = null;

                switch (message.what) {
                    case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
                        if (mChannelListener != null) {
                            mChannelListener.onChannelDisconnected();
                            mChannelListener = null;
                        }
                        break;

                    case REQUEST_ANQP_INFO_SUCCEEDED:
                        WifiPasspointInfo result = (WifiPasspointInfo) message.obj;
                        anqpRequestFinish(result);
                        listener = getListener(message.arg2, false);
                        if (listener != null) {
                            ((ActionListener) listener).onSuccess();
                        }
                        break;

                    case REQUEST_ANQP_INFO_FAILED:
                        anqpRequestFinish((ScanResult) message.obj);
                        listener = getListener(message.arg2, false);
                        if (listener == null)
                            getListener(message.arg2, true);
                        if (listener != null) {
                            ((ActionListener) listener).onFailure(message.arg1);
                        }
                        break;

                    case START_OSU_SUCCEEDED:
                        listener = getListener(message.arg2, true);
                        if (listener != null) {
                            ((OsuRemListener) listener).onSuccess();
                        }
                        break;

                    case START_OSU_FAILED:
                        listener = getListener(message.arg2, true);
                        if (listener != null) {
                            ((OsuRemListener) listener).onFailure(message.arg1);
                        }
                        break;

                    case START_OSU_BROWSER:
                        listener = peekListener(message.arg2);
                        if (listener != null) {
                            ParcelableString str = (ParcelableString) message.obj;
                            if (str == null || str.string == null)
                                ((OsuRemListener) listener).onBrowserDismiss();
                            else
                                ((OsuRemListener) listener).onBrowserLaunch(str.string);
                        }
                        break;

                    default:
                        Log.d(TAG, "Ignored " + message);
                        break;
                }
            }
        }

    }

    public static class ParcelableString implements Parcelable {
        public String string;

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeString(string);
        }

        public static final Parcelable.Creator<ParcelableString> CREATOR =
                new Parcelable.Creator<ParcelableString>() {
                    @Override
                    public ParcelableString createFromParcel(Parcel in) {
                        ParcelableString ret = new ParcelableString();
                        ret.string = in.readString();
                        return ret;
                    }
                    @Override
                    public ParcelableString[] newArray(int size) {
                        return new ParcelableString[size];
                    }
        };
    }

    private static final int BASE = Protocol.BASE_WIFI_PASSPOINT_MANAGER;

    public static final int REQUEST_ANQP_INFO                   = BASE + 1;
    public static final int REQUEST_ANQP_INFO_FAILED            = BASE + 2;
    public static final int REQUEST_ANQP_INFO_SUCCEEDED         = BASE + 3;
    public static final int REQUEST_OSU_ICON                    = BASE + 4;
    public static final int REQUEST_OSU_ICON_FAILED             = BASE + 5;
    public static final int REQUEST_OSU_ICON_SUCCEEDED          = BASE + 6;
    public static final int START_OSU                           = BASE + 7;
    public static final int START_OSU_BROWSER                   = BASE + 8;
    public static final int START_OSU_FAILED                    = BASE + 9;
    public static final int START_OSU_SUCCEEDED                 = BASE + 10;

    private Context mContext;
    IWifiPasspointManager mService;

    /**
     * TODO: doc
     * @param context
     * @param service
     */
    public WifiPasspointManager(Context context, IWifiPasspointManager service) {
        mContext = context;
        mService = service;
    }

    /**
     * Registers the application with the framework. This function must be the
     * first to be called before any async passpoint operations are performed.
     *
     * @param srcContext is the context of the source
     * @param srcLooper is the Looper on which the callbacks are receivied
     * @param listener for callback at loss of framework communication. Can be
     *            null.
     * @return Channel instance that is necessary for performing any further
     *         passpoint operations
     *
     */
    public Channel initialize(Context srcContext, Looper srcLooper, ChannelListener listener) {
        Messenger messenger = getMessenger();
        if (messenger == null)
            return null;

        Channel c = new Channel(srcContext, srcLooper, listener);
        if (c.mAsyncChannel.connectSync(srcContext, c.mHandler, messenger)
                == AsyncChannel.STATUS_SUCCESSFUL) {
            return c;
        } else {
            return null;
        }
    }

    /**
     * STOPSHIP: temp solution, should use supplicant manager instead, check
     * with b/13931972
     */
    public Messenger getMessenger() {
        try {
            return mService.getMessenger();
        } catch (RemoteException e) {
            return null;
        }
    }

    public int getPasspointState() {
        try {
            return mService.getPasspointState();
        } catch (RemoteException e) {
            return PASSPOINT_STATE_UNKNOWN;
        }
    }

    public void requestAnqpInfo(Channel c, List<ScanResult> requested, int mask,
            ActionListener listener) {
        Log.d(TAG, "requestAnqpInfo start");
        Log.d(TAG, "requested.size=" + requested.size());
        checkChannel(c);
        List<ScanResult> list = new ArrayList<ScanResult>();
        for (ScanResult sr : requested)
            if (sr.capabilities.contains("[HS20]")) {
                list.add(sr);
                c.anqpRequestStart(sr);
                Log.d(TAG, "adding " + sr.BSSID);
            }
        int count = list.size();
        Log.d(TAG, "after filter, count=" + count);
        if (count == 0) {
            if (DBG)
                Log.d(TAG, "ANQP info request contains no HS20 APs, skipped");
            listener.onSuccess();
            return;
        }
        int key = c.putListener(listener, count);
        for (ScanResult sr : list)
            c.mAsyncChannel.sendMessage(REQUEST_ANQP_INFO, mask, key, sr);
        Log.d(TAG, "requestAnqpInfo end");
    }

    public void requestOsuIcons(Channel c, List<WifiPasspointOsuProvider> requested,
            int resolution, ActionListener listener) {
    }

    public List<WifiPasspointPolicy> requestCredentialMatch(List<ScanResult> requested) {
        try {
            return mService.requestCredentialMatch(requested);
        } catch (RemoteException e) {
            return null;
        }
    }

    /**
     * Get a list of saved Passpoint credentials. Only those credentials owned
     * by the caller will be returned.
     *
     * @return The list of credentials
     */
    public List<WifiPasspointCredential> getCredentials() {
        try {
            return mService.getCredentials();
        } catch (RemoteException e) {
            return null;
        }
    }

    /**
     * Add a new Passpoint credential.
     *
     * @param cred The credential to be added
     * @return {@code true} if the operation succeeds, {@code false} otherwise
     */
    public boolean addCredential(WifiPasspointCredential cred) {
        try {
            return mService.addCredential(cred);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Update an existing Passpoint credential. Only system or the owner of this
     * credential has the permission to do this.
     *
     * @param cred The credential to be updated
     * @return {@code true} if the operation succeeds, {@code false} otherwise
     */
    public boolean updateCredential(WifiPasspointCredential cred) {
        try {
            return mService.updateCredential(cred);
        } catch (RemoteException e) {
            return false;
        }
    }

    /**
     * Remove an existing Passpoint credential. Only system or the owner of this
     * credential has the permission to do this.
     *
     * @param cred The credential to be removed
     * @return {@code true} if the operation succeeds, {@code false} otherwise
     */
    public boolean removeCredential(WifiPasspointCredential cred) {
        try {
            return mService.removeCredential(cred);
        } catch (RemoteException e) {
            return false;
        }
    }

    public void startOsu(Channel c, WifiPasspointOsuProvider osu, OsuRemListener listener) {
        Log.d(TAG, "startOsu start");
        checkChannel(c);
        int key = c.putListener(listener);
        c.mAsyncChannel.sendMessage(START_OSU, 0, key, osu);
        Log.d(TAG, "startOsu end");
    }

    public void startRemediation(Channel c, OsuRemListener listener) {
    }

    public void connect(WifiPasspointPolicy policy) {
    }

    private static void checkChannel(Channel c) {
        if (c == null) throw new IllegalArgumentException("Channel needs to be initialized");
    }
}