From ad5f6263d4c83720c156130e07ee463fccae1c5a Mon Sep 17 00:00:00 2001 From: Casper Andersson Date: Thu, 7 Mar 2024 15:28:21 +0100 Subject: [PATCH] Add timestamp disabling on exit and some other minor things Signed-off-by: Casper Andersson --- CMakeLists.txt | 37 +++++++++++++++++++++++++++++++++++++ include/timestamping.h | 1 + src/delay.c | 6 +++--- src/pkt.c | 15 ++++++++++++++- src/timestamping.c | 5 +++++ 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28f0566..b9bdd80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,3 +41,40 @@ add_custom_target(test # ============= Configure =============== configure_file(include/version.h.in include/version.h) + +#project(tcalign VERSION 1.0) +##set(PROJECT_NAME_VERSION "${CMAKE_PROJECT_NAME}-${CMAKE_PROJECT_VERSION}") + + +## ---- PROJECT SETUP ---- +##set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wpedantic -Wextra") + +## ---- BUILD ---- +#find_library (LIBNL_LIBRARY nl-3) +#find_library (LIBNL_GENL_LIBRARY nl-genl-3) + +#set(LIBNL_LIBRARIES ${LIBNL_LIBRARY} ${LIBNL_GENL_LIBRARY}) +#set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +##set(CMAKE_C_FLAGS + ##"${CMAKE_C_FLAGS} -Wall -Wpedantic -Wextra" +##) + +#find_path (LIBNL_INCLUDE_DIR NAMES netlink/netlink.h PATH_SUFFIXES libnl3) +#include_directories(include ${LIBNL_INCLUDE_DIR}) + + + + +#add_executable(tcalign) +#target_sources(tcalign PRIVATE src/tcalign.c) + +#install(TARGETS tcalign + #CONFIGURATIONS Debug + #RUNTIME DESTINATION bin) +#install(TARGETS tcalign + #CONFIGURATIONS Release + #RUNTIME DESTINATION bin) + +#target_link_libraries(tcalign PUBLIC gpiod ${LIBNL_LIBRARIES}) + diff --git a/include/timestamping.h b/include/timestamping.h index f721324..d99c929 100644 --- a/include/timestamping.h +++ b/include/timestamping.h @@ -119,6 +119,7 @@ static unsigned char p2p_dst_mac[] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x0E }; int sk_receive(int fd, void *buf, int buflen, struct address *addr, struct hw_timestamp *hwts, int flags); int raw_send(int fd, enum transport_event event, void *buf, int len, struct hw_timestamp *hwts); +int sk_timestamping_destroy(int fd, const char *device); int sk_timestamping_init(int fd, const char *device, enum timestamp_type type, enum transport_type transport, int vclock); //int socket_init_raw(char *interface); diff --git a/src/delay.c b/src/delay.c index 0dd2de4..0ea4551 100644 --- a/src/delay.c +++ b/src/delay.c @@ -13,7 +13,7 @@ /* TODO: * - Implement mmedian_sample delay filter (ptp4l delay_filter) - * - Implement E2E + * - Set twoStepFlag correctly */ @@ -342,9 +342,9 @@ int delay_parse_opt(int argc, char **argv, struct delay_cfg *cfg) case 'v': if (optarg == NULL) printf("bad version input\n"); - else if (strncmp(optarg, "2.1", 3) == 0) + else if (strncmp(optarg, "2.1", 3) == 0 && strlen(optarg) == 3) cfg->version = 2 | (1 << 4); - else if (strncmp(optarg, "2", 1) == 0) + else if (strncmp(optarg, "2", 1) == 0 && strlen(optarg) == 1) cfg->version = 2; else printf("bad version input\n"); diff --git a/src/pkt.c b/src/pkt.c index a51afdd..18794e6 100644 --- a/src/pkt.c +++ b/src/pkt.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "net_tstamp_cpy.h" @@ -18,6 +19,8 @@ * */ +int pkt_running = 1; + #ifndef SO_TIMESTAMPING #define SO_TIMESTAMPING 37 #define SCM_TIMESTAMPING SO_TIMESTAMPING @@ -81,12 +84,18 @@ Options:\n\ -c . Set to 0 to send until stopped\n\ -l <0|1>. 0: Never fetch timestamp. 1: Always fetch timestamp (even for types that might not have)\n\ -d Enable debug output\n\ + -v <2|2.1> PTP version of the packet\n\ -h help\n\ --transportSpecific . Set value for the transportSpecific field\n\ --twoStepFlag <0|1>. Force if twoStepFlag should be set or not. Default is automatic\n\ \n"); } +static void sig_handler(int sig) +{ + pkt_running = 0; +} + void set_two_step_flag(struct pkt_cfg *cfg, struct ptp_header *hdr, int type) { int twoStepFlag = 0x00; @@ -199,7 +208,7 @@ int rx_mode(struct pkt_cfg *cfg, int sock, struct hw_timestamp *hwts) rx_msg = (union Message *)buf; - while (1) { + while (pkt_running) { sk_receive(sock, rx_msg, 1600, NULL, hwts, 0); printf("Type: %s. ", ptp_type2str(rx_msg->hdr.tsmt & 0xF)); print_ts("TS: ", hwts->ts.ns); @@ -353,6 +362,8 @@ int run_pkt_mode(int argc, char **argv) /*return EINVAL;*/ /*}*/ + signal(SIGINT, sig_handler); + if (!cfg.count) cfg.nonstop_flag = 1; @@ -379,5 +390,7 @@ int run_pkt_mode(int argc, char **argv) else tx_mode(&cfg, sock, &hwts); + sk_timestamping_destroy(sock, cfg.interface); + return 0; } diff --git a/src/timestamping.c b/src/timestamping.c index 8f5df55..1527d73 100644 --- a/src/timestamping.c +++ b/src/timestamping.c @@ -290,6 +290,11 @@ int raw_send(int fd, enum transport_event event, void *buf, int len, struct hw_t return event == TRANS_EVENT ? sk_receive(fd, pkt, len, NULL, hwts, MSG_ERRQUEUE) : cnt; } +int sk_timestamping_destroy(int fd, const char *device) +{ + return hwts_init(fd, device, 0, 0, HWTSTAMP_TX_OFF); +} + int sk_timestamping_init(int fd, const char *device, enum timestamp_type type, enum transport_type transport, int vclock) {