Replace sys/time.h with <chrono> for portability/modernization.

This commit is contained in:
Ryan Volz
2023-07-18 17:31:02 -04:00
committed by Bastian Bloessl
parent a15d6bb34f
commit 28be829378
4 changed files with 30 additions and 19 deletions
+3 -3
View File
@@ -51,11 +51,11 @@ if((CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
endif() endif()
IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") IF(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
SET(CMAKE_CXX_STANDARD 14) SET(CMAKE_CXX_STANDARD 17)
ELSEIF(CMAKE_CXX_COMPILER_ID MATCHES "Clang") ELSEIF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CMAKE_CXX_STANDARD 14) SET(CMAKE_CXX_STANDARD 17)
ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") ELSEIF(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
SET(CMAKE_CXX_STANDARD 14) SET(CMAKE_CXX_STANDARD 17)
ELSE() ELSE()
message(WARNING "C++ standard could not be set because compiler is not GNU, Clang or MSVC.") message(WARNING "C++ standard could not be set because compiler is not GNU, Clang or MSVC.")
ENDIF() ENDIF()
+7 -4
View File
@@ -16,8 +16,8 @@
*/ */
#include <foo/packet_pad2.h> #include <foo/packet_pad2.h>
#include <chrono>
#include <gnuradio/io_signature.h> #include <gnuradio/io_signature.h>
#include <sys/time.h>
#ifdef FOO_UHD #ifdef FOO_UHD
#include <uhd/types/time_spec.hpp> #include <uhd/types/time_spec.hpp>
#endif #endif
@@ -65,9 +65,12 @@ int work (int noutput_items, gr_vector_int& ninput_items,
#ifdef FOO_UHD #ifdef FOO_UHD
if(d_delay) { if(d_delay) {
static const pmt::pmt_t time_key = pmt::string_to_symbol("tx_time"); static const pmt::pmt_t time_key = pmt::string_to_symbol("tx_time");
struct timeval t; double t_now(
gettimeofday(&t, NULL); std::chrono::duration<double>(std::chrono::system_clock::now()
uhd::time_spec_t now = uhd::time_spec_t(t.tv_sec + t.tv_usec / 1000000.0) .time_since_epoch()
).count()
);
uhd::time_spec_t now = uhd::time_spec_t(t_now)
+ uhd::time_spec_t(d_delay_sec); + uhd::time_spec_t(d_delay_sec);
const pmt::pmt_t time_value = pmt::make_tuple( const pmt::pmt_t time_value = pmt::make_tuple(
+7 -4
View File
@@ -16,9 +16,9 @@
*/ */
#include "packet_pad_impl.h" #include "packet_pad_impl.h"
#include <chrono>
#include <gnuradio/io_signature.h> #include <gnuradio/io_signature.h>
#include <iostream> #include <iostream>
#include <sys/time.h>
#ifdef FOO_UHD #ifdef FOO_UHD
#include <uhd/types/time_spec.hpp> #include <uhd/types/time_spec.hpp>
#endif #endif
@@ -58,9 +58,12 @@ packet_pad_impl::add_sob(uint64_t item) {
#ifdef FOO_UHD #ifdef FOO_UHD
if(d_delay) { if(d_delay) {
static const pmt::pmt_t time_key = pmt::string_to_symbol("tx_time"); static const pmt::pmt_t time_key = pmt::string_to_symbol("tx_time");
struct timeval t; double t_now(
gettimeofday(&t, NULL); std::chrono::duration<double>(std::chrono::system_clock::now()
uhd::time_spec_t now = uhd::time_spec_t(t.tv_sec + t.tv_usec / 1000000.0) .time_since_epoch()
).count()
);
uhd::time_spec_t now = uhd::time_spec_t(t_now)
+ uhd::time_spec_t(d_delay_sec); + uhd::time_spec_t(d_delay_sec);
const pmt::pmt_t time_value = pmt::make_tuple( const pmt::pmt_t time_value = pmt::make_tuple(
+12 -7
View File
@@ -19,9 +19,9 @@
#include <gnuradio/io_signature.h> #include <gnuradio/io_signature.h>
#include <gnuradio/block_detail.h> #include <gnuradio/block_detail.h>
#include <chrono>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <sys/time.h>
using namespace gr::foo; using namespace gr::foo;
@@ -55,8 +55,13 @@ void
wireshark_connector_impl::handle_pdu(pmt::pmt_t pdu) { wireshark_connector_impl::handle_pdu(pmt::pmt_t pdu) {
// get current time // get current time
struct timeval t; auto tp_now = std::chrono::system_clock::now();
gettimeofday(&t, NULL); auto tp_now_sec = std::chrono::floor<std::chrono::seconds>(tp_now);
auto duration_us = std::chrono::duration_cast<std::chrono::microseconds>(
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<const char*>(pmt::blob_data(pmt::cdr(pdu))); const char *buf = reinterpret_cast<const char*>(pmt::blob_data(pmt::cdr(pdu)));
std::size_t len = pmt::blob_length(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))); len + sizeof(radiotap_hdr) + sizeof(pcap_hdr)));
pcap_hdr *hdr = reinterpret_cast<pcap_hdr*>(d_msg); pcap_hdr *hdr = reinterpret_cast<pcap_hdr*>(d_msg);
hdr->ts_sec = t.tv_sec; hdr->ts_sec = ts_sec;
hdr->ts_usec = t.tv_usec; hdr->ts_usec = ts_usec;
hdr->incl_len = len + sizeof(radiotap_hdr); hdr->incl_len = len + sizeof(radiotap_hdr);
hdr->orig_len = len + sizeof(radiotap_hdr); hdr->orig_len = len + sizeof(radiotap_hdr);
offset += sizeof(struct pcap_hdr); offset += sizeof(struct pcap_hdr);
@@ -132,8 +137,8 @@ wireshark_connector_impl::handle_pdu(pmt::pmt_t pdu) {
len + sizeof(pcap_hdr))); len + sizeof(pcap_hdr)));
pcap_hdr *hdr = reinterpret_cast<pcap_hdr*>(d_msg); pcap_hdr *hdr = reinterpret_cast<pcap_hdr*>(d_msg);
hdr->ts_sec = t.tv_sec; hdr->ts_sec = ts_sec;
hdr->ts_usec = t.tv_usec; hdr->ts_usec = ts_usec;
hdr->incl_len = len; hdr->incl_len = len;
hdr->orig_len = len; hdr->orig_len = len;
offset += sizeof(pcap_hdr); offset += sizeof(pcap_hdr);