Skip to content

Commit

Permalink
Stash changes to modem.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Slattery committed Oct 17, 2024
1 parent 09dac2b commit 6c55b0b
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 137 deletions.
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[build]
target = "riscv32imc-esp-espidf"
# target = "riscv32imc-esp-espidf"
#target = "xtensa-esp32-espidf"
target = "xtensa-esp32s3-espidf"

[target.xtensa-esp32-espidf]
linker = "ldproxy"
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ log = { version = "0.4", default-features = false }
uncased = { version = "0.9.7", default-features = false }
embedded-hal-async = { version = "1", default-features = false }
embedded-svc = { version = "0.28", default-features = false }
esp-idf-hal = { version = "0.44", default-features = false }
# esp-idf-hal = { version = "0.44", default-features = false }
esp-idf-hal ={path = "../esp-idf-hal"}
embassy-time-driver = { version = "0.1", optional = true, features = [
"tick-hz-1_000_000",
] }
Expand Down
8 changes: 8 additions & 0 deletions espflash.toml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
partition_table = "partitions.csv"

[connection]

[[usb_device]]
vid = "303a"
pid = "1001"

[flash]
268 changes: 133 additions & 135 deletions src/modem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{

pub struct EspModem<'d, T>
where
T: BorrowMut<UartDriver<'d>>,
T: BorrowMut<UartDriver<'d>> + Send,
{
serial: T,
status: Arc<mutex::Mutex<ModemDriverStatus>>,
Expand Down Expand Up @@ -59,14 +59,7 @@ where
self.netif.handle() as *mut core::ffi::c_void,
)
})?;
esp!(unsafe {
esp_event_handler_register(
NETIF_PPP_STATUS,
ESP_EVENT_ANY_ID as _,
Some(Self::raw_on_ppp_changed),
self.netif.handle() as *mut core::ffi::c_void,
)
})?;

let (mut tx, rx) = self.serial.borrow_mut().split();
let driver = EspNetifDriver::new_nonstatic(
&mut self.netif,
Expand Down Expand Up @@ -201,23 +194,21 @@ where
) {
Self::on_ip_event(event_id as _, event_data)
}
}

fn on_ppp_changed(event_id: u32, _event_data: *mut ::core::ffi::c_void) {
use log::info;
info!("Got event id ppp changed: {}", event_id);

if event_id == esp_netif_ppp_status_event_t_NETIF_PPP_ERRORUSER {
info!("user interrupted event from netif");
}
}

