seeed_lora_e5_at_commands/
client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#[cfg(feature = "async")]
pub mod asynch {
    use crate::general::responses::VerResponse;
    pub use atat::asynch::Client;
    use atat::Error;
    #[cfg(feature = "debug")]
    use defmt::{error, info, warn};
    pub use embedded_io_async::Write;
    use heapless::String;

    #[derive(Clone, Debug, Copy)]
    pub enum JoinStatus {
        Joining,
        Success,
        Failure,
        NotJoined,
        Unknown,
    }

    pub struct OtaaJoinStatus {
        pub join_status: JoinStatus,
        pub net_id: Option<String<12>>,
        pub dev_addr: Option<String<22>>,
    }

    pub struct SeeedLoraE5Client<'a, W: Write, const INGRESS_BUF_SIZE: usize> {
        pub(crate) client: Client<'a, W, INGRESS_BUF_SIZE>,
        pub(crate) join_status: OtaaJoinStatus,
    }

    impl<'a, W: Write, const INGRESS_BUF_SIZE: usize> SeeedLoraE5Client<'a, W, INGRESS_BUF_SIZE> {
        pub fn eject_client(self) -> Client<'a, W, INGRESS_BUF_SIZE> {
            self.client
        }
    }

    impl<'a, W: Write, const INGRESS_BUF_SIZE: usize> SeeedLoraE5Client<'a, W, INGRESS_BUF_SIZE> {
        pub async fn new(
            client: Client<'a, W, INGRESS_BUF_SIZE>,
        ) -> Result<SeeedLoraE5Client<'a, W, INGRESS_BUF_SIZE>, Error> {
            let mut s = Self {
                client,
                join_status: OtaaJoinStatus {
                    join_status: JoinStatus::NotJoined,
                    net_id: None,
                    dev_addr: None,
                },
            };

            #[cfg(feature = "debug")]
            if let Err(e) = s.verify_com_is_working().await {
                error!("Error verifying Seeed LoRa-E5 comms: {:?}", e);
            }

            #[cfg(not(feature = "debug"))]
            let _ = s.verify_com_is_working().await;
            // if s.reset().await.is_err() {
            //     #[cfg(feature = "debug")]
            //     error!("Error resetting Seeed LoRa-E5");
            // }
            let mut count_down = 10;
            while s.verify_com_is_working().await.is_err() && count_down > 0 {
                #[cfg(feature = "debug")]
                warn!("Waiting for LoRa-E5 to reset...");
                count_down -= 1;
            }
            if count_down == 0 {
                s.factory_reset().await?;
                return Err(Error::Timeout);
            }

            #[cfg(feature = "debug")]
            {
                let version = s.version().await;
                match version {
                    Err(e) => {
                        error!("Error getting Seeed LoRa-E5 firmware version: {:?}", e);
                    }
                    Ok(VerResponse {
                        major,
                        minor,
                        patch,
                    }) => {
                        info!(
                            "Seeed LoRa-E5 firmware version: {}.{}.{}",
                            major, minor, patch
                        );
                    }
                }
            }

            Ok(s)
        }
    }
}