diff options
Diffstat (limited to 'embassy-net-nrf91/src/context.rs')
| -rw-r--r-- | embassy-net-nrf91/src/context.rs | 53 |
1 files changed, 34 insertions, 19 deletions
diff --git a/embassy-net-nrf91/src/context.rs b/embassy-net-nrf91/src/context.rs index 2f2452bd0..22629fe5b 100644 --- a/embassy-net-nrf91/src/context.rs +++ b/embassy-net-nrf91/src/context.rs | |||
| @@ -95,6 +95,8 @@ impl<'a> Control<'a> { | |||
| 95 | /// | 95 | /// |
| 96 | /// NOTE: This will disconnect the modem from any current APN and should not | 96 | /// NOTE: This will disconnect the modem from any current APN and should not |
| 97 | /// be called if the configuration has not been changed. | 97 | /// be called if the configuration has not been changed. |
| 98 | /// | ||
| 99 | /// After configuring, invoke [`enable()`] to activate the configuration. | ||
| 98 | pub async fn configure(&self, config: &Config<'_>) -> Result<(), Error> { | 100 | pub async fn configure(&self, config: &Config<'_>) -> Result<(), Error> { |
| 99 | let mut cmd: [u8; 256] = [0; 256]; | 101 | let mut cmd: [u8; 256] = [0; 256]; |
| 100 | let mut buf: [u8; 256] = [0; 256]; | 102 | let mut buf: [u8; 256] = [0; 256]; |
| @@ -131,22 +133,6 @@ impl<'a> Control<'a> { | |||
| 131 | // info!("RES2: {}", unsafe { core::str::from_utf8_unchecked(&buf[..n]) }); | 133 | // info!("RES2: {}", unsafe { core::str::from_utf8_unchecked(&buf[..n]) }); |
| 132 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; | 134 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; |
| 133 | 135 | ||
| 134 | let op = CommandBuilder::create_set(&mut cmd, true) | ||
| 135 | .named("+CFUN") | ||
| 136 | .with_int_parameter(1) | ||
| 137 | .finish() | ||
| 138 | .map_err(|_| Error::BufferTooSmall)?; | ||
| 139 | let n = self.control.at_command(op, &mut buf).await; | ||
| 140 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; | ||
| 141 | |||
| 142 | let op = CommandBuilder::create_set(&mut cmd, true) | ||
| 143 | .named("%XPDNCFG") | ||
| 144 | .with_int_parameter(1) | ||
| 145 | .finish() | ||
| 146 | .map_err(|_| Error::BufferTooSmall)?; | ||
| 147 | let n = self.control.at_command(op, &mut buf).await; | ||
| 148 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; | ||
| 149 | |||
| 150 | Ok(()) | 136 | Ok(()) |
| 151 | } | 137 | } |
| 152 | 138 | ||
| @@ -301,20 +287,49 @@ impl<'a> Control<'a> { | |||
| 301 | Ok(status) | 287 | Ok(status) |
| 302 | } | 288 | } |
| 303 | 289 | ||
| 304 | /// Run a control loop for this context, ensuring that reaattach is handled. | 290 | /// Disable modem |
| 305 | pub async fn run<F: Fn(&Status)>(&self, reattach: F) -> Result<(), Error> { | 291 | pub async fn disable(&self) -> Result<(), Error> { |
| 292 | let mut cmd: [u8; 256] = [0; 256]; | ||
| 293 | let mut buf: [u8; 256] = [0; 256]; | ||
| 294 | |||
| 295 | let op = CommandBuilder::create_set(&mut cmd, true) | ||
| 296 | .named("+CFUN") | ||
| 297 | .with_int_parameter(0) | ||
| 298 | .finish() | ||
| 299 | .map_err(|_| Error::BufferTooSmall)?; | ||
| 300 | let n = self.control.at_command(op, &mut buf).await; | ||
| 301 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; | ||
| 302 | |||
| 303 | Ok(()) | ||
| 304 | } | ||
| 305 | |||
| 306 | /// Enable modem | ||
| 307 | pub async fn enable(&self) -> Result<(), Error> { | ||
| 306 | let mut cmd: [u8; 256] = [0; 256]; | 308 | let mut cmd: [u8; 256] = [0; 256]; |
| 307 | let mut buf: [u8; 256] = [0; 256]; | 309 | let mut buf: [u8; 256] = [0; 256]; |
| 308 | 310 | ||
| 309 | // Make sure modem is enabled | ||
| 310 | let op = CommandBuilder::create_set(&mut cmd, true) | 311 | let op = CommandBuilder::create_set(&mut cmd, true) |
| 311 | .named("+CFUN") | 312 | .named("+CFUN") |
| 312 | .with_int_parameter(1) | 313 | .with_int_parameter(1) |
| 313 | .finish() | 314 | .finish() |
| 314 | .map_err(|_| Error::BufferTooSmall)?; | 315 | .map_err(|_| Error::BufferTooSmall)?; |
| 316 | let n = self.control.at_command(op, &mut buf).await; | ||
| 317 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; | ||
| 315 | 318 | ||
| 319 | // Make modem survive PDN detaches | ||
| 320 | let op = CommandBuilder::create_set(&mut cmd, true) | ||
| 321 | .named("%XPDNCFG") | ||
| 322 | .with_int_parameter(1) | ||
| 323 | .finish() | ||
| 324 | .map_err(|_| Error::BufferTooSmall)?; | ||
| 316 | let n = self.control.at_command(op, &mut buf).await; | 325 | let n = self.control.at_command(op, &mut buf).await; |
| 317 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; | 326 | CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?; |
| 327 | Ok(()) | ||
| 328 | } | ||
| 329 | |||
| 330 | /// Run a control loop for this context, ensuring that reaattach is handled. | ||
| 331 | pub async fn run<F: Fn(&Status)>(&self, reattach: F) -> Result<(), Error> { | ||
| 332 | self.enable().await?; | ||
| 318 | let status = self.wait_attached().await?; | 333 | let status = self.wait_attached().await?; |
| 319 | let mut fd = self.control.open_raw_socket().await; | 334 | let mut fd = self.control.open_raw_socket().await; |
| 320 | reattach(&status); | 335 | reattach(&status); |
