134 lines
5.3 KiB
C++
134 lines
5.3 KiB
C++
#pragma once
|
|
|
|
#include "nm_types.h"
|
|
#include "nm_device.h"
|
|
#include "nm_connection.h"
|
|
#include <chrono>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace radiantwave::network {
|
|
|
|
// ─── Callbacks ─────────────────────────────────────────────────────────
|
|
|
|
using StateCallback = std::function<void(ConnectionState new_state,
|
|
const std::string& message)>;
|
|
using LogCallback = std::function<void(const std::string& message)>;
|
|
|
|
// ─── Network Manager ───────────────────────────────────────────────────
|
|
|
|
class NetworkManager {
|
|
public:
|
|
NetworkManager();
|
|
~NetworkManager();
|
|
|
|
// Non-copyable, movable
|
|
NetworkManager(const NetworkManager&) = delete;
|
|
NetworkManager& operator=(const NetworkManager&) = delete;
|
|
NetworkManager(NetworkManager&&) noexcept;
|
|
NetworkManager& operator=(NetworkManager&&) noexcept;
|
|
|
|
// ── Lifecycle ──────────────────────────────────────────────────────
|
|
|
|
// Connect to system D-Bus. Returns false if NM unavailable.
|
|
bool initialize();
|
|
|
|
// Shut down D-Bus connection
|
|
void shutdown();
|
|
|
|
// ── Scanning ───────────────────────────────────────────────────────
|
|
|
|
// Request a WiFi scan. Returns immediately.
|
|
// Use getScanResults() after ~2s to get results.
|
|
bool requestScan();
|
|
|
|
// Get cached scan results (no D-Bus call, returns last scan)
|
|
std::vector<NetworkInfo> getScanResults() const;
|
|
|
|
// ── Connection ─────────────────────────────────────────────────────
|
|
|
|
// Connect to WiFi network. Blocks until connected or failed.
|
|
// password can contain any characters - properly escaped.
|
|
// Returns connection state and descriptive error on failure.
|
|
ConnectResult connect(const std::string& ssid,
|
|
const std::string& password,
|
|
std::chrono::seconds timeout = std::chrono::seconds(30));
|
|
|
|
// Disconnect current connection
|
|
bool disconnect();
|
|
|
|
// ── Network Management ─────────────────────────────────────────────
|
|
|
|
// Forget (delete) a saved connection profile
|
|
bool forgetNetwork(const std::string& ssid);
|
|
|
|
// ── Status ─────────────────────────────────────────────────────────
|
|
|
|
// Is WiFi device connected to any network?
|
|
bool isConnected() const;
|
|
|
|
// What's the current SSID? Empty if not connected.
|
|
std::string currentSSID() const;
|
|
|
|
// Full connectivity check (tests actual internet access)
|
|
bool hasInternet() const;
|
|
|
|
// Force libc to reload /etc/resolv.conf and flush stale DNS cache
|
|
void refreshDnsResolver() const;
|
|
|
|
// Get list of saved network SSIDs
|
|
std::vector<std::string> savedNetworks() const;
|
|
|
|
// ── Callbacks ──────────────────────────────────────────────────────
|
|
|
|
// Called on connection state changes
|
|
void onStateChanged(StateCallback callback);
|
|
|
|
// Called with log messages
|
|
void setLogCallback(LogCallback callback);
|
|
|
|
// ── Auto-reconnect ─────────────────────────────────────────────────
|
|
|
|
// Enable/disable auto-reconnect on boot (default: enabled)
|
|
void setAutoReconnect(bool enable);
|
|
|
|
// Get the last successfully connected SSID (for auto-reconnect)
|
|
std::string lastConnectedSSID() const;
|
|
|
|
private:
|
|
// Internal helpers
|
|
void log(const std::string& message); // Info level
|
|
void logDebug(const std::string& message); // Debug level
|
|
void logWarning(const std::string& message); // Warning level
|
|
void notifyState(ConnectionState state, const std::string& message);
|
|
|
|
// Wait for device state
|
|
bool waitForDeviceState(uint32_t target_state, std::chrono::seconds timeout);
|
|
|
|
// Delete stale profiles for SSID
|
|
void deleteStaleProfiles(const std::string& ssid);
|
|
|
|
// Find access point by SSID
|
|
std::string findAccessPoint(const std::string& ssid);
|
|
|
|
// Build and activate connection
|
|
ConnectResult addAndActivateConnection(
|
|
const ConnectionSettings& settings,
|
|
const std::string& device_path,
|
|
const std::string& ap_path
|
|
);
|
|
|
|
// Wait for full connectivity (DHCP + DNS + routing)
|
|
bool waitForFullConnectivity(std::chrono::seconds timeout);
|
|
|
|
// Check actual internet via TCP
|
|
bool checkInternetTcp() const;
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> impl_;
|
|
};
|
|
|
|
} // namespace radiantwave::network
|