diff --git a/hwstamp_ctl.c b/hwstamp_ctl.c new file mode 100644 index 0000000..7226ab1 --- /dev/null +++ b/hwstamp_ctl.c @@ -0,0 +1,142 @@ +/** + * @file hwstamp_ctl.c + * @brief Utility program to set time stamping policy at the driver level. + * @note Copyright (C) 2012 Richard Cochran + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static void usage(char *progname) +{ + fprintf(stderr, + "\n" + "usage: %s [options]\n\n" + " -h prints this message and exits\n" + " -i [device] interface device to use, for example 'eth0'\n" + " -r [%d..%d] select receive time stamping:\n" + "\t\t%2d time stamp no incoming packet at all\n" + "\t\t%2d time stamp any incoming packet\n" + "\t\t%2d (reserved value)\n" + "\t\t%2d PTP v1, UDP, any kind of event packet\n" + "\t\t%2d PTP v1, UDP, Sync packet\n" + "\t\t%2d PTP v1, UDP, Delay_req packet\n" + "\t\t%2d PTP v2, UDP, any kind of event packet\n" + "\t\t%2d PTP v2, UDP, Sync packet\n" + "\t\t%2d PTP v2, UDP, Delay_req packet\n" + "\t\t%2d 802.AS1, Ethernet, any kind of event packet\n" + "\t\t%2d 802.AS1, Ethernet, Sync packet\n" + "\t\t%2d 802.AS1, Ethernet, Delay_req packet\n" + "\t\t%2d PTP v2/802.AS1, any layer, any kind of event packet\n" + "\t\t%2d PTP v2/802.AS1, any layer, Sync packet\n" + "\t\t%2d PTP v2/802.AS1, any layer, Delay_req packet\n" + " -t [%d|%d] disable or enable transmit time stamping\n" + "\n", + progname, + HWTSTAMP_FILTER_NONE, HWTSTAMP_FILTER_PTP_V2_DELAY_REQ, + HWTSTAMP_FILTER_NONE, + HWTSTAMP_FILTER_ALL, + HWTSTAMP_FILTER_SOME, + HWTSTAMP_FILTER_PTP_V1_L4_EVENT, + HWTSTAMP_FILTER_PTP_V1_L4_SYNC, + HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ, + HWTSTAMP_FILTER_PTP_V2_L4_EVENT, + HWTSTAMP_FILTER_PTP_V2_L4_SYNC, + HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ, + HWTSTAMP_FILTER_PTP_V2_L2_EVENT, + HWTSTAMP_FILTER_PTP_V2_L2_SYNC, + HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ, + HWTSTAMP_FILTER_PTP_V2_EVENT, + HWTSTAMP_FILTER_PTP_V2_SYNC, + HWTSTAMP_FILTER_PTP_V2_DELAY_REQ, + HWTSTAMP_TX_OFF, + HWTSTAMP_TX_ON); +} + +int main(int argc, char *argv[]) +{ + struct ifreq ifreq; + struct hwtstamp_config cfg; + char *device = NULL, *progname; + int c, err, fd, rxopt = HWTSTAMP_FILTER_NONE, txopt = HWTSTAMP_TX_OFF; + + /* Process the command line arguments. */ + progname = strrchr(argv[0], '/'); + progname = progname ? 1+progname : argv[0]; + while (EOF != (c = getopt(argc, argv, "hi:r:t:"))) { + switch (c) { + case 'i': + device = optarg; + break; + case 'r': + rxopt = atoi(optarg); + break; + case 't': + txopt = atoi(optarg); + break; + case 'h': + usage(progname); + return 0; + case '?': + default: + usage(progname); + return -1; + } + } + + if (!device) { + usage(progname); + return -1; + } + if (rxopt < HWTSTAMP_FILTER_NONE || + rxopt > HWTSTAMP_FILTER_PTP_V2_DELAY_REQ || + txopt < HWTSTAMP_TX_OFF || txopt > HWTSTAMP_TX_ON) { + usage(progname); + return -1; + } + + memset(&ifreq, 0, sizeof(ifreq)); + memset(&cfg, 0, sizeof(cfg)); + + strncpy(ifreq.ifr_name, device, sizeof(ifreq.ifr_name)); + + ifreq.ifr_data = (void *) &cfg; + cfg.tx_type = txopt; + cfg.rx_filter = rxopt; + + fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (fd < 0) { + perror("socket"); + return -1; + } + + err = ioctl(fd, SIOCSHWTSTAMP, &ifreq); + if (err < 0) + perror("SIOCSHWTSTAMP failed"); + + printf("tx_type %d\n" "rx_filter %d\n", cfg.tx_type, cfg.rx_filter); + + return err ? errno : 0; +} diff --git a/makefile b/makefile index 08e9986..0bcb5cd 100644 --- a/makefile +++ b/makefile @@ -27,18 +27,20 @@ PRG = ptp4l phc2sys OBJ = bmc.o clock.o config.o fsm.o ptp4l.o mave.o msg.o phc.o pi.o port.o \ print.o servo.o tmtab.o transport.o udp.o util.o -OBJECTS = $(OBJ) phc2sys.o +OBJECTS = $(OBJ) phc2sys.o hwstamp_ctl.o SRC = $(OBJECTS:.o=.c) DEPEND = $(OBJECTS:.o=.d) srcdir := $(dir $(lastword $(MAKEFILE_LIST))) VPATH = $(srcdir) -all: ptp4l phc2sys +all: ptp4l phc2sys hwstamp_ctl ptp4l: $(OBJ) phc2sys: phc2sys.o +hwstamp_ctl: hwstamp_ctl.o + clean: rm -f $(OBJECTS) $(DEPEND)