WificondControl.java revision 70603901b67c48202ecbb1818e59d487bbcceeda
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.server.wifi;
18
19import android.net.wifi.IApInterface;
20import android.net.wifi.IClientInterface;
21import android.net.wifi.IWificond;
22import android.os.Binder;
23import android.os.RemoteException;
24import android.util.Log;
25
26/**
27 * This class provides methods for WifiNative to send control commands to wificond.
28 * NOTE: This class should only be used from WifiNative.
29 */
30public class WificondControl {
31
32    private static final String TAG = "WificondControl";
33    private IWificond mWificond;
34    private IClientInterface mClientInterface;
35    private IApInterface mApInterface;
36    private WifiInjector mWifiInjector;
37
38    WificondControl(WifiInjector wifiInjector) {
39        mWifiInjector = wifiInjector;
40    }
41
42    /**
43    * Setup driver for client mode via wificond.
44    * @return An IClientInterface as wificond client interface binder handler.
45    * Returns null on failure.
46    */
47    public IClientInterface setupDriverForClientMode() {
48        mWificond = mWifiInjector.makeWificond();
49        if (mWificond == null) {
50            Log.e(TAG, "Failed to get reference to wificond");
51            return null;
52        }
53
54        IClientInterface clientInterface = null;
55        try {
56            clientInterface = mWificond.createClientInterface();
57        } catch (RemoteException e1) {
58            Log.e(TAG, "Failed to get IClientInterface due to remote exception");
59            return null;
60        }
61
62        if (clientInterface == null) {
63            Log.e(TAG, "Could not get IClientInterface instance from wificond");
64            return null;
65        }
66        Binder.allowBlocking(clientInterface.asBinder());
67
68        // Refresh Handlers
69        mClientInterface = clientInterface;
70
71        return clientInterface;
72    }
73
74    /**
75    * Setup driver for softAp mode via wificond.
76    * @return An IApInterface as wificond Ap interface binder handler.
77    * Returns null on failure.
78    */
79    public IApInterface setupDriverForSoftApMode() {
80        mWificond = mWifiInjector.makeWificond();
81        if (mWificond == null) {
82            Log.e(TAG, "Failed to get reference to wificond");
83            return null;
84        }
85
86        IApInterface apInterface = null;
87        try {
88            apInterface = mWificond.createApInterface();
89        } catch (RemoteException e1) {
90            Log.e(TAG, "Failed to get IApInterface due to remote exception");
91            return null;
92        }
93
94        if (apInterface == null) {
95            Log.e(TAG, "Could not get IApInterface instance from wificond");
96            return null;
97        }
98        Binder.allowBlocking(apInterface.asBinder());
99
100        // Refresh Handlers
101        mApInterface = apInterface;
102
103        return apInterface;
104    }
105
106    /**
107    * Teardown all interfaces configured in wificond.
108    * @return Returns true on success.
109    */
110    public boolean tearDownInterfaces() {
111        // Explicitly refresh the wificodn handler because |tearDownInterfaces()|
112        // could be used to cleanup before we setup any interfaces.
113        mWificond = mWifiInjector.makeWificond();
114        if (mWificond == null) {
115            Log.e(TAG, "Failed to get reference to wificond");
116            return false;
117        }
118
119        try {
120            mWificond.tearDownInterfaces();
121            return true;
122        } catch (RemoteException e) {
123            Log.e(TAG, "Failed to tear down interfaces due to remote exception");
124        }
125        return false;
126    }
127
128    /**
129    * Disable wpa_supplicant via wificond.
130    * @return Returns true on success.
131    */
132    public boolean disableSupplicant() {
133        if (mClientInterface == null) {
134            Log.e(TAG, "No valid wificond client interface handler");
135            return false;
136        }
137        try {
138            return mClientInterface.disableSupplicant();
139        } catch (RemoteException e) {
140            Log.e(TAG, "Failed to disable supplicant due to remote exception");
141        }
142        return false;
143    }
144
145    /**
146    * Enable wpa_supplicant via wificond.
147    * @return Returns true on success.
148    */
149    public boolean enableSupplicant() {
150        if (mClientInterface == null) {
151            Log.e(TAG, "No valid wificond client interface handler");
152            return false;
153        }
154
155        try {
156            return mClientInterface.enableSupplicant();
157        } catch (RemoteException e) {
158            Log.e(TAG, "Failed to enable supplicant due to remote exception");
159        }
160        return false;
161    }
162}
163