66import java .io .InputStream ;
77
88import cf .netdex .hidfuzzer .MainActivity ;
9- import cf .netdex .hidfuzzer .util .SUExecute ;
109import eu .chainfire .libsuperuser .Shell ;
1110
1211/**
12+ * Wrapper for HID class for ease of usage
13+ *
1314 * Created by netdex on 1/16/2017.
1415 */
1516
@@ -34,6 +35,10 @@ public void delay(long m) {
3435 }
3536 }
3637
38+ public int test (){
39+ return hid_keyboard ((byte ) 0 , Input .KB .K .VOLUME_UP .c );
40+ }
41+
3742 public int hid_mouse (byte ... offset ) {
3843 return HID .hid_mouse (mSU , mDevMouse , offset );
3944 }
@@ -49,21 +54,26 @@ public int press_keys(byte... keys) {
4954 return ec ;
5055 }
5156
52- /* String to code conversion tables */
57+ /* Begin string to c conversion tables */
5358 private static final String MP_ALPHA = "abcdefghijklmnopqrstuvwxyz" ; // 0x04
5459 private static final String MP_ALPHA_ALT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; // 0x04 SHIFT
5560 private static final String MP_NUM = "1234567890" ; // 0x1E
5661 private static final String MP_NUM_ALT = "!@#$%^&*()" ; // 0x1E SHIFT
5762 private static final String MP_SPEC = " -=[]\\ #;'`,./" ; // 0x2C
5863 private static final String MP_SPEC_ALT = " _+{}| :\" ~<>?" ; // 0x2C SHIFT
64+ private static final String MP_SU_SPEC = "\n " ; // 0X28
5965
60- private static final String [] AP_ATT = {MP_ALPHA , MP_ALPHA_ALT , MP_NUM , MP_NUM_ALT , MP_SPEC , MP_SPEC_ALT };
61- private static final boolean [] AP_SHIFT = {false , true , false , true , false , true };
62- private static final byte [] AP_OFFSET = {0x04 , 0x04 , 0x1E , 0x1E , 0x2C , 0x2C };
66+ private static final String [] AP_ATT = {MP_ALPHA , MP_ALPHA_ALT , MP_NUM , MP_NUM_ALT , MP_SPEC , MP_SPEC_ALT , MP_SU_SPEC };
67+ private static final boolean [] AP_SHIFT = {false , true , false , true , false , true , false };
68+ private static final byte [] AP_OFFSET = {0x04 , 0x04 , 0x1E , 0x1E , 0x2C , 0x2C , 0x28 };
6369
64- public int send_string (String s ) {
65- int ec = 0 ;
66- for (char c : s .toCharArray ()) {
70+ private static final byte [] AP_MAP_CODE = new byte [128 ];
71+ private static final boolean [] AP_MAP_SHIFT = new boolean [128 ];
72+
73+ // build fast conversion tables from human readable data
74+ static {
75+ for (int i = 0 ; i < 128 ; i ++) {
76+ char c = (char ) i ;
6777 boolean shift = false ;
6878 byte code = 0 ;
6979
@@ -77,28 +87,57 @@ public int send_string(String s) {
7787 }
7888 idx ++;
7989 }
80- if (idx == AP_ATT .length )
81- throw new IllegalArgumentException ("Given string contains illegal characters" );
90+ if (idx == AP_ATT .length ) {
91+ AP_MAP_CODE [i ] = -1 ;
92+ } else {
93+ AP_MAP_CODE [i ] = code ;
94+ AP_MAP_SHIFT [i ] = shift ;
95+ }
96+ }
97+ }
98+ /* End string to c conversion tables */
99+
100+ public int send_string (String s ) {
101+ return send_string (s , 0 );
102+ }
82103
83- ec |= hid_keyboard (shift ? Input .Keyboard .ModifierMask .LSHIFT .code : 0 , code );
104+ public int send_string (String s , int d ) {
105+ int ec = 0 ;
106+ char lc = Character .MIN_VALUE ;
107+ for (char c : s .toCharArray ()) {
108+ byte cd = AP_MAP_CODE [(int ) c ];
109+ boolean st = AP_MAP_SHIFT [(int ) c ];
110+ if (cd == -1 )
111+ throw new IllegalArgumentException ("Given string contains illegal characters" );
112+ if (c == lc )
113+ ec |= hid_keyboard ();
114+ ec |= hid_keyboard (st ? Input .KB .M .LSHIFT .c : 0 , cd );
115+ if (d != 0 )
116+ delay (d );
117+ lc = c ;
84118 }
85119 ec |= hid_keyboard ();
86120 return ec ;
87121 }
88122
89- public KeyboardLightListener getKeyboardLightListener (){
123+ public KeyboardLightListener getKeyboardLightListener () {
90124 return mKeyboardLightListener ;
91125 }
92126
93127 public class KeyboardLightListener {
94128 private Process mKeyboardLightProc ;
95129 private InputStream mKeyboardLightStream ;
130+ private int mLastLightState ;
96131
97132 public int start () {
98133 if (mKeyboardLightProc != null )
99- throw new IllegalArgumentException ("Keyboard light proc already running" );
134+ throw new IllegalArgumentException ("KB light proc already running" );
100135
101- mKeyboardLightProc = SUExecute .execute ("cat " + mDevKeyboard );
136+ try {
137+ mKeyboardLightProc = Runtime .getRuntime ().exec ("cat " + mDevKeyboard );
138+ } catch (IOException e ) {
139+ e .printStackTrace ();
140+ }
102141 if (mKeyboardLightProc != null ) {
103142 mKeyboardLightStream = mKeyboardLightProc .getInputStream ();
104143 return 0 ;
@@ -118,23 +157,62 @@ public int start() {
118157 public int read () {
119158 try {
120159 if (mKeyboardLightStream != null )
121- return mKeyboardLightStream .read ();
160+ return mLastLightState = mKeyboardLightStream .read ();
122161 return -1 ;
123162 } catch (IOException e ) {
124163 Log .d (MainActivity .TAG , "Light stream forcibly terminated" );
125164 return -1 ;
126165 }
127166 }
128167
168+ public int available () {
169+ if (mKeyboardLightStream != null ) {
170+ try {
171+ return mKeyboardLightStream .available ();
172+ } catch (IOException e ) {
173+ e .printStackTrace ();
174+ }
175+ }
176+ return -1 ;
177+ }
178+
129179 public void kill () {
130- try {
180+ // HACK don't stare at this for too long, or your eyes will burn out
181+ // Field f = mKeyboardLightProc.getClass().getDeclaredField("pid");
182+ // f.setAccessible(true);
183+ // long pid = f.getLong(mKeyboardLightProc);
184+ // f.setAccessible(false);
185+ // String cmd = "pkill -KILL -P " + pid;
186+ // Log.d("A", cmd);
187+ // mSU.addCommand(cmd, 0, new Shell.OnCommandLineListener() {
188+ // @Override
189+ // public void onCommandResult(int commandCode, int exitCode) {
190+ // Log.d("A", commandCode + " " + exitCode);
191+ // }
192+ //
193+ // @Override
194+ // public void onLine(String line) {
195+ // Log.d("A", line);
196+ // }
197+ // });
198+ if (mKeyboardLightStream != null ) {
199+ try {
200+ mKeyboardLightStream .close ();
201+ } catch (IOException e ) {
202+ e .printStackTrace ();
203+ }
204+ mKeyboardLightStream = null ;
205+ }
206+
207+ // close the stream before killing the process
208+ if (mKeyboardLightProc != null ) {
131209 mKeyboardLightProc .destroy ();
132210 mKeyboardLightProc = null ;
133- mKeyboardLightStream .close ();
134- mKeyboardLightStream = null ;
135- } catch (IOException e ) {
136- e .printStackTrace ();
137211 }
138212 }
213+
214+ public int getLastLightState () {
215+ return mLastLightState ;
216+ }
139217 }
140218}
0 commit comments