seeed_lora_e5_at_commands/
client.rs1#[cfg(feature = "async")]
2pub mod asynch {
3 use crate::general::responses::VerResponse;
4 pub use atat::asynch::Client;
5 use atat::Error;
6 #[cfg(feature = "debug")]
7 use defmt::{error, info, warn};
8 pub use embedded_io_async::Write;
9 use heapless::String;
10
11 #[derive(Clone, Debug, Copy)]
12 pub enum JoinStatus {
13 Joining,
14 Success,
15 Failure,
16 NotJoined,
17 Unknown,
18 }
19
20 pub struct OtaaJoinStatus {
21 pub join_status: JoinStatus,
22 pub net_id: Option<String<12>>,
23 pub dev_addr: Option<String<22>>,
24 }
25
26 pub struct SeeedLoraE5Client<'a, W: Write, const INGRESS_BUF_SIZE: usize> {
27 pub(crate) client: Client<'a, W, INGRESS_BUF_SIZE>,
28 pub(crate) join_status: OtaaJoinStatus,
29 }
30
31 impl<'a, W: Write, const INGRESS_BUF_SIZE: usize> SeeedLoraE5Client<'a, W, INGRESS_BUF_SIZE> {
32 pub fn eject_client(self) -> Client<'a, W, INGRESS_BUF_SIZE> {
33 self.client
34 }
35 }
36
37 impl<'a, W: Write, const INGRESS_BUF_SIZE: usize> SeeedLoraE5Client<'a, W, INGRESS_BUF_SIZE> {
38 pub async fn new(
39 client: Client<'a, W, INGRESS_BUF_SIZE>,
40 ) -> Result<SeeedLoraE5Client<'a, W, INGRESS_BUF_SIZE>, Error> {
41 let mut s = Self {
42 client,
43 join_status: OtaaJoinStatus {
44 join_status: JoinStatus::NotJoined,
45 net_id: None,
46 dev_addr: None,
47 },
48 };
49
50 #[cfg(feature = "debug")]
51 if let Err(e) = s.verify_com_is_working().await {
52 error!("Error verifying Seeed LoRa-E5 comms: {:?}", e);
53 }
54
55 #[cfg(not(feature = "debug"))]
56 let _ = s.verify_com_is_working().await;
57 let mut count_down = 10;
62 while s.verify_com_is_working().await.is_err() && count_down > 0 {
63 #[cfg(feature = "debug")]
64 warn!("Waiting for LoRa-E5 to reset...");
65 count_down -= 1;
66 }
67 if count_down == 0 {
68 s.factory_reset().await?;
69 return Err(Error::Timeout);
70 }
71
72 #[cfg(feature = "debug")]
73 {
74 let version = s.version().await;
75 match version {
76 Err(e) => {
77 error!("Error getting Seeed LoRa-E5 firmware version: {:?}", e);
78 }
79 Ok(VerResponse {
80 major,
81 minor,
82 patch,
83 }) => {
84 info!(
85 "Seeed LoRa-E5 firmware version: {}.{}.{}",
86 major, minor, patch
87 );
88 }
89 }
90 }
91
92 Ok(s)
93 }
94 }
95}