(
dev: &device::Device,
dp: &device_profile::DeviceProfile,
rx_info: &[gw::UplinkRxInfo],
pl: clocksync::v2::AppTimeReqPayload,
)
| 105 | } |
| 106 | |
| 107 | async 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)] |
| 157 | mod test { |
no test coverage detected