aboutsummaryrefslogtreecommitdiff
path: root/src/control.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-05-01 14:50:29 +0000
committerGitHub <[email protected]>2023-05-01 14:50:29 +0000
commit73cd016885fc6db38c71ca3c1941554c864670ce (patch)
treeadc59df0d99eb70eeaffb404bc626351387e59ee /src/control.rs
parent0589f2f36e29a6ad6506a0935f83131f69a106fe (diff)
parenta186694fddd00beb4c3b45349cd79ca1959b4d17 (diff)
Merge pull request #72 from tana/ap-mode
Add AP mode
Diffstat (limited to 'src/control.rs')
-rw-r--r--src/control.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/control.rs b/src/control.rs
index 934bade23..e1ad06e6b 100644
--- a/src/control.rs
+++ b/src/control.rs
@@ -226,6 +226,73 @@ impl<'a> Control<'a> {
226 .await 226 .await
227 } 227 }
228 228
229 pub async fn start_ap_open(&mut self, ssid: &str, channel: u8) {
230 self.start_ap(ssid, "", Security::OPEN, channel).await;
231 }
232
233 pub async fn start_ap_wpa2(&mut self, ssid: &str, passphrase: &str, channel: u8) {
234 self.start_ap(ssid, passphrase, Security::WPA2_AES_PSK, channel).await;
235 }
236
237 async fn start_ap(&mut self, ssid: &str, passphrase: &str, security: Security, channel: u8) {
238 if security != Security::OPEN
239 && (passphrase.as_bytes().len() < MIN_PSK_LEN || passphrase.as_bytes().len() > MAX_PSK_LEN)
240 {
241 panic!("Passphrase is too short or too long");
242 }
243
244 // Temporarily set wifi down
245 self.ioctl(IoctlType::Set, IOCTL_CMD_DOWN, 0, &mut []).await;
246
247 // Turn off APSTA mode
248 self.set_iovar_u32("apsta", 0).await;
249
250 // Set wifi up again
251 self.ioctl(IoctlType::Set, IOCTL_CMD_UP, 0, &mut []).await;
252
253 // Turn on AP mode
254 self.ioctl_set_u32(IOCTL_CMD_SET_AP, 0, 1).await;
255
256 // Set SSID
257 let mut i = SsidInfoWithIndex {
258 index: 0,
259 ssid_info: SsidInfo {
260 len: ssid.as_bytes().len() as _,
261 ssid: [0; 32],
262 },
263 };
264 i.ssid_info.ssid[..ssid.as_bytes().len()].copy_from_slice(ssid.as_bytes());
265 self.set_iovar("bsscfg:ssid", &i.to_bytes()).await;
266
267 // Set channel number
268 self.ioctl_set_u32(IOCTL_CMD_SET_CHANNEL, 0, channel as u32).await;
269
270 // Set security
271 self.set_iovar_u32x2("bsscfg:wsec", 0, (security as u32) & 0xFF).await;
272
273 if security != Security::OPEN {
274 self.set_iovar_u32x2("bsscfg:wpa_auth", 0, 0x0084).await; // wpa_auth = WPA2_AUTH_PSK | WPA_AUTH_PSK
275
276 Timer::after(Duration::from_millis(100)).await;
277
278 // Set passphrase
279 let mut pfi = PassphraseInfo {
280 len: passphrase.as_bytes().len() as _,
281 flags: 1, // WSEC_PASSPHRASE
282 passphrase: [0; 64],
283 };
284 pfi.passphrase[..passphrase.as_bytes().len()].copy_from_slice(passphrase.as_bytes());
285 self.ioctl(IoctlType::Set, IOCTL_CMD_SET_PASSPHRASE, 0, &mut pfi.to_bytes())
286 .await;
287 }
288
289 // Change mutlicast rate from 1 Mbps to 11 Mbps
290 self.set_iovar_u32("2g_mrate", 11000000 / 500000).await;
291
292 // Start AP
293 self.set_iovar_u32x2("bss", 0, 1).await; // bss = BSS_UP
294 }
295
229 async fn set_iovar_u32x2(&mut self, name: &str, val1: u32, val2: u32) { 296 async fn set_iovar_u32x2(&mut self, name: &str, val1: u32, val2: u32) {
230 let mut buf = [0; 8]; 297 let mut buf = [0; 8];
231 buf[0..4].copy_from_slice(&val1.to_le_bytes()); 298 buf[0..4].copy_from_slice(&val1.to_le_bytes());