...

Source file src/edge-infra.dev/pkg/sds/display/k8s/apis/v1/displayconfig_validate.go

Documentation: edge-infra.dev/pkg/sds/display/k8s/apis/v1

     1  package v1
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  var (
     9  	errStandByLongerThanSuspendTime = errors.New("stand-by time cannot be longer than suspend time")
    10  	errSuspendLongerThanOffTime     = errors.New("suspend time cannot be longer than off time")
    11  )
    12  
    13  // Validates the DisplayConfig, checking:
    14  //   - Supported resolutions are not configured in spec.
    15  //   - Only one display is configured as the primary.
    16  //   - DPMS configuration is valid (standby-time < suspend-time < off-time).
    17  func (displayConfig *DisplayConfig) Validate() error {
    18  	if displayConfig == nil {
    19  		return nil
    20  	}
    21  
    22  	if err := validateNonSettableFields(displayConfig); err != nil {
    23  		return err
    24  	}
    25  
    26  	if err := validatePrimary(displayConfig); err != nil {
    27  		return err
    28  	}
    29  
    30  	return validateDPMS(displayConfig)
    31  }
    32  
    33  // Displays cannot configure supported resolution.
    34  func validateNonSettableFields(displayConfig *DisplayConfig) error {
    35  	for mpid, display := range displayConfig.Displays {
    36  		if display.SupportedResolutions != nil {
    37  			return fmt.Errorf("display %s specified 'supportedResolutions': resolution should be configured via the 'resolution' field", mpid)
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  // Only one display can be the primary.
    44  func validatePrimary(displayConfig *DisplayConfig) error {
    45  	primaries := []MPID{}
    46  	for mpid, display := range displayConfig.Displays {
    47  		if display.IsPrimary() {
    48  			primaries = append(primaries, mpid)
    49  		}
    50  	}
    51  
    52  	if len(primaries) > 1 {
    53  		return fmt.Errorf("there can only be one primary, found %d: %v", len(primaries), primaries)
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  // Validates DPMS configuration.
    60  //
    61  // Where these fields are set, they must be configured so
    62  // standby-time < suspend-time < off-time.
    63  //
    64  // If a field is not set, it is assumed to be 0.
    65  func validateDPMS(displayConfig *DisplayConfig) error {
    66  	if displayConfig.DPMS == nil {
    67  		return nil
    68  	}
    69  
    70  	var standybyTime, suspendTimeTime, offTimeTime int
    71  	if displayConfig.DPMS.StandbyTime != nil {
    72  		standybyTime = *displayConfig.DPMS.StandbyTime
    73  	}
    74  	if displayConfig.DPMS.SuspendTime != nil {
    75  		suspendTimeTime = *displayConfig.DPMS.SuspendTime
    76  	}
    77  	if displayConfig.DPMS.OffTime != nil {
    78  		offTimeTime = *displayConfig.DPMS.OffTime
    79  	}
    80  
    81  	if standybyTime > suspendTimeTime {
    82  		return errStandByLongerThanSuspendTime
    83  	} else if suspendTimeTime > offTimeTime {
    84  		return errSuspendLongerThanOffTime
    85  	}
    86  
    87  	return nil
    88  }
    89  

View as plain text