151
151
#[ cfg( feature = "utils" ) ]
152
152
pub mod utils;
153
153
154
+ mod log;
154
155
mod socket;
155
156
mod types;
156
157
157
158
pub use crate :: types:: * ;
158
159
159
160
#[ cfg( any( feature = "log" , feature = "defmt" ) ) ]
160
- use core :: str ;
161
+ use crate :: log :: debug ;
161
162
162
163
/// Network types used by the `sntpc` crate
163
164
pub mod net {
@@ -167,10 +168,7 @@ pub mod net {
167
168
pub use std:: net:: UdpSocket ;
168
169
}
169
170
170
- #[ cfg( feature = "defmt" ) ]
171
- use defmt:: debug;
172
- #[ cfg( all( feature = "log" , not( feature = "defmt" ) ) ) ]
173
- use log:: debug;
171
+ use cfg_if:: cfg_if;
174
172
175
173
/// Retrieves the current time from an NTP server.
176
174
///
@@ -605,19 +603,15 @@ where
605
603
/// Synchronous interface for the SNTP client
606
604
#[ cfg( feature = "sync" ) ]
607
605
pub mod sync {
606
+ #[ cfg( any( feature = "log" , feature = "defmt" ) ) ]
607
+ use crate :: log:: debug;
608
608
use crate :: net;
609
609
use crate :: types:: {
610
610
NtpContext , NtpResult , NtpTimestampGenerator , NtpUdpSocket , Result ,
611
611
SendRequestResult ,
612
612
} ;
613
613
614
614
use miniloop:: executor:: Executor ;
615
-
616
- #[ cfg( feature = "defmt" ) ]
617
- use defmt:: debug;
618
- #[ cfg( all( feature = "log" , not( feature = "defmt" ) ) ) ]
619
- use log:: debug;
620
-
621
615
/// Send request to a NTP server with the given address and process the response in a single call
622
616
///
623
617
/// May be useful under an environment with `std` networking implementation, where all
@@ -814,7 +808,6 @@ pub mod sync {
814
808
/// // you would want to fix destination address, since string hostname may resolve to
815
809
/// // different IP addresses
816
810
/// let addr = "time.google.com:123".to_socket_addrs().unwrap().filter(SocketAddr::is_ipv4).next().unwrap();
817
- ///
818
811
/// let send_request_result = sntpc::sync::sntp_send_request(addr, &socket, context).unwrap();
819
812
/// let result = sntpc::sync::sntp_process_response(addr, &socket, context, send_request_result);
820
813
///
@@ -858,8 +851,13 @@ fn process_response(
858
851
let mut packet = NtpPacket :: from ( resp) ;
859
852
860
853
convert_from_network ( & mut packet) ;
861
- #[ cfg( any( feature = "log" , feature = "defmt" ) ) ]
862
- debug_ntp_packet ( & packet, recv_timestamp) ;
854
+
855
+ cfg_if ! (
856
+ if #[ cfg( any( feature = "log" , feature = "defmt" ) ) ] {
857
+ let debug_packet = DebugNtpPacket :: new( & packet, recv_timestamp) ;
858
+ debug!( "{:#?}" , debug_packet) ;
859
+ }
860
+ ) ;
863
861
864
862
if send_req_result. originate_timestamp != packet. origin_timestamp {
865
863
return Err ( Error :: IncorrectOriginTimestamp ) ;
@@ -994,72 +992,6 @@ fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 {
994
992
}
995
993
}
996
994
997
- #[ cfg( feature = "defmt" ) ]
998
- fn debug_ntp_packet ( packet : & NtpPacket , recv_timestamp : u64 ) {
999
- let mode = shifter ( packet. li_vn_mode , MODE_MASK , MODE_SHIFT ) ;
1000
- let version = shifter ( packet. li_vn_mode , VERSION_MASK , VERSION_SHIFT ) ;
1001
- let li = shifter ( packet. li_vn_mode , LI_MASK , LI_SHIFT ) ;
1002
-
1003
- debug ! ( "NTP Packet:" ) ;
1004
- debug ! ( "Mode: {}" , mode) ;
1005
- debug ! ( "Version: {}" , version) ;
1006
- debug ! ( "Leap: {}" , li) ;
1007
- debug ! ( "Stratum: {}" , packet. stratum) ;
1008
- debug ! ( "Poll: {}" , packet. poll) ;
1009
- debug ! ( "Precision: {}" , packet. precision) ;
1010
- debug ! ( "Root delay: {}" , packet. root_delay) ;
1011
- debug ! ( "Root dispersion: {}" , packet. root_dispersion) ;
1012
- debug ! (
1013
- "Reference ID: {}" ,
1014
- str :: from_utf8( & packet. ref_id. to_be_bytes( ) ) . unwrap_or( "" )
1015
- ) ;
1016
- debug ! ( "Origin timestamp (client): {}" , packet. origin_timestamp) ;
1017
- debug ! ( "Receive timestamp (server): {}" , packet. recv_timestamp) ;
1018
- debug ! ( "Transmit timestamp (server): {}" , packet. tx_timestamp) ;
1019
- debug ! ( "Receive timestamp (client): {}" , recv_timestamp) ;
1020
- debug ! ( "Reference timestamp (server): {}" , packet. ref_timestamp) ;
1021
- }
1022
-
1023
- #[ cfg( all( feature = "log" , not( feature = "defmt" ) ) ) ]
1024
- fn debug_ntp_packet ( packet : & NtpPacket , recv_timestamp : u64 ) {
1025
- let mode = shifter ( packet. li_vn_mode , MODE_MASK , MODE_SHIFT ) ;
1026
- let version = shifter ( packet. li_vn_mode , VERSION_MASK , VERSION_SHIFT ) ;
1027
- let li = shifter ( packet. li_vn_mode , LI_MASK , LI_SHIFT ) ;
1028
- let delimiter_gen = || unsafe { str:: from_utf8_unchecked ( & [ b'=' ; 64 ] ) } ;
1029
-
1030
- debug ! ( "{}" , delimiter_gen( ) ) ;
1031
- debug ! ( "| Mode:\t \t {}" , mode) ;
1032
- debug ! ( "| Version:\t {}" , version) ;
1033
- debug ! ( "| Leap:\t \t {}" , li) ;
1034
- debug ! ( "| Stratum:\t {}" , packet. stratum) ;
1035
- debug ! ( "| Poll:\t \t {}" , packet. poll) ;
1036
- debug ! ( "| Precision:\t \t {}" , packet. precision) ;
1037
- debug ! ( "| Root delay:\t \t {}" , packet. root_delay) ;
1038
- debug ! ( "| Root dispersion:\t {}" , packet. root_dispersion) ;
1039
- debug ! (
1040
- "| Reference ID:\t \t {}" ,
1041
- str :: from_utf8( & packet. ref_id. to_be_bytes( ) ) . unwrap_or( "" )
1042
- ) ;
1043
- debug ! (
1044
- "| Origin timestamp (client):\t {:>16}" ,
1045
- packet. origin_timestamp
1046
- ) ;
1047
- debug ! (
1048
- "| Receive timestamp (server):\t {:>16}" ,
1049
- packet. recv_timestamp
1050
- ) ;
1051
- debug ! (
1052
- "| Transmit timestamp (server):\t {:>16}" ,
1053
- packet. tx_timestamp
1054
- ) ;
1055
- debug ! ( "| Receive timestamp (client):\t {:>16}" , recv_timestamp) ;
1056
- debug ! (
1057
- "| Reference timestamp (server):\t {:>16}" ,
1058
- packet. ref_timestamp
1059
- ) ;
1060
- debug ! ( "{}" , delimiter_gen( ) ) ;
1061
- }
1062
-
1063
995
fn get_ntp_timestamp < T : NtpTimestampGenerator > ( timestamp_gen : & T ) -> u64 {
1064
996
( ( timestamp_gen. timestamp_sec ( )
1065
997
+ ( u64:: from ( NtpPacket :: NTP_TIMESTAMP_DELTA ) ) )
0 commit comments