summaryrefslogtreecommitdiffstats
path: root/service/java/com/android/server/wifi/RttService.java
blob: 7e4648b10e545a82acefce4d62959cf32467681f (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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
package com.android.server.wifi;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.IApInterface;
import android.net.wifi.IClientInterface;
import android.net.wifi.IInterfaceEventCallback;
import android.net.wifi.IRttManager;
import android.net.wifi.IWificond;
import android.net.wifi.RttManager;
import android.net.wifi.RttManager.ResponderConfig;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.AsyncChannel;
import com.android.internal.util.IState;
import com.android.internal.util.Protocol;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.server.SystemService;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;

public final class RttService extends SystemService {

    public static final boolean DBG = true;
    private static final String WIFICOND_SERVICE_NAME = "wificond";

    static class RttServiceImpl extends IRttManager.Stub {

        @Override
        public Messenger getMessenger() {
            return new Messenger(mClientHandler);
        }

        private class ClientHandler extends Handler {

            ClientHandler(android.os.Looper looper) {
                super(looper);
            }

            @Override
            public void handleMessage(Message msg) {

                if (DBG) {
                    Log.d(TAG, "ClientHandler got" + msg + " what = " + getDescription(msg.what));
                }

                switch (msg.what) {

                    case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
                        if (msg.arg1 == AsyncChannel.STATUS_SEND_UNSUCCESSFUL) {
                            Slog.e(TAG, "Send failed, client connection lost");
                        } else {
                            if (DBG) Slog.d(TAG, "Client connection lost with reason: " + msg.arg1);
                        }
                        if (DBG) Slog.d(TAG, "closing client " + msg.replyTo);
                        synchronized (mLock) {
                            ClientInfo ci = mClients.remove(msg.replyTo);
                            if (ci != null) ci.cleanup();
                        }
                        return;
                    case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
                        AsyncChannel ac = new AsyncChannel();
                        ac.connected(mContext, this, msg.replyTo);
                        ClientInfo client = new ClientInfo(ac, msg.sendingUid);
                        synchronized (mLock) {
                            mClients.put(msg.replyTo, client);
                        }
                        ac.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
                                AsyncChannel.STATUS_SUCCESSFUL);
                        return;
                }

                ClientInfo ci;
                synchronized (mLock) {
                    ci = mClients.get(msg.replyTo);
                }
                if (ci == null) {
                    Slog.e(TAG, "Could not find client info for message " + msg.replyTo);
                    replyFailed(msg, RttManager.REASON_INVALID_LISTENER, "Could not find listener");
                    return;
                }
                if (!enforcePermissionCheck(msg)) {
                    replyFailed(msg, RttManager.REASON_PERMISSION_DENIED,
                            "Client doesn't have LOCATION_HARDWARE permission");
                    return;
                }
                final int validCommands[] = {
                        RttManager.CMD_OP_START_RANGING,
                        RttManager.CMD_OP_STOP_RANGING,
                        RttManager.CMD_OP_ENABLE_RESPONDER,
                        RttManager.CMD_OP_DISABLE_RESPONDER,
                        };

                for (int cmd : validCommands) {
                    if (cmd == msg.what) {
                        mStateMachine.sendMessage(Message.obtain(msg));
                        return;
                    }
                }

                replyFailed(msg, RttManager.REASON_INVALID_REQUEST, "Invalid request");
            }

            private String getDescription(int what) {
                switch(what) {
                    case RttManager.CMD_OP_ENABLE_RESPONDER:
                        return "CMD_OP_ENABLE_RESPONDER";
                    case RttManager.CMD_OP_DISABLE_RESPONDER:
                        return "CMD_OP_DISABLE_RESPONDER";
                    default:
                        return "CMD_UNKNOWN";
                }
            }
        }

        private final WifiNative mWifiNative;
        private final Context mContext;
        private final Looper mLooper;
        private RttStateMachine mStateMachine;
        private ClientHandler mClientHandler;
        private WifiInjector mWifiInjector;

        RttServiceImpl(Context context, Looper looper, WifiInjector wifiInjector) {
            mContext = context;
            mWifiNative = wifiInjector.getWifiNative();
            mLooper = looper;
            mWifiInjector = wifiInjector;
        }

        public void startService() {
            mClientHandler = new ClientHandler(mLooper);
            mStateMachine = new RttStateMachine(mLooper);
            mContext.registerReceiver(
                    new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            int state = intent.getIntExtra(
                                    WifiManager.EXTRA_SCAN_AVAILABLE, WifiManager.WIFI_STATE_DISABLED);
                            if (DBG) Log.d(TAG, "SCAN_AVAILABLE : " + state);
                            if (state == WifiManager.WIFI_STATE_ENABLED) {
                                mStateMachine.sendMessage(CMD_DRIVER_LOADED);
                            } else if (state == WifiManager.WIFI_STATE_DISABLED) {
                                mStateMachine.sendMessage(CMD_DRIVER_UNLOADED);
                            }
                        }
                    }, new IntentFilter(WifiManager.WIFI_SCAN_AVAILABLE));

            mStateMachine.start();
        }

        private class RttRequest {
            Integer key;
            ClientInfo ci;
            RttManager.RttParams[] params;

            @Override
            public String toString() {
                String str = getClass().getName() + "@" + Integer.toHexString(hashCode());
                if(this.key != null) {
                    return str + " key: " + this.key;
                } else {
                    return str + " key: " + " , null";
                }
            }
        }

        private class ClientInfo {
            private final AsyncChannel mChannel;
            private final int mUid;

            ArrayMap<Integer, RttRequest> mRequests = new ArrayMap<>();
            // Client keys of all outstanding responders.
            Set<Integer> mResponderRequests = new HashSet<>();

            ClientInfo(AsyncChannel channel, int uid) {
                mChannel = channel;
                mUid = uid;
            }

            void addResponderRequest(int key) {
                mResponderRequests.add(key);
            }

            void removeResponderRequest(int key) {
                mResponderRequests.remove(key);
            }

            boolean addRttRequest(int key, RttManager.ParcelableRttParams parcelableParams) {
                if (parcelableParams == null) {
                    return false;
                }

                RttManager.RttParams params[] = parcelableParams.mParams;

                RttRequest request = new RttRequest();
                request.key = key;
                request.ci = this;
                request.params = params;
                mRequests.put(key, request);
                mRequestQueue.add(request);
                return true;
            }

            void removeRttRequest(int key) {
                mRequests.remove(key);
            }

            void reportResponderEnableSucceed(int key, ResponderConfig config) {
                mChannel.sendMessage(RttManager.CMD_OP_ENALBE_RESPONDER_SUCCEEDED, 0, key, config);
            }

            void reportResponderEnableFailed(int key, int reason) {
                mChannel.sendMessage(RttManager.CMD_OP_ENALBE_RESPONDER_FAILED, reason, key);
                removeResponderRequest(key);
            }

            void reportResult(RttRequest request, RttManager.RttResult[] results) {
                RttManager.ParcelableRttResults parcelableResults =
                        new RttManager.ParcelableRttResults(results);

                mChannel.sendMessage(RttManager.CMD_OP_SUCCEEDED,
                        0, request.key, parcelableResults);
                removeRttRequest(request.key);
            }

            void reportFailed(RttRequest request, int reason, String description) {
                reportFailed(request.key, reason, description);
            }

            void reportFailed(int key, int reason, String description) {
                Bundle bundle = new Bundle();
                bundle.putString(RttManager.DESCRIPTION_KEY, description);
                mChannel.sendMessage(RttManager.CMD_OP_FAILED, key, reason, bundle);
                removeRttRequest(key);
            }

            void reportAborted(int key) {
                mChannel.sendMessage(RttManager.CMD_OP_ABORTED, 0, key);
                //All Queued RTT request will be cleaned
                cleanup();
            }

            void cleanup() {
                mRequests.clear();
                mRequestQueue.clear();
                // When client is lost, clean up responder requests and send disable responder
                // message to RttStateMachine.
                mResponderRequests.clear();
                mStateMachine.sendMessage(RttManager.CMD_OP_DISABLE_RESPONDER);
            }

            @Override
            public String toString() {
                return "ClientInfo [uid=" + mUid + ", channel=" + mChannel + "]";
            }
        }

        private Queue<RttRequest> mRequestQueue = new LinkedList<>();

        @GuardedBy("mLock")
        private ArrayMap<Messenger, ClientInfo> mClients = new ArrayMap<>();
        // Lock for mClients.
        private final Object mLock = new Object();

        private static final int BASE = Protocol.BASE_WIFI_RTT_SERVICE;

        private static final int CMD_DRIVER_LOADED                       = BASE + 0;
        private static final int CMD_DRIVER_UNLOADED                     = BASE + 1;
        private static final int CMD_ISSUE_NEXT_REQUEST                  = BASE + 2;
        private static final int CMD_RTT_RESPONSE                        = BASE + 3;
        private static final int CMD_CLIENT_INTERFACE_READY              = BASE + 4;
        private static final int CMD_CLIENT_INTERFACE_DOWN               = BASE + 5;

        // Maximum duration for responder role.
        private static final int MAX_RESPONDER_DURATION_SECONDS = 60 * 10;

        private static class InterfaceEventHandler extends IInterfaceEventCallback.Stub {
            InterfaceEventHandler(RttStateMachine rttStateMachine) {
                mRttStateMachine = rttStateMachine;
            }
            @Override
            public void OnClientTorndownEvent(IClientInterface networkInterface) {
                mRttStateMachine.sendMessage(CMD_CLIENT_INTERFACE_DOWN, networkInterface);
            }
            @Override
            public void OnClientInterfaceReady(IClientInterface networkInterface) {
                mRttStateMachine.sendMessage(CMD_CLIENT_INTERFACE_READY, networkInterface);
            }
            @Override
            public void OnApTorndownEvent(IApInterface networkInterface) { }
            @Override
            public void OnApInterfaceReady(IApInterface networkInterface) { }

            private RttStateMachine mRttStateMachine;
        }

        class RttStateMachine extends StateMachine {
            private IWificond mWificond;
            private InterfaceEventHandler mInterfaceEventHandler;
            private IClientInterface mClientInterface;

            DefaultState mDefaultState = new DefaultState();
            EnabledState mEnabledState = new EnabledState();
            InitiatorEnabledState mInitiatorEnabledState = new InitiatorEnabledState();
            ResponderEnabledState mResponderEnabledState = new ResponderEnabledState();
            ResponderConfig mResponderConfig;

            RttStateMachine(Looper looper) {
                super("RttStateMachine", looper);

                // CHECKSTYLE:OFF IndentationCheck
                addState(mDefaultState);
                addState(mEnabledState);
                    addState(mInitiatorEnabledState, mEnabledState);
                    addState(mResponderEnabledState, mEnabledState);
                // CHECKSTYLE:ON IndentationCheck

                setInitialState(mDefaultState);
            }

            class DefaultState extends State {
                @Override
                public boolean processMessage(Message msg) {
                    if (DBG) Log.d(TAG, "DefaultState got" + msg);
                    switch (msg.what) {
                        case CMD_DRIVER_LOADED:
                            transitionTo(mEnabledState);
                            break;
                        case CMD_ISSUE_NEXT_REQUEST:
                            deferMessage(msg);
                            break;
                        case RttManager.CMD_OP_START_RANGING:
                            replyFailed(msg, RttManager.REASON_NOT_AVAILABLE, "Try later");
                            break;
                        case RttManager.CMD_OP_STOP_RANGING:
                            return HANDLED;
                        case RttManager.CMD_OP_ENABLE_RESPONDER:

                            ClientInfo client;
                            synchronized (mLock) {
                                client = mClients.get(msg.replyTo);
                            }
                            if (client == null) {
                                Log.e(TAG, "client not connected yet!");
                                break;
                            }
                            int key = msg.arg2;
                            client.reportResponderEnableFailed(key,
                                    RttManager.REASON_NOT_AVAILABLE);
                            break;
                        case RttManager.CMD_OP_DISABLE_RESPONDER:
                            return HANDLED;
                        default:
                            return NOT_HANDLED;
                    }
                    return HANDLED;
                }
            }

            class EnabledState extends State {
                @Override
                public void enter() {
                    // This allows us to tolerate wificond restarts.
                    // When wificond restarts WifiStateMachine is supposed to go
                    // back to initial state and restart.
                    // 1) RttService watches for WIFI_STATE_ENABLED broadcasts
                    // 2) WifiStateMachine sends these broadcasts in the SupplicantStarted state
                    // 3) Since WSM will only be in SupplicantStarted for as long as wificond is
                    // alive, we refresh our wificond handler here and we don't subscribe to
                    // wificond's death explicitly.
                    mWificond = mWifiInjector.makeWificond();
                    if (mWificond == null) {
                        Log.w(TAG, "Failed to get wificond binder handler");
                        transitionTo(mDefaultState);
                    }
                    mInterfaceEventHandler = new InterfaceEventHandler(mStateMachine);
                    try {
                        mWificond.RegisterCallback(mInterfaceEventHandler);
                        // Get the current client interface, assuming there is at most
                        // one client interface for now.
                        List<IBinder> interfaces = mWificond.GetClientInterfaces();
                        if (interfaces.size() > 0) {
                            mStateMachine.sendMessage(
                                    CMD_CLIENT_INTERFACE_READY,
                                    IClientInterface.Stub.asInterface(interfaces.get(0)));
                        }
                    } catch (RemoteException e1) { }

                }
                @Override
                public void exit() {
                    try {
                        mWificond.UnregisterCallback(mInterfaceEventHandler);
                    } catch (RemoteException e1) { }
                    mInterfaceEventHandler = null;
                }
                @Override
                public boolean processMessage(Message msg) {
                    if (DBG) Log.d(TAG, "EnabledState got" + msg);
                    ClientInfo ci;
                    synchronized (mLock) {
                        ci = mClients.get(msg.replyTo);
                    }

                    switch (msg.what) {
                        case CMD_DRIVER_UNLOADED:
                            transitionTo(mDefaultState);
                            break;
                        case CMD_ISSUE_NEXT_REQUEST:
                            deferMessage(msg);
                            transitionTo(mInitiatorEnabledState);
                            break;
                        case RttManager.CMD_OP_START_RANGING: {
                            RttManager.ParcelableRttParams params =
                                    (RttManager.ParcelableRttParams)msg.obj;
                            if (params == null || params.mParams == null
                                    || params.mParams.length == 0) {
                                replyFailed(msg,
                                        RttManager.REASON_INVALID_REQUEST, "No params");
                            } else if (ci.addRttRequest(msg.arg2, params) == false) {
                                replyFailed(msg,
                                        RttManager.REASON_INVALID_REQUEST, "Unspecified");
                            } else {
                                sendMessage(CMD_ISSUE_NEXT_REQUEST);
                            }
                        }
                            break;
                        case RttManager.CMD_OP_STOP_RANGING:
                            for (Iterator<RttRequest> it = mRequestQueue.iterator();
                                    it.hasNext(); ) {
                                RttRequest request = it.next();
                                if (request.key == msg.arg2) {
                                    if (DBG) Log.d(TAG, "Cancelling not-yet-scheduled RTT");
                                    mRequestQueue.remove(request);
                                    request.ci.reportAborted(request.key);
                                    break;
                                }
                            }
                            break;
                        case RttManager.CMD_OP_ENABLE_RESPONDER:
                            int key = msg.arg2;
                            mResponderConfig =
                                    mWifiNative.enableRttResponder(MAX_RESPONDER_DURATION_SECONDS);
                            if (DBG) Log.d(TAG, "mWifiNative.enableRttResponder called");

                            if (mResponderConfig != null) {
                                // TODO: remove once mac address is added when enabling responder.
                                mResponderConfig.macAddress = mWifiNative.getMacAddress();
                                ci.addResponderRequest(key);
                                ci.reportResponderEnableSucceed(key, mResponderConfig);
                                transitionTo(mResponderEnabledState);
                            } else {
                                Log.e(TAG, "enable responder failed");
                                ci.reportResponderEnableFailed(key, RttManager.REASON_UNSPECIFIED);
                            }
                            break;
                        case RttManager.CMD_OP_DISABLE_RESPONDER:
                            break;
                        case CMD_CLIENT_INTERFACE_DOWN:
                            if (mClientInterface == (IClientInterface) msg.obj) {
                                mClientInterface = null;
                            }
                            break;
                        case CMD_CLIENT_INTERFACE_READY:
                            mClientInterface = (IClientInterface) msg.obj;
                            break;
                        default:
                            return NOT_HANDLED;
                    }
                    return HANDLED;
                }
            }

            class InitiatorEnabledState extends State {
                RttRequest mOutstandingRequest;
                @Override
                public boolean processMessage(Message msg) {
                    if (DBG) Log.d(TAG, "RequestPendingState got" + msg);
                    switch (msg.what) {
                        case CMD_DRIVER_UNLOADED:
                            if (mOutstandingRequest != null) {
                                mWifiNative.cancelRtt(mOutstandingRequest.params);
                                if (DBG) Log.d(TAG, "abort key: " + mOutstandingRequest.key);
                                mOutstandingRequest.ci.reportAborted(mOutstandingRequest.key);
                                mOutstandingRequest = null;
                            }
                            transitionTo(mDefaultState);
                            break;
                        case CMD_ISSUE_NEXT_REQUEST:
                            if (mOutstandingRequest == null) {
                                mOutstandingRequest = issueNextRequest();
                                if (mOutstandingRequest == null) {
                                    transitionTo(mEnabledState);
                                }
                                if(mOutstandingRequest != null) {
                                    if (DBG) Log.d(TAG, "new mOutstandingRequest.key is: " +
                                            mOutstandingRequest.key);
                                } else {
                                    if (DBG) Log.d(TAG,
                                            "CMD_ISSUE_NEXT_REQUEST: mOutstandingRequest =null ");
                                }
                            } else {
                                /* just wait; we'll issue next request after
                                 * current one is finished */
                                 if (DBG) Log.d(TAG, "Current mOutstandingRequest.key is: " +
                                         mOutstandingRequest.key);
                                 if (DBG) Log.d(TAG, "Ignoring CMD_ISSUE_NEXT_REQUEST");
                            }
                            break;
                        case CMD_RTT_RESPONSE:
                            if (DBG) Log.d(TAG, "Received an RTT response from: " + msg.arg2);
                            mOutstandingRequest.ci.reportResult(
                                    mOutstandingRequest, (RttManager.RttResult[])msg.obj);
                            mOutstandingRequest = null;
                            sendMessage(CMD_ISSUE_NEXT_REQUEST);
                            break;
                        case RttManager.CMD_OP_STOP_RANGING:
                            if (mOutstandingRequest != null
                                    && msg.arg2 == mOutstandingRequest.key) {
                                if (DBG) Log.d(TAG, "Cancelling ongoing RTT of: " + msg.arg2);
                                mWifiNative.cancelRtt(mOutstandingRequest.params);
                                mOutstandingRequest.ci.reportAborted(mOutstandingRequest.key);
                                mOutstandingRequest = null;
                                sendMessage(CMD_ISSUE_NEXT_REQUEST);
                            } else {
                                /* Let EnabledState handle this */
                                return NOT_HANDLED;
                            }
                            break;
                        default:
                            return NOT_HANDLED;
                    }
                    return HANDLED;
                }
            }

            // Check if there are still outstanding responder requests from any client.
            private boolean hasOutstandingReponderRequests() {
                synchronized (mLock) {
                    for (ClientInfo client : mClients.values()) {
                        if (!client.mResponderRequests.isEmpty()) {
                            return true;
                        }
                    }
                }
                return false;
            }

            /**
             * Representing an outstanding RTT responder state.
             */
            class ResponderEnabledState extends State {
                @Override
                public boolean processMessage(Message msg) {
                    if (DBG) Log.d(TAG, "ResponderEnabledState got " + msg);
                    ClientInfo ci;
                    synchronized (mLock) {
                        ci = mClients.get(msg.replyTo);
                    }
                    int key = msg.arg2;
                    switch(msg.what) {
                        case RttManager.CMD_OP_ENABLE_RESPONDER:
                            // Responder already enabled, simply return the responder config.
                            ci.addResponderRequest(key);
                            ci.reportResponderEnableSucceed(key, mResponderConfig);
                            return HANDLED;
                        case RttManager.CMD_OP_DISABLE_RESPONDER:
                            if (ci != null) {
                                ci.removeResponderRequest(key);
                            }
                            // Only disable responder when there are no outstanding clients.
                            if (!hasOutstandingReponderRequests()) {
                                if (!mWifiNative.disableRttResponder()) {
                                    Log.e(TAG, "disable responder failed");
                                }
                                if (DBG) Log.d(TAG, "mWifiNative.disableRttResponder called");
                                transitionTo(mEnabledState);
                            }
                            return HANDLED;
                        case RttManager.CMD_OP_START_RANGING:
                        case RttManager.CMD_OP_STOP_RANGING:  // fall through
                            // Concurrent initiator and responder role is not supported.
                            replyFailed(msg,
                                    RttManager.REASON_INITIATOR_NOT_ALLOWED_WHEN_RESPONDER_ON,
                                    "Initiator not allowed when responder is turned on");
                            return HANDLED;
                        default:
                            return NOT_HANDLED;
                    }
                }
            }

            /**
             * Returns name of current state.
             */
            String currentState() {
                IState state = getCurrentState();
                return state == null ? "null" : state.getName();
            }
        }

        void replySucceeded(Message msg, Object obj) {
            if (msg.replyTo != null) {
                Message reply = Message.obtain();
                reply.what = RttManager.CMD_OP_SUCCEEDED;
                reply.arg2 = msg.arg2;
                reply.obj = obj;
                try {
                    msg.replyTo.send(reply);
                } catch (RemoteException e) {
                    // There's not much we can do if reply can't be sent!
                }
            } else {
                // locally generated message; doesn't need a reply!
            }
        }

        void replyFailed(Message msg, int reason, String description) {
            Message reply = Message.obtain();
            reply.what = RttManager.CMD_OP_FAILED;
            reply.arg1 = reason;
            reply.arg2 = msg.arg2;

            Bundle bundle = new Bundle();
            bundle.putString(RttManager.DESCRIPTION_KEY, description);
            reply.obj = bundle;

            try {
                if (msg.replyTo != null) {
                    msg.replyTo.send(reply);
                }
            } catch (RemoteException e) {
                // There's not much we can do if reply can't be sent!
            }
        }

        boolean enforcePermissionCheck(Message msg) {
            try {
                mContext.enforcePermission(Manifest.permission.LOCATION_HARDWARE,
                         -1, msg.sendingUid, "LocationRTT");
            } catch (SecurityException e) {
                Log.e(TAG, "UID: " + msg.sendingUid + " has no LOCATION_HARDWARE Permission");
                return false;
            }
            return true;
        }

        @Override
        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
            if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                    != PackageManager.PERMISSION_GRANTED) {
                pw.println("Permission Denial: can't dump RttService from from pid="
                        + Binder.getCallingPid()
                        + ", uid=" + Binder.getCallingUid()
                        + " without permission "
                        + android.Manifest.permission.DUMP);
                return;
            }
            pw.println("current state: " + mStateMachine.currentState());
            pw.println("clients:");
            synchronized (mLock) {
                for (ClientInfo client : mClients.values()) {
                    pw.println("  " + client);
                }
            }
        }

        private WifiNative.RttEventHandler mEventHandler = new WifiNative.RttEventHandler() {
            @Override
            public void onRttResults(RttManager.RttResult[] result) {
                mStateMachine.sendMessage(CMD_RTT_RESPONSE, result);
            }
        };

        RttRequest issueNextRequest() {
            RttRequest request = null;
            while (mRequestQueue.isEmpty() == false) {
                request = mRequestQueue.remove();
                if(request !=  null) {
                    if (mWifiNative.requestRtt(request.params, mEventHandler)) {
                        if (DBG) Log.d(TAG, "Issued next RTT request with key: " + request.key);
                        return request;
                    } else {
                        Log.e(TAG, "Fail to issue key at native layer");
                        request.ci.reportFailed(request,
                                RttManager.REASON_UNSPECIFIED, "Failed to start");
                    }
                }
            }

            /* all requests exhausted */
            if (DBG) Log.d(TAG, "No more requests left");
            return null;
        }
        @Override
        public RttManager.RttCapabilities getRttCapabilities() {
            return mWifiNative.getRttCapabilities();
        }
    }

    private static final String TAG = "RttService";
    RttServiceImpl mImpl;
    private final HandlerThread mHandlerThread;

    public RttService(Context context) {
        super(context);
        mHandlerThread = new HandlerThread("WifiRttService");
        mHandlerThread.start();
        Log.i(TAG, "Creating " + Context.WIFI_RTT_SERVICE);
    }

    @Override
    public void onStart() {
        mImpl = new RttServiceImpl(getContext(),
                mHandlerThread.getLooper(), WifiInjector.getInstance());

        Log.i(TAG, "Starting " + Context.WIFI_RTT_SERVICE);
        publishBinderService(Context.WIFI_RTT_SERVICE, mImpl);
    }

    @Override
    public void onBootPhase(int phase) {
        if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
            Log.i(TAG, "Registering " + Context.WIFI_RTT_SERVICE);
            if (mImpl == null) {
                mImpl = new RttServiceImpl(getContext(),
                        mHandlerThread.getLooper(),  WifiInjector.getInstance());
            }
            mImpl.startService();
        }
    }


}