-
Notifications
You must be signed in to change notification settings - Fork 157
/
ChipsetDetection.java
893 lines (793 loc) · 27.1 KB
/
ChipsetDetection.java
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/**
* Copyright (C) 2011 The Serval Project
*
* This file is part of Serval Software (http://www.servalproject.org)
*
* Serval Software 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.
*
* This source code 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 this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.servalproject.system;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.servalproject.LogActivity;
import org.servalproject.ServalBatPhoneApplication;
import org.servalproject.shell.CommandLog;
import org.servalproject.shell.Shell;
import android.os.Build;
import android.util.Log;
public class ChipsetDetection {
private static final String strMustExist = "exists";
private static final String strMustNotExist = "missing";
private static final String strandroid = "androidversion";
private static final String strCapability = "capability";
private static final String strExperimental = "experimental";
private static final String strNoWirelessExtensions = "nowirelessextensions";
private static final String strNl80211 = "nl80211";
private static final String strAh_on_tag = "#Insert_Adhoc_on";
private static final String strAh_off_tag = "#Insert_Adhoc_off";
private static final String strProduct = "productmatches";
private static final String strNotProduct = "productisnt";
private String logFile;
private String detectPath;
private String edifyPath;
private String edifysrcPath;
private ServalBatPhoneApplication app;
private Chipset wifichipset;
private String manufacturer;
private String brand;
private String model;
private String name;
private ChipsetDetection() {
this.app = ServalBatPhoneApplication.context;
this.logFile = app.coretask.DATA_FILE_PATH + "/var/wifidetect.log";
this.detectPath = app.coretask.DATA_FILE_PATH + "/conf/wifichipsets/";
this.edifyPath = app.coretask.DATA_FILE_PATH + "/conf/adhoc.edify";
this.edifysrcPath = app.coretask.DATA_FILE_PATH
+ "/conf/adhoc.edify.src";
manufacturer = app.coretask.getProp("ro.product.manufacturer");
brand = app.coretask.getProp("ro.product.brand");
model = app.coretask.getProp("ro.product.model");
name = app.coretask.getProp("ro.product.name");
}
private static ChipsetDetection detection;
public static ChipsetDetection getDetection() {
if (detection == null)
detection = new ChipsetDetection();
return detection;
}
private HashMap<String, Boolean> existsTests = new HashMap<String, Boolean>();
// Check if the corresponding file exists
private boolean fileExists(String filename) {
// Check if the specified file exists during wifi chipset detection.
// Record the result in a dictionary or similar structure so that if
// we fail to detect a phone, we can create a bundle of information
// that can be sent back to the Serval Project developers to help them
// add support for the phone.
Boolean result = existsTests.get(filename);
if (result == null) {
result = (new File(filename)).exists();
existsTests.put(filename, result);
}
return result;
}
private void scan(File folder, List<File> results,
Set<String> insmodCommands) {
File files[] = folder.listFiles();
if (files == null)
return;
for (File file : files) {
try {
if (file.isDirectory()) {
scan(file, results, insmodCommands);
} else {
String path = file.getCanonicalPath();
if (path.contains("wifi") || path.endsWith(".ko")) {
results.add(file);
}
// Only look in small files, and stop looking if a file is
// binary
if (insmodCommands != null && file.length() < 16384
&& ((file.getName().endsWith(".so") == false))
&& ((file.getName().endsWith(".ttf") == false))
&& ((file.getName().endsWith(".ogg") == false))
&& ((file.getName().endsWith(".odex") == false))
&& ((file.getName().endsWith(".apk") == false))) {
BufferedReader b = new BufferedReader(new FileReader(
file));
try {
String line = null;
String dmp = null;
while ((line = b.readLine()) != null) {
// Stop looking if the line seems to be binary
if (line.length() > 0
&& (line.charAt(0) > 0x7d || line
.charAt(0) < 0x09)) {
// LogActivity.logMessage("guess", file
// + " seems to be binary", false);
break;
}
if (line.startsWith("DRIVER_MODULE_PATH="))
dmp = line.substring(19);
if (dmp != null
&& line
.startsWith("DRIVER_MODULE_ARG=")) {
insmodCommands.add("insmod " + dmp + " \""
+ line.substring(18) + "\"");
dmp = null;
}
if (line.contains("insmod ")) {
// Ooh, an insmod command.
// Let's see if it is interesting.
insmodCommands.add(line);
}
}
b.close();
} catch (IOException e) {
b.close();
} finally {
b.close();
}
}
}
} catch (IOException e) {
continue;
}
}
}
private List<File> interestingFiles = null;
private List<File> findModules(Set<String> insmodCommands) {
if (interestingFiles == null || insmodCommands != null) {
interestingFiles = new ArrayList<File>();
scan(new File("/system"), interestingFiles, insmodCommands);
scan(new File("/lib"), interestingFiles, insmodCommands);
scan(new File("/wifi"), interestingFiles, insmodCommands);
scan(new File("/etc"), interestingFiles, insmodCommands);
}
return interestingFiles;
}
private final static String BASE_URL = "http://developer.servalproject.org/";
private boolean downloadIfModified(String url, File destination)
throws IOException {
Date modified = null;
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
if (destination.exists()) {
modified = new Date(destination.lastModified());
httpGet.addHeader("If-Modified-Since",
DateUtils.formatDate(modified));
}
try {
Log.v("BatPhone", "Fetching: " + url);
HttpResponse response = httpClient.execute(httpGet, httpContext);
int code = response.getStatusLine().getStatusCode();
Log.v("BatPhone", "Result code: " + code);
switch (code - code % 100) {
case 200:
HttpEntity entity = response.getEntity();
FileOutputStream output = new FileOutputStream(destination);
entity.writeTo(output);
output.close();
Header modifiedHeader = response
.getFirstHeader("Last-Modified");
if (modifiedHeader != null) {
try {
destination.setLastModified(DateUtils.parseDate(
modifiedHeader.getValue()).getTime());
} catch (DateParseException e) {
Log.v("BatPhone", e.toString(), e);
}
}
Log.v("BatPhone", "Saved to " + destination);
return true;
case 300:
Log.v("BatPhone", "Not Changed");
// not changed
return false;
default:
throw new IOException(response.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
throw new IOException(e.toString());
}
}
public boolean downloadNewScripts() {
try {
File f = new File(app.coretask.DATA_FILE_PATH + "/conf/chipset.zip");
if (this.downloadIfModified(BASE_URL + "chipset.zip", f)) {
Log.v("BatPhone", "Extracting archive");
app.coretask.extractZip(new FileInputStream(f),
new File(app.coretask.DATA_FILE_PATH,
"conf/wifichipsets"));
return true;
}
} catch (IOException e) {
Log.e("BatPhone", e.toString(), e);
}
return false;
}
private String getUrl(URL url) throws IOException {
Log.v("BatPhone", "Fetching " + url);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
return new Scanner(in).useDelimiter("\\A").next();
}
private String uploadFile(File f, String name, URL url) throws IOException {
final String boundary = "*****";
Log.v("BatPhone", "Uploading file " + f.getName() + " to " + url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="
+ boundary);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes("--" + boundary + "\n");
out
.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ name + "\"\nContent-Type: text/plain\n\n");
{
FileInputStream in = new FileInputStream(f);
try {
byte buff[] = new byte[4 * 1024];
int read;
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
}
out.writeBytes("\n--" + boundary + "\n");
out.flush();
}
InputStream in = conn.getInputStream();
return new Scanner(in).useDelimiter("\\A").next();
}
public boolean needUpload = false;
private String logName;
private void testLog() {
// PGS - Disabling uploading of logs for now until we make a UI to ask
// the
// user if they are willing, and show them what will be sent.
// try {
// needUpload = true;
// logName = manufacturer + "_" + brand + "_" + model + "_"
// + name;
//
// String result = getUrl(new URL(BASE_URL
// + "upload_v1_exists.php?name=" + logName));
// Log.v("BatPhone", result);
// if (result.equals("Ok."))
// needUpload = false;
// } catch (Exception e) {
// Log.e("BatPhone", e.toString(), e);
// }
}
public void uploadLog() {
if (!app.settings.getBoolean("dataCollection", false))
return;
// PGS - Disabling uploading of logs for now until we make a UI to ask
// the
// user if they are willing, and show them what will be sent.
return;
//
// try {
// testLog();
// if (needUpload) {
// String result = uploadFile(new File(this.logFile), logName,
// new URL(BASE_URL + "upload_v1_log.php"));
// Log.v("BatPhone", result);
// }
// } catch (Exception e) {
// Log.e("BatPhone", e.toString(), e);
// }
}
public Set<Chipset> detected_chipsets = null;
public Chipset getWifiChipset() {
return wifichipset;
}
public String getChipset() {
if (wifichipset == null)
return null;
return wifichipset.chipset;
}
public boolean testAndSetChipset(String value) {
File script = new File(detectPath, value + ".detect");
if (script.exists()) {
Chipset c = new Chipset(script);
if (testForChipset(c)) {
setChipset(c);
return true;
}
}
return false;
}
/* Check if the chipset matches with the available chipsets */
private boolean testForChipset(Chipset chipset) {
// Read
// /data/data/org.servalproject/conf/wifichipsets/"+chipset+".detect"
// and see if we can meet the criteria.
// This method needs to interpret the lines of that file as test
// instructions
// that can do everything that the old big hairy if()else() chain did.
// This largely consists of testing for the existence of files.
// use fileExists() to test for the existence of files so that we can
// generate
// a report for this phone in case it is not supported.
// XXX Stub}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(logFile,
true), 256);
writer.write("trying " + chipset + "\n");
boolean reject = false;
int matches = 0;
chipset.supportedModes.clear();
chipset.detected = false;
chipset.experimental = false;
try {
FileInputStream fstream = new FileInputStream(
chipset.detectScript);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
String strLine;
// Read File Line By Line
while ((strLine = in.readLine()) != null) {
if (strLine.startsWith("#") || strLine.equals(""))
continue;
writer.write("# " + strLine + "\n");
String arChipset[] = strLine.split(" ");
if (arChipset[0].equals(strCapability)) {
for (String mode : arChipset[1].split(",")) {
try {
WifiMode m = WifiMode.valueOf(mode);
if (m != null)
chipset.supportedModes.add(m);
} catch (IllegalArgumentException e) {
}
}
if (arChipset.length >= 3)
chipset.adhocOn = arChipset[2];
if (arChipset.length >= 4)
chipset.adhocOff = arChipset[3];
if (arChipset.length >= 5)
chipset.interfaceUp = arChipset[4];
} else if (arChipset[0].equals(strExperimental)) {
chipset.experimental = true;
} else if (arChipset[0].equals(strNoWirelessExtensions)) {
chipset.noWirelessExtensions = true;
} else if (arChipset[0].equals(strNl80211)) {
chipset.nl80211 = true;
} else {
boolean lineMatch = false;
if (arChipset[0].equals(strMustExist)
|| arChipset[0].equals(strMustNotExist)) {
boolean exist = fileExists(arChipset[1]);
boolean wanted = arChipset[0].equals(strMustExist);
writer.write((exist ? "exists" : "missing") + " "
+ arChipset[1] + "\n");
lineMatch = (exist == wanted);
} else if (arChipset[0].equals(strandroid)) {
int sdkVersion = Build.VERSION.SDK_INT;
writer.write(strandroid + " = "
+ Build.VERSION.SDK_INT + "\n");
int requestedVersion = Integer
.parseInt(arChipset[2]);
if (arChipset[1].indexOf('!') >= 0) {
lineMatch = (sdkVersion != requestedVersion);
} else
lineMatch = ((arChipset[1].indexOf('=') >= 0 && sdkVersion == requestedVersion)
|| (arChipset[1].indexOf('<') >= 0 && sdkVersion < requestedVersion) || (arChipset[1]
.indexOf('>') >= 0 && sdkVersion > requestedVersion));
} else if (arChipset[0].equals(strProduct)) {
writer.write(strProduct + " = "
+ Build.PRODUCT + "\n");
lineMatch = false;
for (int i = 2; i < arChipset.length; i++)
if (Build.PRODUCT.contains(arChipset[i]))
lineMatch = true;
} else if (arChipset[0].equals(strNotProduct)) {
lineMatch = true;
for (int i = 2; i < arChipset.length; i++)
if (Build.PRODUCT.contains(arChipset[i]))
lineMatch = false;
} else {
Log.v("BatPhone", "Unhandled line in " + chipset
+ " detect script " + strLine);
continue;
}
if (lineMatch)
matches++;
else
reject = true;
}
}
in.close();
} catch (IOException e) {
Log.i("BatPhone", e.toString(), e);
writer.write("Exception Caught in testForChipset" + e + "\n");
reject = true;
}
if (reject)
writer.write("isnot " + chipset + "\n");
else {
chipset.detected = true;
Log.i("BatPhone", "identified chipset " + chipset
+ (chipset.experimental ? " (experimental)" : ""));
writer.write("is " + chipset + "\n");
LogActivity.logMessage("detect",
"Detected this handset as a " + chipset, false);
}
writer.close();
return !reject;
} catch (IOException e) {
Log.e("BatPhone", e.toString(), e);
return false;
}
}
private void appendFile(FileOutputStream out, String path)
throws IOException {
DataInputStream input = new DataInputStream(new FileInputStream(path));
String strLineinput;
while ((strLineinput = input.readLine()) != null) {
out.write((strLineinput + "\n").getBytes());
}
input.close();
}
private static int nl80211 = 0;
public boolean hasNl80211() {
if (nl80211 == 0) {
try {
CommandLog c = new CommandLog(app.coretask.DATA_FILE_PATH
+ "/bin/iw list");
Shell.runCommand(c);
if (c.exitCode() == 0)
nl80211 = 1;
else
nl80211 = -1;
} catch (Exception e) {
Log.e("ChipsetDetection", e.getMessage(), e);
}
}
return nl80211 == 1;
}
public void inventSupport() {
// Make a wild guess for a script that MIGHT work
// Start with list of kernel modules
// XXX we should search for files containing insmod to see if there are
// any parameters that might be needed (as is the case on the IDEOS
// U8150)
Set<String> insmodCommands = new HashSet<String>();
List<String> knownModules = getList(this.detectPath
+ "known-wifi.modules");
List<String> knownNonModules = getList(this.detectPath
+ "non-wifi.modules");
List<File> candidatemodules = findModules(insmodCommands);
List<File> modules = new ArrayList<File>();
int guesscount = 0;
boolean nl80211Support = hasNl80211();
// First, let's just try only known modules.
// XXX - These are the wrong search methods
for (File module : candidatemodules) {
if (module.getName().endsWith(".ko"))
if (!knownNonModules.contains(module.getName()))
if (knownModules.contains(module.getName()))
modules.add(module);
}
if (modules.isEmpty()) {
// We didn't find any on our strict traversal, so try again
// allowing any non-black-listed modules
for (File module : candidatemodules) {
if (module.getName().endsWith(".ko"))
if (!knownNonModules.contains(module.getName()))
modules.add(module);
}
}
if (modules.isEmpty()) {
// Blast. Couldn't find any modules.
// Well, let's just try ifconfig and iwconfig anyway, as they
// might just work.
}
// Now that we have the list of modules, we could have a look to see
// if there are any sample insmod commands
// that we can find in any system files for clues on what parameters
// to pass when loading the module, e.g.,
// any firmware blobs or nvram.txt or other options.
// XXX - Rather obviously we have not implemented this yet.
LogActivity.logErase("guess");
String profilename = "failed";
for (File m : modules) {
String path = m.getPath();
insmodCommands.add("insmod " + path + " \"\"");
}
for (String s : insmodCommands) {
String module = null;
String args = null;
String modname = "noidea";
int i;
i = s.indexOf("insmod ");
if (i == -1)
continue;
i += 7;
module = getNextShellArg(s.substring(i));
i += module.length() + 1;
if (i < s.length())
args = getNextShellArg(s.substring(i));
else
args = "\"\"";
if (args.charAt(0) != '\"')
args = "\"" + args + "\"";
modname = module;
if (modname.lastIndexOf(".") > -1)
modname = modname.substring(1, modname.lastIndexOf("."));
if (modname.lastIndexOf("/") > -1)
modname = modname.substring(1 + modname.lastIndexOf("/"));
guesscount++;
profilename = "guess-" + guesscount + "-" + modname + "-"
+ args.length();
// Now write out a detect script for this device.
// Mark it experimental because we can't be sure that it will be any
// good. This means that users will have to actively choose it from
// the
// wifi settings menu. We could offer it if no non-experimental
// chipsets match, but that is best done as a general
// policy in the way the chipset selection works.
BufferedWriter writer;
File detectFile = new File(this.detectPath
+ profilename + ".detect");
try {
writer = new BufferedWriter(new FileWriter(detectFile, false),
256);
writer.write(strCapability + " Adhoc " + profilename
+ ".adhoc.edify " + profilename + ".off.edify " +
(nl80211Support ? "iw" : "iwconfig") + ".adhoc.edify\n");
writer.write(strExperimental + "\n");
if (nl80211Support)
writer.write(strNl80211 + "\n" +
strNoWirelessExtensions + "\n");
if (module.contains("/")) {
// XXX We have a problem if we don't know the full path to
// the module
// for ensuring specificity for choosing this option.
// Will think about a nice solution later.
writer.write("exists " + module + "\n");
}
writer.close();
} catch (IOException e) {
Log.e("BatPhone", e.toString(), e);
}
// The actual edify script consists of the insmod commands
// Thus this code does not work with unusual chipsets like the
// tiwlan drivers that use
// funny configuration commands. Oh well. One day we might add some
// cleverness for that.
try {
writer = new BufferedWriter(new FileWriter(this.detectPath
+ profilename + ".adhoc.edify", false), 256);
// Write out edify command to load the module
writer.write("module_loaded(\"" + modname
+ "\") || log(insmod(\"" + module + "\"," + args
+ "),\"Loading " + module + " module\");\n");
writer.close();
} catch (IOException e) {
Log.e("BatPhone", e.toString(), e);
}
// Finally to turn off wifi let's just unload all the modules we
// loaded earlier.
// Crude but fast and effective.
try {
writer = new BufferedWriter(new FileWriter(this.detectPath
+ profilename + ".off.edify", false), 256);
// Write out edify command to load the module
writer.write("module_loaded(\"" + modname + "\") && rmmod(\""
+ modname + "\");\n");
writer.close();
} catch (IOException e) {
Log.e("BatPhone", e.toString(), e);
}
Chipset c = new Chipset(detectFile);
if (this.testForChipset(c))
this.detected_chipsets.add(c);
LogActivity
.logMessage("guess", "Creating best-guess support scripts "
+ profilename + " based on kernel module "
+ modname + ".", false);
}
}
private String getNextShellArg(String s) {
int i = 0;
boolean quoteMode = false;
boolean escMode = false;
// Skip leading white space
while (i < s.length() && s.charAt(i) <= ' ')
i++;
// Get arg
while (i < s.length()) {
if (escMode)
escMode = false;
if (quoteMode) {
if (s.charAt(i) == '"')
quoteMode = false;
else if (s.charAt(i) == '\\')
escMode = true;
} else if (s.charAt(i) <= ' ') {
// End of arg
return s.substring(0, i);
} else if (s.charAt(i) == '\"')
quoteMode = true;
else if (s.charAt(i) == '\\')
escMode = true;
i++;
}
// No word breaks, so return whole thing
return s;
}
public static List<String> getList(String filename) {
// Read lines from file into a list
List<String> l = new ArrayList<String>();
String line;
try {
BufferedReader f = new BufferedReader(new FileReader(filename));
while ((line = f.readLine()) != null) {
l.add(line);
}
f.close();
} catch (IOException e) {
Log.e("BatPhone", e.toString(), e);
}
return l;
}
private void logMore() {
// log other interesting modules/files
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(logFile,
true), 256);
writer.write("\nHandset Type;\n");
writer.write("Manufacturer: " + manufacturer + "\n");
writer.write("Brand: " + brand + "\n");
writer.write("Model: " + model + "\n");
writer.write("Name: " + name + "\n");
writer.write("Software Version: " + app.getVersionName() + "\n");
writer.write("Android Version: " + Build.VERSION.RELEASE + " (API "
+ Build.VERSION.SDK_INT + ")\n");
writer.write("Kernel Version: " + app.coretask.getKernelVersion()
+ "\n");
writer.write("\nInteresting modules;\n");
for (File path : findModules(null)) {
writer.write(path.getCanonicalPath() + "\n");
}
writer.close();
} catch (IOException e) {
Log.e("BatPhone", e.toString(), e);
}
}
public File getAdhocAttemptFile(Chipset chipset) {
return new File(app.coretask.DATA_FILE_PATH + "/var/attempt_"
+ chipset.chipset);
}
// set chipset configuration
public void setChipset(Chipset chipset) {
if (chipset == null) {
chipset = new Chipset();
if (detected_chipsets == null || detected_chipsets.size() == 0)
chipset.chipset = "Unsupported - " + brand + " " + model + " "
+ name;
chipset.unknown = true;
}
// add support for modes via SDK if available
if (!chipset.supportedModes.contains(WifiMode.Ap)
&& WifiApControl.isApSupported())
chipset.supportedModes.add(WifiMode.Ap);
if (!chipset.supportedModes.contains(WifiMode.Client))
chipset.supportedModes.add(WifiMode.Client);
if (!chipset.supportedModes.contains(WifiMode.Off))
chipset.supportedModes.add(WifiMode.Off);
// make sure we have root permission for adhoc support
if (chipset.supportedModes.contains(WifiMode.Adhoc)) {
if (getAdhocAttemptFile(chipset).exists()) {
chipset.supportedModes.remove(WifiMode.Adhoc);
Log.v("BatPhone",
"Adhoc mode has previously failed and cannot be supported.");
} else if (!app.coretask.hasRootPermission()) {
chipset.supportedModes.remove(WifiMode.Adhoc);
Log.v("BatPhone",
"Unable to support adhoc mode without root permission");
}
}
wifichipset = chipset;
if (chipset.supportedModes.contains(WifiMode.Adhoc)) {
try {
FileOutputStream out = new FileOutputStream(edifyPath);
FileInputStream fstream = new FileInputStream(edifysrcPath);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
String strLine;
// Read File Line By Line
while ((strLine = in.readLine()) != null) {
if (strLine.startsWith(strAh_on_tag)) {
if (chipset.adhocOn != null)
appendFile(out, detectPath + chipset.adhocOn);
if (chipset.interfaceUp != null)
appendFile(out, detectPath + chipset.interfaceUp);
} else if (strLine.startsWith(strAh_off_tag)) {
if (chipset.adhocOff != null)
appendFile(out, detectPath + chipset.adhocOff);
} else
out.write((strLine + "\n").getBytes());
}
in.close();
out.close();
} catch (IOException exc) {
Log.e("Exception caught at set_Adhoc_mode", exc.getMessage(),
exc);
}
}
}
public boolean isModeSupported(WifiMode mode) {
if (mode == null)
return true;
if (wifichipset == null)
return false;
return wifichipset.supportedModes.contains(mode);
}
public Set<Chipset> getDetectedChipsets() {
if (detected_chipsets == null) {
detected_chipsets = new TreeSet<Chipset>();
File detectScripts = new File(detectPath);
if (!detectScripts.isDirectory())
return null;
for (File script : detectScripts.listFiles()) {
if (!script.getName().endsWith(".detect"))
continue;
Chipset c = new Chipset(script);
if (testForChipset(c)) {
detected_chipsets.add(c);
}
}
}
return detected_chipsets;
}
}