aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/network/WpaCli.java
blob: cdaba3993961a79abe416b7bdced1e11691436ad (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
//
// Copyright 2017, 2018 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.network;

import fil.libre.repwifiapp.helpers.Logger;
import fil.libre.repwifiapp.helpers.RootCommand;

public abstract class WpaCli {

    private static final int SCAN_WAIT_INTERVAL = 4;
    private static final String BASE_COMMAND = "wpa_cli -p" + WpaSupplicant.SOCKET_DIR + " -P"
                    + WpaSupplicant.PID_FILE + " -i" + WpaSupplicant.INTERFACE_NAME;

    public static String createNetworkGetId() {

        try {

            RootCommand su = new RootCommand(BASE_COMMAND + " add_network");
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out == null || out.trim().equals("")) {
                    return null;
                } else {
                    return out.replace("\n", "");
                }
            } else {
                return null;
            }

        } catch (Exception e) {
            Logger.logError("Error while creating network", e);
            return null;
        }

    }

    public static boolean setNetworkSSID(String ssid, String networkID) {

        try {

            // needs root (wpa_cli)
            return executeCmd(BASE_COMMAND + " set_network " + networkID + " ssid '\""
                            + escapeSingleQuotes(ssid) + "\"'");

        } catch (Exception e) {
            Logger.logError("Error while setting network SSID", e);
            return false;
        }

    }

    public static boolean setNetworkPSK(AccessPointInfo info, String networkID) {

        try {

            // needs root (wpa_cli)
            String cmdSetPass = null;   
            if (info.needsPassword()) {
                cmdSetPass = BASE_COMMAND + " set_network " + networkID + " psk '\""
                                + escapeSingleQuotes(info.getPassword()) + "\"'";
            } else {
                cmdSetPass = BASE_COMMAND + " set_network " + networkID + " key_mgmt NONE";
            }

            return executeCmd(cmdSetPass);

        } catch (Exception e) {
            Logger.logError("Error while setting network PSK", e);
            return false;
        }

    }

    public static boolean setNetworkScanSSID(String networkID) {

        try {

            // needs root (wpa_cli)
            return executeCmd(BASE_COMMAND + " set_network " + networkID + " scan_ssid 1");

        } catch (Exception e) {
            Logger.logError("Error while setting network SSID", e);
            return false;
        }
    }

    public static boolean selectNetwork(String networkID) {

        try {

            // needs root (wpa_cli)
            return executeCmd(BASE_COMMAND + " select_network " + networkID);

        } catch (Exception e) {
            Logger.logError("Error while selecting network", e);
            return false;
        }

    }

    public static boolean enableNetwork(String networkID) {

        try {

            // needs root (wpa_cli)
            return executeCmd(BASE_COMMAND + " enable_network " + networkID);

        } catch (Exception e) {
            Logger.logError("Error while enabling network", e);
            return false;
        }

    }

    public static boolean scanNetworks() {

        // needs root (for wpa_supplicant and wpa_cli)
        return RootCommand.executeRootCmd(BASE_COMMAND
                        + " scan; if [ $? -ne 0 ]; then exit 1; fi; sleep " + SCAN_WAIT_INTERVAL);

    }

    public static String getScanResults() {

        try {

            RootCommand su = new RootCommand(BASE_COMMAND
                            + " scan_results; if [ $? -ne 0 ]; then exit 1; fi ");
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out == null || out.trim().equals("")) {
                    return null;
                } else {
                    return out;
                }
            } else {
                return null;
            }

        } catch (Exception e) {
            Logger.logError("Error while executing wpa_cli status", e);
            return null;
        }

    }

    /***
     * returns null if unable to determine connection status for any reason.
     */
    public static ConnectionStatus getConnectionStatus() {

        Logger.logDebug("called getConnecitonStatus()");
        if (!WpaSupplicant.isRunning()) {
            // wpa_supplicant is not running.
            // unable to determine status.
            Logger.logDebug("wpa not running, cannot get connection status.");
            return null;

        }

        try {

            RootCommand su = new RootCommand(BASE_COMMAND + " status");
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out == null || out.trim().equals("")) {
                    return null;
                } else {
                    return ConnectionStatus.parseWpaCliOutput(out);
                }
            } else {
                return null;
            }

        } catch (Exception e) {
            Logger.logError("Error while executing wpa_cli status", e);
            return null;
        }

    }

    public static boolean disconnect() {

        // needs root (for wpa_cli)

        if (!WpaSupplicant.isRunning()) {
            return true;
        }

        try {

            return executeCmd(BASE_COMMAND + " disconnect");

        } catch (Exception e) {
            Logger.logError("Error while enabling network", e);
            return false;
        }
    }

    public static boolean terminateSupplicant() {

        if (!WpaSupplicant.isRunning()) {
            return true;
        }
        try {

            return executeCmd(BASE_COMMAND + " terminate");

        } catch (Exception e) {
            Logger.logError("Error while enabling network", e);
            return false;
        }

    }

    private static boolean executeCmd(String cmdTxt) throws Exception {

        RootCommand su = new RootCommand(cmdTxt);
        if (su.execute() == 0) {
            String out = su.getOutput();
            if (out != null && out.trim().replace("\n", "").equals("OK")) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    private static String escapeSingleQuotes(String target) {

        
        if (target == null)
            return "";

        return target.replace("'", "'\"'\"'");
    }

}