implement non-blocking read with timeout

much better than before :)
This commit is contained in:
Vladan Popovic 2022-06-19 00:33:32 +02:00
parent 94b9ce312f
commit 792eef13ba
4 changed files with 276 additions and 175 deletions

View file

@ -7,15 +7,7 @@ pub struct Command {
}
impl Command {
pub fn initgprs() -> Command {
Command {
text: "AT+SAPBR=3,1,\"Contype\",\"GPRS\"".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn modeminfo() -> Command {
pub fn modem_info() -> Command {
Command {
text: "ATI".to_string(),
timeout: Duration::from_millis(6000),
@ -23,7 +15,7 @@ impl Command {
}
}
pub fn fwrevision() -> Command {
pub fn fw_revision() -> Command {
Command {
text: "AT+CGMR".to_string(),
timeout: Duration::from_millis(3000),
@ -63,7 +55,7 @@ impl Command {
}
}
pub fn checkreg() -> Command {
pub fn check_reg() -> Command {
Command {
text: "AT+CREG?".to_string(),
timeout: Duration::from_millis(3000),
@ -71,7 +63,15 @@ impl Command {
}
}
pub fn opengprs() -> Command {
pub fn gprs_init() -> Command {
Command {
text: "AT+SAPBR=3,1,\"Contype\",\"GPRS\"".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn gprs_open() -> Command {
Command {
text: "AT+SAPBR=1,1".to_string(),
timeout: Duration::from_millis(3000),
@ -79,6 +79,30 @@ impl Command {
}
}
pub fn gprs_set_apn(apn: &str) -> Command {
Command {
text: format!("AT+SAPBR=3,1,\"APN\",\"{}\"", apn),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn gprs_set_user(user: &str) -> Command {
Command {
text: format!("AT+SAPBR=3,1,\"USER\",\"{}\"", user),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn gprs_set_pwd(password: &str) -> Command {
Command {
text: format!("AT+SAPBR=3,1,\"PWD\",\"{}\"", password),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn getbear() -> Command {
Command {
text: "AT+SAPBR=2,1".to_string(),
@ -87,7 +111,23 @@ impl Command {
}
}
pub fn inithttp() -> Command {
pub fn get_local_ip_addr() -> Command {
Command {
text: "AT+CIFSR".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn ping(domain: &str) -> Command {
Command {
text: format!("AT+CIPPING=\"{}\"", domain),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn http_init() -> Command {
Command {
text: "AT+HTTPINIT".to_string(),
timeout: Duration::from_millis(3000),
@ -95,7 +135,7 @@ impl Command {
}
}
pub fn sethttp() -> Command {
pub fn http_set() -> Command {
Command {
text: "AT+HTTPPARA=\"CID\",1".to_string(),
timeout: Duration::from_millis(3000),
@ -103,15 +143,7 @@ impl Command {
}
}
pub fn checkssl() -> Command {
Command {
text: "AT+CIPSSL=?".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn enablessl() -> Command {
pub fn http_enable_ssl() -> Command {
Command {
text: "AT+HTTPSSL=1".to_string(),
timeout: Duration::from_millis(3000),
@ -119,7 +151,7 @@ impl Command {
}
}
pub fn disablessl() -> Command {
pub fn http_disable_ssl() -> Command {
Command {
text: "AT+HTTPSSL=0".to_string(),
timeout: Duration::from_millis(3000),
@ -127,7 +159,7 @@ impl Command {
}
}
pub fn initurl() -> Command {
pub fn http_init_url() -> Command {
Command {
text: "AT+HTTPPARA=\"URL\",\"{}\"".to_string(),
timeout: Duration::from_millis(3000),
@ -135,7 +167,7 @@ impl Command {
}
}
pub fn doget() -> Command {
pub fn http_get() -> Command {
Command {
text: "AT+HTTPACTION=0".to_string(),
timeout: Duration::from_millis(3000),
@ -143,7 +175,7 @@ impl Command {
}
}
pub fn setcontent() -> Command {
pub fn http_set_content() -> Command {
Command {
text: "AT+HTTPPARA=\"CONTENT\",\"{}\"".to_string(),
timeout: Duration::from_millis(3000),
@ -151,7 +183,7 @@ impl Command {
}
}
pub fn postlen() -> Command {
pub fn http_post_len() -> Command {
Command {
text: "AT+HTTPDATA={}5000".to_string(),
timeout: Duration::from_millis(3000),
@ -159,7 +191,7 @@ impl Command {
}
}
pub fn dopost() -> Command {
pub fn http_post() -> Command {
Command {
text: "AT+HTTPACTION=1".to_string(),
timeout: Duration::from_millis(3000),
@ -167,7 +199,7 @@ impl Command {
}
}
pub fn getdata() -> Command {
pub fn http_get_data() -> Command {
Command {
text: "AT+HTTPREAD".to_string(),
timeout: Duration::from_millis(3000),
@ -191,30 +223,6 @@ impl Command {
}
}
pub fn setapn(apn: &str) -> Command {
Command {
text: format!("AT+SAPBR=3,1,\"APN\",\"{}\"", apn),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn setuser(user: &str) -> Command {
Command {
text: format!("AT+SAPBR=3,1,\"USER\",\"{}\"", user),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn setpwd(password: &str) -> Command {
Command {
text: format!("AT+SAPBR=3,1,\"PWD\",\"{}\"", password),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn probe() -> Command {
Command {
text: "AT".to_string(),
@ -230,4 +238,60 @@ impl Command {
contains: Some("OK".to_string()),
}
}
pub fn tcp_ssl_disable() -> Command {
Command {
text: "AT+CIPSSL=0".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn tcp_ssl_enable() -> Command {
Command {
text: "AT+CIPSSL=1".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn tcp_ssl_check() -> Command {
Command {
text: "AT+CIPSSL=?".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn tcp_connect(addr: &str, port: u16) -> Command {
Command {
text: format!("AT+CIPSTART=\"TCP\",\"{}\",\"{}\"", addr, port),
timeout: Duration::from_millis(5000),
contains: Some("CONNECT OK".to_string()),
}
}
pub fn tcp_send(size: usize) -> Command {
Command {
text: format!("AT+CIPSEND={}", size),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn tcp_write(payload: &str) -> Command {
Command {
text: payload.to_string(),
timeout: Duration::from_millis(2000),
contains: Some("> ".to_string()),
}
}
pub fn tcp_set_quick_mode(quick_mode: bool) -> Command {
Command {
text: format!("AT+CIPQSEND={}", quick_mode as u8),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
}