Skip to content

Commit fca55c4

Browse files
refactor clock and ntp tzinfo
1 parent 7927f36 commit fca55c4

File tree

6 files changed

+66
-55
lines changed

6 files changed

+66
-55
lines changed

examples/pico-embassy/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async fn main(spawner: Spawner) -> ! {
111111
});
112112

113113
modem
114-
.send_and_wait_reply(at_command::ntp::NTPTime {})
114+
.send_and_wait_reply(at_command::ntp::Clock {})
115115
.await
116116
.unwrap();
117117

examples/pico/src/main.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,28 +174,30 @@ fn main() -> ! {
174174
.send_and_wait_reply(&at_command::at_creg::NetworkRegistration {})
175175
.unwrap();
176176

177-
delay.delay_ms(4000);
178-
let _ = modem
179-
.send_and_wait_reply(&at_command::ntp::StartQueryNTP {
180-
url: "202.112.29.82",
181-
tzinfo: None,
182-
})
183-
.or_else(|e| {
184-
warn!("failed starting ntp connection. Connection already established?");
185-
return Err(e);
186-
});
187-
188-
modem
189-
.send_and_wait_reply(&at_command::ntp::NTPTime {})
177+
let time = modem
178+
.send_and_wait_reply(&at_command::clock::Clock {})
190179
.unwrap();
191-
192-
delay.delay_ms(4000);
193-
let _ = modem
194-
.send_and_wait_reply(&at_command::ntp::StopQueryNTP {})
195-
.or_else(|e| {
196-
warn!("failed stopping ntp connection. Connection already established?");
197-
return Err(e);
198-
});
180+
info!("network time: {}", time);
181+
//
182+
// delay.delay_ms(4000);
183+
// let _ = modem
184+
// .send_and_wait_reply(&at_command::ntp::StartQueryNTP {
185+
// url: "pool.ntp.org",
186+
// tzinfo: Some("+32"),
187+
// })
188+
// .or_else(|e| {
189+
// warn!("failed starting ntp connection. Connection already established?");
190+
// return Err(e);
191+
// });
192+
//
193+
// delay.delay_ms(10000);
194+
// let _ = modem
195+
// .send_and_wait_reply(&at_command::ntp::StopQueryNTP {})
196+
// .or_else(|e| {
197+
// warn!("failed stopping ntp connection. Connection already established?");
198+
// return Err(e);
199+
// });
200+
// // here we should expect an unsolicited ntp response
199201

200202
// if let Err(e) = test_http_connection(&mut modem) {
201203
// error!("http test failed");

src/at_command/clock.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::at_command::{AtRequest, AtResponse, BufferType};
2+
use crate::AtError;
3+
use chrono::NaiveDateTime;
4+
5+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6+
pub struct Clock {}
7+
8+
impl AtRequest for Clock {
9+
type Response = Result<(), AtError>;
10+
fn get_command<'a>(&'a self, buffer: &'a mut BufferType) -> Result<&'a [u8], usize> {
11+
at_commands::builder::CommandBuilder::create_query(buffer, true)
12+
.named("+CCLK")
13+
.finish()
14+
}
15+
16+
fn parse_response(&self, data: &[u8]) -> Result<AtResponse, AtError> {
17+
let (parsed,) = at_commands::parser::CommandParser::parse(data)
18+
.expect_identifier(b"\r\n+CCLK: ")
19+
.expect_raw_string()
20+
.expect_identifier(b"\r\n\r\nOK")
21+
.finish()?;
22+
// 00/01/01,00:07:50+32 // +32 means east according to datasheet. Need to understand how to interpret
23+
// these zone infos
24+
let timestamp = NaiveDateTime::parse_from_str(&parsed[..17], "%y/%m/%d,%H:%M:%S").unwrap();
25+
Ok(AtResponse::NTPTimestamp(timestamp.and_utc().timestamp()))
26+
}
27+
}

src/at_command/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod ate;
2121
pub mod ati;
2222
pub mod ceer;
2323
pub mod cgcontrdp;
24+
pub mod clock;
2425
pub mod cmee;
2526
pub(crate) mod flow_control;
2627
pub mod http;

src/at_command/model_identification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl AtRequest for ModelIdentification {
1919
let (parsed,) = at_commands::parser::CommandParser::parse(data)
2020
.expect_identifier(b"\r\n")
2121
.expect_raw_string()
22-
.expect_identifier(b"\r\n\r\nOK\r")
22+
.expect_identifier(b"\r\n\r\nOK")
2323
.finish()
2424
.inspect(|e| {
2525
#[cfg(feature = "defmt")]

src/at_command/ntp.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
use crate::at_command::{AtRequest, AtResponse, BufferType};
1+
use crate::at_command::{AtRequest, BufferType};
22
use crate::AtError;
3-
use chrono::NaiveDateTime;
43

54
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
65
pub struct StartQueryNTP<'a> {
76
pub url: &'a str,
8-
pub tzinfo: Option<i8>,
7+
pub tzinfo: Option<&'a str>, // currently not implemented
98
}
109

1110
impl AtRequest for StartQueryNTP<'_> {
1211
type Response = Result<(), AtError>;
1312

1413
fn get_command<'a>(&'a self, buffer: &'a mut BufferType) -> Result<&'a [u8], usize> {
15-
at_commands::builder::CommandBuilder::create_set(buffer, true)
16-
.named("+CSNTPSTART")
17-
.with_string_parameter(self.url)
18-
.with_optional_int_parameter(self.tzinfo)
19-
.finish()
14+
match &self.tzinfo {
15+
None => at_commands::builder::CommandBuilder::create_set(buffer, true)
16+
.named("+CSNTPSTART")
17+
.with_string_parameter(self.url)
18+
.finish(),
19+
Some(tzinfo) => at_commands::builder::CommandBuilder::create_set(buffer, true)
20+
.named("+CSNTPSTART")
21+
.with_string_parameter(self.url)
22+
.with_string_parameter(tzinfo)
23+
.finish(),
24+
}
2025
}
2126
}
2227

@@ -32,27 +37,3 @@ impl AtRequest for StopQueryNTP {
3237
.finish()
3338
}
3439
}
35-
36-
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
37-
pub struct NTPTime {}
38-
39-
impl AtRequest for NTPTime {
40-
type Response = Result<(), AtError>;
41-
fn get_command<'a>(&'a self, buffer: &'a mut BufferType) -> Result<&'a [u8], usize> {
42-
at_commands::builder::CommandBuilder::create_query(buffer, true)
43-
.named("+CCLK")
44-
.finish()
45-
}
46-
47-
fn parse_response(&self, data: &[u8]) -> Result<AtResponse, AtError> {
48-
let (parsed,) = at_commands::parser::CommandParser::parse(data)
49-
.expect_identifier(b"\r\n+CCLK: ")
50-
.expect_raw_string()
51-
.expect_identifier(b"\r\n\r\nOK")
52-
.finish()?;
53-
// 00/01/01,00:07:50+32 // +32 means east according to datasheet. Need to understand how to interpret
54-
// these zone infos
55-
let timestamp = NaiveDateTime::parse_from_str(&parsed[..17], "%y/%m/%d,%H:%M:%S").unwrap();
56-
Ok(AtResponse::NTPTimestamp(timestamp.and_utc().timestamp()))
57-
}
58-
}

0 commit comments

Comments
 (0)