MCPcopy Create free account
hub / github.com/chirpstack/chirpstack / handle_v1_app_time_req

Function handle_v1_app_time_req

chirpstack/src/applayer/clocksync.rs:58–105  ·  view source on GitHub ↗
(
    dev: &device::Device,
    dp: &device_profile::DeviceProfile,
    rx_info: &[gw::UplinkRxInfo],
    pl: clocksync::v1::AppTimeReqPayload,
)

Source from the content-addressed store, hash-verified

56}
57
58async fn handle_v1_app_time_req(
59 dev: &device::Device,
60 dp: &device_profile::DeviceProfile,
61 rx_info: &[gw::UplinkRxInfo],
62 pl: clocksync::v1::AppTimeReqPayload,
63) -> Result<()> {
64 info!("Handling AppTimeReq");
65
66 let now_time_since_gps = if let Some(t) = helpers::get_time_since_gps_epoch(rx_info) {
67 chrono::Duration::from_std(t)?
68 } else {
69 helpers::get_rx_timestamp_chrono(rx_info).to_gps_time()
70 };
71 let dev_time_since_gps = chrono::Duration::seconds(pl.device_time.into());
72
73 let time_diff = (now_time_since_gps - dev_time_since_gps).num_seconds();
74 let time_correction: i32 = if time_diff < 0 {
75 time_diff.try_into().unwrap_or(i32::MIN)
76 } else {
77 time_diff.try_into().unwrap_or(i32::MAX)
78 };
79
80 if time_diff == 0 && !pl.param.ans_required {
81 return Ok(());
82 }
83
84 info!(
85 time_correcrtion = time_correction,
86 "Responding with AppTimeAns"
87 );
88
89 let ans = clocksync::v1::Payload::AppTimeAns(clocksync::v1::AppTimeAnsPayload {
90 time_correction,
91 param: clocksync::v1::AppTimeAnsPayloadParam {
92 token_ans: pl.param.token_req,
93 },
94 });
95
96 device_queue::enqueue_item(device_queue::DeviceQueueItem {
97 dev_eui: dev.dev_eui,
98 f_port: dp.app_layer_params.ts003_f_port.into(),
99 data: ans.to_vec()?,
100 ..Default::default()
101 })
102 .await?;
103
104 Ok(())
105}
106
107async fn handle_v2_app_time_req(
108 dev: &device::Device,

Callers 1

handle_uplink_v100Function · 0.85

Calls 6

get_time_since_gps_epochFunction · 0.85
get_rx_timestamp_chronoFunction · 0.85
enqueue_itemFunction · 0.85
to_gps_timeMethod · 0.80
intoMethod · 0.45
to_vecMethod · 0.45

Tested by

no test coverage detected