http post - not working because sim800l supports tlsv1.0 only

This commit is contained in:
Vladan Popovic 2022-07-03 02:27:36 +02:00
parent edf427dcb1
commit a215c628a7
3 changed files with 179 additions and 107 deletions

View file

@ -165,26 +165,25 @@ impl<UART: serial::Uart> Modem<UART> {
}
#[inline(always)]
fn send_bytes(&mut self, payload: &[u8], eos: char) -> Result<()> {
fn send_bytes(&mut self, payload: &[u8], eos: Option<u8>) -> Result<()> {
self.rx.clear();
for b in payload.iter() {
nb::block!(self.tx.write(*b))
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
}
nb::block!(self.tx.write(eos as u8))
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", eos)))?;
eos.map(|b| nb::block!(self.tx.write(b)));
Ok(())
}
fn send_command(&mut self, cmd: Command) -> Result<String> {
println!("-----------------------------------------------------------");
println!("Sending {} ...", cmd.text);
let _ = self.send_bytes(cmd.text.as_bytes(), '\r')?;
let _ = self.send_bytes(cmd.text.as_bytes(), Some('\r' as u8))?;
self.read_response(cmd.contains, cmd.timeout)
}
fn send_data(&mut self, buf: &[u8]) -> Result<String> {
self.rx.clear();
let _ = self.send_bytes("AT+CIPSEND".as_bytes(), '\r')?;
fn tcp_send_data(&mut self, buf: &[u8]) -> Result<String> {
let _ = self.send_bytes("AT+CIPSEND".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();
@ -193,10 +192,9 @@ impl<UART: serial::Uart> Modem<UART> {
return Err(ModemError::SendDataError);
}
self.send_bytes(buf, 26 as char)?; // 26_u8 = Ctrl+z - to end sending data
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));
self.rx.clear();
let res = self.send_command(Command {
text: "AT+CIPACK".to_string(),
contains: Some("OK".to_string()),
@ -205,11 +203,11 @@ impl<UART: serial::Uart> Modem<UART> {
Ok(res)
}
pub fn get_ip_addr(&mut self) -> Result<String> {
self.send_command(Command::getbear())
pub fn gprs_status(&mut self) -> Result<String> {
self.send_command(Command::gprs_bearer_status())
}
pub fn connect_to_gprs_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<()> {
pub fn gprs_attach_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<()> {
println!("init gprs ...");
let _ = self.send_command(Command::gprs_init())?;
@ -219,8 +217,12 @@ impl<UART: serial::Uart> Modem<UART> {
let _ = self.send_command(Command::gprs_set_user(username))?;
let _ = self.send_command(Command::gprs_set_pwd(password))?;
Ok(())
}
pub fn gprs_connect(&mut self)-> Result<()> {
println!("open gprs ...");
let _ = self.send_command(Command::gprs_open())?;
let _ = self.send_command(Command::gprs_bearer_open())?;
Ok(())
}
@ -261,7 +263,7 @@ impl<UART: serial::Uart> Modem<UART> {
}
pub fn tcp_send(&mut self, buf: &[u8]) -> Result<()> {
self.send_data(buf)?;
self.tcp_send_data(buf)?;
Ok(())
}
@ -313,4 +315,42 @@ impl<UART: serial::Uart> Modem<UART> {
pub fn tcp_close_connection(&mut self) -> Result<String> {
self.send_command(Command::tcp_close())
}
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_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_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())
}
pub fn http_close(&mut self) -> Result<()> {
let _ = self.send_command(Command::http_close())?;
Ok(())
}
pub fn chip_info(&mut self) -> Result<()> {
let _ = self.send_command(Command::manufacturer_id())?;
thread::sleep(Duration::from_millis(1000));
let _ = self.send_command(Command::model_id())?;
thread::sleep(Duration::from_millis(1000));
let _ = self.send_command(Command::release_id())?;
Ok(())
}
pub fn location(&mut self) -> Result<()> {
let _ = self.send_command(Command::get_location())?;
Ok(())
}
pub fn ssl_opt(&mut self) -> Result<()> {
let _ = self.send_command(Command::ssl_opt())?;
Ok(())
}
}