int main(int argc, char *argv[]) { const char *progname; char **cmdv, *default_cmdv[] = { "caps" }; int c, result, cmdc; int print_level = LOG_INFO, verbose = 1, use_syslog = 1; clockid_t clkid; install_handler(SIGALRM, handle_alarm); /* Process the command line arguments. */ progname = strrchr(argv[0], '/'); progname = progname ? 1+progname : argv[0]; while (EOF != (c = getopt(argc, argv, "l:qQvh"))) { switch (c) { case 'l': if (get_arg_val_i(c, optarg, &print_level, PRINT_LEVEL_MIN, PRINT_LEVEL_MAX)) return -1; break; case 'q': use_syslog = 0; break; case 'Q': verbose = 0; break; case 'v': version_show(stdout); return 0; case 'h': usage(progname); return 0; default: usage(progname); return -1; } } print_set_progname(progname); print_set_verbose(verbose); print_set_syslog(use_syslog); print_set_level(print_level); if ((argc - optind) < 1) { usage(progname); return -1; } if ((argc - optind) == 1) { cmdv = default_cmdv; cmdc = 1; } else { cmdv = &argv[optind+1]; cmdc = argc - optind - 1; } clkid = clock_open(argv[optind]); if (clkid == CLOCK_INVALID) return -1; /* pass the remaining arguments to the run_cmds loop */ result = run_cmds(clkid, cmdc, cmdv); if (result < -1) { /* show usage when command fails */ usage(progname); return result; } return 0; }
int main(int argc, char *argv[]) { char *config = NULL, *req_phc = NULL, *progname; int c, i; struct interface *iface = cfg_settings.iface; char *ports[MAX_PORTS]; int nports = 0; int *cfg_ignore = &cfg_settings.cfg_ignore; enum delay_mechanism *dm = &cfg_settings.dm; enum transport_type *transport = &cfg_settings.transport; enum timestamp_type *timestamping = &cfg_settings.timestamping; struct clock *clock; struct defaultDS *ds = &cfg_settings.dds.dds; int phc_index = -1, required_modes = 0; if (SIG_ERR == signal(SIGINT, handle_int_quit_term)) { fprintf(stderr, "cannot handle SIGINT\n"); return -1; } if (SIG_ERR == signal(SIGQUIT, handle_int_quit_term)) { fprintf(stderr, "cannot handle SIGQUIT\n"); return -1; } if (SIG_ERR == signal(SIGTERM, handle_int_quit_term)) { fprintf(stderr, "cannot handle SIGTERM\n"); return -1; } /* Set fault timeouts to a default value */ for (i = 0; i < FT_CNT; i++) { cfg_settings.pod.flt_interval_pertype[i].type = FTMO_LOG2_SECONDS; cfg_settings.pod.flt_interval_pertype[i].val = 4; } /* Process the command line arguments. */ progname = strrchr(argv[0], '/'); progname = progname ? 1+progname : argv[0]; while (EOF != (c = getopt(argc, argv, "AEP246HSLf:i:p:sl:mqvh"))) { switch (c) { case 'A': *dm = DM_AUTO; *cfg_ignore |= CFG_IGNORE_DM; break; case 'E': *dm = DM_E2E; *cfg_ignore |= CFG_IGNORE_DM; break; case 'P': *dm = DM_P2P; *cfg_ignore |= CFG_IGNORE_DM; break; case '2': *transport = TRANS_IEEE_802_3; *cfg_ignore |= CFG_IGNORE_TRANSPORT; break; case '4': *transport = TRANS_UDP_IPV4; *cfg_ignore |= CFG_IGNORE_TRANSPORT; break; case '6': *transport = TRANS_UDP_IPV6; *cfg_ignore |= CFG_IGNORE_TRANSPORT; break; case 'H': *timestamping = TS_HARDWARE; *cfg_ignore |= CFG_IGNORE_TIMESTAMPING; break; case 'S': *timestamping = TS_SOFTWARE; *cfg_ignore |= CFG_IGNORE_TIMESTAMPING; break; case 'L': *timestamping = TS_LEGACY_HW; *cfg_ignore |= CFG_IGNORE_TIMESTAMPING; break; case 'f': config = optarg; break; case 'i': ports[nports] = optarg; nports++; break; case 'p': req_phc = optarg; break; case 's': ds->flags |= DDS_SLAVE_ONLY; *cfg_ignore |= CFG_IGNORE_SLAVEONLY; break; case 'l': if (get_arg_val_i(c, optarg, &cfg_settings.print_level, PRINT_LEVEL_MIN, PRINT_LEVEL_MAX)) return -1; *cfg_ignore |= CFG_IGNORE_PRINT_LEVEL; break; case 'm': cfg_settings.verbose = 1; *cfg_ignore |= CFG_IGNORE_VERBOSE; break; case 'q': cfg_settings.use_syslog = 0; *cfg_ignore |= CFG_IGNORE_USE_SYSLOG; break; case 'v': version_show(stdout); return 0; case 'h': usage(progname); return 0; case '?': usage(progname); return -1; default: usage(progname); return -1; } } if (config && (c = config_read(config, &cfg_settings))) { return c; } if (ds->flags & DDS_SLAVE_ONLY) { ds->clockQuality.clockClass = 255; } print_set_progname(progname); print_set_verbose(cfg_settings.verbose); print_set_syslog(cfg_settings.use_syslog); print_set_level(cfg_settings.print_level); for (i = 0; i < nports; i++) { if (config_create_interface(ports[i], &cfg_settings) < 0) { fprintf(stderr, "too many interfaces\n"); return -1; } } if (!cfg_settings.nports) { fprintf(stderr, "no interface specified\n"); usage(progname); return -1; } if (!(ds->flags & DDS_TWO_STEP_FLAG)) { switch (*timestamping) { case TS_SOFTWARE: case TS_LEGACY_HW: fprintf(stderr, "one step is only possible " "with hardware time stamping\n"); return -1; case TS_HARDWARE: *timestamping = TS_ONESTEP; break; case TS_ONESTEP: break; } } switch (*timestamping) { case TS_SOFTWARE: required_modes |= SOF_TIMESTAMPING_TX_SOFTWARE | SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_SOFTWARE; break; case TS_LEGACY_HW: required_modes |= SOF_TIMESTAMPING_TX_HARDWARE | SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_SYS_HARDWARE; break; case TS_HARDWARE: case TS_ONESTEP: required_modes |= SOF_TIMESTAMPING_TX_HARDWARE | SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_RAW_HARDWARE; break; } /* check whether timestamping mode is supported. */ for (i = 0; i < cfg_settings.nports; i++) { if (iface[i].ts_info.valid && ((iface[i].ts_info.so_timestamping & required_modes) != required_modes)) { fprintf(stderr, "interface '%s' does not support " "requested timestamping mode.\n", iface[i].name); return -1; } } /* determine PHC Clock index */ if (cfg_settings.dds.free_running) { phc_index = -1; } else if (*timestamping == TS_SOFTWARE || *timestamping == TS_LEGACY_HW) { phc_index = -1; } else if (req_phc) { if (1 != sscanf(req_phc, "/dev/ptp%d", &phc_index)) { fprintf(stderr, "bad ptp device string\n"); return -1; } } else if (iface[0].ts_info.valid) { phc_index = iface[0].ts_info.phc_index; } else { fprintf(stderr, "ptp device not specified and\n" "automatic determination is not\n" "supported. please specify ptp device\n"); return -1; } if (phc_index >= 0) { pr_info("selected /dev/ptp%d as PTP clock", phc_index); } if (generate_clock_identity(&ds->clockIdentity, iface[0].name)) { fprintf(stderr, "failed to generate a clock identity\n"); return -1; } clock = clock_create(phc_index, iface, cfg_settings.nports, *timestamping, &cfg_settings.dds, cfg_settings.clock_servo); if (!clock) { fprintf(stderr, "failed to create a clock\n"); return -1; } while (running) { if (clock_poll(clock)) break; } clock_destroy(clock); return 0; }