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

Function handle_v2_app_time_req

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

Source from the content-addressed store, hash-verified

105}
106
107async fn handle_v2_app_time_req(
108 dev: &device::Device,
109 dp: &device_profile::DeviceProfile,
110 rx_info: &[gw::UplinkRxInfo],
111 pl: clocksync::v2::AppTimeReqPayload,
112) -> Result<()> {
113 info!("Handling AppTimeReq");
114
115 let now_time_since_gps = if let Some(t) = helpers::get_time_since_gps_epoch(rx_info) {
116 chrono::Duration::from_std(t)?
117 } else {
118 helpers::get_rx_timestamp_chrono(rx_info).to_gps_time()
119 };
120 let dev_time_since_gps = chrono::Duration::seconds(pl.device_time.into());
121
122 let time_diff = (now_time_since_gps - dev_time_since_gps).num_seconds();
123 let time_correction: i32 = if time_diff < 0 {
124 time_diff.try_into().unwrap_or(i32::MIN)
125 } else {
126 time_diff.try_into().unwrap_or(i32::MAX)
127 };
128
129 if time_diff == 0 && !pl.param.ans_required {
130 return Ok(());
131 }
132
133 info!(
134 time_correcrtion = time_correction,
135 "Responding with AppTimeAns"
136 );
137
138 let ans = clocksync::v2::Payload::AppTimeAns(clocksync::v2::AppTimeAnsPayload {
139 time_correction,
140 param: clocksync::v2::AppTimeAnsPayloadParam {
141 token_ans: pl.param.token_req,
142 },
143 });
144
145 device_queue::enqueue_item(device_queue::DeviceQueueItem {
146 dev_eui: dev.dev_eui,
147 f_port: dp.app_layer_params.ts003_f_port.into(),
148 data: ans.to_vec()?,
149 ..Default::default()
150 })
151 .await?;
152
153 Ok(())
154}
155
156#[cfg(test)]
157mod test {

Callers 1

handle_uplink_v200Function · 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