Skip to content

Commit 25aef4e

Browse files
brandonweekscathay4t
authored andcommitted
wiphy: Add support for setting phy parameters
1 parent 7d4f45a commit 25aef4e

File tree

5 files changed

+134
-3
lines changed

5 files changed

+134
-3
lines changed

examples/nl80211_set_wiphy.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
use std::env::args;
4+
5+
use anyhow::{bail, Context, Error};
6+
use futures::stream::TryStreamExt;
7+
8+
fn main() -> Result<(), Error> {
9+
let argv: Vec<_> = args().collect();
10+
11+
if argv.len() < 2 {
12+
eprintln!("Usage: nl80211_set_wiphy <interface index>");
13+
bail!("Required arguments not given");
14+
}
15+
16+
let err_msg = format!("Invalid interface index value: {}", argv[1]);
17+
let index = argv[1].parse::<u32>().context(err_msg)?;
18+
19+
let rt = tokio::runtime::Builder::new_current_thread()
20+
.enable_io()
21+
.enable_time()
22+
.build()
23+
.unwrap();
24+
rt.block_on(set_wiphy_params(index));
25+
26+
Ok(())
27+
}
28+
29+
async fn set_wiphy_params(if_index: u32) {
30+
let (connection, handle, _) = wl_nl80211::new_connection().unwrap();
31+
tokio::spawn(connection);
32+
33+
let attrs = wl_nl80211::Nl80211Channel::new(if_index)
34+
.frequency(5180)
35+
.frequency_offset(0)
36+
.channel_width(wl_nl80211::Nl80211ChannelWidth::NoHt20)
37+
.channel_type(wl_nl80211::Nl80211HtWiphyChannelType::NoHt)
38+
.center_frequency(5180)
39+
.build();
40+
41+
let mut channel_handle =
42+
handle.wireless_physic().set(attrs).execute().await;
43+
channel_handle.try_next().await.unwrap();
44+
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ pub use self::wifi7::{
8383
};
8484
pub use self::wiphy::{
8585
Nl80211Band, Nl80211BandInfo, Nl80211BandType, Nl80211BandTypes,
86-
Nl80211CipherSuit, Nl80211Frequency, Nl80211FrequencyInfo, Nl80211IfMode,
86+
Nl80211Channel, Nl80211ChannelSwitchRequest, Nl80211CipherSuit,
87+
Nl80211Frequency, Nl80211FrequencyInfo, Nl80211IfMode,
8788
Nl80211WiphyGetRequest, Nl80211WiphyHandle, Nl80211WowlanTcpTrigerSupport,
8889
Nl80211WowlanTrigerPatternSupport, Nl80211WowlanTrigersSupport,
8990
};

src/wiphy/handle.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// SPDX-License-Identifier: MIT
22

3-
use crate::{Nl80211Handle, Nl80211WiphyGetRequest};
3+
use crate::{
4+
Nl80211Attr, Nl80211AttrsBuilder, Nl80211ChannelSwitchRequest,
5+
Nl80211ChannelWidth, Nl80211Handle, Nl80211HtWiphyChannelType,
6+
Nl80211WiphyGetRequest,
7+
};
48

59
#[derive(Debug)]
610
pub struct Nl80211WiphyHandle(Nl80211Handle);
@@ -15,4 +19,42 @@ impl Nl80211WiphyHandle {
1519
pub fn get(&mut self) -> Nl80211WiphyGetRequest {
1620
Nl80211WiphyGetRequest::new(self.0.clone())
1721
}
22+
23+
pub fn set(
24+
&mut self,
25+
attributes: Vec<Nl80211Attr>,
26+
) -> Nl80211ChannelSwitchRequest {
27+
Nl80211ChannelSwitchRequest::new(self.0.clone(), attributes)
28+
}
29+
}
30+
31+
#[derive(Debug)]
32+
pub struct Nl80211Channel;
33+
34+
impl Nl80211Channel {
35+
pub fn new(if_index: u32) -> Nl80211AttrsBuilder<Self> {
36+
Nl80211AttrsBuilder::<Self>::new().if_index(if_index)
37+
}
38+
}
39+
40+
impl Nl80211AttrsBuilder<Nl80211Channel> {
41+
pub fn frequency(self, value: u32) -> Self {
42+
self.replace(Nl80211Attr::WiphyFreq(value))
43+
}
44+
45+
pub fn frequency_offset(self, value: u32) -> Self {
46+
self.replace(Nl80211Attr::WiphyFreqOffset(value))
47+
}
48+
49+
pub fn channel_width(self, value: Nl80211ChannelWidth) -> Self {
50+
self.replace(Nl80211Attr::ChannelWidth(value))
51+
}
52+
53+
pub fn channel_type(self, value: Nl80211HtWiphyChannelType) -> Self {
54+
self.replace(Nl80211Attr::WiphyChannelType(value))
55+
}
56+
57+
pub fn center_frequency(self, value: u32) -> Self {
58+
self.replace(Nl80211Attr::CenterFreq1(value))
59+
}
1860
}

src/wiphy/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod command;
66
mod get;
77
mod handle;
88
mod ifmode;
9+
mod set;
910
mod wowlan;
1011

1112
pub use self::band::{
@@ -14,8 +15,9 @@ pub use self::band::{
1415
};
1516
pub use self::cipher::Nl80211CipherSuit;
1617
pub use self::get::Nl80211WiphyGetRequest;
17-
pub use self::handle::Nl80211WiphyHandle;
18+
pub use self::handle::{Nl80211Channel, Nl80211WiphyHandle};
1819
pub use self::ifmode::Nl80211IfMode;
20+
pub use self::set::Nl80211ChannelSwitchRequest;
1921
pub use self::wowlan::{
2022
Nl80211WowlanTcpTrigerSupport, Nl80211WowlanTrigerPatternSupport,
2123
Nl80211WowlanTrigersSupport,

src/wiphy/set.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
use futures::TryStream;
4+
use netlink_packet_core::{NLM_F_ACK, NLM_F_REQUEST};
5+
use netlink_packet_generic::GenlMessage;
6+
7+
use crate::{
8+
nl80211_execute, Nl80211Attr, Nl80211Command, Nl80211Error, Nl80211Handle,
9+
Nl80211Message,
10+
};
11+
12+
pub struct Nl80211ChannelSwitchRequest {
13+
handle: Nl80211Handle,
14+
attributes: Vec<Nl80211Attr>,
15+
}
16+
17+
impl Nl80211ChannelSwitchRequest {
18+
pub(crate) fn new(
19+
handle: Nl80211Handle,
20+
attributes: Vec<Nl80211Attr>,
21+
) -> Self {
22+
Nl80211ChannelSwitchRequest { handle, attributes }
23+
}
24+
25+
pub async fn execute(
26+
self,
27+
) -> impl TryStream<Ok = GenlMessage<Nl80211Message>, Error = Nl80211Error>
28+
{
29+
let Nl80211ChannelSwitchRequest {
30+
mut handle,
31+
attributes,
32+
} = self;
33+
34+
let nl80211_msg = Nl80211Message {
35+
cmd: Nl80211Command::SetWiphy,
36+
attributes,
37+
};
38+
let flags = NLM_F_REQUEST | NLM_F_ACK;
39+
40+
nl80211_execute(&mut handle, nl80211_msg, flags).await
41+
}
42+
}

0 commit comments

Comments
 (0)