From 28be8293784a7272760ef718213ff97f4c12fe4e Mon Sep 17 00:00:00 2001 From: Ryan Volz Date: Tue, 18 Jul 2023 17:31:02 -0400 Subject: [PATCH] Replace sys/time.h with for portability/modernization. --- CMakeLists.txt | 6 +++--- lib/packet_pad2.cc | 11 +++++++---- lib/packet_pad_impl.cc | 11 +++++++---- lib/wireshark_connector_impl.cc | 21 +++++++++++++-------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index acfdd7a..ade7c7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,11 +51,11 @@ if((CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR endif() IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - SET(CMAKE_CXX_STANDARD 14) + SET(CMAKE_CXX_STANDARD 17) ELSEIF(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - SET(CMAKE_CXX_STANDARD 14) + SET(CMAKE_CXX_STANDARD 17) ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - SET(CMAKE_CXX_STANDARD 14) + SET(CMAKE_CXX_STANDARD 17) ELSE() message(WARNING "C++ standard could not be set because compiler is not GNU, Clang or MSVC.") ENDIF() diff --git a/lib/packet_pad2.cc b/lib/packet_pad2.cc index 4d3fd2e..731c902 100644 --- a/lib/packet_pad2.cc +++ b/lib/packet_pad2.cc @@ -16,8 +16,8 @@ */ #include +#include #include -#include #ifdef FOO_UHD #include #endif @@ -65,9 +65,12 @@ int work (int noutput_items, gr_vector_int& ninput_items, #ifdef FOO_UHD if(d_delay) { static const pmt::pmt_t time_key = pmt::string_to_symbol("tx_time"); - struct timeval t; - gettimeofday(&t, NULL); - uhd::time_spec_t now = uhd::time_spec_t(t.tv_sec + t.tv_usec / 1000000.0) + double t_now( + std::chrono::duration(std::chrono::system_clock::now() + .time_since_epoch() + ).count() + ); + uhd::time_spec_t now = uhd::time_spec_t(t_now) + uhd::time_spec_t(d_delay_sec); const pmt::pmt_t time_value = pmt::make_tuple( diff --git a/lib/packet_pad_impl.cc b/lib/packet_pad_impl.cc index f26b426..6c37545 100644 --- a/lib/packet_pad_impl.cc +++ b/lib/packet_pad_impl.cc @@ -16,9 +16,9 @@ */ #include "packet_pad_impl.h" +#include #include #include -#include #ifdef FOO_UHD #include #endif @@ -58,9 +58,12 @@ packet_pad_impl::add_sob(uint64_t item) { #ifdef FOO_UHD if(d_delay) { static const pmt::pmt_t time_key = pmt::string_to_symbol("tx_time"); - struct timeval t; - gettimeofday(&t, NULL); - uhd::time_spec_t now = uhd::time_spec_t(t.tv_sec + t.tv_usec / 1000000.0) + double t_now( + std::chrono::duration(std::chrono::system_clock::now() + .time_since_epoch() + ).count() + ); + uhd::time_spec_t now = uhd::time_spec_t(t_now) + uhd::time_spec_t(d_delay_sec); const pmt::pmt_t time_value = pmt::make_tuple( diff --git a/lib/wireshark_connector_impl.cc b/lib/wireshark_connector_impl.cc index a5925f0..11c6ed9 100644 --- a/lib/wireshark_connector_impl.cc +++ b/lib/wireshark_connector_impl.cc @@ -19,9 +19,9 @@ #include #include +#include #include #include -#include using namespace gr::foo; @@ -55,8 +55,13 @@ void wireshark_connector_impl::handle_pdu(pmt::pmt_t pdu) { // get current time - struct timeval t; - gettimeofday(&t, NULL); + auto tp_now = std::chrono::system_clock::now(); + auto tp_now_sec = std::chrono::floor(tp_now); + auto duration_us = std::chrono::duration_cast( + tp_now - tp_now_sec + ); + auto ts_sec = tp_now_sec.time_since_epoch().count(); + auto ts_usec = duration_us.count(); const char *buf = reinterpret_cast(pmt::blob_data(pmt::cdr(pdu))); std::size_t len = pmt::blob_length(pmt::cdr(pdu)); @@ -78,8 +83,8 @@ wireshark_connector_impl::handle_pdu(pmt::pmt_t pdu) { len + sizeof(radiotap_hdr) + sizeof(pcap_hdr))); pcap_hdr *hdr = reinterpret_cast(d_msg); - hdr->ts_sec = t.tv_sec; - hdr->ts_usec = t.tv_usec; + hdr->ts_sec = ts_sec; + hdr->ts_usec = ts_usec; hdr->incl_len = len + sizeof(radiotap_hdr); hdr->orig_len = len + sizeof(radiotap_hdr); offset += sizeof(struct pcap_hdr); @@ -132,8 +137,8 @@ wireshark_connector_impl::handle_pdu(pmt::pmt_t pdu) { len + sizeof(pcap_hdr))); pcap_hdr *hdr = reinterpret_cast(d_msg); - hdr->ts_sec = t.tv_sec; - hdr->ts_usec = t.tv_usec; + hdr->ts_sec = ts_sec; + hdr->ts_usec = ts_usec; hdr->incl_len = len; hdr->orig_len = len; offset += sizeof(pcap_hdr); @@ -204,7 +209,7 @@ wireshark_connector_impl::general_work(int noutput, gr_vector_int& ninput_items, memcpy(out, d_msg + d_msg_offset, to_copy); dout << "WIRESHARK: d_msg_offset: " << d_msg_offset << - " to_copy: " << to_copy << + " to_copy: " << to_copy << " d_msg_len " << d_msg_len << std::endl; d_msg_offset += to_copy;