unsafe extern "C" fn raw_on_ppp_changed(
_event_handler_arg: *mut ::core::ffi::c_void,
_event_base: esp_event_base_t,
event_id: i32,
event_data: *mut ::core::ffi::c_void,
) {
Self::on_ppp_changed(event_id as _, event_data)
impl<'d, T> Drop for EspModem<'d, T>
where
T: BorrowMut<UartDriver<'d>> + Send,
{
fn drop(&mut self) {
esp!(unsafe {
esp_event_handler_unregister(
IP_EVENT,
ESP_EVENT_ANY_ID as _,
Some(Self::raw_on_ip_event),
)
})
.unwrap();
}
}

Expand Down Expand Up @@ -278,7 +269,6 @@ pub mod sim {
//!
//! Models a modem device with a sim card able to serve as a
//! network interface for the host.
use esp_idf_hal::uart::UartDriver;

/// The generic device trait. Implementations of this trait should provide
/// relevant AT commands and confirm the modem replies to drive the modem
Expand All @@ -288,7 +278,11 @@ pub mod sim {
fn get_mode(&self) -> &CommunicationMode;

/// Initialise the remote modem so that it is in PPPoS mode.
fn negotiate(&mut self, comm: &mut UartDriver, buffer: [u8; 64]) -> Result<(), ModemError>;
fn negotiate<T: embedded_svc::io::Write + embedded_svc::io::Read>(
&mut self,
comm: &mut T,
buffer: [u8; 64],
) -> Result<(), ModemError>;
}

/// State of the modem.
Expand Down Expand Up @@ -337,7 +331,7 @@ pub mod sim {
use core::fmt::Display;

use at_commands::{builder::CommandBuilder, parser::CommandParser};
use esp_idf_hal::{delay::TickType, uart::UartDriver};
use esp_idf_hal::{delay::TickType, io::BufReader, uart::UartDriver};

use super::{CommunicationMode, ModemError, SimModem};
pub struct SIM7600(CommunicationMode);
Expand Down Expand Up @@ -483,32 +477,33 @@ pub mod sim {
}

impl SimModem for SIM7600 {
fn negotiate(
fn negotiate<T: embedded_svc::io::Write + embedded_svc::io::Read>(
&mut self,
comm: &mut UartDriver,
comm: &mut T,
mut buffer: [u8; 64],
) -> Result<(), ModemError> {
let mut read_buf = BufReader::new(T);
reset(comm, &mut buffer)?;

//disable echo
set_echo(comm, &mut buffer, false)?;
// //disable echo
// set_echo(comm, &mut buffer, false)?;

// get signal quality
let (rssi, ber) = get_signal_quality(comm, &mut buffer)?;
log::info!("RSSI = {rssi}");
log::info!("BER = {ber}");
// get iccid
let iccid = get_iccid(comm, &mut buffer)?;
log::info!("ICCID = [{}]", iccid);
// // get signal quality
// let (rssi, ber) = get_signal_quality(comm, &mut buffer)?;
// log::info!("RSSI = {rssi}");
// log::info!("BER = {ber}");
// // get iccid
// let iccid = get_iccid(comm, &mut buffer)?;
// log::info!("ICCID = [{}]", iccid);

// check pdp network reg
read_gprs_registration_status(comm, &mut buffer)?;
// // check pdp network reg
// read_gprs_registration_status(comm, &mut buffer)?;

//configure apn
set_pdp_context(comm, &mut buffer)?;
// //configure apn
// set_pdp_context(comm, &mut buffer)?;

// start ppp
set_data_mode(comm, &mut buffer)?;
// // start ppp
// set_data_mode(comm, &mut buffer)?;

self.0 = CommunicationMode::Data;
Ok(())
Expand Down Expand Up @@ -570,105 +565,108 @@ pub mod sim {
Ok(heapless::String::try_from(ccid).unwrap())
}

fn reset(comm: &mut UartDriver, buff: &mut [u8]) -> Result<(), ModemError> {
fn reset<T: embedded_svc::io::Write + embedded_svc::io::Read>(
comm: &mut T,
buff: &mut [u8],
) -> Result<(), ModemError> {
let cmd = CommandBuilder::create_execute(buff, false)
.named("ATZ0")
.finish()?;
log::info!("Send Reset");

comm.write(cmd).map_err(|_| ModemError::IO)?;

let len = comm
.read(buff, TickType::new_millis(1000).ticks())
.map_err(|_| ModemError::IO)?;
log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));
CommandParser::parse(&buff[..len])
.expect_identifier(b"ATZ0\r")
.expect_identifier(b"\r\nOK\r\n")
.finish()?;
Ok(())
}

fn set_echo(comm: &mut UartDriver, buff: &mut [u8], echo: bool) -> Result<(), ModemError> {
let cmd = CommandBuilder::create_execute(buff, false)
.named(format!("ATE{}", i32::from(echo)))
.finish()?;
log::info!("Set echo ");
comm.write(cmd).map_err(|_| ModemError::IO)?;
let len = comm
.read(buff, TickType::new_millis(1000).ticks())
.map_err(|_| ModemError::IO)?;
log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

CommandParser::parse(&buff[..len])
.expect_identifier(b"ATE0\r")
.expect_identifier(b"\r\nOK\r\n")
.finish()?;
Ok(())
}

fn read_gprs_registration_status(
comm: &mut UartDriver,
buff: &mut [u8],
) -> Result<(i32, i32, Option<i32>, Option<i32>), ModemError> {
let cmd = CommandBuilder::create_query(buff, true)
.named("+CGREG")
.finish()?;
log::info!("Get Registration Status");
comm.write(cmd).map_err(|_| ModemError::IO)?;
let len = comm
.read(buff, TickType::new_millis(1000).ticks())
.map_err(|_| ModemError::IO)?;
log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

Ok(CommandParser::parse(&buff[..len])
.expect_identifier(b"\r\n+CGREG: ")
.expect_int_parameter()
.expect_int_parameter()
.expect_optional_int_parameter()
.expect_optional_int_parameter()
.expect_identifier(b"\r\n\r\nOK\r\n")
.finish()?)
}

fn set_pdp_context(comm: &mut UartDriver, buff: &mut [u8]) -> Result<(), ModemError> {
let cmd = CommandBuilder::create_set(buff, true)
.named("+CGDCONT")
.with_int_parameter(1) // context id
.with_string_parameter("IP") // pdp type
.with_string_parameter("flolive.net") // apn
.finish()?;
log::info!("Set PDP Context");
comm.write(cmd).map_err(|_| ModemError::IO)?;
let len = comm
.read(buff, TickType::new_millis(1000).ticks())
.map_err(|_| ModemError::IO)?;
log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

CommandParser::parse(&buff[..len])
.expect_identifier(b"\r\nOK\r\n")
.finish()?;
// let len = comm
// .read(buff, TickType::new_millis(1000).ticks())
// .map_err(|_| ModemError::IO)?;
// log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));
// CommandParser::parse(&buff[..len])
// .expect_identifier(b"ATZ0\r")
// .expect_identifier(b"\r\nOK\r\n")
// .finish()?;
Ok(())
}

fn set_data_mode(comm: &mut UartDriver, buff: &mut [u8]) -> Result<(), ModemError> {
let cmd = CommandBuilder::create_execute(buff, false)
.named("ATD*99#")
.finish()?;
log::info!("Set Data mode");
comm.write(cmd).map_err(|_| ModemError::IO)?;
let len = comm
.read(buff, TickType::new_millis(1000).ticks())
.map_err(|_| ModemError::IO)?;
log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

let (connect_parm,) = CommandParser::parse(&buff[..len])
.expect_identifier(b"\r\nCONNECT ")
.expect_optional_raw_string()
.expect_identifier(b"\r\n")
.finish()?;
log::info!("connect {:?}", connect_parm);
Ok(())
}
// fn set_echo(comm: &mut UartDriver, buff: &mut [u8], echo: bool) -> Result<(), ModemError> {
// let cmd = CommandBuilder::create_execute(buff, false)
// .named(format!("ATE{}", i32::from(echo)))
// .finish()?;
// log::info!("Set echo ");
// comm.write(cmd).map_err(|_| ModemError::IO)?;
// let len = comm
// .read(buff, TickType::new_millis(1000).ticks())
// .map_err(|_| ModemError::IO)?;
// log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

// CommandParser::parse(&buff[..len])
// .expect_identifier(b"ATE0\r")
// .expect_identifier(b"\r\nOK\r\n")
// .finish()?;
// Ok(())
// }

// fn read_gprs_registration_status(
// comm: &mut UartDriver,
// buff: &mut [u8],
// ) -> Result<(i32, i32, Option<i32>, Option<i32>), ModemError> {
// let cmd = CommandBuilder::create_query(buff, true)
// .named("+CGREG")
// .finish()?;
// log::info!("Get Registration Status");
// comm.write(cmd).map_err(|_| ModemError::IO)?;
// let len = comm
// .read(buff, TickType::new_millis(1000).ticks())
// .map_err(|_| ModemError::IO)?;
// log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

// Ok(CommandParser::parse(&buff[..len])
// .expect_identifier(b"\r\n+CGREG: ")
// .expect_int_parameter()
// .expect_int_parameter()
// .expect_optional_int_parameter()
// .expect_optional_int_parameter()
// .expect_identifier(b"\r\n\r\nOK\r\n")
// .finish()?)
// }

// fn set_pdp_context(comm: &mut UartDriver, buff: &mut [u8]) -> Result<(), ModemError> {
// let cmd = CommandBuilder::create_set(buff, true)
// .named("+CGDCONT")
// .with_int_parameter(1) // context id
// .with_string_parameter("IP") // pdp type
// .with_string_parameter("flolive.net") // apn
// .finish()?;
// log::info!("Set PDP Context");
// comm.write(cmd).map_err(|_| ModemError::IO)?;
// let len = comm
// .read(buff, TickType::new_millis(1000).ticks())
// .map_err(|_| ModemError::IO)?;
// log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

// CommandParser::parse(&buff[..len])
// .expect_identifier(b"\r\nOK\r\n")
// .finish()?;
// Ok(())
// }

// fn set_data_mode(comm: &mut UartDriver, buff: &mut [u8]) -> Result<(), ModemError> {
// let cmd = CommandBuilder::create_execute(buff, false)
// .named("ATD*99#")
// .finish()?;
// log::info!("Set Data mode");
// comm.write(cmd).map_err(|_| ModemError::IO)?;
// let len = comm
// .read(buff, TickType::new_millis(1000).ticks())
// .map_err(|_| ModemError::IO)?;
// log::info!("got response{:?}", std::str::from_utf8(&buff[..len]));

// let (connect_parm,) = CommandParser::parse(&buff[..len])
// .expect_identifier(b"\r\nCONNECT ")
// .expect_optional_raw_string()
// .expect_identifier(b"\r\n")
// .finish()?;
// log::info!("connect {:?}", connect_parm);
// Ok(())
// }
}
}

0 comments on commit 6c55b0b

Please sign in to comment.