@@ -66,6 +66,17 @@ def auto_connect(ssid=None,
66
66
connect ()
67
67
68
68
69
+ # Returns True when a connection to a router is made, or the Hotspot is live
70
+ def check_device_state ():
71
+ # Save the wi-fi device object to a variable
72
+ if get_device ().State == NetworkManager .NM_DEVICE_STATE_ACTIVATED :
73
+ return True
74
+ else :
75
+ return False
76
+
77
+
78
+ # Ignores device and Wi-Fi status and checks for internet. Helpful for when
79
+ # connected to Ethernet.
69
80
def check_internet_status (host = "8.8.8.8" , port = 53 , timeout = 5 ):
70
81
try :
71
82
socket .setdefaulttimeout (timeout )
@@ -77,6 +88,7 @@ def check_internet_status(host="8.8.8.8", port=53, timeout=5):
77
88
return False
78
89
79
90
91
+ # Checks if there is an active connection to an external Wi-Fi router
80
92
def check_wifi_status ():
81
93
try :
82
94
run = subprocess .run (["iw" , "dev" , "wlan0" , "link" ],
@@ -111,38 +123,27 @@ def connect(conn_type=config.type_hotspot,
111
123
NetworkManager .Settings .AddConnection (conn_dict )
112
124
logger .info (f"Adding connection of type { conn_type } " )
113
125
114
- # Find this connection and its device
115
- connections = \
116
- dict ([(x .GetSettings ()['connection' ]['id' ], x )
117
- for x in NetworkManager .Settings .ListConnections ()])
118
- conn = connections [config .ap_name ]
119
-
120
126
# Save the wi-fi device object to a variable
121
- devices = dict ([(x .DeviceType , x )
122
- for x in NetworkManager .NetworkManager .GetDevices ()])
123
-
124
- if NetworkManager .NM_DEVICE_TYPE_WIFI in devices :
125
- dev = devices [NetworkManager .NM_DEVICE_TYPE_WIFI ]
126
- else :
127
- logger .error ("No suitable and available device found" )
128
- raise WifiNoSuitableDevice
127
+ dev = get_device ()
129
128
130
129
# Connect
131
- NetworkManager .NetworkManager .ActivateConnection (conn , dev , "/" )
130
+ NetworkManager .NetworkManager .ActivateConnection (get_connection_id (),
131
+ dev ,
132
+ "/" )
132
133
133
134
# If not a hotspot, log the connection SSID being attempted
134
135
if conn_type != config .type_hotspot :
135
136
logger .info (f"Attempting connection to { ssid } " )
136
137
137
138
# Wait for ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
138
139
loop_count = 0
139
- while dev . State != NetworkManager . NM_DEVICE_STATE_ACTIVATED :
140
+ while not check_device_state () :
140
141
time .sleep (1 )
141
142
loop_count += 1
142
143
if loop_count > 30 : # Only wait 30 seconds max
143
144
break
144
145
145
- if dev . State == NetworkManager . NM_DEVICE_STATE_ACTIVATED :
146
+ if check_device_state () :
146
147
logger .info ("Connection active." )
147
148
148
149
# Activate the LED to indicate device is connected.
@@ -172,22 +173,18 @@ def connect(conn_type=config.type_hotspot,
172
173
def forget (create_new_hotspot = False , all_networks = False ):
173
174
# Find and delete the hotspot connection
174
175
try :
175
- connections = NetworkManager .Settings .ListConnections ()
176
-
177
176
if all_networks :
178
- for connection in connections :
177
+ for connection in NetworkManager . Settings . ListConnections () :
179
178
if connection .GetSettings ()["connection" ]["type" ] \
180
179
== "802-11-wireless" :
181
180
# Delete the identified connection
182
181
network_id = connection .GetSettings ()["connection" ]["id" ]
183
182
connection .Delete ()
184
183
logger .debug (f"Deleted connection: { network_id } " )
185
184
else :
186
- connection_ids = \
187
- dict ([(x .GetSettings ()['connection' ]['id' ], x )
188
- for x in connections ])
189
- if config .ap_name in connection_ids :
190
- connection_ids [config .ap_name ].Delete ()
185
+ connection_id = get_connection_id ()
186
+ if connection_id :
187
+ connection_id .Delete ()
191
188
logger .debug (f"Deleted connection: { config .ap_name } " )
192
189
193
190
# Disable LED indicating Wi-Fi is not active.
@@ -205,6 +202,27 @@ def forget(create_new_hotspot=False, all_networks=False):
205
202
return True
206
203
207
204
205
+ def get_connection_id ():
206
+ connection = dict ([(x .GetSettings ()['connection' ]['id' ], x )
207
+ for x in NetworkManager .Settings .ListConnections ()])
208
+
209
+ if config .ap_name in connection :
210
+ return connection [config .ap_name ]
211
+ else :
212
+ return False
213
+
214
+
215
+ def get_device ():
216
+ devices = dict ([(x .DeviceType , x )
217
+ for x in NetworkManager .NetworkManager .GetDevices ()])
218
+
219
+ if NetworkManager .NM_DEVICE_TYPE_WIFI in devices :
220
+ return devices [NetworkManager .NM_DEVICE_TYPE_WIFI ]
221
+ else :
222
+ logger .error ("No suitable or available device found." )
223
+ raise WifiNoSuitableDevice
224
+
225
+
208
226
def list_access_points ():
209
227
# Run IW to reduce chance of empty SSID list. Storing result
210
228
# to return so that if IW does not work on this device the refresh
@@ -215,15 +233,7 @@ def list_access_points():
215
233
216
234
try :
217
235
# Fetch dictionary of devices
218
- devices = dict ([(x .DeviceType , x )
219
- for x in NetworkManager .NetworkManager .GetDevices ()])
220
-
221
- # Save the wi-fi device object to a variable
222
- if NetworkManager .NM_DEVICE_TYPE_WIFI in devices :
223
- dev = devices [NetworkManager .NM_DEVICE_TYPE_WIFI ]
224
- else :
225
- logger .error ("No suitable and available device found" )
226
- raise WifiNoSuitableDevice
236
+ dev = get_device ()
227
237
228
238
# For each wi-fi connection in range, identify it's details
229
239
compiled_ssids = [analyse_access_point (ap )
@@ -267,7 +277,7 @@ def refresh_networks(retries=5):
267
277
time .sleep (3 )
268
278
subprocess .check_output (["iw" , "dev" , "wlan0" , "scan" ])
269
279
except subprocess .CalledProcessError :
270
- logger .warning ('Resource busy. Retrying...' )
280
+ logger .warning ('IW resource busy. Retrying...' )
271
281
continue
272
282
except Exception :
273
283
logger .error ('Unknown error calling IW.' )
0 commit comments