Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 98 additions & 29 deletions pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,32 @@ type Ptp4lConf struct {
gnss_serial_port string // gnss serial port
}

func (conf *Ptp4lConf) getPtp4lConfOptionOrEmptyString(sectionName string, key string) (string, bool) {
func getSection(conf *Ptp4lConf, sectionName string) (ptp4lConfSection, bool) {
for _, section := range conf.sections {
if section.sectionName == sectionName {
for _, option := range section.options {
if option.key == key {
return option.value, true
}
}
return section, true
}
}
return ptp4lConfSection{}, false
}

func getValueFromSection(section ptp4lConfSection, key string) (string, bool) {
for _, option := range section.options {
if option.key == key {
return option.value, true
}
}
return "", false
}

func (conf *Ptp4lConf) getPtp4lConfOptionOrEmptyString(sectionName string, key string) (string, bool) {
section, found := getSection(conf, sectionName)
if !found {
return "", false
}
return getValueFromSection(section, key)
}

func (conf *Ptp4lConf) setPtp4lConfOption(sectionName string, key string, value string, overwrite bool) {
var updatedSection ptp4lConfSection
index := -1
Expand Down Expand Up @@ -179,12 +191,89 @@ func tryToLoadOldConfig(nodeProfilesJson []byte) ([]ptpv1.PtpProfile, bool) {
return []ptpv1.PtpProfile{*ptpConfig}, true
}

func isMasterOnly(section ptp4lConfSection, defaultValue bool) bool {
if v, found := getValueFromSection(section, "masterOnly"); found {
switch v {
case "1":
return true
case "0":
return false
}
}
if v, found := getValueFromSection(section, "serverOnly"); found {
switch v {
case "1":
return true
case "0":
return false
}
}
return defaultValue
}

func isSlaveOnly(section ptp4lConfSection, defaultValue bool) bool {
if v, found := getValueFromSection(section, "slaveOnly"); found {
if v == "1" {
return true
}
}
if v, found := getValueFromSection(section, "clientOnly"); found {
if v == "1" {
return true
}
}
return defaultValue
}

func (conf *Ptp4lConf) getClockType() event.ClockType {
onlySlaveDefault := false
onlyMasterDefault := false

if globalSection, found := getSection(conf, "global"); found {
onlySlaveDefault = isSlaveOnly(globalSection, false)
onlyMasterDefault = isMasterOnly(globalSection, false)
}

numberOfMasters := 0
numberOfSlaves := 0
numberOfNiether := 0

for _, section := range conf.sections {
if section.sectionName == "global" {
continue
}

onlyMaster := isMasterOnly(section, onlySlaveDefault)
onlySlave := isSlaveOnly(section, onlyMasterDefault)

if onlyMaster && !onlySlave {
numberOfMasters++
} else if onlySlave && !onlyMaster {
numberOfSlaves++
} else {
numberOfNiether++
}
}

// Not sure about the niether ports so for now they are ignored
if numberOfMasters > 0 && numberOfSlaves == 0 {
// Only defined master ports defined
return event.GM
} else if (numberOfMasters > 0) && (numberOfSlaves > 0) {
// Master and slave ports
return event.BC
} else if numberOfSlaves > 0 {
// Only Slave ports
return event.OC
}
// Default to OC if can't determin
return event.OC
}

// PopulatePtp4lConf takes as input a PtpProfile.Ptp4lConf string and outputs as ptp4lConf struct
func (conf *Ptp4lConf) PopulatePtp4lConf(config *string) error {
var currentSectionName string
conf.sections = make([]ptp4lConfSection, 0)
hasSlaveConfigDefined := false
ifaceCount := 0
if config != nil {
for _, line := range strings.Split(*config, "\n") {
line = strings.TrimSpace(line)
Expand All @@ -196,40 +285,20 @@ func (conf *Ptp4lConf) PopulatePtp4lConf(config *string) error {
return errors.New("Section missing closing ']': " + line)
}
currentSectionName = fmt.Sprintf("%s]", currentLine[0])
if currentSectionName != GlobalSectionName && currentSectionName != NmeaSectionName && currentSectionName != UnicastSectionName {
ifaceCount++
}
conf.setPtp4lConfOption(currentSectionName, "", "", false)
} else if currentSectionName != "" {
split := strings.IndexByte(line, ' ')
if split > 0 {
key := line[:split]
value := strings.TrimSpace(line[split:])
conf.setPtp4lConfOption(currentSectionName, key, value, false)
if (key == "masterOnly" && value == "0" && currentSectionName != GlobalSectionName) ||
(key == "serverOnly" && value == "0") ||
(key == "slaveOnly" && value == "1") ||
(key == "clientOnly" && value == "1") {
hasSlaveConfigDefined = true
}
}
} else {
return errors.New("Config option not in section: " + line)
}
}
}

if !hasSlaveConfigDefined {
// No Slave Interfaces defined
conf.clock_type = event.GM
} else if ifaceCount > 1 {
// Multiple interfaces with at least one slave Interface defined
conf.clock_type = event.BC
} else {
// Single slave Interface defined
conf.clock_type = event.OC
}

conf.clock_type = conf.getClockType()
return nil
}

Expand Down
Loading