aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/Engine4p2.java
blob: 12d8da80522de600656314fb4a40ea8b2e0cf07e (plain)
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
//
// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp 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.
// 
// RepWifiApp 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 RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.helpers;

import fil.libre.repwifiapp.Commons;


public class Engine4p2 extends Engine{

	@Override
	protected String getCmdWpaStart(){
		return "wpa_supplicant -B -dd -i" + Commons.INTERFACE_NAME + " -C\"" + Commons.SOCKET_DIR + "\" -c\"" + Commons.WPA_CONF + "\" -P\"" + Commons.PID_FILE + "\"";
	}
	
	public boolean loadModules(){
		try {
			//TODO
			//implement kernel modules loading
			return true;
		} catch (Exception e) {
			Utils.logError("Error while loading kernel modules",e);
			return false;
		}
	}
	
	@Override
	public boolean connect(AccessPointInfo info){
		
		killPreviousConnections();
				
		if (info == null){
			Utils.logDebug("Engine's connect() received a null AccessPointInfo");
			return false;
		}
		
		if (! createConfigFile(info)){
			return false;
		}
		
		//launch wpa_supplicant specifying our custom configuration and the socket file
		if (! executeRootCmd(getCmdWpaStart())){
			Utils.logError("wpa_supplicant connection command failed.");
			return false;
		}

		//negotiate DHCP lease
		if (!runDhcpcd()){
			return false;
		}

		//set DNS's
		if (! executeRootCmd("setprop net.dns1 " + DNS1)){
			Utils.logError("setting dns1 failed");
			return false;
		}

		if (! executeRootCmd("setprop net.dns2 " + DNS2)){
			Utils.logError("setting dns2 failed");
			return false;
		}

		//TODO
		//implement wpa_cli command to query wpa_supplicant's state
		//in order to confirm that connection was successful.

		return true;
		
	}
	
	private boolean createConfigFile(AccessPointInfo info){
		
		try {
						
			if (! deleteFileIfExists(Commons.WPA_CONF)){
				Utils.logError("Unable to remove wpa_supplicant.conf before writing it.");
				return false;				
			}
			
			String configText = "ctrl_interface=DIR=" + Commons.SOCKET_DIR + "\n" +
							"update_config=1\n" +
							"network={\n"+
							"	ssid=\"" + info.getSSID() + "\"\n";
						
			if (info.needsPassword()){
				configText += "	psk=\""+ info.getPassword() + "\"\n";
			}else {
				configText += "	key_mgmt=NONE\n";
			}					
			
			configText += "}\n";
			
			if ( ! Utils.writeFile(Commons.WPA_CONF, configText, true) ){
				Utils.logError("Unable to write wpa_supplicant.conf file!");
				return false;
			}
			
			//chmod wpa_supplicant.conf, in order to make it accessible
			if(chmodFile(Commons.WPA_CONF, "666")){
				return true;
			}else {
				Utils.logError("Unable to chmod wpa_supplicant.conf");
				return false;			
			}
						
		} catch (Exception e) {
			Utils.logError("Error while creating wpa_supplicant.conf",e);
			return false;
		}
		
	}

	
}