MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / start_ap_mode

Method start_ap_mode

wifi_manager.py:2310–2378  ·  view source on GitHub ↗

Start Access Point mode

(self)

Source from the content-addressed store, hash-verified

2308 self.ap_logger.debug("Hostapd_cli not available, checking DHCP leases...")
2309 dhcp_leases_file = '/var/lib/dhcp/dhcpd.leases'
2310 if os.path.exists(dhcp_leases_file):
2311 with open(dhcp_leases_file, 'r') as f:
2312 content = f.read()
2313 # Count active leases (simplified check)
2314 active_leases = content.count('binding state active')
2315
2316 if active_leases != getattr(self, 'last_client_count', 0):
2317 self.ap_logger.info(f"DHCP active leases: {active_leases}")
2318 self.last_client_count = active_leases
2319
2320 self.ap_clients_count = active_leases
2321 self.ap_clients_connected = active_leases > 0
2322 return active_leases
2323
2324 # Method 3: Check ARP table for AP subnet
2325 self.ap_logger.debug("DHCP leases not available, checking ARP table...")
2326 result = subprocess.run(['arp', '-a'], capture_output=True, text=True, timeout=5)
2327 if result.returncode == 0:
2328 # Count IPs in AP subnet (192.168.4.x)
2329 ap_clients = [line for line in result.stdout.split('\n') if '192.168.4.' in line]
2330 client_count = len(ap_clients)
2331
2332 if client_count != getattr(self, 'last_client_count', 0):
2333 if client_count > 0:
2334 self.ap_logger.info(f"ARP table shows {client_count} clients in AP subnet")
2335 self.ap_logger.debug(f"ARP entries: {ap_clients}")
2336 else:
2337 self.ap_logger.debug("No clients found in ARP table")
2338 self.last_client_count = client_count
2339
2340 self.ap_clients_count = client_count
2341 self.ap_clients_connected = client_count > 0
2342 return client_count
2343
2344 self.ap_logger.warning("All client detection methods failed")
2345 return 0
2346
2347 except Exception as e:
2348 self.logger.warning(f"Error checking AP clients: {e}")
2349 self.ap_logger.warning(f"Exception checking AP clients: {e}")
2350 return 0
2351
2352 def should_stop_idle_ap(self):
2353 """Check if AP should be stopped due to inactivity"""
2354 if not self.ap_mode_active or not self.ap_mode_start_time:
2355 return False
2356
2357 # Check if AP has been running for more than the idle timeout
2358 ap_running_time = time.time() - self.ap_mode_start_time
2359
2360 # If no clients have connected and idle timeout reached
2361 if not self.ap_clients_connected and ap_running_time > self.ap_idle_timeout:
2362 self.logger.info(f"AP idle timeout reached ({self.ap_idle_timeout}s) with no clients")
2363 self.ap_logger.info(f"AP idle timeout reached: {self.ap_idle_timeout}s with no clients connected")
2364 self.ap_logger.info(f"AP has been running for {ap_running_time:.1f} seconds")
2365 self.ap_logger.info("Initiating AP shutdown due to inactivity")
2366 return True
2367

Callers 7

start_wardrive_apMethod · 0.95
disconnect_wifiFunction · 0.80
start_wifi_apFunction · 0.80

Calls 11

scan_networksMethod · 0.95
_start_ap_servicesMethod · 0.95
_cleanup_ap_modeMethod · 0.95
infoMethod · 0.80
warningMethod · 0.80
errorMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected