|
| 1 | +<?php |
| 2 | +require_once '../../includes/autoload.php'; |
| 3 | +require_once '../../includes/CSRF.php'; |
| 4 | +require_once '../../includes/session.php'; |
| 5 | +require_once '../../includes/config.php'; |
| 6 | +require_once '../../includes/authenticate.php'; |
| 7 | + |
| 8 | +header('Content-Type: application/json'); |
| 9 | + |
| 10 | +$interface = $_POST['interface'] ?? ''; |
| 11 | + |
| 12 | +if (empty($interface)) { |
| 13 | + http_response_code(400); |
| 14 | + echo json_encode(['error' => 'Missing interface parameter']); |
| 15 | + exit; |
| 16 | +} |
| 17 | + |
| 18 | +$suggestedHWMode = suggestWifiHwMode($interface); |
| 19 | +$supportedModes = supportedModes($suggestedHWMode); |
| 20 | + |
| 21 | +$data = [ |
| 22 | + 'interface' => $interface, |
| 23 | + 'suggested_hw_mode' => $suggestedHWMode, |
| 24 | + 'supported_modes' => $supportedModes, |
| 25 | +]; |
| 26 | + |
| 27 | +function suggestWifiHwMode(string $interface): string { |
| 28 | + $interface = escapeshellarg($interface); |
| 29 | + |
| 30 | + $phyCmd = "iw dev $interface info 2>/dev/null | grep -oP 'phy\\s*\\K\\S+'"; |
| 31 | + $phy = trim(shell_exec($phyCmd)); |
| 32 | + |
| 33 | + if ($phy === '') { |
| 34 | + http_response_code(400); |
| 35 | + header('Content-Type: application/json'); |
| 36 | + echo json_encode(['error' => 'No valid phy for ' . $interface]); |
| 37 | + exit; |
| 38 | + } |
| 39 | + |
| 40 | + $cmd = "iw phy phy$phy info 2>/dev/null"; |
| 41 | + $text = shell_exec($cmd); |
| 42 | + |
| 43 | + if (empty($text)) { |
| 44 | + http_response_code(500); |
| 45 | + echo json_encode(['error' => 'No phy info retrieved']); |
| 46 | + exit; |
| 47 | + } |
| 48 | + |
| 49 | + // --- Capability presence --- |
| 50 | + $hasEHT = preg_match('/EHT\s+(?:Capabilities|MAC Capabilities|PHY Capabilities)/i', $text); |
| 51 | + $hasHE = preg_match('/HE\s+(?:Capabilities|MAC Capabilities|PHY Capabilities)/i', $text); |
| 52 | + $hasVHT = preg_match('/VHT\s+Capabilities/i', $text); |
| 53 | + $hasHT = preg_match('/HT\s+(?:Capabilities|MCS rate indexes supported)/i', $text); |
| 54 | + |
| 55 | + // --- Band / frequency detection --- |
| 56 | + $has24GHz = preg_match('/Band\s+1:|2412\s*MHz|2437\s*MHz|2462\s*MHz/i', $text); |
| 57 | + $has5GHz = preg_match('/Band\s+2:|5180\s*MHz|5500\s*MHz|5745\s*MHz/i', $text); |
| 58 | + $has6GHz = preg_match('/Band\s+[34]:|5955\s*MHz|6[0-9]{3}\s*MHz/i', $text); |
| 59 | + |
| 60 | + // Decision - prioritize explicit capability blocks over bands |
| 61 | + if ($hasEHT) { |
| 62 | + $suggested = 'be'; |
| 63 | + } elseif ($hasHE) { |
| 64 | + $suggested = 'ax'; |
| 65 | + } elseif ($hasVHT) { |
| 66 | + $suggested = 'ac'; |
| 67 | + } elseif ($hasHT) { |
| 68 | + $suggested = 'n'; |
| 69 | + } elseif ($has5GHz) { |
| 70 | + $suggested = 'a'; |
| 71 | + } else { |
| 72 | + $suggested = 'g'; |
| 73 | + } |
| 74 | + |
| 75 | + // Return debug info in JSON for now |
| 76 | + // header('Content-Type: application/json'); |
| 77 | + // echo json_encode([ |
| 78 | + // 'phy' => $phy, |
| 79 | + // 'cmd' => $cmd, |
| 80 | + // 'suggested' => $suggested, |
| 81 | + // 'text' => $text, |
| 82 | + // 'debug' => [ |
| 83 | + // 'hasEHT' => (bool)$hasEHT, |
| 84 | + // 'hasHE' => (bool)$hasHE, |
| 85 | + // 'hasVHT' => (bool)$hasVHT, |
| 86 | + // 'hasHT' => (bool)$hasHT, |
| 87 | + // 'has5GHz' => (bool)$has5GHz, |
| 88 | + // 'has6GHz' => (bool)$has6GHz, |
| 89 | + // 'heMatches' => $heMatches, |
| 90 | + // ], |
| 91 | + // 'label' => match($suggested) { |
| 92 | + // 'be' => '802.11be - 2.4/5/6 GHz', |
| 93 | + // 'ax' => '802.11ax - 2.4/5/6 GHz', |
| 94 | + // 'ac' => '802.11ac - 5 GHz', |
| 95 | + // 'n' => '802.11n - 2.4/5 GHz', |
| 96 | + // 'a' => '802.11a - 5 GHz', |
| 97 | + // 'g' => '802.11g - 2.4 GHz', |
| 98 | + // default => 'Unknown' |
| 99 | + // } |
| 100 | + // ]); |
| 101 | + |
| 102 | + // Comment out the above echo and uncomment below for production: |
| 103 | + return $suggested; |
| 104 | +} |
| 105 | + |
| 106 | +function supportedModes(string $highestMode, bool $incl = true): array { |
| 107 | + $standards = HotspotService::get80211Standards(); |
| 108 | + |
| 109 | + $standardsCodes = array_keys($standards); |
| 110 | + |
| 111 | + $i = array_search($highestMode, $standardsCodes, true); |
| 112 | + |
| 113 | + return $i === false ? $arr : array_slice($standardsCodes, 0, $incl ? $i + 1 : $i); |
| 114 | +} |
| 115 | + |
| 116 | +echo json_encode($data, JSON_PRETTY_PRINT); |
| 117 | + |
0 commit comments