]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpadebug/src/w1/fi/wpadebug/MainActivity.java
wpadebug: Add generic control interface command mechanism
[thirdparty/hostap.git] / wpadebug / src / w1 / fi / wpadebug / MainActivity.java
CommitLineData
4bb4f7cd
JM
1/*
2 * wpadebug - wpa_supplicant and Wi-Fi debugging app for Android
3 * Copyright (c) 2013, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9package w1.fi.wpadebug;
10
11import java.io.BufferedReader;
12import java.io.InputStreamReader;
13import java.io.IOException;
14
15import android.app.Activity;
16import android.app.AlertDialog;
17import android.os.Bundle;
18import android.view.View;
19import android.content.Intent;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.widget.EditText;
e32547ef 23import android.widget.Toast;
4bb4f7cd
JM
24import android.util.Log;
25import android.net.wifi.WifiManager;
26import android.net.wifi.WifiInfo;
27import android.net.wifi.WifiConfiguration;
e32547ef
JM
28import android.nfc.NdefMessage;
29import android.nfc.NdefRecord;
30import android.nfc.NfcAdapter;
4bb4f7cd
JM
31
32public class MainActivity extends Activity
33{
34 public final static String EXTRA_MESSAGE = "w1.fi.wpadebug.MESSAGE";
35 private static final String TAG = "wpadebug";
36
37 @Override
38 public void onCreate(Bundle savedInstanceState)
39 {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.main);
42 }
43
cb54718c 44 public void runCommands(View view)
4bb4f7cd 45 {
cb54718c 46 Intent intent = new Intent(this, CommandListActivity.class);
4bb4f7cd
JM
47 startActivity(intent);
48 }
49
a9008e43
JM
50 public void runWpaCommands(View view)
51 {
52 Intent intent = new Intent(this, WpaCommandListActivity.class);
53 startActivity(intent);
54 }
55
4bb4f7cd
JM
56 public void runWpaCliCmd(View view)
57 {
58 Intent intent = new Intent(this, DisplayMessageActivity.class);
59 EditText editText = (EditText) findViewById(R.id.edit_cmd);
60 String cmd = editText.getText().toString();
61 if (cmd.trim().length() == 0) {
62 show_alert("wpa_cli command", "Invalid command");
63 return;
64 }
65 wpaCmd(view, cmd);
66 }
67
4bb4f7cd
JM
68 public void wpaLogLevelInfo(View view)
69 {
70 wpaCmd(view, "LOG_LEVEL INFO 1");
71 }
72
73 public void wpaLogLevelDebug(View view)
74 {
75 wpaCmd(view, "LOG_LEVEL DEBUG 1");
76 }
77
78 public void wpaLogLevelExcessive(View view)
79 {
80 wpaCmd(view, "LOG_LEVEL EXCESSIVE 1");
81 }
82
83 private void wpaCmd(View view, String cmd)
84 {
85 Intent intent = new Intent(this, DisplayMessageActivity.class);
86 String message = run("wpa_cli " + cmd);
87 if (message == null)
88 return;
89 intent.putExtra(EXTRA_MESSAGE, message);
90 startActivity(intent);
91 }
92
93 private String run(String cmd)
94 {
95 try {
96 Log.d(TAG, "Running external process: " + cmd);
97 Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/mksh-su", "-c", cmd});
98 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
99 StringBuffer output = new StringBuffer();
100 int read;
101 char[] buffer = new char[1024];
102 while ((read = reader.read(buffer)) > 0)
103 output.append(buffer, 0, read);
104 reader.close();
105 proc.waitFor();
106 Log.d(TAG, "External process completed - exitValue " +
107 proc.exitValue());
108 return output.toString();
109 } catch (IOException e) {
110 show_alert("Could not run external program",
111 "Execution of an external program failed. " +
112 "Maybe mksh-su was not installed.");
113 return null;
114 } catch (InterruptedException e) {
115 throw new RuntimeException(e);
116 }
117 }
118
119 private void show_alert(String title, String message)
120 {
121 AlertDialog.Builder alert = new AlertDialog.Builder(this);
122 alert.setTitle(title);
123 alert.setMessage(message);
124 alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
125 public void onClick(DialogInterface dialog, int id)
126 {
127 }
128 });
129 alert.create().show();
130 }
131
132 public void wifiManagerInfo(View view)
133 {
134 Intent intent = new Intent(this, DisplayMessageActivity.class);
135 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
136 String message = "WifiState: " + manager.getWifiState() + "\n" +
137 "WifiEnabled: " + manager.isWifiEnabled() + "\n" +
138 "pingSupplicant: " + manager.pingSupplicant() + "\n" +
139 "DhcpInfo: " + manager.getDhcpInfo().toString() + "\n";
140 intent.putExtra(EXTRA_MESSAGE, message);
141 startActivity(intent);
142 }
143
144 public void wifiInfo(View view)
145 {
146 Intent intent = new Intent(this, DisplayMessageActivity.class);
147 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
148 WifiInfo wifi = manager.getConnectionInfo();
149 String message = wifi.toString() + "\n" + wifi.getSupplicantState();
150 intent.putExtra(EXTRA_MESSAGE, message);
151 startActivity(intent);
152 }
153
154 public void wifiConfiguredNetworks(View view)
155 {
156 Intent intent = new Intent(this, DisplayMessageActivity.class);
157 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
158 StringBuilder sb = new StringBuilder();
159 for (WifiConfiguration n: manager.getConfiguredNetworks())
160 sb.append(n.toString() + "\n");
161 intent.putExtra(EXTRA_MESSAGE, sb.toString());
162 startActivity(intent);
163 }
e32547ef
JM
164
165 public void nfcWpsHandoverRequest(View view)
166 {
167 NfcAdapter nfc;
168 nfc = NfcAdapter.getDefaultAdapter(this);
169 if (nfc == null) {
170 Toast.makeText(this, "NFC is not available",
171 Toast.LENGTH_LONG).show();
172 return;
173 }
174
175 NdefMessage msg;
176 msg = new NdefMessage(new NdefRecord[] {
177 NdefRecord.createMime("application/vnd.wfa.wsc",
178 new byte[0])
179 });
180
181 nfc.setNdefPushMessage(msg, this);
182 Toast.makeText(this, "NFC push message (WSC) configured",
183 Toast.LENGTH_LONG).show();
184 }
4bb4f7cd 185}