| 228 | } |
| 229 | |
| 230 | void openhd::wb::takeover_cards_monitor_mode( |
| 231 | const std::vector<WiFiCard>& cards, |
| 232 | std::shared_ptr<spdlog::logger> console) { |
| 233 | console->debug("takeover_cards_monitor_mode() begin"); |
| 234 | |
| 235 | // Take "ownership" from the system for cards used in monitor mode / |
| 236 | // wifibroadcast. Depending on the OS, we tell the network manager to ignore |
| 237 | // these cards instead of killing all networking processes that might |
| 238 | // interfere. |
| 239 | bool emulate = false; |
| 240 | |
| 241 | for (const auto& card : cards) { |
| 242 | if (card.type == WiFiCardType::QUALCOMM) { |
| 243 | console->debug("Qualcomm (ath0) card detected: {}", card.device_name); |
| 244 | return; |
| 245 | } |
| 246 | if (card.type == WiFiCardType::OPENHD_EMULATED) { |
| 247 | console->debug("Skipping emulated card: {}", card.device_name); |
| 248 | emulate = true; |
| 249 | continue; // Skip emulated cards |
| 250 | } |
| 251 | |
| 252 | wifi::commandhelper::nmcli_set_device_managed_status(card.device_name, |
| 253 | false); |
| 254 | } |
| 255 | |
| 256 | if (!emulate) { |
| 257 | wifi::commandhelper::rfkill_unblock_all(); |
| 258 | |
| 259 | // Allow time for network manager changes to take effect |
| 260 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 261 | |
| 262 | // Enable monitor mode for applicable cards |
| 263 | for (const auto& card : cards) { |
| 264 | if (card.type == WiFiCardType::QUALCOMM) { |
| 265 | // Execute the script to enable monitor mode for Qualcomm cards |
| 266 | const char* script_path = "/data/misc/wifi/start_monitor"; |
| 267 | console->debug( |
| 268 | "Running script to set Qualcomm (ath0) card to Monitor Mode: {}", |
| 269 | script_path); |
| 270 | |
| 271 | FILE* pipe = popen(script_path, "r"); |
| 272 | if (!pipe) { |
| 273 | console->error("Failed to execute script: {}", script_path); |
| 274 | continue; // Skip to the next card |
| 275 | } |
| 276 | |
| 277 | // Read the output of the script |
| 278 | char buffer[128]; |
| 279 | std::string result; |
| 280 | while (fgets(buffer, sizeof(buffer), pipe) != nullptr) { |
| 281 | result += buffer; |
| 282 | } |
| 283 | |
| 284 | // Wait for the script to complete |
| 285 | int return_code = pclose(pipe); |
| 286 | if (return_code == 0) { |
| 287 | console->info("Script executed successfully. Output:\n{}", result); |