mqtt works ... connect + publish
This commit is contained in:
parent
a215c628a7
commit
d023f5db76
3 changed files with 202 additions and 87 deletions
120
src/modem.rs
120
src/modem.rs
|
@ -140,12 +140,10 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Reads the serial RX until a \\n char is encoutered, or a timeout is reached. The timeout is
|
||||
/// provided on input via the `timeout` argument. The first argument `contains` is checked
|
||||
/// against a line in the response, if it's there the reading stops.
|
||||
///
|
||||
/// If `contains` is `None`, the first line only is returned in the response. If it's
|
||||
/// `Some(match_txt)`, then the end of the response is matched against `match_txt`.
|
||||
/// Reads the serial RX until the `contains` string is encoutered if `contains` is Some(s), if
|
||||
/// None, then the first line is returned. If a timeout is reached. The timeout is provided on
|
||||
/// input via the `timeout` argument. The first argument `contains` is checked against every
|
||||
/// line in the response.
|
||||
fn read_response(&mut self, contains: Option<String>, timeout: Duration) -> Result<String> {
|
||||
let mut response = String::new();
|
||||
let start = Instant::now();
|
||||
|
@ -166,7 +164,7 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
|
||||
#[inline(always)]
|
||||
fn send_bytes(&mut self, payload: &[u8], eos: Option<u8>) -> Result<()> {
|
||||
self.rx.clear();
|
||||
//self.rx.clear();
|
||||
for b in payload.iter() {
|
||||
nb::block!(self.tx.write(*b))
|
||||
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
|
||||
|
@ -188,19 +186,13 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
.map(char::from)
|
||||
.take_while(|c| *c != '>').collect();
|
||||
|
||||
if send_request == "" {
|
||||
if send_request != "\r\n" {
|
||||
println!("{:?}", send_request.as_bytes());
|
||||
return Err(ModemError::SendDataError);
|
||||
}
|
||||
|
||||
self.send_bytes(buf, Some(26))?; // 26_u8 = Ctrl+z - to end sending data
|
||||
let _ = self.read_response(Some("DATA ACCEPT".to_string()), Duration::from_millis(3000));
|
||||
|
||||
let res = self.send_command(Command {
|
||||
text: "AT+CIPACK".to_string(),
|
||||
contains: Some("OK".to_string()),
|
||||
timeout: Duration::from_millis(3000),
|
||||
})?;
|
||||
Ok(res)
|
||||
self.read_response(Some("SEND OK".to_string()), Duration::from_millis(3000))
|
||||
}
|
||||
|
||||
pub fn gprs_status(&mut self) -> Result<String> {
|
||||
|
@ -257,8 +249,8 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn tcp_set_manual_receive(&mut self) -> Result<()> {
|
||||
self.send_command(Command::tcp_set_manual_receive())?;
|
||||
pub fn tcp_set_manual_receive(&mut self, is_manual: bool) -> Result<()> {
|
||||
self.send_command(Command::tcp_set_manual_receive(is_manual))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -289,25 +281,22 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
pub fn tcp_receive(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut size = 0;
|
||||
loop {
|
||||
let reply: usize = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
|
||||
.map(|reply: String| {
|
||||
reply.lines()
|
||||
.map(|line| {
|
||||
if !line.starts_with("\r") && !line.contains("+CIPRXGET: 2,") && !line.contains("OK") {
|
||||
line.chars().enumerate().map(|(idx, c)| { buf[size + idx] = c as u8; }).count()
|
||||
}
|
||||
else {
|
||||
0
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
let reply = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
|
||||
.map(|reply| {
|
||||
reply
|
||||
.split("\r\n")
|
||||
.filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,"))
|
||||
.next()
|
||||
.map(|line| line.chars().enumerate().map(|(idx, c)| buf[size + idx] = c as u8).count())
|
||||
})?;
|
||||
if reply == 0 {
|
||||
break Ok(size)
|
||||
}
|
||||
else {
|
||||
size += reply;
|
||||
continue
|
||||
match reply {
|
||||
Some(0) | None => {
|
||||
break Ok(size)
|
||||
},
|
||||
Some(x) => {
|
||||
size += x;
|
||||
continue
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -318,16 +307,26 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
|
||||
pub fn http_post(&mut self, url: &str, token: &str, content: &[u8]) -> Result<String> {
|
||||
let _ = self.send_command(Command::http_init());
|
||||
let _ = self.send_command(Command::http_set_ssl(true));
|
||||
let _ = self.send_command(Command::http_set_cid());
|
||||
let _ = self.send_command(Command::http_init_url(url));
|
||||
let _ = self.send_command(Command::http_set_url(url));
|
||||
let _ = self.send_command(Command::http_set_header("X-Secret", token));
|
||||
let _ = self.send_command(Command::http_set_header("X-Topic", "device-dev"));
|
||||
let _ = self.send_command(Command::http_set_content("application/json"));
|
||||
let _ = self.send_command(Command::http_set_ssl(true));
|
||||
let _ = self.send_command(Command::http_post_len(content.len(), 100000));
|
||||
let _ = self.send_bytes(content, Some(26));
|
||||
let _ = self.send_command(Command::http_post());
|
||||
self.send_command(Command::http_response())
|
||||
self.send_command(Command::http_read_response())
|
||||
}
|
||||
|
||||
pub fn http_get(&mut self, url: &str) -> Result<String> {
|
||||
let _ = self.send_command(Command::http_init());
|
||||
let _ = self.send_command(Command::http_set_cid());
|
||||
let _ = self.send_command(Command::http_set_url(url));
|
||||
let _ = self.send_command(Command::http_set_redirect(true));
|
||||
let _ = self.send_command(Command::http_set_ssl(true));
|
||||
let _ = self.send_command(Command::http_get());
|
||||
self.send_command(Command::http_read_response())
|
||||
}
|
||||
|
||||
pub fn http_close(&mut self) -> Result<()> {
|
||||
|
@ -353,4 +352,47 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
let _ = self.send_command(Command::ssl_opt())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn file_write(&mut self, buf: &[u8], path: &str, append: bool, input_time_sec: usize) -> Result<()> {
|
||||
let cmd = Command::fs_file_write(path, append, buf.len(), input_time_sec);
|
||||
let _ = self.send_bytes(cmd.text.as_bytes(), Some('\r' as u8))?;
|
||||
let send_request: String = self.rx.reset(Duration::from_millis(3000))
|
||||
.map(char::from)
|
||||
.take_while(|c| *c != '>').collect();
|
||||
|
||||
if send_request == "" {
|
||||
return Err(ModemError::SendDataError);
|
||||
}
|
||||
|
||||
self.send_bytes(buf, None)?;
|
||||
let _ = self.read_response(Some("OK".to_string()), Duration::from_millis(3000));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn upload_cert(&mut self, path: &str, cert: &[u8]) -> Result<()> {
|
||||
let _ = self.send_command(Command::fs_file_create(path))?;
|
||||
let _ = self.file_write(cert, path, false, 20000)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn fs_list(&mut self, path: &str) -> Result<()> {
|
||||
let _ = self.send_command(Command::fs_list(path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn fs_free_space(&mut self) -> Result<()> {
|
||||
let _ = self.send_command(Command::fs_free_size())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ssl_set_client_cert(&mut self, path: &str, password: &str) -> Result<()> {
|
||||
let _ = self.send_command(Command::ssl_set_client_cert(path, password))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ssl_set_root_cert(&mut self, path: &str, filesize: usize) -> Result<()> {
|
||||
let _ = self.send_command(Command::ssl_set_root_cert(path, filesize))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue