2011-12-31 16:14:18 +08:00
|
|
|
/**
|
|
|
|
* @file config.c
|
|
|
|
* @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "config.h"
|
2012-07-27 17:30:22 +08:00
|
|
|
#include "ether.h"
|
2012-08-21 01:56:56 +08:00
|
|
|
#include "print.h"
|
2011-12-31 16:14:18 +08:00
|
|
|
|
2012-03-21 03:09:40 +08:00
|
|
|
static void scan_line(char *s, struct config *cfg)
|
2011-12-31 16:14:18 +08:00
|
|
|
{
|
2012-05-03 17:17:34 +08:00
|
|
|
double df;
|
2012-08-21 01:57:01 +08:00
|
|
|
int i, val, cfg_ignore = cfg->cfg_ignore;
|
2011-12-31 16:14:18 +08:00
|
|
|
Integer8 i8;
|
|
|
|
UInteger16 u16;
|
|
|
|
UInteger8 u8;
|
2012-07-27 17:30:22 +08:00
|
|
|
unsigned char mac[MAC_LEN];
|
2012-08-21 01:56:56 +08:00
|
|
|
char string[1024];
|
2011-12-31 16:14:18 +08:00
|
|
|
|
2012-08-21 01:56:29 +08:00
|
|
|
struct defaultDS *dds = &cfg->dds;
|
|
|
|
struct port_defaults *pod = &cfg->pod;
|
2012-03-21 03:09:40 +08:00
|
|
|
|
2011-12-31 16:14:18 +08:00
|
|
|
if (1 == sscanf(s, " twoStepFlag %d", &val)) {
|
|
|
|
|
|
|
|
if (val) /* TODO - implement one step */
|
2012-08-21 01:56:56 +08:00
|
|
|
dds->twoStepFlag = val ? 1 : 0;
|
2011-12-31 16:14:18 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " slaveOnly %d", &val)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_SLAVEONLY))
|
|
|
|
dds->slaveOnly = val ? 1 : 0;
|
2011-12-31 16:14:18 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " priority1 %hhu", &u8)) {
|
|
|
|
|
|
|
|
dds->priority1 = u8;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " priority2 %hhu", &u8)) {
|
|
|
|
|
|
|
|
dds->priority2 = u8;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " domainNumber %hhu", &u8)) {
|
|
|
|
|
|
|
|
if (u8 < 128)
|
|
|
|
dds->domainNumber = u8;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " clockClass %hhu", &u8)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_SLAVEONLY))
|
|
|
|
dds->clockQuality.clockClass = u8;
|
2011-12-31 16:14:18 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " clockAccuracy %hhx", &u8)) {
|
|
|
|
|
|
|
|
dds->clockQuality.clockAccuracy = u8;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " offsetScaledLogVariance %hx", &u16)) {
|
|
|
|
|
|
|
|
dds->clockQuality.offsetScaledLogVariance = u16;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " logAnnounceInterval %hhd", &i8)) {
|
|
|
|
|
|
|
|
pod->logAnnounceInterval = i8;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " logSyncInterval %hhd", &i8)) {
|
|
|
|
|
|
|
|
pod->logSyncInterval = i8;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " logMinDelayReqInterval %hhd", &i8)) {
|
|
|
|
|
|
|
|
pod->logMinDelayReqInterval = i8;
|
|
|
|
|
2012-07-28 04:04:00 +08:00
|
|
|
} else if (1 == sscanf(s, " logMinPdelayReqInterval %hhd", &i8)) {
|
|
|
|
|
|
|
|
pod->logMinPdelayReqInterval = i8;
|
|
|
|
|
2011-12-31 16:14:18 +08:00
|
|
|
} else if (1 == sscanf(s, " announceReceiptTimeout %hhu", &u8)) {
|
|
|
|
|
|
|
|
pod->announceReceiptTimeout = u8;
|
2012-03-21 03:16:58 +08:00
|
|
|
|
2012-07-27 16:51:31 +08:00
|
|
|
} else if (1 == sscanf(s, " transportSpecific %hhx", &u8)) {
|
|
|
|
|
|
|
|
pod->transportSpecific = u8 << 4;
|
|
|
|
|
2012-08-07 15:16:19 +08:00
|
|
|
} else if (1 == sscanf(s, " path_trace_enabled %u", &val)) {
|
|
|
|
|
|
|
|
pod->path_trace_enabled = val ? 1 : 0;
|
|
|
|
|
2012-08-09 02:16:47 +08:00
|
|
|
} else if (1 == sscanf(s, " follow_up_info %u", &val)) {
|
|
|
|
|
|
|
|
pod->follow_up_info = val ? 1 : 0;
|
|
|
|
|
2012-07-28 04:34:34 +08:00
|
|
|
} else if (1 == sscanf(s, " assume_two_step %u", &val)) {
|
|
|
|
|
|
|
|
*cfg->assume_two_step = val ? 1 : 0;
|
|
|
|
|
2012-03-21 03:16:58 +08:00
|
|
|
} else if (1 == sscanf(s, " tx_timestamp_retries %u", &val)) {
|
|
|
|
|
|
|
|
if (val > 0)
|
|
|
|
*cfg->tx_timestamp_retries = val;
|
2012-05-03 17:17:34 +08:00
|
|
|
|
2012-07-27 18:00:47 +08:00
|
|
|
} else if (1 == sscanf(s, " rx_timestamp_l2only %u", &val)) {
|
|
|
|
|
|
|
|
*cfg->rx_timestamp_l2only = val ? 1 : 0;
|
|
|
|
|
2012-05-03 17:17:34 +08:00
|
|
|
} else if (1 == sscanf(s, " pi_proportional_const %lf", &df)) {
|
|
|
|
|
|
|
|
if (df > 0.0 && df < 1.0)
|
|
|
|
*cfg->pi_proportional_const = df;
|
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " pi_integral_const %lf", &df)) {
|
|
|
|
|
|
|
|
if (df > 0.0 && df < 1.0)
|
|
|
|
*cfg->pi_integral_const = df;
|
2012-07-27 17:30:22 +08:00
|
|
|
|
|
|
|
} else if (MAC_LEN == sscanf(s, " ptp_dst_mac %hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
|
|
|
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5])) {
|
|
|
|
|
|
|
|
for (i = 0; i < MAC_LEN; i++)
|
|
|
|
cfg->ptp_dst_mac[i] = mac[i];
|
|
|
|
|
|
|
|
} else if (MAC_LEN == sscanf(s, " p2p_dst_mac %hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
|
|
|
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5])) {
|
|
|
|
|
|
|
|
for (i = 0; i < MAC_LEN; i++)
|
|
|
|
cfg->p2p_dst_mac[i] = mac[i];
|
2012-08-21 01:56:56 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " logging_level %d", &val)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_VERBOSE)) {
|
|
|
|
|
|
|
|
if (val >= PRINT_LEVEL_MIN && val <= PRINT_LEVEL_MAX)
|
|
|
|
cfg->print_level = val;
|
|
|
|
|
|
|
|
}
|
2012-08-21 01:56:56 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " verbose %d", &val)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_VERBOSE))
|
|
|
|
cfg->verbose = val ? 1 : 0;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " use_syslog %d", &val)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_USE_SYSLOG))
|
|
|
|
cfg->use_syslog = val ? 1 : 0;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " time_stamping %1023s", string)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_TIMESTAMPING)) {
|
|
|
|
|
|
|
|
if (0 == strcasecmp("hardware", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->timestamping = TS_HARDWARE;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
else if (0 == strcasecmp("software", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->timestamping = TS_SOFTWARE;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
else if (0 == strcasecmp("legacy", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->timestamping = TS_LEGACY_HW;
|
|
|
|
}
|
2012-08-21 01:56:56 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " delay_mechanism %1023s", string)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_DM)) {
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (0 == strcasecmp("E2E", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->dm = DM_E2E;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
else if (0 == strcasecmp("P2P", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->dm = DM_P2P;
|
|
|
|
|
|
|
|
else if (0 == strcasecmp("Auto", string))
|
|
|
|
|
|
|
|
cfg->dm = DM_AUTO;
|
|
|
|
}
|
2012-08-21 01:56:56 +08:00
|
|
|
|
|
|
|
} else if (1 == sscanf(s, " network_transport %1023s", string)) {
|
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
if (!(cfg_ignore & CFG_IGNORE_TRANSPORT)) {
|
|
|
|
|
|
|
|
if (0 == strcasecmp("UDPv4", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->transport = TRANS_UDP_IPV4;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
else if (0 == strcasecmp("UDPv6", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->transport = TRANS_UDP_IPV6;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
else if (0 == strcasecmp("L2", string))
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
cfg->transport = TRANS_IEEE_802_3;
|
2012-08-21 01:56:56 +08:00
|
|
|
|
2012-08-21 01:57:01 +08:00
|
|
|
}
|
2011-12-31 16:14:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 03:09:40 +08:00
|
|
|
int config_read(char *name, struct config *cfg)
|
2011-12-31 16:14:18 +08:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[1024];
|
|
|
|
|
|
|
|
fp = 0 == strncmp(name, "-", 2) ? stdin : fopen(name, "r");
|
|
|
|
|
|
|
|
if (!fp) {
|
|
|
|
perror("fopen");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
2012-03-21 03:09:40 +08:00
|
|
|
scan_line(line, cfg);
|
2011-12-31 16:14:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-21 01:56:45 +08:00
|
|
|
|
|
|
|
/* returns the number matching that interface, or -1 on failure */
|
|
|
|
int config_create_interface(char *name, struct config *cfg)
|
|
|
|
{
|
|
|
|
struct interface *iface;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (cfg->nports >= MAX_PORTS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iface = &cfg->iface[cfg->nports];
|
|
|
|
|
|
|
|
/* only create each interface once (by name) */
|
|
|
|
for(i = 0; i < cfg->nports; i++) {
|
|
|
|
if (0 == strncmp(name, cfg->iface[i].name, MAX_IFNAME_SIZE))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(iface->name, name, MAX_IFNAME_SIZE);
|
|
|
|
iface->dm = cfg->dm;
|
|
|
|
iface->transport = cfg->transport;
|
2012-08-21 01:56:50 +08:00
|
|
|
memcpy(&iface->pod, &cfg->pod, sizeof(cfg->pod));
|
|
|
|
|
2012-08-21 01:56:45 +08:00
|
|
|
cfg->nports++;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|