shield_sim7000/clients/sms/
mod.rs

1mod at_commands;
2
3use std::{thread, time::Duration};
4
5use self::at_commands::{CMGF, CMGS, MAX_SEND_SMS_SIZE};
6use crate::serial::SerialClient;
7use at_commands_crate::builder::CommandBuilder;
8use log::error;
9
10pub struct SMSClient {
11    client: SerialClient,
12}
13
14impl SMSClient {
15    pub fn new(client: SerialClient) -> Self {
16        Self { client }
17    }
18
19    pub fn enable(&mut self, bool: bool) {
20        let mut buffer = [0; 128];
21        let result = CommandBuilder::create_set(&mut buffer, true)
22            .named(CMGF)
23            .with_int_parameter(bool as i32)
24            .finish()
25            .unwrap();
26        self.client.send(result);
27    }
28
29    pub fn send_sms(&mut self, message: &str, number: &str) -> bool {
30        let mut buffer = [0; 1000];
31
32        let first_end: usize = 12 + number.len();
33        if message.len() > MAX_SEND_SMS_SIZE {
34            error!(
35                "Message is too long ({} chars), max is {}",
36                message.len(),
37                MAX_SEND_SMS_SIZE
38            );
39            return false;
40        }
41
42        CommandBuilder::create_set(&mut buffer[..first_end], true)
43            .named(CMGS)
44            .with_string_parameter(number)
45            .finish()
46            .unwrap();
47
48        if !self.client.send(&buffer[..first_end]) {
49            return false;
50        }
51
52        thread::sleep(Duration::from_millis(1000));
53        let second_end: usize = first_end + MAX_SEND_SMS_SIZE + 1;
54        CommandBuilder::create_execute(&mut buffer[first_end..second_end], false)
55            .named(&message[..MAX_SEND_SMS_SIZE])
56            .finish_with(b"\x1a")
57            .unwrap();
58
59        if !self.client.send(&buffer[first_end..second_end]) {
60            return false;
61        }
62        true
63    }
64}