11
11
import struct
12
12
import json
13
13
14
-
15
14
class WiFi ():
16
15
17
16
CONFIG_FILE = "/etc/coderbot_wifi.conf"
18
- adapters = ["RT5370" , "RTL8188CUS" ]
19
- hostapds = {"RT5370" : "hostapd.RT5370" , "RTL8188CUS" : "hostapd.RTL8188" }
17
+ adapters = ["default" , " RT5370" , "RTL8188CUS" ]
18
+ hostapds = {"default" : "hostapd" , " RT5370" : "hostapd.RT5370" , "RTL8188CUS" : "hostapd.RTL8188" }
20
19
web_url = "http://my.coderbot.org/coderbot/v1.0/bot/new"
21
20
wifi_client_conf_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
22
21
_config = {}
@@ -51,12 +50,22 @@ def start_hostapd(cls):
51
50
hostapd_type = cls .hostapds .get (adapter )
52
51
try :
53
52
print "starting hostapd..."
54
- out = os .system ("/usr/sbin/" + hostapd_type + " /etc/hostapd/" + hostapd_type + " -B " )
53
+ out = os .system ("/usr/sbin/" + hostapd_type + " -B /etc/hostapd/" + hostapd_type + ".conf " )
55
54
print "hostapd out: " + str (out )
56
55
57
56
except subprocess .CalledProcessError as e :
58
57
print e .output
59
58
59
+ @classmethod
60
+ def start_dnsmasq (cls ):
61
+ try :
62
+ print "starting dnsmasq..."
63
+ out = os .system ("systemctl start dnsmasq" )
64
+ print "dnsmasq out: " + str (out )
65
+
66
+ except subprocess .CalledProcessError as e :
67
+ print e .output
68
+
60
69
@classmethod
61
70
def stop_hostapd (cls ):
62
71
try :
@@ -66,6 +75,15 @@ def stop_hostapd(cls):
66
75
except subprocess .CalledProcessError as e :
67
76
print e .output
68
77
78
+ @classmethod
79
+ def stop_dnsmasq (cls ):
80
+ try :
81
+ print "stopping dnsmasq..."
82
+ out = subprocess .check_output (["systemctl" , "stop" , "dnsmasq" ])
83
+ print "dnsmasq out: " + str (out )
84
+ except subprocess .CalledProcessError as e :
85
+ print e .output
86
+
69
87
@classmethod
70
88
def get_ipaddr (cls , ifname ):
71
89
s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
@@ -105,37 +123,48 @@ def set_client_params(cls, wssid, wpsk):
105
123
f .write (" psk=\" " + wpsk + "\" \n " )
106
124
f .write ("}" )
107
125
126
+ @classmethod
127
+ def set_ap_params (cls , wssid , wpsk ):
128
+ adapter = cls .get_adapter_type ()
129
+ os .system ("sudo sed -i s/ssid=.*$/ssid=" + wssid + "/ /etc/hostapd/" + cls .hostapds .get (adapter ) + ".conf" )
130
+ if wpsk :
131
+ os .system ("sudo sed -i s/wpa_passphrase=.*$/wpa_passphrase=" + wpsk + "/ /etc/hostapd/" + cls .hostapds .get (adapter ) + ".conf" )
132
+
108
133
@classmethod
109
134
def set_start_as_client (cls ):
110
- shutil .copy ("/etc/network/interfaces_cli" , "/etc/network/interfaces" )
111
135
cls ._config ["wifi_mode" ] = "client"
112
136
cls .save_config ()
113
137
114
138
@classmethod
115
139
def start_as_client (cls ):
140
+ cls .stop_dnsmasq ()
116
141
cls .stop_hostapd ()
117
142
try :
118
143
time .sleep (1.0 )
119
- out = subprocess .check_output (["ifdown" , "--force" , "wlan0" ])
120
- out = subprocess .check_output (["ifup" , "wlan0" ])
144
+ out = os .system ("wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1" )
145
+ out += os .system ("dhclient -1 wlan0" )
146
+ print out
121
147
cls .register_ipaddr (cls .get_config ().get ('bot_name' , 'CoderBot' ), cls .get_ipaddr ("wlan0" ))
122
- # print "registered bot, ip: " + str(cls.get_ipaddr("wlan0") + " name: " + cls.get_config().get('bot_name', 'CoderBot'))
148
+ print "registered bot, ip: " + str (cls .get_ipaddr ("wlan0" ) + " name: " + cls .get_config ().get ('bot_name' , 'CoderBot' ))
123
149
except subprocess .CalledProcessError as e :
124
150
print e .output
125
151
raise
126
152
127
153
@classmethod
128
154
def set_start_as_ap (cls ):
129
- shutil .copy ("/etc/network/interfaces_ap" , "/etc/network/interfaces" )
130
155
cls ._config ["wifi_mode" ] = "ap"
131
156
cls .save_config ()
132
157
133
158
@classmethod
134
159
def start_as_ap (cls ):
135
160
time .sleep (1.0 )
136
- out = subprocess .check_output (["ifdown" , "--force" , "wlan0" ])
137
- out = subprocess .check_output (["ifup" , "wlan0" ])
161
+ out = subprocess .check_output (["ip" , "link" , "set" , "dev" , "wlan0" , "down" ])
162
+ out += subprocess .check_output (["ip" , "a" , "add" , "10.0.0.1/24" , "dev" , "wlan0" ])
163
+ out += subprocess .check_output (["ip" , "link" , "set" , "dev" , "wlan0" , "up" ])
164
+ out += subprocess .check_output (["ifconfig" ])
165
+ print out
138
166
cls .start_hostapd ()
167
+ cls .start_dnsmasq ()
139
168
140
169
@classmethod
141
170
def start_service (cls ):
@@ -156,24 +185,15 @@ def main():
156
185
if len (sys .argv ) > 2 and sys .argv [1 ] == "updatecfg" :
157
186
if len (sys .argv ) > 2 and sys .argv [2 ] == "ap" :
158
187
w .set_start_as_ap ()
159
- #w.start_as_ap()
188
+ if len (sys .argv ) > 4 :
189
+ w .set_ap_params (sys .argv [3 ], sys .argv [4 ])
160
190
elif len (sys .argv ) > 2 and sys .argv [2 ] == "client" :
161
191
if len (sys .argv ) > 3 :
162
192
w .set_client_params (sys .argv [3 ], sys .argv [4 ])
163
193
w .set_start_as_client ()
164
- """
165
- try:
166
- w.start_as_client()
167
- except:
168
- print "Unable to register ip, revert to ap mode"
169
- w.start_as_ap()
170
- """
171
194
elif len (sys .argv ) > 3 and sys .argv [2 ] == "bot_name" :
172
195
WiFi .get_config ()['bot_name' ] = sys .argv [3 ]
173
196
WiFi .save_config ()
174
- elif len (sys .argv ) > 1 and sys .argv [1 ] == "register" :
175
- print "registering: " + str (w .get_ipaddr ("wlan0" ))
176
- w .register_ipaddr (w .get_config ().get ('bot_name' , 'CoderBot' ), w .get_ipaddr ("wlan0" ))
177
197
else :
178
198
w .start_service ()
179
199
0 commit comments