1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! WiFi auto connect toggle skill - enable/disable auto connect to known networks
use crate::DriverCallback;
use crate::DriverContext;
use crate::{
DriverCategory,
types::{Driver, DriverParameter},
};
use anyhow::Result;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::process::Command;
#[derive(Debug)]
pub struct WifiAutoConnectToggleDriver;
#[async_trait::async_trait]
impl Driver for WifiAutoConnectToggleDriver {
fn name(&self) -> &str {
"wifi_auto_connect_toggle"
}
fn description(&self) -> &str {
"Enable or disable automatic connection to known WiFi networks"
}
fn usage_hint(&self) -> &str {
"Use this skill to control whether the device automatically connects to saved WiFi networks when in range."
}
fn parameters(&self) -> Vec<DriverParameter> {
vec![
DriverParameter {
name: "enabled".to_string(),
param_type: "boolean".to_string(),
description: "Enable (true) or disable (false) auto-connect".to_string(),
required: true,
default: None,
example: Some(Value::Bool(true)),
enum_values: None,
},
DriverParameter {
name: "ssid".to_string(),
param_type: "string".to_string(),
description: "Specific SSID to configure (default: all networks)".to_string(),
required: false,
default: None,
example: Some(Value::String("MyWiFi".to_string())),
enum_values: None,
},
]
}
fn example_call(&self) -> Value {
json!({
"action": "wifi_auto_connect_toggle",
"parameters": {
"enabled": false
}
})
}
fn example_output(&self) -> String {
"Auto-connect for WiFi disabled".to_string()
}
fn category(&self) -> DriverCategory {
DriverCategory::Wifi
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
callback: Option<&dyn DriverCallback>,
context: Option<&DriverContext>,
) -> Result<String> {
let enabled = parameters
.get("enabled")
.and_then(|v| v.as_bool())
.ok_or_else(|| anyhow::anyhow!("Missing 'enabled' parameter"))?;
let _ssid = parameters.get("ssid").and_then(|v| v.as_str());
#[cfg(target_os = "windows")]
{
let value = if enabled { "yes" } else { "no" };
if let Some(ssid) = _ssid {
Command::new("netsh")
.args([
"wlan",
"set",
"profile",
"parameter",
"name=",
ssid,
"connectionmode=",
value,
])
.output()?;
} else {
// For all profiles
let output = Command::new("netsh")
.args(["wlan", "show", "profiles"])
.output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if line.contains(":") {
if let Some(profile) = line.split(':').nth(1) {
let profile = profile.trim();
if !profile.is_empty() {
let _ = Command::new("netsh")
.args([
"wlan",
"set",
"profile",
"parameter",
"name=",
profile,
"connectionmode=",
value,
])
.output();
}
}
}
}
}
}
#[cfg(target_os = "linux")]
{
let value = if enabled { "yes" } else { "no" };
if let Some(ssid) = _ssid {
Command::new("nmcli")
.args([
"connection",
"modify",
ssid,
"802-11-wireless.mode",
"infrastructure",
])
.output()?;
Command::new("nmcli")
.args([
"connection",
"modify",
ssid,
"connection.autoconnect",
value,
])
.output()?;
} else {
Command::new("nmcli")
.args([
"networking",
"connectivity",
if enabled { "on" } else { "off" },
])
.output()?;
}
}
let status = if enabled { "enabled" } else { "disabled" };
Ok(format!("Auto-connect for WiFi {}", status))
}
}