diff --git a/config.c b/config.c new file mode 100644 index 0000000..90e51f4 --- /dev/null +++ b/config.c @@ -0,0 +1,100 @@ +/** + * @file config.c + * @note Copyright (C) 2011 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 "config.h" + +static void scan_line(char *s, struct defaultDS *dds, struct port_defaults *pod) +{ + int val; + Integer8 i8; + UInteger16 u16; + UInteger8 u8; + + if (1 == sscanf(s, " twoStepFlag %d", &val)) { + + if (val) /* TODO - implement one step */ + dds->twoStepFlag = val ? 1 : 0; + + } else if (1 == sscanf(s, " slaveOnly %d", &val)) { + + dds->slaveOnly = val ? 1 : 0; + + } 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)) { + + dds->clockQuality.clockClass = u8; + + } 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; + + } else if (1 == sscanf(s, " announceReceiptTimeout %hhu", &u8)) { + + pod->announceReceiptTimeout = u8; + } +} + +int config_read(char *name, struct defaultDS *dds, struct port_defaults *pod) +{ + 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)) { + scan_line(line, dds, pod); + } + + fclose(fp); + return 0; +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..fb33479 --- /dev/null +++ b/config.h @@ -0,0 +1,27 @@ +/** + * @file config.h + * @brief Configuration file code + * @note Copyright (C) 2011 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. + */ +#ifndef HAVE_CONFIG_H +#define HAVE_CONFIG_H + +#include "ds.h" + +int config_read(char *name, struct defaultDS *dds, struct port_defaults *pod); + +#endif diff --git a/default.cfg b/default.cfg new file mode 100644 index 0000000..a2f86e9 --- /dev/null +++ b/default.cfg @@ -0,0 +1,18 @@ +# +# Default Data Set +# +twoStepFlag 1 +slaveOnly 0 +priority1 128 +priority2 128 +domainNumber 0 +clockClass 248 +clockAccuracy 0xFE +offsetScaledLogVariance 0xFFFF +# +# Port Data Set +# +logAnnounceInterval 1 +logSyncInterval 0 +logMinDelayReqInterval 0 +announceReceiptTimeout 3 diff --git a/ds.h b/ds.h index b9c7810..bda0de9 100644 --- a/ds.h +++ b/ds.h @@ -76,4 +76,11 @@ struct timePropertiesDS { Enumeration8 timeSource; }; +struct port_defaults { + Integer8 logAnnounceInterval; + Integer8 logSyncInterval; + Integer8 logMinDelayReqInterval; + UInteger8 announceReceiptTimeout; +}; + #endif diff --git a/makefile b/makefile index 222d8a6..7a7c839 100644 --- a/makefile +++ b/makefile @@ -24,7 +24,7 @@ CFLAGS = -Wall $(INC) $(DEBUG) LDFLAGS = LDLIBS = -lm -lrt PRG = ptp4l -OBJ = bmc.o clock.o fsm.o ptp4l.o mave.o msg.o phc.o pi.o port.o \ +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 SRC = $(OBJ:.o=.c)