@@ -17,6 +17,7 @@ class WiFi():
17
17
18
18
CONFIG_FILE = "/etc/coderbot_wifi.conf"
19
19
HOSTAPD_CONF_FILE = "/etc/hostapd/hostapd.conf"
20
+ INTERFACES_FILE = "/etc/network/interfaces_cli"
20
21
adapters = ["RT5370" , "RTL8188CUS" , "RT3572" ]
21
22
hostapds = {"RT5370" : "hostapd.RT5370" , "RTL8188CUS" : "hostapd.RTL8188" }
22
23
web_url = "http://coderbotsrv.appspot.com/register_ip"
@@ -91,6 +92,42 @@ def set_hostapd_params(cls, wssid, wpsk):
91
92
except IOError as e :
92
93
print e
93
94
95
+ def set_client_params (cls , wssid , wpsk , number ):
96
+ config = ConfigParser .ConfigParser ()
97
+ # configparser requires sections like '[section]'
98
+ # open hostapd.conf with dummy section '[interfaces]'
99
+
100
+ try :
101
+ with open (cls .INTERFACES_FILE ) as f :
102
+ inter_split = f .read ().split ("static" )
103
+ inter_start = inter_split [0 ] + "static\n "
104
+ conf_str = '[interfaces]\n ' + inter_split [1 ]
105
+ #parser needs = to work
106
+ conf_str = conf_str .replace (" " ,"=" )
107
+ conf_fp = StringIO .StringIO (conf_str )
108
+ config .readfp (conf_fp )
109
+
110
+ except IOError as e :
111
+ print e
112
+ return
113
+
114
+ config .set ('interfaces' ,'address' ,"192.168.0.1" + number )
115
+ config .set ('interfaces' ,'wpa-ssid' ,str (wssid ))
116
+ config .set ('interfaces' ,'wpa-psk' ,str (wpsk ))
117
+
118
+ try :
119
+ with open (cls .INTERFACES_FILE , 'wb' ) as f :
120
+ conf_items = config .items ('interfaces' )
121
+ f .write (inter_start )
122
+ for (key ,value ) in conf_items :
123
+ #quick workaround for values with spaces in
124
+ value = value .replace ("wlan0=inet=static" ,"wlan0 inet static" )
125
+ f .write ("{0} {1}\n " .format (key , value ))
126
+ f .write ("\n " )
127
+ except IOError as e :
128
+ print e
129
+ shutil .copy (cls .INTERFACES_FILE , "/etc/network/interfaces" )
130
+
94
131
@classmethod
95
132
def get_ipaddr (cls , ifname ):
96
133
s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
@@ -137,57 +174,46 @@ def start_service(cls):
137
174
138
175
def main ():
139
176
w = WiFi ()
140
-
141
- print 'Testing Client Connection...'
142
- print 'Wait 3 seconds before checking connection to router...'
143
- sleep (3 )
144
- print 'pinging router...'
145
- #ping hub router
146
- response = os .system ('ping -c 1 192.168.0.1' )
147
- #healthy response is 0
148
-
149
- if response == 0 :
150
- print 'Router has been found, staying on client mode'
151
- else :
152
- print 'Router not found, switching to AP mode'
153
- #setup hotspot
154
- shutil .copy ("/etc/network/interfaces_ap" , "/etc/network/interfaces" )
155
- print 'restart networking...'
156
- os .system ('sudo service networking restart' )
157
- w .start_hostapd ()
158
- print 'Waiting for hostapd to startup'
177
+ if len (sys .argv ) < 2 :
178
+ print 'Testing Client Connection...'
179
+ print 'Wait 3 seconds before checking connection to router...'
159
180
sleep (3 )
160
- print 'copying client interfaces back for next time'
161
- shutil .copy ("/etc/network/interfaces_cli" , "/etc/network/interfaces" )
181
+ print 'pinging router...'
182
+ #ping hub router
183
+ response = os .system ('ping -c 1 192.168.0.1' )
184
+ #healthy response is 0
185
+
186
+ if response == 0 :
187
+ print 'Router has been found, staying on client mode'
188
+ else :
189
+ print 'Router not found, switching to AP mode'
190
+ #setup hotspot
191
+ shutil .copy ("/etc/network/interfaces_ap" , "/etc/network/interfaces" )
192
+ print 'restart networking...'
193
+ os .system ('sudo service networking restart' )
194
+ w .start_hostapd ()
195
+ print 'Waiting for hostapd to startup'
196
+ sleep (3 )
197
+ print 'copying client interfaces back for next time'
198
+ shutil .copy ("/etc/network/interfaces_cli" , "/etc/network/interfaces" )
162
199
163
- if len (sys .argv ) > 2 and sys .argv [1 ] == "updatecfg" :
164
- if len (sys .argv ) > 2 and sys .argv [2 ] == "ap" :
165
- if len (sys .argv ) > 3 :
166
- w .set_hostapd_params (sys .argv [3 ], sys .argv [4 ])
167
- #w.set_start_as_ap()
168
- #w.start_as_ap()
169
- elif len (sys .argv ) > 2 and sys .argv [2 ] == "hub" :
170
- if len (sys .argv ) > 3 :
171
- w .set_client_params (sys .argv [3 ], sys .argv [4 ])
172
- #w.set_start_as_client()
173
- #w.stop_hostapd()
174
- #try:
175
- # w.start_as_client()
176
- #except:
177
- # print "Unable to register ip, revert to ap mode"
178
- # w.start_as_ap()
179
- elif len (sys .argv ) > 2 and sys .argv [2 ] == "local_client" :
180
- if len (sys .argv ) > 3 :
181
- w .set_client_params (sys .argv [3 ], sys .argv [4 ])
182
- w .set_start_as_client ()
183
- try :
184
- w .start_as_local_client ()
185
- except :
186
- print "Unable to connect to WLAN, revert to ap mode"
187
- w .start_as_ap ()
188
-
189
- #else:
190
- # w.start_service()
200
+ elif sys .argv [1 ] == "updatecfg" :
201
+ if len (sys .argv ) == 3 :
202
+ print "updating configs..."
203
+ #Update config to use this number, eg. for 7
204
+ #client ip goes to 192.168.0.7
205
+ w .set_client_params ("Lboro_Coderbot_Hub" ,"welovepis" , sys .argv [2 ])
206
+ #hostspot ssid goes to Lboro Coderbot 7
207
+ w .set_hostapd_params ("Lboro Coderbot " + sys .argv [2 ], "coderbot" )
208
+ print "done!"
209
+ else :
210
+ if len (sys .argv ) > 2 and sys .argv [2 ] == "ap" :
211
+ if len (sys .argv ) > 3 :
212
+ w .set_hostapd_params (sys .argv [3 ], sys .argv [4 ])
213
+ elif len (sys .argv ) > 2 and sys .argv [2 ] == "client" :
214
+ if len (sys .argv ) > 4 :
215
+ w .set_client_params (sys .argv [3 ], sys .argv [4 ], sys .argv [5 ])
216
+
191
217
192
218
if __name__ == "__main__" :
193
219
main ()
0 commit comments