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 9 package w1.fi.wpadebug; 10 11 import java.io.BufferedReader; 12 import java.io.InputStreamReader; 13 import java.io.IOException; 14 15 import android.app.Activity; 16 import android.app.AlertDialog; 17 import android.os.Bundle; 18 import android.view.View; 19 import android.content.Intent; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.widget.EditText; 23 import android.widget.Toast; 24 import android.util.Log; 25 import android.net.wifi.WifiManager; 26 import android.net.wifi.WifiInfo; 27 import android.net.wifi.WifiConfiguration; 28 import android.nfc.NdefMessage; 29 import android.nfc.NdefRecord; 30 import android.nfc.NfcAdapter; 31 32 public 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 onCreate(Bundle savedInstanceState)38 public void onCreate(Bundle savedInstanceState) 39 { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.main); 42 } 43 runCommands(View view)44 public void runCommands(View view) 45 { 46 Intent intent = new Intent(this, CommandListActivity.class); 47 startActivity(intent); 48 } 49 runQrScan(View view)50 public void runQrScan(View view) 51 { 52 Intent intent = new Intent(this, QrCodeScannerActivity.class); 53 startActivity(intent); 54 } 55 runQrInput(View view)56 public void runQrInput(View view) 57 { 58 Intent intent = new Intent(this, InputUri.class); 59 startActivity(intent); 60 } 61 runQrDisplay(View view)62 public void runQrDisplay(View view) 63 { 64 Intent intent = new Intent(this, QrCodeDisplayActivity.class); 65 startActivity(intent); 66 } 67 runWpaCommands(View view)68 public void runWpaCommands(View view) 69 { 70 Intent intent = new Intent(this, WpaCommandListActivity.class); 71 startActivity(intent); 72 } 73 runWpaCredentials(View view)74 public void runWpaCredentials(View view) 75 { 76 Intent intent = new Intent(this, WpaCredActivity.class); 77 startActivity(intent); 78 } 79 runWpaCliCmd(View view)80 public void runWpaCliCmd(View view) 81 { 82 Intent intent = new Intent(this, DisplayMessageActivity.class); 83 EditText editText = (EditText) findViewById(R.id.edit_cmd); 84 String cmd = editText.getText().toString(); 85 if (cmd.trim().length() == 0) { 86 show_alert("wpa_cli command", "Invalid command"); 87 return; 88 } 89 wpaCmd(view, cmd); 90 } 91 wpaLogLevelInfo(View view)92 public void wpaLogLevelInfo(View view) 93 { 94 wpaCmd(view, "LOG_LEVEL INFO 1"); 95 } 96 wpaLogLevelDebug(View view)97 public void wpaLogLevelDebug(View view) 98 { 99 wpaCmd(view, "LOG_LEVEL DEBUG 1"); 100 } 101 wpaLogLevelExcessive(View view)102 public void wpaLogLevelExcessive(View view) 103 { 104 wpaCmd(view, "LOG_LEVEL EXCESSIVE 1"); 105 } 106 wpaCmd(View view, String cmd)107 private void wpaCmd(View view, String cmd) 108 { 109 Intent intent = new Intent(this, DisplayMessageActivity.class); 110 String message = run("wpa_cli " + cmd); 111 if (message == null) 112 return; 113 intent.putExtra(EXTRA_MESSAGE, message); 114 startActivity(intent); 115 } 116 run(String cmd)117 private String run(String cmd) 118 { 119 try { 120 Log.d(TAG, "Running external process: " + cmd); 121 Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/mksh-su", "-c", cmd}); 122 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); 123 StringBuffer output = new StringBuffer(); 124 int read; 125 char[] buffer = new char[1024]; 126 while ((read = reader.read(buffer)) > 0) 127 output.append(buffer, 0, read); 128 reader.close(); 129 proc.waitFor(); 130 Log.d(TAG, "External process completed - exitValue " + 131 proc.exitValue()); 132 return output.toString(); 133 } catch (IOException e) { 134 show_alert("Could not run external program", 135 "Execution of an external program failed. " + 136 "Maybe mksh-su was not installed."); 137 return null; 138 } catch (InterruptedException e) { 139 throw new RuntimeException(e); 140 } 141 } 142 show_alert(String title, String message)143 private void show_alert(String title, String message) 144 { 145 AlertDialog.Builder alert = new AlertDialog.Builder(this); 146 alert.setTitle(title); 147 alert.setMessage(message); 148 alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 149 public void onClick(DialogInterface dialog, int id) 150 { 151 } 152 }); 153 alert.create().show(); 154 } 155 wifiManagerInfo(View view)156 public void wifiManagerInfo(View view) 157 { 158 Intent intent = new Intent(this, DisplayMessageActivity.class); 159 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 160 String message = "WifiState: " + manager.getWifiState() + "\n" + 161 "WifiEnabled: " + manager.isWifiEnabled() + "\n" + 162 "pingSupplicant: " + manager.pingSupplicant() + "\n" + 163 "DhcpInfo: " + manager.getDhcpInfo().toString() + "\n"; 164 intent.putExtra(EXTRA_MESSAGE, message); 165 startActivity(intent); 166 } 167 wifiInfo(View view)168 public void wifiInfo(View view) 169 { 170 Intent intent = new Intent(this, DisplayMessageActivity.class); 171 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 172 WifiInfo wifi = manager.getConnectionInfo(); 173 String message = wifi.toString() + "\n" + wifi.getSupplicantState(); 174 intent.putExtra(EXTRA_MESSAGE, message); 175 startActivity(intent); 176 } 177 wifiConfiguredNetworks(View view)178 public void wifiConfiguredNetworks(View view) 179 { 180 Intent intent = new Intent(this, DisplayMessageActivity.class); 181 WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 182 StringBuilder sb = new StringBuilder(); 183 for (WifiConfiguration n: manager.getConfiguredNetworks()) 184 sb.append(n.toString() + "\n"); 185 intent.putExtra(EXTRA_MESSAGE, sb.toString()); 186 startActivity(intent); 187 } 188 nfcWpsHandoverRequest(View view)189 public void nfcWpsHandoverRequest(View view) 190 { 191 NfcAdapter nfc; 192 nfc = NfcAdapter.getDefaultAdapter(this); 193 if (nfc == null) { 194 Toast.makeText(this, "NFC is not available", 195 Toast.LENGTH_LONG).show(); 196 return; 197 } 198 199 NdefMessage msg; 200 msg = new NdefMessage(new NdefRecord[] { 201 NdefRecord.createMime("application/vnd.wfa.wsc", 202 new byte[0]) 203 }); 204 205 nfc.setNdefPushMessage(msg, this); 206 Toast.makeText(this, "NFC push message (WSC) configured", 207 Toast.LENGTH_LONG).show(); 208 } 209 } 210