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.os.Parcelable;
19 import android.view.MenuItem;
20 import android.content.Intent;
21 import android.content.DialogInterface;
22 import android.widget.TextView;
23 import android.widget.Toast;
24 import android.text.method.ScrollingMovementMethod;
25 import android.util.Log;
26 import android.nfc.NdefMessage;
27 import android.nfc.NdefRecord;
28 import android.nfc.NfcAdapter;
29 
30 public class WpaNfcActivity extends Activity
31 {
32     private static final String TAG = "wpadebug";
33 
byteArrayHex(byte[] a)34     String byteArrayHex(byte[] a) {
35 	StringBuilder sb = new StringBuilder();
36 	for (byte b: a)
37 	    sb.append(String.format("%02x", b));
38 	return sb.toString();
39     }
40 
show_alert(String title, String message)41     private void show_alert(String title, String message)
42     {
43 	AlertDialog.Builder alert = new AlertDialog.Builder(this);
44 	alert.setTitle(title);
45 	alert.setMessage(message);
46 	alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
47 		public void onClick(DialogInterface dialog, int id)
48 		{
49 		    finish();
50 		}
51 	    });
52 	alert.create().show();
53     }
54 
wpaCmd(String cmd)55     private String wpaCmd(String cmd)
56     {
57 	try {
58 	    Log.d(TAG, "Executing wpaCmd: " + cmd);
59 	    Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/mksh-su", "-c", "wpa_cli " + cmd});
60 	    BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
61 	    StringBuffer output = new StringBuffer();
62 	    int read;
63 	    char[] buffer = new char[1024];
64 	    while ((read = reader.read(buffer)) > 0)
65 		output.append(buffer, 0, read);
66 	    reader.close();
67 	    proc.waitFor();
68 	    Log.d(TAG, "External process completed - exitValue " +
69 		  proc.exitValue());
70 	    return output.toString();
71 	} catch (IOException e) {
72 	    show_alert("Could not run external program",
73 		       "Execution of an external program failed. " +
74 		       "Maybe mksh-su was not installed.");
75 	    return null;
76 	} catch (InterruptedException e) {
77 	    throw new RuntimeException(e);
78 	}
79     }
80 
report_tag_read(byte[] payload)81     public boolean report_tag_read(byte[] payload)
82     {
83 	String res = wpaCmd("WPS_NFC_TAG_READ " + byteArrayHex(payload));
84 	if (res == null)
85 	    return false;
86 	if (!res.contains("OK")) {
87 	    Toast.makeText(this, "Failed to report WSC tag read to " +
88 			   "wpa_supplicant", Toast.LENGTH_LONG).show();
89 	} else {
90 	    Toast.makeText(this, "Reported WSC tag read to wpa_supplicant",
91 			   Toast.LENGTH_LONG).show();
92 	}
93 	finish();
94 	return true;
95     }
96 
97     @Override
onCreate(Bundle savedInstanceState)98     public void onCreate(Bundle savedInstanceState)
99     {
100 	super.onCreate(savedInstanceState);
101 
102 	Intent intent = getIntent();
103 	String action = intent.getAction();
104 	Log.d(TAG, "onCreate: action=" + action);
105 
106 	if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
107 	    Log.d(TAG, "NDEF discovered");
108 	    Parcelable[] raw = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
109 	    if (raw != null) {
110 		Log.d(TAG, "NDEF message count: " + raw.length);
111 		NdefMessage[] msgs = new NdefMessage[raw.length];
112 		for (int i = 0; i < raw.length; i++) {
113 		    msgs[i] = (NdefMessage) raw[i];
114 		    NdefRecord rec = msgs[i].getRecords()[0];
115 		    Log.d(TAG, "MIME type: " + rec.toMimeType());
116 		    byte[] a = rec.getPayload();
117 		    Log.d(TAG, "NDEF record: " + byteArrayHex(a));
118 		    if (rec.getTnf() == NdefRecord.TNF_MIME_MEDIA &&
119 			rec.toMimeType().equals("application/vnd/wfa.wsc")) {
120 			Log.d(TAG, "WSC tag read");
121 		    }
122 
123 		    if (!report_tag_read(a))
124 			return;
125 		}
126 	    }
127 	}
128 
129 	finish();
130     }
131 }
132